diff --git a/BSP/Debug/debug.c b/BSP/Debug/debug.c index 9eb432c..82c3df5 100644 --- a/BSP/Debug/debug.c +++ b/BSP/Debug/debug.c @@ -155,14 +155,14 @@ __attribute__((used)) int _write(int fd, char *buf, int size) for(i = 0; i < size; i++) { #if(DEBUG_UART == DEBUG_UART1) - while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); - USART_SendData(USART1, *buf++); + while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); + USART_SendData(USART1, buf[i]); #elif(DEBUG_UART == DEBUG_UART2) - while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); - USART_SendData(USART2, *buf++); + while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); + USART_SendData(USART2, buf[i]); #elif(DEBUG_UART == DEBUG_UART3) - while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); - USART_SendData(USART3, *buf++); + while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); + USART_SendData(USART3, buf[i]); #endif } diff --git a/BSP/Ld/Link.ld b/BSP/Ld/Link.ld index 09174c7..083e0e9 100644 --- a/BSP/Ld/Link.ld +++ b/BSP/Ld/Link.ld @@ -1,9 +1,12 @@ ENTRY( _start ) +/* Configure heap and stack space reserved for application. + * Too low may cause standard library failed, + * too high may cause SRAM overflow. + * Collision may occur with heap when stack gets too deep. + */ __stack_size = 2048; - -PROVIDE( _stack_size = __stack_size ); - +__heap_size = 2048; MEMORY { @@ -150,27 +153,25 @@ SECTIONS *(.gnu.linkonce.b.*) *(COMMON*) . = ALIGN(4); - PROVIDE( _ebss = .); + PROVIDE(_ebss = .); } >RAM - PROVIDE( _end = _ebss); - PROVIDE( end = . ); - - .heap : + .heap_stack : { + . = ALIGN(8); + PROVIDE(_end = .); + PROVIDE(end = . ); - } + . = . + __heap_size; + . = ALIGN(8); + PROVIDE(_heap_end = .); - .stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size : - { - PROVIDE( _heap_end = . ); - . = ALIGN(4); - PROVIDE(_susrstack = . ); . = . + __stack_size; - PROVIDE( _eusrstack = .); - } >RAM + . = ALIGN(8); + } >RAM + + /* Place initial SP to the end of SRAM */ + __stack_top = ORIGIN(RAM) + LENGTH(RAM); + PROVIDE(_eusrstack = __stack_top); } - - - diff --git a/include/board.h b/include/board.h index 1f7acd4..0031a7c 100644 --- a/include/board.h +++ b/include/board.h @@ -1,4 +1,8 @@ #ifndef BOARD_H #define BOARD_H +void BOARD_IOMUX_Init(void); +void BOARD_Peripheral_Init(void); +void BOARD_SystemClock_Config(void); + #endif \ No newline at end of file diff --git a/src/board.c b/src/board.c index 518671d..280b066 100644 --- a/src/board.c +++ b/src/board.c @@ -1,5 +1,30 @@ #include "ch32v30x_conf.h" -void BOARD_LED_Init(void) { - +/** + * @brief Initialize IOMUX. + * + */ +void BOARD_IOMUX_Init(void) { + /* The official devkit (CH32V307V-R1-1v0) + * does not have any LEDs directly connected to the MCU, + * and does not have any buttons directly connected to the MCU either... + */ +} + +/** + * @brief Initialize on-board peripherals. + * + */ +void BOARD_Peripheral_Init(void) { +} + +/** + * @brief Configure system clock, + * If you are not satisfied with the default options. + * + */ +void BOARD_SystemClock_Config(void) { + + // After you are done playing with RCC... + SystemCoreClockUpdate(); } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 5e6a0da..863f4d5 100644 --- a/src/main.c +++ b/src/main.c @@ -4,13 +4,25 @@ #include "debug.h" -int main(void) { +#include "board.h" +int main(void) { + /* Initialize board components. */ + BOARD_SystemClock_Config(); + BOARD_IOMUX_Init(); + BOARD_Peripheral_Init(); + + + /* Initialize UART for libc function calls. */ USART_Printf_Init(115200); + Delay_Init(); /* Silly function, will be replaced next time. */ printf("Hello world?\r\n"); + uint32_t i = 0; + for(;;) { - // + Delay_Ms(500); + printf("Dead loop @%lu\r\n", i++); } }