Add i2c_message_t and use it all across the codebase. This will allow handling variable-length I2C messages and validation with minimal effort. The test LED and brightness PWM update features got temporarily broken and will fix them soon.
This commit is contained in:
@@ -36,21 +36,20 @@ void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash)
|
||||
|
||||
crc16_data_t crc16data;
|
||||
|
||||
void CRC16_AppendToMessage(uint8_t *message, uint32_t lengthInBytes)
|
||||
void CRC16_UpdateMessageChecksum(i2c_message_t *message)
|
||||
{
|
||||
uint16_t hash;
|
||||
crc16_init(&crc16data);
|
||||
crc16_update(&crc16data, message, lengthInBytes);
|
||||
crc16_update(&crc16data, message->data, message->length);
|
||||
crc16_finalize(&crc16data, &hash);
|
||||
message[lengthInBytes] = hash & 0xff;
|
||||
message[lengthInBytes+1] = hash >> 8;
|
||||
message->crc = hash;
|
||||
}
|
||||
|
||||
bool CRC16_IsMessageValid(uint8_t *message, uint32_t lengthInBytes)
|
||||
bool CRC16_IsMessageValid(i2c_message_t *message)
|
||||
{
|
||||
uint16_t hash;
|
||||
crc16_init(&crc16data);
|
||||
crc16_update(&crc16data, message, lengthInBytes);
|
||||
crc16_update(&crc16data, message->data, message->length);
|
||||
crc16_finalize(&crc16data, &hash);
|
||||
return (message[lengthInBytes] == (hash & 0xff)) && (message[lengthInBytes+1] == (hash >> 8));
|
||||
return message->crc == hash;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "slave_protocol.h"
|
||||
|
||||
#define CRC16_HASH_LENGTH 2 // bytes
|
||||
|
||||
@@ -25,7 +26,7 @@ void crc16_update(crc16_data_t *crc16Config, const uint8_t *src, uint32_t length
|
||||
//! @param hash Pointer to the value returned for the final calculated crc value.
|
||||
void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash);
|
||||
|
||||
void CRC16_AppendToMessage(uint8_t *message, uint32_t lengthInBytes);
|
||||
bool CRC16_IsMessageValid(uint8_t *message, uint32_t lengthInBytes);
|
||||
void CRC16_UpdateMessageChecksum(i2c_message_t *message);
|
||||
bool CRC16_IsMessageValid(i2c_message_t *message);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
#ifndef __SLAVE_PROTOCOL_H__
|
||||
#define __SLAVE_PROTOCOL_H__
|
||||
|
||||
// Macros:
|
||||
|
||||
#define I2C_MESSAGE_MAX_LENGTH 255
|
||||
#define I2C_BUFFER_MAX_LENGTH (I2C_MESSAGE_MAX_LENGTH + 3)
|
||||
|
||||
// Typedefs:
|
||||
|
||||
typedef enum {
|
||||
@@ -10,4 +15,10 @@
|
||||
SlaveCommand_JumpToBootloader,
|
||||
} slave_command_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t length;
|
||||
uint16_t crc;
|
||||
uint8_t data[I2C_MESSAGE_MAX_LENGTH];
|
||||
} __attribute__ ((packed)) i2c_message_t;
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user