eeprom working now
This commit is contained in:
parent
7c7e1c4725
commit
371c22768a
@ -61,7 +61,7 @@ void MX_SPI2_Init(void)
|
||||
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi2.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
|
||||
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
|
||||
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Processed by ../tools/insertMyCode.sh
|
||||
##########################################################################################################################
|
||||
# File automatically-generated by tool: [projectgenerator] version: [3.10.0-B14] date: [Thu Nov 05 21:11:36 CET 2020]
|
||||
# File automatically-generated by tool: [projectgenerator] version: [3.10.0-B14] date: [Fri Nov 06 20:44:40 CET 2020]
|
||||
##########################################################################################################################
|
||||
|
||||
# ------------------------------------------------
|
||||
|
@ -1,5 +1,5 @@
|
||||
##########################################################################################################################
|
||||
# File automatically-generated by tool: [projectgenerator] version: [3.10.0-B14] date: [Thu Nov 05 21:11:36 CET 2020]
|
||||
# File automatically-generated by tool: [projectgenerator] version: [3.10.0-B14] date: [Fri Nov 06 20:44:40 CET 2020]
|
||||
##########################################################################################################################
|
||||
|
||||
# ------------------------------------------------
|
||||
|
@ -24,14 +24,20 @@ typedef struct {
|
||||
bool waiting;
|
||||
} t_mbusDevice;
|
||||
|
||||
|
||||
typedef enum {
|
||||
MBCRR_TRIGGERED = 0,
|
||||
MBCRR_BUSY = 1
|
||||
} e_mbusCommRequestResult;
|
||||
|
||||
typedef struct {
|
||||
uint32_t requestCnt;
|
||||
uint32_t errorCnt;
|
||||
} t_mbusCommStats;
|
||||
|
||||
e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice);
|
||||
void mbusCommTxCpltCallback(UART_HandleTypeDef *huart);
|
||||
void mbusCommRxCpltCallback(UART_HandleTypeDef *huart);
|
||||
void mbusCommSetStats(t_mbusCommStats stats);
|
||||
t_mbusCommStats *mbusCommGetStats();
|
||||
|
||||
#endif // _MBUSCOMM_H_
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <eeprom.h>
|
||||
#include <string.h>
|
||||
#include <logger.h>
|
||||
#include <mbusComm.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
|
||||
#define HIGH GPIO_PIN_SET
|
||||
@ -18,11 +19,9 @@ static const uint8_t EEPROM_WREN = 0x06;
|
||||
// static const uint8_t EEPROM_WRSR = 0x01;
|
||||
|
||||
|
||||
static const uint32_t EEPROM_MAGIC = 0xaffe0000;
|
||||
static const uint32_t EEPROM_MAGIC = 0xaffe0005;
|
||||
|
||||
|
||||
// static const uint8_t NUM_OF_BLOCKS;
|
||||
|
||||
typedef union {
|
||||
struct __attribute__((__packed__)) s_eepromHeader {
|
||||
uint32_t magic;
|
||||
@ -48,8 +47,8 @@ typedef union {
|
||||
static const uint16_t DEVICE_STATS_ADDR = 32;
|
||||
static t_deviceStats deviceStats;
|
||||
|
||||
// static const uint8_t NUM_OF_BLOCKS;
|
||||
// static const uint16_t BLOCK_ADDR[] = { 64, 4128 };
|
||||
static const uint8_t NUM_OF_BLOCKS = 2;
|
||||
static const uint16_t BLOCK_ADDR[] = { 64, 4128 };
|
||||
|
||||
|
||||
typedef union {
|
||||
@ -79,8 +78,6 @@ void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len) {
|
||||
};
|
||||
memcpy(msg.s.data, buf, len);
|
||||
|
||||
coloredMsg(LOG_HIGH, "w: %02x %02x %02x", msg.b[0], msg.b[1], msg.b[2]);
|
||||
coloredMsg(LOG_HIGH, "b: %02x %02x %02x %02x %02x %02x %02x %02x", msg.s.data[0], msg.s.data[1], msg.s.data[2], msg.s.data[3], msg.s.data[4], msg.s.data[5], msg.s.data[6], msg.s.data[7]);
|
||||
|
||||
uint8_t writeEnable = EEPROM_WREN;
|
||||
__EEPROM_CS(LOW);
|
||||
@ -97,14 +94,12 @@ void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len) {
|
||||
.s.cmd = EEPROM_READ,
|
||||
.s.addr = swap(addr)
|
||||
};
|
||||
coloredMsg(LOG_HIGH, "r: %02x %02x %02x", txMsg.b[0], txMsg.b[1], txMsg.b[2]);
|
||||
|
||||
t_spiMsg rxMsg;
|
||||
|
||||
__EEPROM_CS(LOW);
|
||||
HAL_SPI_TransmitReceive(&eepromSpi, txMsg.b, rxMsg.b, ((uint16_t)(len+3)), HAL_MAX_DELAY);
|
||||
__EEPROM_CS(HIGH);
|
||||
coloredMsg(LOG_HIGH, "b: %02x %02x %02x %02x %02x %02x %02x %02x", rxMsg.s.data[0], rxMsg.s.data[1], rxMsg.s.data[2], rxMsg.s.data[3], rxMsg.s.data[4], rxMsg.s.data[5], rxMsg.s.data[6], rxMsg.s.data[7]);
|
||||
|
||||
memcpy(buf, rxMsg.s.data, len);
|
||||
}
|
||||
@ -115,11 +110,30 @@ void eepromSpiTxCpltCallback(SPI_HandleTypeDef *hspi) {
|
||||
|
||||
static void eepromHourlyUpdateDeviceStats(void *handle) {
|
||||
deviceStats.s.totalRunningHours += 1;
|
||||
logMsg("eeI, about to write updated device stats");
|
||||
|
||||
t_mbusCommStats *stats = mbusCommGetStats();
|
||||
|
||||
deviceStats.s.totalRequests = stats->requestCnt;
|
||||
deviceStats.s.totalFailures = stats->errorCnt;
|
||||
|
||||
logMsg("eeHUDS, about to write updated device stats");
|
||||
logMsg("eeHUDS, total powercycles so far: %d", deviceStats.s.totalPowercycles);
|
||||
logMsg("eeHUDS, total running hours so far: %d", deviceStats.s.totalRunningHours);
|
||||
logMsg("eeHUDS, total requests so far: %d", deviceStats.s.totalRequests);
|
||||
logMsg("eeHUDS, total failures so far: %d", deviceStats.s.totalFailures);
|
||||
|
||||
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
|
||||
}
|
||||
|
||||
// active waiting, use only during initialization!
|
||||
static void eepromActiveDelay(uint8_t delay_ms) {
|
||||
uint32_t startTime = HAL_GetTick();
|
||||
while (startTime + delay_ms > HAL_GetTick());
|
||||
}
|
||||
|
||||
void eepromInit() {
|
||||
__EEPROM_CS(HIGH);
|
||||
|
||||
logMsg("eeI, read header");
|
||||
eepromRead(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader));
|
||||
coloredMsg(LOG_RED, "eeI, magic: %08x", eepromHeader.s.magic);
|
||||
@ -133,24 +147,26 @@ void eepromInit() {
|
||||
deviceStats.s.totalFailures = 0;
|
||||
coloredMsg(LOG_RED, "eeI, about to write device stats for the first time");
|
||||
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
|
||||
eepromActiveDelay(7);
|
||||
|
||||
/*
|
||||
uint8_t emptyBlock[32];
|
||||
memset(emptyBlock, 0, sizeof(emptyBlock));
|
||||
for (uint8_t i = 0; i < 2; i++) {
|
||||
for (uint8_t i = 0; i < NUM_OF_BLOCKS; i++) {
|
||||
for (uint8_t j = 0; j <= 127; j++) {
|
||||
uint16_t addr = BLOCK_ADDR[i] + sizeof(emptyBlock) * j;
|
||||
coloredMsg(LOG_RED, "eeI, about to write empty block at %d", addr);
|
||||
eepromWrite(addr, emptyBlock, sizeof(emptyBlock));
|
||||
eepromActiveDelay(7);
|
||||
}
|
||||
}
|
||||
*/
|
||||
coloredMsg(LOG_RED, "eeI, storage blocks initialized");
|
||||
|
||||
|
||||
eepromHeader.s.magic = EEPROM_MAGIC;
|
||||
eepromHeader.s.activeBlock = 0;
|
||||
eepromHeader.s.writeCounter = 1;
|
||||
coloredMsg(LOG_RED, "eeI, about to write header for the first time");
|
||||
eepromWrite(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader));
|
||||
eepromActiveDelay(7);
|
||||
coloredMsg(LOG_GREEN, "eeI, eeprom has been initialized");
|
||||
} else {
|
||||
coloredMsg(LOG_GREEN, "eeI, eeprom is initialized");
|
||||
@ -163,10 +179,14 @@ void eepromInit() {
|
||||
coloredMsg(LOG_GREEN, "eeI, total requests so far: %d", deviceStats.s.totalRequests);
|
||||
coloredMsg(LOG_GREEN, "eeI, total failures so far: %d", deviceStats.s.totalFailures);
|
||||
|
||||
t_mbusCommStats stats = { .requestCnt = deviceStats.s.totalRequests, .errorCnt = deviceStats.s.totalFailures};
|
||||
mbusCommSetStats(stats);
|
||||
|
||||
deviceStats.s.totalPowercycles += 1;
|
||||
coloredMsg(LOG_GREEN, "eeI, about to write device stats with updated power cycles counter");
|
||||
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
|
||||
eepromActiveDelay(7);
|
||||
|
||||
schAdd(eepromHourlyUpdateDeviceStats, NULL, 0, 10 * 1000);
|
||||
schAdd(eepromHourlyUpdateDeviceStats, NULL, 0, 60 * 60 * 1000);
|
||||
coloredMsg(LOG_GREEN, "eeI, hourly device stats update scheduled");
|
||||
}
|
||||
|
@ -187,11 +187,11 @@ void my_setup_2() {
|
||||
|
||||
eepromInit();
|
||||
|
||||
// frontendInit();
|
||||
// frontendSetThreshold(240);
|
||||
frontendInit();
|
||||
frontendSetThreshold(240);
|
||||
|
||||
// schAdd(scheduleMBusRequest, NULL, 0, 1000);
|
||||
// schAdd(triggerMBusRequest, NULL, 0, 100);
|
||||
schAdd(scheduleMBusRequest, NULL, 0, 1000);
|
||||
schAdd(triggerMBusRequest, NULL, 0, 100);
|
||||
}
|
||||
|
||||
void my_loop() {
|
||||
|
@ -38,7 +38,7 @@ typedef enum {
|
||||
typedef enum {
|
||||
MBCS_IDLE,
|
||||
MBCS_SEND,
|
||||
MBCS_SEND_CONT,
|
||||
MBCS_SEND_CONTINUED,
|
||||
MBCS_SENDING_DONE,
|
||||
MBCS_ENABLE_FRONTEND,
|
||||
MBCS_START1,
|
||||
@ -88,8 +88,17 @@ typedef struct {
|
||||
|
||||
static t_mbusCommHandle mbusCommHandle = { .state = MBCS_IDLE, .retryCnt = 0, .cmd = 0, .addr = 0, .receiveCnt = 0, .receivedOctet = 0, .receiving = false };
|
||||
|
||||
static t_mbusCommStats mbusCommStats = { .requestCnt = 0, .errorCnt = 0 };
|
||||
|
||||
|
||||
void mbusCommSetStats(t_mbusCommStats stats) {
|
||||
mbusCommStats = stats;
|
||||
}
|
||||
|
||||
t_mbusCommStats *mbusCommGetStats() {
|
||||
return &mbusCommStats;
|
||||
}
|
||||
|
||||
static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
|
||||
t_longframe *frame = &(mbusCommHandle->frame);
|
||||
|
||||
@ -196,11 +205,11 @@ static void handleRequestEngine(void *handle) {
|
||||
localMbusCommHandle->sendBuf[2] = localMbusCommHandle->addr;
|
||||
localMbusCommHandle->sendBuf[3] = localMbusCommHandle->cmd + localMbusCommHandle->addr; // checksum
|
||||
localMbusCommHandle->sendBuf[4] = 0x16;
|
||||
localMbusCommHandle->state = MBCS_SEND_CONT;
|
||||
localMbusCommHandle->state = MBCS_SEND_CONTINUED;
|
||||
// no break !!
|
||||
|
||||
case MBCS_SEND_CONT:
|
||||
logMsg("hre state SEND_CONT");
|
||||
case MBCS_SEND_CONTINUED:
|
||||
logMsg("hre state SEND_CONTINUED");
|
||||
show(LED_RED, OFF);
|
||||
if (! loopActive) {
|
||||
logMsg("hre enabling loop, try %d", localMbusCommHandle->retryCnt);
|
||||
@ -375,6 +384,7 @@ static void handleRequestEngine(void *handle) {
|
||||
|
||||
case MBCS_TIMEOUT:
|
||||
errMsg("hre state TIMEOUT");
|
||||
mbusCommStats.errorCnt += 1;
|
||||
localMbusCommHandle->device->failures += 1;
|
||||
localMbusCommHandle->receiving = false;
|
||||
if (localMbusCommHandle->frame.userdata != NULL) {
|
||||
@ -415,16 +425,15 @@ void mbusCommRxCpltCallback(UART_HandleTypeDef *huart) {
|
||||
}
|
||||
|
||||
e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) {
|
||||
static uint32_t cnt = 0;
|
||||
e_mbusCommRequestResult res = MBCRR_BUSY;
|
||||
|
||||
if (mbusCommHandle.state == MBCS_IDLE) {
|
||||
coloredMsg(LOG_BLUE, "*** NEW REQUEST %s R:%d F:%d C:%d ***",
|
||||
coloredMsg(LOG_BLUE, "*** NEW REQUEST %s R:%d F:%d GRC:%d GEC:%d ***",
|
||||
mbusDevice->deviceName,
|
||||
mbusDevice->requests,
|
||||
mbusDevice->failures,
|
||||
cnt);
|
||||
cnt++;
|
||||
mbusCommStats.requestCnt,
|
||||
mbusCommStats.errorCnt);
|
||||
|
||||
mbusCommHandle.state = MBCS_SEND;
|
||||
mbusCommHandle.retryCnt = 0;
|
||||
@ -434,6 +443,8 @@ e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) {
|
||||
mbusDevice->requests += 1;
|
||||
schAdd(handleRequestEngine, (void*) &mbusCommHandle, 0, 0);
|
||||
res = MBCRR_TRIGGERED;
|
||||
|
||||
mbusCommStats.requestCnt += 1;
|
||||
}
|
||||
|
||||
return res;
|
||||
|
@ -239,10 +239,10 @@ SPI1.Direction=SPI_DIRECTION_2LINES
|
||||
SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate
|
||||
SPI1.Mode=SPI_MODE_MASTER
|
||||
SPI1.VirtualType=VM_MASTER
|
||||
SPI2.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_32
|
||||
SPI2.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_4
|
||||
SPI2.CLKPhase=SPI_PHASE_1EDGE
|
||||
SPI2.CLKPolarity=SPI_POLARITY_LOW
|
||||
SPI2.CalculateBaudRate=1.125 MBits/s
|
||||
SPI2.CalculateBaudRate=9.0 MBits/s
|
||||
SPI2.Direction=SPI_DIRECTION_2LINES
|
||||
SPI2.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,BaudRatePrescaler,CLKPolarity,CLKPhase
|
||||
SPI2.Mode=SPI_MODE_MASTER
|
||||
|
Loading…
x
Reference in New Issue
Block a user