1. 最近在露天買了一張stm32f072b discovery board, 基本上stm32f072 = stm32f051 + usb, 而在研
究了一下之後, 也順手將FreeRTOS v8.2.3和USB Virtual Com-Port & HID移植完畢, 接著會將
移植的過程簡單的描述一下.
2. main flow:
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
/* Steup Hardware. */
prvSetupHardware();
/* Create task. */
vAltStartTask1Tasks( mainCREATOR_TASK_PRIORITY); //Task1
vAltStartTask2Tasks( mainCREATOR_TASK_PRIORITY); //Task2
/* Start the scheduler. */
vTaskStartScheduler();
/* Will only get here if there was not enough heap space to create the idle task. */
return 0;
}
static void prvSetupHardware( void )
{
/* LED Init. */
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
STM_EVAL_LEDInit(LED5);
STM_EVAL_LEDInit(LED6);
STM_EVAL_LEDOff(LED3);
STM_EVAL_LEDOff(LED4);
STM_EVAL_LEDOff(LED5);
STM_EVAL_LEDOff(LED6);
/* USB VCP or HID Init. */
USB_Init();
}
- USB VCP & HID的切換在usb_define.h裡面去做選擇.
- Task1: 就是固定每100msec去Toggle LED3而已.
//Task1
static portTASK_FUNCTION( vTask1FunctionTask, pvParameters )
{
portTickType xLastWakeTime;
/* Just to stop compiler warnings. */
( void ) pvParameters;
xLastWakeTime = xTaskGetTickCount();
for( ;; )
{
Task1Task_Counter++;
STM_EVAL_LEDToggle(LED3);
vTaskDelayUntil( &xLastWakeTime, ( 100 / portTICK_RATE_MS ) );
}
}
- Task2: 固定每200msec會去Toggle LED6外, 還會根據usb_define選擇的結果來輸出VCP or HID測試信號.
//Task2
static portTASK_FUNCTION( vTask2FunctionTask, pvParameters )
{
portTickType xLastWakeTime;
/* Just to stop compiler warnings. */
( void ) pvParameters;
xLastWakeTime = xTaskGetTickCount();
for( ;; )
{
Task2Task_Counter++;
STM_EVAL_LEDToggle(LED6);
#ifdef HIDRAW
for(int i=0;i<32;i++)
HID_Buffer[i] = i;
USBD_HID_SendReport (&USB_Device_dev, HID_Buffer, 32);
PrevXferDone = 0;
#endif
#ifdef VirtualComPort
memset(VCP_Buffer,0x00,sizeof(VCP_Buffer));
sprintf(VCP_Buffer, "$,HelloWorld:%d\r\n", vcp_counter++);
USBD_VCP_SendReport(&USB_Device_dev, CDC_IN_EP, (uint8_t*)&VCP_Buffer[0], strlen(VCP_Buffer));
#endif
vTaskDelayUntil( &xLastWakeTime, ( 200 / portTICK_RATE_MS ) );
}
}
3. Test Flow: 當我們利用usb_define.h來切換VCP/HID的時候, 程式會根據切換的結果來做不同
的Init, 因此在裝置管理員上看到的裝置也不相同.
#ifdef HIDRAW
#define USBD_VID 0x0483
#define USBD_PID 0x5750
#define USBD_LANGID_STRING 0x409
#define USBD_MANUFACTURER_STRING "STMicroelectronics"
#define USBD_PRODUCT_FS_STRING "Custome HID"
#define USBD_CONFIGURATION_FS_STRING "HID Config"
#define USBD_INTERFACE_FS_STRING "HID Interface"
#endif
#ifdef VirtualComPort
#define USBD_VID 0x0483
#define USBD_PID 0x5740
/** @defgroup USB_String_Descriptors
* @{
*/
#define USBD_LANGID_STRING 0x409
#define USBD_MANUFACTURER_STRING "STMicroelectronics"
#define USBD_PRODUCT_FS_STRING "STM32 Virtual ComPort in FS Mode"
#define USBD_CONFIGURATION_FS_STRING "VCP Config"
#define USBD_INTERFACE_FS_STRING "VCP Interface"
#endif
- HID: 假設我們要測試HID, 可以利用BusHound這套軟體來測試, 它可以針對特定裝置來側錄輸出入的log, 如下圖, 可以看到系統新增出一個HID-compliant device, 透過Hardware ID(VID/PID)可以確認這就是我們目前在使用的stm32f072 discovery board.
- 從log上看到和我們程式的結果符合, 固定每200msec輸出0x00~0x1f這32個bytes.
- 燒錄程式後, 先確認目前ST VCP的編號是多少? (ex: COM14)
4. Test Code Link: