usb communication works (incl. echo)

This commit is contained in:
Hubert Denkmair 2016-04-09 22:31:38 +02:00
parent b52e20ecb6
commit f5c4316e07
11 changed files with 243 additions and 203 deletions

View File

@ -54,7 +54,6 @@
<listOptionValue builtIn="false" value="&quot;../system/include/stm32f0xx&quot;"/>
<listOptionValue builtIn="false" value="&quot;../system/include/cmsis/device&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Core/Inc&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc&quot;"/>
</option>
<option id="ilg.gnuarmeclipse.managedbuild.cross.option.assembler.defs.498111027" name="Defined symbols (-D)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.assembler.defs" valueType="definedSymbols">
<listOptionValue builtIn="false" value="DEBUG"/>
@ -71,7 +70,6 @@
<listOptionValue builtIn="false" value="&quot;../system/include/stm32f0xx&quot;"/>
<listOptionValue builtIn="false" value="&quot;../system/include/cmsis/device&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Core/Inc&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc&quot;"/>
</option>
<option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.defs.1001664418" name="Defined symbols (-D)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.defs" useByScannerDiscovery="false" valueType="definedSymbols">
<listOptionValue builtIn="false" value="DEBUG"/>
@ -88,7 +86,6 @@
<listOptionValue builtIn="false" value="&quot;../system/include/stm32f0xx&quot;"/>
<listOptionValue builtIn="false" value="&quot;../system/include/cmsis/device&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Core/Inc&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc&quot;"/>
</option>
<option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.noexceptions.198118015" name="Do not use exceptions (-fno-exceptions)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.noexceptions" useByScannerDiscovery="true" value="true" valueType="boolean"/>
<option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.nortti.1973209190" name="Do not use RTTI (-fno-rtti)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.nortti" useByScannerDiscovery="true" value="true" valueType="boolean"/>
@ -206,7 +203,6 @@
<listOptionValue builtIn="false" value="&quot;../system/include/stm32f0xx&quot;"/>
<listOptionValue builtIn="false" value="&quot;../system/include/cmsis/device&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Core/Inc&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc&quot;"/>
</option>
<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.assembler.input.1226943399" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.assembler.input"/>
</tool>
@ -218,7 +214,6 @@
<listOptionValue builtIn="false" value="&quot;../system/include/stm32f0xx&quot;"/>
<listOptionValue builtIn="false" value="&quot;../system/include/cmsis/device&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Core/Inc&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc&quot;"/>
</option>
<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler.input.352342197" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler.input"/>
</tool>
@ -230,7 +225,6 @@
<listOptionValue builtIn="false" value="&quot;../system/include/stm32f0xx&quot;"/>
<listOptionValue builtIn="false" value="&quot;../system/include/cmsis/device&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Core/Inc&quot;"/>
<listOptionValue builtIn="false" value="&quot;../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc&quot;"/>
</option>
<option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.noexceptions.306235030" name="Do not use exceptions (-fno-exceptions)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.noexceptions" useByScannerDiscovery="true" value="true" valueType="boolean"/>
<option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.nortti.1876505535" name="Do not use RTTI (-fno-rtti)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.nortti" useByScannerDiscovery="true" value="true" valueType="boolean"/>

View File

@ -3,6 +3,7 @@
#include <stdbool.h>
#include "stm32f0xx_hal.h"
void can_init(CAN_HandleTypeDef *hcan, CAN_TypeDef *instance);
void can_set_bittiming(CAN_HandleTypeDef *hcan, uint16_t brp, uint8_t phase_seg1, uint8_t phase_seg2, uint8_t sjw);
void can_enable(CAN_HandleTypeDef *hcan, bool loop_back, bool listen_only, bool one_shot, bool triple_sample);
void can_enable(CAN_HandleTypeDef *hcan, bool loop_back, bool listen_only, bool one_shot);
void can_disable(CAN_HandleTypeDef *hcan);

17
include/queue.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <stdbool.h>
#include <util.h>
typedef struct {
unsigned max_elements;
unsigned first;
unsigned size;
void **buf;
} queue_t;
queue_t *queue_create(unsigned max_elements);
void queue_free(queue_t *q);
unsigned queue_size(queue_t *q);
bool queue_is_empty(queue_t *q);
bool queue_push_back(queue_t *q, void *el);
void *queue_pop_front(queue_t *q);

View File

@ -1,11 +1,15 @@
#pragma once
#include "usbd_def.h"
#include <stdbool.h>
#include <usbd_def.h>
#include <queue.h>
extern USBD_ClassTypeDef USBD_GS_CAN;
uint8_t USBD_GS_CAN_Init(USBD_HandleTypeDef *pdev, queue_t *q_free_frames, queue_t *q_from_host);
void USBD_GS_CAN_SetChannel(USBD_HandleTypeDef *pdev, uint8_t channel, CAN_HandleTypeDef* handle);
bool USBD_GS_CAN_TxReady(USBD_HandleTypeDef *pdev);
uint8_t USBD_GS_CAN_Transmit(USBD_HandleTypeDef *pdev, uint8_t *buf, uint16_t len);
void USBD_GS_CAN_SendFrameToHost(
USBD_HandleTypeDef *pdev,

4
include/util.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
int disable_irq(void);
void enable_irq(int primask);

View File

@ -1,10 +1,27 @@
#include "can.h"
void can_init(CAN_HandleTypeDef *hcan, CAN_TypeDef *instance)
{
/* hcan->Instance = instance;
hcan->Init.Prescaler = 6-1;
hcan->Init.Mode = CAN_MODE_SILENT;
hcan->Init.SJW = CAN_SJW_1TQ;
hcan->Init.BS1 = CAN_BS1_13TQ;
hcan->Init.BS2 = CAN_BS2_2TQ;
hcan->Init.TTCM = DISABLE;
hcan->Init.ABOM = DISABLE;
hcan->Init.AWUM = DISABLE;
hcan->Init.NART = DISABLE;
hcan->Init.RFLM = DISABLE;
hcan->Init.TXFP = DISABLE;
HAL_CAN_Init(hcan); */
}
void can_set_bittiming(CAN_HandleTypeDef *hcan, uint16_t brp, uint8_t phase_seg1, uint8_t phase_seg2, uint8_t sjw)
{
HAL_CAN_DeInit(hcan);
/* HAL_CAN_DeInit(hcan);
hcan->Init.Prescaler = brp;
hcan->Init.Prescaler = brp & 0x3FF;
if ((phase_seg1>0) && (phase_seg1<17)) {
hcan->Init.BS1 = (phase_seg1-1)<<16;
@ -18,12 +35,12 @@ void can_set_bittiming(CAN_HandleTypeDef *hcan, uint16_t brp, uint8_t phase_seg1
hcan->Init.SJW = (sjw-1)<<24;
}
HAL_CAN_Init(hcan);
HAL_CAN_Init(hcan); */
}
void can_enable(CAN_HandleTypeDef *hcan, bool loop_back, bool listen_only, bool one_shot, bool triple_sample)
void can_enable(CAN_HandleTypeDef *hcan, bool loop_back, bool listen_only, bool one_shot)
{
hcan->Init.Mode = 0;
/* hcan->Init.Mode = 0;
if (loop_back) {
hcan->Init.Mode |= CAN_MODE_LOOPBACK;
}
@ -32,13 +49,10 @@ void can_enable(CAN_HandleTypeDef *hcan, bool loop_back, bool listen_only, bool
}
hcan->Init.NART = one_shot ? ENABLE : DISABLE;
// tripple sample not supported on bxCAN
HAL_CAN_Init(hcan);
HAL_CAN_Init(hcan); */
}
void can_disable(CAN_HandleTypeDef *hcan)
{
HAL_CAN_DeInit(hcan);
//HAL_CAN_DeInit(hcan);
}

View File

@ -1,100 +1,77 @@
/**
******************************************************************************
* File Name : main.c
* Description : Main program body
******************************************************************************
*
* COPYRIGHT(c) 2016 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
/* USER CODE BEGIN Includes */
#include "usbd_def.h"
#include "usbd_desc.h"
#include "usbd_core.h"
#include "usbd_gs_can.h"
#include <queue.h>
#include <gs_usb.h>
#include <can.h>
/* USER CODE END Includes */
#define CAN_QUEUE_SIZE 32
/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN_Init(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
USBD_HandleTypeDef hUsbDeviceFS;
CAN_HandleTypeDef hCAN;
USBD_HandleTypeDef hUSB;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_CAN_Init();
can_init(&hCAN, CAN);
USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
USBD_RegisterClass(&hUsbDeviceFS, &USBD_GS_CAN);
USBD_GS_CAN_SetChannel(&hUsbDeviceFS, 0, &hcan);
USBD_Start(&hUsbDeviceFS);
queue_t *q_free_frames = queue_create(CAN_QUEUE_SIZE);
queue_t *q_from_host = queue_create(CAN_QUEUE_SIZE);
struct gs_host_frame *msgbuf = calloc(CAN_QUEUE_SIZE, sizeof(struct gs_host_frame));
for (int i=0; i<CAN_QUEUE_SIZE; i++) {
queue_push_back(q_free_frames, &msgbuf[i]);
}
USBD_Init(&hUSB, &FS_Desc, DEVICE_FS);
USBD_RegisterClass(&hUSB, &USBD_GS_CAN);
USBD_GS_CAN_Init(&hUSB, q_free_frames, q_from_host);
USBD_GS_CAN_SetChannel(&hUSB, 0, &hCAN);
USBD_Start(&hUSB);
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED1_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
uint32_t t_next_send = 100;
while (1) {
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED1_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
HAL_Delay(200);
HAL_GPIO_WritePin(LED1_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
HAL_Delay(200);
USBD_GS_CAN_SendFrameToHost(&hUsbDeviceFS, -1, 0x100, 0, 0, 0, 0);
HAL_Delay(500);
if (queue_size(q_from_host)>0) {
HAL_Delay(10);
if (USBD_GS_CAN_TxReady(&hUSB)) {
struct gs_host_frame *frame = queue_pop_front(q_from_host);
if (USBD_GS_CAN_Transmit(&hUSB, (uint8_t*)frame, sizeof(struct gs_host_frame))==USBD_OK) {
queue_push_back(q_free_frames, frame);
} else {
queue_push_back(q_from_host, frame);
}
}
}
if (HAL_GetTick() >= t_next_send) {
t_next_send = HAL_GetTick() + 500;
//USBD_GS_CAN_SendFrameToHost(&hUSB, -1, 0x100, 0, 0, 0, 0);
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED2_Pin);
}
}
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
@ -137,33 +114,6 @@ void SystemClock_Config(void)
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/* CAN init function */
void MX_CAN_Init(void)
{
hcan.Instance = CAN;
hcan.Init.Prescaler = 16;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_1TQ;
hcan.Init.BS2 = CAN_BS2_1TQ;
hcan.Init.TTCM = DISABLE;
hcan.Init.ABOM = DISABLE;
hcan.Init.AWUM = DISABLE;
hcan.Init.NART = DISABLE;
hcan.Init.RFLM = DISABLE;
hcan.Init.TXFP = DISABLE;
HAL_CAN_Init(&hcan);
}
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
{
@ -195,37 +145,3 @@ void MX_GPIO_Init(void)
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

57
src/queue.c Normal file
View File

@ -0,0 +1,57 @@
#include <queue.h>
#include <stdlib.h>
queue_t *queue_create(unsigned max_elements){
queue_t *q = calloc(1, sizeof(queue_t));
q->buf = calloc(max_elements, sizeof(void*));
q->max_elements = max_elements;
return q;
}
void queue_destroy(queue_t *q)
{
free(q->buf);
free(q);
}
unsigned queue_size(queue_t *q)
{
int primask = disable_irq();
unsigned retval = q->size;
enable_irq(primask);
return retval;
}
bool queue_is_empty(queue_t *q)
{
return queue_size(q)==0;
}
bool queue_push_back(queue_t *q, void *el)
{
bool retval = false;
int primask = disable_irq();
if (q->size < q->max_elements) {
unsigned pos = (q->first + q->size) % q->max_elements;
q->buf[pos] = el;
q->size += 1;
retval = true;
}
enable_irq(primask);
return retval;
}
void *queue_pop_front(queue_t *q)
{
int primask = disable_irq();
void *el = 0;
if (q->size > 0) {
el = q->buf[q->first];
q->first = (q->first + 1) % q->max_elements;
q->size -= 1;
}
enable_irq(primask);
return el;
}

View File

@ -274,9 +274,10 @@ USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev)
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 0x18);
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58);
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0xC0);
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0xC0);
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x01 , PCD_SNG_BUF, 0x110);
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x82 , PCD_SNG_BUF, 0x100);
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x82 , PCD_SNG_BUF, 0x100);
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x02 , PCD_SNG_BUF, 0x200);
return USBD_OK;
}

View File

@ -13,35 +13,37 @@
#define NUM_CAN_CHANNEL 1
typedef struct {
uint8_t ep0_buf[CAN_CMD_PACKET_SIZE];
uint8_t ep_out_buf[CAN_DATA_MAX_PACKET_SIZE];
uint8_t ep_in_buf[CAN_DATA_MAX_PACKET_SIZE];
__IO uint32_t TxState;
uint8_t req_bRequest;
uint16_t req_wLength;
uint16_t req_wValue;
uint8_t ep0_buf[CAN_CMD_PACKET_SIZE];
uint8_t ep_out_buf[CAN_DATA_MAX_PACKET_SIZE];
uint8_t ep_in_buf[CAN_DATA_MAX_PACKET_SIZE];
struct gs_host_config host_config;
queue_t *q_free_frames;
queue_t *q_from_host;
CAN_HandleTypeDef *channels[NUM_CAN_CHANNEL];
} USBD_GS_CAN_HandleTypeDef;
} USBD_GS_CAN_HandleTypeDef __attribute__ ((aligned (4)));
static uint8_t USBD_GS_CAN_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_GS_CAN_Start(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_GS_CAN_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_GS_CAN_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static uint8_t USBD_GS_CAN_EP0_RxReady(USBD_HandleTypeDef *pdev);
static uint8_t USBD_GS_CAN_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_GS_CAN_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t *USBD_GS_CAN_GetCfgDesc(uint16_t *len);
static uint8_t USBD_GS_CAN_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_GS_CAN_PrepareReceive(USBD_HandleTypeDef *pdev);
static uint8_t USBD_GS_CAN_Transmit(USBD_HandleTypeDef *pdev, uint8_t *buf, uint16_t len);
/* CAN interface class callbacks structure */
USBD_ClassTypeDef USBD_GS_CAN = {
USBD_GS_CAN_Init,
USBD_GS_CAN_Start,
USBD_GS_CAN_DeInit,
USBD_GS_CAN_Setup,
NULL, // EP0_TxSent
@ -111,26 +113,53 @@ __ALIGN_BEGIN uint8_t USBD_GS_CAN_CfgDesc[USB_CAN_CONFIG_DESC_SIZ] __ALIGN_END =
};
static uint8_t USBD_GS_CAN_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
uint8_t USBD_GS_CAN_Init(USBD_HandleTypeDef *pdev, queue_t *q_free_frames, queue_t *q_from_host)
{
(void) cfgidx;
uint8_t ret = 0;
USBD_LL_OpenEP(pdev, GSUSB_ENDPOINT_IN, USBD_EP_TYPE_BULK, CAN_DATA_MAX_PACKET_SIZE);
USBD_LL_OpenEP(pdev, GSUSB_ENDPOINT_OUT, USBD_EP_TYPE_BULK, CAN_DATA_MAX_PACKET_SIZE);
uint8_t ret = USBD_FAIL;
USBD_GS_CAN_HandleTypeDef *hcan = calloc(1, sizeof(USBD_GS_CAN_HandleTypeDef));
USBD_GS_CAN_HandleTypeDef *hcan = USBD_malloc(sizeof(USBD_GS_CAN_HandleTypeDef));
if(hcan == 0) {
ret = 1;
} else {
memset(hcan, 0, sizeof(USBD_GS_CAN_HandleTypeDef));
if(hcan != 0) {
hcan->q_free_frames = q_free_frames;
hcan->q_from_host = q_from_host;
pdev->pClassData = hcan;
USBD_GS_CAN_PrepareReceive(pdev);
ret = USBD_OK;
} else {
pdev->pClassData = 0;
}
return ret;
}
static uint8_t USBD_GS_CAN_Start(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
UNUSED(cfgidx);
uint8_t ret = USBD_FAIL;
if (pdev->pClassData) {
USBD_LL_OpenEP(pdev, GSUSB_ENDPOINT_IN, USBD_EP_TYPE_BULK, CAN_DATA_MAX_PACKET_SIZE);
USBD_LL_OpenEP(pdev, GSUSB_ENDPOINT_OUT, USBD_EP_TYPE_BULK, CAN_DATA_MAX_PACKET_SIZE);
USBD_GS_CAN_PrepareReceive(pdev);
ret = USBD_OK;
} else {
ret = USBD_FAIL;
}
return ret;
}
static uint8_t USBD_GS_CAN_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
UNUSED(cfgidx);
USBD_LL_CloseEP(pdev, GSUSB_ENDPOINT_IN);
USBD_LL_CloseEP(pdev, GSUSB_ENDPOINT_OUT);
return USBD_OK;
}
void USBD_GS_CAN_SetChannel(USBD_HandleTypeDef *pdev, uint8_t channel, CAN_HandleTypeDef* handle) {
USBD_GS_CAN_HandleTypeDef *hcan = (USBD_GS_CAN_HandleTypeDef*) pdev->pClassData;
if ((hcan!=NULL) && (channel < NUM_CAN_CHANNEL)) {
@ -138,23 +167,6 @@ void USBD_GS_CAN_SetChannel(USBD_HandleTypeDef *pdev, uint8_t channel, CAN_Handl
}
}
static uint8_t USBD_GS_CAN_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
(void) cfgidx;
uint8_t ret = 0;
USBD_LL_CloseEP(pdev, GSUSB_ENDPOINT_IN);
USBD_LL_CloseEP(pdev, GSUSB_ENDPOINT_OUT);
/* DeInit physical Interface components */
if(pdev->pClassData != NULL) {
USBD_free(pdev->pClassData);
pdev->pClassData = NULL;
}
return ret;
}
static uint8_t USBD_GS_CAN_EP0_RxReady(USBD_HandleTypeDef *pdev) {
USBD_GS_CAN_HandleTypeDef *hcan = (USBD_GS_CAN_HandleTypeDef*) pdev->pClassData;
@ -185,8 +197,8 @@ static uint8_t USBD_GS_CAN_EP0_RxReady(USBD_HandleTypeDef *pdev) {
can_enable(ch,
(mode->flags & GS_CAN_MODE_LOOP_BACK) != 0,
(mode->flags & GS_CAN_MODE_LISTEN_ONLY) != 0,
(mode->flags & GS_CAN_MODE_ONE_SHOT) != 0,
(mode->flags & GS_CAN_MODE_TRIPLE_SAMPLE) != 0
(mode->flags & GS_CAN_MODE_ONE_SHOT) != 0
// triple sampling not supported on bxCAN
);
}
@ -300,16 +312,22 @@ static uint8_t USBD_GS_CAN_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) {
static uint8_t USBD_GS_CAN_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) {
uint8_t retval = USBD_FAIL;
USBD_GS_CAN_HandleTypeDef *hcan = (USBD_GS_CAN_HandleTypeDef*)pdev->pClassData;
uint32_t rxlen = USBD_LL_GetRxDataSize(pdev, epnum);
if (rxlen >= sizeof(struct gs_host_frame)) {
struct gs_host_frame *hf = (struct gs_host_frame*) hcan->ep_out_buf;
// TODO process and send echo back to host (from non-interrupt context?)
struct gs_host_frame *hf = queue_pop_front(hcan->q_free_frames);
if (hf) {
memcpy(hf, hcan->ep_out_buf, sizeof(struct gs_host_frame));
queue_push_back(hcan->q_from_host, hf);
retval = USBD_OK;
}
}
USBD_GS_CAN_PrepareReceive(pdev);
return USBD_OK;
return retval;
}
static uint8_t *USBD_GS_CAN_GetCfgDesc(uint16_t *len)
@ -324,16 +342,18 @@ static uint8_t USBD_GS_CAN_PrepareReceive(USBD_HandleTypeDef *pdev)
return USBD_LL_PrepareReceive(pdev, GSUSB_ENDPOINT_OUT, hcan->ep_out_buf, CAN_DATA_MAX_PACKET_SIZE);
}
static uint8_t USBD_GS_CAN_Transmit(USBD_HandleTypeDef *pdev, uint8_t *buf, uint16_t len)
bool USBD_GS_CAN_TxReady(USBD_HandleTypeDef *pdev)
{
USBD_GS_CAN_HandleTypeDef *hcan = (USBD_GS_CAN_HandleTypeDef*)pdev->pClassData;
// TODO handle received CAN messages via this function
if (hcan->TxState == 0) {
hcan->TxState = 1;
return hcan->TxState == 0;
}
USBD_memset(hcan->ep_in_buf, 0, CAN_DATA_MAX_PACKET_SIZE);
USBD_memcpy(hcan->ep_in_buf, buf, len);
USBD_LL_Transmit(pdev, GSUSB_ENDPOINT_IN, hcan->ep_in_buf, len);
uint8_t USBD_GS_CAN_Transmit(USBD_HandleTypeDef *pdev, uint8_t *buf, uint16_t len)
{
USBD_GS_CAN_HandleTypeDef *hcan = (USBD_GS_CAN_HandleTypeDef*)pdev->pClassData;
if (USBD_GS_CAN_TxReady(pdev)) {
hcan->TxState = 1;
USBD_LL_Transmit(pdev, GSUSB_ENDPOINT_IN, buf, len);
return USBD_OK;
} else {
return USBD_BUSY;
@ -350,7 +370,6 @@ void USBD_GS_CAN_SendFrameToHost(
uint8_t flags,
uint8_t *data
) {
USBD_GS_CAN_HandleTypeDef *hcan = (USBD_GS_CAN_HandleTypeDef*)pdev->pClassData;
struct gs_host_frame hf;
if (dlc>8) { dlc = 8; }

13
src/util.c Normal file
View File

@ -0,0 +1,13 @@
#include <util.h>
inline int disable_irq(void) {
/* int primask;
asm volatile("mrs %0, PRIMASK\n"
"cpsid i\n" : "=r"(primask));
return primask & 1; */
}
inline void enable_irq(int primask) {
/* if (primask)
asm volatile("cpsie i\n"); */
}