吾輩はちよパパである。 記事はまだ無い。
QMK Firmware
keyboard.json
{
"manufacturer": "tiyochan",
"keyboard_name": "tiyochan/tiyokeys",
"maintainer": "tiyochan",
"bootloader": "rp2040",
"diode_direction": "COL2ROW",
"features": {
"bootmagic": false,
"extrakey": false,
"mousekey": false,
"nkro": false,
"oled": false,
"via": false
},
"matrix_pins": {
"direct": [
["GP0", "GP1", "GP2", "GP3", "GP4", "GP5", "GP6", "GP7",
"GP8", "GP9", "GP10", "GP11", "GP12", "GP13", "GP14", "GP15"]
]
},
"processor": "RP2040",
"url": "",
"usb": {
"device_version": "1.0.0",
"pid": "0x0000",
"vid": "0xFEED"
},
"layouts": {
"LAYOUT": {
"layout": [
{"matrix": [0, 0], "x": 0, "y": 0},
{"matrix": [0, 1], "x": 1, "y": 0},
{"matrix": [0, 2], "x": 2, "y": 0},
{"matrix": [0, 3], "x": 3, "y": 0},
{"matrix": [0, 4], "x": 4, "y": 0},
{"matrix": [0, 5], "x": 5, "y": 0},
{"matrix": [0, 6], "x": 6, "y": 0},
{"matrix": [0, 7], "x": 7, "y": 0},
{"matrix": [0, 8], "x": 8, "y": 0},
{"matrix": [0, 9], "x": 9, "y": 0},
{"matrix": [0, 10], "x": 10, "y": 0},
{"matrix": [0, 11], "x": 11, "y": 0},
{"matrix": [0, 12], "x": 12, "y": 0},
{"matrix": [0, 13], "x": 12, "y": 0},
{"matrix": [0, 14], "x": 12, "y": 0},
{"matrix": [0, 15], "x": 13, "y": 0}
]
}
}
}
keymap.c
// Copyright 2023 QMK // SPDX-License-Identifier: GPL-2.0-or-later #include QMK_KEYBOARD_H const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { [0] = LAYOUT( KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G, KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O, KC_P ) };
PicoSDK キーボード編
main.c
/* * The MIT License (MIT) * * Copyright (c) 2019 Ha Thach (tinyusb.org) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include "bsp/board_api.h" #include "tusb.h" #include "usb_descriptors.h" #include "pico/stdlib.h" //#define CAPSLOCK_LED_CONTROL //CAPSLOCKの状態でLEDを点灯させるなら有効にする //#define TIMER_TEST //LEDの点滅時間でタイマーの動作が正しいかどうかを判別しよう //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTYPES //--------------------------------------------------------------------+ /* Blink pattern * - 250 ms : device not mounted * - 1000 ms : device mounted * - 2500 ms : device is suspended */ enum { BLINK_NOT_MOUNTED = 250, BLINK_MOUNTED = 1000, BLINK_SUSPENDED = 2500, }; #define MAX_KEYSWITCH (21) struct PinInfo { uint8_t byPinNo; uint8_t byKeyCode; }; typedef struct PinInfo PinInfo; const PinInfo audtPinInfo[MAX_KEYSWITCH] = { { 0, HID_KEY_A }, { 1, HID_KEY_B }, { 2, HID_KEY_C }, { 3, HID_KEY_D }, { 4, HID_KEY_E }, { 5, HID_KEY_F }, { 6, HID_KEY_G }, { 7, HID_KEY_H }, { 8, HID_KEY_I }, { 9, HID_KEY_J }, { 10, HID_KEY_K }, { 11, HID_KEY_L }, { 12, HID_KEY_M }, { 13, HID_KEY_N }, { 14, HID_KEY_O }, { 15, HID_KEY_P }, { 16, HID_KEY_Q }, { 17, HID_KEY_R }, { 18, HID_KEY_S }, { 19, HID_KEY_T }, { 20, HID_KEY_U } }; static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; void led_blinking_task(void); void hid_task(void); bool board_keypush_check( uint8_t *keycode ) { const uint8_t zerokeycode[7] = {0,0,0,0,0,0,0}; uint8_t nowkeycode[7] = {0,0,0,0,0,0,0}; uint8_t index = 1; for( int i = 0 ; i < MAX_KEYSWITCH ; i++ ) { if( gpio_get( audtPinInfo[i].byPinNo ) == false ) { switch( audtPinInfo[i].byKeyCode ) { case HID_KEY_CONTROL_LEFT: //0xE0 case HID_KEY_SHIFT_LEFT: //0xE1 case HID_KEY_ALT_LEFT: //0xE2 case HID_KEY_GUI_LEFT: //0xE3,Windowsキーのこと case HID_KEY_CONTROL_RIGHT://0xE4 case HID_KEY_SHIFT_RIGHT: //0xE5 case HID_KEY_ALT_RIGHT: //0xE6 case HID_KEY_GUI_RIGHT: //0xE7,Menuキーのこと //HID_KEY_CONTROL_LEFT~HID_KEY_GUI_RIGHTの場合、キーコードの送信ではなくmodifireでの送信になります。 nowkeycode[0] = (uint8_t)(nowkeycode[0] | (0x01 << (audtPinInfo[i].byKeyCode - 0xE0))); break; default: //それ以外の場合、キーコードを格納します。 nowkeycode[index] = audtPinInfo[i].byKeyCode; break; } index++; } if( index >= 7 ) { //キー配列は6個しかありませんので、6個の押されているキーが見つかった場合はそこまでで打ち切りです。 break; } } if( keycode != NULL ) { //NULLでなかったら現在のキー状態をコピー。※引数にNULLを許可しているということ。NULLの場合コピーしない。 memcpy( keycode , nowkeycode , sizeof(nowkeycode) ); } if( memcmp( nowkeycode , zerokeycode , sizeof(nowkeycode) ) != 0 ) { //何らかのキーが押されていたらtrueを返す。 return true; } else { //何のキーも押されてなかったらfalseを返す。 return false; } } bool hid_task_timer_callback( repeating_timer_t *t ) { (void) t; hid_task(); return true; } /*------------- MAIN -------------*/ int main(void) { repeating_timer_t timer; board_init(); // init device stack on configured roothub port tusb_rhport_init_t dev_init = { .role = TUSB_ROLE_DEVICE, .speed = TUSB_SPEED_AUTO }; tusb_init(BOARD_TUD_RHPORT, &dev_init); if (board_init_after_tusb) { board_init_after_tusb(); } for( int i = 0 ; i < MAX_KEYSWITCH ; i++ ) { if( audtPinInfo[i].byPinNo < 30 ) { gpio_init( audtPinInfo[i].byPinNo ); gpio_set_dir( audtPinInfo[i].byPinNo , GPIO_IN ); gpio_pull_up( audtPinInfo[i].byPinNo ); } } add_repeating_timer_ms ( -10 , hid_task_timer_callback, NULL, &timer ); //10msに1回(正の数10の場合、「タスク終了」から10msで同じ関数が呼ばれる。負の数の場合「タスク開始」から10msで同じ関数が呼ばれる。正の数の場合は「指定時間+関数の処理時間」、負数の場合は「指定時間」毎に関数が呼ばれる。) while (1) { tud_task(); // tinyusb device task // led_blinking_task(); // hid_task(); //10ms毎に実行するタイマーで呼び出すように変更。 } } //--------------------------------------------------------------------+ // Device callbacks //--------------------------------------------------------------------+ // Invoked when device is mounted void tud_mount_cb(void) { blink_interval_ms = BLINK_MOUNTED; } // Invoked when device is unmounted void tud_umount_cb(void) { blink_interval_ms = BLINK_NOT_MOUNTED; } // Invoked when usb bus is suspended // remote_wakeup_en : if host allow us to perform remote wakeup // Within 7ms, device must draw an average of current less than 2.5 mA from bus void tud_suspend_cb(bool remote_wakeup_en) { (void) remote_wakeup_en; blink_interval_ms = BLINK_SUSPENDED; } // Invoked when usb bus is resumed void tud_resume_cb(void) { blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED; } //--------------------------------------------------------------------+ // USB HID //--------------------------------------------------------------------+ static void send_hid_report(uint8_t report_id, uint8_t keycode[]) { static uint8_t oldkeycode[7] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 };//前回送信したキー状態 ※oldkeycode[0]は修飾キー状態 // skip if hid is not ready yet if ( !tud_hid_ready() ) return; switch(report_id) { case REPORT_ID_KEYBOARD: { if( keycode == NULL ) { return; } if( memcmp( keycode , oldkeycode , sizeof(oldkeycode) ) != 0 ) { //キーの状態が前回と異なる場合、キーの状態を送信する。 if( tud_hid_keyboard_report(REPORT_ID_KEYBOARD, keycode[0], &(keycode[1])) != false ) { //送信(の登録)に成功した場合、古い値を保存しておく。これが前回送信した修飾キー/キー状態になります。 memcpy( oldkeycode , keycode , sizeof(oldkeycode) ); } } //サンプルコードではキーが押されていないときに // tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL); //の処理をしていましたが、 // tud_hid_keyboard_report(REPORT_ID_KEYBOARD, uint8_t modifier = 0, uint8_t keycode[6] = {0,0,0,0,0,0} ); //でも問題ないはずです。何でNULL指定があるんでしょう? } break; default: break; } } // Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..) // tud_hid_report_complete_cb() is used to send the next report after previous one is complete void hid_task(void) { //10ミリ秒に1回タイマーで呼ばれる関数化をしたので、時間計測関連の処理は削除。 // Poll every 10ms // const uint32_t interval_ms = 10; // static uint32_t start_ms = 0; uint8_t keycode[7] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 };//現在のキー状態 ※keycode[0]は修飾キー状態、keycode[1]~keycode[6]が押されているキーコード。 bool result; // if ( board_millis() - start_ms < interval_ms) return; // not enough time // start_ms += interval_ms; #ifdef TIMER_TEST //10msに1回呼ばれるなら1秒で点灯、その後1秒で消灯を繰り返すはず。ちゃんと呼ばれているかの確認のためのコード。 static int counter; counter = (counter + 1) % 200;//0~199の値を繰り返す。 if( counter < 100 ) { board_led_write(true); } else { board_led_write(false); } #endif result = board_keypush_check(keycode); //何らかのキーボードのキーが押されていればtrue、キーが押されてなければfalseを返す。 // Remote wakeup if ( tud_suspended() && ( result != false ) ) { //suspended状態、かつ、何らかのキーが押されている // Wake up host if we are in suspend mode // and REMOTE_WAKEUP feature is enabled by host tud_remote_wakeup(); }else { // Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb() send_hid_report(REPORT_ID_KEYBOARD, keycode); //元のサンプルコードの形を生かしただけ。REPORT_ID_KEYBOARDしか処理しないので、この関数を呼ぶことにあまり意味がない。引数btnは変えてます。 } } //リポートの送信を終えると次のリポートの送信をする関数だったが、REPORT_ID_KEYBOARDしかないので、ここは空処理になっています。 // Invoked when sent REPORT successfully to host // Application can use this to send the next report // Note: For composite reports, report[0] is report ID void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint16_t len) { (void) instance; (void) report[0]; (void) len; } // Invoked when received GET_REPORT control request // Application must fill buffer report's content and return its length. // Return zero will cause the stack to STALL request uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) { // TODO not Implemented (void) instance; (void) report_id; (void) report_type; (void) buffer; (void) reqlen; return 0; } // Invoked when received SET_REPORT control request or // received data on OUT endpoint ( Report ID = 0, Type = 0 ) void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) { (void) instance; #ifndef CAPSLOCK_LED_CONTROL (void) report_id; (void) report_type; (void) buffer; (void) bufsize; #else if (report_type == HID_REPORT_TYPE_OUTPUT) { // Set keyboard LED e.g Capslock, Numlock etc... if (report_id == REPORT_ID_KEYBOARD) { // bufsize should be (at least) 1 if ( bufsize < 1 ) return; uint8_t const kbd_leds = buffer[0]; if (kbd_leds & KEYBOARD_LED_CAPSLOCK) { // Capslock On: disable blink, turn led on blink_interval_ms = 0; board_led_write(true); }else { // Caplocks Off: back to normal blink board_led_write(false); blink_interval_ms = BLINK_MOUNTED; } } } #endif } //ソースコードは残したが、どこからも呼び出されていない。 //--------------------------------------------------------------------+ // BLINKING TASK //--------------------------------------------------------------------+ void led_blinking_task(void) { static uint32_t start_ms = 0; static bool led_state = false; // blink is disabled if (!blink_interval_ms) return; // Blink every interval ms if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time start_ms += blink_interval_ms; board_led_write(led_state); led_state = 1 - led_state; // toggle }
usb_descriptors.c
/* * The MIT License (MIT) * * Copyright (c) 2019 Ha Thach (tinyusb.org) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ #include "bsp/board_api.h" #include "tusb.h" #include "usb_descriptors.h" /* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug. * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC. * * Auto ProductID layout's Bitmap: * [MSB] HID | MSC | CDC [LSB] */ #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) #define USB_VID 0xCafe #define USB_BCD 0x0200 //--------------------------------------------------------------------+ // Device Descriptors //--------------------------------------------------------------------+ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, .bcdUSB = USB_BCD, .bDeviceClass = 0x00, .bDeviceSubClass = 0x00, .bDeviceProtocol = 0x00, .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, .idVendor = USB_VID, .idProduct = USB_PID, .bcdDevice = 0x0100, .iManufacturer = 0x01, .iProduct = 0x02, .iSerialNumber = 0x03, .bNumConfigurations = 0x01 }; // Invoked when received GET DEVICE DESCRIPTOR // Application return pointer to descriptor uint8_t const * tud_descriptor_device_cb(void) { return (uint8_t const *) &desc_device; } //--------------------------------------------------------------------+ // HID Report Descriptor //--------------------------------------------------------------------+ uint8_t const desc_hid_report[] = { TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD )) // TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE )), // TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(REPORT_ID_CONSUMER_CONTROL )), // TUD_HID_REPORT_DESC_GAMEPAD ( HID_REPORT_ID(REPORT_ID_GAMEPAD )) }; // Invoked when received GET HID REPORT DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance) { (void) instance; return desc_hid_report; } //--------------------------------------------------------------------+ // Configuration Descriptor //--------------------------------------------------------------------+ enum { ITF_NUM_HID, ITF_NUM_TOTAL }; #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN) #define EPNUM_HID 0x81 uint8_t const desc_configuration[] = { // Config number, interface count, string index, total length, attribute, power in mA TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5) }; #if TUD_OPT_HIGH_SPEED // Per USB specs: high speed capable device must report device_qualifier and other_speed_configuration // other speed configuration uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN]; // device qualifier is mostly similar to device descriptor since we don't change configuration based on speed tusb_desc_device_qualifier_t const desc_device_qualifier = { .bLength = sizeof(tusb_desc_device_qualifier_t), .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER, .bcdUSB = USB_BCD, .bDeviceClass = 0x00, .bDeviceSubClass = 0x00, .bDeviceProtocol = 0x00, .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, .bNumConfigurations = 0x01, .bReserved = 0x00 }; // Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete. // device_qualifier descriptor describes information about a high-speed capable device that would // change if the device were operating at the other speed. If not highspeed capable stall this request. uint8_t const* tud_descriptor_device_qualifier_cb(void) { return (uint8_t const*) &desc_device_qualifier; } // Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete // Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index) { (void) index; // for multiple configurations // other speed config is basically configuration with type = OHER_SPEED_CONFIG memcpy(desc_other_speed_config, desc_configuration, CONFIG_TOTAL_LEN); desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG; // this example use the same configuration for both high and full speed mode return desc_other_speed_config; } #endif // highspeed // Invoked when received GET CONFIGURATION DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete uint8_t const * tud_descriptor_configuration_cb(uint8_t index) { (void) index; // for multiple configurations // This example use the same configuration for both high and full speed mode return desc_configuration; } //--------------------------------------------------------------------+ // String Descriptors //--------------------------------------------------------------------+ // String Descriptor Index enum { STRID_LANGID = 0, STRID_MANUFACTURER, STRID_PRODUCT, STRID_SERIAL, }; // array of pointer to string descriptors char const *string_desc_arr[] = { (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) "TiyoUSB", // 1: Manufacturer "TiyoUSB Device", // 2: Product NULL, // 3: Serials will use unique ID if possible }; static uint16_t _desc_str[32 + 1]; // Invoked when received GET STRING DESCRIPTOR request // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { (void) langid; size_t chr_count; switch ( index ) { case STRID_LANGID: memcpy(&_desc_str[1], string_desc_arr[0], 2); chr_count = 1; break; case STRID_SERIAL: chr_count = board_usb_get_serial(_desc_str + 1, 32); break; default: // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors. // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL; const char *str = string_desc_arr[index]; // Cap at max char chr_count = strlen(str); size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type if ( chr_count > max_count ) chr_count = max_count; // Convert ASCII string into UTF-16 for ( size_t i = 0; i < chr_count; i++ ) { _desc_str[1 + i] = str[i]; } break; } // first byte is length (including header), second byte is string type _desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2)); return _desc_str; }
tusb_config.h
/* * The MIT License (MIT) * * Copyright (c) 2019 Ha Thach (tinyusb.org) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ #include "bsp/board_api.h" #include "tusb.h" #include "usb_descriptors.h" /* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug. * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC. * * Auto ProductID layout's Bitmap: * [MSB] HID | MSC | CDC [LSB] */ #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) #define USB_VID 0xCafe #define USB_BCD 0x0200 //--------------------------------------------------------------------+ // Device Descriptors //--------------------------------------------------------------------+ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, .bcdUSB = USB_BCD, .bDeviceClass = 0x00, .bDeviceSubClass = 0x00, .bDeviceProtocol = 0x00, .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, .idVendor = USB_VID, .idProduct = USB_PID, .bcdDevice = 0x0100, .iManufacturer = 0x01, .iProduct = 0x02, .iSerialNumber = 0x03, .bNumConfigurations = 0x01 }; // Invoked when received GET DEVICE DESCRIPTOR // Application return pointer to descriptor uint8_t const * tud_descriptor_device_cb(void) { return (uint8_t const *) &desc_device; } //--------------------------------------------------------------------+ // HID Report Descriptor //--------------------------------------------------------------------+ uint8_t const desc_hid_report[] = { TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD )) // TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE )), // TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(REPORT_ID_CONSUMER_CONTROL )), // TUD_HID_REPORT_DESC_GAMEPAD ( HID_REPORT_ID(REPORT_ID_GAMEPAD )) }; // Invoked when received GET HID REPORT DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance) { (void) instance; return desc_hid_report; } //--------------------------------------------------------------------+ // Configuration Descriptor //--------------------------------------------------------------------+ enum { ITF_NUM_HID, ITF_NUM_TOTAL }; #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN) #define EPNUM_HID 0x81 uint8_t const desc_configuration[] = { // Config number, interface count, string index, total length, attribute, power in mA TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5) }; #if TUD_OPT_HIGH_SPEED // Per USB specs: high speed capable device must report device_qualifier and other_speed_configuration // other speed configuration uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN]; // device qualifier is mostly similar to device descriptor since we don't change configuration based on speed tusb_desc_device_qualifier_t const desc_device_qualifier = { .bLength = sizeof(tusb_desc_device_qualifier_t), .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER, .bcdUSB = USB_BCD, .bDeviceClass = 0x00, .bDeviceSubClass = 0x00, .bDeviceProtocol = 0x00, .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, .bNumConfigurations = 0x01, .bReserved = 0x00 }; // Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete. // device_qualifier descriptor describes information about a high-speed capable device that would // change if the device were operating at the other speed. If not highspeed capable stall this request. uint8_t const* tud_descriptor_device_qualifier_cb(void) { return (uint8_t const*) &desc_device_qualifier; } // Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete // Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index) { (void) index; // for multiple configurations // other speed config is basically configuration with type = OHER_SPEED_CONFIG memcpy(desc_other_speed_config, desc_configuration, CONFIG_TOTAL_LEN); desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG; // this example use the same configuration for both high and full speed mode return desc_other_speed_config; } #endif // highspeed // Invoked when received GET CONFIGURATION DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete uint8_t const * tud_descriptor_configuration_cb(uint8_t index) { (void) index; // for multiple configurations // This example use the same configuration for both high and full speed mode return desc_configuration; } //--------------------------------------------------------------------+ // String Descriptors //--------------------------------------------------------------------+ // String Descriptor Index enum { STRID_LANGID = 0, STRID_MANUFACTURER, STRID_PRODUCT, STRID_SERIAL, }; // array of pointer to string descriptors char const *string_desc_arr[] = { (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) "TiyoUSB", // 1: Manufacturer "TiyoUSB Device", // 2: Product NULL, // 3: Serials will use unique ID if possible }; static uint16_t _desc_str[32 + 1]; // Invoked when received GET STRING DESCRIPTOR request // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { (void) langid; size_t chr_count; switch ( index ) { case STRID_LANGID: memcpy(&_desc_str[1], string_desc_arr[0], 2); chr_count = 1; break; case STRID_SERIAL: chr_count = board_usb_get_serial(_desc_str + 1, 32); break; default: // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors. // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL; const char *str = string_desc_arr[index]; // Cap at max char chr_count = strlen(str); size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type if ( chr_count > max_count ) chr_count = max_count; // Convert ASCII string into UTF-16 for ( size_t i = 0; i < chr_count; i++ ) { _desc_str[1 + i] = str[i]; } break; } // first byte is length (including header), second byte is string type _desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2)); return _desc_str; }
usb_descriptors.h
/* * The MIT License (MIT) * * Copyright (c) 2019 Ha Thach (tinyusb.org) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #ifndef USB_DESCRIPTORS_H_ #define USB_DESCRIPTORS_H_ enum { REPORT_ID_KEYBOARD = 1, // REPORT_ID_MOUSE, // REPORT_ID_CONSUMER_CONTROL, // REPORT_ID_GAMEPAD, REPORT_ID_COUNT }; #endif /* USB_DESCRIPTORS_H_ */
マークダウン表記での記事って初めて書いたわー。はてな記法だとソースコードの一部が「注釈」扱いになってしまったのでな。
じゃ、そういうことで。