eeprom stuff

This commit is contained in:
Wolfgang Hottgenroth 2020-11-05 13:05:19 +01:00
parent a875b34958
commit 076eca0a49
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
2 changed files with 34 additions and 103 deletions

View File

@ -8,24 +8,11 @@
#ifndef EEPROM_H_ #ifndef EEPROM_H_
#define EEPROM_H_ #define EEPROM_H_
#include "stm32f1xx_hal.h" #include <stdint.h>
#define STORAGE_ADDRESS 0x20 #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 eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len);
void eepromRead(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_ */ #endif /* EEPROM_H_ */

View File

@ -1,18 +1,12 @@
/* #include <main.h>
* eeprom.c #include <spi.h>
* #include <eeprom.h>
* Created on: Jun 7, 2017 // #include <stm32f1xx_hal.h>
* Author: wn
*/
#include "eeprom.h"
#include "stm32f1xx_hal.h"
#include <string.h> #include <string.h>
#define HIGH GPIO_PIN_SET #define HIGH GPIO_PIN_SET
#define LOW GPIO_PIN_RESET #define LOW GPIO_PIN_RESET
extern SPI_HandleTypeDef hspi1;
const uint8_t EEPROM_READ = 0x03; const uint8_t EEPROM_READ = 0x03;
@ -27,101 +21,51 @@ const uint32_t STORAGE_MAGIC = 0xaffe000b;
tStorage storage;
extern tDisplay display;
static void __EEPROM_CS(GPIO_PinState v) { typedef union {
struct {
uint8_t cmd;
uint16_t addr;
uint8_t data[32];
} s;
uint8_t b[35];
} t_spiMsg;
inline static void __EEPROM_CS(GPIO_PinState v) {
HAL_GPIO_WritePin(EEPROM_CS_GPIO_Port, EEPROM_CS_Pin, 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) { void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len) {
struct { t_spiMsg msg = {
uint8_t write; .s.cmd = EEPROM_WRITE,
uint16_t addr; .s.addr = addr
uint8_t data[32];
} msg = {
.write = EEPROM_WRITE,
.addr = addr
}; };
memcpy(msg.data, buf, len); memcpy(msg.s.data, buf, len);
eepromWren(); uint8_t writeEnable = EEPROM_WREN;
__EEPROM_CS(LOW); __EEPROM_CS(LOW);
HAL_SPI_Transmit(&eepromSpi, &msg, ((uint16_t)(len+3+1)), HAL_MAX_DELAY); HAL_SPI_Transmit(&eepromSpi, &writeEnable, 1, HAL_MAX_DELAY);
__EEPROM_CS(HIGH);
__EEPROM_CS(LOW);
HAL_SPI_Transmit(&eepromSpi, msg.b, ((uint16_t)(len+3+1)), HAL_MAX_DELAY);
__EEPROM_CS(HIGH); __EEPROM_CS(HIGH);
} }
void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len) { void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len) {
struct { t_spiMsg txMsg = {
uint8_t read; .s.cmd = EEPROM_READ,
uint16_t addr; .s.addr = addr
uint8_t data[32]; };
} txMsg = {
.read = EEPROM_READ, t_spiMsg rxMsg;
.addr = addr
},
rxMsg;
__EEPROM_CS(LOW); __EEPROM_CS(LOW);
HAL_SPI_TransmitReceive(&eepromSpi, &txMsg, &rxMsg, ((uint16_t)(len+3+1)), HAL_MAX_DELAY); HAL_SPI_TransmitReceive(&eepromSpi, txMsg.b, rxMsg.b, ((uint16_t)(len+3+1)), HAL_MAX_DELAY);
__EEPROM_CS(HIGH); __EEPROM_CS(HIGH);
memcpy(buf, rxMsg.data, len); memcpy(buf, rxMsg.s.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));
} }