pull/1/head
Ondřej Hruška 8 years ago
parent 10791c1d80
commit 330cc0ad69
  1. 2
      CMakeLists.txt
  2. 12
      Inc/main.h
  3. 2
      Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc/usbd_hid.h
  4. 103
      Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c
  5. 45
      Src/main.c
  6. 1
      Src/usb_device.c
  7. 30
      Src/usbd_desc.c

@ -22,7 +22,7 @@ include_directories(Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc)
include_directories(Middlewares/ST/STM32_USB_Device_Library/Core/Inc) include_directories(Middlewares/ST/STM32_USB_Device_Library/Core/Inc)
add_definitions(-DSTM32F103x6) add_definitions(-DSTM32F103x6)
add_executable(${PROJECT_NAME}.elf ${USER_SOURCES} ${LINKER_SCRIPT}) add_executable(${PROJECT_NAME}.elf ${USER_SOURCES} ${LINKER_SCRIPT} Inc/main.h)
target_link_libraries(${PROJECT_NAME}.elf HAL CMSIS USBFS) target_link_libraries(${PROJECT_NAME}.elf HAL CMSIS USBFS)

@ -0,0 +1,12 @@
//
// Created by ondra on 7.8.16.
//
#ifndef F103_USB_MAIN_H_H
#define F103_USB_MAIN_H_H
#include "stm32f1xx_hal.h"
extern void dbg(const char* msg);
#endif //F103_USB_MAIN_H_H

@ -54,7 +54,7 @@ extern "C" {
#define USB_HID_CONFIG_DESC_SIZ 34 #define USB_HID_CONFIG_DESC_SIZ 34
#define USB_HID_DESC_SIZ 9 #define USB_HID_DESC_SIZ 9
#define HID_MOUSE_REPORT_DESC_SIZE 74 //#define HID_MOUSE_REPORT_DESC_SIZE 74
#define HID_KEYBD_REPORT_DESC_SIZE 63 #define HID_KEYBD_REPORT_DESC_SIZE 63
#define HID_DESCRIPTOR_TYPE 0x21 #define HID_DESCRIPTOR_TYPE 0x21

@ -47,6 +47,7 @@
*/ */
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include <main.h>
#include "usbd_hid.h" #include "usbd_hid.h"
#include "usbd_desc.h" #include "usbd_desc.h"
#include "usbd_ctlreq.h" #include "usbd_ctlreq.h"
@ -163,7 +164,7 @@ __ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_
0x01, /*bNumEndpoints*/ 0x01, /*bNumEndpoints*/
0x03, /*bInterfaceClass: HID*/ 0x03, /*bInterfaceClass: HID*/
0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ 0x01, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*//* CHANGED */
0, /*iInterface: Index of string descriptor*/ 0, /*iInterface: Index of string descriptor*/
/******************** Descriptor of Joystick Mouse HID ********************/ /******************** Descriptor of Joystick Mouse HID ********************/
/* 18 */ /* 18 */
@ -174,7 +175,7 @@ __ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_
0x00, /*bCountryCode: Hardware target country*/ 0x00, /*bCountryCode: Hardware target country*/
0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
0x22, /*bDescriptorType*/ 0x22, /*bDescriptorType*/
HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ HID_KEYBD_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*//* CHANGED */
0x00, 0x00,
/******************** Descriptor of Mouse endpoint ********************/ /******************** Descriptor of Mouse endpoint ********************/
/* 27 */ /* 27 */
@ -200,7 +201,7 @@ __ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END =
0x00, /*bCountryCode: Hardware target country*/ 0x00, /*bCountryCode: Hardware target country*/
0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
0x22, /*bDescriptorType*/ 0x22, /*bDescriptorType*/
HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ HID_KEYBD_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*//* CHANGED */
0x00, 0x00,
}; };
@ -219,54 +220,40 @@ __ALIGN_BEGIN static uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_
0x00, 0x00,
}; };
__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END = __ALIGN_BEGIN static uint8_t HID_KEYBD_ReportDesc[HID_KEYBD_REPORT_DESC_SIZE] __ALIGN_END =
{ {
0x05, 0x01, 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x02, 0x09, 0x06, // USAGE (Keyboard)
0xA1, 0x01, 0xa1, 0x01, // COLLECTION (Application)
0x09, 0x01, 0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0xA1, 0x00, 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x05, 0x09, 0x15, 0x00, // LOGICAL_MINIMUM (0)
0x19, 0x01, 0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x29, 0x03, 0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x15, 0x00, 0x81, 0x02, // INPUT (Data,Var,Abs)
0x25, 0x01, 0x95, 0x01, // REPORT_COUNT (1)
0x95, 0x03, 0x75, 0x08, // REPORT_SIZE (8)
0x75, 0x01, 0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x95, 0x05, // REPORT_COUNT (5)
0x81, 0x02, 0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x01, 0x05, 0x08, // USAGE_PAGE (LEDs)
0x75, 0x05, 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
0x81, 0x01, 0x29, 0x05, // USAGE_MAXIMUM (Kana)
0x91, 0x02, // OUTPUT (Data,Var,Abs)
0x05, 0x01, 0x95, 0x01, // REPORT_COUNT (1)
0x09, 0x30, 0x75, 0x03, // REPORT_SIZE (3)
0x09, 0x31, 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
0x09, 0x38, 0x95, 0x06, // REPORT_COUNT (6)
0x75, 0x08, // REPORT_SIZE (8)
0x15, 0x81, 0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x7F, 0x25, 0x65, // LOGICAL_MAXIMUM (101)
0x75, 0x08, 0x05, 0x07, // USAGE_PAGE (Keyboard)
0x95, 0x03, 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x06, 0x81, 0x00, // INPUT (Data,Ary,Abs)
0xC0, 0x09, 0xc0 // END_COLLECTION
0x3c, 0x05,
0xff, 0x09,
0x01, 0x15,
0x00, 0x25,
0x01, 0x75,
0x01, 0x95,
0x02, 0xb1,
0x22, 0x75,
0x06, 0x95,
0x01, 0xb1,
0x01, 0xc0
}; };
/** /**
@ -315,8 +302,7 @@ static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{ {
/* Close HID EPs */ /* Close HID EPs */
USBD_LL_CloseEP(pdev, USBD_LL_CloseEP(pdev, HID_EPIN_ADDR);
HID_EPIN_ADDR);
/* FRee allocated memory */ /* FRee allocated memory */
if (pdev->pClassData != NULL) { if (pdev->pClassData != NULL) {
@ -350,9 +336,7 @@ static uint8_t USBD_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *re
break; break;
case HID_REQ_GET_PROTOCOL: case HID_REQ_GET_PROTOCOL:
USBD_CtlSendData(pdev, USBD_CtlSendData(pdev, (uint8_t *) &hhid->Protocol, 1);
(uint8_t *) &hhid->Protocol,
1);
break; break;
case HID_REQ_SET_IDLE: case HID_REQ_SET_IDLE:
@ -360,9 +344,7 @@ static uint8_t USBD_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *re
break; break;
case HID_REQ_GET_IDLE: case HID_REQ_GET_IDLE:
USBD_CtlSendData(pdev, USBD_CtlSendData(pdev, (uint8_t *) &hhid->IdleState, 1);
(uint8_t *) &hhid->IdleState,
1);
break; break;
default: default:
@ -375,8 +357,8 @@ static uint8_t USBD_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *re
switch (req->bRequest) { switch (req->bRequest) {
case USB_REQ_GET_DESCRIPTOR: case USB_REQ_GET_DESCRIPTOR:
if (req->wValue >> 8 == HID_REPORT_DESC) { if (req->wValue >> 8 == HID_REPORT_DESC) {
len = MIN(HID_MOUSE_REPORT_DESC_SIZE, req->wLength); len = MIN(HID_KEYBD_REPORT_DESC_SIZE, req->wLength);
pbuf = HID_MOUSE_ReportDesc; pbuf = HID_KEYBD_ReportDesc;
} }
else if (req->wValue >> 8 == HID_DESCRIPTOR_TYPE) { else if (req->wValue >> 8 == HID_DESCRIPTOR_TYPE) {
pbuf = USBD_HID_Desc; pbuf = USBD_HID_Desc;
@ -476,7 +458,6 @@ static uint8_t *USBD_HID_GetCfgDesc(uint16_t *length)
*/ */
static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
{ {
/* Ensure that the FIFO is empty before a new transfer, this condition could /* Ensure that the FIFO is empty before a new transfer, this condition could
be caused by a new transfer before the end of the previous transfer */ be caused by a new transfer before the end of the previous transfer */
((USBD_HID_HandleTypeDef *) pdev->pClassData)->state = HID_IDLE; ((USBD_HID_HandleTypeDef *) pdev->pClassData)->state = HID_IDLE;

@ -66,6 +66,13 @@ static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN 0 */ /* USER CODE BEGIN 0 */
/** Print a debug message */
void dbg(const char *msg)
{
HAL_UART_Transmit(&huart2, (char*)msg, (uint16_t)strlen(msg), 10);
}
/* USER CODE END 0 */ /* USER CODE END 0 */
void _Noreturn main(void) void _Noreturn main(void)
@ -94,13 +101,29 @@ void _Noreturn main(void)
/* Infinite loop */ /* Infinite loop */
/* USER CODE BEGIN WHILE */ /* USER CODE BEGIN WHILE */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
char msg[] = "A\r\n";
while (1) { while (1) {
/* USER CODE END WHILE */ /* USER CODE END WHILE */
/* USER CODE BEGIN 3 */ /* USER CODE BEGIN 3 */
HAL_Delay(100); HAL_Delay(500);
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
dbg(msg);
// Toggle
if(msg[0] == 'A') {
msg[0] = 'B';
} else {
msg[0] = 'A';
}
} }
#pragma clang diagnostic pop
/* USER CODE END 3 */ /* USER CODE END 3 */
} }
@ -228,26 +251,6 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : PA0 PA1 PA4 PA5
PA6 PA7 PA8 PA9
PA10 PA13 PA14 PA15 */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5
| GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9
| GPIO_PIN_10 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PB0 PB1 PB2 PB10
PB11 PB12 PB13 PB14
PB15 PB3 PB4 PB5
PB6 PB7 PB8 PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_10
| GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14
| GPIO_PIN_15 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5
| GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
} }
/* USER CODE BEGIN 4 */ /* USER CODE BEGIN 4 */

@ -51,7 +51,6 @@ void MX_USB_DEVICE_Init(void)
USBD_RegisterClass(&hUsbDeviceFS, &USBD_HID); USBD_RegisterClass(&hUsbDeviceFS, &USBD_HID);
USBD_Start(&hUsbDeviceFS); USBD_Start(&hUsbDeviceFS);
} }
/** /**
* @} * @}

@ -83,11 +83,17 @@
* @{ * @{
*/ */
uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
#ifdef USB_SUPPORT_USER_STRING_DESC #ifdef USB_SUPPORT_USER_STRING_DESC
@ -199,12 +205,10 @@ uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *leng
*/ */
uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{ {
if(speed == 0) if (speed == 0) {
{
USBD_GetString(USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); USBD_GetString(USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
} }
else else {
{
USBD_GetString(USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); USBD_GetString(USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
} }
return USBD_StrDesc; return USBD_StrDesc;
@ -232,12 +236,10 @@ uint8_t * USBD_FS_ManufacturerStrDescriptor( USBD_SpeedTypeDef speed , uint16_t
*/ */
uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{ {
if(speed == USBD_SPEED_HIGH) if (speed == USBD_SPEED_HIGH) {
{
USBD_GetString(USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); USBD_GetString(USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length);
} }
else else {
{
USBD_GetString(USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); USBD_GetString(USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length);
} }
return USBD_StrDesc; return USBD_StrDesc;
@ -252,12 +254,10 @@ uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *leng
*/ */
uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{ {
if(speed == USBD_SPEED_HIGH) if (speed == USBD_SPEED_HIGH) {
{
USBD_GetString(USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); USBD_GetString(USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
} }
else else {
{
USBD_GetString(USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); USBD_GetString(USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
} }
return USBD_StrDesc; return USBD_StrDesc;
@ -272,12 +272,10 @@ uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *leng
*/ */
uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
{ {
if(speed == 0) if (speed == 0) {
{
USBD_GetString(USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); USBD_GetString(USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
} }
else else {
{
USBD_GetString(USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); USBD_GetString(USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
} }
return USBD_StrDesc; return USBD_StrDesc;

Loading…
Cancel
Save