EEPROM 24LC02 integration
This commit is contained in:
31
Core/Src/24LC02.c
Normal file
31
Core/Src/24LC02.c
Normal 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;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user