From 71dc079032816c7c3b86485cc674ea1aeb3d5a84 Mon Sep 17 00:00:00 2001 From: Jonathan Struebel Date: Sat, 18 Jun 2016 20:02:22 -0700 Subject: [PATCH] Added USB HID driver to community HAL --- os/hal/hal.mk | 1 + os/hal/include/hal_community.h | 5 + os/hal/include/hal_usb_hid.h | 510 +++++++++++++++ os/hal/src/hal_usb_hid.c | 581 ++++++++++++++++++ .../FRDM-KL25Z/USB_HID/Client/Makefile | 23 + .../FRDM-KL25Z/USB_HID/Client/test-usb-hid.c | 180 ++++++ .../FRDM-KL25Z/USB_HID/Client/udev.rules | 2 + testhal/KINETIS/FRDM-KL25Z/USB_HID/Makefile | 216 +++++++ testhal/KINETIS/FRDM-KL25Z/USB_HID/chconf.h | 516 ++++++++++++++++ testhal/KINETIS/FRDM-KL25Z/USB_HID/halconf.h | 368 +++++++++++ .../FRDM-KL25Z/USB_HID/halconf_community.h | 142 +++++ testhal/KINETIS/FRDM-KL25Z/USB_HID/main.c | 91 +++ testhal/KINETIS/FRDM-KL25Z/USB_HID/mcuconf.h | 63 ++ testhal/KINETIS/FRDM-KL25Z/USB_HID/usbcfg.c | 418 +++++++++++++ testhal/KINETIS/FRDM-KL25Z/USB_HID/usbcfg.h | 45 ++ 15 files changed, 3161 insertions(+) create mode 100644 os/hal/include/hal_usb_hid.h create mode 100644 os/hal/src/hal_usb_hid.c create mode 100644 testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/Makefile create mode 100644 testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/test-usb-hid.c create mode 100644 testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/udev.rules create mode 100644 testhal/KINETIS/FRDM-KL25Z/USB_HID/Makefile create mode 100644 testhal/KINETIS/FRDM-KL25Z/USB_HID/chconf.h create mode 100644 testhal/KINETIS/FRDM-KL25Z/USB_HID/halconf.h create mode 100644 testhal/KINETIS/FRDM-KL25Z/USB_HID/halconf_community.h create mode 100644 testhal/KINETIS/FRDM-KL25Z/USB_HID/main.c create mode 100644 testhal/KINETIS/FRDM-KL25Z/USB_HID/mcuconf.h create mode 100644 testhal/KINETIS/FRDM-KL25Z/USB_HID/usbcfg.c create mode 100644 testhal/KINETIS/FRDM-KL25Z/USB_HID/usbcfg.h diff --git a/os/hal/hal.mk b/os/hal/hal.mk index 751b59a1..ce746200 100644 --- a/os/hal/hal.mk +++ b/os/hal/hal.mk @@ -18,5 +18,6 @@ HALSRC += ${CHIBIOS_CONTRIB}/os/hal/src/hal_community.c \ ${CHIBIOS_CONTRIB}/os/hal/src/hal_eeprom.c \ ${CHIBIOS_CONTRIB}/os/hal/src/hal_timcap.c \ ${CHIBIOS_CONTRIB}/os/hal/src/hal_qei.c \ + ${CHIBIOS_CONTRIB}/os/hal/src/hal_usb_hid.c HALINC += ${CHIBIOS_CONTRIB}/os/hal/include diff --git a/os/hal/include/hal_community.h b/os/hal/include/hal_community.h index 75b39169..1518c7e2 100644 --- a/os/hal/include/hal_community.h +++ b/os/hal/include/hal_community.h @@ -63,6 +63,10 @@ #define HAL_USE_USBH FALSE #endif +#if !defined(HAL_USE_USB_HID) +#define HAL_USE_USB_HID FALSE +#endif + /* Abstract interfaces.*/ /* Shared headers.*/ @@ -79,6 +83,7 @@ #include "hal_onewire.h" #include "hal_crc.h" #include "hal_eeprom.h" +#include "hal_usb_hid.h" /*===========================================================================*/ /* Driver constants. */ diff --git a/os/hal/include/hal_usb_hid.h b/os/hal/include/hal_usb_hid.h new file mode 100644 index 00000000..2a2d73ad --- /dev/null +++ b/os/hal/include/hal_usb_hid.h @@ -0,0 +1,510 @@ +/* + ChibiOS - Copyright (C) 2016 Jonathan Struebel + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file hal_usb_hid.h + * @brief USB HID macros and structures. + * + * @addtogroup USB_HID + * @{ + */ + +#ifndef HAL_USB_HID_H +#define HAL_USB_HID_H + +#if (HAL_USE_USB_HID == TRUE) || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/** + * @name HID specific messages. + * @{ + */ +#define HID_GET_REPORT 0x01U +#define HID_GET_IDLE 0x02U +#define HID_GET_PROTOCOL 0x03U +#define HID_SET_REPORT 0x09U +#define HID_SET_IDLE 0x0AU +#define HID_SET_PROTOCOL 0x0BU +/** @} */ + +/** + * @name HID classes + * @{ + */ +#define HID_INTERFACE_CLASS 0x03U +/** @} */ + +/** + * @name HID subclasses + * @{ + */ +#define HID_BOOT_INTERFACE 0x01U +/** @} */ + +/** + * @name HID descriptors + * @{ + */ +#define USB_DESCRIPTOR_HID 0x21U +#define HID_REPORT 0x22U +#define HID_PHYSICAL 0x23U +/** @} */ + +/** + * @name HID Report items + * @{ + */ +#define HID_REPORT_USAGE_PAGE 0x04 +#define HID_REPORT_USAGE 0x08 +#define HID_REPORT_LOGICAL_MINIMUM 0x14 +#define HID_REPORT_USAGE_MINIMUM 0x18 +#define HID_REPORT_LOGICAL_MAXIMUM 0x24 +#define HID_REPORT_USAGE_MAXIMUM 0x28 +#define HID_REPORT_REPORT_SIZE 0x74 +#define HID_REPORT_INPUT 0x80 +#define HID_REPORT_REPORT_COUNT 0x94 +#define HID_REPORT_COLLECTION 0xA0 +#define HID_REPORT_END_COLLECTION 0xC0 +/** @} */ + +/** + * @name HID Collection item definitions + * @{ + */ +#define HID_COLLECTION_PHYSICAL 0x00 +#define HID_COLLECTION_APPLICATION 0x01 +#define HID_COLLECTION_LOGICAL 0x02 +#define HID_COLLECTION_REPORT 0x03 +#define HID_COLLECTION_NAMED_ARRAY 0x04 +#define HID_COLLECTION_USAGE_SWITCH 0x05 +#define HID_COLLECTION_USAGE_MODIFIER 0x06 +/** @} */ + +/** + * @name HID Usage Page item definitions + * @{ + */ +#define HID_USAGE_PAGE_GENERIC_DESKTOP 0x01 +#define HID_USAGE_PAGE_SIMULATION 0x02 +#define HID_USAGE_PAGE_VR 0x03 +#define HID_USAGE_PAGE_SPORT 0x04 +#define HID_USAGE_PAGE_GAME 0x05 +#define HID_USAGE_PAGE_GENERIC_DEVICE 0x06 +#define HID_USAGE_PAGE_KEYBOARD_KEYPAD 0x07 +#define HID_USAGE_PAGE_LEDS 0x08 +#define HID_USAGE_PAGE_BUTTON 0x09 +#define HID_USAGE_PAGE_ORDINAL 0x0A +#define HID_USAGE_PAGE_TELEPHONY 0x0B +#define HID_USAGE_PAGE_CONSUMER 0x0C +#define HID_USAGE_PAGE_DIGITIZER 0x0D +#define HID_USAGE_PAGE_PID 0x0F +#define HID_USAGE_PAGE_UNICODE 0x10 +/** @} */ + +/** + * @name HID Usage item definitions + * @{ + */ +#define HID_USAGE_ALPHANUMERIC_DISPLAY 0x14 +#define HID_USAGE_MEDICAL_INSTRUMENTS 0x40 +#define HID_USAGE_MONITOR_PAGE1 0x80 +#define HID_USAGE_MONITOR_PAGE2 0x81 +#define HID_USAGE_MONITOR_PAGE3 0x82 +#define HID_USAGE_MONITOR_PAGE4 0x83 +#define HID_USAGE_POWER_PAGE1 0x84 +#define HID_USAGE_POWER_PAGE2 0x85 +#define HID_USAGE_POWER_PAGE3 0x86 +#define HID_USAGE_POWER_PAGE4 0x87 +#define HID_USAGE_BAR_CODE_SCANNER_PAGE 0x8C +#define HID_USAGE_SCALE_PAGE 0x8D +#define HID_USAGE_MSR_PAGE 0x8E +#define HID_USAGE_CAMERA_PAGE 0x90 +#define HID_USAGE_ARCADE_PAGE 0x91 + +#define HID_USAGE_POINTER 0x01 +#define HID_USAGE_MOUSE 0x02 +#define HID_USAGE_JOYSTICK 0x04 +#define HID_USAGE_GAMEPAD 0x05 +#define HID_USAGE_KEYBOARD 0x06 +#define HID_USAGE_KEYPAD 0x07 +#define HID_USAGE_MULTIAXIS_CONTROLLER 0x08 + +#define HID_USAGE_BUTTON1 0x01 +#define HID_USAGE_BUTTON2 0x02 +#define HID_USAGE_BUTTON3 0x03 +#define HID_USAGE_BUTTON4 0x04 +#define HID_USAGE_BUTTON5 0x05 +#define HID_USAGE_BUTTON6 0x06 +#define HID_USAGE_BUTTON7 0x07 +#define HID_USAGE_BUTTON8 0x08 + +#define HID_USAGE_X 0x30 +#define HID_USAGE_Y 0x31 +#define HID_USAGE_Z 0x32 +#define HID_USAGE_RX 0x33 +#define HID_USAGE_RY 0x34 +#define HID_USAGE_RZ 0x35 +#define HID_USAGE_VX 0x40 +#define HID_USAGE_VY 0x41 +#define HID_USAGE_VZ 0x42 +#define HID_USAGE_VBRX 0x43 +#define HID_USAGE_VBRY 0x44 +#define HID_USAGE_VBRZ 0x45 +#define HID_USAGE_VNO 0x46 +/** @} */ + +/** + * @name HID Input item definitions. + * @{ + */ +#define HID_INPUT_DATA_VAR_ABS 0x02 +#define HID_INPUT_CNST_VAR_ABS 0x03 +#define HID_INPUT_DATA_VAR_REL 0x06 +/** @} */ + +/** + * @name Helper macros for USB HID descriptors + * @{ + */ +/* + * @define HID Descriptor size. + */ +#define USB_DESC_HID_SIZE 9U + +/** + * @brief HID Descriptor helper macro. + * @note This macro can only be used with a single HID report descriptor + */ +#define USB_DESC_HID(bcdHID, bCountryCode, bNumDescriptors, \ + bDescriptorType, wDescriptorLength) \ + USB_DESC_BYTE(USB_DESC_HID_SIZE), \ + USB_DESC_BYTE(USB_DESCRIPTOR_HID), \ + USB_DESC_BCD(bcdHID), \ + USB_DESC_BYTE(bCountryCode), \ + USB_DESC_BYTE(bNumDescriptors), \ + USB_DESC_BYTE(bDescriptorType), \ + USB_DESC_WORD(wDescriptorLength) + +/** + * @brief HID Report Usage Page item helper macro (Single byte). + */ +#define HID_USAGE_PAGE_B(up) \ + USB_DESC_BYTE(HID_REPORT_USAGE_PAGE | 0x01), \ + USB_DESC_BYTE(up) + +/** + * @brief HID Report Usage Page item helper macro (Double byte). + */ +#define HID_USAGE_PAGE_W(up) \ + USB_DESC_BYTE(HID_REPORT_USAGE_PAGE | 0x02), \ + USB_DESC_WORD(up) + +/** + * @brief HID Report Usage item helper macro (Single byte). + */ +#define HID_USAGE_B(u) \ + USB_DESC_BYTE(HID_REPORT_USAGE | 0x01), \ + USB_DESC_BYTE(u) + +/** + * @brief HID Report Usage item helper macro (Double byte). + */ +#define HID_USAGE_W(u) \ + USB_DESC_BYTE(HID_REPORT_USAGE | 0x02), \ + USB_DESC_WORD(u) + +/** + * @brief HID Report Collection item helper macro (Single Byte). + */ +#define HID_COLLECTION_B(c) \ + USB_DESC_BYTE(HID_REPORT_COLLECTION | 0x01), \ + USB_DESC_BYTE(c) + +/** + * @brief HID Report Collection item helper macro (Double Byte). + */ +#define HID_COLLECTION_W(c) \ + USB_DESC_BYTE(HID_REPORT_COLLECTION | 0x02), \ + USB_DESC_WORD(c) + +/** + * @brief HID Report End Collection item helper macro. + */ +#define HID_END_COLLECTION \ + USB_DESC_BYTE(HID_REPORT_END_COLLECTION) + +/** + * @brief HID Report Usage Minimum item helper macro (Single byte). + */ +#define HID_USAGE_MINIMUM_B(x) \ + USB_DESC_BYTE(HID_REPORT_USAGE_MINIMUM | 0x01), \ + USB_DESC_BYTE(x) + +/** + * @brief HID Report Usage Minimum item helper macro (Double byte). + */ +#define HID_USAGE_MINIMUM_W(x) \ + USB_DESC_BYTE(HID_REPORT_USAGE_MINIMUM | 0x02), \ + USB_DESC_WORD(x) + +/** + * @brief HID Report Usage Maximum item helper macro (Single byte). + */ +#define HID_USAGE_MAXIMUM_B(x) \ + USB_DESC_BYTE(HID_REPORT_USAGE_MAXIMUM | 0x01), \ + USB_DESC_BYTE(x) + +/** + * @brief HID Report Usage Maximum item helper macro (Double byte). + */ +#define HID_USAGE_MAXIMUM_W(x) \ + USB_DESC_BYTE(HID_REPORT_USAGE_MAXIMUM | 0x02), \ + USB_DESC_WORD(x) + +/** + * @brief HID Report Logical Minimum item helper macro (Single byte). + */ +#define HID_LOGICAL_MINIMUM_B(x) \ + USB_DESC_BYTE(HID_REPORT_LOGICAL_MINIMUM | 0x01), \ + USB_DESC_BYTE(x) + +/** + * @brief HID Report Logical Minimum item helper macro (Double byte). + */ +#define HID_LOGICAL_MINIMUM_W(x) \ + USB_DESC_BYTE(HID_REPORT_LOGICAL_MINIMUM | 0x02), \ + USB_DESC_WORD(x) + +/** + * @brief HID Report Logical Maximum item helper macro (Single byte). + */ +#define HID_LOGICAL_MAXIMUM_B(x) \ + USB_DESC_BYTE(HID_REPORT_LOGICAL_MAXIMUM | 0x01), \ + USB_DESC_BYTE(x) + +/** + * @brief HID Report Logical Maximum item helper macro (Double byte). + */ +#define HID_LOGICAL_MAXIMUM_W(x) \ + USB_DESC_BYTE(HID_REPORT_LOGICAL_MAXIMUM | 0x02), \ + USB_DESC_WORD(x) + +/** + * @brief HID Report Count item helper macro (Single byte). + */ +#define HID_REPORT_COUNT_B(x) \ + USB_DESC_BYTE(HID_REPORT_REPORT_COUNT | 0x01), \ + USB_DESC_BYTE(x) + +/** + * @brief HID Report Count item helper macro (Double byte). + */ +#define HID_REPORT_COUNT_W(x) \ + USB_DESC_BYTE(HID_REPORT_REPORT_COUNT | 0x02), \ + USB_DESC_WORD(x) + +/** + * @brief HID Report Size item helper macro (Single byte). + */ +#define HID_REPORT_SIZE_B(x) \ + USB_DESC_BYTE(HID_REPORT_REPORT_SIZE | 0x01), \ + USB_DESC_BYTE(x) + +/** + * @brief HID Report Size item helper macro (Double byte). + */ +#define HID_REPORT_SIZE_W(x) \ + USB_DESC_BYTE(HID_REPORT_REPORT_SIZE | 0x02), \ + USB_DESC_WORD(x) + +/** + * @brief HID Report Input item helper macro (Single byte). + */ +#define HID_INPUT_B(x) \ + USB_DESC_BYTE(HID_REPORT_INPUT | 0x01), \ + USB_DESC_BYTE(x) + +/** + * @brief HID Report Input item helper macro (Double byte). + */ +#define HID_INPUT_W(x) \ + USB_DESC_BYTE(HID_REPORT_INPUT | 0x02), \ + USB_DESC_WORD(x) +/** @} */ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name USB HID configuration options + * @{ + */ +/** + * @brief USB HID buffers size. + * @details Configuration parameter, the buffer size must be a multiple of + * the USB data endpoint maximum packet size. + * @note The default is 256 bytes for both the transmission and receive + * buffers. + */ +#if !defined(USB_HID_BUFFERS_SIZE) || defined(__DOXYGEN__) +#define USB_HID_BUFFERS_SIZE 256 +#endif + +/** + * @brief USB HID number of buffers. + * @note The default is 2 buffers. + */ +#if !defined(USB_HID_BUFFERS_NUMBER) || defined(__DOXYGEN__) +#define USB_HID_BUFFERS_NUMBER 2 +#endif +/** @} */ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +#if HAL_USE_USB == FALSE +#error "USB HID Driver requires HAL_USE_USB" +#endif + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @brief Driver state machine possible states. + */ +typedef enum { + HID_UNINIT = 0, /**< Not initialized. */ + HID_STOP = 1, /**< Stopped. */ + HID_READY = 2 /**< Ready. */ +} hidstate_t; + +/** + * @brief Structure representing a USB HID driver. + */ +typedef struct USBHIDDriver USBHIDDriver; + +/** + * @brief USB HID Driver configuration structure. + * @details An instance of this structure must be passed to @p hidStart() + * in order to configure and start the driver operations. + */ +typedef struct { + /** + * @brief USB driver to use. + */ + USBDriver *usbp; + /** + * @brief Interrupt IN endpoint used for outgoing data transfer. + */ + usbep_t int_in; + /** + * @brief Interrupt OUT endpoint used for incoming data transfer. + */ + usbep_t int_out; +} USBHIDConfig; + +/** + * @brief @p USBHIDDriver specific data. + */ +#define _usb_hid_driver_data \ + _base_asynchronous_channel_data \ + /* Driver state.*/ \ + hidstate_t state; \ + /* Input buffers queue.*/ \ + input_buffers_queue_t ibqueue; \ + /* Output queue.*/ \ + output_buffers_queue_t obqueue; \ + /* Input buffer.*/ \ + uint8_t ib[BQ_BUFFER_SIZE(USB_HID_BUFFERS_NUMBER, \ + USB_HID_BUFFERS_SIZE)]; \ + /* Output buffer.*/ \ + uint8_t ob[BQ_BUFFER_SIZE(USB_HID_BUFFERS_NUMBER, \ + USB_HID_BUFFERS_SIZE)]; \ + /* End of the mandatory fields.*/ \ + /* Current configuration data.*/ \ + const USBHIDConfig *config; + +/** + * @brief @p USBHIDDriver specific methods. + */ +#define _usb_hid_driver_methods \ + _base_asynchronous_channel_methods \ + /* Buffer flush method.*/ \ + void (*flush)(void *instance); + +/** + * @extends BaseAsynchronousChannelVMT + * + * @brief @p USBHIDDriver virtual methods table. + */ +struct USBHIDDriverVMT { + _usb_hid_driver_methods +}; + +/** + * @extends BaseAsynchronousChannel + * + * @brief Full duplex USB HID driver class. + * @details This class extends @p BaseAsynchronousChannel by adding physical + * I/O queues. + */ +struct USBHIDDriver { + /** @brief Virtual Methods Table.*/ + const struct USBHIDDriverVMT *vmt; + _usb_hid_driver_data +}; + +#define USB_DRIVER_EXT_FIELDS \ + USBHIDDriver hid + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ +#ifdef __cplusplus +extern "C" { +#endif + void hidInit(void); + void hidObjectInit(USBHIDDriver *uhdp); + void hidStart(USBHIDDriver *uhdp, const USBHIDConfig *config); + void hidStop(USBHIDDriver *uhdp); + void hidDisconnectI(USBHIDDriver *uhdp); + void hidConfigureHookI(USBHIDDriver *uhdp); + bool hidRequestsHook(USBDriver *usbp); + void hidDataTransmitted(USBDriver *usbp, usbep_t ep); + void hidDataReceived(USBDriver *usbp, usbep_t ep); + size_t hidWriteReport(USBHIDDriver *uhdp, uint8_t *bp, size_t n); + size_t hidWriteReportt(USBHIDDriver *uhdp, uint8_t *bp, size_t n, systime_t timeout); + size_t hidReadReport(USBHIDDriver *uhdp, uint8_t *bp, size_t n); + size_t hidReadReportt(USBHIDDriver *uhdp, uint8_t *bp, size_t n, systime_t timeout); +#ifdef __cplusplus +} +#endif + +#endif /* HAL_USE_USB_HID */ + +#endif /* HAL_USB_HID_H */ + +/** @} */ diff --git a/os/hal/src/hal_usb_hid.c b/os/hal/src/hal_usb_hid.c new file mode 100644 index 00000000..56be9b71 --- /dev/null +++ b/os/hal/src/hal_usb_hid.c @@ -0,0 +1,581 @@ +/* + ChibiOS - Copyright (C) 2016 Jonathan Struebel + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file hal_usb_hid.c + * @brief USB HID Driver code. + * + * @addtogroup USB_HID + * @{ + */ + +#include "hal.h" + +#if (HAL_USE_USB_HID == TRUE) || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +static uint16_t get_hword(uint8_t *p) { + uint16_t hw; + + hw = (uint16_t)*p++; + hw |= (uint16_t)*p << 8U; + return hw; +} + +/* + * Interface implementation. + */ + +static size_t write(void *ip, const uint8_t *bp, size_t n) { + + if (usbGetDriverStateI(((USBHIDDriver *)ip)->config->usbp) != USB_ACTIVE) { + return 0; + } + + return obqWriteTimeout(&((USBHIDDriver *)ip)->obqueue, bp, + n, TIME_INFINITE); +} + +static size_t read(void *ip, uint8_t *bp, size_t n) { + + if (usbGetDriverStateI(((USBHIDDriver *)ip)->config->usbp) != USB_ACTIVE) { + return 0; + } + + return ibqReadTimeout(&((USBHIDDriver *)ip)->ibqueue, bp, + n, TIME_INFINITE); +} + +static msg_t put(void *ip, uint8_t b) { + + if (usbGetDriverStateI(((USBHIDDriver *)ip)->config->usbp) != USB_ACTIVE) { + return MSG_RESET; + } + + return obqPutTimeout(&((USBHIDDriver *)ip)->obqueue, b, TIME_INFINITE); +} + +static msg_t get(void *ip) { + + if (usbGetDriverStateI(((USBHIDDriver *)ip)->config->usbp) != USB_ACTIVE) { + return MSG_RESET; + } + + return ibqGetTimeout(&((USBHIDDriver *)ip)->ibqueue, TIME_INFINITE); +} + +static msg_t putt(void *ip, uint8_t b, systime_t timeout) { + + if (usbGetDriverStateI(((USBHIDDriver *)ip)->config->usbp) != USB_ACTIVE) { + return MSG_RESET; + } + + return obqPutTimeout(&((USBHIDDriver *)ip)->obqueue, b, timeout); +} + +static msg_t gett(void *ip, systime_t timeout) { + + if (usbGetDriverStateI(((USBHIDDriver *)ip)->config->usbp) != USB_ACTIVE) { + return MSG_RESET; + } + + return ibqGetTimeout(&((USBHIDDriver *)ip)->ibqueue, timeout); +} + +static size_t writet(void *ip, const uint8_t *bp, size_t n, systime_t timeout) { + + if (usbGetDriverStateI(((USBHIDDriver *)ip)->config->usbp) != USB_ACTIVE) { + return 0; + } + + return obqWriteTimeout(&((USBHIDDriver *)ip)->obqueue, bp, n, timeout); +} + +static size_t readt(void *ip, uint8_t *bp, size_t n, systime_t timeout) { + + if (usbGetDriverStateI(((USBHIDDriver *)ip)->config->usbp) != USB_ACTIVE) { + return 0; + } + + return ibqReadTimeout(&((USBHIDDriver *)ip)->ibqueue, bp, n, timeout); +} + +static void flush(void *ip) { + + obqFlush(&((USBHIDDriver *)ip)->obqueue); +} + +static const struct USBHIDDriverVMT vmt = { + write, read, put, get, + putt, gett, writet, readt, + flush +}; + +/** + * @brief Notification of empty buffer released into the input buffers queue. + * + * @param[in] bqp the buffers queue pointer. + */ +static void ibnotify(io_buffers_queue_t *bqp) { + USBHIDDriver *uhdp = bqGetLinkX(bqp); + + /* If the USB driver is not in the appropriate state then transactions + must not be started.*/ + if ((usbGetDriverStateI(uhdp->config->usbp) != USB_ACTIVE) || + (uhdp->state != HID_READY)) { + return; + } + + /* Checking if there is already a transaction ongoing on the endpoint.*/ + if (!usbGetReceiveStatusI(uhdp->config->usbp, uhdp->config->int_out)) { + /* Trying to get a free buffer.*/ + uint8_t *buf = ibqGetEmptyBufferI(&uhdp->ibqueue); + if (buf != NULL) { + /* Buffer found, starting a new transaction.*/ + usbStartReceiveI(uhdp->config->usbp, uhdp->config->int_out, + buf, SERIAL_USB_BUFFERS_SIZE); + } + } +} + +/** + * @brief Notification of filled buffer inserted into the output buffers queue. + * + * @param[in] bqp the buffers queue pointer. + */ +static void obnotify(io_buffers_queue_t *bqp) { + size_t n; + USBHIDDriver *uhdp = bqGetLinkX(bqp); + + /* If the USB driver is not in the appropriate state then transactions + must not be started.*/ + if ((usbGetDriverStateI(uhdp->config->usbp) != USB_ACTIVE) || + (uhdp->state != HID_READY)) { + return; + } + + /* Checking if there is already a transaction ongoing on the endpoint.*/ + if (!usbGetTransmitStatusI(uhdp->config->usbp, uhdp->config->int_in)) { + /* Trying to get a full buffer.*/ + uint8_t *buf = obqGetFullBufferI(&uhdp->obqueue, &n); + if (buf != NULL) { + /* Buffer found, starting a new transaction.*/ + usbStartTransmitI(uhdp->config->usbp, uhdp->config->int_in, buf, n); + } + } +} + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief USB HID Driver initialization. + * @note This function is implicitly invoked by @p halInit(), there is + * no need to explicitly initialize the driver. + * + * @init + */ +void hidInit(void) { +} + +/** + * @brief Initializes a generic full duplex USB HID driver object. + * @details The HW dependent part of the initialization has to be performed + * outside, usually in the hardware initialization code. + * + * @param[out] uhdp pointer to a @p USBHIDDriver structure + * + * @init + */ +void hidObjectInit(USBHIDDriver *uhdp) { + + uhdp->vmt = &vmt; + osalEventObjectInit(&uhdp->event); + uhdp->state = HID_STOP; + ibqObjectInit(&uhdp->ibqueue, uhdp->ib, + USB_HID_BUFFERS_SIZE, USB_HID_BUFFERS_NUMBER, + ibnotify, uhdp); + obqObjectInit(&uhdp->obqueue, uhdp->ob, + USB_HID_BUFFERS_SIZE, USB_HID_BUFFERS_NUMBER, + obnotify, uhdp); +} + +/** + * @brief Configures and starts the driver. + * + * @param[in] uhdp pointer to a @p USBHIDDriver object + * @param[in] config the USB HID driver configuration + * + * @api + */ +void hidStart(USBHIDDriver *uhdp, const USBHIDConfig *config) { + USBDriver *usbp = config->usbp; + + osalDbgCheck(uhdp != NULL); + + osalSysLock(); + osalDbgAssert((uhdp->state == HID_STOP) || (uhdp->state == HID_READY), + "invalid state"); + usbp->in_params[config->int_in - 1U] = uhdp; + usbp->out_params[config->int_out - 1U] = uhdp; + uhdp->config = config; + uhdp->state = HID_READY; + osalSysUnlock(); +} + +/** + * @brief Stops the driver. + * @details Any thread waiting on the driver's queues will be awakened with + * the message @p MSG_RESET. + * + * @param[in] uhdp pointer to a @p USBHIDDriver object + * + * @api + */ +void hidStop(USBHIDDriver *uhdp) { + USBDriver *usbp = uhdp->config->usbp; + + osalDbgCheck(uhdp != NULL); + + osalSysLock(); + osalDbgAssert((uhdp->state == HID_STOP) || (uhdp->state == HID_READY), + "invalid state"); + + /* Driver in stopped state.*/ + usbp->in_params[uhdp->config->int_in - 1U] = NULL; + usbp->out_params[uhdp->config->int_out - 1U] = NULL; + uhdp->state = HID_STOP; + + /* Enforces a disconnection.*/ + hidDisconnectI(uhdp); + osalOsRescheduleS(); + osalSysUnlock(); +} + +/** + * @brief USB device disconnection handler. + * @note If this function is not called from an ISR then an explicit call + * to @p osalOsRescheduleS() in necessary afterward. + * + * @param[in] uhdp pointer to a @p USBHIDDriver object + * + * @iclass + */ +void hidDisconnectI(USBHIDDriver *uhdp) { + + /* Queues reset in order to signal the driver stop to the application.*/ + chnAddFlagsI(uhdp, CHN_DISCONNECTED); + ibqResetI(&uhdp->ibqueue); + obqResetI(&uhdp->obqueue); +} + +/** + * @brief USB device configured handler. + * + * @param[in] uhdp pointer to a @p USBHIDDriver object + * + * @iclass + */ +void hidConfigureHookI(USBHIDDriver *uhdp) { + uint8_t *buf; + + ibqResetI(&uhdp->ibqueue); + obqResetI(&uhdp->obqueue); + chnAddFlagsI(uhdp, CHN_CONNECTED); + + /* Starts the first OUT transaction immediately.*/ + buf = ibqGetEmptyBufferI(&uhdp->ibqueue); + + osalDbgAssert(buf != NULL, "no free buffer"); + + usbStartReceiveI(uhdp->config->usbp, uhdp->config->int_out, + buf, USB_HID_BUFFERS_SIZE); +} + +/** + * @brief Default requests hook. + * @details Applications wanting to use the USB HID driver can use + * this function at the end of the application specific + * requests hook. The HID_* requests handled here do not + * transfer any data to the application. + * The following requests are handled: + * - HID_GET_IDLE. + * - HID_GET_PROTOCOL. + * - HID_SET_REPORT. + * - HID_SET_IDLE. + * - HID_SET_PROTOCOL. + * - USB_REQ_GET_DESCRIPTOR. + * . + * + * @param[in] usbp pointer to the @p USBDriver object + * @return The hook status. + * @retval true Message handled internally. + * @retval false Message not handled. + */ +bool hidRequestsHook(USBDriver *usbp) { + const USBDescriptor *dp; + + if ((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) { + switch (usbp->setup[1]) { + case HID_GET_IDLE: + usbSetupTransfer(usbp, NULL, 0, NULL); + return true; + case HID_GET_PROTOCOL: + return true; + case HID_SET_REPORT: + usbSetupTransfer(usbp, NULL, 0, NULL); + return true; + case HID_SET_IDLE: + usbSetupTransfer(usbp, NULL, 0, NULL); + return true; + case HID_SET_PROTOCOL: + return true; + default: + return false; + } + } + + /* GET_DESCRIPTOR from interface not handled by default so handle it here */ + if (((usbp->setup[0] & USB_RTYPE_DIR_MASK) == USB_RTYPE_DIR_DEV2HOST) && + ((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE)) { + switch (usbp->setup[1]) { + case USB_REQ_GET_DESCRIPTOR: + dp = usbp->config->get_descriptor_cb(usbp, usbp->setup[3], usbp->setup[2], + get_hword(&usbp->setup[4])); + if (dp == NULL) + return false; + + usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL); + return true; + default: + return false; + } + } + return false; +} + +/** + * @brief Default data transmitted callback. + * @details The application must use this function as callback for the IN + * data endpoint. + * + * @param[in] usbp pointer to the @p USBDriver object + * @param[in] ep IN endpoint number + */ +void hidDataTransmitted(USBDriver *usbp, usbep_t ep) { + uint8_t *buf; + size_t n; + USBHIDDriver *uhdp = usbp->in_params[ep - 1U]; + + if (uhdp == NULL) { + return; + } + + osalSysLockFromISR(); + + /* Signaling that space is available in the output queue.*/ + chnAddFlagsI(uhdp, CHN_OUTPUT_EMPTY); + + /* Freeing the buffer just transmitted, if it was not a zero size packet.*/ + if (usbp->epc[ep]->in_state->txsize > 0U) { + obqReleaseEmptyBufferI(&uhdp->obqueue); + } + + /* Checking if there is a buffer ready for transmission.*/ + buf = obqGetFullBufferI(&uhdp->obqueue, &n); + + if (buf != NULL) { + /* The endpoint cannot be busy, we are in the context of the callback, + so it is safe to transmit without a check.*/ + usbStartTransmitI(usbp, ep, buf, n); + } + else if ((usbp->epc[ep]->in_state->txsize > 0U) && + ((usbp->epc[ep]->in_state->txsize & + ((size_t)usbp->epc[ep]->in_maxsize - 1U)) == 0U)) { + /* Transmit zero sized packet in case the last one has maximum allowed + size. Otherwise the recipient may expect more data coming soon and + not return buffered data to app. See section 5.8.3 Bulk Transfer + Packet Size Constraints of the USB Specification document.*/ + usbStartTransmitI(usbp, ep, usbp->setup, 0); + + } + else { + /* Nothing to transmit.*/ + } + + osalSysUnlockFromISR(); +} + +/** + * @brief Default data received callback. + * @details The application must use this function as callback for the OUT + * data endpoint. + * + * @param[in] usbp pointer to the @p USBDriver object + * @param[in] ep OUT endpoint number + */ +void hidDataReceived(USBDriver *usbp, usbep_t ep) { + uint8_t *buf; + USBHIDDriver *uhdp = usbp->out_params[ep - 1U]; + + if (uhdp == NULL) { + return; + } + + osalSysLockFromISR(); + + /* Signaling that data is available in the input queue.*/ + chnAddFlagsI(uhdp, CHN_INPUT_AVAILABLE); + + /* Posting the filled buffer in the queue.*/ + ibqPostFullBufferI(&uhdp->ibqueue, + usbGetReceiveTransactionSizeX(uhdp->config->usbp, ep)); + + /* The endpoint cannot be busy, we are in the context of the callback, + so a packet is in the buffer for sure. Trying to get a free buffer + for the next transaction.*/ + buf = ibqGetEmptyBufferI(&uhdp->ibqueue); + if (buf != NULL) { + /* Buffer found, starting a new transaction.*/ + usbStartReceiveI(uhdp->config->usbp, ep, buf, USB_HID_BUFFERS_SIZE); + } + + osalSysUnlockFromISR(); +} + +/** + * @brief Write HID Report + * @details The function writes data from a buffer to an output queue. The + * operation completes when the specified amount of data has been + * transferred or if the queue has been reset. + * + * @param[in] uhdp pointer to the @p USBHIDDriver object + * @param[in] bp pointer to the report data buffer + * @param[in] n the maximum amount of data to be transferred, the + * value 0 is reserved + * @return The number of bytes effectively transferred. + * @retval 0 if a timeout occurred. + * + * @api + */ +size_t hidWriteReport(USBHIDDriver *uhdp, uint8_t *bp, size_t n) { + size_t val; + + val = uhdp->vmt->write(uhdp, bp, n); + + if (val > 0) + uhdp->vmt->flush(uhdp); + + return val; +} + +/** + * @brief Write HID report with timeout + * @details The function writes data from a buffer to an output queue. The + * operation completes when the specified amount of data has been + * transferred or after the specified timeout or if the queue has + * been reset. + * + * @param[in] uhdp pointer to the @p USBHIDDriver object + * @param[in] bp pointer to the report data buffer + * @param[in] n the maximum amount of data to be transferred, the + * value 0 is reserved + * @param[in] timeout the number of ticks before the operation timeouts, + * the following special values are allowed: + * - @a TIME_IMMEDIATE immediate timeout. + * - @a TIME_INFINITE no timeout. + * . + * @return The number of bytes effectively transferred. + * @retval 0 if a timeout occurred. + * + * @api + */ +size_t hidWriteReportt(USBHIDDriver *uhdp, uint8_t *bp, size_t n, systime_t timeout) { + size_t val; + + val = uhdp->vmt->writet(uhdp, bp, n, timeout); + + if (val > 0) + uhdp->vmt->flush(uhdp); + + return val; +} + +/** + * @brief Read HID report + * @details The function reads data from an input queue into a buffer. + * The operation completes when the specified amount of data has been + * transferred or if the queue has been reset. + * + * @param[in] uhdp pointer to the @p input_buffers_queue_t object + * @param[out] bp pointer to the data buffer + * @param[in] n the maximum amount of data to be transferred, the + * value 0 is reserved + * @return The number of bytes effectively transferred. + * @retval 0 if a timeout occurred. + * + * @api + */ +size_t hidReadReport(USBHIDDriver *uhdp, uint8_t *bp, size_t n) { + + return uhdp->vmt->read(uhdp, bp, n); +} + +/** + * @brief Read HID report with timeout + * @details The function reads data from an input queue into a buffer. + * The operation completes when the specified amount of data has been + * transferred or after the specified timeout or if the queue has + * been reset. + * + * @param[in] uhdp pointer to the @p input_buffers_queue_t object + * @param[out] bp pointer to the data buffer + * @param[in] n the maximum amount of data to be transferred, the + * value 0 is reserved + * @param[in] timeout the number of ticks before the operation timeouts, + * the following special values are allowed: + * - @a TIME_IMMEDIATE immediate timeout. + * - @a TIME_INFINITE no timeout. + * . + * @return The number of bytes effectively transferred. + * @retval 0 if a timeout occurred. + * + * @api + */ +size_t hidReadReportt(USBHIDDriver *uhdp, uint8_t *bp, size_t n, systime_t timeout) { + + return uhdp->vmt->readt(uhdp, bp, n, timeout); +} + +#endif /* HAL_USE_USB_HID == TRUE */ + +/** @} */ diff --git a/testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/Makefile b/testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/Makefile new file mode 100644 index 00000000..ed84ee9a --- /dev/null +++ b/testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/Makefile @@ -0,0 +1,23 @@ +CC=gcc +CFLAGS = -Wall -Wextra -O2 -g + +SRCS = $(wildcard *.c) +OBJS = $(SRCS:%.c=%.o) +EXE = test-usb-hid + +all: $(EXE) + +$(EXE): $(OBJS) + +-include $(subst .c,.d,$(SRCS)) + +%.d: %.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -MM -MF $@ -MP -MT $(subst .c,.o,$<) $< + +clean: + rm -f $(EXE) + rm -f $(OBJS) + rm -f $(subst .c,.d,$(SRCS)) + rm -f *~ + +.PHONY: clean all diff --git a/testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/test-usb-hid.c b/testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/test-usb-hid.c new file mode 100644 index 00000000..241b129f --- /dev/null +++ b/testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/test-usb-hid.c @@ -0,0 +1,180 @@ +/* + + Copyright (c) 2014 Guillaume Duc + + 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define USB_HID_IN_REPORT_SIZE 1 +#define USB_HID_OUT_REPORT_SIZE 1 + +struct usb_hid_in_report_s +{ + uint8_t sequence_number; +}; + +struct usb_hid_out_report_s +{ + uint8_t sequence_number; +}; + +static uint8_t usb_hid_in_report_buf[USB_HID_IN_REPORT_SIZE]; +// +1 for the report index +static uint8_t usb_hid_out_report_buf[USB_HID_OUT_REPORT_SIZE + 1]; + +static struct usb_hid_in_report_s *usb_hid_in_report = + (struct usb_hid_in_report_s *) usb_hid_in_report_buf; + +static struct usb_hid_out_report_s *usb_hid_out_report = + (struct usb_hid_out_report_s *) (&usb_hid_out_report_buf[1]); + +static int usb_hid_fd; +static uint8_t wkup_pb_old_value = 0; + +static void +read_in_report () +{ + int res, i; + + printf ("read()\n"); + res = read (usb_hid_fd, usb_hid_in_report_buf, USB_HID_IN_REPORT_SIZE); + if (res < 0) + { + perror ("read"); + exit (EXIT_FAILURE); + } + else + { + printf ("read() read %d bytes:\t", res); + for (i = 0; i < res; i++) + printf ("%02hhx ", usb_hid_in_report_buf[i]); + printf ("\n"); + } +} + +static void +send_out_report () +{ + int res; + + usb_hid_out_report_buf[0] = 0; + + res = + write (usb_hid_fd, usb_hid_out_report_buf, USB_HID_OUT_REPORT_SIZE + 1); + if (res < 0) + { + perror ("write"); + exit (EXIT_FAILURE); + } + + usb_hid_out_report->sequence_number++; +} + +static void +usb_hid_init (const char *dev_name) +{ + int i, res; + int desc_size = 0; + char buf[256]; + + struct hidraw_report_descriptor rpt_desc; + struct hidraw_devinfo info; + + usb_hid_fd = open (dev_name, O_RDWR); + + if (usb_hid_fd < 0) + { + perror ("Unable to open device"); + exit (EXIT_FAILURE); + } + + memset (&rpt_desc, 0x0, sizeof (rpt_desc)); + memset (&info, 0x0, sizeof (info)); + memset (buf, 0x0, sizeof (buf)); + + // Get Report Descriptor Size + res = ioctl (usb_hid_fd, HIDIOCGRDESCSIZE, &desc_size); + if (res < 0) + perror ("HIDIOCGRDESCSIZE"); + else + printf ("Report Descriptor Size: %d\n", desc_size); + + // Get Report Descriptor + rpt_desc.size = desc_size; + res = ioctl (usb_hid_fd, HIDIOCGRDESC, &rpt_desc); + if (res < 0) + { + perror ("HIDIOCGRDESC"); + } + else + { + printf ("Report Descriptor:\n"); + for (i = 0; i < rpt_desc.size; i++) + printf ("%02hhx ", rpt_desc.value[i]); + puts ("\n"); + } +} + +int +main (int argc, char **argv) +{ + if (argc < 2) + { + fprintf (stderr, "Usage: %s /dev/hidrawX\n", argv[0]); + return EXIT_FAILURE; + } + + memset (usb_hid_out_report_buf, 0, sizeof (usb_hid_out_report_buf)); + + usb_hid_init (argv[1]); + usb_hid_out_report->sequence_number = 4; + send_out_report (); + + while (1) + { + read_in_report (); + + if (usb_hid_in_report->sequence_number == 40) + { + usb_hid_out_report->sequence_number = usb_hid_in_report->sequence_number / 2; + send_out_report (); + } + + } + + close (usb_hid_fd); + + return EXIT_SUCCESS; +} diff --git a/testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/udev.rules b/testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/udev.rules new file mode 100644 index 00000000..8c93f15b --- /dev/null +++ b/testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/udev.rules @@ -0,0 +1,2 @@ +SUBSYSTEM=="usb", ATTR{idVendor}=="0179", ATTR{idProduct}=="0002", MODE:="0666" +KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="0179", ATTRS{idProduct}=="0002", MODE:="0666" diff --git a/testhal/KINETIS/FRDM-KL25Z/USB_HID/Makefile b/testhal/KINETIS/FRDM-KL25Z/USB_HID/Makefile new file mode 100644 index 00000000..39c7ccdb --- /dev/null +++ b/testhal/KINETIS/FRDM-KL25Z/USB_HID/Makefile @@ -0,0 +1,216 @@ +############################################################################## +# Build global options +# NOTE: Can be overridden externally. +# + +# Compiler options here. +ifeq ($(USE_OPT),) + USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16 --specs=nosys.specs -lnosys +endif + +# C specific options here (added to USE_OPT). +ifeq ($(USE_COPT),) + USE_COPT = +endif + +# C++ specific options here (added to USE_OPT). +ifeq ($(USE_CPPOPT),) + USE_CPPOPT = -fno-rtti +endif + +# Enable this if you want the linker to remove unused code and data +ifeq ($(USE_LINK_GC),) + USE_LINK_GC = yes +endif + +# Linker extra options here. +ifeq ($(USE_LDOPT),) + USE_LDOPT = +endif + +# Enable this if you want link time optimizations (LTO) +ifeq ($(USE_LTO),) + USE_LTO = no +endif + +# If enabled, this option allows to compile the application in THUMB mode. +ifeq ($(USE_THUMB),) + USE_THUMB = yes +endif + +# Enable this if you want to see the full log while compiling. +ifeq ($(USE_VERBOSE_COMPILE),) + USE_VERBOSE_COMPILE = no +endif + +# If enabled, this option makes the build process faster by not compiling +# modules not used in the current configuration. +ifeq ($(USE_SMART_BUILD),) + USE_SMART_BUILD = yes +endif + +# +# Build global options +############################################################################## + +############################################################################## +# Architecture or project specific options +# + +# Stack size to be allocated to the Cortex-M process stack. This stack is +# the stack used by the main() thread. +ifeq ($(USE_PROCESS_STACKSIZE),) + USE_PROCESS_STACKSIZE = 0x200 +endif + +# Stack size to the allocated to the Cortex-M main/exceptions stack. This +# stack is used for processing interrupts and exceptions. +ifeq ($(USE_EXCEPTIONS_STACKSIZE),) + USE_EXCEPTIONS_STACKSIZE = 0x400 +endif + +# +# Architecture or project specific options +############################################################################## + +############################################################################## +# Project, sources and paths +# + +# Define project name here +PROJECT = ch + +# Imported source files and paths +CHIBIOS = ../../../../../ChibiOS-RT +CHIBIOS_CONTRIB = $(CHIBIOS)/../ChibiOS-Contrib +# Startup files. +include $(CHIBIOS_CONTRIB)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_kl2x.mk +# HAL-OSAL files (optional). +include $(CHIBIOS_CONTRIB)/os/hal/hal.mk +include $(CHIBIOS_CONTRIB)/os/hal/ports/KINETIS/KL2x/platform.mk +include $(CHIBIOS_CONTRIB)/os/hal/boards/FREESCALE_FREEDOM_KL25Z/board.mk +include $(CHIBIOS)/os/hal/osal/rt/osal.mk +# RTOS files (optional). +include $(CHIBIOS)/os/rt/rt.mk +include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v6m.mk +# Other files (optional). +#include $(CHIBIOS)/test/rt/test.mk +#include $(CHIBIOS)/os/hal/lib/streams/streams.mk + +# Define linker script file here +LDSCRIPT= $(STARTUPLD)/MKL2xZ128.ld + +# C sources that can be compiled in ARM or THUMB mode depending on the global +# setting. +CSRC = $(STARTUPSRC) \ + $(KERNSRC) \ + $(PORTSRC) \ + $(OSALSRC) \ + $(HALSRC) \ + $(PLATFORMSRC) \ + $(BOARDSRC) \ + usbcfg.c \ + main.c + +# C++ sources that can be compiled in ARM or THUMB mode depending on the global +# setting. +CPPSRC = + +# C sources to be compiled in ARM mode regardless of the global setting. +# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler +# option that results in lower performance and larger code size. +ACSRC = + +# C++ sources to be compiled in ARM mode regardless of the global setting. +# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler +# option that results in lower performance and larger code size. +ACPPSRC = + +# C sources to be compiled in THUMB mode regardless of the global setting. +# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler +# option that results in lower performance and larger code size. +TCSRC = + +# C sources to be compiled in THUMB mode regardless of the global setting. +# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler +# option that results in lower performance and larger code size. +TCPPSRC = + +# List ASM source files here +ASMSRC = +ASMXSRC = $(STARTUPASM) $(PORTASM) $(OSALASM) + +INCDIR = $(CHIBIOS)/os/license \ + $(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \ + $(HALINC) $(PLATFORMINC) $(BOARDINC) + + +# +# Project, sources and paths +############################################################################## + +############################################################################## +# Compiler settings +# + +MCU = cortex-m0 + +#TRGT = arm-elf- +TRGT = arm-none-eabi- +CC = $(TRGT)gcc +CPPC = $(TRGT)g++ +# Enable loading with g++ only if you need C++ runtime support. +# NOTE: You can use C++ even without C++ support if you are careful. C++ +# runtime support makes code size explode. +LD = $(TRGT)gcc +#LD = $(TRGT)g++ +CP = $(TRGT)objcopy +AS = $(TRGT)gcc -x assembler-with-cpp +AR = $(TRGT)ar +OD = $(TRGT)objdump +SZ = $(TRGT)size +HEX = $(CP) -O ihex +BIN = $(CP) -O binary +SREC = $(CP) -O srec + +# ARM-specific options here +AOPT = + +# THUMB-specific options here +TOPT = -mthumb -DTHUMB + +# Define C warning options here +CWARN = -Wall -Wextra -Wundef -Wstrict-prototypes + +# Define C++ warning options here +CPPWARN = -Wall -Wextra -Wundef + +# +# Compiler settings +############################################################################## + +############################################################################## +# Start of user section +# + +# List all user C define here, like -D_DEBUG=1 +UDEFS = + +# Define ASM defines here +UADEFS = + +# List all user directories here +UINCDIR = + +# List the user directory to look for the libraries here +ULIBDIR = + +# List all user libraries here +ULIBS = + +# +# End of user defines +############################################################################## + +RULESPATH = $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC +include $(RULESPATH)/rules.mk diff --git a/testhal/KINETIS/FRDM-KL25Z/USB_HID/chconf.h b/testhal/KINETIS/FRDM-KL25Z/USB_HID/chconf.h new file mode 100644 index 00000000..6774a447 --- /dev/null +++ b/testhal/KINETIS/FRDM-KL25Z/USB_HID/chconf.h @@ -0,0 +1,516 @@ +/* + ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file templates/chconf.h + * @brief Configuration file template. + * @details A copy of this file must be placed in each project directory, it + * contains the application specific kernel settings. + * + * @addtogroup config + * @details Kernel related settings and hooks. + * @{ + */ + +#ifndef CHCONF_H +#define CHCONF_H + +#define _CHIBIOS_RT_CONF_ + +/*===========================================================================*/ +/** + * @name System timers settings + * @{ + */ +/*===========================================================================*/ + +/** + * @brief System time counter resolution. + * @note Allowed values are 16 or 32 bits. + */ +#define CH_CFG_ST_RESOLUTION 32 + +/** + * @brief System tick frequency. + * @details Frequency of the system timer that drives the system ticks. This + * setting also defines the system tick time unit. + */ +#define CH_CFG_ST_FREQUENCY 1000 + +/** + * @brief Time delta constant for the tick-less mode. + * @note If this value is zero then the system uses the classic + * periodic tick. This value represents the minimum number + * of ticks that is safe to specify in a timeout directive. + * The value one is not valid, timeouts are rounded up to + * this value. + */ +#define CH_CFG_ST_TIMEDELTA 0 + +/** @} */ + +/*===========================================================================*/ +/** + * @name Kernel parameters and options + * @{ + */ +/*===========================================================================*/ + +/** + * @brief Round robin interval. + * @details This constant is the number of system ticks allowed for the + * threads before preemption occurs. Setting this value to zero + * disables the preemption for threads with equal priority and the + * round robin becomes cooperative. Note that higher priority + * threads can still preempt, the kernel is always preemptive. + * @note Disabling the round robin preemption makes the kernel more compact + * and generally faster. + * @note The round robin preemption is not supported in tickless mode and + * must be set to zero in that case. + */ +#define CH_CFG_TIME_QUANTUM 20 + +/** + * @brief Managed RAM size. + * @details Size of the RAM area to be managed by the OS. If set to zero + * then the whole available RAM is used. The core memory is made + * available to the heap allocator and/or can be used directly through + * the simplified core memory allocator. + * + * @note In order to let the OS manage the whole RAM the linker script must + * provide the @p __heap_base__ and @p __heap_end__ symbols. + * @note Requires @p CH_CFG_USE_MEMCORE. + */ +#define CH_CFG_MEMCORE_SIZE 0 + +/** + * @brief Idle thread automatic spawn suppression. + * @details When this option is activated the function @p chSysInit() + * does not spawn the idle thread. The application @p main() + * function becomes the idle thread and must implement an + * infinite loop. + */ +#define CH_CFG_NO_IDLE_THREAD FALSE + +/** @} */ + +/*===========================================================================*/ +/** + * @name Performance options + * @{ + */ +/*===========================================================================*/ + +/** + * @brief OS optimization. + * @details If enabled then time efficient rather than space efficient code + * is used when two possible implementations exist. + * + * @note This is not related to the compiler optimization options. + * @note The default is @p TRUE. + */ +#define CH_CFG_OPTIMIZE_SPEED TRUE + +/** @} */ + +/*===========================================================================*/ +/** + * @name Subsystem options + * @{ + */ +/*===========================================================================*/ + +/** + * @brief Time Measurement APIs. + * @details If enabled then the time measurement APIs are included in + * the kernel. + * + * @note The default is @p TRUE. + */ +#define CH_CFG_USE_TM FALSE + +/** + * @brief Threads registry APIs. + * @details If enabled then the registry APIs are included in the kernel. + * + * @note The default is @p TRUE. + */ +#define CH_CFG_USE_REGISTRY TRUE + +/** + * @brief Threads synchronization APIs. + * @details If enabled then the @p chThdWait() function is included in + * the kernel. + * + * @note The default is @p TRUE. + */ +#define CH_CFG_USE_WAITEXIT TRUE + +/** + * @brief Semaphores APIs. + * @details If enabled then the Semaphores APIs are included in the kernel. + * + * @note The default is @p TRUE. + */ +#define CH_CFG_USE_SEMAPHORES TRUE + +/** + * @brief Semaphores queuing mode. + * @details If enabled then the threads are enqueued on semaphores by + * priority rather than in FIFO order. + * + * @note The default is @p FALSE. Enable this if you have special + * requirements. + * @note Requires @p CH_CFG_USE_SEMAPHORES. + */ +#define CH_CFG_USE_SEMAPHORES_PRIORITY FALSE + +/** + * @brief Mutexes APIs. + * @details If enabled then the mutexes APIs are included in the kernel. + * + * @note The default is @p TRUE. + */ +#define CH_CFG_USE_MUTEXES TRUE + +/** + * @brief Enables recursive behavior on mutexes. + * @note Recursive mutexes are heavier and have an increased + * memory footprint. + * + * @note The default is @p FALSE. + * @note Requires @p CH_CFG_USE_MUTEXES. + */ +#define CH_CFG_USE_MUTEXES_RECURSIVE FALSE + +/** + * @brief Conditional Variables APIs. + * @details If enabled then the conditional variables APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_MUTEXES. + */ +#define CH_CFG_USE_CONDVARS TRUE + +/** + * @brief Conditional Variables APIs with timeout. + * @details If enabled then the conditional variables APIs with timeout + * specification are included in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_CONDVARS. + */ +#define CH_CFG_USE_CONDVARS_TIMEOUT TRUE + +/** + * @brief Events Flags APIs. + * @details If enabled then the event flags APIs are included in the kernel. + * + * @note The default is @p TRUE. + */ +#define CH_CFG_USE_EVENTS TRUE + +/** + * @brief Events Flags APIs with timeout. + * @details If enabled then the events APIs with timeout specification + * are included in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_EVENTS. + */ +#define CH_CFG_USE_EVENTS_TIMEOUT TRUE + +/** + * @brief Synchronous Messages APIs. + * @details If enabled then the synchronous messages APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + */ +#define CH_CFG_USE_MESSAGES TRUE + +/** + * @brief Synchronous Messages queuing mode. + * @details If enabled then messages are served by priority rather than in + * FIFO order. + * + * @note The default is @p FALSE. Enable this if you have special + * requirements. + * @note Requires @p CH_CFG_USE_MESSAGES. + */ +#define CH_CFG_USE_MESSAGES_PRIORITY FALSE + +/** + * @brief Mailboxes APIs. + * @details If enabled then the asynchronous messages (mailboxes) APIs are + * included in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_SEMAPHORES. + */ +#define CH_CFG_USE_MAILBOXES TRUE + +/** + * @brief Core Memory Manager APIs. + * @details If enabled then the core memory manager APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + */ +#define CH_CFG_USE_MEMCORE TRUE + +/** + * @brief Heap Allocator APIs. + * @details If enabled then the memory heap allocator APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_MEMCORE and either @p CH_CFG_USE_MUTEXES or + * @p CH_CFG_USE_SEMAPHORES. + * @note Mutexes are recommended. + */ +#define CH_CFG_USE_HEAP TRUE + +/** + * @brief Memory Pools Allocator APIs. + * @details If enabled then the memory pools allocator APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + */ +#define CH_CFG_USE_MEMPOOLS TRUE + +/** + * @brief Dynamic Threads APIs. + * @details If enabled then the dynamic threads creation APIs are included + * in the kernel. + * + * @note The default is @p TRUE. + * @note Requires @p CH_CFG_USE_WAITEXIT. + * @note Requires @p CH_CFG_USE_HEAP and/or @p CH_CFG_USE_MEMPOOLS. + */ +#define CH_CFG_USE_DYNAMIC TRUE + +/** @} */ + +/*===========================================================================*/ +/** + * @name Debug options + * @{ + */ +/*===========================================================================*/ + +/** + * @brief Debug option, kernel statistics. + * + * @note The default is @p FALSE. + */ +#define CH_DBG_STATISTICS FALSE + +/** + * @brief Debug option, system state check. + * @details If enabled the correct call protocol for system APIs is checked + * at runtime. + * + * @note The default is @p FALSE. + */ +#define CH_DBG_SYSTEM_STATE_CHECK TRUE + +/** + * @brief Debug option, parameters checks. + * @details If enabled then the checks on the API functions input + * parameters are activated. + * + * @note The default is @p FALSE. + */ +#define CH_DBG_ENABLE_CHECKS TRUE + +/** + * @brief Debug option, consistency checks. + * @details If enabled then all the assertions in the kernel code are + * activated. This includes consistency checks inside the kernel, + * runtime anomalies and port-defined checks. + * + * @note The default is @p FALSE. + */ +#define CH_DBG_ENABLE_ASSERTS TRUE + +/** + * @brief Debug option, trace buffer. + * @details If enabled then the context switch circular trace buffer is + * activated. + * + * @note The default is @p FALSE. + */ +#define CH_DBG_ENABLE_TRACE TRUE + +/** + * @brief Debug option, stack checks. + * @details If enabled then a runtime stack check is performed. + * + * @note The default is @p FALSE. + * @note The stack check is performed in a architecture/port dependent way. + * It may not be implemented or some ports. + * @note The default failure mode is to halt the system with the global + * @p panic_msg variable set to @p NULL. + */ +#define CH_DBG_ENABLE_STACK_CHECK FALSE + +/** + * @brief Debug option, stacks initialization. + * @details If enabled then the threads working area is filled with a byte + * value when a thread is created. This can be useful for the + * runtime measurement of the used stack. + * + * @note The default is @p FALSE. + */ +#define CH_DBG_FILL_THREADS TRUE + +/** + * @brief Debug option, threads profiling. + * @details If enabled then a field is added to the @p thread_t structure that + * counts the system ticks occurred while executing the thread. + * + * @note The default is @p FALSE. + * @note This debug option is not currently compatible with the + * tickless mode. + */ +#define CH_DBG_THREADS_PROFILING FALSE + +/** @} */ + +/*===========================================================================*/ +/** + * @name Kernel hooks + * @{ + */ +/*===========================================================================*/ + +/** + * @brief Threads descriptor structure extension. + * @details User fields added to the end of the @p thread_t structure. + */ +#define CH_CFG_THREAD_EXTRA_FIELDS \ + /* Add threads custom fields here.*/ + +/** + * @brief Threads initialization hook. + * @details User initialization code added to the @p chThdInit() API. + * + * @note It is invoked from within @p chThdInit() and implicitly from all + * the threads creation APIs. + */ +#define CH_CFG_THREAD_INIT_HOOK(tp) { \ + /* Add threads initialization code here.*/ \ +} + +/** + * @brief Threads finalization hook. + * @details User finalization code added to the @p chThdExit() API. + * + * @note It is inserted into lock zone. + * @note It is also invoked when the threads simply return in order to + * terminate. + */ +#define CH_CFG_THREAD_EXIT_HOOK(tp) { \ + /* Add threads finalization code here.*/ \ +} + +/** + * @brief Context switch hook. + * @details This hook is invoked jus * @brief ISR enter hook. + */ +#define CH_CFG_IRQ_PROLOGUE_HOOK() { \ + /* IRQ prologue code here.*/ \ +} + +/** + * @brief ISR exit hook. + */ +#define CH_CFG_IRQ_EPILOGUE_HOOK() { \ + /* IRQ epilogue code here.*/ \ +} + +/** +t before switching between threads. + */ +#define CH_CFG_CONTEXT_SWITCH_HOOK(ntp, otp) { \ + /* Context switch code here.*/ \ +} + +/** + * @brief Idle thread enter hook. + * @note This hook is invoked within a critical zone, no OS functions + * should be invoked from here. + * @note This macro can be used to activate a power saving mode. + */ +#define CH_CFG_IDLE_ENTER_HOOK() { \ +} + +/** + * @brief Idle thread leave hook. + * @note This hook is invoked within a critical zone, no OS functions + * should be invoked from here. + * @note This macro can be used to deactivate a power saving mode. + */ +#define CH_CFG_IDLE_LEAVE_HOOK() { \ +} + +/** + * @brief Idle Loop hook. + * @details This hook is continuously invoked by the idle thread loop. + */ +#define CH_CFG_IDLE_LOOP_HOOK() { \ + /* Idle loop code here.*/ \ +} + +/** + * @brief System tick event hook. + * @details This hook is invoked in the system tick handler immediately + * after processing the virtual timers queue. + */ +#define CH_CFG_SYSTEM_TICK_HOOK() { \ + /* System tick event code here.*/ \ +} + +/** + * @brief System halt hook. + * @details This hook is invoked in case to a system halting error before + * the system is halted. + */ +#define CH_CFG_SYSTEM_HALT_HOOK(reason) { \ + /* System halt code here.*/ \ +} + +/** + * @brief Trace hook. + * @details This hook is invoked each time a new record is written in the + * trace buffer. + */ +#define CH_CFG_TRACE_HOOK(tep) { \ + /* Trace code here.*/ \ +} + +/** @} */ + +/*===========================================================================*/ +/* Port-specific settings (override port settings defaulted in chcore.h). */ +/*===========================================================================*/ + +#endif /* CHCONF_H */ + +/** @} */ diff --git a/testhal/KINETIS/FRDM-KL25Z/USB_HID/halconf.h b/testhal/KINETIS/FRDM-KL25Z/USB_HID/halconf.h new file mode 100644 index 00000000..a3466b43 --- /dev/null +++ b/testhal/KINETIS/FRDM-KL25Z/USB_HID/halconf.h @@ -0,0 +1,368 @@ +/* + ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file boards/FRDM-KL25Z/halconf.h + * @brief HAL configuration header. + * @details HAL configuration file, this file allows to enable or disable the + * various device drivers from your application. You may also use + * this file in order to override the device drivers default settings. + * + * @addtogroup HAL_CONF + * @{ + */ + +#ifndef HALCONF_H +#define HALCONF_H + +#include "mcuconf.h" + +/** + * @brief Enables the PAL subsystem. + */ +#if !defined(HAL_USE_PAL) || defined(__DOXYGEN__) +#define HAL_USE_PAL TRUE +#endif + +/** + * @brief Enables the ADC subsystem. + */ +#if !defined(HAL_USE_ADC) || defined(__DOXYGEN__) +#define HAL_USE_ADC FALSE +#endif + +/** + * @brief Enables the CAN subsystem. + */ +#if !defined(HAL_USE_CAN) || defined(__DOXYGEN__) +#define HAL_USE_CAN FALSE +#endif + +/** + * @brief Enables the DAC subsystem. + */ +#if !defined(HAL_USE_DAC) || defined(__DOXYGEN__) +#define HAL_USE_DAC FALSE +#endif + +/** + * @brief Enables the EXT subsystem. + */ +#if !defined(HAL_USE_EXT) || defined(__DOXYGEN__) +#define HAL_USE_EXT FALSE +#endif + +/** + * @brief Enables the GPT subsystem. + */ +#if !defined(HAL_USE_GPT) || defined(__DOXYGEN__) +#define HAL_USE_GPT FALSE +#endif + +/** + * @brief Enables the I2C subsystem. + */ +#if !defined(HAL_USE_I2C) || defined(__DOXYGEN__) +#define HAL_USE_I2C FALSE +#endif + +/** + * @brief Enables the I2S subsystem. + */ +#if !defined(HAL_USE_I2S) || defined(__DOXYGEN__) +#define HAL_USE_I2S FALSE +#endif + +/** + * @brief Enables the ICU subsystem. + */ +#if !defined(HAL_USE_ICU) || defined(__DOXYGEN__) +#define HAL_USE_ICU FALSE +#endif + +/** + * @brief Enables the MAC subsystem. + */ +#if !defined(HAL_USE_MAC) || defined(__DOXYGEN__) +#define HAL_USE_MAC FALSE +#endif + +/** + * @brief Enables the MMC_SPI subsystem. + */ +#if !defined(HAL_USE_MMC_SPI) || defined(__DOXYGEN__) +#define HAL_USE_MMC_SPI FALSE +#endif + +/** + * @brief Enables the PWM subsystem. + */ +#if !defined(HAL_USE_PWM) || defined(__DOXYGEN__) +#define HAL_USE_PWM FALSE +#endif + +/** + * @brief Enables the RTC subsystem. + */ +#if !defined(HAL_USE_RTC) || defined(__DOXYGEN__) +#define HAL_USE_RTC FALSE +#endif + +/** + * @brief Enables the SDC subsystem. + */ +#if !defined(HAL_USE_SDC) || defined(__DOXYGEN__) +#define HAL_USE_SDC FALSE +#endif + +/** + * @brief Enables the SERIAL subsystem. + */ +#if !defined(HAL_USE_SERIAL) || defined(__DOXYGEN__) +#define HAL_USE_SERIAL TRUE +#endif + +/** + * @brief Enables the SERIAL over USB subsystem. + */ +#if !defined(HAL_USE_SERIAL_USB) || defined(__DOXYGEN__) +#define HAL_USE_SERIAL_USB FALSE +#endif + +/** + * @brief Enables the SPI subsystem. + */ +#if !defined(HAL_USE_SPI) || defined(__DOXYGEN__) +#define HAL_USE_SPI FALSE +#endif + +/** + * @brief Enables the UART subsystem. + */ +#if !defined(HAL_USE_UART) || defined(__DOXYGEN__) +#define HAL_USE_UART FALSE +#endif + +/** + * @brief Enables the USB subsystem. + */ +#if !defined(HAL_USE_USB) || defined(__DOXYGEN__) +#define HAL_USE_USB TRUE +#endif + +/** + * @brief Enables the WDG subsystem. + */ +#if !defined(HAL_USE_WDG) || defined(__DOXYGEN__) +#define HAL_USE_WDG FALSE +#endif + +/*===========================================================================*/ +/* ADC driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(ADC_USE_WAIT) || defined(__DOXYGEN__) +#define ADC_USE_WAIT TRUE +#endif + +/** + * @brief Enables the @p adcAcquireBus() and @p adcReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(ADC_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define ADC_USE_MUTUAL_EXCLUSION TRUE +#endif + +/*===========================================================================*/ +/* CAN driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Sleep mode related APIs inclusion switch. + */ +#if !defined(CAN_USE_SLEEP_MODE) || defined(__DOXYGEN__) +#define CAN_USE_SLEEP_MODE TRUE +#endif + +/*===========================================================================*/ +/* I2C driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables the mutual exclusion APIs on the I2C bus. + */ +#if !defined(I2C_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define I2C_USE_MUTUAL_EXCLUSION TRUE +#endif + +/*===========================================================================*/ +/* MAC driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables an event sources for incoming packets. + */ +#if !defined(MAC_USE_ZERO_COPY) || defined(__DOXYGEN__) +#define MAC_USE_ZERO_COPY FALSE +#endif + +/** + * @brief Enables an event sources for incoming packets. + */ +#if !defined(MAC_USE_EVENTS) || defined(__DOXYGEN__) +#define MAC_USE_EVENTS TRUE +#endif + +/*===========================================================================*/ +/* MMC_SPI driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Delays insertions. + * @details If enabled this options inserts delays into the MMC waiting + * routines releasing some extra CPU time for the threads with + * lower priority, this may slow down the driver a bit however. + * This option is recommended also if the SPI driver does not + * use a DMA channel and heavily loads the CPU. + */ +#if !defined(MMC_NICE_WAITING) || defined(__DOXYGEN__) +#define MMC_NICE_WAITING TRUE +#endif + +/*===========================================================================*/ +/* SDC driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Number of initialization attempts before rejecting the card. + * @note Attempts are performed at 10mS intervals. + */ +#if !defined(SDC_INIT_RETRY) || defined(__DOXYGEN__) +#define SDC_INIT_RETRY 100 +#endif + +/** + * @brief Include support for MMC cards. + * @note MMC support is not yet implemented so this option must be kept + * at @p FALSE. + */ +#if !defined(SDC_MMC_SUPPORT) || defined(__DOXYGEN__) +#define SDC_MMC_SUPPORT FALSE +#endif + +/** + * @brief Delays insertions. + * @details If enabled this options inserts delays into the MMC waiting + * routines releasing some extra CPU time for the threads with + * lower priority, this may slow down the driver a bit however. + */ +#if !defined(SDC_NICE_WAITING) || defined(__DOXYGEN__) +#define SDC_NICE_WAITING TRUE +#endif + +/*===========================================================================*/ +/* SERIAL driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Default bit rate. + * @details Configuration parameter, this is the baud rate selected for the + * default configuration. + */ +#if !defined(SERIAL_DEFAULT_BITRATE) || defined(__DOXYGEN__) +#define SERIAL_DEFAULT_BITRATE 38400 +#endif + +/** + * @brief Serial buffers size. + * @details Configuration parameter, you can change the depth of the queue + * buffers depending on the requirements of your application. + * @note The default is 64 bytes for both the transmission and receive + * buffers. + */ +#if !defined(SERIAL_BUFFERS_SIZE) || defined(__DOXYGEN__) +#define SERIAL_BUFFERS_SIZE 16 +#endif + +/*===========================================================================*/ +/* SERIAL_USB driver related setting. */ +/*===========================================================================*/ + +/** + * @brief Serial over USB buffers size. + * @details Configuration parameter, the buffer size must be a multiple of + * the USB data endpoint maximum packet size. + * @note The default is 256 bytes for both the transmission and receive + * buffers. + */ +#if !defined(SERIAL_USB_BUFFERS_SIZE) || defined(__DOXYGEN__) +#define SERIAL_USB_BUFFERS_SIZE 64 +#endif + +/** + * @brief Serial over USB number of buffers. + * @note The default is 2 buffers. + */ +#if !defined(SERIAL_USB_BUFFERS_NUMBER) || defined(__DOXYGEN__) +#define SERIAL_USB_BUFFERS_NUMBER 8 +#endif +/** @} */ + +/*===========================================================================*/ +/* SPI driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(SPI_USE_WAIT) || defined(__DOXYGEN__) +#define SPI_USE_WAIT TRUE +#endif + +/** + * @brief Enables the @p spiAcquireBus() and @p spiReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(SPI_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define SPI_USE_MUTUAL_EXCLUSION TRUE +#endif + +/*===========================================================================*/ +/* USB driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(USB_USE_WAIT) || defined(__DOXYGEN__) +#define USB_USE_WAIT FALSE +#endif + +/*===========================================================================*/ +/* Community drivers's includes */ +/*===========================================================================*/ + +#include "halconf_community.h" + +#endif /* HALCONF_H */ + +/** @} */ diff --git a/testhal/KINETIS/FRDM-KL25Z/USB_HID/halconf_community.h b/testhal/KINETIS/FRDM-KL25Z/USB_HID/halconf_community.h new file mode 100644 index 00000000..b2f75afb --- /dev/null +++ b/testhal/KINETIS/FRDM-KL25Z/USB_HID/halconf_community.h @@ -0,0 +1,142 @@ +/* + ChibiOS - Copyright (C) 2014 Uladzimir Pylinsky aka barthess + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#ifndef HALCONF_COMMUNITY_H +#define HALCONF_COMMUNITY_H + +/** + * @brief Enables the community overlay. + */ +#if !defined(HAL_USE_COMMUNITY) || defined(__DOXYGEN__) +#define HAL_USE_COMMUNITY TRUE +#endif + +/** + * @brief Enables the FSMC subsystem. + */ +#if !defined(HAL_USE_FSMC) || defined(__DOXYGEN__) +#define HAL_USE_FSMC FALSE +#endif + +/** + * @brief Enables the NAND subsystem. + */ +#if !defined(HAL_USE_NAND) || defined(__DOXYGEN__) +#define HAL_USE_NAND FALSE +#endif + +/** + * @brief Enables the 1-wire subsystem. + */ +#if !defined(HAL_USE_ONEWIRE) || defined(__DOXYGEN__) +#define HAL_USE_ONEWIRE FALSE +#endif + +/** + * @brief Enables the EICU subsystem. + */ +#if !defined(HAL_USE_EICU) || defined(__DOXYGEN__) +#define HAL_USE_EICU FALSE +#endif + +/** + * @brief Enables the CRC subsystem. + */ +#if !defined(HAL_USE_CRC) || defined(__DOXYGEN__) +#define HAL_USE_CRC FALSE +#endif + +/** + * @brief Enables the RNG subsystem. + */ +#if !defined(HAL_USE_RNG) || defined(__DOXYGEN__) +#define HAL_USE_RNG FALSE +#endif + +/** + * @brief Enables the EEPROM subsystem. + */ +#if !defined(HAL_USE_EEPROM) || defined(__DOXYGEN__) +#define HAL_USE_EEPROM FALSE +#endif + +/** + * @brief Enables the TIMCAP subsystem. + */ +#if !defined(HAL_USE_TIMCAP) || defined(__DOXYGEN__) +#define HAL_USE_TIMCAP FALSE +#endif + +/** + * @brief Enables the USBH subsystem. + */ +#if !defined(HAL_USE_USBH) || defined(__DOXYGEN__) +#define HAL_USE_USBH FALSE +#endif + +/** + * @brief Enables the USB HID subsystem. + */ +#if !defined(HAL_USE_USB_HID) || defined(__DOXYGEN__) +#define HAL_USE_USB_HID TRUE +#endif + +/*===========================================================================*/ +/* FSMCNAND driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables the @p nandAcquireBus() and @p nanReleaseBus() APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(NAND_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) +#define NAND_USE_MUTUAL_EXCLUSION TRUE +#endif + +/*===========================================================================*/ +/* 1-wire driver related settings. */ +/*===========================================================================*/ +/** + * @brief Enables strong pull up feature. + * @note Disabling this option saves both code and data space. + */ +#define ONEWIRE_USE_STRONG_PULLUP FALSE + +/** + * @brief Enables search ROM feature. + * @note Disabling this option saves both code and data space. + */ +#define ONEWIRE_USE_SEARCH_ROM TRUE + + /*===========================================================================*/ +/* EEProm driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables 24xx series I2C eeprom device driver. + * @note Disabling this option saves both code and data space. + */ +#define EEPROM_USE_EE24XX TRUE + /** + * @brief Enables 25xx series SPI eeprom device driver. + * @note Disabling this option saves both code and data space. + */ +#define EEPROM_USE_EE25XX TRUE + + +#endif /* HALCONF_COMMUNITY_H */ + +/** @} */ diff --git a/testhal/KINETIS/FRDM-KL25Z/USB_HID/main.c b/testhal/KINETIS/FRDM-KL25Z/USB_HID/main.c new file mode 100644 index 00000000..80aeb5d3 --- /dev/null +++ b/testhal/KINETIS/FRDM-KL25Z/USB_HID/main.c @@ -0,0 +1,91 @@ +/* + Copyright (C) 2016 Jonathan Struebel + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +#include "hal.h" + +#include "usbcfg.h" + +/* + * Blue LED blinker thread, times are in milliseconds. + */ +static THD_WORKING_AREA(waBlinkThread, 128); +static THD_FUNCTION(BlinkThread, arg) { + + (void)arg; + + chRegSetThreadName("blinker"); + while (true) { + systime_t time = USBD1.state == USB_ACTIVE ? 250 : 500; + palTogglePad(GPIO_LED_BLUE, PIN_LED_BLUE); + chThdSleepMilliseconds(time); + } +} + +/* + * Application entry point. + */ +int main(void) { + /* + * System initializations. + * - HAL initialization, this also initializes the configured device drivers + * and performs the board-specific initializations. + * - Kernel initialization, the main() function becomes a thread and the + * RTOS is active. + */ + halInit(); + chSysInit(); + + /* + * Turn off the RGB LED. + */ + palSetPad(GPIO_LED_RED, PIN_LED_RED); /* red */ + palSetPad(GPIO_LED_GREEN, PIN_LED_GREEN); /* green */ + palSetPad(GPIO_LED_BLUE, PIN_LED_BLUE); /* blue */ + + /* + * Initializes a USB HID driver. + */ + hidObjectInit(&UHD1); + hidStart(&UHD1, &usbhidcfg); + + /* + * Activates the USB driver and then the USB bus pull-up on D+. + * Note, a delay is inserted in order to not have to disconnect the cable + * after a reset. + */ + + usbDisconnectBus(usbhidcfg.usbp); + chThdSleepMilliseconds(1000); + usbStart(usbhidcfg.usbp, &usbcfg); + usbConnectBus(usbhidcfg.usbp); + + /* + * Creates the blinker thread. + */ + chThdCreateStatic(waBlinkThread, sizeof(waBlinkThread), NORMALPRIO, BlinkThread, NULL); + + while (true) { + if (usbhidcfg.usbp->state == USB_ACTIVE) { + uint8_t report; + size_t n = hidGetReport(0, &report, sizeof(report)); + hidWriteReport(&UHD1, &report, n); + n = hidReadReportt(&UHD1, &report, sizeof(report), TIME_IMMEDIATE); + if (n > 0) + hidSetReport(0, &report, n); + } + palTogglePad(GPIO_LED_RED, PIN_LED_RED); + chThdSleepMilliseconds(1000); + } +} diff --git a/testhal/KINETIS/FRDM-KL25Z/USB_HID/mcuconf.h b/testhal/KINETIS/FRDM-KL25Z/USB_HID/mcuconf.h new file mode 100644 index 00000000..57629efc --- /dev/null +++ b/testhal/KINETIS/FRDM-KL25Z/USB_HID/mcuconf.h @@ -0,0 +1,63 @@ +/* + ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#ifndef MCUCONF_H +#define MCUCONF_H + +#define KL2x_MCUCONF + +/* + * HAL driver system settings. + */ +#if 0 +/* PEE mode - 48MHz system clock driven by (8 MHz) external crystal. */ +#define KINETIS_MCG_MODE KINETIS_MCG_MODE_PEE +#define KINETIS_PLLCLK_FREQUENCY 96000000UL +#define KINETIS_SYSCLK_FREQUENCY 48000000UL +#endif + +#if 1 +/* crystal-less FEI mode - 48 MHz with internal 32.768 kHz crystal */ +#define KINETIS_MCG_MODE KINETIS_MCG_MODE_FEI +#define KINETIS_MCG_FLL_DMX32 1 /* Fine-tune for 32.768 kHz */ +#define KINETIS_MCG_FLL_DRS 1 /* 1464x FLL factor */ +#define KINETIS_SYSCLK_FREQUENCY 47972352UL /* 32.768 kHz * 1464 (~48 MHz) */ +#define KINETIS_CLKDIV1_OUTDIV1 1 /* do not divide system clock */ +#endif + +/* + * I2C driver system settings. + */ +#define KINETIS_I2C_USE_I2C1 TRUE + +/* + * SERIAL driver system settings. + */ +#define KINETIS_SERIAL_USE_UART0 TRUE + +/* + * USB driver settings + */ +#define KINETIS_USB_USE_USB0 TRUE +/* need to redefine this, since the default is for K20x */ +#define KINETIS_USB_USB0_IRQ_PRIORITY 2 + +/* + * Endianess of MCU + */ +#define ARCH_LITTLE_ENDIAN + +#endif /* MCUCONF_H */ diff --git a/testhal/KINETIS/FRDM-KL25Z/USB_HID/usbcfg.c b/testhal/KINETIS/FRDM-KL25Z/USB_HID/usbcfg.c new file mode 100644 index 00000000..30944b50 --- /dev/null +++ b/testhal/KINETIS/FRDM-KL25Z/USB_HID/usbcfg.c @@ -0,0 +1,418 @@ +/* + Copyright (C) 2016 Jonathan Struebel + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file usbcfg.c + * @brief USB driver config. + * + * @addtogroup USB + * @{ + */ +#include "hal.h" +#include "usbcfg.h" + +/* + * Endpoints to be used for USBD1. + */ +#define USBD1_DATA_REQUEST_EP 1 +#define USBD1_DATA_AVAILABLE_EP 1 + +/* + * USB HID Driver structure. + */ +USBHIDDriver UHD1; + +/* + * Data used for feedback + */ +uint8_t increment_var = 0; + +/* + * USB Device Descriptor. + */ +static const uint8_t hid_device_descriptor_data[18] = { + USB_DESC_DEVICE (0x0110, /* bcdUSB (1.1). */ + 0x00, /* bDeviceClass. */ + 0x00, /* bDeviceSubClass. */ + 0x00, /* bDeviceProtocol. */ + 0x40, /* bMaxPacketSize. */ + 0x0179, /* idVendor. */ + 0x0002, /* idProduct. */ + 0x0200, /* bcdDevice. */ + 1, /* iManufacturer. */ + 2, /* iProduct. */ + 3, /* iSerialNumber. */ + 1) /* bNumConfigurations. */ +}; + +/* + * Device Descriptor wrapper. + */ +static const USBDescriptor hid_device_descriptor = { + sizeof hid_device_descriptor_data, + hid_device_descriptor_data +}; + +/* + * Configuration Descriptor tree for a HID device + * + * The HID Specifications version 1.11 require the following order: + * - Configuration Descriptor + * - Interface Descriptor + * - HID Descriptor + * - Endpoints Descriptors + */ +#define HID_DESCRIPTOR_OFFSET 18 +#define HID_DESCRIPTOR_SIZE USB_DESC_HID_SIZE + +static const uint8_t hid_configuration_descriptor_data[41] = { + /* Configuration Descriptor.*/ + USB_DESC_CONFIGURATION(41, /* wTotalLength. */ + 0x01, /* bNumInterfaces. */ + 0x01, /* bConfigurationValue. */ + 0, /* iConfiguration. */ + 0xC0, /* bmAttributes (self powered). */ + 50), /* bMaxPower (100mA). */ + /* Interface Descriptor.*/ + USB_DESC_INTERFACE (0x00, /* bInterfaceNumber. */ + 0x00, /* bAlternateSetting. */ + 0x02, /* bNumEndpoints. */ + 0x03, /* bInterfaceClass (HID Interface + Class). */ + 0x00, /* bInterfaceSubClass (None). */ + 0x00, /* bInterfaceProtocol (None). */ + 0), /* iInterface. */ + /* HID Descriptor.*/ + USB_DESC_HID (0x0110, /* bcdHID. */ + 0x00, /* bCountryCode. */ + 0x01, /* bNumDescriptors. */ + 0x22, /* bDescriptorType (Report + Descriptor). */ + 34), /* wDescriptorLength. */ + /* Endpoint 1 Descriptor.*/ + USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP, /* bEndpointAddress.*/ + 0x03, /* bmAttributes (Interrupt). */ + 0x0040, /* wMaxPacketSize. */ + 0x0A), /* bInterval (10ms). */ + /* Endpoint 1 Descriptor.*/ + USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/ + 0x03, /* bmAttributes (Interrupt). */ + 0x0040, /* wMaxPacketSize. */ + 0x0A) /* bInterval (10ms). */ +}; + +/* + * Configuration Descriptor wrapper. + */ +static const USBDescriptor hid_configuration_descriptor = { + sizeof hid_configuration_descriptor_data, + hid_configuration_descriptor_data +}; + +/* + * HID Descriptor wrapper. + */ +static const USBDescriptor hid_descriptor = { + HID_DESCRIPTOR_SIZE, + &hid_configuration_descriptor_data[HID_DESCRIPTOR_OFFSET] +}; + +/* + * HID Report Descriptor + * + * This is the description of the format and the content of the + * different IN or/and OUT reports that your application can + * receive/send + * + * See "Device Class Definition for Human Interface Devices (HID)" + * (http://www.usb.org/developers/hidpage/HID1_11.pdf) for the + * detailed description of all the fields + */ +static const uint8_t hid_report_descriptor_data[] = { + USB_DESC_BYTE (0x06), /* Usage Page - */ + USB_DESC_WORD (0xFF00), /* Vendor Defined. */ + USB_DESC_BYTE (0x09), /* Usage - */ + USB_DESC_BYTE (0x01), /* Vendor Defined. */ + USB_DESC_BYTE (0xA1), /* Collection - */ + USB_DESC_BYTE (0x01), /* Application. */ + + USB_DESC_BYTE (0x09), /* Usage - */ + USB_DESC_BYTE (0x01), /* Vendor Defined. */ + USB_DESC_BYTE (0x15), /* Logical Minimum - */ + USB_DESC_BYTE (0x00), /* 0. */ + USB_DESC_BYTE (0x26), /* Logical Maximum - */ + USB_DESC_WORD (0x00FF), /* 255. */ + USB_DESC_BYTE (0x75), /* Report size - */ + USB_DESC_BYTE (0x08), /* 8 bits. */ + USB_DESC_BYTE (0x95), /* Report count - */ + USB_DESC_BYTE (0x01), /* 1. */ + USB_DESC_BYTE (0x81), /* Input - */ + USB_DESC_BYTE (0x02), /* Data, Variable, Absolute. */ + + USB_DESC_BYTE (0x09), /* Usage - */ + USB_DESC_BYTE (0x01), /* Vendor Defined. */ + USB_DESC_BYTE (0x15), /* Logical Minimum - */ + USB_DESC_BYTE (0x00), /* 0. */ + USB_DESC_BYTE (0x26), /* Logical Maximum - */ + USB_DESC_WORD (0x00FF), /* 255. */ + USB_DESC_BYTE (0x75), /* Report Size - */ + USB_DESC_BYTE (0x08), /* 8 bits. */ + USB_DESC_BYTE (0x95), /* Report Count - */ + USB_DESC_BYTE (0x01), /* 1. */ + USB_DESC_BYTE (0x91), /* Output - */ + USB_DESC_BYTE (0x02), /* Data, Variable, Absolute. */ + + USB_DESC_BYTE (0xC0) /* End Collection. */ +}; + +/* + * HID Report Descriptor wrapper + */ +static const USBDescriptor hid_report_descriptor = { + sizeof hid_report_descriptor_data, + hid_report_descriptor_data +}; + +/* + * U.S. English language identifier. + */ +static const uint8_t hid_string0[] = { + USB_DESC_BYTE(2+2*1), /* bLength. */ + USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */ + USB_DESC_WORD(0x0409) /* wLANGID (U.S. English). */ +}; + +/* + * Vendor string. + */ +static const uint8_t hid_string1[] = { + USB_DESC_BYTE(2+2*7), /* bLength. */ + USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */ + 'N', 0, 'o', 0, 'p', 0, 'e', 0, 'L', 0, 'a', 0, 'b', 0, +}; + +/* + * Device Description string. + */ +static const uint8_t hid_string2[] = { + USB_DESC_BYTE(2+5*2), /* bLength. */ + USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */ + 'C', 0, 'h', 0, 'T', 0, 's', 0, 'y', 0, +}; + +/* + * Serial Number string. + */ +static const uint8_t hid_string3[] = { + USB_DESC_BYTE(2+2*3), /* bLength. */ + USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */ + '0' + CH_KERNEL_MAJOR, 0, + '0' + CH_KERNEL_MINOR, 0, + '0' + CH_KERNEL_PATCH, 0 +}; + +/* + * Strings wrappers array. + */ +static const USBDescriptor hid_strings[] = { + {sizeof hid_string0, hid_string0}, + {sizeof hid_string1, hid_string1}, + {sizeof hid_string2, hid_string2}, + {sizeof hid_string3, hid_string3} +}; + +/* + * Handles the GET_DESCRIPTOR callback. All required descriptors must be + * handled here. + */ +static const USBDescriptor *get_descriptor(USBDriver *usbp, + uint8_t dtype, + uint8_t dindex, + uint16_t lang) { + (void)usbp; + (void)lang; + switch (dtype) { + case USB_DESCRIPTOR_DEVICE: + return &hid_device_descriptor; + case USB_DESCRIPTOR_CONFIGURATION: + return &hid_configuration_descriptor; + case USB_DESCRIPTOR_STRING: + if (dindex < 4) + return &hid_strings[dindex]; + case USB_DESCRIPTOR_INTERFACE: + break; + case USB_DESCRIPTOR_ENDPOINT: + break; + case USB_DESCRIPTOR_HID: + return &hid_descriptor; + case HID_REPORT: + return &hid_report_descriptor; + default: + break; + } + return NULL; +} + +/** + * @brief IN EP1 state. + */ +static USBInEndpointState ep1instate; + +/** + * @brief OUT EP1 state. + */ +static USBOutEndpointState ep1outstate; + +/** + * @brief EP1 initialization structure (both IN and OUT). + */ +static const USBEndpointConfig ep1config = { + USB_EP_MODE_TYPE_INTR, + NULL, + hidDataTransmitted, + hidDataReceived, + 0x0040, + 0x0040, + &ep1instate, + &ep1outstate, + 2, + NULL +}; + +/* + * Handles the USB driver global events. + */ +static void usb_event(USBDriver *usbp, usbevent_t event) { +// (void)usbp; + switch (event) { + case USB_EVENT_RESET: + return; + case USB_EVENT_ADDRESS: + return; + case USB_EVENT_CONFIGURED: + osalSysLockFromISR(); + + /* Enables the endpoints specified into the configuration. + Note, this callback is invoked from an ISR so I-Class functions + must be used.*/ + usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config); + + /* Resetting the state of the CDC subsystem.*/ + hidConfigureHookI(&UHD1); + + osalSysUnlockFromISR(); + return; + case USB_EVENT_SUSPEND: + return; + case USB_EVENT_WAKEUP: + return; + case USB_EVENT_STALLED: + return; + } + return; +} + +static bool req_handler(USBDriver *usbp) { + size_t n; + + if ((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) { + switch (usbp->setup[1]) { + case HID_GET_REPORT: + n = hidGetReport(0, &increment_var, sizeof(increment_var)); + usbSetupTransfer(usbp, &increment_var, n, NULL); + return true; + default: + return hidRequestsHook(usbp); + } + } + return hidRequestsHook(usbp); +} + +#if 0 +/* + * Handles the USB driver global events. + */ +static void sof_handler(USBDriver *usbp) { + + (void)usbp; + + osalSysLockFromISR(); + sduSOFHookI(&SDU1); + osalSysUnlockFromISR(); +} +#endif + +/** + * @brief Generate HID Report + * @details This function generates the data for an HID report so + * that it can be transferred to the host. + * + * @param[in] id report ID + * @param[out] bp data buffer pointer + * @param[in] n the maximum number of bytes for data buffer + * @return number of bytes of report in data buffer + */ +size_t hidGetReport(uint8_t id, uint8_t *bp, size_t n) { + + (void) id; + (void) n; + + increment_var++; + *bp = increment_var; + return sizeof(increment_var); +} + +/** + * @brief Set HID Report + * @details This function sets the data for an HID report + * that was transferred from the host. + * + * @param[in] id report ID + * @param[in] bp data buffer pointer + * @param[in] n the number of bytes in data buffer + * @return The operation status. + * @retval MSG_OK if the report was set. + */ +msg_t hidSetReport(uint8_t id, uint8_t *bp, size_t n) { + + (void) id; + (void) n; + + increment_var = *bp; + return MSG_OK; +} + +/* + * USB driver configuration. + */ +const USBConfig usbcfg = { + usb_event, + get_descriptor, + req_handler, + NULL + /*sof_handler*/ +}; + +/* + * USB HID driver configuration. + */ +const USBHIDConfig usbhidcfg = { + &USBD1, + USBD1_DATA_REQUEST_EP, + USBD1_DATA_AVAILABLE_EP +}; + +/** @} */ diff --git a/testhal/KINETIS/FRDM-KL25Z/USB_HID/usbcfg.h b/testhal/KINETIS/FRDM-KL25Z/USB_HID/usbcfg.h new file mode 100644 index 00000000..3d69417f --- /dev/null +++ b/testhal/KINETIS/FRDM-KL25Z/USB_HID/usbcfg.h @@ -0,0 +1,45 @@ +/* + Copyright (C) 2016 Jonathan Struebel + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file usbcfg.h + * @brief USB driver config header. + * + * @addtogroup USB + * @{ + */ + +#ifndef USBCFG_H +#define USBCFG_H + +#include "hal_usb_hid.h" + +extern const USBConfig usbcfg; +extern const USBHIDConfig usbhidcfg; +extern USBHIDDriver UHD1; + +#ifdef __cplusplus +extern "C" { +#endif + size_t hidGetReport(uint8_t id, uint8_t *bp, size_t n); + msg_t hidSetReport(uint8_t id, uint8_t *bp, size_t n); +#ifdef __cplusplus +} +#endif + +#endif /* USBCFG_H */ + +/** @} */