From 9d9f9dc034765aa4b1c5bd2d0aa4cd24e4110055 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 25 Sep 2022 15:21:12 +0200 Subject: c++ to c for stm32 firmware --- stm32f091/main.c | 21 +++++++++++++++++++++ stm32f091/main.cpp | 21 --------------------- stm32f091/makefile | 14 +++++++------- 3 files changed, 28 insertions(+), 28 deletions(-) create mode 100644 stm32f091/main.c delete mode 100644 stm32f091/main.cpp (limited to 'stm32f091') diff --git a/stm32f091/main.c b/stm32f091/main.c new file mode 100644 index 0000000..180240e --- /dev/null +++ b/stm32f091/main.c @@ -0,0 +1,21 @@ +#include +#include + +#define PORT GPIOA +#define PIN 5 + +int main() { + RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; + + PORT->MODER &= ~(0b11 << (PIN * 2)); + PORT->MODER |= (0b01 << (PIN * 2)); + + uint8_t led = 1; + + while (1) { + PORT->ODR &= ~(1 << PIN); + PORT->ODR |= (led << PIN); + led ^= 1; + for (unsigned long i = 0; i < 50000; i++) asm("nop"); + } +} diff --git a/stm32f091/main.cpp b/stm32f091/main.cpp deleted file mode 100644 index 180240e..0000000 --- a/stm32f091/main.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include - -#define PORT GPIOA -#define PIN 5 - -int main() { - RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; - - PORT->MODER &= ~(0b11 << (PIN * 2)); - PORT->MODER |= (0b01 << (PIN * 2)); - - uint8_t led = 1; - - while (1) { - PORT->ODR &= ~(1 << PIN); - PORT->ODR |= (led << PIN); - led ^= 1; - for (unsigned long i = 0; i < 50000; i++) asm("nop"); - } -} diff --git a/stm32f091/makefile b/stm32f091/makefile index c187df7..5f5371d 100644 --- a/stm32f091/makefile +++ b/stm32f091/makefile @@ -1,5 +1,5 @@ -CC = arm-none-eabi-g++ -LD = arm-none-eabi-g++ +CC = arm-none-eabi-gcc +LD = arm-none-eabi-gcc OC = arm-none-eabi-objcopy RM = rm -f @@ -9,7 +9,7 @@ SHARED_FLAGS += -g SHARED_FLAGS += -D STM32F091xC SHARED_FLAGS += -Wall SHARED_FLAGS += -Wextra -SHARED_FLAGS += -Wno-register +# SHARED_FLAGS += -Wno-register SHARED_FLAGS += -Wa,--defsym,CALL_ARM_SYSTEM_INIT=1 SHARED_FLAGS += -I./lib/STM32-base-STM32Cube/CMSIS/ARM/inc SHARED_FLAGS += -I./lib/STM32-base-STM32Cube/CMSIS/STM32F0xx/inc @@ -20,8 +20,8 @@ SHARED_FLAGS += -mlittle-endian SHARED_FLAGS += -mthumb SHARED_FLAGS += -masm-syntax-unified SHARED_FLAGS += -specs=nosys.specs -SHARED_FLAGS += -fno-threadsafe-statics -SHARED_FLAGS += -fno-rtti +# SHARED_FLAGS += -fno-threadsafe-statics +# SHARED_FLAGS += -fno-rtti SHARED_FLAGS += -fno-exceptions SHARED_FLAGS += -fno-unwind-tables SHARED_FLAGS += -Wl,-L./lib/STM32-base/linker,-T./lib/STM32-base/linker/STM32F0xx/STM32F091xC.ld @@ -32,7 +32,7 @@ CFLAGS += $(SHARED_FLAGS) LFLAGS += $(SHARED_FLAGS) AFLAGS += $(SHARED_FLAGS) -OBJS += $(patsubst %.cpp,%.o, $(wildcard *.cpp)) +OBJS += $(patsubst %.c,%.o, $(wildcard *.c)) OBJS += ./lib/STM32-base/startup/STM32F0xx/STM32F091xC.o OBJS += ./lib/STM32-base-STM32Cube/CMSIS/STM32F0xx/src/system_stm32f0xx.o @@ -47,7 +47,7 @@ $(TARGET).bin: $(TARGET).elf %.o: %.c $(CC) -c $(CFLAGS) $< -o $@ -%.o: %.cpp +%.o: %.c $(CC) -c $(CFLAGS) $< -o $@ $(TARGET).elf: $(OBJS) -- cgit v1.2.3 From 3608f1cc19f4bc102c1f0c5a3ab242e2e318a0b9 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 25 Sep 2022 15:47:14 +0200 Subject: implement backlog interface --- stm32f091/backlog.c | 1 + stm32f091/backlog.h | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ stm32f091/makefile | 2 +- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 stm32f091/backlog.c create mode 100644 stm32f091/backlog.h (limited to 'stm32f091') diff --git a/stm32f091/backlog.c b/stm32f091/backlog.c new file mode 100644 index 0000000..1c847f0 --- /dev/null +++ b/stm32f091/backlog.c @@ -0,0 +1 @@ +#include "backlog.h" diff --git a/stm32f091/backlog.h b/stm32f091/backlog.h new file mode 100644 index 0000000..7051ecf --- /dev/null +++ b/stm32f091/backlog.h @@ -0,0 +1,54 @@ +#pragma once + +#include + +/** + * @brief allocate backlog buffer and set global backlog pointer + * @param record_amt amount of records to keep before overwriting oldest record + */ +void ws_backlog_alloc(uint16_t record_amt); + +// enable struct packing +#pragma pack(push, 1) + +/** @brief backlog record */ +typedef struct { + uint16_t id; /**< unique record identifier, numbered sequentially */ + uint8_t sens_temperature; /**< temperature reading */ + uint8_t sens_humidity; /**< humidity reading */ + uint8_t sens_atm_pressure; /**< atmospheric pressure reading */ +} ws_s_backlog_record; + +typedef struct { + uint16_t buffer_size; /**< buffer size (buffer_size + 1 = amount of records) */ + uint16_t buffer_start; /** first record index */ + uint16_t buffer_end; /** last record index */ + ws_s_backlog_record* records; /** pointer to record array */ +} ws_s_backlog_database; + +// disable struct packing +#pragma pack(pop) + +/** @brief global record backlog database pointer */ +extern ws_s_backlog_database* WS_G_BACKLOG_DATABASE; + +/** + * @brief add record to database + * + * automatically sets record.id, pushes buffer_end forwards and overwrites the + * last record if the buffer is full + */ +void ws_backlog_add_record(ws_s_backlog_record* record); + +/** + * there's intentionally no function to retrieve multiple records as an array, + * as this would either require + * (a) copying the selection which is not possible with the current memory + * constraints, or + * (b) giving a direct pointer, but this would cause undefined behavior at the + * ring buffer seam + */ + +/** @brief get pointer to record with index `record_index` from the database */ +ws_s_backlog_record* ws_backlog_get_record(uint16_t record_index); + diff --git a/stm32f091/makefile b/stm32f091/makefile index 5f5371d..08c582a 100644 --- a/stm32f091/makefile +++ b/stm32f091/makefile @@ -18,7 +18,7 @@ SHARED_FLAGS += -ffunction-sections SHARED_FLAGS += -fdata-sections SHARED_FLAGS += -mlittle-endian SHARED_FLAGS += -mthumb -SHARED_FLAGS += -masm-syntax-unified +# SHARED_FLAGS += -masm-syntax-unified SHARED_FLAGS += -specs=nosys.specs # SHARED_FLAGS += -fno-threadsafe-statics # SHARED_FLAGS += -fno-rtti -- cgit v1.2.3 From 5829fb58101a9edb6b9d31c9e22183f5159e1480 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Mon, 26 Sep 2022 14:15:14 +0200 Subject: implement backlog database functions --- stm32f091/backlog.c | 33 +++++++++++++++++++++++++++++++++ stm32f091/backlog.h | 9 ++++++--- stm32f091/makefile | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) (limited to 'stm32f091') diff --git a/stm32f091/backlog.c b/stm32f091/backlog.c index 1c847f0..3f21924 100644 --- a/stm32f091/backlog.c +++ b/stm32f091/backlog.c @@ -1 +1,34 @@ +#include + #include "backlog.h" + +ws_s_backlog_database* WS_G_BACKLOG_DATABASE = NULL; + +void ws_backlog_alloc(uint16_t record_amt) { + WS_G_BACKLOG_DATABASE = malloc(sizeof(ws_s_backlog_database) + sizeof(ws_s_backlog_record) * record_amt); + WS_G_BACKLOG_DATABASE->buffer_size = record_amt; + WS_G_BACKLOG_DATABASE->buffer_start = 0; + WS_G_BACKLOG_DATABASE->buffer_end = 0; +} + +void ws_backlog_add_record(ws_s_backlog_record record) { + static uint16_t id = 0; + + WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].id = id++; + WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].sens_atm_pressure = record.sens_atm_pressure; + WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].sens_humidity = record.sens_humidity; + WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].sens_temperature = record.sens_temperature; + + // shift buffer start/end + WS_G_BACKLOG_DATABASE->buffer_end = (WS_G_BACKLOG_DATABASE->buffer_end + 1) % WS_G_BACKLOG_DATABASE->buffer_size; + if (WS_G_BACKLOG_DATABASE->buffer_end == WS_G_BACKLOG_DATABASE->buffer_start) + WS_G_BACKLOG_DATABASE->buffer_start = (WS_G_BACKLOG_DATABASE->buffer_start + 1) % WS_G_BACKLOG_DATABASE->buffer_size; +} + +ws_s_backlog_record* ws_backlog_get_record(uint16_t record_index) { + return &WS_G_BACKLOG_DATABASE->records[record_index]; +} + +ws_s_backlog_record* ws_backlog_get_last_record(uint16_t record_offset) { + return ws_backlog_get_record((WS_G_BACKLOG_DATABASE->buffer_end - record_offset - 1) % WS_G_BACKLOG_DATABASE->buffer_size); +} diff --git a/stm32f091/backlog.h b/stm32f091/backlog.h index 7051ecf..465b3c0 100644 --- a/stm32f091/backlog.h +++ b/stm32f091/backlog.h @@ -20,10 +20,10 @@ typedef struct { } ws_s_backlog_record; typedef struct { - uint16_t buffer_size; /**< buffer size (buffer_size + 1 = amount of records) */ + uint16_t buffer_size; /**< buffer size (amount of records) */ uint16_t buffer_start; /** first record index */ uint16_t buffer_end; /** last record index */ - ws_s_backlog_record* records; /** pointer to record array */ + ws_s_backlog_record records[]; /** record array */ } ws_s_backlog_database; // disable struct packing @@ -38,7 +38,7 @@ extern ws_s_backlog_database* WS_G_BACKLOG_DATABASE; * automatically sets record.id, pushes buffer_end forwards and overwrites the * last record if the buffer is full */ -void ws_backlog_add_record(ws_s_backlog_record* record); +void ws_backlog_add_record(ws_s_backlog_record record); /** * there's intentionally no function to retrieve multiple records as an array, @@ -52,3 +52,6 @@ void ws_backlog_add_record(ws_s_backlog_record* record); /** @brief get pointer to record with index `record_index` from the database */ ws_s_backlog_record* ws_backlog_get_record(uint16_t record_index); +/** @brief get pointer to last record with offset `record_offset` from the database */ +ws_s_backlog_record* ws_backlog_get_last_record(uint16_t record_offset); + diff --git a/stm32f091/makefile b/stm32f091/makefile index 08c582a..10ede8c 100644 --- a/stm32f091/makefile +++ b/stm32f091/makefile @@ -11,6 +11,7 @@ SHARED_FLAGS += -Wall SHARED_FLAGS += -Wextra # SHARED_FLAGS += -Wno-register SHARED_FLAGS += -Wa,--defsym,CALL_ARM_SYSTEM_INIT=1 +# SHARED_FLAGS += -I/usr/arm-none-eabi/include/ SHARED_FLAGS += -I./lib/STM32-base-STM32Cube/CMSIS/ARM/inc SHARED_FLAGS += -I./lib/STM32-base-STM32Cube/CMSIS/STM32F0xx/inc SHARED_FLAGS += -I./lib/STM32-base/startup -- cgit v1.2.3