Implementing USB HID Keyboard Functionality on the MM32F5330 Development Board
The MM32F5330 development board features the MM32F5333D7PV microcontroller, which utilizes the Armv8-M architecture "Star-MC1" processor from Anxinke Technology, operating at up to 180 MHz. It includes 128 KB of Flash memory and 32 KB of SRAM, both with ECC (Error Checking and Correction) for data integrity. The board entegrates various peripherals such as ADC, DAC, analog comparators, timers, and UART, along with communication interfaces like I2C, I3C, CAN, SPI, and UART. Additional features include a low-power timer and a USB 2.0 full-speed Device/Host controller.
To implement USB HID keyboard functionality, the TinyUSB library is employed. TinyUSB is a lightweight, open-source USB stack designed for embedded systems, supporting cross-platform USB Host and Device protocols. USB HID keyboards operate under the USB Human Interface Device (HID) class protocol, which defines how human-interactive devices communicate with a host via control and interrupt pipes. The control pipe handles USB descriptors and class requests, while the interrupt pipe transmits input data from the device to the host.
Using TinyUSB simplifies development by abstracting the complexity of the USB protocol. The MindSDK online platform (https://mindsdk.mindmotion.com.cn) provides pre-ported examples for the MM32F5330. To set up the project, select Windows as the development platform, MDK as the cross-compilation tool, and the mini-f5330 evaluation board, then build the project. Choose "Download Standalone Examplee Project" and select the tinyusb -> tud_hid_keyboard example to download the mini-f5330_tud_hid_keyboard_mdk.zip file.
Open the project in Keil and configure the tusb_config.h file to enable HID functionality:
//------------- CLASS -------------//
#define CFG_TUD_HID 1
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0
The main.c file initializes the system and runs the TinyUSB tasks:
int main(void)
{
BOARD_Init();
tusb_init();
while (1)
{
hid_task();
tud_task();
}
}
Key functions include tusb_init() to initialize the TinyUSB stack and tud_task() to handle device tasks. The hid_task() function manages HID device operations, modified to continuously send the 'a' key without button input checks:
void hid_task(void)
{
if (tud_suspended())
{
tud_remote_wakeup();
}
else
{
send_hid_report(REPORT_ID_KEYBOARD, 1);
}
}
The send_hid_report() function transmits HID reports:
static void send_hid_report(uint8_t report_id, uint32_t btn)
{
if (!tud_hid_ready()) return;
switch(report_id)
{
case REPORT_ID_KEYBOARD:
{
static bool key_pressed = false;
if (btn)
{
uint8_t keycodes[6] = {0};
keycodes[0] = HID_KEY_A;
tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycodes);
key_pressed = true;
}
else
{
if (key_pressed) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
key_pressed = false;
}
}
break;
default:
break;
}
}
In tud_hid_keyboard_report(), the report_id parameter is set to REPORT_ID_KEYBOARD for keyboard functionality. The modifier parameter is a byte representing control keys (e.g., Left Control, Shift), and keycode[6] can transmit up to six characters simultaneously; this example assigns only keycode[0] = HID_KEY_A to output the 'a' character.
After compiling, flash the program to the board. Upon connection to a host, the device enumerates as a USB HID keyboard and repeatedly sends the 'a' key press.