M471M/R1/S BSP V3.02.000
The Board Support Package for M471M/R1/S
retarget.c
Go to the documentation of this file.
1/**************************************************************************/
10#include <stdio.h>
11#include "NuMicro.h"
12
13#ifndef __WEAK
14 #define __WEAK __attribute__((weak))
15#endif
16#ifndef __NO_RETURN
17 #define __NO_RETURN __attribute__((noreturn))
18#endif
19
20#if(defined(__ICCARM__) && (__VER__ >= 9020000))
21#include <LowLevelIOInterface.h>
22#endif
23
24#if defined (__ICCARM__)
25#pragma diag_suppress=Pm150
26#endif
27
28#if defined ( __CC_ARM )
29#if (__ARMCC_VERSION < 400000)
30#else
31/* Insist on keeping widthprec, to avoid X propagation by benign code in C-lib */
32#pragma import _printf_widthprec
33#endif
34#endif
35
36/* Uncomment this line to disable all printf and getchar. getchar() will always return 0x00*/
37/* #define DISABLE_UART */
38
39#if defined(DEBUG_ENABLE_SEMIHOST)
40 #ifndef DISABLE_UART
41 #define DISABLE_UART
42 #endif
43#endif
44
45
46#define DEBUG_PORT UART0
47
48/*---------------------------------------------------------------------------------------------------------*/
49/* Global variables */
50/*---------------------------------------------------------------------------------------------------------*/
51#if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 6040000)) || (defined(__ICCARM__) && (__VER__ >= 8000000))
52struct __FILE { int handle; /* Add whatever you need here */ };
53#endif
54
57
58int kbhit(void);
59int IsDebugFifoEmpty(void);
60void _ttywrch(int ch);
61
62char GetChar(void);
63void SendChar_ToUART(int ch);
64void SendChar(int ch);
65
66#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
67# ifdef __MICROLIB
68
70void __aeabi_assert(const char* expr, const char* file, int line)
71{
72 char str[12], * p;
73
74 fputs("*** assertion failed: ", stderr);
75 fputs(expr, stderr);
76 fputs(", file ", stderr);
77 fputs(file, stderr);
78 fputs(", line ", stderr);
79
80 p = str + sizeof(str);
81 *--p = '\0';
82 *--p = '\n';
83 while(line > 0)
84 {
85 *--p = '0' + (line % 10);
86 line /= 10;
87 }
88 fputs(p, stderr);
89
90 for(;;);
91}
92
93
95void abort(void)
96{
97 for(;;);
98}
99
100# else
101__asm(" .global __ARM_use_no_argv\n");
102__asm(" .global __use_no_semihosting\n");
103
104
105FILE __stdout;
106FILE __stdin;
107FILE __stderr;
108
109void _sys_exit(int return_code)__attribute__((noreturn));
110void _sys_exit(int return_code)
111{
112 (void) return_code;
113 while(1);
114}
115
116# endif
117#endif // defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
118
119/*---------------------------------------------------------------------------------------------------------*/
120/* Routine to write a char */
121/*---------------------------------------------------------------------------------------------------------*/
122#if defined(__ICCARM__)
123#ifndef DEBUG_ENABLE_SEMIHOST
124size_t __write(int handle, const unsigned char *buf, size_t bufSize)
125{
126 size_t nChars = 0;
127
128 /* Check for the command to flush all handles */
129 if (handle == -1)
130 {
131 return 0;
132 }
133
134 /* Check for stdout and stderr (only necessary if FILE descriptors are enabled.) */
135
136 if (handle != 1 && handle != 2)
137 {
138 return -1;
139 }
140
141 for (/* Empty */; bufSize > 0; --bufSize)
142 {
143 SendChar(*buf);
144 ++buf;
145 ++nChars;
146 }
147
148 return nChars;
149}
150
151size_t __read(int handle, unsigned char* buf, size_t bufSize)
152{
153 size_t nChars = 0;
154 /* Check for stdin (only necessary if FILE descriptors are enabled) */
155 if(handle != 0)
156 {
157 return -1;
158 }
159
160 for( ; bufSize > 0; --bufSize)
161 {
162 unsigned char c;
163 c = GetChar();
164 if(c == 0)
165 break;
166 *buf++ = c;
167 ++nChars;
168 }
169 return nChars;
170}
171#endif /* ndef DEBUG_ENABLE_SEMIHOST */
172#endif /* defined(__ICCARM__) */
173
174#if (defined(__ARMCC_VERSION) || defined(__ICCARM__))
175extern int32_t SH_DoCommand(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0);
176int fgetc(FILE* stream);
177int fputc(int ch, FILE* stream);
178int ferror(FILE* stream);
179__WEAK
180uint32_t ProcessHardFault(uint32_t lr, uint32_t msp, uint32_t psp);
181#endif
182
183#if defined(DEBUG_ENABLE_SEMIHOST)
184/* The static buffer is used to speed up the semihost */
185static char g_buf[16];
186static char g_buf_len = 0;
187static volatile int32_t g_ICE_Conneced = 1;
188
189void _sys_exit(int return_code)__attribute__((noreturn));
190
191uint32_t ProcessHardFault(uint32_t lr, uint32_t msp, uint32_t psp)
192{
193 uint32_t *sp = NULL;
194 uint32_t inst;
195
196 /* Check the used stack */
197 if(lr & 0x40)
198 {
199 /* Secure stack used */
200 if(lr & 4)
201 sp = (uint32_t *)psp;
202 else
203 sp = (uint32_t *)msp;
204
205 }
206#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
207 else
208 {
209 /* Non-secure stack used */
210 if(lr & 4)
211 sp = (uint32_t *)__TZ_get_PSP_NS();
212 else
213 sp = (uint32_t *)__TZ_get_MSP_NS();
214
215 }
216#endif
217
218 /* Get the instruction caused the hardfault */
219 if( sp != NULL )
220 inst = M16(sp[6]);
221
222
223 if(inst == 0xBEAB)
224 {
225 /*
226 If the instruction is 0xBEAB, it means it is caused by BKPT without ICE connected.
227 We still return for output/input message to UART.
228 */
229 g_ICE_Conneced = 0; // Set a flag for ICE offline
230 sp[6] += 2; // return to next instruction
231 return lr; // Keep lr in R0
232 }
233
234 /* It is casued by hardfault (Not semihost). Just process the hard fault here. */
235 /* TODO: Implement your hardfault handle code here */
236
237
238 printf(" HardFault!\n\n");
239 printf("r0 = 0x%x\n", sp[0]);
240 printf("r1 = 0x%x\n", sp[1]);
241 printf("r2 = 0x%x\n", sp[2]);
242 printf("r3 = 0x%x\n", sp[3]);
243 printf("r12 = 0x%x\n", sp[4]);
244 printf("lr = 0x%x\n", sp[5]);
245 printf("pc = 0x%x\n", sp[6]);
246 printf("psr = 0x%x\n", sp[7]);
247
248
249 while(1) {}
250
251}
252
264int32_t SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
265{
266 if(g_ICE_Conneced)
267 {
268 if(pn32Out_R0)
269 *pn32Out_R0 = n32In_R0;
270
271 return 1;
272 }
273 return 0;
274}
275
276#else /* ndef (DEBUG_ENABLE_SEMIHOST) */
277
289 int32_t SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0);
290
291__WEAK uint32_t ProcessHardFault(uint32_t lr, uint32_t msp, uint32_t psp)
292{
293 uint32_t *sp = NULL;
294 uint32_t inst, addr, taddr, tdata;
295 int32_t secure;
296 uint32_t rm, rn, rt, imm5, imm8;
297
298 /* It is casued by hardfault. Just process the hard fault */
299 /* TODO: Implement your hardfault handle code here */
300
301
302 /* Check the used stack */
303 secure = (lr & 0x40ul) ? 1 : 0;
304 if(secure)
305 {
306 /* Secure stack used */
307 if(lr & 4UL)
308 {
309 sp = (uint32_t *)psp;
310 }
311 else
312 {
313 sp = (uint32_t *)msp;
314 }
315
316 }
317#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3)
318 else
319 {
320 /* Non-secure stack used */
321 if(lr & 4)
322 sp = (uint32_t *)(__TZ_get_PSP_NS());
323 else
324 sp = (uint32_t *)(__TZ_get_MSP_NS());
325
326 }
327#endif
328
329 /*
330 r0 = sp[0]
331 r1 = sp[1]
332 r2 = sp[2]
333 r3 = sp[3]
334 r12 = sp[4]
335 lr = sp[5]
336 pc = sp[6]
337 psr = sp[7]
338 */
339
340 printf("HardFault @ 0x%08x\n", sp[6]);
341 /* Get the instruction caused the hardfault */
342 if( sp != NULL )
343 {
344 addr = sp[6];
345 inst = M16(addr);
346 }
347
348 printf("HardFault Analysis:\n");
349
350 printf("Instruction code = %x\n", inst);
351
352 if(inst == 0xBEAB)
353 {
354 printf("Execute BKPT without ICE connected\n");
355 }
356 else if((inst >> 12) == 5)
357 {
358 /* 0101xx Load/store (register offset) on page C2-327 of armv8m ref */
359 rm = (inst >> 6) & 0x7;
360 rn = (inst >> 3) & 0x7;
361 rt = inst & 0x7;
362
363 printf("LDR/STR rt=%x rm=%x rn=%x\n", rt, rm, rn);
364 taddr = sp[rn] + sp[rm];
365 tdata = sp[rt];
366 printf("[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
367 (inst & BIT11) ? "LDR" : "STR", tdata, taddr);
368
369 }
370 else if((inst >> 13) == 3)
371 {
372 /* 011xxx Load/store word/byte (immediate offset) on page C2-327 of armv8m ref */
373 imm5 = (inst >> 6) & 0x1f;
374 rn = (inst >> 3) & 0x7;
375 rt = inst & 0x7;
376
377 printf("LDR/STR rt=%x rn=%x imm5=%x\n", rt, rn, imm5);
378 taddr = sp[rn] + imm5;
379 tdata = sp[rt];
380 printf("[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
381 (inst & BIT11) ? "LDR" : "STR", tdata, taddr);
382 }
383 else if((inst >> 12) == 8)
384 {
385 /* 1000xx Load/store halfword (immediate offset) on page C2-328 */
386 imm5 = (inst >> 6) & 0x1f;
387 rn = (inst >> 3) & 0x7;
388 rt = inst & 0x7;
389
390 printf("LDRH/STRH rt=%x rn=%x imm5=%x\n", rt, rn, imm5);
391 taddr = sp[rn] + imm5;
392 tdata = sp[rt];
393 printf("[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
394 (inst & BIT11) ? "LDR" : "STR", tdata, taddr);
395
396 }
397 else if((inst >> 12) == 9)
398 {
399 /* 1001xx Load/store (SP-relative) on page C2-328 */
400 imm8 = inst & 0xff;
401 rt = (inst >> 8) & 0x7;
402
403 printf("LDRH/STRH rt=%x imm8=%x\n", rt, imm8);
404 taddr = sp[6] + imm8;
405 tdata = sp[rt];
406 printf("[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
407 (inst & BIT11) ? "LDR" : "STR", tdata, taddr);
408 }
409 else
410 {
411 printf("Unexpected instruction\n");
412 }
413
414 /* Or *sp to remove compiler warning */
415 while(1U | *sp) {}
416
417 return lr;
418}
419
420int32_t SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
421{
422 return 0;
423}
424
425#endif /* defined(DEBUG_ENABLE_SEMIHOST) */
426
427
428#ifndef DISABLE_UART
438#ifndef NONBLOCK_PRINTF
439void SendChar_ToUART(int ch)
440{
441 while(DEBUG_PORT->FIFOSTS & UART_FIFOSTS_TXFULL_Msk);
442
443 if(ch == '\n')
444 {
445 DEBUG_PORT->DAT = '\r';
446 while(DEBUG_PORT->FIFOSTS & UART_FIFOSTS_TXFULL_Msk);
447 }
448 DEBUG_PORT->DAT = ch;
449}
450#else
451/* Non-block implement of send char */
452#define BUF_SIZE 2048
453static void SendChar_ToUART(int ch)
454{
455 static uint8_t u8Buf[BUF_SIZE] = {0};
456 static int32_t i32Head = 0;
457 static int32_t i32Tail = 0;
458 int32_t i32Tmp;
459
460 /* Only flush the data in buffer to UART when ch == 0 */
461 if(ch)
462 {
463 /* Push char */
464 if(ch == '\n')
465 {
466 i32Tmp = i32Head+1;
467 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
468 if(i32Tmp != i32Tail)
469 {
470 u8Buf[i32Head] = '\r';
471 i32Head = i32Tmp;
472 }
473 }
474
475 i32Tmp = i32Head+1;
476 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
477 if(i32Tmp != i32Tail)
478 {
479 u8Buf[i32Head] = ch;
480 i32Head = i32Tmp;
481 }
482 }
483 else
484 {
485 if(i32Tail == i32Head)
486 return;
487 }
488
489 /* pop char */
490 do
491 {
492 i32Tmp = i32Tail + 1;
493 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
494
495 if((DEBUG_PORT->FIFOSTS & UART_FIFOSTS_TXFULL_Msk) == 0)
496 {
497 DEBUG_PORT->DAT = u8Buf[i32Tail];
498 i32Tail = i32Tmp;
499 }
500 else
501 break; /* FIFO full */
502 }while(i32Tail != i32Head);
503}
504#endif /* else for NONBLOCK_PRINTF */
505#endif /* if not def DISABLE_UART */
506
516void SendChar(int ch)
517{
518#if defined(DEBUG_ENABLE_SEMIHOST)
519 g_buf[g_buf_len++] = ch;
520 g_buf[g_buf_len] = '\0';
521 if(g_buf_len + 1 >= sizeof(g_buf) || ch == '\n' || ch == '\0')
522 {
523 /* Send the char */
524 if(g_ICE_Conneced)
525 {
526 if(SH_DoCommand(0x04, (int)g_buf, NULL) != 0)
527 {
528 g_buf_len = 0;
529 return;
530 }
531 }
532 else
533 {
534# if (DEBUG_ENABLE_SEMIHOST == 2) // Re-direct to UART Debug Port only when DEBUG_ENABLE_SEMIHOST=2
535 int i;
536 for(i = 0; i < g_buf_len; i++)
537 SendChar_ToUART(g_buf[i]);
538 g_buf_len = 0;
539# endif
540 }
541 }
542#else
543
544#ifndef DISABLE_UART
545 SendChar_ToUART(ch);
546#endif
547
548#endif
549}
550
560char GetChar(void)
561{
562#ifdef DEBUG_ENABLE_SEMIHOST
563 int nRet;
564# if defined (__ICCARM__)
565 while(SH_DoCommand(0x7, 0, &nRet) != 0)
566 {
567 if(nRet != 0)
568 return (char)nRet;
569 }
570# else
571 while(SH_DoCommand(0x101, 0, &nRet) != 0)
572 {
573 if(nRet != 0)
574 {
575 SH_DoCommand(0x07, 0, &nRet);
576 return (char)nRet;
577 }
578 }
579# endif
580
581# if (DEBUG_ENABLE_SEMIHOST == 2) // Re-direct to UART Debug Port only when DEBUG_ENABLE_SEMIHOST=2
582 /* Use debug port when ICE is not connected at semihost mode */
583 while(!g_ICE_Conneced)
584 {
585 if((DEBUG_PORT->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk) == 0)
586 {
587 return (DEBUG_PORT->DAT);
588 }
589 }
590# endif
591
592 return (0);
593#else
594
595#ifndef DISABLE_UART
596 while(1)
597 {
598 if((DEBUG_PORT->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk) == 0)
599 {
600 return (DEBUG_PORT->DAT);
601 }
602 }
603#else
604 return 0;
605#endif
606
607#endif
608}
609
621int kbhit(void)
622{
623#ifndef DISABLE_UART
624 return !((DEBUG_PORT->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk) == 0);
625#else
626 return 1;
627#endif
628}
641{
642#ifndef DISABLE_UART
643 return ((DEBUG_PORT->FIFOSTS & UART_FIFOSTS_TXEMPTYF_Msk) != 0);
644#else
645 return 1;
646#endif
647}
648
659void _ttywrch(int ch)
660{
661 SendChar(ch);
662 return;
663}
664
665
684int fputc(int ch, FILE *stream)
685{
686 (void)stream;
687 SendChar(ch);
688 return ch;
689}
690
691
692#if defined (__GNUC__) && !defined(__ARMCC_VERSION)
693#if !defined(OS_USE_SEMIHOSTING)
694int _write(int fd, char *ptr, int len)
695{
696 int i = len;
697 while(i--)
698 {
699 if(*ptr == '\n')
700 {
701 while(DEBUG_PORT->FIFOSTS & UART_FIFOSTS_TXFULL_Msk);
702 DEBUG_PORT->DAT = '\r';
703 }
704
705 while(DEBUG_PORT->FIFOSTS & UART_FIFOSTS_TXFULL_Msk);
706 DEBUG_PORT->DAT = *ptr++;
707 }
708 return len;
709}
710
711int _read(int fd, char *ptr, int len)
712{
713
714 while((DEBUG_PORT->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk) != 0);
715 *ptr = DEBUG_PORT->DAT;
716 return 1;
717
718
719}
720#endif
721
722#else
723
735int fgetc(FILE *stream)
736{
737 (void)stream;
738 return ((int)GetChar());
739}
740
756int ferror(FILE *stream)
757{
758 (void)stream;
759 return EOF;
760}
761
762#endif
763
764
765#ifdef DEBUG_ENABLE_SEMIHOST
766
767# ifdef __ICCARM__
768void __exit(int return_code)
769{
770 /* Check if link with ICE */
771 if(SH_DoCommand(0x18, 0x20026, NULL) == 0)
772 {
773 /* Make sure all message is print out */
774 while(IsDebugFifoEmpty() == 0);
775 }
776label:
777 goto label; /* endless loop */
778}
779# else
780void _sys_exit(int return_code)
781{
782 (void)return_code;
783 /* Check if link with ICE */
784 if(SH_DoCommand(0x18, 0x20026, NULL) == 0)
785 {
786 /* Make sure all message is print out */
787 while(IsDebugFifoEmpty() == 0);
788 }
789label:
790 goto label; /* endless loop */
791}
792# endif // ifdef __ICCARM__
793
794#endif /* ifdef DEBUG_ENABLE_SEMIHOST */
#define UART_FIFOSTS_TXFULL_Msk
Definition: M471M_R1_S.h:12041
#define UART_FIFOSTS_TXEMPTYF_Msk
Definition: M471M_R1_S.h:12047
#define UART_FIFOSTS_RXEMPTY_Msk
Definition: M471M_R1_S.h:12029
NuMicro peripheral access layer header file.
void *__dso_handle __attribute__((weak))
A callback called by library while smartcard request for a time extension.
Definition: _syscalls.c:35
#define NULL
Definition: M471M_R1_S.h:13908
#define BIT11
Definition: M471M_R1_S.h:13867
#define M16(adr)
Definition: M471M_R1_S.h:13936
int32_t SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
This function is called by Hardfault handler.
Definition: retarget.c:420
#define __NO_RETURN
Definition: retarget.c:17
void _ttywrch(int ch)
C library retargetting.
Definition: retarget.c:659
int fputc(int ch, FILE *stream)
Write character to stream.
Definition: retarget.c:684
int ferror(FILE *stream)
Check error indicator.
Definition: retarget.c:756
int IsDebugFifoEmpty(void)
Check if debug message finished.
Definition: retarget.c:640
void SendChar_ToUART(int ch)
Routine to send a char.
Definition: retarget.c:439
__WEAK uint32_t ProcessHardFault(uint32_t lr, uint32_t msp, uint32_t psp)
Definition: retarget.c:291
int kbhit(void)
Check any char input from UART.
Definition: retarget.c:621
int fgetc(FILE *stream)
Get character from UART debug port or semihosting input.
Definition: retarget.c:735
FILE __stdin
Definition: retarget.c:56
void SendChar(int ch)
Routine to send a char.
Definition: retarget.c:516
FILE __stdout
Definition: retarget.c:55
#define __WEAK
Definition: retarget.c:14
char GetChar(void)
Routine to get a char.
Definition: retarget.c:560
#define DEBUG_PORT
Definition: retarget.c:46