15 #define __WEAK __attribute__((weak))
18 #define __NO_RETURN __attribute__((noreturn))
21#if(defined(__ICCARM__) && (__VER__ >= 9020000))
22#include <LowLevelIOInterface.h>
25#if defined (__ICCARM__)
26#pragma diag_suppress=Pm150
29#if defined ( __CC_ARM )
30#if (__ARMCC_VERSION < 400000)
33#pragma import _printf_widthprec
40#if defined(DEBUG_ENABLE_SEMIHOST)
47#define DEBUG_PORT UART0
52#if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 6040000)) || (defined(__ICCARM__) && (__VER__ >= 8000000))
53struct __FILE {
int handle; };
67#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
71void __aeabi_assert(
const char* expr,
const char* file,
int line)
75 fputs(
"*** assertion failed: ", stderr);
77 fputs(
", file ", stderr);
79 fputs(
", line ", stderr);
81 p = str +
sizeof(str);
86 *--p =
'0' + (line % 10);
102__asm(
" .global __ARM_use_no_argv\n");
103__asm(
" .global __use_no_semihosting\n");
111void _sys_exit(
int return_code)
123#if defined(__ICCARM__)
124#ifndef DEBUG_ENABLE_SEMIHOST
125size_t __write(
int handle,
const unsigned char *buf,
size_t bufSize)
137 if (handle != 1 && handle != 2)
142 for (; bufSize > 0; --bufSize)
152size_t __read(
int handle,
unsigned char* buf,
size_t bufSize)
161 for( ; bufSize > 0; --bufSize)
175#if (defined(__ARMCC_VERSION) || defined(__ICCARM__))
176extern int32_t SH_DoCommand(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0);
177int fgetc(FILE* stream);
178int fputc(
int ch, FILE* stream);
184#if defined(DEBUG_ENABLE_SEMIHOST)
186static char g_buf[16];
187static char g_buf_len = 0;
188static volatile int32_t g_ICE_Conneced = 1;
202 sp = (uint32_t *)psp;
204 sp = (uint32_t *)msp;
207#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
212 sp = (uint32_t *)__TZ_get_PSP_NS();
214 sp = (uint32_t *)__TZ_get_MSP_NS();
239 printf(
" HardFault!\n\n");
240 printf(
"r0 = 0x%x\n", sp[0]);
241 printf(
"r1 = 0x%x\n", sp[1]);
242 printf(
"r2 = 0x%x\n", sp[2]);
243 printf(
"r3 = 0x%x\n", sp[3]);
244 printf(
"r12 = 0x%x\n", sp[4]);
245 printf(
"lr = 0x%x\n", sp[5]);
246 printf(
"pc = 0x%x\n", sp[6]);
247 printf(
"psr = 0x%x\n", sp[7]);
265int32_t
SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
270 *pn32Out_R0 = n32In_R0;
290 int32_t
SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0);
295 uint32_t inst, addr, taddr, tdata;
297 uint32_t rm, rn, rt, imm5, imm8;
304 secure = (
lr & 0x40ul) ? 1 : 0;
310 sp = (uint32_t *)psp;
314 sp = (uint32_t *)msp;
318#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3)
323 sp = (uint32_t *)(__TZ_get_PSP_NS());
325 sp = (uint32_t *)(__TZ_get_MSP_NS());
341 printf(
"HardFault @ 0x%08x\n", sp[6]);
349 printf(
"HardFault Analysis:\n");
351 printf(
"Instruction code = %x\n", inst);
355 printf(
"Execute BKPT without ICE connected\n");
357 else if((inst >> 12) == 5)
360 rm = (inst >> 6) & 0x7;
361 rn = (inst >> 3) & 0x7;
364 printf(
"LDR/STR rt=%x rm=%x rn=%x\n", rt, rm, rn);
365 taddr = sp[rn] + sp[rm];
367 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
368 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
371 else if((inst >> 13) == 3)
374 imm5 = (inst >> 6) & 0x1f;
375 rn = (inst >> 3) & 0x7;
378 printf(
"LDR/STR rt=%x rn=%x imm5=%x\n", rt, rn, imm5);
379 taddr = sp[rn] + imm5;
381 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
382 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
384 else if((inst >> 12) == 8)
387 imm5 = (inst >> 6) & 0x1f;
388 rn = (inst >> 3) & 0x7;
391 printf(
"LDRH/STRH rt=%x rn=%x imm5=%x\n", rt, rn, imm5);
392 taddr = sp[rn] + imm5;
394 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
395 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
398 else if((inst >> 12) == 9)
402 rt = (inst >> 8) & 0x7;
404 printf(
"LDRH/STRH rt=%x imm8=%x\n", rt, imm8);
405 taddr = sp[6] + imm8;
407 printf(
"[0x%08x] 0x%04x %s 0x%x [0x%x]\n", addr, inst,
408 (inst &
BIT11) ?
"LDR" :
"STR", tdata, taddr);
412 printf(
"Unexpected instruction\n");
421int32_t
SH_Return(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0)
439#ifndef NONBLOCK_PRINTF
456 static uint8_t u8Buf[BUF_SIZE] = {0};
457 static int32_t i32Head = 0;
458 static int32_t i32Tail = 0;
468 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
469 if(i32Tmp != i32Tail)
471 u8Buf[i32Head] =
'\r';
477 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
478 if(i32Tmp != i32Tail)
486 if(i32Tail == i32Head)
493 i32Tmp = i32Tail + 1;
494 if(i32Tmp > BUF_SIZE) i32Tmp = 0;
503 }
while(i32Tail != i32Head);
519#if defined(DEBUG_ENABLE_SEMIHOST)
520 g_buf[g_buf_len++] = ch;
521 g_buf[g_buf_len] =
'\0';
522 if(g_buf_len + 1 >=
sizeof(g_buf) || ch ==
'\n' || ch ==
'\0')
527 if(SH_DoCommand(0x04, (
int)g_buf,
NULL) != 0)
535# if (DEBUG_ENABLE_SEMIHOST == 2)
537 for(i = 0; i < g_buf_len; i++)
563#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)
766#ifdef DEBUG_ENABLE_SEMIHOST
769void __exit(
int return_code)
772 if(SH_DoCommand(0x18, 0x20026,
NULL) == 0)
781void _sys_exit(
int return_code)
785 if(SH_DoCommand(0x18, 0x20026,
NULL) == 0)
void *__dso_handle __attribute__((weak))
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
#define UART_FIFOSTS_TXFULL_Msk
#define UART_FIFOSTS_TXEMPTYF_Msk
#define UART_FIFOSTS_RXEMPTY_Msk
#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.
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.