From ee7654a3ed5acc39a8ab6a0d6fbf3ffae71d61bb Mon Sep 17 00:00:00 2001 From: Oleg Borodin Date: Mon, 19 Sep 2022 18:48:32 +0200 Subject: [PATCH] config incapsulated to module --- flashrw/Makefile | 3 +++ flashrw/config.c | 26 ++++++++++++++++++++++++++ flashrw/config.h | 25 +++++++++++++++++++++++++ flashrw/main.c | 34 ++++++++-------------------------- flashrw/syscall.c | 8 +------- 5 files changed, 63 insertions(+), 33 deletions(-) create mode 100644 flashrw/config.c create mode 100644 flashrw/config.h diff --git a/flashrw/Makefile b/flashrw/Makefile index 669f260..2b80eaa 100644 --- a/flashrw/Makefile +++ b/flashrw/Makefile @@ -30,6 +30,9 @@ all: main.bin OBJS+= main.o OBJS+= syscall.o OBJS+= usartu.o +OBJS+= config.o + + main.elf: $(OBJS) diff --git a/flashrw/config.c b/flashrw/config.c new file mode 100644 index 0000000..0f8e970 --- /dev/null +++ b/flashrw/config.c @@ -0,0 +1,26 @@ +/* + * Copyright 2022 Oleg Borodin + */ + + +#include + +#include +#include + + +void config_init(config_t* c) { + c->gz = 0x00; + c->gy = 0x00; +}; + +config_t* config_getptr() { + return (config_t*)&_config; +} + +void config_save(config_t* ptr) { + flash_unlock(); + flash_erase_sector(SECTOR_NO, sizeof(config_t)); + uint32_t caddr = (uint32_t)&_config; + flash_program(caddr, (uint8_t*)ptr, (uint32_t)sizeof(config_t)); +} diff --git a/flashrw/config.h b/flashrw/config.h new file mode 100644 index 0000000..5be01b4 --- /dev/null +++ b/flashrw/config.h @@ -0,0 +1,25 @@ +/* + * Copyright 2022 Oleg Borodin + */ + + +#ifndef CONFIG_H_QWERTY +#define CONFIG_H_QWERTY + + +#include + +#define SECTOR_NO 1 +extern uint8_t _config; + +typedef struct __attribute__ ((packed)) { + int32_t gz; + int32_t gy; +} config_t; + +void config_init(config_t* c); +void config_save(config_t* ptr); +config_t* config_getptr(); + + +#endif diff --git a/flashrw/main.c b/flashrw/main.c index 5c769dc..fad0992 100644 --- a/flashrw/main.c +++ b/flashrw/main.c @@ -19,6 +19,7 @@ #include #include +#include static void clock_setup(void) { rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]); @@ -48,41 +49,22 @@ static void usart_setup(uint32_t usart, uint32_t gpioport, uint32_t gpiopins, ui usart_enable(usart); } -#define SECTOR_NO 1 -extern uint8_t _config; - - -typedef struct { - int32_t gz; - int32_t gy; -} config_t; - -void config_init(config_t* c){ - c->gz = 0x12; - c->gy = 0x17; -}; int main(void) { clock_setup(); usart_setup(USART1, GPIOA, GPIO9 | GPIO10, 115200); - uint32_t caddr = 0xFFFFFFFF; - caddr = (uint32_t)&_config; - config_t* dconfig = (config_t*)caddr; - - config_t tconfig; - config_init(&tconfig); - - flash_unlock(); - flash_erase_sector(SECTOR_NO, sizeof(config_t)); + config_t rconfig; + config_init(&rconfig); + rconfig.gz = 0x15; + rconfig.gy = 0x19; - printf("\r\n"); - printf("gy = 0x%08lx \r\n", dconfig->gy); + config_save(&rconfig); - flash_program(caddr, (uint8_t*)&tconfig, (uint32_t)sizeof(config_t)); + config_t* sconfig = config_getptr(); - printf("gy = 0x%08lx \r\n", dconfig->gy); + printf("gy = 0x%08lx \r\n", sconfig->gy); while (true) { diff --git a/flashrw/syscall.c b/flashrw/syscall.c index 2b1d919..bacb025 100644 --- a/flashrw/syscall.c +++ b/flashrw/syscall.c @@ -116,7 +116,7 @@ int _wait(int *status) { } #if 0 -void *xxx_sbrk(int incr) { +void *_sbrk(int incr) { extern unsigned char *_end; static unsigned char *heap = NULL; @@ -146,14 +146,8 @@ caddr_t __attribute__((weak)) _sbrk (int incr) { prev_heap_end = heap_end; if (heap_end + incr > stack_ptr) { -#if 0 - extern void abort (void); - _write (1, "_sbrk: Heap and stack collision\n", 32); - abort (); -#else errno = ENOMEM; return (caddr_t) -1; -#endif } heap_end += incr;