15 #define __WEAK __attribute__((weak))
18#if(defined(__ICCARM__) && (__VER__ >= 9020000))
19#include <LowLevelIOInterface.h>
22#if defined (__ICCARM__)
23#pragma diag_suppress=Pm150
26#if defined ( __CC_ARM )
27#if (__ARMCC_VERSION < 400000)
30#pragma import _printf_widthprec
37#if defined(DEBUG_ENABLE_SEMIHOST)
44#define DEBUG_PORT UART0
49#if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 6040000)) || (defined(__ICCARM__) && (__VER__ >= 8000000))
50struct __FILE {
int handle; };
64#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
68void __aeabi_assert(
const char* expr,
const char* file,
int line)
72 fputs(
"*** assertion failed: ", stderr);
74 fputs(
", file ", stderr);
76 fputs(
", line ", stderr);
78 p = str +
sizeof(str);
83 *--p =
'0' + (line % 10);
99__asm(
" .global __ARM_use_no_argv\n");
100__asm(
" .global __use_no_semihosting\n");
108void _sys_exit(
int return_code)
120#if defined(__ICCARM__)
121#ifndef DEBUG_ENABLE_SEMIHOST
122size_t __write(
int handle,
const unsigned char *buf,
size_t bufSize)
134 if (handle != 1 && handle != 2)
139 for (; bufSize > 0; --bufSize)
149size_t __read(
int handle,
unsigned char* buf,
size_t bufSize)
158 for( ; bufSize > 0; --bufSize)
172#if (defined(__ARMCC_VERSION) || defined(__ICCARM__))
173extern int32_t SH_DoCommand(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0);
175int fgetc(FILE* stream);
176int fputc(
int ch, FILE* stream);
182#if defined(DEBUG_ENABLE_SEMIHOST)
184static char g_buf[16];
185static char g_buf_len = 0;
186static volatile int32_t g_ICE_Conneced = 1;
200 sp = (uint32_t *)psp;
202 sp = (uint32_t *)msp;
205#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
210 sp = (uint32_t *)__TZ_get_PSP_NS();
212 sp = (uint32_t *)__TZ_get_MSP_NS();
263int32_t
SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
268 *pn32Out_R0 = n32In_R0;
277int32_t
SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0);
293 uint32_t inst, addr, taddr, tdata;
295 uint32_t rm, rn, rt, imm5, imm8;
302 secure = (
lr & 0x40ul) ? 1 : 0;
308 sp = (uint32_t *)psp;
312 sp = (uint32_t *)msp;
316#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3)
321 sp = (uint32_t *)(__TZ_get_PSP_NS());
323 sp = (uint32_t *)(__TZ_get_MSP_NS());
339 printf(
"HardFault @ 0x%08x\n", sp[6]);
347 printf(
"HardFault Analysis:\n");
349 printf(
"Instruction code = %x\n", inst);
353 printf(
"Execute BKPT without ICE connected\n");
355 else if((inst >> 12) == 5)
358 rm = (inst >> 6) & 0x7;
359 rn = (inst >> 3) & 0x7;
362 printf(
"LDR/STR rt=%x rm=%x rn=%x\n", rt, rm, rn);
363 taddr = sp[rn] + sp[rm];
365 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
366 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
369 else if((inst >> 13) == 3)
372 imm5 = (inst >> 6) & 0x1f;
373 rn = (inst >> 3) & 0x7;
376 printf(
"LDR/STR rt=%x rn=%x imm5=%x\n", rt, rn, imm5);
377 taddr = sp[rn] + imm5;
379 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
380 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
382 else if((inst >> 12) == 8)
385 imm5 = (inst >> 6) & 0x1f;
386 rn = (inst >> 3) & 0x7;
389 printf(
"LDRH/STRH rt=%x rn=%x imm5=%x\n", rt, rn, imm5);
390 taddr = sp[rn] + imm5;
392 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
393 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
396 else if((inst >> 12) == 9)
400 rt = (inst >> 8) & 0x7;
402 printf(
"LDRH/STRH rt=%x imm8=%x\n", rt, imm8);
403 taddr = sp[6] + imm8;
405 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
406 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
410 printf(
"Unexpected instruction\n");
419int32_t
SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
437#ifndef NONBLOCK_PRINTF
454 static uint8_t u8Buf[BUF_SIZE] = {0};
455 static int32_t i32Head = 0;
456 static int32_t i32Tail = 0;
466 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
467 if(i32Tmp != i32Tail)
469 u8Buf[i32Head] =
'\r';
475 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
476 if(i32Tmp != i32Tail)
484 if(i32Tail == i32Head)
491 i32Tmp = i32Tail + 1;
492 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
501 }
while(i32Tail != i32Head);
517#if defined(DEBUG_ENABLE_SEMIHOST)
518 g_buf[g_buf_len++] = ch;
519 g_buf[g_buf_len] =
'\0';
520 if(g_buf_len + 1 >=
sizeof(g_buf) || ch ==
'\n' || ch ==
'\0')
525 if(SH_DoCommand(0x04, (
int)g_buf,
NULL) != 0)
533# if (DEBUG_ENABLE_SEMIHOST == 2)
535 for(i = 0; i < g_buf_len; i++)
561#ifdef DEBUG_ENABLE_SEMIHOST
565# if defined (__ICCARM__)
566 while(SH_DoCommand(0x7, 0, &nRet) != 0)
572 while(SH_DoCommand(0x101, 0, &nRet) != 0)
576 SH_DoCommand(0x07, 0, &nRet);
582# if (DEBUG_ENABLE_SEMIHOST == 2)
584 while(!g_ICE_Conneced)
693#if defined (__GNUC__) && !defined(__ARMCC_VERSION)
694#if !defined(OS_USE_SEMIHOSTING)
695int _write(
int fd,
char *ptr,
int len)
712int _read(
int fd,
char *ptr,
int len)
727int _lseek(
int file,
int ptr,
int dir)
731int _fstat(
int file,
struct stat *st)
733 st->st_mode = S_IFCHR;
740int _kill(
int pid,
int sig)
753int _unlink(
const char *path)
803#ifdef DEBUG_ENABLE_SEMIHOST
806void __exit(
int return_code)
809 if(SH_DoCommand(0x18, 0x20026,
NULL) == 0)
818void _sys_exit(
int return_code)
822 if(SH_DoCommand(0x18, 0x20026,
NULL) == 0)
void *__dso_handle __attribute__((weak))
NuMicro peripheral access layer header file.
#define M16(addr)
Get a 16-bit unsigned value from specified address.
#define NULL
NULL pointer.
#define BIT11
Bit 11 mask of an 32 bit integer.
#define UART_FIFOSTS_TXFULL_Msk
#define UART_FIFOSTS_TXEMPTYF_Msk
#define UART_FIFOSTS_RXEMPTY_Msk
int32_t SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
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)
This function is called by Hardfault handler.
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.