below a CONTROL_SERIAL_NAIVE_USART3 && CONTROL_SERIAL_NAIVE_CRC Arduino example (esp8266) that works but when the 8 byte serial command gets out of sync, the crc failes on the hoverboard. When i reset the esp8266 there seems to be a 1/8 chance that the the buffer is filled correctly :-(
What HAL_UART_ command would reset the buffer ?
ideas welcome !
`#define CONTROL_SERIAL_NAIVE_CRC // Add CRC32 check to control serial
typedef struct{
int16_t steer;
int16_t speed;
#ifdef CONTROL_SERIAL_NAIVE_CRC
uint32_t crc;
#endif
} Serialcommand;
volatile Serialcommand oCmd;
void setup()
{
Serial.begin(115200);
}
#ifdef CONTROL_SERIAL_NAIVE_CRC
uint32_t crc32_for_byte(uint32_t r)
{
for(int j = 0; j < 8; ++j)
r = (r & 1? 0: (uint32_t)0xEDB88320L) ^ r >> 1;
return r ^ (uint32_t)0xFF000000L;
}
void crc32(const void data, size_t n_bytes, uint32_t crc) {
static uint32_t table[0x100];
if(!*table)
for(size_t i = 0; i < 0x100; ++i)
table[i] = crc32_for_byte(i);
for(size_t i = 0; i < n_bytes; ++i)
*crc = table[(uint8_t)crc ^ ((uint8_t)data)[i]] ^ *crc >> 8;
}
#endif
void Send(int16_t iSpeed)
{
oCmd.steer = 0;
oCmd.speed = iSpeed;
#ifdef CONTROL_SERIAL_NAIVE_CRC
uint32_t crc = 0;
crc32((const void *)&oCmd, 4, &crc); // 4 2x uint16_t = 4 bytes
oCmd.crc = crc;
#endif
Serial.write((uint8_t *) &oCmd, sizeof(oCmd));
}
void loop(void)
{
//Send(100);delay(500); return;
for (int i=0; i<= 1000; i +=20)
{
Send(i);
delay(200);
}
for (int i=1000; i>= 0; i -=20)
{
Send(i);
delay(200);
}
for (int i=0; i<= 1000; i +=20)
{
Send(-i);
delay(200);
}
for (int i=1000; i>= 0; i -=20)
{
Send(-i);
delay(200);
}
}`