2017-06-07 20:09:53 +02:00
|
|
|
/*
|
|
|
|
* eeprom.c
|
|
|
|
*
|
|
|
|
* Created on: Jun 7, 2017
|
|
|
|
* Author: wn
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "eeprom.h"
|
|
|
|
#include "stm32f1xx_hal.h"
|
2017-06-11 22:02:07 +02:00
|
|
|
#include <string.h>
|
2017-06-07 20:09:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
|
|
|
|
|
|
static void __EEPROM_CS(GPIO_PinState v) {
|
|
|
|
HAL_GPIO_WritePin(EEPROM_CS_GPIO_Port, EEPROM_CS_Pin, v);
|
|
|
|
}
|
|
|
|
|
2017-06-12 11:52:46 +02:00
|
|
|
|
|
|
|
uint8_t eepromReadStatus() {
|
|
|
|
struct {
|
|
|
|
uint8_t rdsr;
|
|
|
|
uint8_t data;
|
|
|
|
} txMsg = {
|
|
|
|
.rdsr = EEPROM_RDSR
|
|
|
|
}, rxMsg;
|
|
|
|
|
|
|
|
__EEPROM_CS(LOW);
|
|
|
|
HAL_SPI_TransmitReceive(&hspi1, &txMsg, &rxMsg, 2, HAL_MAX_DELAY);
|
|
|
|
__EEPROM_CS(HIGH);
|
|
|
|
|
|
|
|
return rxMsg.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void eepromWren() {
|
|
|
|
struct {
|
|
|
|
uint8_t wren;
|
|
|
|
} txMsg = {
|
|
|
|
.wren = EEPROM_WREN
|
|
|
|
};
|
|
|
|
|
|
|
|
__EEPROM_CS(LOW);
|
|
|
|
HAL_SPI_Transmit(&hspi1, &txMsg, 1, HAL_MAX_DELAY);
|
|
|
|
__EEPROM_CS(HIGH);
|
|
|
|
}
|
|
|
|
void eepromWrdi() {
|
|
|
|
struct {
|
|
|
|
uint8_t wrdi;
|
|
|
|
} txMsg = {
|
|
|
|
.wrdi = EEPROM_WRDI
|
|
|
|
};
|
|
|
|
|
|
|
|
__EEPROM_CS(LOW);
|
|
|
|
HAL_SPI_Transmit(&hspi1, &txMsg, 1, HAL_MAX_DELAY);
|
|
|
|
__EEPROM_CS(HIGH);
|
2017-06-07 20:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len) {
|
2017-06-11 22:02:07 +02:00
|
|
|
struct {
|
|
|
|
uint8_t write;
|
|
|
|
uint16_t addr;
|
|
|
|
uint8_t data[32];
|
|
|
|
} msg = {
|
|
|
|
.write = EEPROM_WRITE,
|
|
|
|
.addr = addr
|
|
|
|
};
|
|
|
|
memcpy(msg.data, buf, len);
|
2017-06-07 20:09:53 +02:00
|
|
|
|
2017-06-12 11:52:46 +02:00
|
|
|
eepromWren();
|
|
|
|
|
2017-06-11 22:02:07 +02:00
|
|
|
__EEPROM_CS(LOW);
|
2017-06-12 11:52:46 +02:00
|
|
|
HAL_SPI_Transmit(&hspi1, &msg, ((uint16_t)(len+3)), HAL_MAX_DELAY);
|
2017-06-07 20:09:53 +02:00
|
|
|
__EEPROM_CS(HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len) {
|
2017-06-11 22:02:07 +02:00
|
|
|
struct {
|
|
|
|
uint8_t read;
|
|
|
|
uint16_t addr;
|
|
|
|
uint8_t data[32];
|
|
|
|
} txMsg = {
|
|
|
|
.read = EEPROM_READ,
|
|
|
|
.addr = addr
|
|
|
|
},
|
|
|
|
rxMsg;
|
2017-06-07 20:09:53 +02:00
|
|
|
|
2017-06-11 22:02:07 +02:00
|
|
|
__EEPROM_CS(LOW);
|
|
|
|
HAL_SPI_TransmitReceive(&hspi1, &txMsg, &rxMsg, ((uint16_t)(len+3)), HAL_MAX_DELAY);
|
2017-06-07 20:09:53 +02:00
|
|
|
__EEPROM_CS(HIGH);
|
|
|
|
|
2017-06-12 11:52:46 +02:00
|
|
|
memcpy(buf, rxMsg.data, len);
|
2017-06-07 20:09:53 +02:00
|
|
|
}
|