![]() |
USB Component
Version 6.12.8
MDK Middleware for USB Device and Host Communication
|
This example application shows how to communicate with serial RS232 adapter with Prolific PL2303 UART-to-USB bridge chip from a microcontroller. It is a simple demonstration on how to send data from the USB Host via the USB to serial RS232 adapter to an attached serial terminal. Here, the USB Host sends "Test!" to the USB to serial RS232 adapter and stores all incoming data from the device into the array receive_buf
. This example is using the Custom Class because the PL2303 is not USB CDC ACM compliant.
The following picture shows an exemplary connection of the development board and an PL2303 based Device. The PL2303 Device is connected to a PC via RS232 to check incoming messages.
Create a new project in MDK. In this example, we are using the MCBSTM32F400 board with the STM32F407IGHx device. In the Manage Run-Time Environment window, select the following components:
Click the Resolve button and then OK.
Before continuing to add the required source code, you need to add a template file called USBH_PL2303.c:
Finally, your Project should look like this:
#include <stdio.h> /* Standard I/O .h-file */ #include <ctype.h> /* Character functions */ #include <string.h> /* String and memory functions */ #include "cmsis_os2.h" /* CMSIS RTOS definitions */ #include "rl_usb.h" /* RL-USB function prototypes */ #include "Board_LED.h" #include "Driver_USART.h" #include "stm32f4xx_hal.h" /* UART Driver */ extern ARM_DRIVER_USART Driver_USART9; #define ptrUART_USB (&Driver_USART9) static uint8_t receive_buf[64]; extern uint32_t os_time; uint32_t HAL_GetTick(void) { return os_time; } /* System Clock Configuration */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; /* Enable Power Control clock */ __PWR_CLK_ENABLE(); /* The voltage scaling allows optimizing the power consumption when the device is clocked below the maximum system frequency (see datasheet). */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /* Enable HSE Oscillator and activate PLL with HSE as source */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 25; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 7; HAL_RCC_OscConfig(&RCC_OscInitStruct); /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); } /*------------------------------------------------------------------------------ * UART Done Callback *----------------------------------------------------------------------------*/ void UART_Done (uint32_t event) { switch (event) { case ARM_USART_EVENT_SEND_COMPLETE: // All requested data was transmitted LED_On(1); break; case ARM_USART_EVENT_RECEIVE_COMPLETE: // After 64 bytes were received, restart new reception ptrUART_USB->Receive (receive_buf, 64); LED_On(2); break; } } /*------------------------------------------------------------------------------ * Application *----------------------------------------------------------------------------*/ int main (void) { static bool con_last = false; bool con; HAL_Init(); /* Initialize the HAL Library */ SystemClock_Config(); /* Configure the System Clock */ LED_Initialize (); USBH_Initialize (0); /* Initialize USB Host 0 */ while (1) { con = (USBH_CustomClass_GetDeviceStatus(0) == usbOK); if (con ^ con_last) { if (con) { con_last = true; LED_On(0); osDelay(1000); /* Initialize and configure UART <-> USB Bridge */ ptrUART_USB->Initialize (UART_Done); ptrUART_USB->PowerControl(ARM_POWER_FULL); ptrUART_USB->Control (ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_8 | ARM_USART_PARITY_NONE | ARM_USART_STOP_BITS_1 | ARM_USART_FLOW_CONTROL_NONE , 115200 ); ptrUART_USB->Control (ARM_USART_CONTROL_TX, 1); ptrUART_USB->Control (ARM_USART_CONTROL_RX, 1); ptrUART_USB->Receive (receive_buf, 64); ptrUART_USB->Send ("Test!", 5); } else { con_last = false; LED_Off(0); } } else { if (con) { // Receive data can be checked by polling as callback will be called // only when all requested number of bytes were received // (in this case 64 bytes) if (ptrUART_USB->GetRxCount() == 5) { // If 5 bytes were received } } osDelay(1000); } } }
Before building the project, you need to edit these configuration files:
Before building and downloading the project to the target, make sure that the correct debugger is set in the Options for Target dialog (ALT + F7). You may then build and download the example project to the evaluation board using the µVision commands:
After these steps, the project should start executing on your evaluation kit. In case of errors, refer to the Evaluation Board User's Guide for configuration information.
receive_buf
buffer called by the ->Receive
function.