This commit is contained in:
2025-05-21 22:44:23 +03:00
parent 1e00ea2791
commit 20ee676f01
6 changed files with 186 additions and 209 deletions

View File

@@ -22,7 +22,7 @@
EndBSPDependencies */
/* Includes ------------------------------------------------------------------*/
#include "usbd_cdc_if_template.h"
#include "usbd_cdc_if.h"
/** @addtogroup STM32_USB_DEVICE_LIBRARY
* @{
@@ -86,6 +86,14 @@ USBD_CDC_LineCodingTypeDef linecoding =
0x08 /* nb. of bits 8*/
};
/* Create buffer for reception and transmission */
/* It's up to user to redefine and/or remove those define */
/** Received data over USB are stored in this buffer */
uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
/** Data to send over USB CDC are stored in this buffer */
uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
extern USBD_HandleTypeDef hUsbDeviceFS;
/* Private functions ---------------------------------------------------------*/
/**
@@ -96,9 +104,8 @@ USBD_CDC_LineCodingTypeDef linecoding =
*/
static int8_t TEMPLATE_Init(void)
{
/*
Add your initialization code here
*/
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
return (0);
}
@@ -208,8 +215,28 @@ static int8_t TEMPLATE_Receive(uint8_t *Buf, uint32_t *Len)
{
UNUSED(Buf);
UNUSED(Len);
if(Buf[0] == '1')
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
else if(Buf[0] == '0')
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
return (USBD_OK);
}
return (0);
uint8_t TEMPLATE_Transmit(uint8_t* Buf, uint16_t Len)
{
uint8_t result = USBD_OK;
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
if (hcdc->TxState != 0){
return
USBD_BUSY;
}
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
return result;
}
/**