protocol.h (2604B)
1 #ifndef __PROTOCOL_H__ 2 #define __PROTOCOL_H__ 3 4 #include <stdint.h> 5 #include <stdbool.h> 6 7 #define PACKET_MAX_DATA_LEN (DMA_RX_BUFFER_SIZE - 5) 8 9 /************************************************************* 10 * TLV: Tag, Length, Value 11 * Packet Structure: 12 * |------|------|------|------|-----|------|------|------| 13 * | tag | len* | dat1 | dat2 | ... | datn | crcM | crcL | 14 * |______|______|______|______|_____|______|______|______| 15 * 16 * Note: len specifies the number of data bytes, NOT the packet length. 17 18 * 19 * ****** Read parameters (PR) Command structure: 20 * | tag | len | addr | n | crcM | crcL | 21 * len = 2 22 * addr = address to read from 23 * n = number of bytes to read 24 * 25 * Response structure: 26 * | tag | len | data1 ... datan | crcM | crcL 27 * tag = ACK or NACK 28 * len = n(if ACK) or 0(if NACK) 29 * 30 31 * ****** Write parameters (PW) Command structure: 32 * | tag | len | dat0 | dat1 |...| datn | crcM | crcL | 33 * len = (number of data bytes) 34 * NOTE: write address is always zero. Because in the process 35 * of updating parameters stored in flash memeory, the entire 36 * sector should be wiped out anyway. 37 * 38 * Response structure: 39 * | tag | len | crcM | crcL 40 * tag = ACK or NACK 41 * len = 0 42 * 43 * ****** Read firmware info (FIR) Command structure: 44 * | tag | len | addr | n | crcM | crcL | 45 * len = 2 46 * addr = address to read from 47 * n = number of bytes to read 48 * 49 * Response structure: 50 * | tag | len | data1 ... datan | crcM | crcL 51 * tag = ACK or NACK 52 * len = n(if ACK) or 0(if NACK) 53 * 54 ************************************************************/ 55 56 #define PACK_TAG_INDEX 0U 57 #define PACK_LENGTH_INDEX 1U 58 #define PACK_DATA_INDEX 2 59 60 typedef enum { 61 PROTOCOL_TAG_PR, 62 PROTOCOL_TAG_PW, 63 PROTOCOL_TAG_FIR, 64 PROTOCOL_TAG_RST, 65 PROTOCOL_TAG_ACK, 66 PROTOCOL_TAG_NACK, 67 PROTOCOL_TAG_LOCK, 68 PROTOCOL_TAG_UNLOCK, 69 PROTOCOL_TAG_DIAG, 70 71 // Frimware update tags 72 PROTOCOL_TAG_SYNC_REQ, 73 PROTOCOL_TAG_UPDATE_REQ, 74 PROTOCOL_TAG_DEV_ID_CHECK, 75 PROTOCOL_TAG_FW_UPDATE_SIZE, 76 PROTOCOL_TAG_ERASE_REQ, 77 PROTOCOL_TAG_FW_NEW, 78 PROTOCOL_TAG_FW_REP, 79 80 PROTOCOL_TAG_INVALID, 81 } ProtocolTag_t; 82 83 typedef struct { 84 ProtocolTag_t tag; 85 uint8_t offset; 86 uint8_t length; 87 } ProtocolPacketAttr_t; 88 89 typedef struct __attribute__((__packed__)) { 90 ProtocolTag_t tag; 91 uint8_t length; 92 uint16_t crc; 93 } ProtocolPacket_t; 94 95 ProtocolPacketAttr_t protocol_extract_packet_attr(uint8_t *packet, bool isFirmware); 96 void protocol_create_packet(uint8_t *buffer, ProtocolTag_t tag, uint8_t length); 97 98 #endif // __PROTOCOL_H__