Skip to content

Commit cb2fa58

Browse files
author
Ethan Zonca
committed
Add data buffering: backport of major buffering features from g4: CAN TX buffering, CDC RX buffering. No USB TX buffering yet.
1 parent 1b8f8b8 commit cb2fa58

File tree

20 files changed

+435
-772
lines changed

20 files changed

+435
-772
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
build/*
2+
/Default/

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
# SOURCES: list of sources in the user application
13-
SOURCES = main.c system.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c interrupts.c system_stm32f0xx.c can.c slcan.c led.c
13+
SOURCES = main.c system.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c interrupts.c system_stm32f0xx.c can.c slcan.c led.c error.c
1414

1515
# Get git version and dirty flag
1616
GIT_VERSION := $(shell git describe --abbrev=7 --dirty --always --tags)

Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extern "C" {
5454
#endif /* CDC_FS_BINTERVAL */
5555

5656
/* CDC Endpoints parameters: you can fine tune these values depending on the needed baudrates and performance. */
57-
#define CDC_DATA_HS_MAX_PACKET_SIZE 512U /* Endpoint IN & OUT Packet size */
57+
#define CDC_DATA_HS_MAX_PACKET_SIZE 64U // UNUSED--match FS size since CDC lib references this
5858
#define CDC_DATA_FS_MAX_PACKET_SIZE 64U /* Endpoint IN & OUT Packet size */
5959
#define CDC_CMD_PACKET_SIZE 8U /* Control Endpoint Packet size */
6060

Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_def.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extern "C" {
5858
#endif /* USBD_LPM_ENABLED */
5959

6060
#ifndef USBD_SELF_POWERED
61-
#define USBD_SELF_POWERED 1U
61+
#define USBD_SELF_POWERED 0U
6262
#endif /*USBD_SELF_POWERED */
6363

6464
#ifndef USBD_SUPPORT_USER_STRING_DESC

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CANable Firmware
22

3-
This repository contains sources for the slcan CANable firmware, based off of the CANtact firwmare. This firmware should still compile and run on the CANtact.
3+
This repository contains sources for the slcan CANable firmware, based off of the CANtact firwmare. This firmware may still compile and run on the CANtact.
44

55
## Supported Commands
66

cantact.ioc cube/canable.ioc

+31-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#MicroXplorer Configuration settings - do not modify
2-
CAN.CalculateTimeBit=3000
2+
CAN.CalculateBaudRate=333333
3+
CAN.CalculateTimeBit=3000.00
34
CAN.CalculateTimeQuantum=1000.0
4-
CAN.IPParameters=CalculateTimeBit,CalculateTimeQuantum
5+
CAN.IPParameters=CalculateTimeBit,CalculateTimeQuantum,CalculateBaudRate
56
File.Version=6
67
KeepUserPlacement=false
78
Mcu.Family=STM32F0
@@ -23,60 +24,59 @@ Mcu.Pin5=PB9
2324
Mcu.Pin6=VP_SYS_VS_Systick
2425
Mcu.Pin7=VP_USB_DEVICE_VS_USB_DEVICE_CDC_FS
2526
Mcu.PinsNb=8
27+
Mcu.ThirdPartyNb=0
2628
Mcu.UserConstants=
2729
Mcu.UserName=STM32F042C6Tx
28-
MxCube.Version=4.16.0
29-
MxDb.Version=DB.4.0.160
30-
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:false
31-
NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:false
32-
NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:false
33-
NVIC.SVC_IRQn=true\:0\:0\:false\:false\:false
34-
NVIC.SysTick_IRQn=true\:0\:0\:false\:false\:true
35-
NVIC.USB_IRQn=true\:0\:0\:false\:false\:true
30+
MxCube.Version=6.1.1
31+
MxDb.Version=DB.6.0.10
32+
NVIC.ForceEnableDMAVector=true
33+
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:false\:true\:false
34+
NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:false\:true\:false
35+
NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:false\:true\:false
36+
NVIC.SVC_IRQn=true\:0\:0\:false\:false\:false\:true\:false
37+
NVIC.SysTick_IRQn=true\:0\:0\:false\:false\:true\:true\:true
38+
NVIC.USB_IRQn=true\:0\:0\:false\:false\:true\:true\:true
3639
PA11.Mode=Device
3740
PA11.Signal=USB_DM
3841
PA12.Mode=Device
3942
PA12.Signal=USB_DP
40-
PB8.Mode=Master
43+
PB8.Mode=CAN_Activate
4144
PB8.Signal=CAN_RX
42-
PB9.Mode=Master
45+
PB9.Mode=CAN_Activate
4346
PB9.Signal=CAN_TX
44-
PCC.Checker=false
45-
PCC.Line=STM32F0x2
46-
PCC.MCU=STM32F042C(4-6)Tx
47-
PCC.MXVersion=4.16.0
48-
PCC.PartNumber=STM32F042C6Tx
49-
PCC.Seq0=0
50-
PCC.Series=STM32F0
51-
PCC.Temperature=25
52-
PCC.Vdd=3.6
5347
PF0-OSC_IN.Mode=HSE-External-Oscillator
5448
PF0-OSC_IN.Signal=RCC_OSC_IN
5549
PF1-OSC_OUT.Mode=HSE-External-Oscillator
5650
PF1-OSC_OUT.Signal=RCC_OSC_OUT
51+
PinOutPanel.RotationAngle=0
5752
ProjectManager.AskForMigrate=true
5853
ProjectManager.BackupPrevious=false
5954
ProjectManager.CompilerOptimize=2
6055
ProjectManager.ComputerToolchain=false
6156
ProjectManager.CoupleFile=true
57+
ProjectManager.CustomerFirmwarePackage=
58+
ProjectManager.DefaultFWLocation=true
6259
ProjectManager.DeletePrevious=true
6360
ProjectManager.DeviceId=STM32F042C6Tx
64-
ProjectManager.FirmwarePackage=STM32Cube FW_F0 V1.6.0
61+
ProjectManager.FirmwarePackage=STM32Cube FW_F0 V1.11.2
6562
ProjectManager.FreePins=false
6663
ProjectManager.HalAssertFull=false
67-
ProjectManager.HeapSize=0x200
64+
ProjectManager.HeapSize=0x400
6865
ProjectManager.KeepUserCode=true
6966
ProjectManager.LastFirmware=true
7067
ProjectManager.LibraryCopy=0
68+
ProjectManager.MainLocation=Src
69+
ProjectManager.NoMain=false
7170
ProjectManager.PreviousToolchain=
7271
ProjectManager.ProjectBuild=false
73-
ProjectManager.ProjectFileName=cantact.ioc
74-
ProjectManager.ProjectName=cantact
72+
ProjectManager.ProjectFileName=canable.ioc
73+
ProjectManager.ProjectName=canable
74+
ProjectManager.RegisterCallBack=
7575
ProjectManager.StackSize=0x400
7676
ProjectManager.TargetToolchain=TrueSTUDIO
77-
ProjectManager.ToolChainLocation=/home/ethanzonca/Projects/cantact-fw-z
77+
ProjectManager.ToolChainLocation=
7878
ProjectManager.UnderRoot=false
79-
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false,2-MX_CAN_Init-CAN-false,3-MX_USB_DEVICE_Init-USB_DEVICE-false
79+
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_CAN_Init-CAN-false-HAL-true,4-MX_USB_DEVICE_Init-USB_DEVICE-false-HAL-false
8080
RCC.AHBFreq_Value=16000000
8181
RCC.APB1Freq_Value=16000000
8282
RCC.APB1TimFreq_Value=16000000
@@ -100,9 +100,12 @@ RCC.USART1Freq_Value=16000000
100100
RCC.USBClockSelection=RCC_USBCLKSOURCE_PLL
101101
RCC.USBFreq_Value=48000000
102102
RCC.VCOOutput2Freq_Value=16000000
103+
USB_DEVICE.APP_RX_DATA_SIZE=64
104+
USB_DEVICE.APP_TX_DATA_SIZE=64
103105
USB_DEVICE.CLASS_NAME_FS=CDC
104-
USB_DEVICE.IPParameters=VirtualMode,VirtualModeFS,USBD_HandleTypeDef,CLASS_NAME_FS
106+
USB_DEVICE.IPParameters=VirtualMode,VirtualModeFS,USBD_HandleTypeDef,CLASS_NAME_FS,USBD_MAX_STR_DESC_SIZ,APP_RX_DATA_SIZE,APP_TX_DATA_SIZE
105107
USB_DEVICE.USBD_HandleTypeDef=hUsbDeviceFS
108+
USB_DEVICE.USBD_MAX_STR_DESC_SIZ=67
106109
USB_DEVICE.VirtualMode=Cdc
107110
USB_DEVICE.VirtualModeFS=Cdc_FS
108111
VP_SYS_VS_Systick.Mode=SysTick

inc/can.h

+20-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,27 @@ enum can_bitrate {
1515
CAN_BITRATE_INVALID,
1616
};
1717

18-
enum can_bus_state {
19-
OFF_BUS,
20-
ON_BUS
21-
};
18+
typedef enum can_bus_state {
19+
OFF_BUS = 0,
20+
ON_BUS = 1,
21+
} can_bus_state_t;
22+
23+
24+
// CAN transmit buffering
25+
#define TXQUEUE_LEN 28 // Number of buffers allocated
26+
#define TXQUEUE_DATALEN 8 // CAN DLC length of data buffers
27+
28+
typedef struct cantxbuf_
29+
{
30+
uint8_t data[TXQUEUE_LEN][TXQUEUE_DATALEN]; // Data buffer
31+
CAN_TxHeaderTypeDef header[TXQUEUE_LEN]; // Header buffer
32+
uint8_t head; // Head pointer
33+
uint8_t tail; // Tail pointer
34+
uint8_t full; // TODO: Set this when we are full, clear when the tail moves one.
35+
} can_txbuf_t;
36+
2237

38+
// Prototypes
2339
void can_init(void);
2440
void can_enable(void);
2541
void can_disable(void);

inc/error.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef INC_ERROR_H_
2+
#define INC_ERROR_H_
3+
4+
5+
typedef enum _error_t
6+
{
7+
ERR_PERIPHINIT = 0,
8+
ERR_USBTX_BUSY,
9+
ERR_CAN_TXFAIL,
10+
ERR_CANRXFIFO_OVERFLOW,
11+
ERR_FULLBUF_CANTX,
12+
ERR_FULLBUF_USBRX,
13+
14+
ERR_MAX
15+
} error_t;
16+
17+
18+
// Prototypes
19+
void error_assert(error_t err);
20+
21+
22+
#endif /* INC_ERROR_H_ */

inc/usbd_cdc_if.h

+16-141
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,34 @@
1-
/**
2-
******************************************************************************
3-
* @file : usbd_cdc_if.h
4-
* @version : v2.0_Cube
5-
* @brief : Header for usbd_cdc_if.c file.
6-
******************************************************************************
7-
* This notice applies to any and all portions of this file
8-
* that are not between comment pairs USER CODE BEGIN and
9-
* USER CODE END. Other portions of this file, whether
10-
* inserted by the user or by software development tools
11-
* are owned by their respective copyright owners.
12-
*
13-
* Copyright (c) 2018 STMicroelectronics International N.V.
14-
* All rights reserved.
15-
*
16-
* Redistribution and use in source and binary forms, with or without
17-
* modification, are permitted, provided that the following conditions are met:
18-
*
19-
* 1. Redistribution of source code must retain the above copyright notice,
20-
* this list of conditions and the following disclaimer.
21-
* 2. Redistributions in binary form must reproduce the above copyright notice,
22-
* this list of conditions and the following disclaimer in the documentation
23-
* and/or other materials provided with the distribution.
24-
* 3. Neither the name of STMicroelectronics nor the names of other
25-
* contributors to this software may be used to endorse or promote products
26-
* derived from this software without specific written permission.
27-
* 4. This software, including modifications and/or derivative works of this
28-
* software, must execute solely and exclusively on microcontroller or
29-
* microprocessor devices manufactured by or for STMicroelectronics.
30-
* 5. Redistribution and use of this software other than as permitted under
31-
* this license is void and will automatically terminate your rights under
32-
* this license.
33-
*
34-
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
35-
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
36-
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
37-
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
38-
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
39-
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40-
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41-
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
42-
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43-
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44-
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
45-
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46-
*
47-
******************************************************************************
48-
*/
49-
50-
/* Define to prevent recursive inclusion -------------------------------------*/
511
#ifndef __USBD_CDC_IF_H__
522
#define __USBD_CDC_IF_H__
533

54-
#ifdef __cplusplus
55-
extern "C" {
56-
#endif
57-
58-
/* Includes ------------------------------------------------------------------*/
594
#include "usbd_cdc.h"
605

61-
/* USER CODE BEGIN INCLUDE */
62-
63-
/* USER CODE END INCLUDE */
64-
65-
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
66-
* @brief For Usb device.
67-
* @{
68-
*/
69-
70-
/** @defgroup USBD_CDC_IF USBD_CDC_IF
71-
* @brief Usb VCP device module
72-
* @{
73-
*/
74-
75-
/** @defgroup USBD_CDC_IF_Exported_Defines USBD_CDC_IF_Exported_Defines
76-
* @brief Defines.
77-
* @{
78-
*/
79-
/* USER CODE BEGIN EXPORTED_DEFINES */
80-
81-
/* USER CODE END EXPORTED_DEFINES */
82-
83-
/**
84-
* @}
85-
*/
86-
87-
/** @defgroup USBD_CDC_IF_Exported_Types USBD_CDC_IF_Exported_Types
88-
* @brief Types.
89-
* @{
90-
*/
91-
92-
/* USER CODE BEGIN EXPORTED_TYPES */
93-
94-
/* USER CODE END EXPORTED_TYPES */
6+
// Buffer settings
7+
#define TX_BUF_SIZE 32 // Linear TX buf size
8+
#define NUM_RX_BUFS 6 // Number of RX buffers in FIFO
9+
#define RX_BUF_SIZE CDC_DATA_FS_MAX_PACKET_SIZE // Size of RX buffer item
9510

96-
/**
97-
* @}
98-
*/
11+
// Receive buffering: circular buffer FIFO
12+
typedef struct _usbrx_buf_
13+
{
14+
// Receive buffering: circular buffer FIFO
15+
uint8_t buf[NUM_RX_BUFS][RX_BUF_SIZE];
16+
uint32_t msglen[NUM_RX_BUFS];
17+
uint8_t head;
18+
uint8_t tail;
9919

100-
/** @defgroup USBD_CDC_IF_Exported_Macros USBD_CDC_IF_Exported_Macros
101-
* @brief Aliases.
102-
* @{
103-
*/
20+
} usbrx_buf_t;
10421

105-
/* USER CODE BEGIN EXPORTED_MACRO */
10622

107-
/* USER CODE END EXPORTED_MACRO */
108-
109-
/**
110-
* @}
111-
*/
112-
113-
/** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
114-
* @brief Public variables.
115-
* @{
116-
*/
117-
118-
/** CDC Interface callback. */
23+
// CDC Interface callback.
11924
extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS;
12025

121-
/* USER CODE BEGIN EXPORTED_VARIABLES */
122-
123-
/* USER CODE END EXPORTED_VARIABLES */
124-
125-
/**
126-
* @}
127-
*/
128-
129-
/** @defgroup USBD_CDC_IF_Exported_FunctionsPrototype USBD_CDC_IF_Exported_FunctionsPrototype
130-
* @brief Public functions declaration.
131-
* @{
132-
*/
13326

27+
// Prototypes
13428
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len);
135-
void usb_process(void);
136-
137-
/* USER CODE BEGIN EXPORTED_FUNCTIONS */
138-
139-
/* USER CODE END EXPORTED_FUNCTIONS */
140-
141-
/**
142-
* @}
143-
*/
144-
145-
/**
146-
* @}
147-
*/
29+
void cdc_process(void);
14830

149-
/**
150-
* @}
151-
*/
15231

153-
#ifdef __cplusplus
154-
}
155-
#endif
15632

15733
#endif /* __USBD_CDC_IF_H__ */
15834

159-
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

inc/usbd_conf.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
/*---------- -----------*/
102102
#define USBD_DEBUG_LEVEL 0
103103
/*---------- -----------*/
104-
#define USBD_SELF_POWERED 1
104+
#define USBD_SELF_POWERED 0
105105
/*---------- -----------*/
106106
#define MAX_STATIC_ALLOC_SIZE 512
107107

0 commit comments

Comments
 (0)