cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 USB device get report

Geo En
Associate III
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
1 ACCEPTED SOLUTION

Accepted Solutions
Geo En
Associate III
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!

View solution in original post

8 REPLIES 8
Geo En
Associate III
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 II
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

HARI krishna
Associate II
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

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

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.

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

Posted on January 10, 2018 at 09:49

Hello,

I guess you need to open an other post because your questions are far away from the initial topic of this one...

Your first question is about C basics... I gave you a function that sends char. So if you need to send strings, create a function that uses my function. Something like that:

void USBD_CUSTOM_HID_SendString(char *string){
 const char* p;
 for (p = string; *p != '\0'; p++) {
 keyboardHID.keys[0] = (*p - 0x5D);// Convert ASCII lowercase letter to USB key code
 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);
 }
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

And use it in the 'int main(void)' like this:

USBD_CUSTOM_HID_SendString('hello\0');�?�?�?�?

But this will work only for lowercase letters. I am not even sure that the '\0' is required in 'hello\0'.

I suppose that you will be able to modify this function to use uppercase and numbers.

For the second question, everything is in the

Mass Storage example. I cannot help you with that one, I have not used it yet. If you do not find your answer, create a new topic.

Geo
dungeonlords789
Senior III

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--);
}