V1.9
This commit is contained in:
@ -1,82 +1,65 @@
|
||||
#include "eeprom.h"
|
||||
#include "stm32f3xx_hal.h"
|
||||
/*
|
||||
* PWM_control.h
|
||||
*
|
||||
* Created on: 10.07.2024
|
||||
* Author: Hamza
|
||||
*/
|
||||
|
||||
#include <eeprom.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#define EEPROM_I2C_ADDR 0xA0
|
||||
// Don't use the beginning of the EEPROM, since the testbench writes there
|
||||
#define EEPROM_CONFIG_BASE 0x0100
|
||||
#define EEPROM_CONFIG_ECC_OFFSET 0x20
|
||||
|
||||
EEPROMConfig eeprom_config;
|
||||
uint32_t current_address;
|
||||
// see Datasheet for these values
|
||||
#define EEPROM_I2C_ADDR 0xB0
|
||||
#define EEPROM_WRITE_FREQ_INACTIVE 1000
|
||||
#define EEPROM_WRITE_FREQ_ACTIVE 100
|
||||
#define EERROM_MEMORY_ADDR_SIZE 2
|
||||
#define EEPROM_MEMORY_SIZE 1024000 // in bits
|
||||
#define EEPROM_PAGE_SIZE 256 // in bits
|
||||
|
||||
static I2C_HandleTypeDef* hi2c;
|
||||
uint32_t current_address;
|
||||
|
||||
void eeprom_init(I2C_HandleTypeDef* handle) {
|
||||
hi2c = handle;
|
||||
current_address = 0;
|
||||
uint8_t buf[sizeof(EEPROMConfig) * 3];
|
||||
// Read 3 EEPROM config buffers at 32 byte offsets
|
||||
for (size_t ecc_i = 0; ecc_i < 3; ecc_i++) {
|
||||
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(
|
||||
hi2c, EEPROM_I2C_ADDR,
|
||||
EEPROM_CONFIG_BASE + EEPROM_CONFIG_ECC_OFFSET * ecc_i, 2,
|
||||
buf + sizeof(EEPROMConfig) * ecc_i, sizeof(EEPROMConfig), 100);
|
||||
if (status != HAL_OK) {
|
||||
set_error_source(ERROR_SOURCE_EEPROM);
|
||||
}
|
||||
}
|
||||
// ECC
|
||||
for (size_t i = 0; i < sizeof(EEPROMConfig); i++) {
|
||||
const uint8_t a = buf[i + sizeof(EEPROMConfig) * 0];
|
||||
const uint8_t b = buf[i + sizeof(EEPROMConfig) * 1];
|
||||
const uint8_t c = buf[i + sizeof(EEPROMConfig) * 2];
|
||||
if (a == b || a == c) {
|
||||
buf[i] = a;
|
||||
} else if (b == c) {
|
||||
buf[i] = b;
|
||||
} else {
|
||||
set_error_source(ERROR_SOURCE_EEPROM);
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(&eeprom_config, buf, sizeof(EEPROMConfig));
|
||||
// Write back config
|
||||
// eeprom_config.id = 3;
|
||||
eeprom_config_save();
|
||||
}
|
||||
|
||||
void eeprom_config_save() {
|
||||
for (size_t ecc_i = 0; ecc_i < 3; ecc_i++) {
|
||||
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(
|
||||
hi2c, EEPROM_I2C_ADDR,
|
||||
EEPROM_CONFIG_BASE + EEPROM_CONFIG_ECC_OFFSET * ecc_i, 2,
|
||||
(uint8_t*)&eeprom_config, sizeof(eeprom_config), 100);
|
||||
if (status != HAL_OK) {
|
||||
set_error_source(ERROR_SOURCE_EEPROM);
|
||||
}
|
||||
HAL_Delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
void eeprom_write_status(){
|
||||
uint8_t data_length = 16;
|
||||
uint8_t data_length = EEPROM_PAGE_SIZE/8; //32
|
||||
uint8_t data[data_length] = {};
|
||||
|
||||
data[0] = ((state.current_state << 4) | (current_powerground_status >> 4));
|
||||
data[1] = ((current_powerground_status << 4) | (state.error_source >> 4)); // 4 bit powerground | 4 bit error
|
||||
data[2] = ((state.error_source << 4) | (0)); // 4 bit error | 4 bit state of charge
|
||||
data[3] = (0); // 8 bit state of charge
|
||||
data[4] = (RELAY_BAT_SIDE_VOLTAGE >> 8); // 16 bit battery voltage
|
||||
data[5] = (RELAY_BAT_SIDE_VOLTAGE);
|
||||
data[6] = (RELAY_ESC_SIDE_VOLTAGE >> 8); // 16 bit Inverter voltage
|
||||
data[7] = (RELAY_ESC_SIDE_VOLTAGE >> 8);
|
||||
data[8] = (CURRENT_MEASUREMENT >> 8); // 16 bit Inverter voltage
|
||||
data[9] = (CURRENT_MEASUREMENT);
|
||||
|
||||
|
||||
eeprom_write(data, data_length);
|
||||
}
|
||||
|
||||
void eeprom_read(int8_t* data){}
|
||||
|
||||
void eeprom_write(uint8_t* data, uint8_t data_length){
|
||||
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(
|
||||
hi2c, EEPROM_I2C_ADDR,
|
||||
current_address, 2,
|
||||
(uint8_t*)&eeprom_config, sizeof(eeprom_config), 100);
|
||||
if (status != HAL_OK) {
|
||||
set_error_source(ERROR_SOURCE_EEPROM);
|
||||
// data 0-23
|
||||
for (int i = 0; i < 13; i++) {
|
||||
data[i+10] = ((int) module.auxVoltages[i]) >> 8;
|
||||
data[i+11] = ((int) module.auxVoltages[i]);
|
||||
}
|
||||
eeprom_write(data, current_address);
|
||||
current_address++;
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef eeprom_read(uint8_t* data, uint16_t address){
|
||||
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(
|
||||
hi2c, EEPROM_I2C_ADDR, address,
|
||||
2, data, sizeof(data), 100);
|
||||
return status;
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef eeprom_write(uint8_t* data, uint16_t address){
|
||||
HAL_GPIO_WritePin(EEPROM___WC__GPIO_Port, EEPROM___WC__Pin, GPIO_PIN_RESET);
|
||||
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(
|
||||
hi2c, EEPROM_I2C_ADDR,
|
||||
current_address, EERROM_MEMORY_ADDR_SIZE,
|
||||
data, sizeof(data), 100);
|
||||
return status;
|
||||
HAL_GPIO_WritePin(EEPROM___WC__GPIO_Port, EEPROM___WC__Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user