#include "p_serial_mgr.h" #include "putil.h" #include "stm32l4xx_hal_uart.h" #define MAX_SERIAL_BUFFER_SIZE (262) // Actually 261, add 1 for padding // this protocol is very similar to the digi api v2 (inspired from) // [0]{1} Start delimiter 0x7E // [1]{1} Source Address // [2]{1} Destination Address // [3]{1} Length Byte (of non-delimited framebuffer) // [4-n]{n} frame data // [n+1]{1} checksum // Escape bytes are 0x7D, only frame data can be escaped // Escaped bytes are followed by the byte to be escaped XOR'd with 0x20 // 0x7D and 0x7E need to be escaped (within the frame data) typedef enum serial_state_t { SS_IDLE = 0, // waiting SS_START = 1, // get start byte, interrupt after 4 more bytes } serial_state_t; static UART_HandleTypeDef *_serial_huart_inst = NULL; static uint8_t rxc = '\0'; static serial_state_t sstate = SS_IDLE; void UART1_RxCpltCallback(UART_HandleTypeDef *huart) { if (rxc == 0x7E && sstate == SS_IDLE) { sstate = SS_START; } } void p_serial_mgr_init(UART_HandleTypeDef *huart) { _serial_huart_inst = huart; _serial_huart_inst->RxCpltCallback = UART1_RxCpltCallback; } void p_serial_mgr_service(void) { if (sstate == SS_START) { return; } else if (sstate = SS_IDLE) {} return b_go; } void p_serial_mgr_start() { b_go = false; HAL_UART_Receive_IT(_serial_huart_inst, &rxc, 1); }