From eebaf4f407e5ca53f61be5d7a4e3a744f6ef2942 Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Tue, 1 Nov 2022 21:22:21 +0100 Subject: [PATCH] nvm draft --- STM32L476_ParaMETEO/src/subdir.mk | 3 + include/etc/pwr_save_configuration.h | 2 +- include/kiss_communication.h | 12 ++ include/main.h | 2 +- src/configuration_handler.c | 2 +- src/main.c | 6 +- src/nvm.c | 162 +++++++++++++++++++++++++++ src/nvm.h | 51 +++++++++ system/src/drivers/max31865.c | 4 + 9 files changed, 240 insertions(+), 4 deletions(-) create mode 100644 src/nvm.c create mode 100644 src/nvm.h diff --git a/STM32L476_ParaMETEO/src/subdir.mk b/STM32L476_ParaMETEO/src/subdir.mk index 1b4d136..6695590 100644 --- a/STM32L476_ParaMETEO/src/subdir.mk +++ b/STM32L476_ParaMETEO/src/subdir.mk @@ -22,6 +22,7 @@ C_SRCS += \ ../src/kiss_callback.c \ ../src/kiss_communication.c \ ../src/main.c \ +../src/nvm.c \ ../src/packet_tx_handler.c \ ../src/pwr_save.c \ ../src/rte_main.c \ @@ -54,6 +55,7 @@ OBJS += \ ./src/kiss_callback.o \ ./src/kiss_communication.o \ ./src/main.o \ +./src/nvm.o \ ./src/packet_tx_handler.o \ ./src/pwr_save.o \ ./src/rte_main.o \ @@ -86,6 +88,7 @@ C_DEPS += \ ./src/kiss_callback.d \ ./src/kiss_communication.d \ ./src/main.d \ +./src/nvm.d \ ./src/packet_tx_handler.d \ ./src/pwr_save.d \ ./src/rte_main.d \ diff --git a/include/etc/pwr_save_configuration.h b/include/etc/pwr_save_configuration.h index c5ae29e..b6ef45e 100644 --- a/include/etc/pwr_save_configuration.h +++ b/include/etc/pwr_save_configuration.h @@ -35,6 +35,6 @@ /** * Do not uncomment this on production devices */ -#define INHIBIT_CUTOFF +//#define INHIBIT_CUTOFF #endif /* INCLUDE_PWR_SAVE_CONFIGURATION_H_ */ diff --git a/include/kiss_communication.h b/include/kiss_communication.h index d3a4e90..4c57750 100644 --- a/include/kiss_communication.h +++ b/include/kiss_communication.h @@ -39,6 +39,18 @@ #define KISS_PROGRAM_STARTUP_CFG (uint8_t) 0x23 #define KISS_PROGRAM_STARTUP_CFG_RESP (uint8_t) 0x73 +#define KISS_CONFIG_CRC (uint8_t) 0x24 +#define KISS_CONFIG_CRC_RESP (uint8_t) 0x74 + +#define KISS_RESTART (uint8_t) 0x25 +#define KISS_RESTART_RESP (uint8_t) 0x75 + +#define KISS_TOGGLE_PTT (uint8_t) 0x26 +//#define KISS_RESTART_RESP (uint8_t) 0x76 + +#define KISS_CONTROL_VOLTAGE (uint8_t) 0x27 +#define KISS_CONTROL_VOLTAGE_RESP (uint8_t) 0x77 + #define KISS_RETURN_IDLE 1 extern uint8_t kiss_current_async_message; diff --git a/include/main.h b/include/main.h index 2d182e3..8d00b0e 100644 --- a/include/main.h +++ b/include/main.h @@ -8,7 +8,7 @@ #include "config_data.h" #define SW_VER "EA17" -#define SW_DATE "21102022" +#define SW_DATE "31102022" #define SW_KISS_PROTO "A" #define SYSTICK_TICKS_PER_SECONDS 100 diff --git a/src/configuration_handler.c b/src/configuration_handler.c index 1f6685c..0682c1b 100644 --- a/src/configuration_handler.c +++ b/src/configuration_handler.c @@ -26,7 +26,7 @@ #endif /** - * STM32L476RE, last flash memory page + * STM32L476RE, 512KB flash mem, last flash memory page * 0x0807F800 - 0x0807FFFF; 2 K; Page 383 * * __config_section_default_start = 0x0801E000; diff --git a/src/main.c b/src/main.c index 134d7cf..721ad06 100644 --- a/src/main.c +++ b/src/main.c @@ -21,6 +21,7 @@ #include "gsm/sim800c_gprs.h" #include "http_client/http_client.h" +#include "nvm.h" #include "aprsis.h" #include "api/api.h" @@ -994,6 +995,9 @@ int main(int argc, char* argv[]){ io_ext_watchdog_service(); #ifdef STM32L471xx + + nvm_measurement_init(); + if (main_config_data_mode->gsm == 1) { it_handlers_inhibit_radiomodem_dcd_led = 1; @@ -1462,7 +1466,7 @@ int main(int argc, char* argv[]){ pwr_save_pooling_handler(main_config_data_mode, main_config_data_basic, packet_tx_get_minutes_to_next_wx(), rte_main_average_battery_voltage); } - if (main_config_data_mode->wx_dust_sensor & WX_DUST_SDS011_PWM > 0) { + if ((main_config_data_mode->wx_dust_sensor & WX_DUST_SDS011_PWM) > 0) { pwm_input_pool(); } diff --git a/src/nvm.c b/src/nvm.c new file mode 100644 index 0000000..22f1efa --- /dev/null +++ b/src/nvm.c @@ -0,0 +1,162 @@ +/* + * nvm.c + * + * Created on: Nov 1, 2022 + * Author: mateusz + */ + +#include "nvm.h" + +#define KB *1024 + +#ifdef STM32L471xx +#include +#include +#include "./drivers/l4/flash_stm32l4x.h" + +#define NVM_PAGE_SIZE 2048 +#define NVM_WRITE_BYTE_ALIGN 8 + +#endif + +#define NVM_MEASUREMENT_OFFSET 0 +#define NVM_MEASUREMENT_MAXIMUM_SIZ (NVM_PAGE_SIZE * 96) + +uint32_t nvm_base_address = 0; + +/** + * Start address of flash page used currently for NVM + */ +uint32_t nvm_current_page_address = 0; + +/** + * Pointer to + */ +uint8_t * nvm_data_ptr = 0; + +nvm_state_result_t nvm_general_state = NVM_UNINITIALIZED; + +#define WAIT_FOR_PGM_COMPLETION \ + while (1) {\ + flash_status = FLASH_GetBank1Status(); \ + \ + if (flash_status == FLASH_BUSY) { \ + ; \ + } \ + else if (flash_status == FLASH_ERROR_PG) { \ + nvm_general_state = NVM_PGM_ERROR; \ + break; \ + } \ + else { \ + break; \ + } \ + } \ + +void nvm_measurement_init(void) { + + uint8_t data = 0; + + // flash operation result + FLASH_Status flash_status = 0; + +#if defined(STM32L471xx) + // check current flash size + if (FLASH_SIZE == 1024 KB) { + // 1024KB + nvm_base_address = 0x08040000; + } + else if (FLASH_SIZE == 512 KB) { + // 512KB + nvm_base_address = 0x08080000; + } + else { + // unknown device ?? + nvm_general_state = NVM_INIT_ERROR; + + return; + } + + // find the first non-erased page + for (uint32_t i = nvm_base_address; i < (nvm_base_address + NVM_MEASUREMENT_MAXIMUM_SIZ); i += NVM_PAGE_SIZE) { + // get the content of first byte + data = *((uint8_t*) i); + + // check if data is in erased state + if (data == 0xFF) { + // first byte is erased, set data pointer to start of this page + nvm_data_ptr = (uint8_t*) i; + + // and break from the loop + break; + } + + // get the last byte of flash memory page + data = *(((uint8_t*) i + NVM_PAGE_SIZE - 1)); + + if (data == 0xFF) { + // last byte is not erased + nvm_data_ptr = (uint8_t*) i; + + break; + } + } + + // if free flash memory page has been found + if (nvm_data_ptr != 0) { + // go through memory page and find clean (erased state) 64 bit word + for (int i = 0; i < NVM_PAGE_SIZE; i += NVM_WRITE_BYTE_ALIGN) { + // get this byte + data = *(nvm_data_ptr + i); + + if (data == 0xFF) { + // rewind data pointer to this byte + nvm_data_ptr += i; + + // and break the loop + break; + } + } + + // program initialization mark + // unlock flash memory + FLASH_Unlock(); + + // enable programming + FLASH->CR |= FLASH_CR_PG; + + *((uint32_t*)(nvm_data_ptr)) = 0xDEADBEEFu; + WAIT_FOR_PGM_COMPLETION + + *((uint32_t*)(nvm_data_ptr)+ 1) = 0x00000000u; + WAIT_FOR_PGM_COMPLETION + + if (nvm_general_state != NVM_PGM_ERROR) { + nvm_data_ptr += NVM_WRITE_BYTE_ALIGN; + + nvm_general_state = NVM_OK; + } + + FLASH_Lock(); + } + else { + // ig no there is no space on the flash memory + nvm_general_state = NVM_NO_SPACE_LEFT; + } +#endif +} + +nvm_state_result_t nvm_measurement_store(nvm_measurement_t * data) { + + // check if NVM has been initialized and it is ready to work + if (nvm_general_state != NVM_UNINITIALIZED) { + // check if there is a room to store this measurement + if (nvm_general_state != NVM_NO_SPACE_LEFT) { + // progam this measurement + + // move data pointer + + // and check if an end has been found + } + } + +} diff --git a/src/nvm.h b/src/nvm.h new file mode 100644 index 0000000..b44ab92 --- /dev/null +++ b/src/nvm.h @@ -0,0 +1,51 @@ +/* + * nvm.h + * + * Created on: Nov 1, 2022 + * Author: mateusz + */ + +#ifndef NVM_H_ +#define NVM_H_ + +#include + +typedef struct __attribute__((packed)) nvm_measurement_t { + /** + * Date-time timestamp in timezone local for a place where station is installed. + * Mixture of BCD and integer format, this is just sligtly processed RTC registers + * content. + * bit 0 - bit 12 === number of minutes starting from midnight (max 1440) + * bit 13 - bit 21 === days from new year (max 356) + * bit 25 - bit 31 === years (from 00 to 99, from 2000 up to 2099) + */ + uint32_t timestamp; + + /** + * Temperature represented as 0.1 degrees increment and + * humidity in 2% steps + * bit 0 - bit 9 === raw temperature, physical: raw / 10 - 50 (from -50.0 to +52.3) + * bit 10 - bit 15 === raw humidity, physical: raw * 2 (from 0% to 100%) + */ + uint16_t temperature_humidity; + + /** + * Average windspeed and gust windspeed stored in 0.2m/s increments + * bit 0 - bit 7 === raw average windspeed, physical: raw / 5 (from 0m/s up to 50m/s) + */ + uint16_t wind; + +}nvm_measurement_t; + +typedef enum nvm_state_result_t { + NVM_UNINITIALIZED, + NVM_OK, + NVM_NO_SPACE_LEFT, + NVM_INIT_ERROR, + NVM_PGM_ERROR +}nvm_state_result_t; + +void nvm_measurement_init(void); +nvm_state_result_t nvm_measurement_store(nvm_measurement_t * data); + +#endif /* NVM_H_ */ diff --git a/system/src/drivers/max31865.c b/system/src/drivers/max31865.c index e01d959..91a4772 100644 --- a/system/src/drivers/max31865.c +++ b/system/src/drivers/max31865.c @@ -139,6 +139,8 @@ uint8_t max31865_current_config_register = 0; max31865_qf_t max31865_quality_factor = MAX_QF_UNKNOWN; +uint8_t max31865_measurements_counter = 0; + /** * Function generates a content of configuration register basing on */ @@ -342,6 +344,8 @@ void max31865_pool(void) { rte_wx_temperature_average_pt = max31865_get_result(100); } + max31865_measurements_counter++; + max31865_quality_factor = MAX_QF_FULL; } else {