14#if defined ( __CC_ARM )
15#if (__ARMCC_VERSION < 400000)
18#pragma import _printf_widthprec
24#if defined(DEBUG_ENABLE_SEMIHOST)
30#define DEBUG_PORT UART0
34#if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 6040000)) || (defined(__ICCARM__) && (__VER__ >= 8000000))
35struct __FILE {
int handle; };
51 printf(
"r0 = 0x%x\n", stack[
r0]);
52 printf(
"r1 = 0x%x\n", stack[
r1]);
53 printf(
"r2 = 0x%x\n", stack[
r2]);
54 printf(
"r3 = 0x%x\n", stack[
r3]);
55 printf(
"r12 = 0x%x\n", stack[
r12]);
56 printf(
"lr = 0x%x\n", stack[
lr]);
57 printf(
"pc = 0x%x\n", stack[
pc]);
58 printf(
"psr = 0x%x\n", stack[
psr]);
69 printf(
"In Hard Fault Handler\n");
83#if defined(DEBUG_ENABLE_SEMIHOST)
86static char g_buf_len = 0;
90# if defined ( __GNUC__ ) && !(__CC_ARM) && !(__ICCARM__)
92# elif defined(__ICCARM__)
119int32_t SH_DoCommand(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
137void Get_LR_and_Branch(
void)
140 "B Hard_Fault_Handler \n"
150void Stack_Use_MSP(
void)
153 "B Get_LR_and_Branch \n"
163void HardFault_Handler_Ret(
void)
168 "BEQ Stack_Use_MSP \n"
170 "B Get_LR_and_Branch \n"
181void SP_Read_Ready(
void)
183 asm(
"LDR R1, [R0, #24] \n"
185 "LDR R2, [pc, #8] \n"
187 "BNE HardFault_Handler_Ret \n"
189 "STR R1, [R0, #24] \n"
192 "B HardFault_Handler_Ret \n"
206 "B Get_LR_and_Branch \n"
257 BNE HardFault_Handler_Ret
270 MRS R0, PSP ;stack use PSP
273 MRS R0, MSP ; stack use MSP
275 MOV R1, LR ; LR current
value
294__asm int32_t SH_DoCommand(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
321# if defined ( __GNUC__ ) && !(__CC_ARM) && !(__ICCARM__)
341 "B Hard_Fault_Handler \n"
344 "B Hard_Fault_Handler \n"
350# elif defined(__ICCARM__)
352void Get_LR_and_Branch(
void)
355 "B Hard_Fault_Handler \n"
359void Stack_Use_MSP(
void)
362 "B Get_LR_and_Branch \n"
381 "BEQ Stack_Use_MSP \n"
383 "B Get_LR_and_Branch \n"
431#ifndef NONBLOCK_PRINTF
449 static uint8_t u8Buf[BUF_SIZE] = {0};
450 static int32_t i32Head = 0;
451 static int32_t i32Tail = 0;
461 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
462 if(i32Tmp != i32Tail)
464 u8Buf[i32Head] =
'\r';
470 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
471 if(i32Tmp != i32Tail)
479 if(i32Tail == i32Head)
486 i32Tmp = i32Tail + 1;
487 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
496 }
while(i32Tail != i32Head);
513#if defined(DEBUG_ENABLE_SEMIHOST)
514 g_buf[g_buf_len++] = ch;
515 g_buf[g_buf_len] =
'\0';
516 if(g_buf_len + 1 >=
sizeof(g_buf) || ch ==
'\n' || ch ==
'\0')
519 if(SH_DoCommand(0x04, (
int)g_buf,
NULL) != 0)
529 for(i = 0; i < g_buf_len; i++)
553#ifdef DEBUG_ENABLE_SEMIHOST
554# if defined (__CC_ARM)
556 while(SH_DoCommand(0x101, 0, &nRet) != 0)
560 SH_DoCommand(0x07, 0, &nRet);
566 while(SH_DoCommand(0x7, 0, &nRet) != 0)
668#if defined (__GNUC__) && !defined(__ARMCC_VERSION)
670int _write (
int fd,
char *ptr,
int len)
690int _read (
int fd,
char *ptr,
int len)
737#ifdef DEBUG_ENABLE_SEMIHOST
739void __exit(
int return_code)
743 if(SH_DoCommand(0x18, 0x20026,
NULL) == 0)
752void _sys_exit(
int return_code)
756 if(SH_DoCommand(0x18, 0x20026,
NULL) == 0)
NuMicro peripheral access layer header file.
#define NULL
NULL pointer.
#define UART_FIFOSTS_TXFULL_Msk
#define UART_FIFOSTS_TXEMPTYF_Msk
#define UART_FIFOSTS_RXEMPTY_Msk
static void SendChar_ToUART(int ch)
Routine to send a char.
void _ttywrch(int ch)
C library retargetting.
__asm int32_t HardFault_Handler(void)
This HardFault handler is implemented to show r0, r1, r2, r3, r12, lr, pc, psr.
int fputc(int ch, FILE *stream)
Write character to stream.
static char GetChar(void)
Routine to get a char.
int ferror(FILE *stream)
Check error indicator.
int IsDebugFifoEmpty(void)
Check if debug message finished.
int kbhit(void)
Check any char input from UART.
int fgetc(FILE *stream)
Get character from UART debug port or semihosting input.
void Hard_Fault_Handler(uint32_t stack[])
Hard fault handler.
static void SendChar(int ch)
Routine to send a char.
static void stackDump(uint32_t stack[])
Helper function to dump register while hard fault occurred.