14 #define __WEAK __attribute__((weak))
17 #define __NO_RETURN __attribute__((noreturn))
20#if(defined(__ICCARM__) && (__VER__ >= 9020000))
21#include <LowLevelIOInterface.h>
24#if defined (__ICCARM__)
25#pragma diag_suppress=Pm150
28#if defined ( __CC_ARM )
29#if (__ARMCC_VERSION < 400000)
32#pragma import _printf_widthprec
39#if defined(DEBUG_ENABLE_SEMIHOST)
46#define DEBUG_PORT UART0
51#if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 6040000)) || (defined(__ICCARM__) && (__VER__ >= 8000000))
52struct __FILE {
int handle; };
66#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
70void __aeabi_assert(
const char* expr,
const char* file,
int line)
74 fputs(
"*** assertion failed: ", stderr);
76 fputs(
", file ", stderr);
78 fputs(
", line ", stderr);
80 p = str +
sizeof(str);
85 *--p =
'0' + (line % 10);
101__asm(
" .global __ARM_use_no_argv\n");
102__asm(
" .global __use_no_semihosting\n");
110void _sys_exit(
int return_code)
122#if defined(__ICCARM__)
123#ifndef DEBUG_ENABLE_SEMIHOST
124size_t __write(
int handle,
const unsigned char *buf,
size_t bufSize)
136 if (handle != 1 && handle != 2)
141 for (; bufSize > 0; --bufSize)
151size_t __read(
int handle,
unsigned char* buf,
size_t bufSize)
160 for( ; bufSize > 0; --bufSize)
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);
183#if defined(DEBUG_ENABLE_SEMIHOST)
185static char g_buf[16];
186static char g_buf_len = 0;
187static volatile int32_t g_ICE_Conneced = 1;
201 sp = (uint32_t *)psp;
203 sp = (uint32_t *)msp;
206#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
211 sp = (uint32_t *)__TZ_get_PSP_NS();
213 sp = (uint32_t *)__TZ_get_MSP_NS();
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]);
264int32_t
SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
269 *pn32Out_R0 = n32In_R0;
289 int32_t
SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0);
294 uint32_t inst, addr, taddr, tdata;
296 uint32_t rm, rn, rt, imm5, imm8;
303 secure = (lr & 0x40ul) ? 1 : 0;
309 sp = (uint32_t *)psp;
313 sp = (uint32_t *)msp;
317#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3)
322 sp = (uint32_t *)(__TZ_get_PSP_NS());
324 sp = (uint32_t *)(__TZ_get_MSP_NS());
340 printf(
"HardFault @ 0x%08x\n", sp[6]);
348 printf(
"HardFault Analysis:\n");
350 printf(
"Instruction code = %x\n", inst);
354 printf(
"Execute BKPT without ICE connected\n");
356 else if((inst >> 12) == 5)
359 rm = (inst >> 6) & 0x7;
360 rn = (inst >> 3) & 0x7;
363 printf(
"LDR/STR rt=%x rm=%x rn=%x\n", rt, rm, rn);
364 taddr = sp[rn] + sp[rm];
366 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
367 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
370 else if((inst >> 13) == 3)
373 imm5 = (inst >> 6) & 0x1f;
374 rn = (inst >> 3) & 0x7;
377 printf(
"LDR/STR rt=%x rn=%x imm5=%x\n", rt, rn, imm5);
378 taddr = sp[rn] + imm5;
380 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
381 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
383 else if((inst >> 12) == 8)
386 imm5 = (inst >> 6) & 0x1f;
387 rn = (inst >> 3) & 0x7;
390 printf(
"LDRH/STRH rt=%x rn=%x imm5=%x\n", rt, rn, imm5);
391 taddr = sp[rn] + imm5;
393 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
394 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
397 else if((inst >> 12) == 9)
401 rt = (inst >> 8) & 0x7;
403 printf(
"LDRH/STRH rt=%x imm8=%x\n", rt, imm8);
404 taddr = sp[6] + imm8;
406 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
407 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
411 printf(
"Unexpected instruction\n");
420int32_t
SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
438#ifndef NONBLOCK_PRINTF
455 static uint8_t u8Buf[BUF_SIZE] = {0};
456 static int32_t i32Head = 0;
457 static int32_t i32Tail = 0;
467 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
468 if(i32Tmp != i32Tail)
470 u8Buf[i32Head] =
'\r';
476 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
477 if(i32Tmp != i32Tail)
485 if(i32Tail == i32Head)
492 i32Tmp = i32Tail + 1;
493 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
502 }
while(i32Tail != i32Head);
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')
526 if(SH_DoCommand(0x04, (
int)g_buf,
NULL) != 0)
534# if (DEBUG_ENABLE_SEMIHOST == 2)
536 for(i = 0; i < g_buf_len; i++)
562#ifdef DEBUG_ENABLE_SEMIHOST
564# if defined (__ICCARM__)
565 while(SH_DoCommand(0x7, 0, &nRet) != 0)
571 while(SH_DoCommand(0x101, 0, &nRet) != 0)
575 SH_DoCommand(0x07, 0, &nRet);
581# if (DEBUG_ENABLE_SEMIHOST == 2)
583 while(!g_ICE_Conneced)
692#if defined (__GNUC__) && !defined(__ARMCC_VERSION)
693#if !defined(OS_USE_SEMIHOSTING)
694int _write(
int fd,
char *ptr,
int len)
711int _read(
int fd,
char *ptr,
int len)
765#ifdef DEBUG_ENABLE_SEMIHOST
768void __exit(
int return_code)
771 if(SH_DoCommand(0x18, 0x20026,
NULL) == 0)
780void _sys_exit(
int return_code)
784 if(SH_DoCommand(0x18, 0x20026,
NULL) == 0)
#define UART_FIFOSTS_TXFULL_Msk
#define UART_FIFOSTS_TXEMPTYF_Msk
#define UART_FIFOSTS_RXEMPTY_Msk
NuMicro peripheral access layer header file.
void *__dso_handle __attribute__((weak))
A callback called by library while smartcard request for a time extension.
int32_t SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
This function is called by Hardfault handler.
void _ttywrch(int ch)
C library retargetting.
int fputc(int ch, FILE *stream)
Write character to stream.
int ferror(FILE *stream)
Check error indicator.
int IsDebugFifoEmpty(void)
Check if debug message finished.
void SendChar_ToUART(int ch)
Routine to send a char.
__WEAK uint32_t ProcessHardFault(uint32_t lr, uint32_t msp, uint32_t psp)
int kbhit(void)
Check any char input from UART.
int fgetc(FILE *stream)
Get character from UART debug port or semihosting input.
void SendChar(int ch)
Routine to send a char.
char GetChar(void)
Routine to get a char.