/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file ux_device_cdc_acm.c * @author MCD Application Team * @brief USBX Device applicative file ****************************************************************************** * @attention * * Copyright (c) 2024 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. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "ux_device_cdc_acm.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "stm32h5xx_hal.h" #include "main.h" #include "string.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ UX_SLAVE_CLASS_CDC_ACM *cdc_acm; UX_SLAVE_CLASS_CDC_ACM_LINE_CODING_PARAMETER CDC_VCP_LineCoding = { 115200, /* baud rate */ 0x00, /* stop bits-1 */ 0x00, /* parity - none */ 0x08 /* nb. of bits 8 */ }; static char Tx_Buffer[]="Hello World\r\n"; static char Rx_Buffer[1024] = {0}; extern TX_SEMAPHORE semaphore; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief USBD_CDC_ACM_Activate * This function is called when insertion of a CDC ACM device. * @param cdc_acm_instance: Pointer to the cdc acm class instance. * @retval none */ VOID USBD_CDC_ACM_Activate(VOID *cdc_acm_instance) { /* USER CODE BEGIN USBD_CDC_ACM_Activate */ /* Save the CDC instance */ cdc_acm = (UX_SLAVE_CLASS_CDC_ACM*) cdc_acm_instance; /* Set device class_cdc_acm with default parameters */ if (ux_device_class_cdc_acm_ioctl(cdc_acm, UX_SLAVE_CLASS_CDC_ACM_IOCTL_SET_LINE_CODING, &CDC_VCP_LineCoding) != UX_SUCCESS) { Error_Handler(); } /* USER CODE END USBD_CDC_ACM_Activate */ return; } /** * @brief USBD_CDC_ACM_Deactivate * This function is called when extraction of a CDC ACM device. * @param cdc_acm_instance: Pointer to the cdc acm class instance. * @retval none */ VOID USBD_CDC_ACM_Deactivate(VOID *cdc_acm_instance) { /* USER CODE BEGIN USBD_CDC_ACM_Deactivate */ UX_PARAMETER_NOT_USED(cdc_acm_instance); /* USER CODE END USBD_CDC_ACM_Deactivate */ return; } /** * @brief USBD_CDC_ACM_ParameterChange * This function is invoked to manage the CDC ACM class requests. * @param cdc_acm_instance: Pointer to the cdc acm class instance. * @retval none */ VOID USBD_CDC_ACM_ParameterChange(VOID *cdc_acm_instance) { /* USER CODE BEGIN USBD_CDC_ACM_ParameterChange */ UX_PARAMETER_NOT_USED(cdc_acm_instance); ULONG request; UX_SLAVE_TRANSFER *transfer_request; UX_SLAVE_DEVICE *device; /* Get the pointer to the device. */ device = &_ux_system_slave -> ux_system_slave_device; /* Get the pointer to the transfer request associated with the control endpoint. */ transfer_request = &device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request; request = *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_REQUEST); switch (request) { case UX_SLAVE_CLASS_CDC_ACM_SET_LINE_CODING : /* Get the Line Coding parameters */ if (ux_device_class_cdc_acm_ioctl(cdc_acm, UX_SLAVE_CLASS_CDC_ACM_IOCTL_GET_LINE_CODING, &CDC_VCP_LineCoding) != UX_SUCCESS) { Error_Handler(); } break; case UX_SLAVE_CLASS_CDC_ACM_GET_LINE_CODING : /* Set the Line Coding parameters */ if (ux_device_class_cdc_acm_ioctl(cdc_acm, UX_SLAVE_CLASS_CDC_ACM_IOCTL_SET_LINE_CODING, &CDC_VCP_LineCoding) != UX_SUCCESS) { Error_Handler(); } break; case UX_SLAVE_CLASS_CDC_ACM_SET_CONTROL_LINE_STATE : default : break; } /* USER CODE END USBD_CDC_ACM_ParameterChange */ return; } /* USER CODE BEGIN 1 */ VOID usbx_cdc_acm_write_thread_entry(ULONG thread_input) { UX_PARAMETER_NOT_USED(thread_input); ULONG actual_length; while (1) { /*wait for button press*/ tx_semaphore_get(&semaphore, TX_WAIT_FOREVER); /*tranmit data*/ ux_device_class_cdc_acm_write(cdc_acm, (UCHAR *)(&Tx_Buffer), (strlen(Tx_Buffer)), &actual_length); } } static volatile uint16_t rx_counter = 0; VOID usbx_cdc_acm_read_thread_entry(ULONG thread_input) { UX_PARAMETER_NOT_USED(thread_input); ULONG actual_length = 0; UINT status = 0xff; UX_SLAVE_DEVICE *device = NULL; device = &_ux_system_slave->ux_system_slave_device; while(1) { if ((device->ux_slave_device_state == UX_DEVICE_CONFIGURED) && (cdc_acm != UX_NULL)) { status = ux_device_class_cdc_acm_read(cdc_acm, (UCHAR *)(&Rx_Buffer), 10, &actual_length); if(status == UX_SUCCESS && actual_length > 0) { rx_counter++; } } tx_thread_sleep(50); } } /* USER CODE END 1 */