EEPROM 24LC02 integration

This commit is contained in:
2024-10-15 15:55:11 +02:00
parent 888b367a8b
commit 5255b6944a
50 changed files with 6857 additions and 5404 deletions

11
Core/Inc/24LC02.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef INC_24LC02_H_
#define INC_24LC02_H_
#include <stdint.h>
#define EEPROM_ADDR 0b1010000
uint8_t eeprom_init();
uint8_t eeprom_write(uint8_t addr, uint8_t data);
uint8_t eeprom_read(uint8_t addr, uint8_t* data);
#endif

View File

@ -103,4 +103,7 @@ uint8 amsClearError();
uint8 amsReadCellVoltages(Cell_Module* module);
uint8_t amsWriteComm(uint8_t device, uint8_t addr, uint8_t data);
uint8_t amsReadComm(uint8_t device, uint8_t addr, uint8_t* buf);
#endif /* INC_ADBMS_ABSTRACTION_H_ */

25
Core/Inc/ADBMS_I2C.h Normal file
View File

@ -0,0 +1,25 @@
// Write Code:
// ICOMx[3:0]
#define I2C_START 0b0110
#define I2C_STOP 0b0001
#define BLANK 0b0000
#define NO_TRANSMIT 0b0111
// FCOMx[3:0]
#define MSTR_ACK 0b0000
#define MSTR_NO_ACK 0b1000
#define MSTR_NO_ACK_ST 0b1001
// Read Code:
// ICOMx[3:0]
#define MSTR_START 0b0110
#define MSTR_STOP 0b0001
#define BLANK_LOW 0b0000
#define BLANK_HIGH 0b0111
// FCOMx[3:0]
#define MSTR_ACK 0b0000
#define SLV_ACK 0b0111
#define SLV_NO_ACK 0b1111
#define SLV_ACK_STOP 0b0001
#define SLV_NO_ACK_STOP 0b1001

View File

@ -28,6 +28,7 @@ uint16 updateDataPEC(uint16 currentPEC, uint8 din);
uint8 checkDataPEC(uint8* data, uint8 datalen);
uint8 writeCMD(uint16 command, uint8* args, uint8 arglen);
uint8 writeCMD_I2C(uint16 command, uint8* args, uint8 arglen);
uint8 readCMD(uint16 command, uint8* buffer, uint8 buflen);
uint8 pollCMD(uint16 command);

31
Core/Src/24LC02.c Normal file
View File

@ -0,0 +1,31 @@
#include "24LC02.h"
#include "ADBMS_Abstraction.h"
#include <stdint.h>
uint8_t eeprom_init() {
uint8_t StartAddr = 0;
uint8_t data = 0;
if (amsWriteComm(EEPROM_ADDR, StartAddr, data) != 0){
return 1;
}
return 0;
}
uint8_t eeprom_write(uint8_t addr, uint8_t data) {
if (amsWriteComm(EEPROM_ADDR, addr, data) != 0){
return 1;
}
return 0;
}
uint8_t eeprom_read(uint8_t addr, uint8_t* data) {
if (amsReadComm(EEPROM_ADDR, addr, &data) != 0){
return 1;
};
return 0;
}

View File

@ -8,6 +8,7 @@
#include "ADBMS_Abstraction.h"
#include "ADBMS_CMD_MAKROS.h"
#include "ADBMS_LL_Driver.h"
#include "ADBMS_I2C.h"
#include <stddef.h>
uint8 numberofcells;
@ -244,3 +245,34 @@ uint8 amsReadCellVoltages(Cell_Module* module) {
return 0;
}
uint8_t amsWriteComm(uint8_t device, uint8_t addr, uint8_t data) {
uint8_t ctrlByte = (device << 1);
uint8_t txbuf[6] = {I2C_START << 4 | SLV_ACK, ctrlByte,
BLANK << 4 | SLV_ACK, addr,
BLANK << 4 | SLV_ACK_STOP, data};
CHECK_RETURN(writeCMD(WRCOMM, txbuf, COMM_GROUP_SIZE));
CHECK_RETURN(writeCMD(STCOMM, NULL, 0));
return 0;
}
uint8_t amsReadComm(uint8_t device, uint8_t addr, uint8_t* buf) {
uint8_t ctrlByteW = (device << 1);
uint8_t txbuf[6] = {I2C_START << 4 | SLV_ACK, ctrlByteW,
BLANK << 4 | SLV_ACK, addr,
0, 0};
CHECK_RETURN(writeCMD(WRCOMM, txbuf, COMM_GROUP_SIZE));
CHECK_RETURN(writeCMD_I2C(STCOMM, NULL, 0));
uint8_t ctrlByteR = ctrlByteW | 1;
uint8_t rxbuf[6] = {I2C_START << 4 | SLV_ACK, ctrlByteR,
BLANK << 4 | SLV_NO_ACK_STOP, 0,
0, 0};
CHECK_RETURN(writeCMD(WRCOMM, rxbuf, COMM_GROUP_SIZE));
CHECK_RETURN(writeCMD_I2C(STCOMM, NULL, 0));
CHECK_RETURN(readCMD(RDCOMM, rxbuf, COMM_GROUP_SIZE));
*buf = rxbuf[4];
return 0;
}

View File

@ -267,9 +267,45 @@ uint8 writeCMD(uint16 command, uint8* args, uint8 arglen) {
return ret;
}
uint8 writeCMD_I2C(uint16 command, uint8* args, uint8 arglen) {
uint8 ret;
if (arglen > 0) {
uint8 buffer[6 + arglen]; //command + PEC (2 bytes) + data + DPEC (2 bytes)
buffer[0] = (command >> 8) & 0xFF;
buffer[1] = (command) & 0xFF;
calculateCommandPEC(buffer, 4);
for (uint8 i = 0; i < arglen; i++) {
buffer[4 + i] = args[i];
}
calculateDataPEC(&buffer[4], arglen + 2); //DPEC is calculated over the data, not the command, and placed at the end of the data
mcuAdbmsCSLow();
ret = mcuSPITransmit(buffer, 6 + arglen);
mcuAdbmsCSHigh();
} else {
uint8 buffer[4];
buffer[0] = (command >> 8) & 0xFF;
buffer[1] = (command) & 0xFF;
calculateCommandPEC(buffer, 4);
mcuAdbmsCSLow();
ret = mcuSPITransmit(buffer, 4);
HAL_Delay(1);
mcuAdbmsCSHigh();
}
return ret;
}
uint8 readCMD(uint16 command, uint8* buffer, uint8 buflen) {
uint8 txbuffer[6 + buflen];
uint8 rxbuffer[6 + buflen];
uint8 txbuffer[6 + buflen];
uint8 rxbuffer[6 + buflen];
txbuffer[0] = (command >> 8) & 0xFF;
txbuffer[1] = (command)&0xFF;

View File

@ -8,6 +8,7 @@
#include "AMS_HighLevel.h"
#include "ADBMS_Abstraction.h"
#include "TMP1075.h"
#include "24LC02.h"
#include "stm32f3xx_hal.h"
#include <stdint.h>
#include <string.h>
@ -63,7 +64,17 @@ uint8_t AMS_Idle_Loop() {
packetChecksumFails += amsCellMeasurement(&module);
packetChecksumFails += amsCheckUnderOverVoltage(&module);
//tmp1075_measure();
if(eeprom_write(0, 7) != 0){
while(1){}
}
int eepromBuf;
if(eeprom_read(0, &eepromBuf) != 0){
while(1){}
}
if (eepromBuf != 7){
while(1){}
}
return 0;
}

View File

@ -21,7 +21,8 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "AMS_HighLevel.h"
#include "24LC02.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@ -91,6 +92,7 @@ int main(void)
MX_SPI2_Init();
/* USER CODE BEGIN 2 */
AMS_Init(&hspi2);
eeprom_init();
/* USER CODE END 2 */
/* Infinite loop */