mirror of
https://gitlab.com/hyperglitch/jellyfish.git
synced 2025-12-26 07:06:47 +00:00
140 lines
4.2 KiB
C
140 lines
4.2 KiB
C
/**
|
|
******************************************************************************
|
|
* @file usbd_cdc_interface.c
|
|
* @author MCD Application Team
|
|
* @brief Source file for USBD CDC interface
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2025 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "usbd_cdc_interface.h"
|
|
|
|
#include "main.h"
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* Private define ------------------------------------------------------------*/
|
|
#define APP_RX_DATA_SIZE 2048
|
|
#define APP_TX_DATA_SIZE 2048
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* Private variables ---------------------------------------------------------*/
|
|
USBD_CDC_LineCodingTypeDef LineCoding =
|
|
{
|
|
115200, /* baud rate*/
|
|
0x00, /* stop bits-1*/
|
|
0x00, /* parity - none*/
|
|
0x08 /* nb. of bits 8*/
|
|
};
|
|
|
|
uint8_t UserRxBuffer[APP_RX_DATA_SIZE];/* Received Data over USB are stored in this buffer */
|
|
uint8_t UserTxBuffer[APP_TX_DATA_SIZE];/* Received Data over UART (CDC interface) are stored in this buffer */
|
|
uint32_t BuffLength;
|
|
uint32_t UserTxBufPtrIn = 0;/* Increment this pointer or roll it back to
|
|
start address when data are received over USART */
|
|
uint32_t UserTxBufPtrOut = 0; /* Increment this pointer or roll it back to
|
|
start address when data are sent over USB */
|
|
|
|
usb_cdc_rx_callback_t rx_callback = NULL;
|
|
|
|
/* USB handler */
|
|
extern USBD_HandleTypeDef USBD_Device;
|
|
|
|
/* Function Prototypes */
|
|
static int8_t CDC_Itf_Init(void);
|
|
static int8_t CDC_Itf_DeInit(void);
|
|
static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length);
|
|
static int8_t CDC_Itf_Receive(uint8_t* pbuf, uint32_t *Len);
|
|
static int8_t CDC_Itf_TransmitCplt(uint8_t *pbuf, uint32_t *Len, uint8_t epnum);
|
|
|
|
/* CDC Interface structure */
|
|
USBD_CDC_ItfTypeDef USBD_CDC_fops = {
|
|
CDC_Itf_Init,
|
|
CDC_Itf_DeInit,
|
|
CDC_Itf_Control,
|
|
CDC_Itf_Receive,
|
|
CDC_Itf_TransmitCplt
|
|
};
|
|
|
|
/* Initialization */
|
|
static int8_t CDC_Itf_Init(void)
|
|
{
|
|
USBD_CDC_SetTxBuffer(&USBD_Device, UserTxBuffer, 0, 0);
|
|
USBD_CDC_SetRxBuffer(&USBD_Device, UserRxBuffer);
|
|
return USBD_OK;
|
|
}
|
|
|
|
/* DeInitialization */
|
|
static int8_t CDC_Itf_DeInit(void)
|
|
{
|
|
return USBD_OK;
|
|
}
|
|
|
|
/* CDC class request handler */
|
|
static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length)
|
|
{
|
|
switch (cmd)
|
|
{
|
|
case CDC_SET_LINE_CODING:
|
|
LineCoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |
|
|
(pbuf[2] << 16) | (pbuf[3] << 24));
|
|
LineCoding.format = pbuf[4];
|
|
LineCoding.paritytype = pbuf[5];
|
|
LineCoding.datatype = pbuf[6];
|
|
break;
|
|
|
|
case CDC_GET_LINE_CODING:
|
|
pbuf[0] = (uint8_t)(LineCoding.bitrate);
|
|
pbuf[1] = (uint8_t)(LineCoding.bitrate >> 8);
|
|
pbuf[2] = (uint8_t)(LineCoding.bitrate >> 16);
|
|
pbuf[3] = (uint8_t)(LineCoding.bitrate >> 24);
|
|
pbuf[4] = LineCoding.format;
|
|
pbuf[5] = LineCoding.paritytype;
|
|
pbuf[6] = LineCoding.datatype;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
return USBD_OK;
|
|
}
|
|
|
|
|
|
void USBD_CDC_Register_Rx_Callback(usb_cdc_rx_callback_t callback) {
|
|
rx_callback = callback;
|
|
}
|
|
|
|
|
|
/* Data received from USB host */
|
|
static int8_t CDC_Itf_Receive(uint8_t* pbuf, uint32_t *Len)
|
|
{
|
|
// Echo back received data (example behavior)
|
|
//USBD_CDC_SetTxBuffer(&USBD_Device, Buf, *Len, 0);
|
|
//USBD_CDC_TransmitPacket(&USBD_Device, 0);
|
|
|
|
if (rx_callback != NULL) {
|
|
rx_callback(pbuf, *Len);
|
|
}
|
|
|
|
// Prepare to receive next packet
|
|
USBD_CDC_ReceivePacket(&USBD_Device);
|
|
return USBD_OK;
|
|
}
|
|
|
|
/* USB IN transfer complete callback */
|
|
static int8_t CDC_Itf_TransmitCplt(uint8_t *pbuf, uint32_t *Len, uint8_t epnum)
|
|
{
|
|
UNUSED(pbuf);
|
|
UNUSED(Len);
|
|
UNUSED(epnum);
|
|
return USBD_OK;
|
|
}
|