This commit is contained in:
hathach 2019-05-02 13:27:00 +07:00
parent 1c0ec61aa1
commit 353d415d3f
8 changed files with 492 additions and 62 deletions

View File

@ -162,13 +162,13 @@ uint16_t const * const string_desc_arr [] =
// tud_desc_set is required by tinyusb stack
tud_desc_set_t tud_desc_set =
{
.device = &desc_device,
.config = desc_configuration,
.device = &desc_device,
.config = desc_configuration,
.string_arr = (uint8_t const **) string_desc_arr,
.string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
.string_arr = (uint8_t const **) string_desc_arr,
.string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
#if CFG_TUD_HID
.hid_report = desc_hid_report,
.hid_report = desc_hid_report,
#endif
};

View File

@ -0,0 +1,15 @@
include ../../../tools/top.mk
include ../../make.mk
INC += \
src \
$(TOP)/hw \
# Example source
EXAMPLE_SOURCE += $(wildcard src/*.c)
SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE))
# Board source
SRC_C += hw/bsp/$(BOARD)/board_$(BOARD).c
include ../../rules.mk

View File

@ -0,0 +1,211 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018, hathach (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.h"
#include "tusb.h"
//--------------------------------------------------------------------+
// 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,
};
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
void led_blinking_task(void);
void hid_task(void);
/*------------- MAIN -------------*/
int main(void)
{
board_init();
tusb_init();
while (1)
{
// tinyusb device task
tud_task();
led_blinking_task();
#if CFG_TUD_HID
hid_task();
#endif
}
return 0;
}
//--------------------------------------------------------------------+
// 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 = BLINK_MOUNTED;
}
//--------------------------------------------------------------------+
// USB HID
//--------------------------------------------------------------------+
#if CFG_TUD_HID
// Must match with ID declared by HID Report Descriptor, better to be in header file
enum
{
REPORT_ID_KEYBOARD = 1,
REPORT_ID_MOUSE
};
void hid_task(void)
{
// Poll every 10ms
const uint32_t interval_ms = 10;
static uint32_t start_ms = 0;
if ( board_millis() < start_ms + interval_ms) return; // not enough time
start_ms += interval_ms;
uint32_t const btn = board_button_read();
// Remote wakeup
if ( tud_suspended() && btn )
{
// Wake up host if we are in suspend mode
// and REMOTE_WAKEUP feature is enabled by host
tud_remote_wakeup();
}
/*------------- Mouse -------------*/
if ( tud_hid_ready() )
{
if ( btn )
{
int8_t const delta = 5;
// no button, right + down, no scroll pan
tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0);
// delay a bit before attempt to send keyboard report
board_delay(2);
}
}
/*------------- Keyboard -------------*/
if ( tud_hid_ready() )
{
// use to avoid send multiple consecutive zero report for keyboard
static bool has_key = false;
if ( btn )
{
uint8_t keycode[6] = { 0 };
keycode[0] = HID_KEY_A;
tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode);
has_key = true;
}else
{
// send empty key report if previously has key pressed
if (has_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
has_key = false;
}
}
}
uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
{
// TODO not Implemented
(void) report_id;
(void) report_type;
(void) buffer;
(void) reqlen;
return 0;
}
void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
{
// TODO not Implemented
(void) report_id;
(void) report_type;
(void) buffer;
(void) bufsize;
}
#endif
//--------------------------------------------------------------------+
// BLINKING TASK
//--------------------------------------------------------------------+
void led_blinking_task(void)
{
static uint32_t start_ms = 0;
static bool led_state = false;
// Blink every 1000 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
}

View File

@ -0,0 +1,88 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018, hathach (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 _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_
#ifdef __cplusplus
extern "C" {
#endif
//--------------------------------------------------------------------
// COMMON CONFIGURATION
//--------------------------------------------------------------------
// defined by compiler flags for flexibility
#ifndef CFG_TUSB_MCU
#error CFG_TUSB_MCU must be defined
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#endif
#define CFG_TUSB_OS OPT_OS_NONE
#define CFG_TUSB_DEBUG 2
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
* Tinyusb use follows macros to declare transferring memory so that they can be put
* into those specific section.
* e.g
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
*/
#ifndef CFG_TUSB_MEM_SECTION
#define CFG_TUSB_MEM_SECTION
#endif
#ifndef CFG_TUSB_MEM_ALIGN
#define CFG_TUSB_MEM_ALIGN ATTR_ALIGNED(4)
#endif
//--------------------------------------------------------------------
// DEVICE CONFIGURATION
//--------------------------------------------------------------------
#define CFG_TUD_ENDOINT0_SIZE 64
//------------- CLASS -------------//
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 0
#define CFG_TUD_HID 1
#define CFG_TUD_MIDI 0
#define CFG_TUD_CUSTOM_CLASS 0
//------------- HID -------------//
// Should be sufficient to hold ID (if any) + Data
#define CFG_TUD_HID_BUFSIZE 64
#ifdef __cplusplus
}
#endif
#endif /* _TUSB_CONFIG_H_ */

View File

@ -0,0 +1,116 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018, hathach (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 "tusb.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) )
//------------- Device Descriptors -------------//
tusb_desc_device_t const desc_device =
{
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = 0x0200,
.bDeviceClass = 0x00,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.idVendor = 0xCafe,
.idProduct = USB_PID,
.bcdDevice = 0x0100,
.iManufacturer = 0x01,
.iProduct = 0x02,
.iSerialNumber = 0x03,
.bNumConfigurations = 0x01
};
//------------- 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), )
};
//------------- Configuration Descriptor -------------//
enum
{
ITF_NUM_HID,
ITF_NUM_TOTAL
};
enum
{
CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN
};
// Use Endpoint 2 instead of 1 due to NXP MCU
// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
// 0 control, 1 In, 2 Bulk, 3 Iso, 4 In etc ...
#define EPNUM_HID 0x01
uint8_t const desc_configuration[] =
{
// Config: self-powered with remote wakeup support, max power up to 100 mA
TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_KEYBOARD, sizeof(desc_hid_report), 0x84, 16, 10)
};
//------------- String Descriptors -------------//
// array of pointer to string descriptors
uint16_t const * const string_desc_arr [] =
{
// 0: is supported language = English
TUD_DESC_STRCONV(0x0409),
// 1: Manufacturer
TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'),
// 2: Product
TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'),
// 3: Serials, should use chip ID
TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'),
};
// tud_desc_set is required by tinyusb stack
tud_desc_set_t tud_desc_set =
{
.device = &desc_device,
.config = desc_configuration,
.string_arr = (uint8_t const **) string_desc_arr,
.string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
.hid_report = desc_hid_report,
};

View File

@ -43,7 +43,6 @@ tusb_desc_device_t const desc_device =
.bDeviceClass = 0x00,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.idVendor = 0xCafe,
@ -80,7 +79,7 @@ uint8_t const desc_configuration[] =
TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
#if CFG_TUD_MSC
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC, 0x80 | EPNUM_MSC, 64), // highspeed 512
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 0, EPNUM_MSC, 0x80 | EPNUM_MSC, 64), // highspeed 512
#endif
};

View File

@ -43,6 +43,20 @@
/** \defgroup ClassDriver_HID_Common Common Definitions
* @{ */
/// USB HID Descriptor
typedef struct ATTR_PACKED
{
uint8_t bLength; /**< Numeric expression that is the total size of the HID descriptor */
uint8_t bDescriptorType; /**< Constant name specifying type of HID descriptor. */
uint16_t bcdHID; /**< Numeric expression identifying the HID Class Specification release */
uint8_t bCountryCode; /**< Numeric expression identifying country code of the localized hardware. */
uint8_t bNumDescriptors; /**< Numeric expression specifying the number of class descriptors */
uint8_t bReportType; /**< Type of HID class report. */
uint16_t wReportLength; /**< the total size of the Report descriptor. */
} tusb_hid_descriptor_hid_t;
/// HID Subclass
typedef enum
{
@ -69,9 +83,10 @@ typedef enum
/// HID Request Report Type
typedef enum
{
HID_REPORT_TYPE_INPUT = 1, ///< Input
HID_REPORT_TYPE_OUTPUT, ///< Output
HID_REPORT_TYPE_FEATURE ///< Feature
HID_REPORT_TYPE_INVALID = 0,
HID_REPORT_TYPE_INPUT, ///< Input
HID_REPORT_TYPE_OUTPUT, ///< Output
HID_REPORT_TYPE_FEATURE ///< Feature
}hid_report_type_t;
/// HID Class Specific Control Request
@ -85,59 +100,45 @@ typedef enum
HID_REQ_CONTROL_SET_PROTOCOL = 0x0b ///< Set Protocol
}hid_request_type_t;
/// USB HID Descriptor
typedef struct ATTR_PACKED
{
uint8_t bLength; /**< Numeric expression that is the total size of the HID descriptor */
uint8_t bDescriptorType; /**< Constant name specifying type of HID descriptor. */
uint16_t bcdHID; /**< Numeric expression identifying the HID Class Specification release */
uint8_t bCountryCode; /**< Numeric expression identifying country code of the localized hardware. */
uint8_t bNumDescriptors; /**< Numeric expression specifying the number of class descriptors */
uint8_t bReportType; /**< Type of HID class report. */
uint16_t wReportLength; /**< the total size of the Report descriptor. */
} tusb_hid_descriptor_hid_t;
/// HID Country Code
typedef enum
{
HID_Local_NotSupported = 0 , ///< NotSupported
HID_Local_Arabic , ///< Arabic
HID_Local_Belgian , ///< Belgian
HID_Local_Canadian_Bilingual , ///< Canadian_Bilingual
HID_Local_Canadian_French , ///< Canadian_French
HID_Local_Czech_Republic , ///< Czech_Republic
HID_Local_Danish , ///< Danish
HID_Local_Finnish , ///< Finnish
HID_Local_French , ///< French
HID_Local_German , ///< German
HID_Local_Greek , ///< Greek
HID_Local_Hebrew , ///< Hebrew
HID_Local_Hungary , ///< Hungary
HID_Local_International , ///< International
HID_Local_Italian , ///< Italian
HID_Local_Japan_Katakana , ///< Japan_Katakana
HID_Local_Korean , ///< Korean
HID_Local_Latin_American , ///< Latin_American
HID_Local_Netherlands_Dutch , ///< Netherlands/Dutch
HID_Local_Norwegian , ///< Norwegian
HID_Local_Persian_Farsi , ///< Persian (Farsi)
HID_Local_Poland , ///< Poland
HID_Local_Portuguese , ///< Portuguese
HID_Local_Russia , ///< Russia
HID_Local_Slovakia , ///< Slovakia
HID_Local_Spanish , ///< Spanish
HID_Local_Swedish , ///< Swedish
HID_Local_Swiss_French , ///< Swiss/French
HID_Local_Swiss_German , ///< Swiss/German
HID_Local_Switzerland , ///< Switzerland
HID_Local_Taiwan , ///< Taiwan
HID_Local_Turkish_Q , ///< Turkish-Q
HID_Local_UK , ///< UK
HID_Local_US , ///< US
HID_Local_Yugoslavia , ///< Yugoslavia
HID_Local_Turkish_F ///< Turkish-F
HID_LOCAL_NotSupported = 0 , ///< NotSupported
HID_LOCAL_Arabic , ///< Arabic
HID_LOCAL_Belgian , ///< Belgian
HID_LOCAL_Canadian_Bilingual , ///< Canadian_Bilingual
HID_LOCAL_Canadian_French , ///< Canadian_French
HID_LOCAL_Czech_Republic , ///< Czech_Republic
HID_LOCAL_Danish , ///< Danish
HID_LOCAL_Finnish , ///< Finnish
HID_LOCAL_French , ///< French
HID_LOCAL_German , ///< German
HID_LOCAL_Greek , ///< Greek
HID_LOCAL_Hebrew , ///< Hebrew
HID_LOCAL_Hungary , ///< Hungary
HID_LOCAL_International , ///< International
HID_LOCAL_Italian , ///< Italian
HID_LOCAL_Japan_Katakana , ///< Japan_Katakana
HID_LOCAL_Korean , ///< Korean
HID_LOCAL_Latin_American , ///< Latin_American
HID_LOCAL_Netherlands_Dutch , ///< Netherlands/Dutch
HID_LOCAL_Norwegian , ///< Norwegian
HID_LOCAL_Persian_Farsi , ///< Persian (Farsi)
HID_LOCAL_Poland , ///< Poland
HID_LOCAL_Portuguese , ///< Portuguese
HID_LOCAL_Russia , ///< Russia
HID_LOCAL_Slovakia , ///< Slovakia
HID_LOCAL_Spanish , ///< Spanish
HID_LOCAL_Swedish , ///< Swedish
HID_LOCAL_Swiss_French , ///< Swiss/French
HID_LOCAL_Swiss_German , ///< Swiss/German
HID_LOCAL_Switzerland , ///< Switzerland
HID_LOCAL_Taiwan , ///< Taiwan
HID_LOCAL_Turkish_Q , ///< Turkish-Q
HID_LOCAL_UK , ///< UK
HID_LOCAL_US , ///< US
HID_LOCAL_Yugoslavia , ///< Yugoslavia
HID_LOCAL_Turkish_F ///< Turkish-F
} hid_country_code_t;
/** @} */

View File

@ -166,7 +166,7 @@ const app_configuration_desc_t desc_configuration =
.bLength = sizeof(tusb_hid_descriptor_hid_t),
.bDescriptorType = HID_DESC_TYPE_HID,
.bcdHID = 0x0111,
.bCountryCode = HID_Local_NotSupported,
.bCountryCode = HID_LOCAL_NotSupported,
.bNumDescriptors = 1,
.bReportType = HID_DESC_TYPE_REPORT,
.wReportLength = sizeof(keyboard_report_descriptor)
@ -201,7 +201,7 @@ const app_configuration_desc_t desc_configuration =
.bLength = sizeof(tusb_hid_descriptor_hid_t),
.bDescriptorType = HID_DESC_TYPE_HID,
.bcdHID = 0x0111,
.bCountryCode = HID_Local_NotSupported,
.bCountryCode = HID_LOCAL_NotSupported,
.bNumDescriptors = 1,
.bReportType = HID_DESC_TYPE_REPORT,
.wReportLength = sizeof(mouse_report_descriptor)