Skip to content

Commit

Permalink
Improve emulated EEPROM reload handling
Browse files Browse the repository at this point in the history
Add variable to control if ESC support read size 4
or 8 Byte. Depends on ESC.

Add variable and function to set user function
to perform emulated EEPROM reload.

Add EEPROM error codes that can be used to indicatë
crc error on reload.
  • Loading branch information
Andreas Karlsson committed Apr 8, 2024
1 parent 66111d5 commit 0c7becc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
68 changes: 62 additions & 6 deletions soes/esc_eep.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <string.h>

static uint8_t eep_buf[8];
static uint16_t eep_read_size = 8U;
static uint16_t (*eep_reload_ptr)(void) = NULL;

/** EPP periodic task of ESC side EEPROM emulation.
*
Expand Down Expand Up @@ -51,23 +53,57 @@ void EEP_process (void)
break;

case EEP_CMD_READ:
case EEP_CMD_RELOAD:
/* handle read request */
if (EEP_read (stat.addr * 2U /* sizeof(uint16_t) */, eep_buf, EEP_READ_SIZE) != 0) {
if (EEP_read (stat.addr * 2U /* sizeof(uint16_t) */, eep_buf, eep_read_size) != 0) {
stat.contstat.bits.ackErr = 1;
} else {
ESC_write (ESCREG_EEDATA, eep_buf, EEP_READ_SIZE);
}
else {
ESC_write(ESCREG_EEDATA, eep_buf, eep_read_size);
}
break;
case EEP_CMD_RELOAD:
/* user defined reload if set */
if (eep_reload_ptr != NULL) {
uint16_t reload_ret = (*eep_reload_ptr)();
if (reload_ret != 0) {
stat.contstat.bits.ackErr = 1;
if (reload_ret & EEP_ERROR_CSUM) {
stat.contstat.bits.csumErr = 1;
}
}
}
else {
if (eep_read_size == 8U) {
/* handle reload request */
if (EEP_read(stat.addr * 2U /* sizeof(uint16_t) */, eep_buf, eep_read_size) != 0) {
stat.contstat.bits.ackErr = 1;
}
else {
ESC_write(ESCREG_EEDATA, eep_buf, eep_read_size);
}
}
else {
/* Default handler of reload request for 4 Byte read, load config alias.
* To support other ESC behavior, implement user defined reload.
*/
if (EEP_read(EEP_CONFIG_ALIAS_WORD_OFFSET * 2U /* sizeof(uint16_t) */,
eep_buf,
2U /* 2 Bytes config alias*/) != 0) {
stat.contstat.bits.ackErr = 1;
}
else {
ESC_write(ESCREG_EEDATA, eep_buf, 2U /* 2 Bytes config alias*/);
}
}
}
break;

case EEP_CMD_WRITE:
/* handle write request */
ESC_read (ESCREG_EEDATA, eep_buf, EEP_WRITE_SIZE);
if (EEP_write (stat.addr * 2U /* sizeof(uint16_t) */, eep_buf, EEP_WRITE_SIZE) != 0) {
stat.contstat.bits.ackErr = 1;
}
break;

default:
stat.contstat.bits.ackErr = 1;
}
Expand All @@ -78,3 +114,23 @@ void EEP_process (void)
}
}

/** EPP Set read size, 4 Byte or 8 Byte depending on ESC.
* Default 8 Byte.
*/
void EEP_set_read_size (uint16_t read_size)
{
if ((read_size == 8U) || (read_size == 4U))
{
eep_read_size = read_size;
}
}

/** EPP Set reload fucntion pointer.
* Function shall return 0 on success, else return
* defined error mask eg. EEP_ERROR_CSUM on CRC error
* on reload.
*/
void EEP_set_reload_function_pointer (uint16_t (*reload_ptr)(void))
{
eep_reload_ptr = reload_ptr;
}
28 changes: 21 additions & 7 deletions soes/esc_eep.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,25 @@
#include <cc.h>
#include "esc.h"



/* EEPROM ERRORs */
#define EEP_ERROR_CSUM (1U << 11)
#define EEP_ERROR_LOADING (1U << 12)
#define EEP_ERROR_ACK (1U << 13)
#define EEP_ERROR_WRITE (1U << 14)

/* EEPROM commands */
#define EEP_CMD_IDLE 0x0
#define EEP_CMD_READ 0x1
#define EEP_CMD_WRITE 0x2
#define EEP_CMD_RELOAD 0x4
#define EEP_CMD_IDLE 0x0
#define EEP_CMD_READ 0x1
#define EEP_CMD_WRITE 0x2
#define EEP_CMD_RELOAD 0x4

/* read/write size */
#define EEP_READ_SIZE 8
#define EEP_WRITE_SIZE 2
/* write size */
#define EEP_WRITE_SIZE 2

/* EEPROm word offset */
#define EEP_CONFIG_ALIAS_WORD_OFFSET 4

/* CONSTAT register content */
typedef struct CC_PACKED
Expand Down Expand Up @@ -69,6 +79,10 @@ typedef union eep_config
/* periodic task */
void EEP_process (void);

/* Set eep internal variables */
void EEP_set_read_size (uint16_t read_size);
void EEP_set_reload_function_pointer (uint16_t (*reload_ptr)(void));

/* From hardware file */
void EEP_init (void);
int8_t EEP_read (uint32_t addr, uint8_t *data, uint16_t size);
Expand Down

0 comments on commit 0c7becc

Please sign in to comment.