From 68e2cd9b296733c9aea8b3401ab946dd25de9c0e Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Wed, 17 Jan 2024 18:07:25 +0000 Subject: [PATCH] Fix WCH example --- .../ch32v307-make-baremetal-builtin/Makefile | 2 +- .../wch/ch32v307-make-baremetal-builtin/hal.c | 142 ++++++++++++++++++ .../wch/ch32v307-make-baremetal-builtin/hal.h | 11 +- .../ch32v307-make-baremetal-builtin/main.c | 78 ++-------- 4 files changed, 163 insertions(+), 70 deletions(-) create mode 100644 examples/wch/ch32v307-make-baremetal-builtin/hal.c diff --git a/examples/wch/ch32v307-make-baremetal-builtin/Makefile b/examples/wch/ch32v307-make-baremetal-builtin/Makefile index 08258de7b6..d00418d5a3 100644 --- a/examples/wch/ch32v307-make-baremetal-builtin/Makefile +++ b/examples/wch/ch32v307-make-baremetal-builtin/Makefile @@ -7,7 +7,7 @@ CFLAGS += -ffunction-sections -fdata-sections -fno-common CFLAGS += -DSYSCLK_FREQ_144MHz_HSE -I. -Ivendor -g3 -Os CFLAGS += -march=rv32imafc -mabi=ilp32 $(CFLAGS_EXTRA) LDFLAGS = -T vendor/link.ld -nostartfiles --specs=nano.specs --specs=nosys.specs -Wl,--gc-sections -SOURCES = main.c mongoose.c net.c packed_fs.c +SOURCES = hal.c main.c mongoose.c net.c packed_fs.c SOURCES += vendor/system_ch32v30x.c vendor/startup_ch32v30x_D8C.S CFLAGS += -DHTTP_URL=\"http://0.0.0.0/\" -DHTTPS_URL=\"https://0.0.0.0/\" diff --git a/examples/wch/ch32v307-make-baremetal-builtin/hal.c b/examples/wch/ch32v307-make-baremetal-builtin/hal.c new file mode 100644 index 0000000000..fa4e227a96 --- /dev/null +++ b/examples/wch/ch32v307-make-baremetal-builtin/hal.c @@ -0,0 +1,142 @@ +// Copyright (c) 2024 Cesanta Software Limited +// All rights reserved + +#include // For _fstat() +#include "hal.h" + +extern void SystemCoreClockUpdate(void); + +void SysTick_Init(void) { + SysTick->CMP = SystemCoreClock / 1000 - 1; + SysTick->CNT = 0; + SysTick->SR = 0; + SysTick->CTLR = BIT(0) | BIT(1) | BIT(2) | BIT(3); + NVIC_EnableIRQ(SysTicK_IRQn); +} + +#if 0 +void SystemInit(void) { // Called automatically by startup code + SystemCoreClockUpdate(); +} +#endif + +static volatile uint64_t s_ticks; // Milliseconds since boot +__attribute__((interrupt())) void SysTick_Handler(void) { + s_ticks++; + SysTick->SR = 0; +} + +void mg_random(void *buf, size_t len) { // Use on-board RNG + for (size_t n = 0; n < len; n += sizeof(uint32_t)) { + uint32_t r = rng_read(); + memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); + } +} + +uint64_t mg_millis(void) { // Let Mongoose use our uptime function + return s_ticks; // Return number of milliseconds since boot +} + +void hal_init(void) { + SystemCoreClockUpdate(); + SysTick_Init(); + uart_init(UART_DEBUG, 115200); + gpio_output(LED_PIN); + ethernet_init(); + rng_init(); +} + +// Newlib syscalls. Implemented are: _sbrk() for malloc, and _write() +int _fstat(int fd, struct stat *st) { + (void) fd, (void) st; + return -1; +} + +extern unsigned char _end[]; // End of data section, start of heap. See link.ld +static unsigned char *s_current_heap_end = _end; + +size_t hal_ram_used(void) { + return (size_t) (s_current_heap_end - _end); +} + +size_t hal_ram_free(void) { + unsigned char endofstack; + return (size_t) (&endofstack - s_current_heap_end); +} + +void *_sbrk(int incr) { + unsigned char *prev_heap; + unsigned char *heap_end = (unsigned char *) ((size_t) &heap_end - 256); + prev_heap = s_current_heap_end; + // Check how much space we got from the heap end to the stack end + if (s_current_heap_end + incr > heap_end) return (void *) -1; + s_current_heap_end += incr; + return prev_heap; +} + +int _open(const char *path) { + (void) path; + return -1; +} + +int _close(int fd) { + (void) fd; + return -1; +} + +int _isatty(int fd) { + (void) fd; + return 1; +} + +int _lseek(int fd, int ptr, int dir) { + (void) fd, (void) ptr, (void) dir; + return 0; +} + +void _exit(int status) { + (void) status; + for (;;) (void) 0; +} + +void _kill(int pid, int sig) { + (void) pid, (void) sig; +} + +int _getpid(void) { + return -1; +} + +int _write(int fd, char *ptr, int len) { + (void) fd, (void) ptr, (void) len; + if (fd == 1) uart_write_buf(UART_DEBUG, ptr, (size_t) len); + return -1; +} + +int _read(int fd, char *ptr, int len) { + (void) fd, (void) ptr, (void) len; + return -1; +} + +int _link(const char *a, const char *b) { + (void) a, (void) b; + return -1; +} + +int _unlink(const char *a) { + (void) a; + return -1; +} + +int _stat(const char *path, struct stat *st) { + (void) path, (void) st; + return -1; +} + +int mkdir(const char *path, mode_t mode) { + (void) path, (void) mode; + return -1; +} + +void _init(void) { +} diff --git a/examples/wch/ch32v307-make-baremetal-builtin/hal.h b/examples/wch/ch32v307-make-baremetal-builtin/hal.h index 2ddff7c242..5f53e8b40a 100644 --- a/examples/wch/ch32v307-make-baremetal-builtin/hal.h +++ b/examples/wch/ch32v307-make-baremetal-builtin/hal.h @@ -5,6 +5,11 @@ #define UART_DEBUG USART1 +#define BTN_PIN PIN('B', 3) // On-board user button +#define LED1_PIN PIN('A', 15) // On-board red LED +#define LED2_PIN PIN('B', 4) // On-board blue LED +#define LED_PIN LED2_PIN + #include #include @@ -19,8 +24,10 @@ #define PINBANK(pin) (pin >> 8) extern uint32_t SystemCoreClock; -extern void SystemInit(void); -extern void SystemCoreClockUpdate(void); + +void hal_init(void); +size_t hal_ram_free(void); +size_t hal_ram_used(void); static inline void spin(volatile uint32_t count) { while (count--) (void) 0; diff --git a/examples/wch/ch32v307-make-baremetal-builtin/main.c b/examples/wch/ch32v307-make-baremetal-builtin/main.c index 571744b360..118763f4b2 100644 --- a/examples/wch/ch32v307-make-baremetal-builtin/main.c +++ b/examples/wch/ch32v307-make-baremetal-builtin/main.c @@ -4,30 +4,8 @@ #include "mongoose.h" #include "net.h" -#define BTN_PIN PIN('B', 3) // On-board user button -#define LED1_PIN PIN('A', 15) // On-board red LED -#define LED2_PIN PIN('B', 4) // On-board blue LED -#define LED_PIN LED2_PIN - #define BLINK_PERIOD_MS 1000 // LED_PIN blinking period in millis -static volatile uint64_t s_ticks; // Milliseconds since boot -__attribute__((interrupt())) void SysTick_Handler(void) { - s_ticks++; - SysTick->SR = 0; -} - -uint64_t mg_millis(void) { // Let Mongoose use our uptime function - return s_ticks; // Return number of milliseconds since boot -} - -void mg_random(void *buf, size_t len) { // Use on-board RNG - for (size_t n = 0; n < len; n += sizeof(uint32_t)) { - uint32_t r = rng_read(); - memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); - } -} - // This flash space resides at after the 0-wait 320k area static char *s_flash_space = (char *) (0x8000000 + 320 * 1024); @@ -45,41 +23,27 @@ static void timer_fn(void *arg) { gpio_toggle(LED_PIN); // Blink LED_PIN struct mg_tcpip_if *ifp = arg; // And show const char *names[] = {"down", "up", "req", "ready"}; // network stats - MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u", + MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u RAM: %lu/%lu", names[ifp->state], mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent, - ifp->ndrop, ifp->nerr)); -} - -void SysTick_Init(void) { - SysTick->CMP = SystemCoreClock / 1000 - 1; - SysTick->CNT = 0; - SysTick->SR = 0; - SysTick->CTLR = BIT(0) | BIT(1) | BIT(2) | BIT(3); - NVIC_EnableIRQ(SysTicK_IRQn); + ifp->ndrop, ifp->nerr, hal_ram_used(), hal_ram_free())); } // https://mongoose.ws/documentation/#2-minute-integration-guide static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - mg_http_reply(c, 200, "", "ok %p %p\r\n", hm, fn_data); + mg_http_reply(c, 200, "", "ok %p\r\n", hm); } } int main(void) { - SystemCoreClockUpdate(); - SysTick_Init(); - - gpio_output(LED_PIN); // Setup LED - uart_init(UART_DEBUG, 115200); // Initialise debug printf + struct mg_mgr mgr; - struct mg_mgr mgr; // Initialise - mg_mgr_init(&mgr); // Mongoose event manager - mg_log_set(MG_LL_DEBUG); // Set log level + hal_init(); + mg_mgr_init(&mgr); + mg_log_set(MG_LL_DEBUG); MG_INFO(("Starting, CPU freq %g MHz", (double) SystemCoreClock / 1000000)); - extern char _end[], _heap_end[]; - MG_INFO(("Heap size: %lu bytes", _heap_end - _end)); // Print chip RAM/Flash configuration, and set to 64/256 const char *sizes[] = {"128/192", "96/224", "64/256", "32/288"}; @@ -87,8 +51,11 @@ int main(void) { MG_INFO(("RAM/FLASH configuration: %s", sizes[mode])); // if (mode != 2) set_ram_size(2); + extern char _end[], _heap_end[]; + MG_INFO(("Heap size: %lu bytes. RAM: used %lu, free %lu", _heap_end - _end, + hal_ram_used(), hal_ram_free())); + // Initialise Mongoose network stack - ethernet_init(); // Initialise ethernet pins struct mg_tcpip_driver_stm32f_data driver_data = {.mdc_cr = 1, .phy_addr = 1}; struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(), // Uncomment below for static configuration: @@ -116,26 +83,3 @@ int main(void) { return 0; } - -// Newlib syscalls -int _write(int fd, char *buf, int len) { - if (fd == 1) uart_write_buf(USART1, buf, len); - return len; -} - -void *_sbrk(ptrdiff_t incr) { - extern char _end[], _heap_end[]; - static char *curbrk = _end; - if ((curbrk + incr < _end) || (curbrk + incr > _heap_end)) { - return NULL - 1; - } - // MG_INFO(("%p %ld", curbrk, incr)); - curbrk += incr; - return curbrk - incr; -} - -void _init(void) { -} - -void _fini(void) { -}