Skip to main content
Geo En
Associate III
November 3, 2017
Solved

STM32F103 USB device get report

  • November 3, 2017
  • 8 replies
  • 3224 views
Posted on November 03, 2017 at 16:04

Hello everyone,

As several people on this forum, I am trying to implement some kind of 'USB keyboard' with an STM32F103C8.

I am working with truestudio and cubeMx, and I use the USB library provided.

I have update the descriptor and I have no problem to send keys and modifiers to the computer, reproducing exactly a keyboard behavior. However, in those examples :

  • STM32Cube_FW_F1_V1.6.0/Projects/STM3210C_EVAL/Applications/USB_Device/CustomHID_Standalone
  • STM32Cube_FW_F1_V1.6.0/Projects/STM3210C_EVAL/Applications/USB_Device/HID_Standalone

I have not seen anyway to read the report provide by the computer about the LED states (Caps Lock, Scroll Lock, Num Lock). My descriptor includes an output report for this information. 

In fact, I would like to know how it is possible to implement the same function as 'USBD_HID_SendReport' to 'receive report'. Or more simply, where can I find an example doing that?

Thanks for your help!

#usb-hid-keyboard #stm32f1
This topic has been closed for replies.
Best answer by Geo En
Posted on November 05, 2017 at 13:24

Ok I had not understood correctly the library.

The out reports are handled in usbd_custom_hid_if.c in the function CUSTOM_HID_OutEvent_FS.

Everything is working correctly now!

8 replies

Geo En
Geo EnAuthorBest answer
Associate III
November 5, 2017
Posted on November 05, 2017 at 13:24

Ok I had not understood correctly the library.

The out reports are handled in usbd_custom_hid_if.c in the function CUSTOM_HID_OutEvent_FS.

Everything is working correctly now!
HARI krishna
Associate III
January 8, 2018
Posted on January 08, 2018 at 08:25

Hi

enjolras.g

,

Could you please us the same true studio code for stm32f103C8 device USB HID keyboard application and i am not getting keyboard enumeration .

Tried to modify the mouse examples but not success

Regards

Hari

Geo En
Geo EnAuthor
Associate III
January 9, 2018
Posted on January 09, 2018 at 10:53

Hey

Krishna.Hari

,

In fact you can't use directly mouse example. It is really hard to modify it to create a keyboard. You need to use custom usb hid. You can take a look at my github

https://github.com/F0cks/F0cks_BluePill_Keyboard/tree/develop

. (develop branch, not master).

I have created a software to replace a broken board in an old keyboard with a bluepill (stm32f103C8).

You do not need to understand the whole code, just compare the differences between original HAL and my modifications.

You can also use and test my code to create your own keyboard. You just need to modify the descriptor (

https://github.com/F0cks/F0cks_BluePill_Keyboard/blob/develop/Application/Inc/keyboard_descriptor.h

) and change gpio and keymap assignment (

https://github.com/F0cks/F0cks_BluePill_Keyboard/blob/develop/Application/Inc/keyboard_config.h

).

Good luck!

Geo

HARI krishna
Associate III
January 9, 2018
Posted on January 09, 2018 at 11:02

Thanks for your support,

Got the key stroke emulation and now st works as keyboard.

Could you please help me on the bellow concepts :

1.How to send 'Hello' string over HID class Device.(Not using COM)

2.What is meant by USB Mass storage class in stm32f103c8t6 i.e.how to interface SD card using SPI.How SPI and USB is related in driver.

Regards

hari

Geo En
Geo EnAuthor
Associate III
January 9, 2018
Posted on January 09, 2018 at 11:43

I think you can modify main.c to write 'Hello'. I would remove everything in 'while(1)' loop and add something like that:

// Empty hid handler
keyboardHID.modifiers = 0;
for ( i=0; i<CUSTOM_KEYBOARD_SIMULT_KEYS; i++ ) {
 keyboardHID.keys[i] = 0x00;
}
keyboardHID.modifiers = 0x20; // Shift
keyboardHID.keys[0] = 0x0B; // H
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(keyboardHID_t));
HAL_Delay(10);
keyboardHID.modifiers = 0x00;
keyboardHID.keys[0] = 0x00;
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(keyboardHID_t));
HAL_Delay(10);
keyboardHID.keys[0] = 0x08; // e
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(keyboardHID_t));
HAL_Delay(10);
keyboardHID.keys[0] = 0x00;
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(keyboardHID_t));
HAL_Delay(10);
keyboardHID.keys[0] = 0x0F; // l
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(keyboardHID_t));
HAL_Delay(10);
keyboardHID.keys[0] = 0x00;
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(keyboardHID_t));
HAL_Delay(10);
keyboardHID.keys[0] = 0x0F; // l
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(keyboardHID_t));
HAL_Delay(10);
keyboardHID.keys[0] = 0x00;
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(keyboardHID_t));
HAL_Delay(10);
keyboardHID.keys[0] = 0x12; // o
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(keyboardHID_t));
HAL_Delay(10);
keyboardHID.keys[0] = 0x00;
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, &keyboardHID, sizeof(keyboardHID_t));
HAL_Delay(10);�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

HARI krishna wrote:

2.What is meant by USB Mass storage class in stm32f103c8t6 i.e.how to interface SD card using SPI.How SPI and USB is related in driver.

??? I do not understand the relation between keyboard and that. But you can probably update the descriptor to add a mass storage section. SPI and USB are not related at all. If you also want to use SPI you need to init SPI library. You will probably have to look at the mass storage example.

HARI krishna
Associate III
January 10, 2018
Posted on January 10, 2018 at 06:53

Thanks for reply,

your sending string as characters but if i need to send long string to PC using USB(Device mode) how to do this?

About SPI :How to use USB mass storage class in STM32F103C8 device or how to interface SD cards to the STM32F103 to show it as a flash drive in PC

Need help on this

dungeonlords789
Associate III
May 13, 2019

I try to get bytes from PC at CUSTOM_HID_OutEvent_FS void, but because of HAL_Delay() MCU goes to while(1). When I am debugging MCU via ST-Link, HAL_Delay() is ok! But when I just run my code - HAL_Delay() sets MCU to while(1). Why? Because of void call? When I use myself void delayUS all is ok!

static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state){
 
 USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef*)hUsbDeviceFS.pClassData; 
 
 
 for (uint8_t i = 0; i <=7 ; i++)
 
 {
 
 dataToReceive[i] = hhid->Report_buf[i];
 
 }
 
 
 if (dataToReceive[0] == 0x6){ //when send 6 from PC ->BEEP!
 
 HAL_GPIO_WritePin(SPEAKER_PC3_GPIO_Port,SPEAKER_PC3_Pin, GPIO_PIN_SET); //on beep
 
 HAL_Delay(100);
 
 HAL_GPIO_WritePin(SPEAKER_PC3_GPIO_Port,SPEAKER_PC3_Pin, GPIO_PIN_RESET); //off beep
 
 }
 
 return (USBD_OK);
}
static inline void delayUS(uint32_t us)	//if no static inline then while(1) at CUSTOM_HID_OutEvent_FS{
	volatile uint32_t counter = 7*us;
	while(counter--);
}