Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast data transmit error handled #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/*
MDK_ARM/*
19 changes: 17 additions & 2 deletions Inc/slcan.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
#ifndef _SLCAN_H
#define _SLCAN_H

#include <string.h>

int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame);
int8_t slcan_parse_str(uint8_t *buf, uint8_t len);
int8_t slcan_parse_str(uint8_t newbyte);

typedef enum{
GET_MSG_TYPE = 0,
GET_FRAME_STD_ID,
GET_FRAME_EXT_ID,
GET_FRAME_DATA,
GET_OPEN_COMMAND,
GET_CLOSE_COMMAND,
GET_BITRATE_COMMAND,
GET_MODE_COMMAND,
GET_FILTER_COMMAND,
GET_MASK_COMMAND
} slcan_usb2can_fsm;

/* maximum rx buffer len: extended CAN frame with timestamp */
#define SLCAN_MTU 30// (sizeof("T1111222281122334455667788EA5F\r")+1)
#define SLCAN_MTU 128// (sizeof("T1111222281122334455667788EA5F\r")+1)

#define SLCAN_STD_ID_LEN 3
#define SLCAN_EXT_ID_LEN 8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -876,20 +876,19 @@ uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev)
if(pdev->dev_speed == USBD_SPEED_HIGH )
{
/* Prepare Out endpoint to receive next packet */
USBD_LL_PrepareReceive(pdev,
return USBD_LL_PrepareReceive(pdev,
CDC_OUT_EP,
hcdc->RxBuffer,
CDC_DATA_HS_OUT_PACKET_SIZE);
}
else
{
/* Prepare Out endpoint to receive next packet */
USBD_LL_PrepareReceive(pdev,
return USBD_LL_PrepareReceive(pdev,
CDC_OUT_EP,
hcdc->RxBuffer,
CDC_DATA_FS_OUT_PACKET_SIZE);
}
return USBD_OK;
}
else
{
Expand Down
53 changes: 32 additions & 21 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ static void led_init(void);
/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

uint8_t circle_buffer[SLCAN_MTU];
uint8_t rx_point = 0;
uint8_t tx_point = 0;
uint8_t need_update;
/* USER CODE END 0 */


volatile int i=0;
int main(void)
{

/* USER CODE BEGIN 1 */

CanRxMsgTypeDef rx_msg;
uint8_t msg_buf[SLCAN_MTU];
int8_t status;
/* USER CODE END 1 */

/* MCU Configuration----------------------------------------------------------*/
Expand All @@ -99,30 +102,38 @@ int main(void)

// blink red LED for test
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
HAL_Delay(100);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
HAL_Delay(100);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);


// loop forever
CanRxMsgTypeDef rx_msg;
uint32_t status;
uint8_t msg_buf[SLCAN_MTU];


for (;;) {
while (!is_can_msg_pending(CAN_FIFO0))
led_process();
status = can_rx(&rx_msg, 3);
if (status == HAL_OK) {
status = slcan_parse_frame((uint8_t *)&msg_buf, &rx_msg);
CDC_Transmit_FS(msg_buf, status);
}
led_process();
//Read circle buffer for commands and messages
while (!is_can_msg_pending(CAN_FIFO0)){
if (tx_point != rx_point) {
slcan_parse_str(circle_buffer[tx_point]);
tx_point = (tx_point + 1) % SLCAN_MTU;
}
led_process();
}
//Get CAN-messages and parse them to USB buffer
if (can_rx(&rx_msg, 0) == HAL_OK){
status = slcan_parse_frame((uint8_t *)&msg_buf, &rx_msg);
CDC_Transmit_FS(msg_buf, status);
}
led_process();

//If fault has occured, try to fix this on the fly
if (need_update){
need_update = 0;
if (USBD_CDC_ReceivePacket(&hUsbDeviceFS) != USBD_OK) need_update = 1;
}
}

/* USER CODE END 3 */
}

Expand Down
Loading