|
|
@ -29,8 +29,8 @@ static UART_HandleTypeDef *_serial_huart_inst = NULL;
|
|
|
|
static serial_state_t sstate = SS_IDLE;
|
|
|
|
static serial_state_t sstate = SS_IDLE;
|
|
|
|
static uint8_t rxc = '\0';
|
|
|
|
static uint8_t rxc = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
static serial_pkt_t pkt_bank[10];
|
|
|
|
volatile serial_pkt_t pkt_bank[10];
|
|
|
|
static p_cb_serial_pkt_t serial_pkt_cb;
|
|
|
|
volatile p_cb_serial_pkt_t serial_pkt_cb;
|
|
|
|
|
|
|
|
|
|
|
|
static volatile uint8_t start_index_tracker = 0;
|
|
|
|
static volatile uint8_t start_index_tracker = 0;
|
|
|
|
static volatile uint8_t frame_index_tracker = 0;
|
|
|
|
static volatile uint8_t frame_index_tracker = 0;
|
|
|
@ -56,24 +56,27 @@ void UART1_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
|
|
case 0: // source addr
|
|
|
|
case 0: // source addr
|
|
|
|
{
|
|
|
|
{
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].src_addr = rxc;
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].src_addr = rxc;
|
|
|
|
|
|
|
|
start_index_tracker++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].dest_addr = rxc;
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].dest_addr = rxc;
|
|
|
|
|
|
|
|
start_index_tracker++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].len = rxc;
|
|
|
|
start_index_tracker = 0;
|
|
|
|
frame_index_tracker = 0;
|
|
|
|
frame_index_tracker = 0;
|
|
|
|
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].len = rxc;
|
|
|
|
sstate = SS_FRAME;
|
|
|
|
sstate = SS_FRAME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// shouldnt get here
|
|
|
|
// shouldnt get here
|
|
|
|
for (;;) {}
|
|
|
|
asm volatile("nop");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -82,8 +85,11 @@ void UART1_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (rxc == 0x7E)
|
|
|
|
if (rxc == 0x7E)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
asm volatile("nop");
|
|
|
|
// error occured. bail
|
|
|
|
// error occured. bail
|
|
|
|
#pragma message(Reminder "add a safe escape routine for this")
|
|
|
|
#pragma message(Reminder "add a safe escape routine for this")
|
|
|
|
|
|
|
|
sstate = SS_IDLE;
|
|
|
|
|
|
|
|
memset(&serial_pkt_cb.buffer[serial_pkt_cb.head], 0, sizeof(serial_pkt_t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rxc == 0x7D)
|
|
|
|
else if (rxc == 0x7D)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -92,7 +98,7 @@ void UART1_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].frame_data[frame_index_tracker++] = rxc;
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].frame_data[frame_index_tracker++] = rxc;
|
|
|
|
if (frame_index_tracker + 1 == serial_pkt_cb.buffer[serial_pkt_cb.head].len)
|
|
|
|
if (frame_index_tracker >= serial_pkt_cb.buffer[serial_pkt_cb.head].len)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sstate = SS_CHECKSUM;
|
|
|
|
sstate = SS_CHECKSUM;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -102,17 +108,23 @@ void UART1_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
|
|
case SS_ESC:
|
|
|
|
case SS_ESC:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].frame_data[frame_index_tracker++] = rxc ^ 0x20;
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].frame_data[frame_index_tracker++] = rxc ^ 0x20;
|
|
|
|
if (frame_index_tracker + 1 == serial_pkt_cb.buffer[serial_pkt_cb.head].len)
|
|
|
|
if (frame_index_tracker >= serial_pkt_cb.buffer[serial_pkt_cb.head].len)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sstate = SS_CHECKSUM;
|
|
|
|
sstate = SS_CHECKSUM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
sstate = SS_FRAME;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case SS_CHECKSUM:
|
|
|
|
case SS_CHECKSUM:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
frame_index_tracker = 0;
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].checksum = rxc;
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].checksum = rxc;
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].b_ready = true;
|
|
|
|
serial_pkt_cb.buffer[serial_pkt_cb.head].b_ready = true;
|
|
|
|
serial_pkt_cb.head = (serial_pkt_cb.head + 1) % serial_pkt_cb.max_len;
|
|
|
|
serial_pkt_cb.head = (serial_pkt_cb.head + 1) % serial_pkt_cb.max_len;
|
|
|
|
|
|
|
|
sstate = SS_IDLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
@ -134,6 +146,7 @@ void p_serial_mgr_init(UART_HandleTypeDef *huart)
|
|
|
|
_serial_huart_inst = huart;
|
|
|
|
_serial_huart_inst = huart;
|
|
|
|
_serial_huart_inst->RxCpltCallback = UART1_RxCpltCallback;
|
|
|
|
_serial_huart_inst->RxCpltCallback = UART1_RxCpltCallback;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
memset(pkt_bank, 0, sizeof(serial_pkt_t) * 10);
|
|
|
|
p_cb_serial_pkt_init(&serial_pkt_cb, pkt_bank, 10);
|
|
|
|
p_cb_serial_pkt_init(&serial_pkt_cb, pkt_bank, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -144,7 +157,7 @@ serial_pkt_t *p_serial_mgr_service(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (serial_pkt_cb.buffer[ind].b_ready)
|
|
|
|
if (serial_pkt_cb.buffer[ind].b_ready)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return &serial_pkt_cb.buffer;
|
|
|
|
return &serial_pkt_cb.buffer[ind];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
return NULL;
|
|
|
|