2017-11-03 08:04 AM
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 :
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 #stm32f1Solved! Go to Solution.
2017-11-05 04:24 AM
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!2017-11-05 04:24 AM
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!2018-01-07 11:25 PM
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
2018-01-09 02:02 AM
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
2018-01-09 02:53 AM
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!
Geo2018-01-09 03:43 AM
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.
2018-01-09 10:53 PM
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
2018-01-10 01:49 AM
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.
Geo2019-05-12 07:33 PM
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--);
}