From 836a9d3addf107e10ee464600ecf2550e25012bc Mon Sep 17 00:00:00 2001 From: imi415 Date: Wed, 10 Aug 2022 01:21:53 +0800 Subject: [PATCH] Newlib _exit will not be executed, replaced by a dead loop. --- CMakeLists.txt | 2 +- src/main.c | 41 +++++++++++++++++++++++++++++++++++++++-- src/stx7105_utils.c | 6 +++--- startup_stx7105.S | 13 +++++++------ 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a95f8de..77f0950 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,7 @@ set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-flto") set(CMAKE_C_FLAGS "${TARGET_FLAGS_HARDWARE} -Wall -ffunction-sections -fdata-sections") set(CMAKE_CXX_FLAGS "${TARGET_FLAGS_HARDWARE} -Wall -ffunction-sections -fdata-sections") -set(CMAKE_EXE_LINKER_FLAGS "${TARGET_FLAGS_HARDWARE} -Wall -lc -lm -nostartfiles -Wl,--print-memory-usage -Wl,--gc-sections") +set(CMAKE_EXE_LINKER_FLAGS "${TARGET_FLAGS_HARDWARE} -Wall -specs=nosys.specs -lc -lm -nostartfiles -Wl,--print-memory-usage -Wl,--gc-sections") add_compile_definitions(${TARGET_DEFS}) include_directories(${TARGET_INCLUDES}) diff --git a/src/main.c b/src/main.c index 58982f6..0e5d0cb 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,9 @@ #define SYSTEM_CONFIG34 (0xFE001188U) /* PIO4 */ #define SYSTEM_CONFIG7 (0xFE00111CU) /* RXSEL */ +#define MEMTEST_START 0x80010000 +#define MEMTEST_END 0x8FFE0000 + void uart_init(void) { PIO4->CLR_PC0 = 1U; /* PC = 110, AFOUT, PP */ PIO4->SET_PC1 = 1U; @@ -31,6 +34,28 @@ void uart_init(void) { CONSOLE_ASC->CTRL = 0x1589UL; /* 8N1, RX enable, FIFO enable, Baud mode 1 */ } +static void memory_test(void) { + for (uint32_t i = MEMTEST_START; i < MEMTEST_END; i += 4) { + *(uint32_t *)i = i; + + if (i % 0x10000 == 0U) { + printf("Write to 0x%08x...\r\n", i); + } + } + + for (uint32_t i = MEMTEST_START; i < MEMTEST_END; i += 4) { + if (*(uint32_t *)i != i) { + printf("Read back error at 0x%08x\r\n", i); + + return; + } + + if (i % 0x10000 == 0U) { + printf("Read from 0x%08x...\r\n", i); + } + } +} + int main(void) { init_led(LED_RED_GPIO, LED_RED_PIN, 0U); init_led(LED_BLUE_GPIO, LED_BLUE_PIN, 0U); @@ -39,10 +64,22 @@ int main(void) { printf("Hello world\r\n"); + printf("Size of int: %d\r\n", sizeof(int)); + printf("Size of short: %d\r\n", sizeof(short)); + printf("Size of char: %d\r\n", sizeof(char)); + printf("Size of long: %d\r\n", sizeof(long)); + printf("Size of pointer: %d\r\n", sizeof(uint8_t *)); + printf("Size of uint8_t: %d\r\n", sizeof(uint8_t)); + printf("Size of uint16_t: %d\r\n", sizeof(uint16_t)); + printf("Size of uint32_t: %d\r\n", sizeof(uint32_t)); + + delay_ms(5000); + + memory_test(); + for (;;) { set_led(LED_BLUE_GPIO, LED_BLUE_PIN, 1U); delay_ms(500); - printf("Hello ?\r\n"); set_led(LED_BLUE_GPIO, LED_BLUE_PIN, 0U); delay_ms(500); } @@ -51,7 +88,7 @@ int main(void) { } void _putchar(char ch) { - while(CONSOLE_ASC->STA & 1 << 9U) { + while (CONSOLE_ASC->STA & 1 << 9U) { // wait for TX FIFO slot. } CONSOLE_ASC->TX_BUF = ch; diff --git a/src/stx7105_utils.c b/src/stx7105_utils.c index 6efa2eb..271c25b 100644 --- a/src/stx7105_utils.c +++ b/src/stx7105_utils.c @@ -24,10 +24,10 @@ void set_led(PIO_TypeDef *gpiox, uint8_t pin, uint8_t val) { void delay_ms(uint32_t msec) { /* Initialize TMU and count to zero */ - /* TMU clock is from Peripheral clock, approx. 66MHz */ - /* Prescale to 66kHz for convenience (TMUs can only divide by max. 1024) */ + /* TMU clock is from Peripheral clock, approx. 100MHz */ + /* Prescale to 100kHz for convenience (TMUs can only divide by max. 1024) */ - uint32_t reload_value = msec * 66 - 1; + uint32_t reload_value = msec * 100 - 1; TMU->TSTR &= ~TMU_TSTR_STR0_Msk; /* Stop counter */ TMU->TCR0 = 0x04U | TMU_TCR_UNIE_Msk; /* 1024 prescale, enable interrupt */ diff --git a/startup_stx7105.S b/startup_stx7105.S index 686fec1..ae1ab94 100644 --- a/startup_stx7105.S +++ b/startup_stx7105.S @@ -63,10 +63,11 @@ _main_entry: jsr @r0 or r0, r0 - mov r0, r4 - mov.l _exit_k, r0 - jsr @r0 - or r0, r0 + mov.l _exit_loop_k, r0 +_exit_loop: + sleep + jmp @r0 + nop .balign 4 /* libc FPU routine */ @@ -88,8 +89,8 @@ _end_k: /* Function pointers */ _main_k: .long _main /* Same address as main */ -_exit_k: - .long _exit +_exit_loop_k: + .long _exit_loop _exc_base_k: .long _exc_base _exc_imask_k: