From a875b349581784f21603844772ab5a15b125571b Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 4 Nov 2020 23:46:55 +0100 Subject: [PATCH] eeprom stuff --- cube/Makefile | 80 +++++++++++++------------- cube/User/Inc/eeprom.h | 31 ++++++++++ cube/User/Src/eeprom.c | 127 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 40 deletions(-) create mode 100644 cube/User/Inc/eeprom.h create mode 100644 cube/User/Src/eeprom.c diff --git a/cube/Makefile b/cube/Makefile index 335b778..58928ec 100644 --- a/cube/Makefile +++ b/cube/Makefile @@ -1,4 +1,4 @@ -# Processed by ../tools/insertMyCode.sh +# Processed by ../tools/insertMyCode.sh ########################################################################################################################## # File automatically-generated by tool: [projectgenerator] version: [3.10.0-B14] date: [Wed Nov 04 21:22:45 CET 2020] ########################################################################################################################## @@ -36,37 +36,37 @@ BUILD_DIR = build # source ###################################### # C sources -C_SOURCES = \ -User/Src/frontend.c User/Src/logger.c User/Src/loopCtrl.c User/Src/main2.c User/Src/mbusComm.c User/Src/ringbuffer.c User/Src/show.c hottislib/PontCoopScheduler.c \ -libmbus/mbus/mbus-protocol.c \ -Core/Src/main.c \ -Core/Src/gpio.c \ -Core/Src/adc.c \ -Core/Src/spi.c \ -Core/Src/usart.c \ -Core/Src/stm32f1xx_it.c \ -Core/Src/stm32f1xx_hal_msp.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_adc.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_adc_ex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \ +C_SOURCES = \ +User/Src/eeprom.c User/Src/frontend.c User/Src/logger.c User/Src/loopCtrl.c User/Src/main2.c User/Src/mbusComm.c User/Src/ringbuffer.c User/Src/show.c hottislib/PontCoopScheduler.c \ +libmbus/mbus/mbus-protocol.c \ +Core/Src/main.c \ +Core/Src/gpio.c \ +Core/Src/adc.c \ +Core/Src/spi.c \ +Core/Src/usart.c \ +Core/Src/stm32f1xx_it.c \ +Core/Src/stm32f1xx_hal_msp.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_adc.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_adc_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \ Core/Src/system_stm32f1xx.c # ASM sources -ASM_SOURCES = \ +ASM_SOURCES = \ startup_stm32f103xe.s @@ -110,8 +110,8 @@ MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI) AS_DEFS = # C defines -C_DEFS = \ --DUSE_HAL_DRIVER \ +C_DEFS = \ +-DUSE_HAL_DRIVER \ -DSTM32F103xE @@ -119,15 +119,15 @@ C_DEFS = \ AS_INCLUDES = # C includes -C_INCLUDES = \ --Ihottislib \ --Ilibmbus \ --IUser/Inc \ --ICore/Inc \ --IDrivers/STM32F1xx_HAL_Driver/Inc \ --IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \ --IDrivers/CMSIS/Device/ST/STM32F1xx/Include \ --IDrivers/CMSIS/Include \ +C_INCLUDES = \ +-Ihottislib \ +-Ilibmbus \ +-IUser/Inc \ +-ICore/Inc \ +-IDrivers/STM32F1xx_HAL_Driver/Inc \ +-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \ +-IDrivers/CMSIS/Device/ST/STM32F1xx/Include \ +-IDrivers/CMSIS/Include \ -IDrivers/CMSIS/Include diff --git a/cube/User/Inc/eeprom.h b/cube/User/Inc/eeprom.h new file mode 100644 index 0000000..07942ba --- /dev/null +++ b/cube/User/Inc/eeprom.h @@ -0,0 +1,31 @@ +/* + * eeprom.h + * + * Created on: Jun 7, 2017 + * Author: wn + */ + +#ifndef EEPROM_H_ +#define EEPROM_H_ + +#include "stm32f1xx_hal.h" + + +#define STORAGE_ADDRESS 0x20 + + +typedef struct { + uint32_t magic; + uint32_t savedTemperature; + uint32_t savedTime; + uint32_t operatingTime; +} tStorage; + + +void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len); +void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len); + +void eepromWriteStorage(); +int eepromReadStorage(); + +#endif /* EEPROM_H_ */ diff --git a/cube/User/Src/eeprom.c b/cube/User/Src/eeprom.c new file mode 100644 index 0000000..fa18354 --- /dev/null +++ b/cube/User/Src/eeprom.c @@ -0,0 +1,127 @@ +/* + * eeprom.c + * + * Created on: Jun 7, 2017 + * Author: wn + */ + +#include "eeprom.h" +#include "stm32f1xx_hal.h" +#include + +#define HIGH GPIO_PIN_SET +#define LOW GPIO_PIN_RESET + +extern SPI_HandleTypeDef hspi1; + + +const uint8_t EEPROM_READ = 0x03; +const uint8_t EEPROM_WRITE = 0x02; +const uint8_t EEPROM_WRDI = 0x04; +const uint8_t EEPROM_WREN = 0x06; +const uint8_t EEPROM_RDSR = 0x05; +const uint8_t EEPROM_WRSR = 0x01; + + +const uint32_t STORAGE_MAGIC = 0xaffe000b; + + + +tStorage storage; +extern tDisplay display; + + +static void __EEPROM_CS(GPIO_PinState v) { + HAL_GPIO_WritePin(EEPROM_CS_GPIO_Port, EEPROM_CS_Pin, v); +} + + +__attribute__((unused)) static uint8_t eepromReadStatus() { + struct { + uint8_t rdsr; + uint8_t data; + } txMsg = { + .rdsr = EEPROM_RDSR + }, rxMsg; + + __EEPROM_CS(LOW); + HAL_SPI_TransmitReceive(&eepromSpi, &txMsg, &rxMsg, 2, HAL_MAX_DELAY); + __EEPROM_CS(HIGH); + + return rxMsg.data; +} + +static void eepromWren() { + struct { + uint8_t wren; + } txMsg = { + .wren = EEPROM_WREN + }; + + __EEPROM_CS(LOW); + HAL_SPI_Transmit(&eepromSpi, &txMsg, 1, HAL_MAX_DELAY); + __EEPROM_CS(HIGH); +} + +__attribute__((unused)) static void eepromWrdi() { + struct { + uint8_t wrdi; + } txMsg = { + .wrdi = EEPROM_WRDI + }; + + __EEPROM_CS(LOW); + HAL_SPI_Transmit(&eepromSpi, &txMsg, 1, HAL_MAX_DELAY); + __EEPROM_CS(HIGH); +} + + +void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len) { + struct { + uint8_t write; + uint16_t addr; + uint8_t data[32]; + } msg = { + .write = EEPROM_WRITE, + .addr = addr + }; + memcpy(msg.data, buf, len); + + eepromWren(); + + __EEPROM_CS(LOW); + HAL_SPI_Transmit(&eepromSpi, &msg, ((uint16_t)(len+3+1)), HAL_MAX_DELAY); + __EEPROM_CS(HIGH); +} + +void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len) { + struct { + uint8_t read; + uint16_t addr; + uint8_t data[32]; + } txMsg = { + .read = EEPROM_READ, + .addr = addr + }, + rxMsg; + + __EEPROM_CS(LOW); + HAL_SPI_TransmitReceive(&eepromSpi, &txMsg, &rxMsg, ((uint16_t)(len+3+1)), HAL_MAX_DELAY); + __EEPROM_CS(HIGH); + + memcpy(buf, rxMsg.data, len); +} + +int eepromReadStorage() { + eepromRead(STORAGE_ADDRESS, (uint8_t*) &storage, sizeof(storage)); + if (storage.magic == STORAGE_MAGIC) { + return 0; + } else { + return -1; + } +} + +void eepromWriteStorage() { + storage.magic = STORAGE_MAGIC; + eepromWrite(STORAGE_ADDRESS, (uint8_t*) &storage, sizeof(storage)); +}