tied cmd parsing properly to usb rx

retry
Ondřej Hruška 7 years ago
parent 896df82e27
commit 8efe91c8ed
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 12
      Src/gex_gateway.c
  2. 6
      Src/main.c
  3. 8
      Src/usbd_cdc_if.c

@ -32,7 +32,7 @@ enum GW_CMD {
CMD_MSG4SLAVE = 'm', // 109 CMD_MSG4SLAVE = 'm', // 109
}; };
void respond_gw_id(); // TODO impl void respond_gw_id() {} // TODO impl
void start_slave_cmd(uint8_t slave_addr, uint16_t frame_len, uint8_t cksum) void start_slave_cmd(uint8_t slave_addr, uint16_t frame_len, uint8_t cksum)
{ {
@ -47,6 +47,7 @@ void gw_process(void)
{ {
static uint8_t buffer[MQ_SLOT_LEN]; static uint8_t buffer[MQ_SLOT_LEN];
while (mq_read(&usb_rxq, buffer)) { // greedy - handle as many as possible while (mq_read(&usb_rxq, buffer)) { // greedy - handle as many as possible
dbg("Handling frame.");
if (urx_state == URXS_IDLE) { if (urx_state == URXS_IDLE) {
PayloadParser pp = pp_start(buffer, MQ_SLOT_LEN, NULL); PayloadParser pp = pp_start(buffer, MQ_SLOT_LEN, NULL);
@ -55,7 +56,7 @@ void gw_process(void)
// magic twice, one inverted - denotes a gateway command // magic twice, one inverted - denotes a gateway command
uint16_t magic1 = pp_u8(&pp); uint16_t magic1 = pp_u8(&pp);
uint16_t magic2 = pp_u8(&pp); uint16_t magic2 = pp_u8(&pp);
if (magic1 == MAGIC_GW_COMMAND && magic2 == ~MAGIC_GW_COMMAND) { if (magic1 == MAGIC_GW_COMMAND && magic2 == (0xFFU & (~MAGIC_GW_COMMAND))) {
// third byte is the command code // third byte is the command code
switch (pp_u8(&pp)) { switch (pp_u8(&pp)) {
case CMD_GET_ID: case CMD_GET_ID:
@ -67,7 +68,7 @@ void gw_process(void)
uint16_t frame_len = pp_u16(&pp); uint16_t frame_len = pp_u16(&pp);
uint8_t cksum = pp_u8(&pp); uint8_t cksum = pp_u8(&pp);
if (frame_len > MAX_FRAME_LEN) { if (frame_len == 0 || frame_len > MAX_FRAME_LEN) {
dbg("Frame too big!"); dbg("Frame too big!");
break; break;
} }
@ -76,10 +77,13 @@ void gw_process(void)
dbg("Collecting frame for slave %02x: %d bytes", (int)slave_addr, (int)frame_len); dbg("Collecting frame for slave %02x: %d bytes", (int)slave_addr, (int)frame_len);
urx_state = URXS_MSG4SLAVE; urx_state = URXS_MSG4SLAVE;
break; break;
default:
dbg("Bad cmd");
} }
} else { } else {
// Bad frame?? // Bad frame??
dbg("Bad USB frame, starts %x", buffer[0]); dbg("Bad USB frame, starts %x,%x", buffer[0],buffer[1]);
} }
} }
else if (urx_state == URXS_MSG4SLAVE) { else if (urx_state == URXS_MSG4SLAVE) {

@ -48,6 +48,7 @@
*/ */
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include <gex_gateway.h> #include <gex_gateway.h>
#include <msg_queue.h>
#include "main.h" #include "main.h"
#include "stm32f1xx_hal.h" #include "stm32f1xx_hal.h"
#include "dma.h" #include "dma.h"
@ -122,12 +123,15 @@ int main(void)
/* USER CODE END 2 */ /* USER CODE END 2 */
dbg("Main loop starts."); dbg("Main loop starts.");
mq_init(&usb_rxq);
mq_init(&usb_txq);
/* Infinite loop */ /* Infinite loop */
/* USER CODE BEGIN WHILE */ /* USER CODE BEGIN WHILE */
int cnt = 0; int cnt = 0;
while (1) while (1)
{ {
if (cnt++ > 10000) { if (cnt++ > 100000) {
GPIOC->ODR ^= (1 << 13); GPIOC->ODR ^= (1 << 13);
cnt = 0; cnt = 0;
} }

@ -49,6 +49,7 @@
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include <debug.h> #include <debug.h>
#include <msg_queue.h>
#include "usbd_cdc_if.h" #include "usbd_cdc_if.h"
/* USER CODE BEGIN INCLUDE */ /* USER CODE BEGIN INCLUDE */
@ -295,7 +296,12 @@ static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]); USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
USBD_CDC_ReceivePacket(&hUsbDeviceFS); USBD_CDC_ReceivePacket(&hUsbDeviceFS);
// // queue the frame
dbg("USB rx frame");
bool suc = mq_post(&usb_rxq, Buf, *Len);
if (!suc) {
dbg("USB rxq overrun!");
}
return (USBD_OK); return (USBD_OK);
/* USER CODE END 6 */ /* USER CODE END 6 */

Loading…
Cancel
Save