diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-08-23 17:08:59 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-08-23 17:12:21 +0200 |
commit | 3061ecca3d0fdfb87dabbf5f63c9e06c2a30f53a (patch) | |
tree | ab49cc16ed0b853452c5c2ed2d3042416d628986 /thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd | |
download | iot-sensors-master.tar.gz iot-sensors-master.tar.bz2 iot-sensors-master.tar.xz iot-sensors-master.zip |
Diffstat (limited to 'thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd')
53 files changed, 20665 insertions, 0 deletions
diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd.c new file mode 100644 index 0000000..e5cbcf8 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd.c @@ -0,0 +1,1766 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(APP_USBD) + +#include "app_usbd.h" +#include "app_usbd_core.h" +#include "nrf_power.h" +#include "nrf_drv_clock.h" +#include "nrf_drv_power.h" +#if APP_USBD_CONFIG_EVENT_QUEUE_ENABLE +#include "nrf_atfifo.h" +#include "nrf_atomic.h" +#endif + +#define NRF_LOG_MODULE_NAME app_usbd + +#if APP_USBD_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL APP_USBD_CONFIG_LOG_LEVEL +#define NRF_LOG_INFO_COLOR APP_USBD_CONFIG_INFO_COLOR +#define NRF_LOG_DEBUG_COLOR APP_USBD_CONFIG_DEBUG_COLOR +#else //APP_USBD_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL 0 +#endif //APP_USBD_CONFIG_LOG_ENABLED +#include "nrf_log.h" +NRF_LOG_MODULE_REGISTER(); + + +/* Base variables tests */ + +/* Check event of app_usbd_event_type_t enumerator */ +STATIC_ASSERT((int32_t)APP_USBD_EVT_FIRST_POWER == (int32_t)NRF_DRV_USBD_EVT_CNT); +STATIC_ASSERT(sizeof(app_usbd_event_type_t) == sizeof(nrf_drv_usbd_event_type_t)); + +STATIC_ASSERT(sizeof(app_usbd_descriptor_header_t) == 2); +STATIC_ASSERT(sizeof(app_usbd_descriptor_device_t) == 18); +STATIC_ASSERT(sizeof(app_usbd_descriptor_configuration_t) == 9); +STATIC_ASSERT(sizeof(app_usbd_descriptor_iface_t) == 9); +STATIC_ASSERT(sizeof(app_usbd_descriptor_ep_t) == 7); +STATIC_ASSERT(sizeof(app_usbd_descriptor_iad_t) == 8); + +STATIC_ASSERT(sizeof(app_usbd_setup_t) == sizeof(nrf_drv_usbd_setup_t)); + +/** + * @internal + * @defgroup app_usbd_internals USBD library internals + * @ingroup app_usbd + * + * Internal variables, auxiliary macros and functions of USBD library. + * @{ + */ + +#if (APP_USBD_PROVIDE_SOF_TIMESTAMP) || defined(__SDK_DOXYGEN__) +/** + * @brief The last received frame number. + */ +static uint16_t m_last_frame; +#endif + +/** + * @brief Variable type for endpoint configuration + * + * Each endpoint would have assigned this type of configuration structure. + */ +typedef struct +{ + /** + * @brief The class instance + * + * The pointer to the class instance that is connected to the endpoint. + */ + app_usbd_class_inst_t const * p_cinst; + + /** + * @brief Endpoint event handler. + * + * Event handler for the endpoint. + * It is set to event handler for the class instance during connection by default, + * but it can be then updated for as a reaction for @ref APP_USBD_EVT_ATTACHED event. + * This way we can speed up the interpretation of endpoint related events. + */ + app_usbd_ep_event_handler_t event_handler; +}app_usbd_ep_conf_t; + + +/** + * @brief Internal event with SOF counter. + */ +typedef struct +{ + app_usbd_internal_evt_t evt; //!< Internal event type + +#if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) \ + || defined(__SDK_DOXYGEN__) + uint16_t sof_cnt; //!< Number of the SOF events that appears before current event + uint16_t start_frame; //!< Number of the SOF frame that starts this event +#endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + +} app_usbd_internal_queue_evt_t; + +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__) +/** + * @brief Event queue + * + * The queue with events to be processed + */ +NRF_ATFIFO_DEF(m_event_queue, app_usbd_internal_queue_evt_t, APP_USBD_CONFIG_EVENT_QUEUE_SIZE); + +#if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) \ + || defined(__SDK_DOXYGEN__) + +/** @brief SOF events counter */ +static nrf_atomic_u32_t m_sof_events_cnt; + +/** @brief SOF Frame counter */ +static uint16_t m_event_frame; + +// Limit of SOF events stacked until warning message. +#define APP_USBD_SOF_WARNING_LIMIT 500 +#endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + // || defined(__SDK_DOXYGEN__) + +#endif + +/** + * @brief Instances connected with IN endpoints + * + * Array of instance pointers connected with every IN endpoint. + * @sa m_epout_instances + */ +static app_usbd_ep_conf_t m_epin_conf[NRF_USBD_EPIN_CNT]; + +/** + * @brief Instances connected with OUT endpoints + * + * Array of instance pointers connected with every OUT endpoint. + * @sa m_epin_instances + */ +static app_usbd_ep_conf_t m_epout_conf[NRF_USBD_EPIN_CNT]; + +/** + * @brief Beginning of classes list + * + * All enabled in current configuration instances are connected into + * a single linked list chain. + * This variable points to first element. + * Core class instance (connected to endpoint 0) is not listed here. + */ +static app_usbd_class_inst_t const * m_p_first_cinst; + +/** + * @brief Classes list that requires SOF events + * + * @todo RK Implement and documentation + */ +static app_usbd_class_inst_t const * m_p_first_sof_cinst; + +/** + * @brief Default configuration (when NULL is passed to @ref app_usbd_init). + */ +static const app_usbd_config_t m_default_conf = { +#if (!(APP_USBD_CONFIG_EVENT_QUEUE_ENABLE)) || defined(__SDK_DOXYGEN__) + .ev_handler = app_usbd_event_execute, +#endif +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__) + .ev_isr_handler = NULL, +#endif + .ev_state_proc = NULL, + .enable_sof = false +}; + +/** + * @brief SUSPEND state machine states + * + * The enumeration of internal SUSPEND state machine states. + */ +typedef enum +{ + SUSTATE_STOPPED, /**< The USB driver was not started */ + SUSTATE_STARTED, /**< The USB driver was started - waiting for USB RESET */ + SUSTATE_ACTIVE, /**< Active state */ + SUSTATE_SUSPENDING, /**< Suspending - waiting for the user to acknowledge */ + SUSTATE_SUSPEND, /**< Suspended */ + SUSTATE_RESUMING, /**< Resuming - waiting for clock */ + SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ, /**< Waking up - waiting for clock and WUREQ from driver */ + SUSTATE_WAKINGUP_WAITING_HFCLK, /**< Waking up - waiting for HFCLK (WUREQ detected) */ + SUSTATE_WAKINGUP_WAITING_WREQ, /**< Waking up - waiting for WREQ (HFCLK active) */ +}app_usbd_sustate_t; + +/** + * @brief Current suspend state + * + * The state of the suspend state machine + */ +static app_usbd_sustate_t m_sustate; + +/** + * @brief Remote wake-up register/unregister + * + * Counter incremented when appended instance required remote wake-up functionality. + * It should be decremented when the class is removed. + * When this counter is not zero, remote wake-up functionality is activated inside core. + */ +static uint8_t m_rwu_registered_counter; + +/** + * @brief Current configuration. + */ +static app_usbd_config_t m_current_conf; + +/** + * @brief Class interface call: event handler + * + * @ref app_usbd_class_interface_t::event_handler + * + * @param[in] p_cinst Class instance + * @param[in] p_event Event passed to class instance + * + * @return Standard error code @ref ret_code_t + * @retval NRF_SUCCESS event handled successfully + * @retval NRF_ERROR_NOT_SUPPORTED unsupported event + * */ +static inline ret_code_t class_event_handler(app_usbd_class_inst_t const * const p_cinst, + app_usbd_complex_evt_t const * const p_event) +{ + ASSERT(p_cinst != NULL); + ASSERT(p_cinst->p_class_methods != NULL); + ASSERT(p_cinst->p_class_methods->event_handler != NULL); + return p_cinst->p_class_methods->event_handler(p_cinst, p_event); +} + +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__) +/** + * @brief User event handler call (passed via configuration). + * + * @param p_event Handler of an event that is going to be added into queue. + * @param queued The event is visible in the queue. + */ +static inline void user_event_handler(app_usbd_internal_evt_t const * const p_event, bool queued) +{ + if ((m_current_conf.ev_isr_handler) != NULL) + { + m_current_conf.ev_isr_handler(p_event, queued); + } +} +#endif + +/** + * @brief User event processor call (passed via configuration) + * + * @param event Event type. + */ +static inline void user_event_state_proc(app_usbd_event_type_t event) +{ + if ((m_current_conf.ev_state_proc) != NULL) + { + m_current_conf.ev_state_proc(event); + } +} + +/** + * @brief Find a specified descriptor + * + * @param[in] p_cinst Class instance + * @param[in] desc_type Descriptor type @ref app_usbd_descriptor_t + * @param[in] desc_index Descriptor index + * @param[out] p_desc Pointer to escriptor + * @param[out] p_desc_len Length of descriptor + * + * @return Standard error code @ref ret_code_t + * @retval NRF_SUCCESS descriptor successfully found + * @retval NRF_ERROR_NOT_FOUND descriptor not found + * */ +ret_code_t app_usbd_class_descriptor_find(app_usbd_class_inst_t const * const p_cinst, + uint8_t desc_type, + uint8_t desc_index, + uint8_t * p_desc, + size_t * p_desc_len) +{ + app_usbd_class_descriptor_ctx_t siz; + APP_USBD_CLASS_DESCRIPTOR_INIT(&siz); + uint32_t total_size = 0; + while(p_cinst->p_class_methods->feed_descriptors(&siz, p_cinst, NULL, sizeof(uint8_t))) + { + total_size++; + } + + uint8_t cur_len = 0; + uint32_t cur_size = 0; + + uint8_t index = 0; + app_usbd_class_descriptor_ctx_t descr; + APP_USBD_CLASS_DESCRIPTOR_INIT(&descr); + + while(cur_size < total_size) + { + /* First byte of a descriptor is its size */ + UNUSED_RETURN_VALUE(p_cinst->p_class_methods->feed_descriptors(&descr, + p_cinst, + &cur_len, + sizeof(uint8_t))); + + /* Second byte is type of the descriptor */ + uint8_t type; + UNUSED_RETURN_VALUE(p_cinst->p_class_methods->feed_descriptors(&descr, + p_cinst, + &type, + sizeof(uint8_t))); + + if(type == desc_type) + { + if(index == desc_index) + { + /* Copy the length of descriptor to *p_desc_len */ + *p_desc_len = cur_len; + /* Two first bytes of descriptor have already been fed - copy them to *p_desc */ + *p_desc++ = cur_len; + *p_desc++ = desc_type; + /* Copy the rest of descriptor to *p_desc */ + UNUSED_RETURN_VALUE(p_cinst->p_class_methods->feed_descriptors(&descr, + p_cinst, + p_desc, + cur_len-2)); + return NRF_SUCCESS; + } + else + { + index++; + } + } + /* Fast-forward through unmatched descriptor */ + UNUSED_RETURN_VALUE(p_cinst->p_class_methods->feed_descriptors(&descr, + p_cinst, + NULL, + cur_len-2)); + cur_size += cur_len; + } + return NRF_ERROR_NOT_FOUND; +} + +/** + * @brief Access into selected endpoint configuration structure + * + * @param ep Endpoint address + * @return A pointer to the endpoint configuration structure + * + * @note This function would assert when endpoint number is not correct and debugging is enabled. + */ +static app_usbd_ep_conf_t * app_usbd_ep_conf_access(nrf_drv_usbd_ep_t ep) +{ + if (NRF_USBD_EPIN_CHECK(ep)) + { + uint8_t nr = NRF_USBD_EP_NR_GET(ep); + ASSERT(nr < NRF_USBD_EPIN_CNT); + return &m_epin_conf[nr]; + } + else + { + uint8_t nr = NRF_USBD_EP_NR_GET(ep); + ASSERT(nr < NRF_USBD_EPOUT_CNT); + return &m_epout_conf[nr]; + } +} + +/** + * @brief Accessing instance connected with selected endpoint + * + * @param ep Endpoint number + * + * @return The pointer to the instance connected with endpoint + */ +static inline app_usbd_class_inst_t const * app_usbd_ep_instance_get(nrf_drv_usbd_ep_t ep) +{ + return app_usbd_ep_conf_access(ep)->p_cinst; +} + +/** + * @brief Connect instance with selected endpoint + * + * This function configures instance connected to endpoint but also sets + * default event handler function pointer. + * + * @param ep Endpoint number + * @param p_cinst The instance to connect into the selected endpoint. + * NULL if endpoint is going to be disconnected. + * + * @note Disconnecting EP0 is not allowed and protected by assertion. + */ +static void app_usbd_ep_instance_set(nrf_drv_usbd_ep_t ep, app_usbd_class_inst_t const * p_cinst) +{ + app_usbd_ep_conf_t * p_ep_conf = app_usbd_ep_conf_access(ep); + /* Set instance and default event handler */ + p_ep_conf->p_cinst = p_cinst; + if (p_cinst == NULL) + { + ASSERT((ep != NRF_DRV_USBD_EPOUT0) && (ep != NRF_DRV_USBD_EPIN0)); /* EP0 should never be disconnected */ + p_ep_conf->event_handler = NULL; + } + else + { + p_ep_conf->event_handler = p_cinst->p_class_methods->event_handler; + } +} + +/** + * @brief Call the core handler + * + * Core instance is special kind of instance that is connected only to endpoint 0. + * It is not present in instance list. + * This auxiliary function makes future changes easier. + * Just call the event instance for core module here. + */ +static inline ret_code_t app_usbd_core_handler_call(app_usbd_internal_evt_t const * const p_event) +{ + return m_epout_conf[0].event_handler( + m_epout_conf[0].p_cinst, + (app_usbd_complex_evt_t const *)p_event); +} + + + +/** + * @brief Add event for execution + * + * Dependent on configuration event would be executed in place or would be added into queue + * to be executed later. + * + * @param p_event Event to be executed + */ +static inline void app_usbd_event_add(app_usbd_internal_evt_t const * const p_event) +{ +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) + +#if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + if (p_event->app_evt.type == APP_USBD_EVT_DRV_SOF) + { + CRITICAL_REGION_ENTER(); + if (m_sof_events_cnt == 0) + { + m_event_frame = p_event->drv_evt.data.sof.framecnt; + } + UNUSED_RETURN_VALUE(nrf_atomic_u32_add(&m_sof_events_cnt, 1)); + CRITICAL_REGION_EXIT(); + + user_event_handler(p_event, true); + if (m_sof_events_cnt == APP_USBD_SOF_WARNING_LIMIT) + { + NRF_LOG_WARNING("Stacked over %d SOF events.", APP_USBD_SOF_WARNING_LIMIT); + } + return; + } +#endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + + if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_INTERRUPT) + { + if (p_event->app_evt.type == APP_USBD_EVT_DRV_SOF) + { + user_event_handler(p_event, false); + app_usbd_event_execute(p_event); + return; + } + } + + nrf_atfifo_item_put_t cx; + app_usbd_internal_queue_evt_t * p_event_item = nrf_atfifo_item_alloc(m_event_queue, &cx); + + if (NULL != p_event_item) + { + bool visible; + p_event_item->evt = *p_event; + +#if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + CRITICAL_REGION_ENTER(); + p_event_item->start_frame = m_event_frame - m_sof_events_cnt + 1; + p_event_item->sof_cnt = nrf_atomic_u32_fetch_store(&m_sof_events_cnt, 0); + CRITICAL_REGION_EXIT(); +#endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + + visible = nrf_atfifo_item_put(m_event_queue, &cx); + user_event_handler(p_event, visible); + } + else + { + NRF_LOG_ERROR("Event queue full."); + } +#else + m_current_conf.ev_handler(p_event); +#endif +} + +/** + * @brief Power event handler + * + * The function that pushes power events into the queue + * @param p_event + */ +#if APP_USBD_CONFIG_POWER_EVENTS_PROCESS +static void app_usbd_power_event_handler(nrf_drv_power_usb_evt_t event) +{ + switch(event) + { + case NRF_DRV_POWER_USB_EVT_DETECTED: + { + static const app_usbd_internal_evt_t ev = { + .type = APP_USBD_EVT_POWER_DETECTED + }; + app_usbd_event_add(&ev); + break; + } + case NRF_DRV_POWER_USB_EVT_REMOVED: + { + static const app_usbd_internal_evt_t ev = { + .type = APP_USBD_EVT_POWER_REMOVED + }; + app_usbd_event_add(&ev); + break; + } + case NRF_DRV_POWER_USB_EVT_READY: + { + static const app_usbd_internal_evt_t ev = { + .type = APP_USBD_EVT_POWER_READY + }; + app_usbd_event_add(&ev); + break; + } + default: + ASSERT(false); + } +} +#endif + +/** + * @brief Event handler + * + * The function that pushes the event into the queue + * @param p_event + */ +static void app_usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event) +{ + app_usbd_event_add((app_usbd_internal_evt_t const *)p_event); +} + +/** + * @brief HF clock ready event handler + * + * Function that is called when high frequency clock is started + * + * @param event Event type that comes from clock driver + */ +static void app_usbd_hfclk_ready(nrf_drv_clock_evt_type_t event) +{ + ASSERT(NRF_DRV_CLOCK_EVT_HFCLK_STARTED == event); + static const app_usbd_evt_t evt_data = { + .type = APP_USBD_EVT_HFCLK_READY + }; + app_usbd_event_add((app_usbd_internal_evt_t const * )&evt_data); +} + +/** + * @brief Check if the HFCLK was requested in selected suspend state machine state + * + * + * @param sustate State to be checked + * + * @retval true High frequency clock was requested in selected state + * @retval false High frequency clock was released in selected state + */ +static inline bool app_usbd_sustate_with_requested_hfclk(app_usbd_sustate_t sustate) +{ + switch(sustate) + { + case SUSTATE_STOPPED: return false; + case SUSTATE_STARTED: return false; + case SUSTATE_ACTIVE: return true; + case SUSTATE_SUSPENDING: return false; + case SUSTATE_SUSPEND: return false; + case SUSTATE_RESUMING: return true; + case SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ: return true; + case SUSTATE_WAKINGUP_WAITING_HFCLK: return true; + case SUSTATE_WAKINGUP_WAITING_WREQ: return true; + default: + return false; + } +} + +/** + * @brief Check it the HFCLK is running in selected suspend state machine state + * + * @param sustate State to be checked + * + * @retval true High frequency clock is running in selected state + * @retval false High frequency clock is released in selected state + */ +static inline bool app_usbd_sustate_with_running_hfclk(app_usbd_sustate_t sustate) +{ + switch(sustate) + { + case SUSTATE_STOPPED: return false; + case SUSTATE_STARTED: return false; + case SUSTATE_ACTIVE: return true; + case SUSTATE_SUSPENDING: return false; + case SUSTATE_SUSPEND: return false; + case SUSTATE_RESUMING: return false; + case SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ: return false; + case SUSTATE_WAKINGUP_WAITING_HFCLK: return false; + case SUSTATE_WAKINGUP_WAITING_WREQ: return true; + default: + return false; + } +} + +/** + * @brief Get current suspend state machine state + * + * @return The state of the suspend state machine + */ +static inline app_usbd_sustate_t sustate_get(void) +{ + return m_sustate; +} + +/** + * @brief Set current suspend state machine state + * + * @param sustate The requested state of the state machine + */ +static inline void sustate_set(app_usbd_sustate_t sustate) +{ + if (app_usbd_sustate_with_requested_hfclk(sustate) != app_usbd_sustate_with_requested_hfclk(m_sustate)) + { + if (app_usbd_sustate_with_requested_hfclk(sustate)) + { + static nrf_drv_clock_handler_item_t clock_handler_item = + { + .event_handler = app_usbd_hfclk_ready + }; + nrf_drv_clock_hfclk_request(&clock_handler_item); + } + else + { + nrf_drv_clock_hfclk_release(); + } + } + if (app_usbd_sustate_with_running_hfclk(sustate) != app_usbd_sustate_with_running_hfclk(m_sustate)) + { + if (app_usbd_sustate_with_running_hfclk(sustate)) + { + nrf_drv_usbd_active_irq_config(); + } + else + { + nrf_drv_usbd_suspend_irq_config(); + } + } + m_sustate = sustate; +} + +/** + * @brief Default selection function for interface + * + * This function just enables and clears interface endpoints + * + * @param p_inst See the documentation for @ref app_usbd_iface_select + * @param iface_idx See the documentation for @ref app_usbd_iface_select + * @param alternate See the documentation for @ref app_usbd_iface_select + * @return + */ +static inline ret_code_t default_iface_select( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx, + uint8_t alternate) +{ + ASSERT(iface_idx <= app_usbd_class_iface_count_get(p_inst)); + + if (alternate != 0) + { + return NRF_ERROR_INVALID_PARAM; + } + + app_usbd_class_iface_conf_t const * p_iface = app_usbd_class_iface_get(p_inst, iface_idx); + uint8_t ep_count = app_usbd_class_iface_ep_count_get(p_iface); + + for (uint8_t i = 0; i < ep_count; ++i) + { + /* Enable every endpoint */ + app_usbd_class_ep_conf_t const * p_ep = app_usbd_class_iface_ep_get(p_iface, i); + app_usbd_ep_enable(p_ep->address); + } + return NRF_SUCCESS; +} + +/** + * @brief Default deselection function for interface + * + * This function just disables all interface endpoints. + * + * @param p_inst See the documentation for @ref app_usbd_iface_deselect + * @param iface_idx See the documentation for @ref app_usbd_iface_deselect + */ +static inline void default_iface_deselect( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx) +{ + ASSERT(iface_idx <= app_usbd_class_iface_count_get(p_inst)); + + app_usbd_class_iface_conf_t const * p_iface = app_usbd_class_iface_get(p_inst, iface_idx); + uint8_t ep_count = app_usbd_class_iface_ep_count_get(p_iface); + + for (uint8_t i = 0; i < ep_count; ++i) + { + /* Disable every endpoint */ + app_usbd_class_ep_conf_t const * p_ep = app_usbd_class_iface_ep_get(p_iface, i); + app_usbd_ep_disable(p_ep->address); + } +} + + +/** @} */ + +#if (APP_USBD_PROVIDE_SOF_TIMESTAMP) || defined(__SDK_DOXYGEN__) +uint32_t app_usbd_sof_timestamp_get(void) +{ + return m_last_frame; +} +#endif + +ret_code_t app_usbd_init(app_usbd_config_t const * p_config) +{ + ASSERT(nrf_drv_clock_init_check()); + ret_code_t ret; + +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__) + ret = NRF_ATFIFO_INIT(m_event_queue); + if (NRF_SUCCESS != ret) + { + return NRF_ERROR_INTERNAL; + } +#endif + + /* This is called at the beginning to secure multiple calls to init function */ + ret = nrf_drv_usbd_init(app_usbd_event_handler); + if (NRF_SUCCESS != ret) + { + return ret; + } + + /* Clear the variables */ + m_sustate = SUSTATE_STOPPED; + m_p_first_cinst = NULL; + m_p_first_sof_cinst = NULL; + memset(m_epin_conf , 0, sizeof(m_epin_conf )); + memset(m_epout_conf, 0, sizeof(m_epout_conf)); + /* Save the new configuration */ + if (p_config == NULL) + { + m_current_conf = m_default_conf; + } + else + { + m_current_conf = *p_config; + } + +#if (!(APP_USBD_CONFIG_EVENT_QUEUE_ENABLE)) + if(m_current_conf.ev_handler == NULL) + { + m_current_conf.ev_handler = m_default_conf.ev_handler; + } +#endif + +#if APP_USBD_CONFIG_POWER_EVENTS_PROCESS + ret = nrf_drv_power_init(NULL); + if ((ret != NRF_SUCCESS) && (ret != NRF_ERROR_MODULE_ALREADY_INITIALIZED)) + { + /* This should never happen */ + APP_ERROR_HANDLER(ret); + } +#endif + + /*Pin core class to required endpoints*/ + uint8_t iface_idx; + app_usbd_class_iface_conf_t const * p_iface; + app_usbd_class_inst_t const * const p_inst = app_usbd_core_instance_access(); + iface_idx = 0; + while ((p_iface = app_usbd_class_iface_get(p_inst, iface_idx++)) != NULL) + { + uint8_t ep_idx = 0; + app_usbd_class_ep_conf_t const * p_ep; + while ((p_ep = app_usbd_class_iface_ep_get(p_iface, ep_idx++)) != NULL) + { + app_usbd_ep_instance_set(app_usbd_class_ep_address_get(p_ep), p_inst); + } + } + + /* Successfully attached */ + const app_usbd_evt_t evt_data = { + .type = APP_USBD_EVT_INST_APPEND + }; + + ret = class_event_handler(p_inst, (app_usbd_complex_evt_t const *)(&evt_data)); + if (NRF_SUCCESS != ret) + { + UNUSED_RETURN_VALUE(nrf_drv_usbd_uninit()); + return ret; + } + + return NRF_SUCCESS; +} + + +ret_code_t app_usbd_uninit(void) +{ +#if APP_USBD_CONFIG_POWER_EVENTS_PROCESS + nrf_drv_power_usbevt_uninit(); +#endif + + /* We get this error at very beginning but it would be used at the end of the function */ + const ret_code_t ret = nrf_drv_usbd_uninit(); + + /* Unchain instance list */ + app_usbd_class_inst_t const * * pp_inst; + pp_inst = &m_p_first_cinst; + while (NULL != (*pp_inst)) + { + app_usbd_class_inst_t const * * pp_next = &app_usbd_class_data_access(*pp_inst)->p_next; + (*pp_inst) = NULL; + pp_inst = pp_next; + } + + /* Unchain SOF list */ + pp_inst = &m_p_first_sof_cinst; + while (NULL != (*pp_inst)) + { + app_usbd_class_inst_t const * * pp_next = &app_usbd_class_data_access(*pp_inst)->p_sof_next; + (*pp_inst) = NULL; + pp_inst = pp_next; + } + + /* Clear all endpoints configurations */ + memset(m_epin_conf , 0, sizeof(m_epin_conf )); + memset(m_epout_conf, 0, sizeof(m_epout_conf)); + /* Clear current configuration */ + memset(&m_current_conf, 0, sizeof(m_current_conf)); + + return ret; +} + + +#if APP_USBD_CONFIG_POWER_EVENTS_PROCESS +ret_code_t app_usbd_power_events_enable(void) +{ + if (!nrf_drv_usbd_is_initialized() || nrf_drv_usbd_is_enabled()) + { + return NRF_ERROR_INVALID_STATE; + } + + ASSERT((!APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || (USBD_CONFIG_IRQ_PRIORITY == POWER_CONFIG_IRQ_PRIORITY)); + + ret_code_t ret; + static const nrf_drv_power_usbevt_config_t config = + { + .handler = app_usbd_power_event_handler + }; + + ret = nrf_drv_power_usbevt_init(&config); + APP_ERROR_CHECK(ret); + + return NRF_SUCCESS; +} +#endif /* APP_USBD_CONFIG_POWER_EVENTS_PROCESS */ + + +void app_usbd_enable(void) +{ + nrf_drv_usbd_enable(); +} + + +void app_usbd_disable(void) +{ + ASSERT(!nrf_drv_usbd_is_started()); + nrf_drv_usbd_disable(); +} + + +void app_usbd_start(void) +{ + ASSERT(nrf_drv_usbd_is_enabled()); + + /* Check if interface numbers are in correct order */ + if (APP_USBD_CONFIG_LOG_ENABLED) + { + uint8_t next_iface = 0; + for (app_usbd_class_inst_t const * * pp_inst = &m_p_first_cinst; + (*pp_inst) != NULL; + pp_inst = &(app_usbd_class_data_access(*pp_inst)->p_next)) + { + uint8_t iface_idx = 0; + app_usbd_class_iface_conf_t const * p_iface; + while (NULL != (p_iface = app_usbd_class_iface_get(*pp_inst, iface_idx++))) + { + if (p_iface->number != next_iface) + { + NRF_LOG_WARNING("Unexpected interface number, expected %d, got %d", + next_iface, + p_iface->number); + } + ++next_iface; + } + } + } + + /* Power should be already enabled - wait just in case if user calls + * app_usbd_start just after app_usbd_enable without waiting for the event. */ + while (!nrf_power_usbregstatus_outrdy_get()) + { + /* Wait for the power but terminate the function if USBD power disappears */ + if (!nrf_power_usbregstatus_vbusdet_get()) + return; + } + + static const app_usbd_evt_t evt_data = { + .type = APP_USBD_EVT_START_REQ + }; + app_usbd_event_add((app_usbd_internal_evt_t const * )&evt_data); +} + + +void app_usbd_stop(void) +{ + const app_usbd_evt_t evt_data = { + .type = APP_USBD_EVT_STOP_REQ + }; + app_usbd_event_add((app_usbd_internal_evt_t const * )&evt_data); +} + +void app_usbd_suspend_req(void) +{ + const app_usbd_evt_t evt_data = { + .type = APP_USBD_EVT_SUSPEND_REQ + }; + app_usbd_event_add((app_usbd_internal_evt_t const * )&evt_data); +} + +bool app_usbd_wakeup_req(void) +{ + ASSERT(app_usbd_class_rwu_enabled_check()); + if (!app_usbd_core_feature_state_get(APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP)) + return false; + + const app_usbd_evt_t evt_data = { + .type = APP_USBD_EVT_WAKEUP_REQ + }; + app_usbd_event_add((app_usbd_internal_evt_t const * )&evt_data); + return true; +} + +bool app_usbd_active_check(void) +{ + return (sustate_get() == SUSTATE_ACTIVE); +} + +void app_usbd_event_execute(app_usbd_internal_evt_t const * const p_event) +{ + ASSERT(NULL != m_p_first_cinst); + /* If no event queue is implemented, it has to be ensured that this function is never called + * from the context higher than USB interrupt level + * If queue is implemented it would be called always from Thread level + * if the library is used correctly. + * NOTE: Higher interrupt level -> lower priority value. + */ + ASSERT(USBD_CONFIG_IRQ_PRIORITY <= current_int_priority_get()); + + /* Note - there should never be situation that event is generated on disconnected endpoint */ + switch (p_event->type) + { + case APP_USBD_EVT_START_REQ: + { + static const app_usbd_evt_t evt_data = { + .type = APP_USBD_EVT_STARTED + }; + + /* Send event to all classes */ + UNUSED_RETURN_VALUE(app_usbd_core_handler_call((app_usbd_internal_evt_t const * )&evt_data)); + app_usbd_all_call((app_usbd_complex_evt_t const *)&evt_data); + user_event_state_proc(APP_USBD_EVT_STARTED); + + nrf_drv_usbd_start((NULL != m_p_first_sof_cinst) || (m_current_conf.enable_sof) || (APP_USBD_PROVIDE_SOF_TIMESTAMP)); + app_usbd_all_iface_deselect(); + sustate_set(SUSTATE_STARTED); + break; + } + case APP_USBD_EVT_STOP_REQ: + { + static const app_usbd_evt_t evt_data = { + .type = APP_USBD_EVT_STOPPED + }; + + app_usbd_all_iface_deselect(); + nrf_drv_usbd_stop(); + sustate_set(SUSTATE_STOPPED); + + /* Send event to all classes */ + app_usbd_all_call((app_usbd_complex_evt_t const * )&evt_data); + UNUSED_RETURN_VALUE(app_usbd_core_handler_call((app_usbd_internal_evt_t const *)&evt_data)); + user_event_state_proc(APP_USBD_EVT_STOPPED); + if (app_usbd_sustate_with_requested_hfclk(sustate_get())) + { + nrf_drv_clock_hfclk_release(); + } + + break; + } + case APP_USBD_EVT_HFCLK_READY: + { + switch(sustate_get()) + { + case SUSTATE_RESUMING: + { + sustate_set(SUSTATE_ACTIVE); + break; + } + case SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ: + { + sustate_set(SUSTATE_WAKINGUP_WAITING_WREQ); + break; + } + case SUSTATE_WAKINGUP_WAITING_HFCLK: + { + sustate_set(SUSTATE_ACTIVE); + break; + } + default: + break; // Just ignore - it can happen in specyfic situation + } + break; + } + case APP_USBD_EVT_SUSPEND_REQ: + { + /* Suspend request can be only processed when we are in suspending state */ + if (SUSTATE_SUSPENDING == sustate_get()) + { + if (nrf_drv_usbd_suspend()) + { + sustate_set(SUSTATE_SUSPEND); + } + } + break; + } + case APP_USBD_EVT_WAKEUP_REQ: + { + /* Suspend temporary if no suspend function was called from the application. + * This makes it possible to generate APP_USBD_EVT_DRV_WUREQ event from the driver */ + if (sustate_get() == SUSTATE_SUSPENDING) + { + if (nrf_drv_usbd_suspend()) + { + sustate_set(SUSTATE_SUSPEND); + } + } + if (nrf_drv_usbd_wakeup_req()) + { + sustate_set(SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ); + } + break; + } + + case APP_USBD_EVT_DRV_SOF: + { +#if (APP_USBD_PROVIDE_SOF_TIMESTAMP) || defined(__SDK_DOXYGEN__) + m_last_frame = p_event->drv_evt.data.sof.framecnt; +#endif + user_event_state_proc(APP_USBD_EVT_DRV_SOF); + + app_usbd_class_inst_t const * p_inst = app_usbd_class_sof_first_get(); + while (NULL != p_inst) + { + ret_code_t r = class_event_handler(p_inst, (app_usbd_complex_evt_t const *)p_event); + UNUSED_VARIABLE(r); + p_inst = app_usbd_class_sof_next_get(p_inst); + } + break; + } + + case APP_USBD_EVT_DRV_RESET: + { + app_usbd_all_iface_deselect(); + sustate_set(SUSTATE_ACTIVE); + user_event_state_proc(APP_USBD_EVT_DRV_RESET); + /* Processing core interface (connected only to EP0) and then all instances from the list */ + UNUSED_RETURN_VALUE(app_usbd_core_handler_call(p_event)); + app_usbd_all_call((app_usbd_complex_evt_t const *)p_event); + break; + } + case APP_USBD_EVT_DRV_RESUME: + { + sustate_set(SUSTATE_RESUMING); + user_event_state_proc(APP_USBD_EVT_DRV_RESUME); + /* Processing core interface (connected only to EP0) and then all instances from the list */ + UNUSED_RETURN_VALUE(app_usbd_core_handler_call(p_event)); + app_usbd_all_call((app_usbd_complex_evt_t const *)p_event); + break; + } + case APP_USBD_EVT_DRV_WUREQ: + { + static const app_usbd_evt_t evt_data = { + .type = APP_USBD_EVT_DRV_RESUME + }; + user_event_state_proc(APP_USBD_EVT_DRV_RESUME); + /* Processing core interface (connected only to EP0) and then all instances from the list */ + UNUSED_RETURN_VALUE(app_usbd_core_handler_call((app_usbd_internal_evt_t const *)&evt_data)); + app_usbd_all_call((app_usbd_complex_evt_t const *)&evt_data); + + switch(sustate_get()) + { + case SUSTATE_WAKINGUP_WAITING_HFCLK_WREQ: + sustate_set(SUSTATE_WAKINGUP_WAITING_HFCLK); + break; + case SUSTATE_WAKINGUP_WAITING_WREQ: + sustate_set(SUSTATE_ACTIVE); + break; + default: + { + // This should not happen - but try to recover by setting directly active state + NRF_LOG_WARNING("Unexpected state on WUREQ event (%u)", sustate_get()); + sustate_set(SUSTATE_ACTIVE); + } + } + break; + } + case APP_USBD_EVT_DRV_SUSPEND: + { + sustate_set(SUSTATE_SUSPENDING); + + user_event_state_proc(APP_USBD_EVT_DRV_SUSPEND); + + /* Processing all instances from the list and then core interface (connected only to EP0) */ + app_usbd_all_call((app_usbd_complex_evt_t const *)p_event); + UNUSED_RETURN_VALUE(app_usbd_core_handler_call(p_event)); + break; + } + + case APP_USBD_EVT_STATE_CHANGED: + { + user_event_state_proc(APP_USBD_EVT_STATE_CHANGED); + /* Processing all instances from the list and then core interface (connected only to EP0) */ + app_usbd_all_call((app_usbd_complex_evt_t const *)p_event); + break; + } + + case APP_USBD_EVT_DRV_SETUP: + { + UNUSED_RETURN_VALUE(app_usbd_core_handler_call(p_event)); + break; + } + + case APP_USBD_EVT_DRV_EPTRANSFER: + { + app_usbd_ep_conf_t const * p_ep_conf = + app_usbd_ep_conf_access(p_event->drv_evt.data.eptransfer.ep); + ASSERT(NULL != p_ep_conf->p_cinst); + ASSERT(NULL != p_ep_conf->event_handler); + + if (NRF_SUCCESS != p_ep_conf->event_handler(p_ep_conf->p_cinst, + (app_usbd_complex_evt_t const *)p_event)) + { + /* If error returned, every bulk/interrupt endpoint would be stalled */ + if (!(0 == NRF_USBD_EP_NR_GET(p_event->drv_evt.data.eptransfer.ep) || + NRF_USBD_EPISO_CHECK(p_event->drv_evt.data.eptransfer.ep))) + { + nrf_drv_usbd_ep_stall(p_event->drv_evt.data.eptransfer.ep); + } + } + break; + } +#if APP_USBD_CONFIG_POWER_EVENTS_PROCESS + case APP_USBD_EVT_POWER_DETECTED: + { + user_event_state_proc(APP_USBD_EVT_POWER_DETECTED); + app_usbd_all_call((app_usbd_complex_evt_t const *)p_event); + break; + } + case APP_USBD_EVT_POWER_REMOVED: + { + user_event_state_proc(APP_USBD_EVT_POWER_REMOVED); + app_usbd_all_call((app_usbd_complex_evt_t const *)p_event); + break; + } + case APP_USBD_EVT_POWER_READY: + { + user_event_state_proc(APP_USBD_EVT_POWER_READY); + app_usbd_all_call((app_usbd_complex_evt_t const *)p_event); + break; + } +#endif + default: + ASSERT(0); + break; + } +} + + +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__) +bool app_usbd_event_queue_process(void) +{ +#if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + app_usbd_internal_evt_t sof_event = { + .app_evt.type = APP_USBD_EVT_DRV_SOF + }; +#endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + static nrf_atfifo_item_get_t cx; + static app_usbd_internal_queue_evt_t * p_event_item = NULL; + if (NULL == p_event_item) + { + p_event_item = nrf_atfifo_item_get(m_event_queue, &cx); + } + + if (NULL != p_event_item) + { + +#if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + if (p_event_item->sof_cnt > 0) + { + if (p_event_item->start_frame > USBD_FRAMECNTR_FRAMECNTR_Msk) + { + p_event_item->start_frame = 0; + } + sof_event.drv_evt.data.sof.framecnt = (p_event_item->start_frame)++; + --(p_event_item->sof_cnt); + app_usbd_event_execute(&sof_event); + return true; + } +#endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + + app_usbd_event_execute(&(p_event_item->evt)); + UNUSED_RETURN_VALUE(nrf_atfifo_item_free(m_event_queue, &cx)); + p_event_item = NULL; + return true; + } +#if (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + else if (m_sof_events_cnt > 0) + { + CRITICAL_REGION_ENTER(); + if (m_event_frame > USBD_FRAMECNTR_FRAMECNTR_Msk) + { + m_event_frame = 0; + } + sof_event.drv_evt.data.sof.framecnt = m_event_frame++; + UNUSED_RETURN_VALUE(nrf_atomic_u32_sub_hs(&m_sof_events_cnt, 1)); + CRITICAL_REGION_EXIT(); + app_usbd_event_execute(&sof_event); + return true; + } +#endif // (APP_USBD_CONFIG_SOF_HANDLING_MODE == APP_USBD_SOF_HANDLING_COMPRESS_QUEUE) + else + { + return false; + } +} +#endif + + +ret_code_t app_usbd_class_append(app_usbd_class_inst_t const * p_cinst) +{ + ASSERT(NULL != p_cinst); + ASSERT(NULL != p_cinst->p_class_methods); + ASSERT(NULL != p_cinst->p_class_methods->event_handler); + ASSERT(NULL == app_usbd_class_data_access(p_cinst)->p_next); + + /* This should be only called if USBD is disabled + * We simply assume that USBD is enabled if its interrupts are */ + ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized()); + + /* Check if all required endpoints are available + * Checking is splitted from setting to avoid situation that anything + * is modified and then operation finishes with error */ + uint8_t iface_idx; + app_usbd_class_iface_conf_t const * p_iface; + + iface_idx = 0; + while (NULL != (p_iface = app_usbd_class_iface_get(p_cinst, iface_idx++))) + { + uint8_t ep_idx = 0; + app_usbd_class_ep_conf_t const * p_ep; + while (NULL != (p_ep = app_usbd_class_iface_ep_get(p_iface, ep_idx++))) + { + if (NULL != app_usbd_ep_instance_get(app_usbd_class_ep_address_get(p_ep))) + { + return NRF_ERROR_BUSY; + } + } + } + + /* Connecting all required endpoints */ + iface_idx = 0; + while (NULL != (p_iface = app_usbd_class_iface_get(p_cinst, iface_idx++))) + { + uint8_t ep_idx = 0; + app_usbd_class_ep_conf_t const * p_ep; + while (NULL != (p_ep = app_usbd_class_iface_ep_get(p_iface, ep_idx++))) + { + app_usbd_ep_instance_set(app_usbd_class_ep_address_get(p_ep), p_cinst); + } + } + + /* Adding pointer to this instance to the end of the chain */ + app_usbd_class_inst_t const * * pp_last = &m_p_first_cinst; + while (NULL != (*pp_last)) + { + ASSERT((*pp_last) != p_cinst); + pp_last = &(app_usbd_class_data_access(*pp_last)->p_next); + } + (*pp_last) = p_cinst; + + /* Successfully attached */ + const app_usbd_evt_t evt_data = {.type = APP_USBD_EVT_INST_APPEND }; + return class_event_handler(p_cinst, (app_usbd_complex_evt_t const *)(&evt_data)); +} + + +ret_code_t app_usbd_class_remove(app_usbd_class_inst_t const * p_cinst) +{ + ASSERT(NULL != p_cinst); + ASSERT(NULL != p_cinst->p_class_methods); + ASSERT(NULL != p_cinst->p_class_methods->event_handler); + /** This function should be only called if USBD is disabled */ + ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized()); + ret_code_t ret; + /* Remove this class from the chain */ + app_usbd_class_inst_t const * * pp_last = &m_p_first_cinst; + while (NULL != (*pp_last)) + { + if ((*pp_last) == p_cinst) + { + /* Inform class instance that removing process is going to be started */ + const app_usbd_evt_t evt_data = { + .type = APP_USBD_EVT_INST_REMOVE + }; + ret = class_event_handler(p_cinst, (app_usbd_complex_evt_t const *)(&evt_data)); + if (ret != NRF_SUCCESS) + { + return ret; + } + + /* Breaking chain */ + (*pp_last) = (app_usbd_class_data_access(p_cinst)->p_next); + app_usbd_class_data_access(p_cinst)->p_next = NULL; + + /* Disconnecting endpoints */ + uint8_t ep_idx; + for (ep_idx = 0; ep_idx < NRF_USBD_EPIN_CNT; ++ep_idx) + { + nrf_drv_usbd_ep_t ep = NRF_DRV_USBD_EPIN(ep_idx); + if (app_usbd_ep_instance_get(ep) == p_cinst) + { + app_usbd_ep_instance_set(ep, NULL); + } + } + for (ep_idx = 0; ep_idx < NRF_USBD_EPOUT_CNT; ++ep_idx) + { + nrf_drv_usbd_ep_t ep = NRF_DRV_USBD_EPOUT(ep_idx); + if (app_usbd_ep_instance_get(ep) == p_cinst) + { + app_usbd_ep_instance_set(ep, NULL); + } + } + + return NRF_SUCCESS; + } + pp_last = &(app_usbd_class_data_access(*pp_last)->p_next); + } + + return NRF_ERROR_NOT_FOUND; +} + + +ret_code_t app_usbd_class_remove_all(void) +{ + ret_code_t ret = NRF_SUCCESS; + while (NULL != m_p_first_cinst) + { + ret = app_usbd_class_remove(m_p_first_cinst); + if (ret != NRF_SUCCESS) + { + break; + } + } + + return ret; +} + + +ret_code_t app_usbd_ep_handler_set(app_usbd_class_inst_t const * const p_cinst, + nrf_drv_usbd_ep_t ep, + app_usbd_ep_event_handler_t handler) +{ + ASSERT(NULL != p_cinst); + ASSERT(NULL != handler); + /** This function should be only called if USBD is disabled */ + ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized()); + + if (p_cinst != app_usbd_ep_instance_get(ep)) + { + return NRF_ERROR_INVALID_PARAM; + } + + (app_usbd_ep_conf_access(ep))->event_handler = handler; + return NRF_SUCCESS; +} + + +ret_code_t app_usbd_class_sof_register(app_usbd_class_inst_t const * p_cinst) +{ + ASSERT(NULL != p_cinst); + ASSERT(NULL != p_cinst->p_class_methods); + ASSERT(NULL != p_cinst->p_class_methods->event_handler); + /** This function should be only called if USBD is disabled */ + ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized()); + + /* Next SOF event requiring instance have to be null now */ + ASSERT(NULL == (app_usbd_class_data_access(p_cinst)->p_sof_next)); + + /* Adding pointer to this instance to the end of the chain */ + app_usbd_class_inst_t const * * pp_last = &m_p_first_sof_cinst; + while (NULL != (*pp_last)) + { + + ASSERT((*pp_last) != p_cinst); + pp_last = &(app_usbd_class_data_access(*pp_last)->p_sof_next); + } + (*pp_last) = p_cinst; + + return NRF_SUCCESS; +} + + +ret_code_t app_usbd_class_sof_unregister(app_usbd_class_inst_t const * p_cinst) +{ + ASSERT(NULL != p_cinst); + /** This function should be only called if USBD is disabled */ + ASSERT(!nrf_drv_usbd_is_enabled() && nrf_drv_usbd_is_initialized()); + + app_usbd_class_inst_t const * * pp_last = &m_p_first_sof_cinst; + while (NULL != (*pp_last)) + { + if ((*pp_last) == p_cinst) + { + /* Breaking chain */ + (*pp_last) = (app_usbd_class_data_access(p_cinst)->p_sof_next); + app_usbd_class_data_access(p_cinst)->p_sof_next = NULL; + + return NRF_SUCCESS; + } + pp_last = &(app_usbd_class_data_access(*pp_last)->p_sof_next); + } + return NRF_ERROR_NOT_FOUND; +} + + +ret_code_t app_usbd_class_rwu_register(app_usbd_class_inst_t const * const p_inst) +{ + ASSERT(p_inst != NULL); + ++m_rwu_registered_counter; + /*Overflow check*/ + ASSERT(m_rwu_registered_counter != 0); + + return NRF_SUCCESS; +} + + +ret_code_t app_usbd_class_rwu_unregister(app_usbd_class_inst_t const * const p_inst) +{ + ASSERT(p_inst != NULL); + /* Usage validation. If counter is 0 unregister is not possible.*/ + ASSERT(m_rwu_registered_counter != 0); + --m_rwu_registered_counter; + + return NRF_SUCCESS; +} + +bool app_usbd_class_rwu_enabled_check(void) +{ + return (m_rwu_registered_counter != 0); +} + +ret_code_t app_usbd_interface_ep_reset(app_usbd_class_inst_t const * const p_cinst, + uint8_t iface) +{ + uint8_t iface_count = app_usbd_class_iface_count_get(p_cinst); + + app_usbd_class_iface_conf_t const * p_iface = NULL; + for (uint8_t j = 0; j < iface_count; ++j) + { + p_iface = app_usbd_class_iface_get(p_cinst, j); + if (app_usbd_class_iface_number_get(p_iface) == iface) + { + break; + } + } + + if (p_iface == NULL) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + uint8_t ep_count = app_usbd_class_iface_ep_count_get(p_iface); + + for (uint8_t j = 0; j < ep_count; ++j) + { + /*Clear stall for every endpoint*/ + app_usbd_class_ep_conf_t const * p_ep = app_usbd_class_iface_ep_get(p_iface, j); + + if (!NRF_USBD_EPISO_CHECK(p_ep->address)) + { + nrf_drv_usbd_ep_dtoggle_clear(p_ep->address); + nrf_drv_usbd_ep_stall_clear(p_ep->address); + } + + } + + return NRF_SUCCESS; +} + +void app_usbd_ep_enable(nrf_drv_usbd_ep_t ep) +{ + if (!NRF_USBD_EPISO_CHECK(ep)) + { + nrf_drv_usbd_ep_dtoggle_clear(ep); + nrf_drv_usbd_ep_stall_clear(ep); + } + nrf_drv_usbd_ep_enable(ep); +} + +void app_usbd_ep_disable(nrf_drv_usbd_ep_t ep) +{ + nrf_drv_usbd_ep_disable(ep); +} + + +app_usbd_class_inst_t const * app_usbd_class_first_get(void) +{ + return m_p_first_cinst; +} + +app_usbd_class_inst_t const * app_usbd_class_sof_first_get(void) +{ + return m_p_first_sof_cinst; +} + +app_usbd_class_inst_t const * app_usbd_iface_find(uint8_t iface, uint8_t * p_iface_idx) +{ + app_usbd_class_inst_t const * p_inst = app_usbd_class_first_get(); + while (p_inst != NULL) + { + uint8_t iface_count = app_usbd_class_iface_count_get(p_inst); + /* Iterate over interfaces */ + for (uint8_t i = 0; i < iface_count; ++i) + { + app_usbd_class_iface_conf_t const * p_iface; + p_iface = app_usbd_class_iface_get(p_inst, i); + if (app_usbd_class_iface_number_get(p_iface) == iface) + { + if (p_iface_idx != NULL) + { + (*p_iface_idx) = i; + } + return p_inst; + } + } + p_inst = app_usbd_class_next_get(p_inst); + } + return NULL; +} + +ret_code_t app_usbd_iface_call( + app_usbd_class_inst_t const * const p_class_inst, + uint8_t iface_idx, + app_usbd_complex_evt_t const * const p_event) +{ + UNUSED_PARAMETER(iface_idx); + return class_event_handler(p_class_inst, p_event); +} + +ret_code_t app_usbd_ep_call(nrf_drv_usbd_ep_t ep, app_usbd_complex_evt_t const * const p_event) +{ + if (NRF_USBD_EP_VALIDATE(ep)) + { + app_usbd_class_inst_t const * p_inst = app_usbd_ep_conf_access(ep)->p_cinst; + if (p_inst != NULL) + { + return class_event_handler(p_inst, p_event); + } + } + return NRF_ERROR_INVALID_ADDR; +} + +void app_usbd_all_call(app_usbd_complex_evt_t const * const p_event) +{ + app_usbd_class_inst_t const * p_inst; + for (p_inst = app_usbd_class_first_get(); NULL != p_inst; + p_inst = app_usbd_class_next_get(p_inst)) + { + UNUSED_RETURN_VALUE(class_event_handler(p_inst, p_event)); + } +} + +ret_code_t app_usbd_all_until_served_call(app_usbd_complex_evt_t const * const p_event) +{ + app_usbd_class_inst_t const * p_inst; + ret_code_t ret = NRF_ERROR_NOT_SUPPORTED; + /* Try to process via every instance */ + for (p_inst = app_usbd_class_first_get(); NULL != p_inst; + p_inst = app_usbd_class_next_get(p_inst)) + { + + ret = class_event_handler(p_inst, p_event); + if (NRF_ERROR_NOT_SUPPORTED != ret) + { + /* Processing finished */ + break; + } + } + + return ret; +} + +ret_code_t app_usbd_ep_transfer( + nrf_drv_usbd_ep_t ep, + nrf_drv_usbd_transfer_t const * const p_transfer) +{ + if (!nrf_drv_usbd_ep_enable_check(ep)) + { + return NRF_ERROR_INVALID_STATE; + } + if (m_sustate != SUSTATE_ACTIVE) + { + return NRF_ERROR_INVALID_STATE; + } + return nrf_drv_usbd_ep_transfer(ep, p_transfer); +} + +ret_code_t app_usbd_ep_handled_transfer( + nrf_drv_usbd_ep_t ep, + nrf_drv_usbd_handler_desc_t const * const p_handler) +{ + if (!nrf_drv_usbd_ep_enable_check(ep)) + { + return NRF_ERROR_INVALID_STATE; + } + if (m_sustate != SUSTATE_ACTIVE) + { + return NRF_ERROR_INVALID_STATE; + } + return nrf_drv_usbd_ep_handled_transfer(ep, p_handler); +} + +ret_code_t app_usbd_iface_select( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx, + uint8_t alternate) +{ + ret_code_t ret = NRF_ERROR_NOT_SUPPORTED; + + if (p_inst->p_class_methods->iface_select != NULL) + { + ret = p_inst->p_class_methods->iface_select(p_inst, iface_idx, alternate); + } + + if(ret == NRF_ERROR_NOT_SUPPORTED) + { + ret = default_iface_select(p_inst, iface_idx, alternate); + } + return ret; +} + +void app_usbd_iface_deselect( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx) +{ + if (p_inst->p_class_methods->iface_deselect != NULL) + { + p_inst->p_class_methods->iface_deselect(p_inst, iface_idx); + } + default_iface_deselect(p_inst, iface_idx); +} + +uint8_t app_usbd_iface_selection_get( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx) +{ + uint8_t alt = 0; + if (p_inst->p_class_methods->iface_selection_get != NULL) + { + alt = p_inst->p_class_methods->iface_selection_get(p_inst, iface_idx); + } + return alt; +} + +void app_usbd_all_iface_select_0(void) +{ + app_usbd_class_inst_t const * p_inst = app_usbd_class_first_get(); + while (p_inst != NULL) + { + uint8_t iface_count = app_usbd_class_iface_count_get(p_inst); + for (uint8_t i = 0; i < iface_count; ++i) + { + ret_code_t ret; + ret = app_usbd_iface_select(p_inst, i, 0); + ASSERT(ret == NRF_SUCCESS); + UNUSED_VARIABLE(ret); + } + p_inst = app_usbd_class_next_get(p_inst); + } +} + +void app_usbd_all_iface_deselect(void) +{ + app_usbd_class_inst_t const * p_inst = app_usbd_class_first_get(); + while (p_inst != NULL) + { + uint8_t iface_count = app_usbd_class_iface_count_get(p_inst); + for (uint8_t i = 0; i < iface_count; ++i) + { + app_usbd_iface_deselect(p_inst, i); + } + p_inst = app_usbd_class_next_get(p_inst); + } +} + +#endif //NRF_MODULE_ENABLED(APP_USBD) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd.h new file mode 100644 index 0000000..f63e63c --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd.h @@ -0,0 +1,745 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef APP_USBD_H__ +#define APP_USBD_H__ + +#include "nrf_drv_usbd.h" +#include "app_usbd_types.h" +#include "app_usbd_class_base.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd USB Device high level library + * @ingroup app_common + * + * @brief @tagAPI52840 Module for easy support for any USB device configuration. + * + * This module manages class instances that would create the USB device, + * manages endpoints and interfaces transactions. + * @{ + */ + +/** + * @brief True if SOF timestamping is really provided. + * + * SOF timestamping is really provided if it was requested and if the logger is enabled. + */ +#if ((APP_USBD_CONFIG_SOF_TIMESTAMP_PROVIDE) && (NRF_LOG_ENABLED)) +#define APP_USBD_PROVIDE_SOF_TIMESTAMP 1 +#else +#define APP_USBD_PROVIDE_SOF_TIMESTAMP 0 +#endif + +/** + * @brief SOF event handling modes. + */ +#define APP_USBD_SOF_HANDLING_NORMAL_QUEUE 0 //!< Push SOF events into event queue. +#define APP_USBD_SOF_HANDLING_COMPRESS_QUEUE 1 //!< Compress SOF events. +#define APP_USBD_SOF_HANDLING_INTERRUPT 2 //!< Handle SOF events in interrupt. + +/** + * @brief Configuration passed to @ref app_usbd_init. + */ +typedef struct { +#if (!(APP_USBD_CONFIG_EVENT_QUEUE_ENABLE)) || defined(__SDK_DOXYGEN__) + /** + * @brief User defined event handler. + * + * This function is called on every event from the interrupt. + * It is prepared for external user function that would queue events to be processed + * from the main context. + * It should be used with operating systems with its own implementation of the queue. + * + * @param p_event The event structure pointer. + * + * @note This field is available only when USB internal queue is disabled + * (see @ref APP_USBD_CONFIG_EVENT_QUEUE_ENABLE). + */ + void (*ev_handler)(app_usbd_internal_evt_t const * const p_event); +#endif + +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__) + /** + * @brief User defined event handler. + * + * This function is called on every event from the interrupt. + * + * @param p_event The event structure pointer. + * @param queued The event is visible in the queue. + * If queue conflict is detected the event might not be accessible inside queue + * until all write operations finish. + * See @ref nrf_atfifo for more details. + * + * @note This field is available only when USBD internal queue is configured + * (see @ref APP_USBD_CONFIG_EVENT_QUEUE_ENABLE). + * + * @note If is set to NULL no event would be called from interrupt. + * @note This function is called before event is processed. + * It means that if the event type is @ref APP_USBD_EVT_DRV_SETUP, + * there would not be setup field present in the event structure. + */ + void (*ev_isr_handler)(app_usbd_internal_evt_t const * const p_event, bool queued); +#endif + + /** + * @brief User defined event processor + * + * This function is called while state event is processed. + * + * * @note This field is available only when USBD internal queue is configured + * (see @ref APP_USBD_CONFIG_EVENT_QUEUE_ENABLE). + * + * @param event Event type. + * Only following events are sent into this function: + * - APP_USBD_EVT_DRV_SOF + * - APP_USBD_EVT_DRV_RESET - Note that it also exits suspend + * - APP_USBD_EVT_DRV_SUSPEND + * - APP_USBD_EVT_DRV_RESUME - It is also generated when remote wakeup is generated + * - APP_USBD_EVT_START + * - APP_USBD_EVT_STOP + * - APP_USBD_EVT_STATE_CHANGED + */ + void (*ev_state_proc)(app_usbd_event_type_t event); + + /** + * @brief SOF processing required by the user event processing + * + * This flag would enable SOF processing for the user events regardless of the fact if any + * of the implemented class requires SOF event. + * + * @note SOF event would be enabled anyway if any of the appended class requires SOF processing. + */ + bool enable_sof; +} app_usbd_config_t; + + +#if (APP_USBD_PROVIDE_SOF_TIMESTAMP) || defined(__SDK_DOXYGEN__) +/** + * @brief Timestamp function for the logger. + * + * @return Current frame number taken directly from the last processed SOF. + */ +uint32_t app_usbd_sof_timestamp_get(void); +#endif + +/** + * @brief USB library initialization. + * + * Call this function before any configuration or class attachment. + * USBD peripheral would be ready to accept commands, and library would be ready, + * but it would not be connected to the bus. + * Call @ref app_usbd_enable to enable USBD communication with the host. + * + * @param p_config Configuration. NULL pointer might be passed here and default + * configuration will be applied then. + */ +ret_code_t app_usbd_init(app_usbd_config_t const * p_config); + +/** + * @brief USB library un-initialization + * + * @note Currently not supported + */ +ret_code_t app_usbd_uninit(void); + +#if (APP_USBD_CONFIG_POWER_EVENTS_PROCESS) || defined(__SDK_DOXYGEN__) +/** + * @brief Function to start USB related power events processing + * + * This function should be called after @ref app_usbd_init and after all the + * required classes were appended (@ref app_usbd_class_append). + * + * @retval NRF_SUCCESS Power events successfully initialized + * @retval NRF_ERROR_INVALID_STATE The state of the driver does not allow to enable + * the power events processing. + */ +ret_code_t app_usbd_power_events_enable(void); +#endif + +/** + * @brief Enable USBD + * + * USBD is enabled. + * Since now the high frequency clock may be requested when USB RESET would be detected. + */ +void app_usbd_enable(void); + +/** + * @brief Disable USBD + * + * Disabled USDB peripheral cannot be accessed but also stops requesting + * High Frequency clock and releases power regulator. + * + * @note This function cannot be called when USB is started. Stop it first. + */ +void app_usbd_disable(void); + +/** + * @brief Request USBD to start + * + * The function sends start request to the event queue. + * If the queue is enabled (@ref APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) it would be processed + * when the queue is processed. + * If queue is disabled it would be processed immediately inside this function. + * It means that if queue is disabled this function cannot be called from interrupt with priority + * higher than USB interrupt. + * + * When start is processed it would: + * 1. Start library. + * 2. Enable interrupts. + * 3. Enable USB pull-ups. + * + * @note + * In some specific circumstances the library can be left not started and this function would + * silently exit. + * This may happen if some glitches appears on USB power line or if the plug was disconnected before + * whole starting process finishes. + * User would get the event from POWER peripheral then. + * Also no @ref APP_USBD_EVT_STARTED event would be generated to the classes and user event handler. + * For the safe code it is recommended to wait for @ref APP_USBD_EVT_STARTED event if anything + * has to be initialized after USB driver is started (just before enabling the interrupts). + * If library is properly started the @ref APP_USBD_EVT_STARTED event passed to the user handler + * from this function body. + */ +void app_usbd_start(void); + +/** + * @brief Stop USB to work + * + * The function sends stop request to the event queue. + * If the queue is enabled (@ref APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) it would be processed + * when the queue is processed. + * If queue is disabled it would be processed immediately inside this function. + * It means that if queue is disabled this function cannot be called from interrupt with priority + * higher than USB interrupt. + * + * When the event is processed interrupts and USB pull-ups are disabled. + * The peripheral itself is left enabled so it can be programmed, + * but a HOST sees it as a peripheral disconnection. + * + * @note + * If the library is not started when this function is called it exits silently - also + * no @ref APP_USBD_EVT_STOPPED is generated. + */ +void app_usbd_stop(void); + +/** + * @brief Request library to suspend + * + * This function send suspend request to the event queue. + * + * @note This function should only be called after @ref APP_USBD_EVT_DRV_SUSPEND os received. + * Internal suspend request processing would give no effect if the bus is not in suspend state. + */ +void app_usbd_suspend_req(void); + +/** + * @brief Request library to wake-up + * + * This function send wakeup request to the event queue. + * + * @note Calling this function does not mean that peripheral is active - the wakeup request is sent + * into message queue and needs to be processed. + * + * @retval true Wakeup generation has been started. + * @retval false No wakeup would be generated becouse it is disabled by the host. + */ +bool app_usbd_wakeup_req(void); + +/** + * @brief Get information whether there is an active connection. + * + * Function to check if the communication with the bus is possible. + * + * @retval true The bus is active. + * @retval false There is no connection or bus is suspended. + */ +bool app_usbd_active_check(void); + +/** + * @brief USBD event processor + * + * Function to be called on each event to be processed by the library. + */ +void app_usbd_event_execute(app_usbd_internal_evt_t const * const p_event); + + +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE) || defined(__SDK_DOXYGEN__) +/** + * @brief Function that process events from the queue + * + * @note This function calls @ref app_usbd_event_execute internally. + * + * @retval true Event was processed + * @retval false The event queue is empty + */ +bool app_usbd_event_queue_process(void); +#endif + +/** + * @brief Add class instance + * + * This function connects given instance into internal class instance chain and + * into all required endpoints. + * The instance event handler would be connected into endpoint by default, + * but this can be overwritten by @ref app_usbd_ep_handler_set. + * + * After successful attachment @ref APP_USBD_EVT_INST_APPEND would be passed to class instance. + * + * @note This function can only be called after USBD library is initialized but still disabled. + * Assertion would be generated otherwise. + * + * @param[in,out] p_cinst Instance to connect. Chain data would be written into writable instance data. + */ +ret_code_t app_usbd_class_append(app_usbd_class_inst_t const * p_cinst); + +/** + * @brief Remove class instance + * + * Instance is removed from instance chain. + * Instance and event handlers are removed also from endpoints. + * Endpoints used by by the class instance are left disabled. + * + * @note This function can only be called after USBD library is initialized but still disabled. + * Assertion would be generated otherwise. + * + * @param p_cinst Instance pointer to remove. + * + * @retval NRF_SUCCESS Instance successfully removed. + * @retval NRF_ERROR_NOT_FOUND Instance not found in the instance chain. + */ +ret_code_t app_usbd_class_remove(app_usbd_class_inst_t const * p_cinst); + +/** + * @brief Remove all class instances + * + * This function basically calls @ref app_usbd_class_remove + * on instances chain as long as there is any element left. + * + * @note This function can only be called after USBD library is initialized but still disabled. + * Assertion would be generated otherwise. + * + * @sa app_usbd_class_remove + * + * @return Is should always return @ref NRF_SUCCESS. + * Any error value returned would mean there is an error inside the library. + */ +ret_code_t app_usbd_class_remove_all(void); + +/** + * @brief Change endpoint handler + * + * This function may be called for the endpoint only if the class instance is + * already properly attached by the @ref app_usbd_class_append function. + * + * The endpoint event handler function can be only overwritten by the class instance + * that was connected into the endpoint. + * + * @note This function can only be called after USBD library is initialized but still disabled. + * Assertion would be generated otherwise. + * + * @param[in] p_cinst Instance of a class that wish to set new event handler. + * It has to match currently configured instance for the selected endpoint. + * In other situation error would be returned. + * @param[in] ep Endpoint address to configure. + * @param[in] handler Event handler function to set. + * + * @retval NRF_SUCCESS New handler successfully set + * @retval NRF_ERROR_INVALID_PARAM p_cinst is not the same as currently set for the endpoint + */ +ret_code_t app_usbd_ep_handler_set(app_usbd_class_inst_t const * p_cinst, + nrf_drv_usbd_ep_t ep, + app_usbd_ep_event_handler_t handler); + +/** + * @brief Register class instance as the one that requires SOF events + * + * This function should be called in reaction on APP_USBD_EVT_INST_APPEND event. + * Connect the class instance to the list of instances that requires SOF processing. + * If none of the appended instances requires SOF event - it is disabled. + * + * @param p_cinst Instance that requires SOF event. + * + * @retval NRF_SUCCESS Instance linked into SOF processing list. + * + * @sa app_usbd_class_sof_unregister + */ +ret_code_t app_usbd_class_sof_register(app_usbd_class_inst_t const * p_cinst); + +/** + * @brief Unregister class instance from SOF processing instances list + * + * Every class that calls @ref app_usbd_class_sof_register have to call also unregistering function + * in reaction to @ref APP_USBD_EVT_INST_REMOVE event. + * + * @param p_cinst Instance to be unregistered from SOF event processing list. + * + * @retval NRF_SUCCESS Instance linked into SOF processing list. + * @retval NRF_ERROR_NOT_FOUND Instance not found in the SOF processing list. + * + * @sa app_usbd_class_sof_register + */ +ret_code_t app_usbd_class_sof_unregister(app_usbd_class_inst_t const * p_cinst); + +/** + * @brief Register class on remote wake-up feature + * + * @param[in] p_inst Instance of the class + * + * @retval NRF_SUCCESS Instance that requires remote wake-up registered + */ +ret_code_t app_usbd_class_rwu_register(app_usbd_class_inst_t const * const p_inst); + +/** + * @brief Unregister class from remote wake-up feature + * + * @param[in] p_inst Instance of the class + * + * @retval NRF_SUCCESS Instance that requires remote wake-up removed + */ +ret_code_t app_usbd_class_rwu_unregister(app_usbd_class_inst_t const * const p_inst); + +/** + * @brief Check if there is any class with remote wakeup + * + * The function checks internal registered class with remote wakeup counter. + * + * @sa app_usbd_class_rwu_register, app_usbd_class_rwu_unregister + * + * @retval true The remote wakeup functionality is required by some class instance + * @retval false There is no class instance that requires wakeup functionality + */ +bool app_usbd_class_rwu_enabled_check(void); + +/** + * @brief Find a specified descriptor + * + * @param[in] p_cinst Class instance + * @param[in] desc_type Descriptor type @ref app_usbd_descriptor_t + * @param[in] desc_index Descriptor index + * @param[out] p_desc Pointer to escriptor + * @param[out] p_desc_len Length of descriptor + * + * @return Standard error code @ref ret_code_t + * @retval NRF_SUCCESS descriptor successfully found + * @retval NRF_ERROR_NOT_FOUND descriptor not found + * */ +ret_code_t app_usbd_class_descriptor_find(app_usbd_class_inst_t const * const p_cinst, + uint8_t desc_type, + uint8_t desc_index, + uint8_t * p_desc, + size_t * p_desc_len); + +/** + * @brief Standard set interface request handle + * + * This function should be called when processing SET_INTERFACE request. + * + * @param[in] p_cinst Instance of a class + * @param[in] iface Interface number + * + * @return Standard error code + * + * @note Selected interface to reset has to be part of given class. + * In other case + * */ +ret_code_t app_usbd_interface_ep_reset(app_usbd_class_inst_t const * const p_cinst, + uint8_t iface); + + +/** + * @brief Enable selected endpoint + * + * Selected endpoint is enabled and cleared. + * + * @param ep Endpoint number + */ +void app_usbd_ep_enable(nrf_drv_usbd_ep_t ep); + +/** + * @brief Disable selected endpoint + * + * @param ep Endpoint number + */ +void app_usbd_ep_disable(nrf_drv_usbd_ep_t ep); + +/** + * @name Iterate through classes lists + * + * Functions that helps to iterate through internally chained classes. + * @{ + */ + /** + * @brief Get first class instance in the list + * + * Get first instance from the list of active class instances. + * That instance may be used then in @ref app_usbd_class_next_get function. + * + * @return First instance in the list or NULL if there are no instances available. + */ + app_usbd_class_inst_t const * app_usbd_class_first_get(void); + + /** + * @brief Get next instance in the list + * + * Get the next instance from the list of active instances. + * Used to iterate through all instances. + * + * @param[in] p_cinst The current instance from with next one is required. + * + * @return Next instance to the given one or NULL if there is no more instances in the list. + */ + static inline app_usbd_class_inst_t const * app_usbd_class_next_get( + app_usbd_class_inst_t const * const p_cinst) + { + ASSERT(NULL != p_cinst); + return app_usbd_class_data_access(p_cinst)->p_next; + } + + /** + * @brief Get first instance in SOF list + * + * Start iteration through the list of instances that requires SOF event processing. + * + * @return First instance in the list or NULL if the list is empty + * + * @sa app_usbd_class_first_get + */ + app_usbd_class_inst_t const * app_usbd_class_sof_first_get(void); + + /** + * @brief Get next instance in the SOF list + * + * Get the next instance from the list of instances requiring SOF event processing. + * Used to iterate through all SOF instances. + * + * @param p_cinst The current instance from with next one is required. + * + * @return Next instance to the given one or NULL if there is no more instances in the list. + */ + static inline app_usbd_class_inst_t const * app_usbd_class_sof_next_get( + app_usbd_class_inst_t const * const p_cinst) + { + ASSERT(NULL != p_cinst); + return app_usbd_class_data_access(p_cinst)->p_sof_next; + } +/** @} */ + +/** + * @brief Search for selected interface + * + * Function searches for the given interface number and returns the class that contains it. + * Optionally it can return interface index inside class instance. + * + * @param[in] iface Interface number + * @param[out] p_iface_idx Pointer to a variable that would hold interface index inside returned + * class instance. + * + * @return Pointer to the class structure that cointain given interface or NULL if not found. + */ +app_usbd_class_inst_t const * app_usbd_iface_find(uint8_t iface, uint8_t * p_iface_idx); + + +/** + * @name Communicate with interfaces, endpoints and instances inside usbd library + * + * @{ + */ + + /** + * @brief Call interface event handler + * + * Call event handler for selected interface. + * @param[in,out] p_class_inst Class instance that holds selected interface + * @param[in] iface_idx Index of the interface in class structure + * @param[in] p_event Event structure to be processed + * + * @return Operation status + */ + ret_code_t app_usbd_iface_call( + app_usbd_class_inst_t const * const p_class_inst, + uint8_t iface_idx, + app_usbd_complex_evt_t const * const p_event); + + /** + * @brief Call endpoint event handler + * + * Call event handler for the selected endpoint. + * @param[in] ep Endpoint number + * @param[in] p_event Event structure to send + * + * @return Operation status + */ + ret_code_t app_usbd_ep_call(nrf_drv_usbd_ep_t ep, app_usbd_complex_evt_t const * const p_event); + + /** + * @brief Auxiliary function that process event by every instance in the list + * + * This function ignores the result of called handler. + * + * @param p_event Event to pass to every instance + */ + void app_usbd_all_call(app_usbd_complex_evt_t const * const p_event); + + /** + * @brief Call interface event handlers and stop when served + * + * Call event handlers from instances as long as we get result different than @ref NRF_ERROR_NOT_SUPPORTED + * @param[in] p_event Event structure to send + * + * @return Operation status or @ref NRF_ERROR_NOT_SUPPORTED if none of instances in the list can support given event. + */ + ret_code_t app_usbd_all_until_served_call(app_usbd_complex_evt_t const * const p_event); +/** @} */ + +/** + * @brief Endpoint transfer + * + * @param ep Endpoint number + * @param p_transfer Description of the transfer to be performed. + * The direction of the transfer is determined by the + * endpoint number. + * + * @retval NRF_ERROR_INVALID_STATE The state of the USB device does not allow + * data transfer on the endpoint. + * @return Values returned by @ref nrf_drv_usbd_ep_transfer + * + * @sa app_usbd_ep_handled_transfer + */ +ret_code_t app_usbd_ep_transfer( + nrf_drv_usbd_ep_t ep, + nrf_drv_usbd_transfer_t const * const p_transfer); + +/** + * @brief Set up an endpoint handled transfer. + * + * Configures a transfer handled by the feedback function. + * + * @param ep Endpoint number. + * @param p_handler Function called when the next chunk of data is requested. + * + * @retval NRF_ERROR_INVALID_STATE The state of the USB device does not allow + * data transfer on the endpoint. + * @return Values returned by @ref nrf_drv_usbd_ep_handled_transfer. + */ +ret_code_t app_usbd_ep_handled_transfer( + nrf_drv_usbd_ep_t ep, + nrf_drv_usbd_handler_desc_t const * const p_handler); + + +/** + * @brief Select interface + * + * Select the given interface. + * This function calls class interface selection function or default + * interface selection function. + * + * After calling this function interface should be functional. + * + * @param[in,out] p_inst Instance of the class + * @param[in] iface_idx Index of the interface inside class structure + * @param[in] alternate Alternate setting that should be selected + * + * @return Standard Error code + */ +ret_code_t app_usbd_iface_select( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx, + uint8_t alternate); + + +/** + * @brief Deselect interface + * + * Disable the given interface. + * This function calls class interface deselection function or + * default interface selection function. + * + * After calling this function all the endpoints from the interface + * have to be disabled. + * + * @param[in,out] p_inst Instance of the class + * @param[in] iface_idx Index of the interface inside class structure + */ +void app_usbd_iface_deselect( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx); + +/** + * @brief Get selected interface + * + * Function retieves currently selected interface. + * If the class contains @ref app_usbd_class_methods_t::iface_selection_get it is called. + * It it does not contain this function this function would return default, 0 value. + * + * @param[in] p_inst Instance of the class + * @param[in] iface_idx Index of the interface inside class structure + * + * @return Selected alternate interface setting + */ +uint8_t app_usbd_iface_selection_get( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx); + +/** + * @brief Select alternate configuration 0 for all interfaces + * + * Auxiliary function that clears settings for all interfaces leaving them enabled. + */ +void app_usbd_all_iface_select_0(void); + +/** + * @brief Deselect all interfaces + * + * Auxiliary function to disable all interfaces. + */ +void app_usbd_all_iface_deselect(void); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_class_base.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_class_base.h new file mode 100644 index 0000000..672ab28 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_class_base.h @@ -0,0 +1,1094 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef APP_USBD_CLASS_BASE_H__ +#define APP_USBD_CLASS_BASE_H__ + +#include <stdint.h> +#include <stddef.h> + +#include "app_usbd_types.h" +#include "nrf_drv_usbd.h" +#include "nrf_assert.h" +#include "app_util.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_class_base USBD Class Base module + * @ingroup app_usbd + * + * @brief @tagAPI52840 The base for any class instance is defined in this module. + * + * @details Any class instance must start from base class instance structure. + * This makes them compatible with USBD library independently of the + * implementation details. + * @{ + */ + +/** + * @brief Endpoint configuration + */ +typedef struct +{ + nrf_drv_usbd_ep_t address; //!< Endpoint address +} app_usbd_class_ep_conf_t; + +/** + * @brief Interface configuration + */ +typedef struct +{ + uint8_t number; //!< Interface number + uint8_t ep_cnt; //!< Endpoint number + uint8_t ep_offset; //!< Offset of the first endpoint + /**< Offset in bytes of the first endpoint. + * The offset is calculated from the address of this interface structure + */ +} app_usbd_class_iface_conf_t; + +/** + * @brief Instance variable data + */ +typedef struct +{ + app_usbd_class_inst_t const * p_next; //!< Pointer to the next instance + app_usbd_class_inst_t const * p_sof_next; //!< Pointer to the next SOF event requiring instance +} app_usbd_class_data_t; + +/** + * @brief Class descriptor context + */ +typedef struct +{ + uint32_t line; //!< Number of line to resume writing descriptors from + uint8_t data_buffer; //!< Data from last call of feeder +} app_usbd_class_descriptor_ctx_t; + +/** + * @brief Class descriptor state + */ +typedef struct +{ + uint8_t * p_buffer; //!< Pointer to buffer + uint32_t current_size; //!< Current size of descriptor + uint32_t maximum_size; //!< Maximum size of descriptor + app_usbd_class_descriptor_ctx_t * p_context; //!< Pointer to context +} app_usbd_class_descriptor_state_t; + +/** + * @brief Class interface function set + * */ +typedef struct { + /** + * @brief Instance callback function + * + * The function used by every class instance. + * @param[in,out] p_inst Instance of the class + * @param[in] p_event Event to process + * + * @return Standard error code + * + * @note If given event is not supported by class, return @ref NRF_ERROR_NOT_SUPPORTED + */ + ret_code_t (* event_handler)(app_usbd_class_inst_t const * const p_inst, + app_usbd_complex_evt_t const * const p_event); + + /** + * @brief Instance feed descriptors + * + * Feeds whole descriptor of the instance + * @param[in] p_ctx Class descriptor context + * @param[in,out] p_inst Instance of the class + * @param[out] p_buff Buffer for descriptor + * @param[in] max_size Requested size of the descriptor + * + * @return True if not finished feeding the descriptor, false if done + */ + bool (* feed_descriptors)(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size); + + + /** + * @brief Select interface + * + * Function called when class interface has to be selected. + * + * This function would be called for every interface when: + * - SET_INTERFACE command is processed by the default handler + * - SET_CONFIG(1) command is processed by the default handler + * + * @note Remember to disable all the endpoints that are not used + * in the selected configuration. + * @note If this function pointer is NULL default procedure would + * just enable all the interface endpoints and selecting + * alternate configurations other than 0 would generate error. + * @note Calling the function with alternate setting 0 has to always succeed. + * + * @param[in,out] p_inst Instance of the class + * @param[in] iface_idx Index of the interface inside class structure + * @param[in] alternate Alternate setting that should be selected + * + * @return Function has to return @ref NRF_SUCCESS when it has successfully proceed + * interface selection. + * If it returns @ref NRF_ERROR_NOT_SUPPORTED, default function would be used + * to proceed the request - just like there would be NULL pointer in this field. + * Any other kind of error would make library to STALL the request. + */ + ret_code_t (* iface_select)(app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx, + uint8_t alternate); + + /** + * @brief Deselect interface + * + * Function called when the class interface has to be deselected. + * + * This function would be called for every interface when: + * - Library start internal event is processed by the default handler + * - RESET event is processed by the default handler + * - SET_ADDRESS is processed by the default handler + * - SET_CONFIG(0) is processed by the default handler + * + * @note Just after this function is called all the interface + * endpoints would be disabled. + * This function does not has to take care about it. + * @note If this function pointer is NULL default procedure would + * just disable all the interface endpoints. + * + * @param[in,out] p_inst Instance of the class + * @param[in] iface_idx Index of the interface inside class structure + */ + void (* iface_deselect)(app_usbd_class_inst_t const * const p_inst, uint8_t iface_idx); + + /** + * @brief Get current interface + * + * Function called when class interface has to return its alternate settings + * in reaction on GET_INTERFACE command. + * It should be defined in a pair with @ref app_usbd_class_methods_t::iface_select. + * + * @param[in] p_inst Instance of the class + * @param[in] iface_idx Index of the interface inside class structure + * + * @return Current alternate setting of the selected interface. + * + * @note For the classes that support this function, when an interface that has not alternate + * configurations has been selected this function has to return 0 - default alternate setting. + * + * @note If this function pointer it NULL default procedure would return alternate interface + * value 0. + */ + uint8_t (* iface_selection_get)(app_usbd_class_inst_t const * const p_inst, uint8_t iface_idx); + +} app_usbd_class_methods_t; + +/** + * @brief The instance structure itself + * + * The structure of base class instance + */ +struct app_usbd_class_inst_s +{ + app_usbd_class_data_t * p_data; //!< Pointer to non-constant data + app_usbd_class_methods_t const * p_class_methods; //!< Class interface methods + struct + { + uint8_t cnt; //!< Number of defined interfaces + uint8_t config[]; //!< Interface configuration data followed by endpoint data + } iface; //!< Interface structure +}; + + +/** + * @brief Get total number of interfaces + * + * + */ +static inline uint8_t app_usbd_class_iface_count_get(app_usbd_class_inst_t const * const p_inst) +{ + return p_inst->iface.cnt; +} + +/** + * @brief Interface accessing function + * + * Get interface pointer. + * Interfaces creates continuous array in the memory so it is possible to get + * interface with index 0 and the just iterate to the next one. + * + * @param p_inst Pointer to the class instance + * @param iface_idx Index of the instance to get. + * This is not the interface identifier. + * Technically it is the index of the interface in the class description array. + * @return Pointer to the interface configuration parameters or NULL if given index is out of interface scope for given class. + */ +static inline app_usbd_class_iface_conf_t const * app_usbd_class_iface_get( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx) +{ + ASSERT(NULL != p_inst); + if (iface_idx >= (app_usbd_class_iface_count_get(p_inst))) + { + return NULL; + } + + app_usbd_class_iface_conf_t const * p_interface = + (app_usbd_class_iface_conf_t const * )(p_inst->iface.config); + return &(p_interface[iface_idx]); +} + +/** + * @brief Get interface number + * + * @param p_iface Pointer to interface structure + * + * @return Interface number from interface configuration structure + */ +static inline uint8_t app_usbd_class_iface_number_get( + app_usbd_class_iface_conf_t const * const p_iface) +{ + return p_iface->number; +} + +/** + * @brief Get number of endpoints in interface + * + * @param p_iface Pointer to interface structure + * + * @return Number of endpoints used by given interface + */ +static inline uint8_t app_usbd_class_iface_ep_count_get( + app_usbd_class_iface_conf_t const * const p_iface) +{ + return p_iface->ep_cnt; +} + +/** + * @brief Interface Endpoint accessing function + * + * @param p_iface Interface configuration pointer + * @param ep_idx Endpoint index + * + * @return Endpoint information structure pointer or NULL if given index is outside of endpoints for selected interface. + * + * @sa app_usbd_class_iface_get + */ +static inline app_usbd_class_ep_conf_t const * app_usbd_class_iface_ep_get( + app_usbd_class_iface_conf_t const * const p_iface, + uint8_t ep_idx) +{ + ASSERT(NULL != p_iface); + if (ep_idx >= p_iface->ep_cnt) + { + return NULL; + } + + app_usbd_class_ep_conf_t const * p_ep = + (app_usbd_class_ep_conf_t const * )(((uint8_t const *)p_iface) + p_iface->ep_offset); + return &(p_ep[ep_idx]); +} + +/** + * @brief Translate endpoint address to class index + * + * @param p_iface Interface configuration pointer + * @param ep_address Endpoint address + * + * @return Endpoint index or number of endpoints if not found + * + */ +static inline uint8_t app_usbd_class_iface_ep_idx_get( + app_usbd_class_iface_conf_t const * const p_iface, + nrf_drv_usbd_ep_t ep_address) +{ + ASSERT(NULL != p_iface); + app_usbd_class_ep_conf_t const * p_ep = + (app_usbd_class_ep_conf_t const * )(((uint8_t const *)p_iface) + p_iface->ep_offset); + + uint8_t i; + for (i = 0; i < p_iface->ep_cnt; ++i) + { + if (ep_address == p_ep[i].address) + { + break; + } + } + + return i; +} + +/** + * @brief Get the selected endpoint address + * + * @param p_ep Endpoint configuration structure + * + * @return Endpoint address + */ +static inline nrf_drv_usbd_ep_t app_usbd_class_ep_address_get(app_usbd_class_ep_conf_t const * p_ep) +{ + return (nrf_drv_usbd_ep_t)p_ep->address; +} + +/** + * @brief Get the pointer to the writable instance data + * + * @param p_inst Instance pointer + * @return Pointer to writable instance data + */ +static inline app_usbd_class_data_t * app_usbd_class_data_access( + app_usbd_class_inst_t const * const p_inst) +{ + return p_inst->p_data; +} + +/** + * @name Internal macros for argument mapping + * + * Functions to be used as a mapping macro for @ref MACRO_MAP, @ref MACRO_MAP_FOR or @ref MACRO_MAP_FOR_PARAM + * @{ + */ + /** + * @brief Count the number of endpoints in given configuration + * + * Config should be given as a interface configuration in a brackets: + * @code + * (interface_nr, ep1, ep2, ep3) + * @endcode + * Number of endpoints may vary from 0 to a few (technically up to 16, but it seems not to make sense to use more than 4). + * Interface number is always present. + * + * @param iface_config Single interface configuration (in brackets) + * + * @return Number of endpoints in interface. This is computed value - can be used by compiler but not by preprocessor. + */ + #define APP_USBD_CLASS_CONF_IFACE_EP_COUNT_(iface_config) \ + (NUM_VA_ARGS(BRACKET_EXTRACT(iface_config)) - 1) + + /** + * @brief Adds the number of endpoints in given config to the current value + * + * This is basically @ref APP_USBD_CLASS_CONF_IFACE_EP_COUNT_ with plus sign added. + * + * @param iface_config See parameters documentation in @ref APP_USBD_CLASS_CONF_IFACE_EP_COUNT_ + * + * @return Plus sign followed by number of endpoints in interface. + * + * @sa APP_USBD_CLASS_CONF_IFACE_EP_COUNT_ + */ + #define APP_USBD_CLASS_CONF_IFACE_EP_PLUS_COUNT_(iface_config) \ + + APP_USBD_CLASS_CONF_IFACE_EP_COUNT_(iface_config) + + /** + * @brief Create variable for endpoint + */ + + /** + * @brief Extract endpoints given interface configuration + * + * This macro gets single endpoint configuration and extracts all the endpoints. + * It also adds comma on the end of extracted endpoints. + * This way when this macro is called few times it generates nice list of all endpoints + * that may be used to array initialization. + * + * @param iface_config Single interface configuration in brackets. + * The format should be similar like described in @ref APP_USBD_CLASS_CONF_IFACE_EP_COUNT_. + */ + #define APP_USBD_CLASS_IFACE_EP_EXTRACT_(iface_config) \ + CONCAT_2(APP_USBD_CLASS_IFACE_EP_EXTRACT_, \ + NUM_VA_ARGS_IS_MORE_THAN_1(BRACKET_EXTRACT(iface_config))) \ + (BRACKET_EXTRACT(iface_config)) + + /** + * @brief Auxiliary macro for @ref APP_USBD_CLASS_IFACE_EP_EXTRACT_ + * + * This macro is called when interface has no endpoints + */ + #define APP_USBD_CLASS_IFACE_EP_EXTRACT_0(iface_nr) + + /** + * @brief Auxiliary macro for @ref APP_USBD_CLASS_IFACE_EP_EXTRACT_ + * + * This macro is called when interface has at least one endpoint + */ + #define APP_USBD_CLASS_IFACE_EP_EXTRACT_1(...) \ + APP_USBD_CLASS_IFACE_EP_EXTRACT_1_(__VA_ARGS__) + + #define APP_USBD_CLASS_IFACE_EP_EXTRACT_1_(iface_nr, ...) \ + MACRO_MAP_REC(PARAM_CBRACE, __VA_ARGS__) + + /** + * @brief Generate configuration for single interface + * + * This macro extract configuration for single interface. + * The configuration is inside curly brackets and comma is added on the end. + * This mean it can be directly used to init array of interface configurations. + * + * @param iface_config Single interface configuration + * @param N Currently processed configuration + * @param iface_configs All interfaces configuration in brackets + */ + #define APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_(iface_config, N, iface_configs) \ + CONCAT_2(APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_, \ + NUM_VA_ARGS_IS_MORE_THAN_1(BRACKET_EXTRACT(iface_config))) \ + (N, iface_configs, BRACKET_EXTRACT(iface_config)) + + #define APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_x(iface_config, N, iface_configs) \ + [N] = !!!iface_config!!! + /** + * @brief Auxiliary macro for @ref APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_ + * + * This macro is called when interface has no endpoints + */ + #define APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_0(N, iface_configs, iface_nr) \ + APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_0_(N, iface_configs, iface_nr) + #define APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_0_(N, iface_configs, iface_nr) \ + { .number = iface_nr, .ep_cnt = 0, .ep_offset = 0 }, + + /** + * @brief Auxiliary macro for @ref APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_ + * + * This macro is called when interface has at last one endpoint + */ + #define APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_1(N, iface_configs, ...) \ + APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_1_(N, iface_configs, __VA_ARGS__) + #define APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_1_(N, iface_configs, iface_nr, ...) \ + { .number = iface_nr, .ep_cnt = NUM_VA_ARGS(__VA_ARGS__), \ + .ep_offset = APP_USBD_CLASS_CONF_TOTAL_EP_COUNT_N(N, iface_configs) * \ + sizeof(app_usbd_class_ep_conf_t) \ + + ((NUM_VA_ARGS(BRACKET_EXTRACT(iface_configs)) - N) * \ + sizeof(app_usbd_class_iface_conf_t)) \ + }, + +/** @} */ + + +/** + * @name Macros that uses mapping macros internally + * + * Auxiliary macros that uses mapping macros to make some calculations or realize other functionality. + * Mapped here for easier unit testing and to hide complex mapping functions calling. + * @{ + */ + +/** + * @brief Count total number of endpoints + * + * @param iface_configs List of interface configurations like explained + * in documentation for @ref APP_USBD_CLASS_INSTANCE_TYPEDEF + * + * @return The equation to calculate the number of endpoints by compiler. + */ +#define APP_USBD_CLASS_CONF_TOTAL_EP_COUNT(iface_configs) \ + (0 MACRO_MAP(APP_USBD_CLASS_CONF_IFACE_EP_PLUS_COUNT_, BRACKET_EXTRACT(iface_configs))) + +/** + * @brief Count total number of endpoint up-to interface index + * + * The version of @ref APP_USBD_CLASS_CONF_TOTAL_EP_COUNT macro which takes the + * number of interfaces to analyze. + * + * @param N Number of interfaces to analyze + * @param iface_configs List of interface configurations like explained + * in documentation for @ref APP_USBD_CLASS_INSTANCE_TYPEDEF + * + * @return The equation to calculate the number of endpoints by compiler. + */ +#define APP_USBD_CLASS_CONF_TOTAL_EP_COUNT_N(N, iface_configs) \ + (0 MACRO_MAP_N(N, APP_USBD_CLASS_CONF_IFACE_EP_PLUS_COUNT_, BRACKET_EXTRACT(iface_configs))) + +/** + * @brief Extract configurations for interfaces + * + * This macro extracts the configurations for every interface. + * Basically uses the @ref APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_ macro on every + * configuration found. + * + * This should generate interface configuration initialization data + * in comma separated initializers in curly braces. + * + * @param iface_configs List of interface configurations like explained + * in documentation for @ref APP_USBD_CLASS_INSTANCE_TYPEDEF + * + * @return Comma separated initialization data for all interfaces. + */ +/*lint -emacro( (40), APP_USBD_CLASS_IFACES_CONFIG_EXTRACT) */ +#define APP_USBD_CLASS_IFACES_CONFIG_EXTRACT(iface_configs) \ + MACRO_MAP_FOR_PARAM(iface_configs, \ + APP_USBD_CLASS_IFACE_CONFIG_EXTRACT_, \ + BRACKET_EXTRACT(iface_configs)) + +/** + * @brief Extract all endpoints + * + * Macro that extracts all endpoints from every interface + * + * @param iface_configs List of interface configurations like explained + * in documentation for @ref APP_USBD_CLASS_INSTANCE_TYPEDEF + * + * @return Comma separated list of endpoints + */ +/*lint -emacro( (40), APP_USBD_CLASS_IFACES_EP_EXTRACT) */ +#define APP_USBD_CLASS_IFACES_EP_EXTRACT(iface_configs) \ + MACRO_MAP(APP_USBD_CLASS_IFACE_EP_EXTRACT_, BRACKET_EXTRACT(iface_configs)) + + +/** @} */ + + +/** + * @brief USBD instance of class mnemonic + * + * Macro that generates mnemonic for the name of the structure that describes instance for selected class. + * + * @param type_name The name of the instance without _t postfix + * + * @return The name with the right postfix to create the name for the type for the class. + */ +#define APP_USBD_CLASS_INSTANCE_TYPE(type_name) CONCAT_2(type_name, _t) + +/** + * @brief USBD data for instance class mnemonic + * + * The mnemonic of the variable type that holds writable part of the class instance. + * + * @param type_name The name of the instance without _t postfix + * + * @return The name with the right postfix to create the name for the data type for the class. + */ +#define APP_USBD_CLASS_DATA_TYPE(type_name) CONCAT_2(type_name, _data_t) + +/** + * @brief Declare class specific member of class instance + * + * @param type Type of the attached class configuration. + * + * @sa APP_USBD_CLASS_INSTANCE_TYPEDEF + */ +#define APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC(type) type class_part; + +/** + * @brief Used if there is no class specific configuration + * + * This constant can be used if there is no specific configuration inside created instance + * + * @sa APP_USBD_CLASS_INSTANCE_TYPEDEF + */ +#define APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC_NONE + +/** + * @brief Declare class specific member of class data + * + * @param type Type of the attached class data. + * + * @sa APP_USBD_CLASS_DATA_TYPEDEF + */ +#define APP_USBD_CLASS_DATA_SPECIFIC_DEC(type) APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC(type) + +/** + * @brief Used if there is no class specific data + * + * This constant can be used if there is no specific writable data inside created instance + * + * @sa APP_USBD_CLASS_DATA_TYPEDEF + */ +#define APP_USBD_CLASS_DATA_SPECIFIC_DEC_NONE APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC_NONE + + + + +/** + * @brief Instance structure declaration + * + * The macro that declares a variable type that would be used to store given class instance. + * Class instance stores all the data from @ref app_usbd_class_inst_t and overlaid data for specified class. + * + * The structure of interface configuration data: + * @code + * ( + * (iface1_nr, (ep1, ep2, ep3)), + (iface2_nr), + (iface3_nr, (ep4)) + * ) + * @endcode + * + * @param type_name The name of the instance without _t postfix. + * @param interfaces_configs List of interface configurations like explained above. + * @param class_config_dec Result of the macro + * @ref APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC or + * @ref APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC_NONE + * + * @return The definition of the structure type that holds all the required data. + * + * @note It should not be used directly in the final application. See @ref APP_USBD_CLASS_DATA_TYPEDEF instead. + * + * @note APP_USBD_CLASS_DATA_TYPEDEF has to be called first for the compilation to success. + * + * @sa APP_USBD_CLASS_TYPEDEF + */ +#define APP_USBD_CLASS_INSTANCE_TYPEDEF(type_name, interfaces_configs, class_config_dec) \ + typedef union CONCAT_2(type_name, _u) \ + { \ + app_usbd_class_inst_t base; \ + struct \ + { \ + APP_USBD_CLASS_DATA_TYPE(type_name) * p_data; \ + app_usbd_class_methods_t const * p_class_methods; \ + struct \ + { \ + uint8_t cnt; \ + app_usbd_class_iface_conf_t \ + config[NUM_VA_ARGS(BRACKET_EXTRACT(interfaces_configs))]; \ + app_usbd_class_ep_conf_t \ + ep[APP_USBD_CLASS_CONF_TOTAL_EP_COUNT(interfaces_configs)]; \ + } iface; \ + class_config_dec \ + } specific; \ + } APP_USBD_CLASS_INSTANCE_TYPE(type_name) + +/** + * @brief Same as @ref APP_USBD_CLASS_INSTANCE_TYPEDEF but for class with EP0 only. + */ +#define APP_USBD_CLASS_INSTANCE_NO_EP_TYPEDEF(type_name, interfaces_configs, class_config_dec) \ + typedef union CONCAT_2(type_name, _u) \ + { \ + app_usbd_class_inst_t base; \ + struct \ + { \ + APP_USBD_CLASS_DATA_TYPE(type_name) * p_data; \ + app_usbd_class_methods_t const * p_class_methods; \ + struct \ + { \ + uint8_t cnt; \ + app_usbd_class_iface_conf_t \ + config[NUM_VA_ARGS(BRACKET_EXTRACT(interfaces_configs))]; \ + } iface; \ + class_config_dec \ + } specific; \ + } APP_USBD_CLASS_INSTANCE_TYPE(type_name) + +/** + * @brief Writable data structure declaration + * + * The macro that declares a variable type that would be used to store given class writable data. + * Writable data contains base part of the type @ref app_usbd_class_data_t followed by + * class specific data. + * + * @param type_name The name of the type without _t postfix. + * @param class_data_dec Result of the macro + * @ref APP_USBD_CLASS_DATA_SPECIFIC_DEC or + * @ref APP_USBD_CLASS_DATA_SPECIFIC_DEC_NONE + * + * @return The definition of the structure type that holds all the required writable data + * + * @note It should not be used directly in the final application. See @ref APP_USBD_CLASS_DATA_TYPEDEF instead. + * + * @sa APP_USBD_CLASS_TYPEDEF + */ +#define APP_USBD_CLASS_DATA_TYPEDEF(type_name, class_data_dec) \ + typedef struct \ + { \ + app_usbd_class_data_t base; \ + class_data_dec \ + }APP_USBD_CLASS_DATA_TYPE(type_name) + + +/** + * @brief Declare all data types required by the class instance + * + * Macro that declares data type first and then instance type. + * + * @param type_name The name of the type without _t postfix. + * @param interface_configs List of interface configurations like in @ref APP_USBD_CLASS_INSTANCE_TYPEDEF. + * @param class_config_dec Result of the macro + * @ref APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC or + * @ref APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC_NONE + * @param class_data_dec Result of the macro + * @ref APP_USBD_CLASS_DATA_SPECIFIC_DEC or + * @ref APP_USBD_CLASS_DATA_SPECIFIC_DEC_NONE + * + * @return Declaration of the data type for the instance and instance itself. + * + * @sa APP_USBD_CLASS_DATA_TYPEDEF + * @sa APP_USBD_CLASS_INSTANCE_TYPEDEF + */ +#define APP_USBD_CLASS_TYPEDEF(type_name, interface_configs, class_config_dec, class_data_dec) \ + APP_USBD_CLASS_DATA_TYPEDEF(type_name, class_data_dec); \ + APP_USBD_CLASS_INSTANCE_TYPEDEF(type_name, interface_configs, class_config_dec) + +/** + * @brief Same as @ref APP_USBD_CLASS_TYPEDEF but for class with EP0 only. + */ +#define APP_USBD_CLASS_NO_EP_TYPEDEF(type_name, \ + interface_configs, \ + class_config_dec, \ + class_data_dec) \ + APP_USBD_CLASS_DATA_TYPEDEF(type_name, class_data_dec); \ + APP_USBD_CLASS_INSTANCE_NO_EP_TYPEDEF(type_name, interface_configs, class_config_dec) + +/** + * @brief Forward declaration of type defined by @ref APP_USBD_CLASS_TYPEDEF + * + * @param type_name The name of the type without _t postfix. + * */ +#define APP_USBD_CLASS_FORWARD(type_name) union CONCAT_2(type_name, _u) + +/** + * @brief Generate the initialization data for + * + * Macro that generates the initialization data for instance. + * + * @param p_ram_data Pointer to writable instance data structure + * @param class_methods Class methods + * @param interfaces_configs Exactly the same interface config data that in @ref APP_USBD_CLASS_INSTANCE_TYPEDEF + * @param class_config_part Configuration part. The data should be inside brackets. + * Any data here would be removed from brackets and then put as an initialization + * data for class_part member of instance structure. + * + * @note It should not be used directly in the final application. See @ref APP_USBD_CLASS_INST_DEF instead. + */ +#define APP_USBD_CLASS_INSTANCE_INITVAL(p_ram_data, \ + class_methods, \ + interfaces_configs, \ + class_config_part) \ + { \ + .specific = { \ + .p_data = p_ram_data, \ + .p_class_methods = class_methods, \ + .iface = { \ + .cnt = NUM_VA_ARGS(BRACKET_EXTRACT(interfaces_configs)), \ + .config = { APP_USBD_CLASS_IFACES_CONFIG_EXTRACT(interfaces_configs) }, \ + .ep = { APP_USBD_CLASS_IFACES_EP_EXTRACT(interfaces_configs) } \ + }, \ + BRACKET_EXTRACT(class_config_part) \ + } \ + } + +/** + * @brief Same as @ref APP_USBD_CLASS_INSTANCE_INITVAL but for class with EP0 only. + */ +#define APP_USBD_CLASS_INSTANCE_NO_EP_INITVAL(p_ram_data, \ + class_methods, \ + interfaces_configs, \ + class_config_part) \ + { \ + .specific = { \ + .p_data = p_ram_data, \ + .p_class_methods = class_methods, \ + .iface = { \ + .cnt = NUM_VA_ARGS(BRACKET_EXTRACT(interfaces_configs)), \ + .config = { APP_USBD_CLASS_IFACES_CONFIG_EXTRACT(interfaces_configs) } \ + }, \ + BRACKET_EXTRACT(class_config_part) \ + } \ + } + +/** + * @brief Define the base class instance + * + * Macro that defines whole instance variable and fill it with initialization data. + * + * The tricky part is @c class_config_part. + * The configuration data here has to be placed inside brackets. + * Then any type of values can be used depending on the type used in @ref APP_USBD_CLASS_TYPEDEF. + * If instance does not has any specyfic data, use just empty bracket here. + * @code + * APP_USBD_CLASS_TYPEDEF( + * some_base_class, + * CLASS_BASE_CONFIGURATION, + * APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC_NONE, + * APP_USBD_CLASS_DATA_SPECIFIC_DEC_NONE + * ); + * APP_USBD_CLASS_INST_DEF( + * some_base_class_inst, + * some_base_class, + * base_class_event_handler, + * CLASS_BASE_CONFIGURATION, + * () // Empty configuration + * ); + * @endcode + * + * If the type of instance configuration is simple type, just provide initialization value: + * @code + * APP_USBD_CLASS_TYPEDEF( + * some_base_class, + * CLASS_BASE_CONFIGURATION, + * APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC_NONE, + * APP_USBD_CLASS_DATA_SPECIFIC_DEC(uint8_t) + * ); + * APP_USBD_CLASS_INST_DEF( + * some_base_class_inst, + * some_base_class, + * base_class_event_handler, + * CLASS_BASE_CONFIGURATION, + * (12) // Example values + * ); + * @endcode + * + * If the type of instance configuration is structure, provide initialization value for the whole structure: + * @code + * typedef structure + * { + * uint32_t p1; + * uint8_t p2; + * }my_config_t; + * + * APP_USBD_CLASS_TYPEDEF( + * some_base_class, + * CLASS_BASE_CONFIGURATION, + * APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC_NONE, + * APP_USBD_CLASS_DATA_SPECIFIC_DEC(my_config_t) + * ); + * APP_USBD_CLASS_INST_DEF( + * some_base_class_inst, + * some_base_class, + * base_class_event_handler, + * CLASS_BASE_CONFIGURATION, + * ({12, 3}) // Example values + * ); + * @endcode + * + * @param instance_name The name of created instance variable. + * It would be constant variable and its type would be app_usbd_class_inst_t. + * @param type_name The name of the variable type. It has to be the same type that was passed to + * @ref APP_USBD_CLASS_TYPEDEF + * @param class_methods Class unified interface. + * @param interfaces_configs The same configuration data that the one passed to @ref APP_USBD_CLASS_TYPEDEF + * @param class_config_part Configuration data to the type that was declared by class_data_dec when calling + * @ref APP_USBD_CLASS_TYPEDEF. + * Configuration data has to be provided in brackets. + * It would be extracted from brackets and placed in initialization part of configuration structure. + * See detailed description of this macro for more informations. + */ +#define APP_USBD_CLASS_INST_DEF(instance_name, \ + type_name, \ + class_methods, \ + interfaces_configs, \ + class_config_part) \ + static APP_USBD_CLASS_DATA_TYPE(type_name) CONCAT_2(instance_name, _data); \ + static const APP_USBD_CLASS_INSTANCE_TYPE(type_name) instance_name = \ + APP_USBD_CLASS_INSTANCE_INITVAL( \ + &CONCAT_2(instance_name, _data), \ + class_methods, \ + interfaces_configs, \ + class_config_part) + + +/** + * @brief Define the base class instance in global scope + * + * This is the same macro like @ref APP_USBD_CLASS_INST_DEF but it creates the instance + * without static keyword. + * + * @param instance_name See documentation for @ref APP_USBD_CLASS_INST_DEF + * @param type_name See documentation for @ref APP_USBD_CLASS_INST_DEF + * @param class_methods See documentation for @ref APP_USBD_CLASS_INST_DEF + * @param interfaces_configs See documentation for @ref APP_USBD_CLASS_INST_DEF + * @param class_config_part See documentation for @ref APP_USBD_CLASS_INST_DEF + */ +#define APP_USBD_CLASS_INST_GLOBAL_DEF(instance_name, \ + type_name, \ + class_methods, \ + interfaces_configs, \ + class_config_part) \ + static APP_USBD_CLASS_DATA_TYPE(type_name) CONCAT_2(instance_name, _data); \ + const APP_USBD_CLASS_INSTANCE_TYPE(type_name) instance_name = \ + APP_USBD_CLASS_INSTANCE_INITVAL( \ + &CONCAT_2(instance_name, _data), \ + class_methods, \ + interfaces_configs, \ + class_config_part) + +/** + * @brief Same as @ref APP_USBD_CLASS_INST_GLOBAL_DEF but for class with EP0 only. + */ +#define APP_USBD_CLASS_INST_NO_EP_GLOBAL_DEF(instance_name, \ + type_name, \ + class_methods, \ + interfaces_configs, \ + class_config_part) \ + static APP_USBD_CLASS_DATA_TYPE(type_name) CONCAT_2(instance_name, _data); \ + const APP_USBD_CLASS_INSTANCE_TYPE(type_name) instance_name = \ + APP_USBD_CLASS_INSTANCE_NO_EP_INITVAL( \ + &CONCAT_2(instance_name, _data), \ + class_methods, \ + interfaces_configs, \ + class_config_part) +/** + * @brief Access class specific configuration + * + * Macro that returns class specific configuration. + * + * @param[in] p_inst Instance pointer + * + * @return A pointer for class specific part of the instance + * + * @note If macro is used on the instance that has no class specific configuration + * an error would be generated during compilation. + */ +#define APP_USBD_CLASS_GET_SPECIFIC_CONFIG(p_inst) (&((p_inst)->specific.class_part)) + +/** + * @brief Access class specific data + * + * @param[in] p_inst Instance pointer + * + * @return A pointer for class specific part of writable data + * + * @note If macro is used on the instance that has no class specific data + * an error would be generated during compilation. + */ +#define APP_USBD_CLASS_GET_SPECIFIC_DATA(p_inst) (&(((p_inst)->specific.p_data)->class_part)) + +/** + * @brief Macro to get base instance from class specific instance + * + * This macro may be used on class specific instance to get base instance that + * can be processed by base instance access functions. + * Class specific instance can be just casted to class base instance, + * but then we would totally lost type safety. + * + * A little more safe is to use pointer to base member of class instance. + * This would generate an error when used on any variable that has no base member + * and would generate also error if this base member is wrong type. + */ +#define APP_USBD_CLASS_BASE_INSTANCE(p_inst) (&((p_inst)->base)) + +/*lint -emacro(142 438 616 646, APP_USBD_CLASS_DESCRIPTOR_INIT, APP_USBD_CLASS_DESCRIPTOR_BEGIN, APP_USBD_CLASS_DESCRIPTOR_YIELD, APP_USBD_CLASS_DESCRIPTOR_END, APP_USBD_CLASS_DESCRIPTOR_WRITE)*/ + +/** + * @brief Initialize class descriptor + * + * @param[in] p_ctx Class descriptor context + */ + +#define APP_USBD_CLASS_DESCRIPTOR_INIT(p_ctx) \ + (p_ctx)->line = 0; + +/** + * @brief Begin class descriptor + * + * @param[in] p_ctx Class descriptor context + * @param[in] p_buff Buffer to write into + * @param[in] max_size Size of the buffer + */ + +#define APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size) \ + ASSERT((p_ctx) != NULL); \ + app_usbd_class_descriptor_state_t this_descriptor_feed; \ + this_descriptor_feed.p_buffer = (p_buff); \ + this_descriptor_feed.current_size = 0; \ + this_descriptor_feed.maximum_size = (max_size); \ + this_descriptor_feed.p_context = (p_ctx); \ + switch ((this_descriptor_feed.p_context)->line) \ + { \ + case 0: \ + ; + +/** + * @brief Yield class descriptor + * + */ + +#define APP_USBD_CLASS_DESCRIPTOR_YIELD() \ +do \ +{ \ + (this_descriptor_feed.p_context)->line = __LINE__; \ + return true; \ + case __LINE__: \ + ; \ +} while (0) + +/*lint -emacro(438 527, APP_USBD_CLASS_DESCRIPTOR_END)*/ + +/** + * @brief End class descriptor + * + * This function has to be called at the end of class descriptor feeder function. + * No other operations in feeder function can be done after calling it. + */ + +#define APP_USBD_CLASS_DESCRIPTOR_END() \ + APP_USBD_CLASS_DESCRIPTOR_YIELD(); \ + } \ + (this_descriptor_feed.p_context)->line = 0; \ + return false; + + +/** + * @brief Write descriptor using protothreads + * + * This function writes one byte to the buffer with offset. If buffer is full + * it yields. + * + * It is used by the class descriptor feeders internally. + * + * @ref APP_USBD_CLASS_DESCRIPTOR_BEGIN has to be called before using this function. + * @ref APP_USBD_CLASS_DESCRIPTOR_END has to be called after last use of this function. + * + * @param data Byte to be written to buffer + */ +#define APP_USBD_CLASS_DESCRIPTOR_WRITE(data) \ +do \ +{ \ + (this_descriptor_feed.p_context)->data_buffer = (data); \ + if (this_descriptor_feed.current_size >= this_descriptor_feed.maximum_size) \ + { \ + APP_USBD_CLASS_DESCRIPTOR_YIELD(); \ + } \ + if(this_descriptor_feed.p_buffer != NULL) \ + { \ + *(this_descriptor_feed.p_buffer + this_descriptor_feed.current_size) = \ + (this_descriptor_feed.p_context)->data_buffer; \ + } \ + this_descriptor_feed.current_size++; \ +} while(0); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_CLASS_BASE_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_core.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_core.c new file mode 100644 index 0000000..c2b02ce --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_core.c @@ -0,0 +1,1222 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(APP_USBD) + +#include "app_usbd_core.h" +#include "app_usbd.h" +#include "app_usbd_request.h" +#include "app_usbd_string_desc.h" +#include "nrf.h" +#include "nrf_atomic.h" +#include "app_util_platform.h" +#include "app_usbd.h" +#include "app_usbd_class_base.h" + +#define NRF_LOG_MODULE_NAME app_usbd_core + +#if APP_USBD_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL APP_USBD_CONFIG_LOG_LEVEL +#define NRF_LOG_INFO_COLOR APP_USBD_CONFIG_INFO_COLOR +#define NRF_LOG_DEBUG_COLOR APP_USBD_CONFIG_DEBUG_COLOR +#else //APP_USBD_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL 0 +#endif //APP_USBD_CONFIG_LOG_ENABLED +#include "nrf_log.h" +NRF_LOG_MODULE_REGISTER(); + +/* Test if VID was configured */ +#ifndef APP_USBD_VID +#error APP_USBD_VID not properly defined. +#endif + + +/* Device version checking */ +#if defined(APP_USBD_DEVICE_VER_MAJOR) && defined(APP_USBD_DEVICE_VER_MINOR) + #if ((APP_USBD_DEVICE_VER_MAJOR)) > 99 || ((APP_USBD_DEVICE_VER_MINOR) > 99) + #error Major and minor device version value have to be limited to 99. + #endif +#else + #error The definition of a pair APP_USBD_DEVICE_VER_MAJOR and APP_USBD_DEVICE_VER_MINOR required. +#endif + +/** + * @internal + * @defgroup app_usbd_core_internals USB Device high level library core module internals + * @ingroup app_usbd_core + * + * Internal variables, auxiliary macros and functions of USBD high level core module. + * @{ + */ + +/** @brief Make USB power value */ +#define APP_USBD_POWER_MAKE(ma) (((ma) + 1) / 2) + +/** + @brief Default device descriptor initializer @ref app_usbd_descriptor_device_t +* */ +#define APP_USBD_CORE_DEVICE_DESCRIPTOR { \ + .bLength = sizeof(app_usbd_descriptor_device_t), /* descriptor size */ \ + .bDescriptorType = APP_USBD_DESCRIPTOR_DEVICE, /* descriptor type */ \ + .bcdUSB = APP_USBD_BCD_VER_MAKE(2,0), /* USB BCD version: 2.0 */ \ + .bDeviceClass = 0, /* device class: 0 - specified by interface */ \ + .bDeviceSubClass = 0, /* device subclass: 0 - specified by interface */ \ + .bDeviceProtocol = 0, /* device protocol: 0 - specified by interface */ \ + .bMaxPacketSize0 = NRF_DRV_USBD_EPSIZE, /* endpoint size: fixed to: NRF_DRV_USBD_EPSIZE*/ \ + .idVendor = APP_USBD_VID, /* Vendor ID*/ \ + .idProduct = APP_USBD_PID, /* Product ID*/ \ + .bcdDevice = APP_USBD_BCD_VER_MAKE( /* Device version BCD */ \ + APP_USBD_DEVICE_VER_MAJOR, \ + APP_USBD_DEVICE_VER_MINOR), \ + .iManufacturer = APP_USBD_STRING_ID_MANUFACTURER, /* String ID: manufacturer */ \ + .iProduct = APP_USBD_STRING_ID_PRODUCT, /* String ID: product */ \ + .iSerialNumber = APP_USBD_STRING_ID_SERIAL, /* String ID: serial */ \ + .bNumConfigurations = 1 /* Fixed value: only one configuration supported*/\ +} + + +#define APP_USBD_CORE_CONFIGURATION_DESCRIPTOR { \ + .bLength = sizeof(app_usbd_descriptor_configuration_t), \ + .bDescriptorType = APP_USBD_DESCRIPTOR_CONFIGURATION, \ + .wTotalLength = 0, /*Calculated dynamically*/ \ + .bNumInterfaces = 0, /*Calculated dynamically*/ \ + .bConfigurationValue = 1, /*Value passed to set configuration*/ \ + .iConfiguration = 0, /*Configuration ID: fixed to 0*/ \ + .bmAttributes = APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_ALWAYS_SET_MASK | \ + ((APP_USBD_CONFIG_SELF_POWERED) ? \ + APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_SELF_POWERED_MASK \ + : \ + 0), \ + .bMaxPower = APP_USBD_POWER_MAKE(APP_USBD_CONFIG_MAX_POWER), \ +} + +/** + * @brief Device descriptor instance. + * + * @note + * Constant part of the device descriptor. + * Values that must be calculated are updated directly in the buffer + * just before the transmission. + */ +static const app_usbd_descriptor_device_t m_device_dsc = + APP_USBD_CORE_DEVICE_DESCRIPTOR; + +/** + * @brief Configuration descriptor instance. + * + * @note + * Constant part of the device descriptor. + * Values that must be calculated are updated directly in the buffer + * just before the transmission. + */ +static const app_usbd_descriptor_configuration_t m_configuration_dsc = + APP_USBD_CORE_CONFIGURATION_DESCRIPTOR; + +/* Required early declaration of event handler function */ +static ret_code_t app_usbd_core_event_handler(app_usbd_class_inst_t const * const p_inst, + app_usbd_complex_evt_t const * const p_event); + +/** + * @brief Current USB device state + * + * This variable is updated automatically by core library. + */ +static app_usbd_state_t m_app_usbd_state = APP_USBD_STATE_Disabled; + +/** + * @brief Active device features + * + * @note Only @ref APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP is supported for device + */ +static uint8_t m_device_features_state; + +/** + * @brief Remote wake-up pending flag + */ +static nrf_atomic_flag_t m_rwu_pending; + + +/** + * @brief Core class methods + * + * Base methods interface for core class. + * This is quite specific class - it would be only connected into endpoint 0. + * Not connected into instances list. + */ +static const app_usbd_class_methods_t m_core_methods = { + .event_handler = app_usbd_core_event_handler, + .feed_descriptors = NULL, +}; + +/** + * @brief Setup transfer buffer + */ +static uint8_t m_setup_transfer_buff[NRF_DRV_USBD_EPSIZE]; + + +/** + * @brief Handler for outgoing setup data + * + * @todo RK documentation + */ +static app_usbd_core_setup_data_handler_desc_t m_ep0_handler_desc; + +#define APP_USBD_CORE_CLASS_INSTANCE_CONFIG () + + +/*lint -u -save -e26 -e40 -e64 -e123 -e505 -e651*/ + +/** + * @brief Core instance + * + * Create instance that would be connected into endpoints in USBD library. + */ +APP_USBD_CLASS_INST_GLOBAL_DEF( + app_usbd_core_inst, + app_usbd_core, + &m_core_methods, + APP_USBD_CORE_CLASS_CONFIGURATION, + () ); +/*lint -restore*/ + +/** + * @brief Set the new USB state + * + * Function changes the internal status of the bus. + * If the bus status is different than the one configured, an event is passed to all + * the instances. + * + * @param state New state to be set + * + * @sa usbd_core_state_get + */ +static void usbd_core_state_set(app_usbd_state_t state) +{ + if (m_app_usbd_state != state) + { + m_app_usbd_state = state; + if(state != APP_USBD_STATE_Configured) + { + CLR_BIT(m_device_features_state, APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP); + } + static const app_usbd_evt_t evt_data = { + .type = APP_USBD_EVT_STATE_CHANGED + }; + app_usbd_event_execute((app_usbd_internal_evt_t const *)&evt_data); + } +} + +/** + * @brief Get the current USB state - internal function + * + * This is just a wrapper for @ref app_usbd_core_state_get + * to make symmetrical function to the internal @ref usbd_core_state_set. + * + * @return Current USB state + * + * @sa usbd_core_state_set + * @sa app_usbd_core_state_get + */ +static inline app_usbd_state_t usbd_core_state_get(void) +{ + return m_app_usbd_state; +} + +/** + * @brief Check current USBD power connection status + * + */ +static inline bool usbd_core_power_is_detected(void) +{ + return 0 != ( (NRF_POWER->USBREGSTATUS) & POWER_USBREGSTATUS_VBUSDETECT_Msk); +} + +/** + * @brief Clear current EP0 handler + * + * Function just clears the EP0 handler without calling it + */ +static inline void usbd_core_ep0_handler_clear(void) +{ + m_ep0_handler_desc.handler = NULL; +} + +/** + * @brief Safely call EP0 handler + * + * Function calls EP0 handler only if its pointer is non-zero. + * + * @param status Status to send as a handler parameter. + */ +static inline ret_code_t usbd_core_ep0_handler_call_and_clear(nrf_drv_usbd_ep_status_t status) +{ + app_usbd_core_setup_data_handler_t handler = m_ep0_handler_desc.handler; + if (NULL != handler) + { + usbd_core_ep0_handler_clear(); + return handler(status, m_ep0_handler_desc.p_context); + } + + return NRF_ERROR_NULL; +} + +/** + * @brief Check if EP0 handler is configured + * + * EP0 handler is configured is any instance that has processed SETUP command + * expects some incoming / outgoing data. + * + * EP0 handler should be cleared automatically just before it is called + * (see @ref usbd_core_ep0_handler_call_and_clear). + * If instance requires more data - it has to setup EP0 handler once more time + * (see @ref app_usbd_core_setup_data_handler_set). + * + * This function adds small layer of abstraction for checking if EP0 handler + * is already configured. + * + * @retval true EP0 handler is set + * @retval false EP0 handler is cleared + */ +static inline bool usb_core_ep0_handler_check(void) +{ + return (NULL != m_ep0_handler_desc.handler); +} + +/** + * @brief Empty data handler + * + * Data handler used only to mark that there is requested data during SETUP. + * + * @return Always NRF_SUCCESS + * @sa setup_empty_data_handler_desc + */ +static ret_code_t setup_data_handler_empty(nrf_drv_usbd_ep_status_t status, void * p_contex) +{ + UNUSED_PARAMETER(status); + UNUSED_PARAMETER(p_contex); + return NRF_SUCCESS; +} + +/** + * @brief + * + * @todo RK Documentation + */ +static app_usbd_core_setup_data_handler_desc_t const m_setup_data_handler_empty_desc = +{ + .handler = setup_data_handler_empty, + .p_context = NULL +}; + +/** + * @brief Structure used as a context for descriptor feeder + * + * Structure with all the data required to process instances to generate descriptor + * data chunk. + */ +typedef struct +{ + app_usbd_class_inst_t const * p_cinst; //!< The class instance that is to be processed next. + const uint8_t * p_desc; //!< Pointer at current descriptor or NULL if finished. + /**< + * If the value passed by @ref p_desc is NULL on transfer function enter it means that ZLP is required. + * Or it is time to finish the transfer (depending on @c total_left). + */ + size_t desc_left; //!< Number of bytes left in the current class descriptor to send + size_t total_left; //!< Number of bytes left that was requested by the host + app_usbd_class_descriptor_ctx_t feed_thread; //!< Class descriptor context +} app_usbd_core_descriptor_conf_feed_data_t; + +/** + * @brief Default data used by the feeder + * + * + */ +static app_usbd_core_descriptor_conf_feed_data_t m_descriptor_conf_feed_data; + +/** + * @brief Descriptor feeder + * + * Descriptor feeder is used as an callback function when descriptors are + * transfered and buffer is ready for next data. + * It prepares next chunk of data to be sent. + * + * @param p_next See @ref nrf_drv_usbd_next_transfer_handler_t documentation. + * @param p_context Pointer to @ref app_usbd_core_descriptor_feed_data_t data type. + * @param ep_size The size of the endpoint. + * + * @return See @ref nrf_drv_usbd_next_transfer_handler_t documentation. + */ +static bool usbd_descriptor_conf_feeder( + nrf_drv_usbd_ep_transfer_t * p_next, + void * p_context, + size_t ep_size) +{ + bool continue_req = true; + + app_usbd_core_descriptor_conf_feed_data_t * p_data = p_context; + + + if ((p_data->p_desc == NULL) && (app_usbd_class_next_get(p_data->p_cinst) == NULL) + && (p_data->desc_left == 0)) + { + /* ZLP */ + continue_req = false; + p_next->p_data.tx = NULL; + p_next->size = 0; + } + else + { + ASSERT(ep_size <= NRF_DRV_USBD_FEEDER_BUFFER_SIZE); + uint8_t * p_tx_buff; + size_t size = 0; /* Currently added number of bytes */ + size_t tx_size; /* Number of bytes to send right now */ + bool feeding = false; + + /* Feeder function can use the USBD driver internal buffer */ + p_tx_buff = nrf_drv_usbd_feeder_buffer_get(); + + tx_size = MIN(ep_size, p_data->total_left); + + while (0 != tx_size) + { + size_t to_copy = MIN(tx_size, p_data->desc_left); + + /* First transfer */ + if (p_data->p_desc != NULL) + { + memcpy(p_tx_buff + size, p_data->p_desc, to_copy); + p_data->p_desc = NULL; + } + /* Starting with second transfer */ + else if (0 < p_data->desc_left) + { + UNUSED_RETURN_VALUE(p_data->p_cinst->p_class_methods->feed_descriptors( + &p_data->feed_thread, p_data->p_cinst, + (uint8_t *)p_tx_buff + size, to_copy)); + feeding = true; + } + else + { + ; + } + + p_data->desc_left -= to_copy; + p_data->total_left -= to_copy; + tx_size -= to_copy; + size += to_copy; + + /* Switch to next class if no descriptor left and first feeding was done */ + if(p_data->desc_left == 0 && feeding) + { + p_data->p_cinst = app_usbd_class_next_get(p_data->p_cinst); + } + + if (0 == p_data->total_left) + { + continue_req = false; + } + else if (0 == p_data->desc_left) + { + if (NULL == p_data->p_cinst) + { + p_data->p_desc = NULL; + /* No more data - check if ZLP is required */ + if (size > 0) + { + if (size < ep_size) + { + continue_req = false; + } + } + break; + } + else + { + /* New class - count descriptor size and initialize feeding thread */ + app_usbd_class_descriptor_ctx_t desiz; + APP_USBD_CLASS_DESCRIPTOR_INIT(&desiz); + while(p_data->p_cinst->p_class_methods->feed_descriptors( + &desiz, p_data->p_cinst, NULL, sizeof(uint8_t))) + { + p_data->desc_left++; + } + APP_USBD_CLASS_DESCRIPTOR_INIT(&p_data->feed_thread); + } + } + else + { + /* Nothing to do */ + } + } + p_next->p_data.tx = p_tx_buff; + p_next->size = size; + } + return continue_req; +} + +/** + * @brief Standard endpoint request handle + * + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + */ +static ret_code_t setup_endpoint_req_std(app_usbd_setup_evt_t const * p_setup_ev) +{ + if (APP_USBD_SETUP_REQTYPE_STD != app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + nrf_drv_usbd_ep_t ep_addr = (nrf_drv_usbd_ep_t)(p_setup_ev->setup.wIndex.lb); + app_usbd_state_t usb_state = usbd_core_state_get(); + + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_SETUP_STDREQ_GET_STATUS: + { + if ((usb_state == APP_USBD_STATE_Configured) || (NRF_USBD_EP_NR_GET(ep_addr) == 0)) + { + size_t tx_size; + uint16_t * p_tx_buff = app_usbd_core_setup_transfer_buff_get(&tx_size); + + p_tx_buff[0] = nrf_drv_usbd_ep_stall_check(ep_addr) ? 1 : 0; + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_tx_buff, sizeof(uint16_t)); + } + else + { + return NRF_ERROR_INVALID_STATE; + } + } + case APP_USBD_SETUP_STDREQ_SET_FEATURE: + { + if ((!NRF_USBD_EPISO_CHECK(ep_addr)) && + (p_setup_ev->setup.wValue.w == APP_USBD_SETUP_STDFEATURE_ENDPOINT_HALT)) + { + if ((usb_state == APP_USBD_STATE_Configured) || (NRF_USBD_EP_NR_GET(ep_addr) == 0)) + { + nrf_drv_usbd_ep_stall(ep_addr); + return NRF_SUCCESS; + } + else + { + return NRF_ERROR_INVALID_STATE; + } + } + break; + } + case APP_USBD_SETUP_STDREQ_CLEAR_FEATURE: + { + if ((!NRF_USBD_EPISO_CHECK(ep_addr)) && + (p_setup_ev->setup.wValue.w == APP_USBD_SETUP_STDFEATURE_ENDPOINT_HALT)) + { + if ((usb_state == APP_USBD_STATE_Configured) || (NRF_USBD_EP_NR_GET(ep_addr) == 0)) + { + nrf_drv_usbd_ep_dtoggle_clear(ep_addr); + nrf_drv_usbd_ep_stall_clear(ep_addr); + return NRF_SUCCESS; + } + else + { + return NRF_ERROR_INVALID_STATE; + } + } + break; + } + default: + break; + } + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Standard interface request handle + * + * @param[in,out] p_class_inst Class instance that holds selected interface + * @param[in] iface_idx Index of the interface in class structure + * @param[in] p_event Event structure to be processed + * + * @return Operation status + */ +static ret_code_t setup_interface_req_std_handle( + app_usbd_class_inst_t const * const p_class_inst, + uint8_t iface_idx, + app_usbd_setup_evt_t const * p_setup_ev) +{ + if (APP_USBD_SETUP_REQTYPE_STD != app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + app_usbd_state_t usb_state = usbd_core_state_get(); + + if (app_usbd_setup_req_dir(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQDIR_IN) + { + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_SETUP_STDREQ_GET_STATUS: + { + if (!(usb_state == APP_USBD_STATE_Configured)) + { + return NRF_ERROR_INVALID_STATE; + } + size_t tx_size; + uint16_t * p_tx_buff = app_usbd_core_setup_transfer_buff_get(&tx_size); + p_tx_buff[0] = 0; + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_tx_buff, sizeof(uint16_t)); + } + case APP_USBD_SETUP_STDREQ_GET_INTERFACE: + { + if (!(usb_state == APP_USBD_STATE_Configured)) + { + return NRF_ERROR_INVALID_STATE; + } + size_t tx_size; + uint8_t * p_tx_buff = app_usbd_core_setup_transfer_buff_get(&tx_size); + p_tx_buff[0] = app_usbd_iface_selection_get(p_class_inst, iface_idx); + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_tx_buff, sizeof(uint8_t)); + } + } + } + else /* APP_USBD_SETUP_REQDIR_OUT */ + { + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_SETUP_STDREQ_SET_INTERFACE: + { + if (!(usb_state == APP_USBD_STATE_Configured)) + { + return NRF_ERROR_INVALID_STATE; + } + + if(p_setup_ev->setup.wValue.w > UINT8_MAX) + { + return NRF_ERROR_INVALID_PARAM; + } + uint8_t alt = p_setup_ev->setup.wValue.lb; + return app_usbd_iface_select(p_class_inst, iface_idx, alt); + } + default: + break; + } + } + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief + * + * @todo RK Documentation + */ +static const nrf_drv_usbd_handler_desc_t usbd_descriptor_feeder_desc = +{ + .handler = { .feeder = usbd_descriptor_conf_feeder }, + .p_context = &m_descriptor_conf_feed_data +}; + +static ret_code_t setup_device_req_get_status( + app_usbd_class_inst_t const * const p_inst, + app_usbd_setup_evt_t const * const p_setup_ev) +{ + size_t max_size; + uint8_t * p_trans_buff = app_usbd_core_setup_transfer_buff_get(&max_size); + ASSERT(sizeof(uint16_t) <= max_size); + + memset(p_trans_buff, 0, sizeof(uint16_t)); + if (m_configuration_dsc.bmAttributes & + APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_SELF_POWERED_MASK) + { + SET_BIT(p_trans_buff[0], 0); + } + if (IS_SET(m_device_features_state, APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP)) + { + SET_BIT(p_trans_buff[0], 1); + } + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_trans_buff, sizeof(uint16_t)); +} + +static ret_code_t setup_device_req_get_descriptor(app_usbd_class_inst_t const * const p_inst, + app_usbd_setup_evt_t const * const p_setup_ev) +{ + switch (p_setup_ev->setup.wValue.hb) + { + case APP_USBD_DESCRIPTOR_DEVICE: + { + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), + &m_device_dsc, + sizeof(m_device_dsc)); + } + case APP_USBD_DESCRIPTOR_CONFIGURATION: + { + /* The size equals the size of configuration descriptor and all classes descriptors */ + const size_t size = MIN( + sizeof(app_usbd_descriptor_configuration_t), + p_setup_ev->setup.wLength.w); + size_t total_length = sizeof(app_usbd_descriptor_configuration_t); + uint8_t iface_count = 0; + + /* Iterate over all registered classes count descriptors and total size */ + app_usbd_class_inst_t const * p_class; + for (p_class = app_usbd_class_first_get(); p_class != NULL; + p_class = app_usbd_class_next_get(p_class)) + { + ASSERT(NULL != (p_class->p_class_methods)); + ASSERT(NULL != (p_class->p_class_methods->feed_descriptors)); + size_t dsc_size = 0; + app_usbd_class_descriptor_ctx_t siz_desc; + APP_USBD_CLASS_DESCRIPTOR_INIT(&siz_desc); + while(p_class->p_class_methods->feed_descriptors(&siz_desc, + p_class, + NULL, + sizeof(uint8_t)) + ) + { + dsc_size++; + } + total_length += dsc_size; + iface_count += app_usbd_class_iface_count_get(p_class); + } + + /* Access transmission buffer */ + size_t max_size; + app_usbd_descriptor_configuration_t * p_trans_buff = + app_usbd_core_setup_transfer_buff_get(&max_size); + /* Copy the configuration descriptor and update the fields that require it */ + ASSERT(size <= max_size); + memcpy(p_trans_buff, &m_configuration_dsc, size); + + p_trans_buff->bNumInterfaces = iface_count; + p_trans_buff->wTotalLength = total_length; + if (app_usbd_class_rwu_enabled_check()) + { + p_trans_buff->bmAttributes |= + APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_REMOTE_WAKEUP_MASK; + } + + + m_descriptor_conf_feed_data.p_cinst = app_usbd_class_first_get(); + m_descriptor_conf_feed_data.p_desc = (void *)p_trans_buff; + m_descriptor_conf_feed_data.desc_left = size; + m_descriptor_conf_feed_data.total_left = p_setup_ev->setup.wLength.w; + + /* Start first transfer */ + ret_code_t ret; + CRITICAL_REGION_ENTER(); + + ret = app_usbd_ep_handled_transfer( + NRF_DRV_USBD_EPIN0, + &usbd_descriptor_feeder_desc); + + if (NRF_SUCCESS == ret) + { + ret = app_usbd_core_setup_data_handler_set( + NRF_DRV_USBD_EPIN0, + &m_setup_data_handler_empty_desc); + } + CRITICAL_REGION_EXIT(); + + return ret; + } + case APP_USBD_DESCRIPTOR_STRING: + { + app_usbd_string_desc_idx_t id = + (app_usbd_string_desc_idx_t)(p_setup_ev->setup.wValue.lb); + uint16_t langid = p_setup_ev->setup.wIndex.w; + uint16_t const * p_string_dsc = app_usbd_string_desc_get(id, langid); + if (p_string_dsc == NULL) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + return app_usbd_core_setup_rsp( + &p_setup_ev->setup, + p_string_dsc, + app_usbd_string_desc_length(p_string_dsc)); + } + default: + break; + } + + + return NRF_ERROR_NOT_SUPPORTED; +} + +static ret_code_t setup_device_req_get_configuration( + app_usbd_class_inst_t const * const p_inst, + app_usbd_setup_evt_t const * const p_setup_ev) +{ + size_t max_size; + uint8_t * p_trans_buff = app_usbd_core_setup_transfer_buff_get(&max_size); + app_usbd_state_t usb_state = usbd_core_state_get(); + if (usb_state == APP_USBD_STATE_Configured) + { + p_trans_buff[0] = 1; + } + else if (usb_state == APP_USBD_STATE_Addressed) + { + p_trans_buff[0] = 0; + } + else + { + return NRF_ERROR_NOT_SUPPORTED; + } + + return app_usbd_core_setup_rsp(&p_setup_ev->setup, p_trans_buff, sizeof(p_trans_buff[0])); +} + +static ret_code_t setup_device_req_set_configuration( + app_usbd_class_inst_t const * const p_inst, + app_usbd_setup_evt_t const * const p_setup_ev) +{ + app_usbd_state_t usb_state = usbd_core_state_get(); + if (!((usb_state == APP_USBD_STATE_Configured) || + (usb_state == APP_USBD_STATE_Addressed))) + { + return NRF_ERROR_INVALID_STATE; + } + + if (p_setup_ev->setup.wValue.lb == 0) + { + app_usbd_all_iface_deselect(); + usbd_core_state_set(APP_USBD_STATE_Addressed); + } + else if (p_setup_ev->setup.wValue.lb == 1) + { + /*Clear all bulk/interrupt endpoint status and set toggle to DATA0*/ + app_usbd_all_iface_select_0(); + usbd_core_state_set(APP_USBD_STATE_Configured); + } + else + { + /*In this driver only one configuration is supported.*/ + return NRF_ERROR_INVALID_PARAM; + } + + return NRF_SUCCESS; +} + +/** + * @brief Internal SETUP event handler + * @param[in] p_inst Instance of the class + * @param[in] p_setup_ev Setup request + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_device_req_std_handler(app_usbd_class_inst_t const * const p_inst, + app_usbd_setup_evt_t const * const p_setup_ev) +{ + ASSERT(p_inst != NULL); + ASSERT(p_setup_ev != NULL); + + if (APP_USBD_SETUP_REQTYPE_STD != app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + if (app_usbd_setup_req_dir(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQDIR_IN) + { + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_SETUP_STDREQ_GET_STATUS: + { + return setup_device_req_get_status(p_inst, p_setup_ev); + } + case APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR: + { + return setup_device_req_get_descriptor(p_inst, p_setup_ev); + } + case APP_USBD_SETUP_STDREQ_GET_CONFIGURATION: + { + return setup_device_req_get_configuration(p_inst, p_setup_ev); + } + default: + /*Not supported*/ + break; + } + return NRF_ERROR_NOT_SUPPORTED; + } + else /* APP_USBD_SETUP_REQDIR_OUT */ + { + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_SETUP_STDREQ_SET_ADDRESS: + { + app_usbd_state_t usb_state = usbd_core_state_get(); + if ((usb_state != APP_USBD_STATE_Default) && + (usb_state != APP_USBD_STATE_Addressed) && + (usb_state != APP_USBD_STATE_Configured)) + { + return NRF_ERROR_INVALID_STATE; + } + app_usbd_all_iface_deselect(); + usbd_core_state_set(APP_USBD_STATE_Addressed); + return NRF_SUCCESS; + } + case APP_USBD_SETUP_STDREQ_SET_FEATURE: + { + if (p_setup_ev->setup.wValue.w == APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP) + { + if (!app_usbd_class_rwu_enabled_check()) + { + return NRF_ERROR_FORBIDDEN; + } + SET_BIT(m_device_features_state, APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP); + return NRF_SUCCESS; + } + break; + } + case APP_USBD_SETUP_STDREQ_CLEAR_FEATURE: + { + if (p_setup_ev->setup.wValue.w == APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP) + { + if (!app_usbd_class_rwu_enabled_check()) + { + return NRF_ERROR_FORBIDDEN; + } + CLR_BIT(m_device_features_state, APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP); + return NRF_SUCCESS; + } + break; + } + case APP_USBD_SETUP_STDREQ_SET_CONFIGURATION: + { + return setup_device_req_set_configuration(p_inst, p_setup_ev); + } + case APP_USBD_SETUP_STDREQ_SET_DESCRIPTOR: + { + /*Not supported yet.*/ + break; + } + default: + /*Not supported*/ + break; + } + return NRF_ERROR_NOT_SUPPORTED; + } +} + +/** + * @brief Process SETUP command + * + * Auxiliary function for SETUP command processing + */ +static inline ret_code_t app_usbd_core_setup_req_handler(app_usbd_class_inst_t const * const p_inst) +{ + app_usbd_setup_evt_t setup_ev; + ret_code_t ret = NRF_ERROR_NOT_SUPPORTED; /* Final result of request processing function */ + + setup_ev.type = APP_USBD_EVT_DRV_SETUP; + nrf_drv_usbd_setup_get((nrf_drv_usbd_setup_t *)&(setup_ev.setup)); + + NRF_LOG_DEBUG("SETUP: t: 0x%.2x r: 0x%.2x", + setup_ev.setup.bmRequestType, + setup_ev.setup.bmRequest); + if (usb_core_ep0_handler_check()) + { + NRF_LOG_WARNING("Previous setup not finished!"); + } + /* Clear EP0 handler if there is anything in progress */ + usbd_core_ep0_handler_clear(); + + switch (app_usbd_setup_req_rec(setup_ev.setup.bmRequestType)) + { + case APP_USBD_SETUP_REQREC_DEVICE: + { + /* Endpoint 0 has core instance (that process device requests) connected */ + ret = setup_device_req_std_handler(p_inst, &setup_ev); + if (ret == NRF_ERROR_NOT_SUPPORTED) + { + ret = app_usbd_all_until_served_call((app_usbd_complex_evt_t const *)&setup_ev); + } + break; + } + case APP_USBD_SETUP_REQREC_INTERFACE: + { + uint8_t const iface_number = setup_ev.setup.wIndex.lb; + uint8_t iface_idx; + app_usbd_class_inst_t const * p_inst_found = app_usbd_iface_find( + iface_number, + &iface_idx); + if (p_inst_found == NULL) + { + ret = NRF_ERROR_INVALID_ADDR; + } + else + { + ret = app_usbd_iface_call( + p_inst_found, iface_idx, + (app_usbd_complex_evt_t const *)&setup_ev); + if (ret == NRF_ERROR_NOT_SUPPORTED) + { + ret = setup_interface_req_std_handle(p_inst_found, iface_idx, &setup_ev); + } + } + break; + } + case APP_USBD_SETUP_REQREC_ENDPOINT: + { + ret = NRF_ERROR_NOT_SUPPORTED; + nrf_drv_usbd_ep_t ep = (nrf_drv_usbd_ep_t)setup_ev.setup.wIndex.lb; + if ((NRF_USBD_EP_NR_GET(ep) != 0)) /* For EP0 we would call this function again! */ + { + ret = app_usbd_ep_call(ep, (app_usbd_complex_evt_t const *)&setup_ev); + } + if (ret == NRF_ERROR_NOT_SUPPORTED) + { + ret = setup_endpoint_req_std(&setup_ev); + } + break; + } + case APP_USBD_SETUP_REQREC_OTHER: + { + /* Try to process via every instance */ + ret = app_usbd_all_until_served_call((app_usbd_complex_evt_t const *)&setup_ev); + break; + } + default: + break; + } + + /* Processing result */ + if (ret == NRF_SUCCESS) + { + if (usb_core_ep0_handler_check()) + { + if (NRF_DRV_USBD_EPOUT0 == nrf_drv_usbd_last_setup_dir_get()) + { + /* Request processed successfully and requires SETUP data */ + nrf_drv_usbd_setup_data_clear(); + } + } + else + { + /* Request processed successfully */ + nrf_drv_usbd_setup_clear(); + } + } + else + { + /* Request finished with error */ + nrf_drv_usbd_setup_stall(); + } + return ret; +} + +/** + * @brief Event handler for core module + * + * The event handler that would process all events directed to device. + * + */ +static ret_code_t app_usbd_core_event_handler(app_usbd_class_inst_t const * const p_inst, + app_usbd_complex_evt_t const * const p_event) +{ + ret_code_t ret = NRF_ERROR_NOT_SUPPORTED; + switch (p_event->type) + { + case APP_USBD_EVT_DRV_RESET: + { + usbd_core_state_set(APP_USBD_STATE_Default); + break; + } + case APP_USBD_EVT_DRV_SUSPEND: + { + ret = NRF_SUCCESS; + break; + } + case APP_USBD_EVT_DRV_RESUME: + { + if (nrf_atomic_flag_clear_fetch(&m_rwu_pending) != 0) + { + nrf_usbd_task_trigger(NRF_USBD_TASK_NODRIVEDPDM); + } + + ASSERT(usbd_core_state_get() >= APP_USBD_STATE_Unattached); + ret = NRF_SUCCESS; + break; + } + case APP_USBD_EVT_DRV_SETUP: + { + ret = app_usbd_core_setup_req_handler(p_inst); + break; + } + case APP_USBD_EVT_INST_APPEND: + { + ASSERT(usbd_core_state_get() == APP_USBD_STATE_Disabled); + ret = NRF_SUCCESS; + break; + } + case APP_USBD_EVT_INST_REMOVE: + { + ASSERT(usbd_core_state_get() == APP_USBD_STATE_Unattached); + usbd_core_state_set(APP_USBD_STATE_Disabled); + ret = NRF_SUCCESS; + break; + } + case APP_USBD_EVT_STARTED: + { + if (usbd_core_power_is_detected()) + { + usbd_core_state_set(APP_USBD_STATE_Powered); + } + else + { + usbd_core_state_set(APP_USBD_STATE_Unattached); + } + ret = NRF_SUCCESS; + break; + } + case APP_USBD_EVT_STOPPED: + { + ASSERT(usbd_core_state_get() >= APP_USBD_STATE_Powered); + usbd_core_state_set(APP_USBD_STATE_Unattached); + ret = NRF_SUCCESS; + break; + } + /* Data transfer on endpoint 0 */ + case APP_USBD_EVT_DRV_EPTRANSFER: + { + if (p_event->drv_evt.data.eptransfer.status == NRF_USBD_EP_ABORTED) + { + /* Just ignore aborting */ + break; + } + /* This EPTRANSFER event has to be called only for EP0 */ + ASSERT((p_event->drv_evt.data.eptransfer.ep == NRF_DRV_USBD_EPOUT0) || + (p_event->drv_evt.data.eptransfer.ep == NRF_DRV_USBD_EPIN0)); + ret = usbd_core_ep0_handler_call_and_clear(p_event->drv_evt.data.eptransfer.status); + /* Processing result */ + if (ret == NRF_SUCCESS) + { + if (usb_core_ep0_handler_check()) + { + if (p_event->drv_evt.data.eptransfer.ep == NRF_DRV_USBD_EPOUT0) + { + /* Request processed successfully and requires SETUP data */ + nrf_drv_usbd_setup_data_clear(); + } + } + else + { + if (!nrf_drv_usbd_errata_154()) + { + nrf_drv_usbd_setup_clear(); + } + } + } + else + { + /* Request finished with error */ + nrf_drv_usbd_setup_stall(); + } + break; + } + default: + break; + } + + return ret; +} + +/** @} */ + +ret_code_t app_usbd_core_setup_rsp(app_usbd_setup_t const * p_setup, + void const * p_data, + size_t size) +{ + size_t req_size = p_setup->wLength.w; + size_t tx_size = MIN(req_size, size); + bool zlp_required = (size < req_size) && + (0 == (size % nrf_drv_usbd_ep_max_packet_size_get(NRF_DRV_USBD_EPIN0))); + + NRF_DRV_USBD_TRANSFER_IN_FLAGS( + transfer, + p_data, + tx_size, + zlp_required ? NRF_DRV_USBD_TRANSFER_ZLP_FLAG : 0); + + ret_code_t ret; + CRITICAL_REGION_ENTER(); + ret = app_usbd_ep_transfer(NRF_DRV_USBD_EPIN0, &transfer); + if (NRF_SUCCESS == ret) + { + ret = app_usbd_core_setup_data_handler_set(NRF_DRV_USBD_EPIN0, + &m_setup_data_handler_empty_desc); + } + CRITICAL_REGION_EXIT(); + + return ret; +} + +ret_code_t app_usbd_core_setup_data_handler_set( + nrf_drv_usbd_ep_t ep, + app_usbd_core_setup_data_handler_desc_t const * const p_handler_desc) +{ + if (nrf_drv_usbd_last_setup_dir_get() != ep) + { + return NRF_ERROR_INVALID_ADDR; + } + + m_ep0_handler_desc = *p_handler_desc; + return NRF_SUCCESS; +} + +void * app_usbd_core_setup_transfer_buff_get(size_t * p_size) +{ + if (p_size != NULL) + *p_size = sizeof(m_setup_transfer_buff); + + return m_setup_transfer_buff; +} + +app_usbd_state_t app_usbd_core_state_get(void) +{ + return usbd_core_state_get(); +} + + +bool app_usbd_core_feature_state_get(app_usbd_setup_stdfeature_t feature) +{ + return IS_SET(m_device_features_state, feature) ? true : false; +} + +#endif //NRF_MODULE_ENABLED(APP_USBD) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_core.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_core.h new file mode 100644 index 0000000..7c6c12e --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_core.h @@ -0,0 +1,206 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef APP_USBD_CORE_H__ +#define APP_USBD_CORE_H__ + +#include <stdint.h> + +#include "sdk_common.h" +#include "nrf_drv_usbd.h" +#include "app_usbd_types.h" +#include "app_usbd_class_base.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @defgroup app_usbd_core USB Device high level library core module + * @ingroup app_usbd + * + * @brief @tagAPI52840 Core module that manages current USB state and process device requests. + * @{ + */ + +/** + * @brief Core interface configuration + * + * Core instance would have 2 endpoints (IN0 and OUT0). + * The interface number does not matter because it is not used. + */ +#define APP_USBD_CORE_CLASS_CONFIGURATION ((0, NRF_DRV_USBD_EPOUT0, NRF_DRV_USBD_EPIN0)) + +/** + * @brief USB Device state + * + * Possible USB Device states according to specification. + */ +typedef enum +{ + APP_USBD_STATE_Disabled , /**< The whole USBD library is disabled */ + APP_USBD_STATE_Unattached, /**< Device is currently not connected to the host */ + APP_USBD_STATE_Powered , /**< Device is connected to the host but has not been enumerated */ + APP_USBD_STATE_Default , /**< USB Reset condition detected, waiting for the address */ + APP_USBD_STATE_Addressed , /**< Device has been addressed but has not been configured */ + APP_USBD_STATE_Configured, /**< Device is addressed and configured */ +}app_usbd_state_t; + +/** + * @brief EP0 handler function pointer + * + * Type of the variable that would hold the pointer to the handler for + * endpoint 0 messages processing. + * + * @param p_contex Context variable configured with the transmission request. + */ +typedef ret_code_t (*app_usbd_core_setup_data_handler_t)(nrf_drv_usbd_ep_status_t status, + void * p_context); + +/** + * @brief Variable type used to register EP0 transfer handler + * + * EP0 messages are processed by core instance. + * Another class can register itself to receive messages from EP0 when requesting + * for Setup data transfer. + */ +typedef struct +{ + app_usbd_core_setup_data_handler_t handler; //!< Event handler to be called when transmission is ready + void * p_context; //!< Context pointer to be send to every called event. +} app_usbd_core_setup_data_handler_desc_t; + +/*lint -save -e10 -e26 -e93 -e123 -e505 */ +/** + * @brief Declare Core instance type + * + * USBD core instance type definition. + */ +APP_USBD_CLASS_TYPEDEF(app_usbd_core, + APP_USBD_CORE_CLASS_CONFIGURATION, + APP_USBD_CLASS_INSTANCE_SPECIFIC_DEC_NONE, + APP_USBD_CLASS_DATA_SPECIFIC_DEC_NONE); +/*lint -restore*/ + +/** + * @brief Access to core instance + * + * Function that returns pointer to the USBD core instance. + * + * @return pointer to the core instance + */ +static inline app_usbd_class_inst_t const * app_usbd_core_instance_access(void) +{ + extern const APP_USBD_CLASS_INSTANCE_TYPE(app_usbd_core) app_usbd_core_inst; + return (app_usbd_class_inst_t const *)&app_usbd_core_inst; +} + +/** + * @brief Default simple response to setup command + * + * This function generates default simple response. + * It sends ZLP when required and on takes care on allowing status stage when + * transfer is finished. + * + * @param p_setup Pointer to original setup message + * @param p_data Pointer to the response. This has to be globaly aviable data. + * @param size Total size of the answer - The function takes care about + * limiting the size of transfered data to the size required + * by setup command. + */ +ret_code_t app_usbd_core_setup_rsp(app_usbd_setup_t const * p_setup, + void const * p_data, + size_t size); + +/** + * @brief Configure the handler for the nearest setup data endpoint transfer + * + * This function would be called on incomming setup data. + * The correct place to set the handler for a data is when SETUP command + * was received. + * + * @param ep Endpoint number (only IN0 and OUT0) are supported. + * @param p_handler_desc Descriptor of the handler to be called. + * + * @retval NRF_SUCCESS Successfully configured + * @retval NRF_ERROR_INVALID_ADDR Last received setup direction does not match + * configured endpoint. + */ +ret_code_t app_usbd_core_setup_data_handler_set( + nrf_drv_usbd_ep_t ep, + app_usbd_core_setup_data_handler_desc_t const * const p_handler_desc); + +/** + * @brief Set up a data transfer buffer. + * + * Returns special internal buffer that can be used in setup transfer. + * @return Internal buffer pointer + */ +void * app_usbd_core_setup_transfer_buff_get(size_t * p_size); + + +/**@brief Return internal USBD core state + * + * @return Check @ref app_usbd_state_t to find possible USBD core states + */ +app_usbd_state_t app_usbd_core_state_get(void); + + +/** + * @brief Check current feature state + * + * Function checks the state of the selected feature that was configured by the host. + * + * @param feature Feature to check. + * Only features related to the device should be checked by this function. + * + * @retval true Selected feature is set + * @retval false Selected feature is cleared + */ +bool app_usbd_core_feature_state_get(app_usbd_setup_stdfeature_t feature); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_CORE_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_descriptor.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_descriptor.h new file mode 100644 index 0000000..4d3dded --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_descriptor.h @@ -0,0 +1,337 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef APP_USBD_DESCRIPTOR_H__ +#define APP_USBD_DESCRIPTOR_H__ + +#include "nrf.h" +#include "nrf_drv_usbd.h" +#include "app_usbd_langid.h" +#include "app_util_platform.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Compiler support for anonymous unions */ +ANON_UNIONS_ENABLE; + +/** + * @defgroup app_usbd_descriptor USB standard descriptors + * @ingroup app_usbd + * + * @brief @tagAPI52840 Module with types definitions used for standard descriptors. + * @{ + */ + +/** + * @brief Helper macro for translating unsigned 24 bit value to 2 byte raw descriptor + * */ +#define APP_USBD_U16_TO_RAW_DSC(val) (uint8_t)(val), \ + (uint8_t)(((val) / (256))) + +/** + * @brief Helper macro for translating unsigned 24 bit value to 3 byte raw descriptor + * */ +#define APP_USBD_U24_TO_RAW_DSC(val) (uint8_t)(val), \ + (uint8_t)(((val) / (256))), \ + (uint8_t)(((val) / (256 * 256))) + +/** + * @brief Helper macro for translating unsigned 32 bit value to 4 byte raw descriptor + * */ +#define APP_USBD_U32_TO_RAW_DSC(val) (uint8_t)(val), \ + (uint8_t)(((val) / (256))), \ + (uint8_t)(((val) / (256 * 256))) \ + (uint8_t)(((val) / (256 * 256 * 256))) +/** + * @brief Descriptor types + * + * Descriptor types used in two situations: + * - when processing @ref APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR SETUP request, + * the required descriptor type may be placed in wValue in HighByte. + * - As a descriptor identifier itself inside descriptor stream. + * + * According to chapter 9.6 of USB 2.0 specification, following descriptors may + * be requested directly by GetDescriptor method: + * - @ref APP_USBD_DESCRIPTOR_DEVICE + * - @ref APP_USBD_DESCRIPTOR_DEVICE_QUALIFIER (not used for FullSpeed only device) + * - @ref APP_USBD_DESCRIPTOR_CONFIGURATION + * - @ref APP_USBD_DESCRIPTOR_STRING + */ +typedef enum +{ + APP_USBD_DESCRIPTOR_DEVICE = 1, /**< Device descriptor. */ + APP_USBD_DESCRIPTOR_CONFIGURATION = 2, /**< + * Specific configuration descriptor. + * Configuration descriptor is always followed by all the related interface + * and endpoints descriptors. + */ + APP_USBD_DESCRIPTOR_STRING = 3, /**< String descriptor. */ + APP_USBD_DESCRIPTOR_INTERFACE = 4, /**< + * Interface descriptor followed by all the related endpoints descriptors. + * + * @note It is returned together with @ref APP_USBD_DESCRIPTOR_CONFIGURATION. + * Cannot be accessed by GetDescriptor or SetDescriptor + */ + APP_USBD_DESCRIPTOR_ENDPOINT = 5, /**< + * Endpoint descriptor. + * + * @note It is returned together with @ref APP_USBD_DESCRIPTOR_CONFIGURATION. + * Cannot be accessed by GetDescriptor or SetDescriptor + */ + APP_USBD_DESCRIPTOR_DEVICE_QUALIFIER = 6, /**< @note Not supported - used only in HighSpeed capable devices. */ + APP_USBD_DESCRIPTOR_OTHER_SPEED_CONFIGURATION = 7, /**< @note Not supported - our USB implementation supports only one speed. */ + APP_USBD_DESCRIPTOR_INTERFACE_POWER = 8, /**< @note Not supported */ + APP_USBD_DESCRIPTOR_OTG = 9, /**< @note Not supported - Our USB have not OTG functionality */ + APP_USBD_DESCRIPTOR_DEBUG = 10, /**< Debug channel descriptor if available, can be only reached by GetDescriptor */ + APP_USBD_DESCRIPTOR_INTERFACE_ASSOCIATION = 11, /**< + * Descriptor used to describe that two or more interfaces are associated to the same function. + * + * @note It is returned together with @ref APP_USBD_DESCRIPTOR_CONFIGURATION. + * Cannot be accessed by GetDescriptor or SetDescriptor + */ + APP_USBD_DESCRIPTOR_REPORT = 34, /**< HID Report descriptor. */ + APP_USBD_DESCRIPTOR_PHYSICAL = 35 /**< HID Physical descriptor. */ + +} app_usbd_descriptor_t; + +/* Make all descriptors packed */ +#pragma pack(push, 1) + +/** + * @brief Common descriptor header + * + * The header that we can find on the beginning of all descriptors that contains + * the descriptor length and type. + */ +typedef struct +{ + uint8_t bLength; //!< Size of the descriptor in bytes. + uint8_t bDescriptorType; //!< Should equal one of @ref app_usbd_descriptor_t. + /** Class specific descriptors values are defined inside classes. */ +} app_usbd_descriptor_header_t; + +/** + * @brief Device descriptor + * + * Descriptor used for the whole device + */ +typedef struct +{ + uint8_t bLength; //!< Size of the descriptor in bytes. + uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_DEVICE. + uint16_t bcdUSB; //!< USB Specification Release Number in Binary-Coded Decimal + uint8_t bDeviceClass; //!< Device class code. + /**< If 0, each interface specifies its own class information. + * 0xFF for vendor-specific. + */ + uint8_t bDeviceSubClass; //!< Subclass code. + /**< If bDevice Class is set to value other than 0xFF, + * all values here are reserved for assignment by USB-IF. + */ + uint8_t bDeviceProtocol; //!< Subclass code. + /**< If 0, no specific protocol is defined on device basis. + * Each interface may define its own protocol then. + * If set to 0xFF, vendor-specific protocol is used. + */ + uint8_t bMaxPacketSize0; //!< Maximum packet size for endpoint zero. + uint16_t idVendor; //!< Vendor ID (Assigned by the USB-IF). + uint16_t idProduct; //!< Product ID (assigned by manufacturer). + uint16_t bcdDevice; //!< Device release number in binary-coded decimal. + uint8_t iManufacturer; //!< Index of string descriptor in describing manufacturer. + uint8_t iProduct; //!< Index of string descriptor in describing product. + uint8_t iSerialNumber; //!< Index of string descriptor in describing the device's serial number. + uint8_t bNumConfigurations; //!< Number of possible configurations. +} app_usbd_descriptor_device_t; + +/** + * @brief Attributes masks + * + * Masks used for attributes in configuration. + */ +typedef enum +{ + /** This is reserved descriptor that has always to be set */ + APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_ALWAYS_SET_MASK = 1U << 7, + /** Attribute that informs that device is self powered */ + APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_SELF_POWERED_MASK = 1U << 6, + /** Attribute that informs that device has Remove Wakeup functionality */ + APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_REMOTE_WAKEUP_MASK = 1U << 5 +} app_usbd_descriptor_configuration_attributes_t; + +/** + * @brief Configuration descriptor + * + * Descriptor used at the beginning of configuration response. + */ +typedef struct +{ + uint8_t bLength; //!< Size of the descriptor in bytes. + uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_DEVICE. + uint16_t wTotalLength; //!< Total length of configuration data, including all descriptors returned after configuration itself. + uint8_t bNumInterfaces; //!< Number of interfaces supportedf by this configuration + uint8_t bConfigurationValue; //!< Value to use as an argument to the SetConfiguration request. + uint8_t iConfiguration; //!< Index of string descriptor describing this configuration. + uint8_t bmAttributes; //!< Configuration characteristics. + uint8_t bMaxPower; //!< Maximum power consumption. Expressed in 2 mA units. +} app_usbd_descriptor_configuration_t; + +/** + * @brief Raw descriptor - String descriptor zero + * + * String descriptor sent only as a response for GetDescriptor. + */ +typedef struct +{ + uint8_t bLength; //!< Size of the descriptor in bytes. + uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_STRING. + uint16_t wLANGID[]; //!< The array of LANGID codes supported by the device. +} app_usbd_descriptor_string0_t; + +/** + * @brief Raw descriptor - Any normal string + * + * String descriptor sent only as a response for GetDescriptor. + */ +typedef struct +{ + uint8_t bLength; //!< Size of the descriptor in bytes. + uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_STRING. + uint16_t bString[]; //!< UNICODE encoded string. +} app_usbd_descriptor_string_t; + + +/** + * @brief Interface descriptor + * + * Interface descriptor, returned as a part of configuration descriptor. + */ +typedef struct +{ + uint8_t bLength; //!< Size of the descriptor in bytes. + uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_INTERFACE. + uint8_t bInterfaceNumber; //!< Number of this interface. + uint8_t bAlternateSetting; //!< Value used to select this alternate setting. + uint8_t bNumEndpoints; //!< Number of endpoints used by this interface. + uint8_t bInterfaceClass; //!< Class code (assigned by the USB-IF). 0xff for vendor specific. + uint8_t bInterfaceSubClass; //!< Subclass code (assigned by the USB-IF). + uint8_t bInterfaceProtocol; //!< Protocol code (assigned by the USB-IF). 0xff for vendor specific. + uint8_t iInterface; //!< Index of string descriptor describing this interface. +} app_usbd_descriptor_iface_t; + +/** Offset of endpoint type attribute bits */ +#define APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET 0 +/** Mask of endpoint type attribute bits */ +#define APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_MASK BF_MASK(2, APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET) + +/** Offset of endpoint synchronization type attribute bits */ +#define APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET 2 +/** Mask of endpoint synchronization type attribute bits */ +#define APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_MASK BF_MASK(2, APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET) + +/** Offset of endpoint usage type attribute bits */ +#define APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET 4 +/** Mask of endpoint usage type attribute bits */ +#define APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_MASK BF_MASK(2, APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET) + +/** + * @brief Endpoint attributes mnemonics + * + * @sa APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_MASK + * @sa APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_MASK + * @sa APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_MASK + */ +typedef enum +{ + APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_CONTROL = 0 << APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET, + APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_ISOCHRONOUS = 1 << APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET, + APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_BULK = 2 << APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET, + APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_INTERRUPT = 3 << APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET, + + APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_NONE = 0 << APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET, + APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_ASYNCHRONOUS = 1 << APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET, + APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_ADAPTIVE = 2 << APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET, + APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_SYNCHRONOUS = 3 << APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET, + + APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_DATA = 0 << APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET, + APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_FEEDBACK = 1 << APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET, + APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_IMPLICIT = 2 << APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET +} app_usbd_descriptor_ep_attr_bitmap_t; + +/** + * @brief Endpoint descriptor + * + * Endpoint descriptor, returned as a part of configuration descriptor. + */ +typedef struct +{ + uint8_t bLength; //!< Size of the descriptor in bytes. + uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_ENDPOINT. + uint8_t bEndpointAddress; //!< Endpoint address + uint8_t bmAttributes; //!< Endpoint attributes + uint16_t wMaxPacketSize; //!< Maximum packet size this endpoint is capable of handling. + uint8_t bInterval; //!< Interval for pooling endpoint for data transfers. +} app_usbd_descriptor_ep_t; + +/** + * @brief Interface association descriptor + */ +typedef struct +{ + uint8_t bLength; //!< size of this descriptor in bytes + uint8_t bDescriptorType; //!< INTERFACE descriptor type + uint8_t bFirstInterface; //!< Number of interface + uint8_t bInterfaceCount; //!< value to select alternate setting + uint8_t bFunctionClass; //!< Class code assigned by the USB + uint8_t bFunctionSubClass;//!< Sub-class code assigned by the USB + uint8_t bFunctionProtocol;//!< Protocol code assigned by the USB + uint8_t iFunction; //!< Index of string descriptor +} app_usbd_descriptor_iad_t; + +#pragma pack(pop) +ANON_UNIONS_DISABLE; + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* APP_USBD_DESCRIPTOR_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_langid.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_langid.h new file mode 100644 index 0000000..0d08f14 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_langid.h @@ -0,0 +1,286 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef APP_USBD_LANGID_H__ +#define APP_USBD_LANGID_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file + * @brief This file contains LANGID variable type with all defined values + * + * This file was created using Language Identifiers (LANGIDs) 3/29/00 Version 1.0, + * available on USB web page: + * http://www.usb.org/developers/docs/USB_LANGIDs.pdf + * + * @note + * Do not include this file directly to the project. + * It is included by @file app_usbd_request.h. + */ + +/** + * Offset of the lowest bit of primary language identifier + * @sa app_usbd_langid_t + */ +#define APP_USB_LANG_OFFSET 0 + +/** + * Bitmask for a primary language identifier + * @sa app_usbd_langid_t + */ +#define APP_USB_LANG_MASK BF_MASK(10, APP_USB_LANG_OFFSET) + +/** + * Offset of the lowest bit of sub-language identifier + * @sa app_usbd_langid_t + */ +#define APP_USB_SUBLANG_OFFSET 10 + +/** + * Bitmask for a sub-language identifier + * @sa app_usbd_langid_t + */ +#define APP_USB_SUBLANG_MASK BF_MASK(6, APP_USB_SUBLANG_OFFSET) + +/** + * @brief Primary language identifiers + * + * Mnemonics for primary language identifiers. + * This mnemonics can be combined using logical OR operator with @ref app_usbd_langid_sub_t. + */ +typedef enum +{ + APP_USBD_LANG_ARABIC = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Arabic */ + APP_USBD_LANG_BULGARIAN = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Bulgarian */ + APP_USBD_LANG_CATALAN = 0x03U << ( APP_USB_LANG_OFFSET ), /**< Catalan */ + APP_USBD_LANG_CHINESE = 0x04U << ( APP_USB_LANG_OFFSET ), /**< Chinese */ + APP_USBD_LANG_CZECH = 0x05U << ( APP_USB_LANG_OFFSET ), /**< Czech */ + APP_USBD_LANG_DANISH = 0x06U << ( APP_USB_LANG_OFFSET ), /**< Danish */ + APP_USBD_LANG_GERMAN = 0x07U << ( APP_USB_LANG_OFFSET ), /**< German */ + APP_USBD_LANG_GREEK = 0x08U << ( APP_USB_LANG_OFFSET ), /**< Greek */ + APP_USBD_LANG_ENGLISH = 0x09U << ( APP_USB_LANG_OFFSET ), /**< English */ + APP_USBD_LANG_SPANISH = 0x0aU << ( APP_USB_LANG_OFFSET ), /**< Spanish */ + APP_USBD_LANG_FINNISH = 0x0bU << ( APP_USB_LANG_OFFSET ), /**< Finnish */ + APP_USBD_LANG_FRENCH = 0x0cU << ( APP_USB_LANG_OFFSET ), /**< French */ + APP_USBD_LANG_HEBREW = 0x0dU << ( APP_USB_LANG_OFFSET ), /**< Hebrew */ + APP_USBD_LANG_HUNGARIAN = 0x0eU << ( APP_USB_LANG_OFFSET ), /**< Hungarian */ + APP_USBD_LANG_ICELANDIC = 0x0fU << ( APP_USB_LANG_OFFSET ), /**< Icelandic */ + APP_USBD_LANG_ITALIAN = 0x10U << ( APP_USB_LANG_OFFSET ), /**< Italian */ + APP_USBD_LANG_JAPANESE = 0x11U << ( APP_USB_LANG_OFFSET ), /**< Japanese */ + APP_USBD_LANG_KOREAN = 0x12U << ( APP_USB_LANG_OFFSET ), /**< Korean */ + APP_USBD_LANG_DUTCH = 0x13U << ( APP_USB_LANG_OFFSET ), /**< Dutch */ + APP_USBD_LANG_NORWEGIAN = 0x14U << ( APP_USB_LANG_OFFSET ), /**< Norwegian */ + APP_USBD_LANG_POLISH = 0x15U << ( APP_USB_LANG_OFFSET ), /**< Polish */ + APP_USBD_LANG_PORTUGUESE = 0x16U << ( APP_USB_LANG_OFFSET ), /**< Portuguese */ + APP_USBD_LANG_ROMANIAN = 0x18U << ( APP_USB_LANG_OFFSET ), /**< Romanian */ + APP_USBD_LANG_RUSSIAN = 0x19U << ( APP_USB_LANG_OFFSET ), /**< Russian */ + APP_USBD_LANG_CROATIAN = 0x1aU << ( APP_USB_LANG_OFFSET ), /**< Croatian */ + APP_USBD_LANG_SERBIAN = 0x1aU << ( APP_USB_LANG_OFFSET ), /**< Serbian */ + APP_USBD_LANG_SLOVAK = 0x1bU << ( APP_USB_LANG_OFFSET ), /**< Slovak */ + APP_USBD_LANG_ALBANIAN = 0x1cU << ( APP_USB_LANG_OFFSET ), /**< Albanian */ + APP_USBD_LANG_SWEDISH = 0x1dU << ( APP_USB_LANG_OFFSET ), /**< Swedish */ + APP_USBD_LANG_THAI = 0x1eU << ( APP_USB_LANG_OFFSET ), /**< Thai */ + APP_USBD_LANG_TURKISH = 0x1fU << ( APP_USB_LANG_OFFSET ), /**< Turkish */ + APP_USBD_LANG_URDU = 0x20U << ( APP_USB_LANG_OFFSET ), /**< Urdu */ + APP_USBD_LANG_INDONESIAN = 0x21U << ( APP_USB_LANG_OFFSET ), /**< Indonesian */ + APP_USBD_LANG_UKRANIAN = 0x22U << ( APP_USB_LANG_OFFSET ), /**< Ukrainian */ + APP_USBD_LANG_BELARUSIAN = 0x23U << ( APP_USB_LANG_OFFSET ), /**< Belarusian */ + APP_USBD_LANG_SLOVENIAN = 0x24U << ( APP_USB_LANG_OFFSET ), /**< Slovenian */ + APP_USBD_LANG_ESTONIAN = 0x25U << ( APP_USB_LANG_OFFSET ), /**< Estonian */ + APP_USBD_LANG_LATVIAN = 0x26U << ( APP_USB_LANG_OFFSET ), /**< Latvian */ + APP_USBD_LANG_LITHUANIAN = 0x27U << ( APP_USB_LANG_OFFSET ), /**< Lithuanian */ + APP_USBD_LANG_FARSI = 0x29U << ( APP_USB_LANG_OFFSET ), /**< Farsi */ + APP_USBD_LANG_VIETNAMESE = 0x2aU << ( APP_USB_LANG_OFFSET ), /**< Vietnamese */ + APP_USBD_LANG_ARMENIAN = 0x2bU << ( APP_USB_LANG_OFFSET ), /**< Armenian */ + APP_USBD_LANG_AZERI = 0x2cU << ( APP_USB_LANG_OFFSET ), /**< Azeri */ + APP_USBD_LANG_BASQUE = 0x2dU << ( APP_USB_LANG_OFFSET ), /**< Basque */ + APP_USBD_LANG_MACEDONIAN = 0x2fU << ( APP_USB_LANG_OFFSET ), /**< Macedonian */ + APP_USBD_LANG_AFRIKAANS = 0x36U << ( APP_USB_LANG_OFFSET ), /**< Afrikaans */ + APP_USBD_LANG_GEORGIAN = 0x37U << ( APP_USB_LANG_OFFSET ), /**< Georgian */ + APP_USBD_LANG_FAEROESE = 0x38U << ( APP_USB_LANG_OFFSET ), /**< Faeroese */ + APP_USBD_LANG_HINDI = 0x39U << ( APP_USB_LANG_OFFSET ), /**< Hindi */ + APP_USBD_LANG_MALAY = 0x3eU << ( APP_USB_LANG_OFFSET ), /**< Malay */ + APP_USBD_LANG_KAZAK = 0x3fU << ( APP_USB_LANG_OFFSET ), /**< Kazak */ + APP_USBD_LANG_SWAHILI = 0x41U << ( APP_USB_LANG_OFFSET ), /**< Swahili */ + APP_USBD_LANG_UZBEK = 0x43U << ( APP_USB_LANG_OFFSET ), /**< Uzbek */ + APP_USBD_LANG_TATAR = 0x44U << ( APP_USB_LANG_OFFSET ), /**< Tatar */ + APP_USBD_LANG_BENGALI = 0x45U << ( APP_USB_LANG_OFFSET ), /**< Bengali */ + APP_USBD_LANG_PUNJABI = 0x46U << ( APP_USB_LANG_OFFSET ), /**< Punjabi */ + APP_USBD_LANG_GUJARATI = 0x47U << ( APP_USB_LANG_OFFSET ), /**< Gujarati */ + APP_USBD_LANG_ORIYA = 0x48U << ( APP_USB_LANG_OFFSET ), /**< Oriya */ + APP_USBD_LANG_TAMIL = 0x49U << ( APP_USB_LANG_OFFSET ), /**< Tamil */ + APP_USBD_LANG_TELUGU = 0x4aU << ( APP_USB_LANG_OFFSET ), /**< Telugu */ + APP_USBD_LANG_KANNADA = 0x4bU << ( APP_USB_LANG_OFFSET ), /**< Kannada */ + APP_USBD_LANG_MALAYALAM = 0x4cU << ( APP_USB_LANG_OFFSET ), /**< Malayalam */ + APP_USBD_LANG_ASSAMESE = 0x4dU << ( APP_USB_LANG_OFFSET ), /**< Assamese */ + APP_USBD_LANG_MARATHI = 0x4eU << ( APP_USB_LANG_OFFSET ), /**< Marathi */ + APP_USBD_LANG_SANSKRIT = 0x4fU << ( APP_USB_LANG_OFFSET ), /**< Sanskrit */ + APP_USBD_LANG_KONKANI = 0x57U << ( APP_USB_LANG_OFFSET ), /**< Konkani */ + APP_USBD_LANG_MANIPURI = 0x58U << ( APP_USB_LANG_OFFSET ), /**< Manipuri */ + APP_USBD_LANG_SINDHI = 0x59U << ( APP_USB_LANG_OFFSET ), /**< Sindhi */ + APP_USBD_LANG_KASHMIRI = 0x60U << ( APP_USB_LANG_OFFSET ), /**< Kashmiri */ + APP_USBD_LANG_NEPALI = 0x61U << ( APP_USB_LANG_OFFSET ), /**< Nepali */ + APP_USBD_LANG_HID = 0xffU << ( APP_USB_LANG_OFFSET ), /**< Reserved for USB HID Class use */ +}app_usbd_langid_primary_t; + +/** + * @brief Sublanguage identifiers + * + * Mnemonics with sublanguage values. + * Use them in combination with @ref app_usbd_langid_primary_t. + */ +typedef enum +{ + APP_USBD_SUBLANG_ARABIC_SAUDI_ARABIA = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Saudi Arabia) */ + APP_USBD_SUBLANG_ARABIC_IRAQ = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Iraq) */ + APP_USBD_SUBLANG_ARABIC_EGYPT = 0x03U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Egypt) */ + APP_USBD_SUBLANG_ARABIC_LIBYA = 0x04U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Libya) */ + APP_USBD_SUBLANG_ARABIC_ALGERIA = 0x05U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Algeria) */ + APP_USBD_SUBLANG_ARABIC_MOROCCO = 0x06U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Morocco) */ + APP_USBD_SUBLANG_ARABIC_TUNISIA = 0x07U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Tunisia) */ + APP_USBD_SUBLANG_ARABIC_OMAN = 0x08U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Oman) */ + APP_USBD_SUBLANG_ARABIC_YEMEN = 0x09U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Yemen) */ + APP_USBD_SUBLANG_ARABIC_SYRIA = 0x10U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Syria) */ + APP_USBD_SUBLANG_ARABIC_JORDAN = 0x11U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Jordan) */ + APP_USBD_SUBLANG_ARABIC_LEBANON = 0x12U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Lebanon) */ + APP_USBD_SUBLANG_ARABIC_KUWAIT = 0x13U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Kuwait) */ + APP_USBD_SUBLANG_ARABIC_UAE = 0x14U << ( APP_USB_LANG_OFFSET ), /**< Arabic (U.A.E.) */ + APP_USBD_SUBLANG_ARABIC_BAHRAIN = 0x15U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Bahrain) */ + APP_USBD_SUBLANG_ARABIC_QATAR = 0x16U << ( APP_USB_LANG_OFFSET ), /**< Arabic (Qatar) */ + APP_USBD_SUBLANG_AZERI_CYRILLIC = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Azeri (Cyrillic) */ + APP_USBD_SUBLANG_AZERI_LATIN = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Azeri (Latin) */ + APP_USBD_SUBLANG_CHINESE_TRADITIONAL = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Chinese (Traditional) */ + APP_USBD_SUBLANG_CHINESE_SIMPLIFIED = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Chinese (Simplified) */ + APP_USBD_SUBLANG_CHINESE_HONGKONG = 0x03U << ( APP_USB_LANG_OFFSET ), /**< Chinese (Hong Kong SAR, PRC) */ + APP_USBD_SUBLANG_CHINESE_SINGAPORE = 0x04U << ( APP_USB_LANG_OFFSET ), /**< Chinese (Singapore) */ + APP_USBD_SUBLANG_CHINESE_MACAU = 0x05U << ( APP_USB_LANG_OFFSET ), /**< Chinese (Macau SAR) */ + APP_USBD_SUBLANG_DUTCH = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Dutch */ + APP_USBD_SUBLANG_DUTCH_BELGIAN = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Dutch (Belgian) */ + APP_USBD_SUBLANG_ENGLISH_US = 0x01U << ( APP_USB_LANG_OFFSET ), /**< English (US) */ + APP_USBD_SUBLANG_ENGLISH_UK = 0x02U << ( APP_USB_LANG_OFFSET ), /**< English (UK) */ + APP_USBD_SUBLANG_ENGLISH_AUS = 0x03U << ( APP_USB_LANG_OFFSET ), /**< English (Australian) */ + APP_USBD_SUBLANG_ENGLISH_CAN = 0x04U << ( APP_USB_LANG_OFFSET ), /**< English (Canadian) */ + APP_USBD_SUBLANG_ENGLISH_NZ = 0x05U << ( APP_USB_LANG_OFFSET ), /**< English (New Zealand) */ + APP_USBD_SUBLANG_ENGLISH_EIRE = 0x06U << ( APP_USB_LANG_OFFSET ), /**< English (Ireland) */ + APP_USBD_SUBLANG_ENGLISH_SOUTH_AFRICA = 0x07U << ( APP_USB_LANG_OFFSET ), /**< English (South Africa) */ + APP_USBD_SUBLANG_ENGLISH_JAMAICA = 0x08U << ( APP_USB_LANG_OFFSET ), /**< English (Jamaica) */ + APP_USBD_SUBLANG_ENGLISH_CARIBBEAN = 0x09U << ( APP_USB_LANG_OFFSET ), /**< English (Caribbean) */ + APP_USBD_SUBLANG_ENGLISH_BELIZE = 0x0aU << ( APP_USB_LANG_OFFSET ), /**< English (Belize) */ + APP_USBD_SUBLANG_ENGLISH_TRINIDAD = 0x0bU << ( APP_USB_LANG_OFFSET ), /**< English (Trinidad) */ + APP_USBD_SUBLANG_ENGLISH_PHILIPPINES = 0x0cU << ( APP_USB_LANG_OFFSET ), /**< English (Zimbabwe) */ + APP_USBD_SUBLANG_ENGLISH_ZIMBABWE = 0x0dU << ( APP_USB_LANG_OFFSET ), /**< English (Philippines) */ + APP_USBD_SUBLANG_FRENCH = 0x01U << ( APP_USB_LANG_OFFSET ), /**< French */ + APP_USBD_SUBLANG_FRENCH_BELGIAN = 0x02U << ( APP_USB_LANG_OFFSET ), /**< French (Belgian) */ + APP_USBD_SUBLANG_FRENCH_CANADIAN = 0x03U << ( APP_USB_LANG_OFFSET ), /**< French (Canadian) */ + APP_USBD_SUBLANG_FRENCH_SWISS = 0x04U << ( APP_USB_LANG_OFFSET ), /**< French (Swiss) */ + APP_USBD_SUBLANG_FRENCH_LUXEMBOURG = 0x05U << ( APP_USB_LANG_OFFSET ), /**< French (Luxembourg) */ + APP_USBD_SUBLANG_FRENCH_MONACO = 0x06U << ( APP_USB_LANG_OFFSET ), /**< French (Monaco) */ + APP_USBD_SUBLANG_GERMAN = 0x01U << ( APP_USB_LANG_OFFSET ), /**< German */ + APP_USBD_SUBLANG_GERMAN_SWISS = 0x02U << ( APP_USB_LANG_OFFSET ), /**< German (Swiss) */ + APP_USBD_SUBLANG_GERMAN_AUSTRIAN = 0x03U << ( APP_USB_LANG_OFFSET ), /**< German (Austrian) */ + APP_USBD_SUBLANG_GERMAN_LUXEMBOURG = 0x04U << ( APP_USB_LANG_OFFSET ), /**< German (Luxembourg) */ + APP_USBD_SUBLANG_GERMAN_LIECHTENSTEIN = 0x05U << ( APP_USB_LANG_OFFSET ), /**< German (Liechtenstein) */ + APP_USBD_SUBLANG_ITALIAN = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Italian */ + APP_USBD_SUBLANG_ITALIAN_SWISS = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Italian (Swiss) */ + APP_USBD_SUBLANG_KASHMIRI_INDIA = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Kashmiri (India) */ + APP_USBD_SUBLANG_KOREAN = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Korean */ + APP_USBD_SUBLANG_LITHUANIAN = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Lithuanian */ + APP_USBD_SUBLANG_MALAY_MALAYSIA = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Malay (Malaysia) */ + APP_USBD_SUBLANG_MALAY_BRUNEI_DARUSSALAM = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Malay (Brunei Darassalam) */ + APP_USBD_SUBLANG_NEPALI_INDIA = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Nepali (India) */ + APP_USBD_SUBLANG_NORWEGIAN_BOKMAL = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Norwegian (Bokmal) */ + APP_USBD_SUBLANG_NORWEGIAN_NYNORSK = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Norwegian (Nynorsk) */ + APP_USBD_SUBLANG_PORTUGUESE = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Portuguese (Brazilian) */ + APP_USBD_SUBLANG_PORTUGUESE_BRAZILIAN = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Portuguese */ + APP_USBD_SUBLANG_SERBIAN_LATIN = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Serbian (Latin) */ + APP_USBD_SUBLANG_SERBIAN_CYRILLIC = 0x03U << ( APP_USB_LANG_OFFSET ), /**< Serbian (Cyrillic) */ + APP_USBD_SUBLANG_SPANISH = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Castilian) */ + APP_USBD_SUBLANG_SPANISH_MEXICAN = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Mexican) */ + APP_USBD_SUBLANG_SPANISH_MODERN = 0x03U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Modern) */ + APP_USBD_SUBLANG_SPANISH_GUATEMALA = 0x04U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Guatemala) */ + APP_USBD_SUBLANG_SPANISH_COSTA_RICA = 0x05U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Costa Rica) */ + APP_USBD_SUBLANG_SPANISH_PANAMA = 0x06U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Panama) */ + APP_USBD_SUBLANG_SPANISH_DOMINICAN_REPUBLIC = 0x07U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Dominican Republic) */ + APP_USBD_SUBLANG_SPANISH_VENEZUELA = 0x08U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Venezuela) */ + APP_USBD_SUBLANG_SPANISH_COLOMBIA = 0x09U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Colombia) */ + APP_USBD_SUBLANG_SPANISH_PERU = 0x0aU << ( APP_USB_LANG_OFFSET ), /**< Spanish (Peru) */ + APP_USBD_SUBLANG_SPANISH_ARGENTINA = 0x0bU << ( APP_USB_LANG_OFFSET ), /**< Spanish (Argentina) */ + APP_USBD_SUBLANG_SPANISH_ECUADOR = 0x0cU << ( APP_USB_LANG_OFFSET ), /**< Spanish (Ecuador) */ + APP_USBD_SUBLANG_SPANISH_CHILE = 0x0dU << ( APP_USB_LANG_OFFSET ), /**< Spanish (Chile) */ + APP_USBD_SUBLANG_SPANISH_URUGUAY = 0x0eU << ( APP_USB_LANG_OFFSET ), /**< Spanish (Uruguay) */ + APP_USBD_SUBLANG_SPANISH_PARAGUAY = 0x0fU << ( APP_USB_LANG_OFFSET ), /**< Spanish (Paraguay) */ + APP_USBD_SUBLANG_SPANISH_BOLIVIA = 0x10U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Bolivia) */ + APP_USBD_SUBLANG_SPANISH_EL_SALVADOR = 0x11U << ( APP_USB_LANG_OFFSET ), /**< Spanish (El Salvador) */ + APP_USBD_SUBLANG_SPANISH_HONDURAS = 0x12U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Honduras) */ + APP_USBD_SUBLANG_SPANISH_NICARAGUA = 0x13U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Nicaragua) */ + APP_USBD_SUBLANG_SPANISH_PUERTO_RICO = 0x14U << ( APP_USB_LANG_OFFSET ), /**< Spanish (Puerto Rico) */ + APP_USBD_SUBLANG_SWEDISH = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Swedish */ + APP_USBD_SUBLANG_SWEDISH_FINLAND = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Swedish (Finland) */ + APP_USBD_SUBLANG_URDU_PAKISTAN = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Urdu (Pakistan) */ + APP_USBD_SUBLANG_URDU_INDIA = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Urdu (India) */ + APP_USBD_SUBLANG_UZBEK_LATIN = 0x01U << ( APP_USB_LANG_OFFSET ), /**< Uzbek (Latin) */ + APP_USBD_SUBLANG_UZBEK_CYRILLIC = 0x02U << ( APP_USB_LANG_OFFSET ), /**< Uzbek (Cyrillic) */ + APP_USBD_SUBLANG_HID_USAGE_DATA_DESCRIPTOR = 0x01U << ( APP_USB_LANG_OFFSET ), /**< HID (Usage Data Descriptor) */ + APP_USBD_SUBLANG_HID_VENDOR_DEFINED_1 = 0x3cU << ( APP_USB_LANG_OFFSET ), /**< HID (Vendor Defined 1) */ + APP_USBD_SUBLANG_HID_VENDOR_DEFINED_2 = 0x3dU << ( APP_USB_LANG_OFFSET ), /**< HID (Vendor Defined 2) */ + APP_USBD_SUBLANG_HID_VENDOR_DEFINED_3 = 0x3eU << ( APP_USB_LANG_OFFSET ), /**< HID (Vendor Defined 3) */ + APP_USBD_SUBLANG_HID_VENDOR_DEFINED_4 = 0x3fU << ( APP_USB_LANG_OFFSET ), /**< HID (Vendor Defined 4) */ +}app_usbd_langid_sub_t; + +/** + * @brief LANGID variable + * + * The LANGID value is composed from: + * - 10-bit (9-0) of Primary Language Identifiers + * - 6-bit (15-10) of Sublanguage Identifiers. + * + * @sa app_usbd_langid_primary_t + * @sa app_usbd_langid_sub_t + */ +typedef uint16_t app_usbd_langid_t; + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_LANGID_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_request.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_request.h new file mode 100644 index 0000000..d2f6f14 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_request.h @@ -0,0 +1,356 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef APP_USBD_REQUEST_H__ +#define APP_USBD_REQUEST_H__ + +#include "sdk_common.h" +#include "nrf.h" +#include "nrf_drv_usbd.h" +#include "app_usbd_descriptor.h" +#include "app_util_platform.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Compiler support for anonymous unions */ +ANON_UNIONS_ENABLE; + +#pragma pack(push, 1) + +/** + * @defgroup app_usbd_request USB standard requests + * @ingroup app_usbd + * + * @brief @tagAPI52840 Module with types definitions used for standard requests processing. + * @{ + */ + +/** + * @brief Recipient bit-field in request type + * + * Bits 4...0 + */ +#define APP_USBD_SETUP_REQ_BF_REC BF_CX(5, 0) + +/** + * @brief Type bit-field in request type + * + * Bits 6...5 + */ +#define APP_USBD_SETUP_REQ_BF_TYP BF_CX(2, 5) + +/** + * @brief Direction bit-field in request type + * + * Bit 7 + */ +#define APP_USBD_SETUP_REQ_BF_DIR BF_CX(1, 7) + +/** + * @brief Recipient enumerator. + * + * @note It is part of @ref app_usbd_setup_reqtype_t variable type. + */ +typedef enum { + APP_USBD_SETUP_REQREC_DEVICE = 0x0, /**< The whole device is a request target */ + APP_USBD_SETUP_REQREC_INTERFACE = 0x1, /**< Selected interface is a request target */ + APP_USBD_SETUP_REQREC_ENDPOINT = 0x2, /**< Selected endpoint is a request target */ + APP_USBD_SETUP_REQREC_OTHER = 0x3 /**< Other element is a request target */ +} app_usbd_setup_reqrec_t; + +/** + * @brief Request type enumerator. + * + * @note It is part of @ref app_usbd_setup_reqtype_t variable type. + */ +typedef enum { + APP_USBD_SETUP_REQTYPE_STD = 0x0, /**< Standard request */ + APP_USBD_SETUP_REQTYPE_CLASS = 0x1, /**< Class specific request */ + APP_USBD_SETUP_REQTYPE_VENDOR = 0x2 /**< Vendor specific request */ +} app_usbd_setup_reqtype_t; + +/** + * @brief Direction of setup command + * + * @note It is part of @ref app_usbd_setup_reqtype_t variable type. + */ +typedef enum { + APP_USBD_SETUP_REQDIR_OUT = 0x0, /**< Host to device */ + APP_USBD_SETUP_REQDIR_IN = 0x1, /**< Device to host */ +} app_usbd_setup_reqdir_t; + + +/** + * @brief Standard requests + * + * Enumerator for standard requests values + */ +typedef enum { + APP_USBD_SETUP_STDREQ_GET_STATUS = 0x00, /**< + * Targets: Device, Interface, Endpoint + * Expected SETUP frame format: + * - wValue: Zero + * - wIndex: Zero, (lb): Interface or Endpoint + * - wLength: 2 + * - Data:2 bytes of data, depending on targets + * - Device: + * - D15..D2: Reserved (Reset to zero) + * - D1: Remove Wakeup + * - D0: Self Powered + * - Interface: + * - D15..D0: Reserved (Reset to zero) + * - Endpoint: + * - D15..D1: Reserved (Reset to zero) + * - D0: Halt + */ + APP_USBD_SETUP_STDREQ_CLEAR_FEATURE = 0x01, /**< + * Targets: Device, Interface, Endpoint + * Expected SETUP frame format: + * - wValue: Feature selector (@ref app_usbd_setup_stdfeature_t) + * - wIndex: Zero, Interface or Endpoint + * - wLength: 0 + * - Data: None + */ + APP_USBD_SETUP_STDREQ_SET_FEATURE = 0x03, /**< + * Targets: Device, Interface, Endpoint + * Expected SETUP frame format: + * - wValue: Feature selector (@ref app_usbd_setup_stdfeature_t) + * - wIndex: Zero, Interface or Endpoint + * - wLength: 0 + * - Data: None + */ + APP_USBD_SETUP_STDREQ_SET_ADDRESS = 0x05, /**< + * @note This SETUP request is processed in hardware. + * Use it only to mark current USB state. + * + * Targets: Device + * Expected SETUP frame format: + * - wValue: New device address + * - wIndex: 0 + * - wLength: 0 + * - Data: None + */ + APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR = 0x06, /**< + * Targets: Device + * - wValue: (hb): Descriptor Type and (lb): Descriptor Index + * - wIndex: Zero of Language ID + * - wLength: Descriptor Length + * - Data: Descriptor + */ + APP_USBD_SETUP_STDREQ_SET_DESCRIPTOR = 0x07, /**< + * Not supported - Stall when called. + */ + APP_USBD_SETUP_STDREQ_GET_CONFIGURATION = 0x08, /**< + * Target: Device + * Expected SETUP frame format: + * - wValue: 0 + * - wIndex: 0 + * - wLength: 1 + * - Data: Configuration value + */ + APP_USBD_SETUP_STDREQ_SET_CONFIGURATION = 0x09, /**< + * Target: Device + * Expected SETUP frame format: + * - wValue: (lb): Configuration value + * - wIndex: 0 + * - wLength: 0 + * - Data: None + */ + APP_USBD_SETUP_STDREQ_GET_INTERFACE = 0x0A, /**< + * Target: Interface + * Expected SETUP frame format: + * - wValue: 0 + * - wIndex: Interface + * - wLength: 1 + * - Data: Alternate setting + */ + APP_USBD_SETUP_STDREQ_SET_INTERFACE = 0x0B, /**< + * Target: Interface + * Expected SETUP frame format: + * - wValue: Alternate setting + * - wIndex: Interface + * - wLength: 0 + * - Data: None + */ + APP_USBD_SETUP_STDREQ_SYNCH_FRAME = 0x0C /**< + * Target: Endpoint + * Expected SETUP frame format: + * - wValue: 0 + * - wIndex: Endpoint + * - wLength: 2 + * - Data: Frame Number + * + * @note + * This request is used only in connection with isochronous endpoints. + * This is rarely used and probably we would not need to support it. + */ +} app_usbd_setup_stdrequest_t; + +/** + * @brief Standard feature selectors + * + * Standard features that may be disabled or enabled by + * @ref APP_USBD_SETUP_STDREQ_CLEAR_FEATURE or @ref APP_USBD_SETUP_STDREQ_SET_FEATURE + */ +typedef enum { + APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP = 1, /**< + * Remote wakeup feature. + * Target: Device only + */ + APP_USBD_SETUP_STDFEATURE_ENDPOINT_HALT = 0, /**< + * Stall or clear the endpoint. + * Target: Endpoint different than default (0) + */ + APP_USBD_SETUP_STDFEATURE_TEST_MODE = 2 /**< + * Upstream port test mode. + * Power has to be cycled to exit test mode. + * This feature cannot be cleared. + * + * Target: Device only + * + * @note + * It should only be supported by HighSpeed capable devices. + * Not supported in this library. + */ +} app_usbd_setup_stdfeature_t; + + +/** + * @brief Universal way to access 16 bit values and its parts + */ +typedef union { + uint16_t w; //!< 16 bit access + struct + { + uint8_t lb; //!< Low byte access + uint8_t hb; //!< High byte access + }; +} app_usbd_setup_w_t; + +/** + * @brief Internal redefinition of setup structure + * + * Redefinition of the structure to simplify changes in the future + * if required - app_usbd API would present setup data using app_usbd_setup_t. + * + * The structure layout is always the same like @ref nrf_drv_usbd_setup_t + */ +typedef struct { + uint8_t bmRequestType; //!< Setup type bitfield + uint8_t bmRequest; //!< One of @ref app_usbd_setup_stdrequest_t values or class dependent one. + app_usbd_setup_w_t wValue; //!< byte 2, 3 + app_usbd_setup_w_t wIndex; //!< byte 4, 5 + app_usbd_setup_w_t wLength; //!< byte 6, 7 +} app_usbd_setup_t; + +#pragma pack(pop) + + +/** + * @brief Extract recipient from request type + * + * @param[in] bmRequestType + * + * @return Extracted recipient field from request type value + */ +static inline app_usbd_setup_reqrec_t app_usbd_setup_req_rec(uint8_t bmRequestType) +{ + return (app_usbd_setup_reqrec_t)BF_CX_GET(bmRequestType, APP_USBD_SETUP_REQ_BF_REC); +} + +/** + * @brief Extract type from request type + * + * @param[in] bmRequestType + * + * @return Extracted type field from request type value + */ +static inline app_usbd_setup_reqtype_t app_usbd_setup_req_typ(uint8_t bmRequestType) +{ + return (app_usbd_setup_reqtype_t)BF_CX_GET(bmRequestType, APP_USBD_SETUP_REQ_BF_TYP); +} + + +/** + * @brief Extract direction from request type + * + * @param[in] bmRequestType + * + * @return Extracted direction field from request type value + */ +static inline app_usbd_setup_reqdir_t app_usbd_setup_req_dir(uint8_t bmRequestType) +{ + return (app_usbd_setup_reqdir_t)BF_CX_GET(bmRequestType, APP_USBD_SETUP_REQ_BF_DIR); +} + +/** + * @brief Make request type value + * + * @param[in] rec Recipient + * @param[in] typ Request type + * @param[in] dir Direction + * + * @return Assembled request type value + */ +static inline uint8_t app_usbd_setup_req_val(app_usbd_setup_reqrec_t rec, + app_usbd_setup_reqtype_t typ, + app_usbd_setup_reqdir_t dir) +{ + uint32_t bmRequestType = ( + BF_CX_VAL(rec, APP_USBD_SETUP_REQ_BF_REC) | + BF_CX_VAL(typ, APP_USBD_SETUP_REQ_BF_TYP) | + BF_CX_VAL(dir, APP_USBD_SETUP_REQ_BF_DIR) + ); + + ASSERT(bmRequestType < 256U); + return (uint8_t)bmRequestType; +} + + +ANON_UNIONS_DISABLE; +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_REQUEST_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_serial_num.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_serial_num.c new file mode 100644 index 0000000..bec2f4e --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_serial_num.c @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "app_usbd_serial_num.h" + +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <inttypes.h> +#include "app_usbd.h" + +#define SERIAL_NUMBER_STRING_SIZE (12) + +/**@brief Serial number generated + * + * Serial number generated by the @ref serial_number_string_create function + */ +uint16_t g_extern_serial_number[SERIAL_NUMBER_STRING_SIZE + 1]; + + +/**@brief Function for creating the serial number string from a regular C string. + * + * @param[in] p_serial_number_string The serial number string. Must be terminated with \0. + */ +static void string_create(char * p_serial_number_string) +{ + g_extern_serial_number[0] = + (uint16_t)APP_USBD_DESCRIPTOR_STRING << 8 | sizeof(g_extern_serial_number); + + for (uint32_t i = 0; i < strlen(p_serial_number_string); i++) + { + g_extern_serial_number[i + 1] = (uint8_t)p_serial_number_string[i]; + } +} + + +void app_usbd_serial_num_generate(void) +{ + char serial_number_string[SERIAL_NUMBER_STRING_SIZE + 1]; + const uint16_t serial_num_high_bytes = (uint16_t)NRF_FICR->DEVICEADDR[1] | 0xC000; // The masking makes the address match the Random Static BLE address. + const uint32_t serial_num_low_bytes = NRF_FICR->DEVICEADDR[0]; + + (void)snprintf(serial_number_string, + SERIAL_NUMBER_STRING_SIZE + 1, + "%04"PRIX16"%08"PRIX32, + serial_num_high_bytes, + serial_num_low_bytes); + + string_create(serial_number_string); +} + diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_serial_num.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_serial_num.h new file mode 100644 index 0000000..ca0957c --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_serial_num.h @@ -0,0 +1,75 @@ +/** + * Copyright (c) 2018 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef APP_USBD_SERIAL_NUM_H__ +#define APP_USBD_SERIAL_NUM_H__ + +#include <stdint.h> +#include <stdio.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_serial_num USBD serial number generator + * @ingroup app_usbd + * + * @brief @tagAPI52840 Generate a standard USB serial number that is unique for each device. + * @{ + */ + +/**@brief Function for generating a default serial number string based on FIRC->DEVICEADDR. + * + * After calling this function, the serial number is ready for the USB driver. + * + * The generated serial number shows up as a 12-hexidecimal-digit string with no delimiters + * (e.g 123456ABCDEF). The byte string is also printed on the PCA10059 dongle. It is also used as + * the default advertising address in the SoftDevice. + */ +void app_usbd_serial_num_generate(void); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif // APP_USBD_SERIAL_NUM_H__ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_string_desc.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_string_desc.c new file mode 100644 index 0000000..0893b45 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_string_desc.c @@ -0,0 +1,188 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_config.h" +#if APP_USBD_ENABLED +#include "app_usbd_string_desc.h" +#include "app_usbd_langid.h" +#include "app_usbd_core.h" +#include "nordic_common.h" + +/** + * @defgroup app_usbd_string_desc + * @ingroup app_usbd + * + * USBD string descriptors management + * @{ + */ + +/** + * @brief Array with language identifiers + * + * This array would be used to search the proper string for selected language. + */ +static const uint16_t m_langids[] = { APP_USBD_STRINGS_LANGIDS }; + +/** + * @brief Language ID descriptor + * + * Language + */ + + +/** + * @brief Mnemonics for the string positions in the array + * + * The mnemonics for the indexes of the strings inside the string array. + */ +enum { + APP_USBD_STRING_ID_LANGIDS_ARRAY_POS = 0, /**< Supported language identifiers */ + APP_USBD_STRING_ID_MANUFACTURER_ARRAY_POS, /**< Manufacturer name */ + APP_USBD_STRING_ID_PRODUCT_ARRAY_POS, /**< Product name */ + APP_USBD_STRING_ID_SERIAL_ARRAY_POS, /**< Serial number */ +#define X(mnemonic, str_idx, ...) CONCAT_2(mnemonic, _ARRAY_POS), + APP_USBD_STRINGS_USER +#undef X +}; + +/** + * @brief String index into internal array index conversion table + * + * The array that transforms the USB string indexes into internal array position. + * @note Value 0 used to mark non-existing string. + */ +static const uint8_t m_string_translation[APP_USBD_STRING_ID_CNT] = +{ + [APP_USBD_STRING_ID_LANGIDS ] = APP_USBD_STRING_ID_LANGIDS_ARRAY_POS, + [APP_USBD_STRING_ID_MANUFACTURER] = APP_USBD_STRING_ID_MANUFACTURER_ARRAY_POS, + [APP_USBD_STRING_ID_PRODUCT ] = APP_USBD_STRING_ID_PRODUCT_ARRAY_POS, + [APP_USBD_STRING_ID_SERIAL ] = APP_USBD_STRING_ID_SERIAL_ARRAY_POS, +#define X(mnemonic, str_idx, ...) [mnemonic] = CONCAT_2(mnemonic, _ARRAY_POS), + APP_USBD_STRINGS_USER +#undef X +}; + +#ifndef APP_USBD_STRINGS_MANUFACTURER_EXTERN +#define APP_USBD_STRINGS_MANUFACTURER_EXTERN 0 +#endif + +#ifndef APP_USBD_STRINGS_PRODUCT_EXTERN +#define APP_USBD_STRINGS_PRODUCT_EXTERN 0 +#endif + +#ifndef APP_USBD_STRING_SERIAL_EXTERN +#define APP_USBD_STRING_SERIAL_EXTERN 0 +#endif + +#if APP_USBD_STRINGS_MANUFACTURER_EXTERN +extern uint16_t APP_USBD_STRINGS_MANUFACTURER[]; +#endif + +#if APP_USBD_STRINGS_PRODUCT_EXTERN +extern uint16_t APP_USBD_STRINGS_PRODUCT[]; +#endif + +#if APP_USBD_STRING_SERIAL_EXTERN +extern uint16_t APP_USBD_STRING_SERIAL[]; +#endif + + +/** + * @brief String descriptors table + * */ +static const uint16_t * m_string_dsc[APP_USBD_STRING_ID_CNT][ARRAY_SIZE(m_langids)] = +{ + [APP_USBD_STRING_ID_LANGIDS_ARRAY_POS] = { APP_USBD_STRING_DESC(APP_USBD_STRINGS_LANGIDS) }, + [APP_USBD_STRING_ID_MANUFACTURER_ARRAY_POS] = { APP_USBD_STRINGS_MANUFACTURER }, + [APP_USBD_STRING_ID_PRODUCT_ARRAY_POS] = { APP_USBD_STRINGS_PRODUCT }, + [APP_USBD_STRING_ID_SERIAL_ARRAY_POS] = { APP_USBD_STRING_SERIAL }, +#define X(mnemonic, str_idx, ...) [CONCAT_2(mnemonic, _ARRAY_POS)] = { __VA_ARGS__ }, + APP_USBD_STRINGS_USER +#undef X +}; + + + +uint16_t const * app_usbd_string_desc_get(app_usbd_string_desc_idx_t idx, uint16_t langid) +{ + /* LANGID string */ + if (APP_USBD_STRING_ID_LANGIDS == idx) + { + return m_string_dsc[APP_USBD_STRING_ID_LANGIDS_ARRAY_POS][0]; + } + + /* Searching for the language */ + uint8_t lang_idx = 0; + if (ARRAY_SIZE(m_langids) > 1) + { + while (m_langids[lang_idx] != langid) + { + if (++lang_idx >= ARRAY_SIZE(m_langids)) + { + return NULL; + } + } + } + + /* Get the string index in array */ + if (idx >= ARRAY_SIZE(m_string_translation)) + { + return NULL; + } + + uint8_t str_pos = m_string_translation[idx]; + if (str_pos == 0) + { + return NULL; + } + + uint16_t const * p_str = m_string_dsc[str_pos][lang_idx]; + if ((ARRAY_SIZE(m_langids) > 1) && (lang_idx != 0)) + { + if (p_str == NULL) + { + p_str = m_string_dsc[str_pos][0]; + } + } + + return p_str; +} + +/** @} */ +#endif // APP_USBD_ENABLED diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_string_desc.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_string_desc.h new file mode 100644 index 0000000..2a4888c --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_string_desc.h @@ -0,0 +1,119 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_STRING_DESC_H__ +#define APP_USBD_STRING_DESC_H__ + +#include <stdint.h> +#include "sdk_common.h" +#include "app_usbd.h" +#include "app_usbd_string_config.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_string_desc USBD string descriptors + * @ingroup app_usbd + * + * @brief @tagAPI52840 USBD string descriptors management. + * @{ + */ + +/** + * @brief USB string initialization + * + * Macro that creates initialization values for USB string. + * The format contains header and string itself. + * The string should be declared as an array of uint16_t type. + * + * @param[in] ... Comma separated string letters or language ID. + * @return String descriptor initialization data. + */ +#define APP_USBD_STRING_DESC(...) (const uint16_t[]){ \ + (0xff & (sizeof((uint16_t[]){__VA_ARGS__}) + 2)) | \ + ((uint16_t)APP_USBD_DESCRIPTOR_STRING) << 8, \ + __VA_ARGS__ } + +/** + * @brief USB string descriptors ID's + */ +typedef enum { + APP_USBD_STRING_ID_LANGIDS = 0, /**< Supported language identifiers */ + APP_USBD_STRING_ID_MANUFACTURER, /**< Manufacturer name */ + APP_USBD_STRING_ID_PRODUCT, /**< Product name */ + APP_USBD_STRING_ID_SERIAL, /**< Serial number */ + +#define X(mnemonic, str_idx, ...) mnemonic str_idx, + APP_USBD_STRINGS_USER +#undef X + + APP_USBD_STRING_ID_CNT /**< Total number of identifiers */ +} app_usbd_string_desc_idx_t; + +/** + * @brief Get string descriptor + * + * @param[in] idx String descriptor index + * @param[in] langid Selected language for the string + * @return String descriptor or NULL if not exist + * */ +uint16_t const * app_usbd_string_desc_get(app_usbd_string_desc_idx_t idx, uint16_t langid); + +/** + * @brief Get string length + * + * Function to get string length from descriptor (descriptor returned by @ref app_usbd_string_desc_get) + * + * @param[in] p_str String descriptor pointer + * @return Total descriptor length in bytes + */ +static inline size_t app_usbd_string_desc_length(uint16_t const * p_str) +{ + return ((const app_usbd_descriptor_string_t *)p_str)->bLength; +} + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_STRING_DESC_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_types.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_types.h new file mode 100644 index 0000000..afb879b --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/app_usbd_types.h @@ -0,0 +1,248 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef APP_USBD_TYPES_H__ +#define APP_USBD_TYPES_H__ + +#include <stdint.h> + +#include "sdk_errors.h" +#include "nrf_drv_usbd.h" +#include "app_usbd_request.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_types USB Device high level library variable types definition + * @ingroup app_usbd + * + * @brief @tagAPI52840 All types used by @ref app_usbd are defined here. + * This helps to avoid cross referencing into types in different files. + * @{ + */ + +/** + * @brief Change given value to 2 digits in BCD notation + * + * @param[in] val The decimal value to be converted in the range from 0 to 99. + * @return Calculated BCD value. + */ +#define APP_USBD_BCD_2_MAKE(val) ( \ + ((((val) % 100) / 10) * 0x10) + \ + ((((val) % 10) / 1) * 0x1) \ + ) + +/** + * @brief Change given decimal version values to 4 digits in BCD notation + * + * USB specification uses 4 digits BCD version notation in many descriptors. + * This macro changes 2 values to 4 BCD digits (one 16 bit value) + * that describes version in USB standard. + * + * @param[in] major Major version + * @param[in] minor Minor version + * + * @return Calculated 16 bit value with BCD representation of the version + */ +#define APP_USBD_BCD_VER_MAKE(major, minor) \ + ((APP_USBD_BCD_2_MAKE(major) << 8) | APP_USBD_BCD_2_MAKE(minor)) + +/** + * @brief Events codes + * + * Redefined application event codes + */ +typedef enum +{ + APP_USBD_EVT_DRV_SOF = NRF_DRV_USBD_EVT_SOF, /**< See documentation for @ref NRF_DRV_USBD_EVT_SOF */ + APP_USBD_EVT_DRV_RESET = NRF_DRV_USBD_EVT_RESET, /**< See documentation for @ref NRF_DRV_USBD_EVT_RESET */ + APP_USBD_EVT_DRV_SUSPEND = NRF_DRV_USBD_EVT_SUSPEND, /**< See documentation for @ref NRF_DRV_USBD_EVT_SUSPEND */ + APP_USBD_EVT_DRV_RESUME = NRF_DRV_USBD_EVT_RESUME, /**< See documentation for @ref NRF_DRV_USBD_EVT_RESUME */ + APP_USBD_EVT_DRV_WUREQ = NRF_DRV_USBD_EVT_WUREQ, /**< See documentation for @ref NRF_DRV_USBD_EVT_WUREQ */ + APP_USBD_EVT_DRV_SETUP = NRF_DRV_USBD_EVT_SETUP, /**< This event type has special structure. See @ref app_usbd_setup_evt_t */ + APP_USBD_EVT_DRV_EPTRANSFER = NRF_DRV_USBD_EVT_EPTRANSFER, /**< See documentation for @ref NRF_DRV_USBD_EVT_EPTRANSFER */ + + APP_USBD_EVT_FIRST_POWER, /**< First power event code - for internal static assert checking */ + + APP_USBD_EVT_POWER_DETECTED, /**< See documentation for @ref NRF_DRV_POWER_USB_EVT_DETECTED */ + APP_USBD_EVT_POWER_REMOVED, /**< See documentation for @ref NRF_DRV_POWER_USB_EVT_REMOVED */ + APP_USBD_EVT_POWER_READY, /**< See documentation for @ref NRF_DRV_POWER_USB_EVT_READY */ + + + APP_USBD_EVT_FIRST_APP, /**< First application event code - for internal static assert checking */ + + APP_USBD_EVT_INST_APPEND = APP_USBD_EVT_FIRST_APP, /**< The instance was attached to the library, any configuration action can be done now */ + APP_USBD_EVT_INST_REMOVE, /**< + * The instance is going to be removed, this event is called just before removing the instance. + * This removing cannot be stopped. + */ + APP_USBD_EVT_STARTED, /**< USBD library has just been started and functional - event passed to all instances, before USBD interrupts have been enabled */ + APP_USBD_EVT_STOPPED, /**< USBD library has just been stopped and is not functional - event passed to all instances, after USBD interrupts have been disabled*/ + + APP_USBD_EVT_STATE_CHANGED, /**< + * Informs all the classes that base state has been changed. + * This event is processed before setup stage that caused the state change finishes (before acknowledging it). + */ + + APP_USBD_EVT_FIRST_INTERNAL = 0x80, /**< First internal event, used by the APP library internally. */ + + APP_USBD_EVT_HFCLK_READY = APP_USBD_EVT_FIRST_INTERNAL, /**< High frequency clock started */ + APP_USBD_EVT_START_REQ, /**< Start requested */ + APP_USBD_EVT_STOP_REQ, /**< Stop requested */ + APP_USBD_EVT_SUSPEND_REQ, /**< Suspend request - HFCLK would be released and USBD peripheral clock would be disconnected */ + APP_USBD_EVT_WAKEUP_REQ, /**< Wakeup request - start the whole wakeup generation. */ + +} app_usbd_event_type_t; + + + +/** + * @brief Specific application event structure + * + * All the data required by the events that comes from the application level + */ +typedef struct +{ + app_usbd_event_type_t type; //!< Event type +} app_usbd_evt_t; + +/** + * @brief Specific application event structure with setup structure included + * + * This event structure would be used when @ref APP_USBD_EVT_DRV_SETUP + * is passed to instance event handler + */ +typedef struct +{ + app_usbd_event_type_t type; //!< Event type + app_usbd_setup_t setup; //!< Setup structure +} app_usbd_setup_evt_t; + + +/** + * @brief Complex event variable type + * + * A variable that can store any kind of event. + */ +typedef union +{ + app_usbd_event_type_t type; //!< Event type + nrf_drv_usbd_evt_t drv_evt; //!< Events that comes directly from the driver. + /**< Use this event structure only for event + * type < @ref APP_USBD_EVT_FIRST_APP + */ + app_usbd_setup_evt_t setup_evt; //!< Event structure with SETUP structure included. + /**< This structure is used in connection with + * @ref APP_USBD_EVT_DRV_SETUP + */ + app_usbd_evt_t app_evt; //!< Events that comes from the application driver. + /**< Use this event structure only for event + * type >= @ref APP_USBD_EVT_FIRST_APP + */ +} app_usbd_complex_evt_t; + + +/** + * @brief Internal event variable type + * + * The variable type used for internal event processing. + * This kind of event is the one that goes into the event queue. + * + * @note There is no setup event structure. + * This structure would be created when setup event is processed. + * The reason for that is the fact that setup event structure has high memory printout. + */ +typedef union +{ + app_usbd_event_type_t type; //!< Event type + nrf_drv_usbd_evt_t drv_evt; //!< Events that comes directly from the driver. + /**< Use this event structure only for event + * type < @ref APP_USBD_EVT_FIRST_APP + */ + app_usbd_evt_t app_evt; //!< Events that comes from the application driver. + /**< Use this event structure only for event + * type >= @ref APP_USBD_EVT_FIRST_APP + */ +} app_usbd_internal_evt_t; + + +#ifdef DOXYGEN +/** + * @brief Base instance of a USBD class + * + * Any USBD class instance have to begin with this instance. + * This may then be followed by any implementation dependent data. + * + * For an instance it should be possible to put whole structure into FLASH. + * + * @note This type is early defined as incomplete type. + * This is required for function declaration that takes the pointer + * to this structure but in second hand - it is also placed inside + * the instance structure. + * @note The structure is defined in @file app_usbd_class_base.h. + */ +typedef struct {} app_usbd_class_inst_t; +#else +typedef struct app_usbd_class_inst_s app_usbd_class_inst_t; +#endif +/** + * @brief Endpoint callback function + * + * The function used by every class instance. + * @param[in,out] p_inst Instance of the class + * @param[in] p_event Event to process + * + * @note If given event is not supported by class, return @ref NRF_ERROR_NOT_SUPPORTED + */ +typedef ret_code_t (*app_usbd_ep_event_handler_t)( + app_usbd_class_inst_t const * const p_inst, + app_usbd_complex_evt_t const * const p_event + ); + + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_TYPES_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio.c new file mode 100644 index 0000000..19e5a36 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio.c @@ -0,0 +1,813 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(APP_USBD_AUDIO) + +#include "app_usbd_audio.h" +#include "app_util_platform.h" + +/** + * @defgroup app_usbd_audio_internals USBD Audio internals + * @{ + * @ingroup app_usbd_audio + * @internal + */ + +STATIC_ASSERT(sizeof(app_usbd_audio_ac_iface_header_desc_t) == 8); +STATIC_ASSERT(sizeof(app_usbd_audio_input_terminal_desc_t) == 12); +STATIC_ASSERT(sizeof(app_usbd_audio_output_terminal_desc_t) == 9); +STATIC_ASSERT(sizeof(app_usbd_audio_feature_unit_desc_t) == 6); +STATIC_ASSERT(sizeof(app_usbd_audio_as_iface_desc_t) == 7); +STATIC_ASSERT(sizeof(app_usbd_audio_as_format_type_one_desc_t) == 8); +STATIC_ASSERT(sizeof(app_usbd_audio_as_format_type_two_desc_t) == 9); +STATIC_ASSERT(sizeof(app_usbd_audio_as_format_type_three_desc_t) == 8); +STATIC_ASSERT(sizeof(app_usbd_audio_as_endpoint_desc_t) == 7); + +#define APP_USBD_AUDIO_CONTROL_IFACE_IDX 0 /**< Audio class control interface index */ +#define APP_USBD_AUDIO_STREAMING_IFACE_IDX 1 /**< Audio class streaming interface index */ + +#define APP_USBD_CDC_AUDIO_STREAMING_EP_IDX 0 /**< Audio streaming isochronous endpoint index */ + +/** + * @brief Auxiliary function to access audio class instance data + * + * @param[in] p_inst Class instance data + * @return Audio class instance data @ref app_usbd_audio_t + */ +static inline app_usbd_audio_t const * audio_get(app_usbd_class_inst_t const * p_inst) +{ + ASSERT(p_inst != NULL); + return (app_usbd_audio_t const *)p_inst; +} + + +/** + * @brief Auxiliary function to access audio class context data + * + * @param[in] p_audio Audio class instance data + * @return Audio class context data @ref app_usbd_audio_ctx_t + */ +static inline app_usbd_audio_ctx_t * audio_ctx_get(app_usbd_audio_t const * p_audio) +{ + ASSERT(p_audio != NULL); + ASSERT(p_audio->specific.p_data != NULL); + return &p_audio->specific.p_data->ctx; +} + + +/** + * @brief User event handler + * + * @param[in] p_inst Class instance + * @param[in] event user Event type @ref app_usbd_audio_user_event_t + */ +static inline void user_event_handler( + app_usbd_class_inst_t const * p_inst, + app_usbd_audio_user_event_t event) +{ + app_usbd_audio_t const * p_audio = audio_get(p_inst); + + if (p_audio->specific.inst.user_ev_handler != NULL) + { + p_audio->specific.inst.user_ev_handler(p_inst, event); + } +} + + +/** + * @brief Select interface + * + * @param[in,out] p_inst Instance of the class + * @param[in] iface_idx Index of the interface inside class structure + * @param[in] alternate Alternate setting that should be selected + */ +static ret_code_t iface_select( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx, + uint8_t alternate) +{ + app_usbd_class_iface_conf_t const * p_iface = app_usbd_class_iface_get(p_inst, iface_idx); + /* Simple check if this is data interface */ + uint8_t const ep_count = app_usbd_class_iface_ep_count_get(p_iface); + + if (ep_count > 0) + { + if (alternate > 1) + { + return NRF_ERROR_INVALID_PARAM; + } + app_usbd_audio_t const * p_audio = audio_get(p_inst); + app_usbd_audio_ctx_t * p_audio_ctx = audio_ctx_get(p_audio); + p_audio_ctx->streaming = (alternate != 0); + + uint8_t i; + + for (i = 0; i < ep_count; ++i) + { + nrf_drv_usbd_ep_t ep_addr = + app_usbd_class_ep_address_get(app_usbd_class_iface_ep_get(p_iface, i)); + if (alternate) + { + app_usbd_ep_enable(ep_addr); + } + else + { + app_usbd_ep_disable(ep_addr); + } + } + return NRF_SUCCESS; + } + return NRF_ERROR_NOT_SUPPORTED; +} + + +static void iface_deselect( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx) +{ + app_usbd_class_iface_conf_t const * p_iface = app_usbd_class_iface_get(p_inst, iface_idx); + + /* Simple check if this is data interface */ + if (p_iface->ep_cnt > 0) + { + app_usbd_audio_t const * p_audio = audio_get(p_inst); + app_usbd_audio_ctx_t * p_audio_ctx = audio_ctx_get(p_audio); + p_audio_ctx->streaming = false; + } + /* Note that all the interface endpoints would be disabled automatically after this function */ +} + + +static uint8_t iface_selection_get( + app_usbd_class_inst_t const * const p_inst, + uint8_t iface_idx) +{ + app_usbd_class_iface_conf_t const * p_iface = app_usbd_class_iface_get(p_inst, iface_idx); + /* Simple check if this is data interface */ + uint8_t const ep_count = app_usbd_class_iface_ep_count_get(p_iface); + + if (ep_count > 0) + { + app_usbd_audio_t const * p_audio = audio_get(p_inst); + app_usbd_audio_ctx_t * p_audio_ctx = audio_ctx_get(p_audio); + return (p_audio_ctx->streaming) ? 1 : 0; + } + return 0; +} + + +/** + * @brief Internal SETUP standard IN request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_req_std_in(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + /* Only Get Descriptor standard IN request is supported by Audio class */ + if ((app_usbd_setup_req_rec(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQREC_INTERFACE) + && + (p_setup_ev->setup.bmRequest == APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR)) + { + size_t dsc_len = 0; + size_t max_size; + + uint8_t * p_trans_buff = app_usbd_core_setup_transfer_buff_get(&max_size); + + /* Try to find descriptor in class internals*/ + ret_code_t ret = app_usbd_class_descriptor_find( + p_inst, + p_setup_ev->setup.wValue.hb, + p_setup_ev->setup.wValue.lb, + p_trans_buff, + &dsc_len); + + if (ret != NRF_ERROR_NOT_FOUND) + { + ASSERT(dsc_len < NRF_DRV_USBD_EPSIZE); + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_trans_buff, dsc_len); + } + } + + return NRF_ERROR_NOT_SUPPORTED; +} + + +/** + * @brief Internal SETUP class IN request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_req_class_in( + app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_AUDIO_REQ_GET_CUR: + case APP_USBD_AUDIO_REQ_GET_MIN: + case APP_USBD_AUDIO_REQ_GET_MAX: + case APP_USBD_AUDIO_REQ_SET_RES: + case APP_USBD_AUDIO_REQ_GET_MEM: + { + app_usbd_audio_t const * p_audio = audio_get(p_inst); + app_usbd_audio_ctx_t * p_audio_ctx = audio_ctx_get(p_audio); + + + p_audio_ctx->request.req_type = (app_usbd_audio_req_type_t)p_setup_ev->setup.bmRequest; + p_audio_ctx->request.control = p_setup_ev->setup.wValue.hb; + p_audio_ctx->request.channel = p_setup_ev->setup.wValue.lb; + p_audio_ctx->request.interface = p_setup_ev->setup.wIndex.hb; + p_audio_ctx->request.entity = p_setup_ev->setup.wIndex.lb; + + p_audio_ctx->request.length = p_setup_ev->setup.wLength.w; + + p_audio_ctx->request.req_target = APP_USBD_AUDIO_CLASS_REQ_IN; + + app_usbd_setup_reqrec_t rec = app_usbd_setup_req_rec(p_setup_ev->setup.bmRequestType); + if (rec == APP_USBD_SETUP_REQREC_ENDPOINT) + { + p_audio_ctx->request.req_target = APP_USBD_AUDIO_EP_REQ_IN; + } + + user_event_handler((app_usbd_class_inst_t const *)p_audio, + APP_USBD_AUDIO_USER_EVT_CLASS_REQ); + + return app_usbd_core_setup_rsp(&p_setup_ev->setup, + p_audio_ctx->request.payload, + p_audio_ctx->request.length); + } + + default: + break; + } + + return NRF_ERROR_NOT_SUPPORTED; +} + + +static ret_code_t audio_req_out_data_cb(nrf_drv_usbd_ep_status_t status, void * p_context) +{ + if (status == NRF_USBD_EP_OK) + { + app_usbd_audio_t const * p_audio = p_context; + + user_event_handler((app_usbd_class_inst_t const *)p_audio, + APP_USBD_AUDIO_USER_EVT_CLASS_REQ); + } + + return NRF_SUCCESS; +} + + +static ret_code_t audio_req_out( + app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_audio_t const * p_audio = audio_get(p_inst); + app_usbd_audio_ctx_t * p_audio_ctx = audio_ctx_get(p_audio); + + + p_audio_ctx->request.req_type = (app_usbd_audio_req_type_t)p_setup_ev->setup.bmRequest; + p_audio_ctx->request.control = p_setup_ev->setup.wValue.hb; + p_audio_ctx->request.channel = p_setup_ev->setup.wValue.lb; + p_audio_ctx->request.interface = p_setup_ev->setup.wIndex.hb; + p_audio_ctx->request.entity = p_setup_ev->setup.wIndex.lb; + + p_audio_ctx->request.length = p_setup_ev->setup.wLength.w; + + p_audio_ctx->request.req_target = APP_USBD_AUDIO_CLASS_REQ_OUT; + if (app_usbd_setup_req_rec(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQREC_ENDPOINT) + { + p_audio_ctx->request.req_target = APP_USBD_AUDIO_EP_REQ_OUT; + } + + /*Request setup data*/ + NRF_DRV_USBD_TRANSFER_OUT(transfer, p_audio_ctx->request.payload, p_audio_ctx->request.length); + ret_code_t ret; + CRITICAL_REGION_ENTER(); + ret = app_usbd_ep_transfer(NRF_DRV_USBD_EPOUT0, &transfer); + if (ret == NRF_SUCCESS) + { + app_usbd_core_setup_data_handler_desc_t desc = { + .handler = audio_req_out_data_cb, + .p_context = (void *)p_audio + }; + + ret = app_usbd_core_setup_data_handler_set(NRF_DRV_USBD_EPOUT0, &desc); + } + CRITICAL_REGION_EXIT(); + + return ret; +} + + +/** + * @brief Internal SETUP class OUT request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_req_class_out( + app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_AUDIO_REQ_SET_CUR: + case APP_USBD_AUDIO_REQ_SET_MIN: + case APP_USBD_AUDIO_REQ_SET_MAX: + case APP_USBD_AUDIO_REQ_SET_RES: + case APP_USBD_AUDIO_REQ_SET_MEM: + return audio_req_out(p_inst, p_setup_ev); + + default: + break; + } + + return NRF_ERROR_NOT_SUPPORTED; +} + + +/** + * @brief Control endpoint handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_event_handler( + app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + ASSERT(p_inst != NULL); + ASSERT(p_setup_ev != NULL); + + if (app_usbd_setup_req_dir(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQDIR_IN) + { + switch (app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + case APP_USBD_SETUP_REQTYPE_STD: + return setup_req_std_in(p_inst, p_setup_ev); + + case APP_USBD_SETUP_REQTYPE_CLASS: + return setup_req_class_in(p_inst, p_setup_ev); + + default: + break; + } + } + else /*APP_USBD_SETUP_REQDIR_OUT*/ + { + switch (app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + case APP_USBD_SETUP_REQTYPE_CLASS: + return setup_req_class_out(p_inst, p_setup_ev); + + default: + break; + } + } + + return NRF_ERROR_NOT_SUPPORTED; +} + + +/** + * @brief Endpoint IN event handler + * + * @param[in] p_inst Generic class instance + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t endpoint_in_event_handler(app_usbd_class_inst_t const * p_inst) +{ + user_event_handler(p_inst, APP_USBD_AUDIO_USER_EVT_TX_DONE); + return NRF_SUCCESS; +} + + +/** + * @brief Endpoint OUT event handler + * + * @param[in] p_inst Generic class instance + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t endpoint_out_event_handler(app_usbd_class_inst_t const * p_inst) +{ + user_event_handler(p_inst, APP_USBD_AUDIO_USER_EVT_RX_DONE); + return NRF_SUCCESS; +} + + +/** + * @brief Auxiliary function to access isochronous endpoint address + * + * @param[in] p_inst Class instance data + * + * @return ISO endpoint address + */ +static inline nrf_drv_usbd_ep_t ep_iso_addr_get(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_class_iface_conf_t const * class_iface; + + class_iface = app_usbd_class_iface_get(p_inst, APP_USBD_AUDIO_STREAMING_IFACE_IDX); + + app_usbd_class_ep_conf_t const * ep_cfg; + ep_cfg = app_usbd_class_iface_ep_get(class_iface, APP_USBD_CDC_AUDIO_STREAMING_EP_IDX); + + return app_usbd_class_ep_address_get(ep_cfg); +} + + +/** + * @brief @ref app_usbd_class_methods_t::event_handler + */ +static ret_code_t audio_event_handler( + app_usbd_class_inst_t const * p_inst, + app_usbd_complex_evt_t const * p_event) +{ + ASSERT(p_inst != NULL); + ASSERT(p_event != NULL); + + ret_code_t ret = NRF_SUCCESS; + + switch (p_event->app_evt.type) + { + case APP_USBD_EVT_DRV_RESET: + break; + + case APP_USBD_EVT_DRV_SETUP: + ret = setup_event_handler(p_inst, (app_usbd_setup_evt_t const *)p_event); + break; + + case APP_USBD_EVT_DRV_EPTRANSFER: + if (NRF_USBD_EPIN_CHECK(p_event->drv_evt.data.eptransfer.ep)) + { + ret = endpoint_in_event_handler(p_inst); + } + else + { + ret = endpoint_out_event_handler(p_inst); + } + break; + + case APP_USBD_EVT_DRV_SUSPEND: + break; + + case APP_USBD_EVT_DRV_RESUME: + break; + + case APP_USBD_EVT_INST_APPEND: + break; + + case APP_USBD_EVT_INST_REMOVE: + break; + + case APP_USBD_EVT_STARTED: + break; + + case APP_USBD_EVT_STOPPED: + break; + + default: + ret = NRF_ERROR_NOT_SUPPORTED; + break; + } + + return ret; +} + + +static size_t audio_get_format_descriptor_size(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_audio_t const * p_audio = audio_get(p_inst); + + return p_audio->specific.inst.p_format_dsc->size; +} + + +static size_t audio_get_format_descriptor_data(app_usbd_class_inst_t const * p_inst, + uint32_t cur_byte) +{ + app_usbd_audio_t const * p_audio = audio_get(p_inst); + + return p_audio->specific.inst.p_format_dsc->p_data[cur_byte]; +} + + +static size_t audio_get_input_descriptor_size(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_audio_t const * p_audio = audio_get(p_inst); + + return p_audio->specific.inst.p_input_dsc->size; +} + + +static size_t audio_get_input_descriptor_data(app_usbd_class_inst_t const * p_inst, + uint32_t cur_byte) +{ + app_usbd_audio_t const * p_audio = audio_get(p_inst); + + return p_audio->specific.inst.p_input_dsc->p_data[cur_byte]; +} + + +static size_t audio_get_output_descriptor_size(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_audio_t const * p_audio = audio_get(p_inst); + + return p_audio->specific.inst.p_output_dsc->size; +} + + +static size_t audio_get_output_descriptor_data(app_usbd_class_inst_t const * p_inst, + uint32_t cur_byte) +{ + app_usbd_audio_t const * p_audio = audio_get(p_inst); + + return p_audio->specific.inst.p_output_dsc->p_data[cur_byte]; +} + + +static size_t audio_get_feature_descriptor_size(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_audio_t const * p_audio = audio_get(p_inst); + + return p_audio->specific.inst.p_feature_dsc->size; +} + + +static size_t audio_get_feature_descriptor_data(app_usbd_class_inst_t const * p_inst, + uint32_t cur_byte) +{ + app_usbd_audio_t const * p_audio = audio_get(p_inst); + + return p_audio->specific.inst.p_feature_dsc->p_data[cur_byte]; +} + + +static uint8_t audio_get_control_interface_number(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_class_iface_conf_t const * ifce = 0; + + ifce = app_usbd_class_iface_get(p_inst, 0); + return app_usbd_class_iface_number_get(ifce); +} + + +/** + * @brief @ref app_usbd_class_methods_t::feed_descriptors + */ + +static bool audio_feed_descriptors(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size) +{ + static uint8_t ifaces = 0; + ifaces = app_usbd_class_iface_count_get(p_inst); + ASSERT(ifaces == 2); + app_usbd_audio_t const * p_audio = audio_get(p_inst); + + APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size); + + /* CONTROL INTERFACE DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE); // bDescriptorType = Interface + + static app_usbd_class_iface_conf_t const * p_cur_iface = NULL; + p_cur_iface = app_usbd_class_iface_get(p_inst, 0); + + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_number_get(p_cur_iface)); // bInterfaceNumber + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bAlternateSetting + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_ep_count_get(p_cur_iface)); // bNumEndpoints + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_CLASS); // bInterfaceClass = Audio + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_SUBCLASS_AUDIOCONTROL); // bInterfaceSubclass (Audio Control) + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_CLASS_PROTOCOL_UNDEFINED); // bInterfaceProtocol + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iInterface + + /* HEADER INTERFACE */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_DESCRIPTOR_INTERFACE); // bDescriptorType = Audio Interfaces + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_AC_IFACE_SUBTYPE_HEADER); // bDescriptorSubtype = Header + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(0x0100)); // bcdADC LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(0x0100)); // bcdADC MSB + + static uint16_t header_desc_len = 0; + header_desc_len = 9 + audio_get_feature_descriptor_size(p_inst) + + audio_get_input_descriptor_size(p_inst) + audio_get_output_descriptor_size( + p_inst); + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(header_desc_len)); // wTotalLength LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(header_desc_len)); // wTotalLength MSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x01); // bInCollection + APP_USBD_CLASS_DESCRIPTOR_WRITE(audio_get_control_interface_number(p_inst) + 1); // baInterfaceNr(1) + + /* INPUT TERMINAL DESCRIPTOR */ + static uint32_t cur_byte = 0; + static size_t input_desc_size = 0; + input_desc_size = audio_get_input_descriptor_size(p_inst); + + for (cur_byte = 0; cur_byte < input_desc_size; cur_byte++) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(audio_get_input_descriptor_data(p_inst, cur_byte)); + } + + /* FEATURE UNIT DESCRIPTOR */ + static size_t feature_desc_size = 0; + feature_desc_size = audio_get_feature_descriptor_size(p_inst); + + for (cur_byte = 0; cur_byte < feature_desc_size; cur_byte++) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(audio_get_feature_descriptor_data(p_inst, cur_byte)); + } + + /* OUTPUT TERMINAL DESCRIPTOR */ + static size_t output_desc_size = 0; + output_desc_size = audio_get_output_descriptor_size(p_inst); + + for (cur_byte = 0; cur_byte < output_desc_size; cur_byte++) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(audio_get_output_descriptor_data(p_inst, cur_byte)); + } + + p_cur_iface++; + + /* STREAM INTERFACE DESCRIPTOR ALT 0 */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE); // bDescriptorType = Interface + APP_USBD_CLASS_DESCRIPTOR_WRITE(audio_get_control_interface_number(p_inst) + 1); // bInterfaceNumber + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bAlternateSetting + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bNumEndpoints + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_CLASS); // bInterfaceClass = Audio + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_audio->specific.inst.type_streaming); // bInterfaceSubclass (Audio Control) + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_CLASS_PROTOCOL_UNDEFINED); // bInterfaceProtocol + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iInterface + + /* STREAM INTERFACE DESCRIPTOR ALT 1 */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE); // bDescriptorType = Interface + APP_USBD_CLASS_DESCRIPTOR_WRITE(audio_get_control_interface_number(p_inst) + 1); // bInterfaceNumber + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x01); // bAlternateSetting + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_ep_count_get(p_cur_iface)); // bNumEndpoints + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_CLASS); // bInterfaceClass = Audio + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_audio->specific.inst.type_streaming); // bInterfaceSubclass (Audio Control) + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_CLASS_PROTOCOL_UNDEFINED); // bInterfaceProtocol + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iInterface + + /* AudioStreaming GENERAL DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x07); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_DESCRIPTOR_INTERFACE); // bDescriptorType = Audio Interface + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_AS_IFACE_SUBTYPE_GENERAL); // bDescriptorSubtype = General + APP_USBD_CLASS_DESCRIPTOR_WRITE(audio_get_control_interface_number(p_inst) + 1); // bTerminalLink + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_audio->specific.inst.delay); // bDelay + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(p_audio->specific.inst.format)); // wFormatTag LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(p_audio->specific.inst.format)); // wFormatTag MSB + + /* FORMAT DESCRIPTOR */ + static size_t format_desc_size = 0; + format_desc_size = audio_get_format_descriptor_size(p_inst); + + for (cur_byte = 0; cur_byte < format_desc_size; cur_byte++) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(audio_get_format_descriptor_data(p_inst, cur_byte)); + } + + /* ENDPOINT GENERAL DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x07); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_DESCRIPTOR_ENDPOINT); // bDescriptorType = Audio Descriptor + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_AUDIO_EP_SUBTYPE_GENERAL); // bDescriptorSubtype = EP General + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bmAttributes + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bLockDelayUnits + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(0x0000)); // wLockDelay LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(0x0000)); // wLockDelay MSB + + /* ENDPOINT ISO DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_ENDPOINT); // bDescriptorType = Endpoint + + static app_usbd_class_ep_conf_t const * p_cur_ep = NULL; + p_cur_ep = app_usbd_class_iface_ep_get(p_cur_iface, 0); + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_ep_address_get(p_cur_ep)); // bEndpointAddress + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_ISOCHRONOUS); // bmAttributes + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(p_audio->specific.inst.ep_size)); // wMaxPacketSize LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(p_audio->specific.inst.ep_size)); // wMaxPacketSize MSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x01); // bInterval + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bRefresh + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bSynchAddress + + APP_USBD_CLASS_DESCRIPTOR_END(); +} + +/** @} */ + +const app_usbd_class_methods_t app_usbd_audio_class_methods = { + .event_handler = audio_event_handler, + .feed_descriptors = audio_feed_descriptors, + .iface_select = iface_select, + .iface_deselect = iface_deselect, + .iface_selection_get = iface_selection_get, +}; + + +size_t app_usbd_audio_class_rx_size_get(app_usbd_class_inst_t const * p_inst) +{ + nrf_drv_usbd_ep_t ep_addr; + + ep_addr = ep_iso_addr_get(p_inst); + ASSERT(NRF_USBD_EPISO_CHECK(ep_addr)); + + return (size_t)nrf_drv_usbd_epout_size_get(ep_addr); +} + + +ret_code_t app_usbd_audio_class_rx_start( + app_usbd_class_inst_t const * p_inst, + void * p_buff, + size_t size) +{ + nrf_drv_usbd_ep_t ep_addr; + + ep_addr = ep_iso_addr_get(p_inst); + ASSERT(NRF_USBD_EPISO_CHECK(ep_addr)); + + NRF_DRV_USBD_TRANSFER_OUT(transfer, p_buff, size); + return app_usbd_ep_transfer(ep_addr, &transfer); +} + + +ret_code_t app_usbd_audio_class_tx_start( + app_usbd_class_inst_t const * p_inst, + const void * p_buff, + size_t size) +{ + nrf_drv_usbd_ep_t ep_addr; + + ep_addr = ep_iso_addr_get(p_inst); + ASSERT(NRF_USBD_EPISO_CHECK(ep_addr)); + + NRF_DRV_USBD_TRANSFER_IN(transfer, p_buff, size); + return app_usbd_ep_transfer(ep_addr, &transfer); +} + + +#endif //NRF_MODULE_ENABLED(APP_USBD_AUDIO) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio.h new file mode 100644 index 0000000..f094bbb --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio.h @@ -0,0 +1,329 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_AUDIO_H__ +#define APP_USBD_AUDIO_H__ + +#include <stdint.h> +#include <stdbool.h> + +#include "nrf_drv_usbd.h" +#include "app_usbd.h" +#include "app_usbd_core.h" +#include "app_usbd_class_base.h" +#include "app_usbd_descriptor.h" + +#include "app_usbd_audio_types.h" +#include "app_usbd_audio_desc.h" +#include "app_usbd_audio_internal.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_audio USB AUDIO class + * @ingroup app_usbd + * + * @brief @tagAPI52840 Module with types, definitions, and API used by USB Audio class. + * + * @details Reference specifications: + * - "Universal Serial Bus Device Class Definition for Audio Devices" + * Release 1.0, March 18, 1998. + * - "Universal Serial Bus Device Class Definition for Audio Data Formats" + * Release 1.0, March 18, 1998. + * - "Universal Serial Bus Device Class Definition for Terminal Types" + * Release 1.0, March 18, 1998. + * + * @{ + */ + + +#ifdef DOXYGEN +/** + * @brief Audio class instance type + * + * @ref APP_USBD_CLASS_TYPEDEF + */ +typedef struct { } app_usbd_audio_t; +#else +/*lint -save -e10 -e26 -e123 -e505 */ +APP_USBD_CLASS_TYPEDEF(app_usbd_audio, \ + APP_USBD_AUDIO_CONFIG(0, 1), \ + APP_USBD_AUDIO_INSTANCE_SPECIFIC_DEC, \ + APP_USBD_AUDIO_DATA_SPECIFIC_DEC \ +); +/*lint -restore*/ +#endif + + +/*lint -save -e407 */ + +/** + * @brief Events passed to user event handler + * + * @note Example prototype of user event handler: + * + * @code + void audio_user_ev_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_audio_user_event_t event); + * @endcode + */ +typedef enum app_usbd_audio_user_event_e { + APP_USBD_AUDIO_USER_EVT_CLASS_REQ, /**< User event CLASS_REQ */ + APP_USBD_AUDIO_USER_EVT_RX_DONE, /**< User event RX_DONE */ + APP_USBD_AUDIO_USER_EVT_TX_DONE, /**< User event TX_DONE */ +} app_usbd_audio_user_event_t; + +/*lint -restore*/ + +/** + * @brief Global definition of app_usbd_audio_t class instance + * + * @param instance_name Name of global instance + * @param interfaces_configs Interfaces configurations + * @param user_ev_handler User event handler + * @param format_descriptor Audio class Format descriptor + * @param input_descriptor Audio class Input Terminal descriptor + * @param output_descriptor Audio class Output Terminal descriptor + * @param feature_descriptor Audio class Feature Unit descriptor + * @param delay Streaming delay + * @param format FormatTag (@ref app_usbd_audio_as_iface_format_tag_t) + * @param ep_size Endpoint size + * @param type_str Streaming type MIDISTREAMING/AUDIOSTREAMING + * + * @note This macro is just simplified version of @ref APP_USBD_AUDIO_GLOBAL_DEF_INTERNAL + * + */ +#define APP_USBD_AUDIO_GLOBAL_DEF(instance_name, \ + interfaces_configs, \ + user_ev_handler, \ + format_descriptor, \ + input_descriptor, \ + output_descriptor, \ + feature_descriptor, \ + delay, \ + format, \ + ep_size, \ + type_str) \ + APP_USBD_AUDIO_GLOBAL_DEF_INTERNAL(instance_name, \ + interfaces_configs, \ + user_ev_handler, \ + format_descriptor, \ + input_descriptor, \ + output_descriptor, \ + feature_descriptor, \ + delay, \ + format, \ + ep_size, \ + type_str) + + +/** + * @brief Initializer of Audio Format descriptor + * + * @param name Format descriptor name + * @param ... Format descriptor data +*/ + +#define APP_USBD_AUDIO_FORMAT_DESCRIPTOR(name, ...) \ + static uint8_t const CONCAT_2(name, _data)[] = \ + { \ + __VA_ARGS__ \ + }; \ + static const app_usbd_audio_subclass_desc_t name = \ + { \ + sizeof(CONCAT_2(name, _data)), \ + APP_USBD_AUDIO_AS_IFACE_SUBTYPE_FORMAT_TYPE, \ + CONCAT_2(name,_data) \ + } + +/** + * @brief Initializer of Audio Input descriptor + * + * @param name Input descriptor name + * @param ... Input descriptor data +*/ + +#define APP_USBD_AUDIO_INPUT_DESCRIPTOR(name, ...) \ + static uint8_t const CONCAT_2(name, _data)[] = \ + { \ + __VA_ARGS__ \ + }; \ + static const app_usbd_audio_subclass_desc_t name = \ + { \ + sizeof(CONCAT_2(name, _data)), \ + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_INPUT_TERMINAL, \ + CONCAT_2(name,_data) \ + } + +/** + * @brief Initializer of Audio Output descriptor + * + * @param name Output descriptor name + * @param ... Output descriptor data +*/ + +#define APP_USBD_AUDIO_OUTPUT_DESCRIPTOR(name, ...) \ + static uint8_t const CONCAT_2(name, _data)[] = \ + { \ + __VA_ARGS__ \ + }; \ + static const app_usbd_audio_subclass_desc_t name = \ + { \ + sizeof(CONCAT_2(name, _data)), \ + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_OUTPUT_TERNINAL, \ + CONCAT_2(name,_data) \ + } + +/** + * @brief Initializer of Feture Output descriptor + * + * @param name Feture descriptor name + * @param ... Feture descriptor data +*/ + +#define APP_USBD_AUDIO_FEATURE_DESCRIPTOR(name, ...) \ + static uint8_t const CONCAT_2(name, _data)[] = \ + { \ + __VA_ARGS__ \ + }; \ + static const app_usbd_audio_subclass_desc_t name = \ + { \ + sizeof(CONCAT_2(name, _data)), \ + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_FEATURE_UNIT, \ + CONCAT_2(name,_data) \ + } + +/** + * @@brief Helper function to get class instance from Audio class + * + * @param[in] p_audio Audio class instance (declared by @ref APP_USBD_AUDIO_GLOBAL_DEF) + * @return Base class instance + */ +static inline app_usbd_class_inst_t const * +app_usbd_audio_class_inst_get(app_usbd_audio_t const * p_audio) +{ + return &p_audio->base; +} + +/** + * @brief Helper function to get audio specific request from audio class + * + * @param[in] p_audio Audio class instance (declared by @ref APP_USBD_AUDIO_GLOBAL_DEF) + * @return Audio class specific request + */ +static inline app_usbd_audio_req_t * +app_usbd_audio_class_request_get(app_usbd_audio_t const * p_audio) +{ + return &p_audio->specific.p_data->ctx.request; +} + +/** + * @brief Helper function to get audio from base class instance + * + * @param[in] p_inst Base class instance + * @return Audio class handle + */ +static inline app_usbd_audio_t const * +app_usbd_audio_class_get(app_usbd_class_inst_t const * p_inst) +{ + return (app_usbd_audio_t const *)p_inst; +} + +/** + * @brief Get the size of last received transfer. + * + * @note Call this function in reaction to a SOF event to check if there is any data to process. + * + * @param p_inst Base class instance. + * + * @return Number of bytes received in the last transmission. + */ +size_t app_usbd_audio_class_rx_size_get(app_usbd_class_inst_t const * p_inst); + +/** + * @brief Start audio data copying from the endpoint buffer. + * + * Function to be used to copy data from an audio OUT endpoint to a given buffer. + * When it finishes, an @ref APP_USBD_AUDIO_USER_EVT_RX_DONE event is generated. + * + * @param p_inst Base class instance. + * @param p_buff Target buffer. + * @param size Size of the requested data. + * + * @return Result of the endpoint transmission start. + * + * @sa app_usbd_audio_class_rx_size_get + * + * @note This function should be called in reaction to a SOF event. + * Isochronous endpoints are double buffered and they are automatically switched at every SOF. + */ +ret_code_t app_usbd_audio_class_rx_start( + app_usbd_class_inst_t const * p_inst, + void * p_buff, + size_t size); + +/** + * @brief Start copying audio data to the endpoint buffer. + * + * Function to be used to copy data to an audio IN endpoint from a given buffer. + * When it finishes, an @ref APP_USBD_AUDIO_USER_EVT_TX_DONE event is generated. + * + * @param p_inst Base class instance. + * @param p_buff Source buffer. + * @param size Size of the data to be sent. + * + * @return Result of the endpoint transsmision start. + * + * @note This function should be called in reaction to a SOF event. + * Isochronous endpoints are double buffered and they are automatically switched at every SOF. + */ +ret_code_t app_usbd_audio_class_tx_start( + app_usbd_class_inst_t const * p_inst, + const void * p_buff, + size_t size); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_AUDIO_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio_desc.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio_desc.h new file mode 100644 index 0000000..dbc4d41 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio_desc.h @@ -0,0 +1,318 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_AUDIO_DESC_H__ +#define APP_USBD_AUDIO_DESC_H__ + +#include "app_usbd_descriptor.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_audio_dsc USB Audio descriptors + * @brief @tagAPI52840 Descriptors used in the USB Audio class. + * @ingroup app_usbd_audio + * @{ + */ + +/** + * @brief Initializer of interface descriptor for AUDIO class + * + * @param interface_number Interface number + * @param alt_setting Interface alternate setting + * @param ep_num Number of endpoints + * @param subclass Audio subclass @ref app_usbd_audio_subclass_t + * */ +#define APP_USBD_AUDIO_INTERFACE_DSC(interface_number, alt_setting, ep_num, subclass) \ + /*.bLength = */ sizeof(app_usbd_descriptor_iface_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_INTERFACE, \ + /*.bInterfaceNumber = */ (interface_number), \ + /*.bAlternateSetting = */ (alt_setting), \ + /*.bNumEndpoints = */ (ep_num), \ + /*.bInterfaceClass = */ APP_USBD_AUDIO_CLASS, \ + /*.bInterfaceSubClass = */ (subclass), \ + /*.bInterfaceProtocol = */ APP_USBD_AUDIO_CLASS_PROTOCOL_UNDEFINED, \ + /*.iInterface = 0, */ 0x00, \ + +/** + * @brief Initializer of isochronous endpoint descriptors for audio class + * + * @param ep ISO endpoint id: @ref NRF_DRV_USBD_EPIN8, @ref NRF_DRV_USBD_EPOUT8 + * @param ep_size Endpoint size (bytes) + * @param interval Endpoint interval (milliseconds) + * @param refresh Refresh value (usually 0) + * @param synch_addr Synch address (usually 0) + * */ +#define APP_USBD_AUDIO_ISO_EP_DSC(ep, ep_size, interval, refresh, synch_addr) \ + /*.bLength = */ sizeof(app_usbd_descriptor_ep_t) + 2, \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_ENDPOINT, \ + /*.bEndpointAddress = */ ep, \ + /*.bmAttributes = */ APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_ISOCHRONOUS, \ + /*.wMaxPacketSize = */ APP_USBD_U16_TO_RAW_DSC(ep_size), \ + /*.bInterval = */ (interval), \ + /*.bRefresh = */ (refresh), \ + /*.bInterval = */ (synch_addr), \ + +/** + * @brief Simplified version of @ref APP_USBD_AUDIO_ISO_EP_DSC for ISO IN endpoint + */ +#define APP_USBD_AUDIO_ISO_EP_IN_DSC(ep_size) \ + APP_USBD_AUDIO_ISO_EP_DSC(NRF_DRV_USBD_EPIN8, ep_size, 1, 0, 0) + +/** + * @brief Simplified version of @ref APP_USBD_AUDIO_ISO_EP_DSC for ISO OUT endpoint + */ +#define APP_USBD_AUDIO_ISO_EP_OUT_DSC(ep_size) \ + APP_USBD_AUDIO_ISO_EP_DSC(NRF_DRV_USBD_EPOUT8, ep_size, 1, 0, 0) + +/** + * @brief Initializer of @ref app_usbd_audio_ac_iface_header_desc_t + * + * @param descriptor_list Descriptor list following audio interface header descriptor + * @param ... List of interfaces following audio control interface + * */ +#define APP_USBD_AUDIO_AC_IFACE_HEADER_DSC(descriptor_list, ...) \ + /*.bLength = */ sizeof(app_usbd_audio_ac_iface_header_desc_t) + \ + (NUM_VA_ARGS(__VA_ARGS__)), \ + /*.bDescriptorType = */ APP_USBD_AUDIO_DESCRIPTOR_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_AUDIO_AC_IFACE_SUBTYPE_HEADER, \ + /*.bcdADC = */ APP_USBD_U16_TO_RAW_DSC(0x0100), \ + /*.wTotalLength = */ APP_USBD_U16_TO_RAW_DSC( \ + sizeof((uint8_t[]){BRACKET_EXTRACT(descriptor_list)}) + \ + sizeof(app_usbd_audio_ac_iface_header_desc_t) + \ + (NUM_VA_ARGS(__VA_ARGS__))), \ + /*.bInCollection = */ (NUM_VA_ARGS(__VA_ARGS__)), \ + /*.baInterfaceNr[] = */ __VA_ARGS__, \ + + +/** + * @brief Initializer of @ref app_usbd_audio_input_terminal_desc_t + * + * @param terminal_id Terminal ID + * @param terminal_type Terminal type @ref app_usbd_audio_terminal_type_t + * @param nr_channels Number of channels + * @param ch_config CHannel config bitmask + * */ +#define APP_USBD_AUDIO_INPUT_TERMINAL_DSC(terminal_id, terminal_type, nr_channels, ch_config) \ + /*.bLength = */ sizeof(app_usbd_audio_input_terminal_desc_t), \ + /*.bDescriptorType = */ APP_USBD_AUDIO_DESCRIPTOR_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_AUDIO_AC_IFACE_SUBTYPE_INPUT_TERMINAL, \ + /*.bTerminalID = */ (terminal_id), \ + /*.wTerminalType = */ APP_USBD_U16_TO_RAW_DSC(terminal_type), \ + /*.bAssocTerminal = */ 0, \ + /*.bNrChannels = */ (nr_channels), \ + /*.wChannelConfig = */ APP_USBD_U16_TO_RAW_DSC(ch_config), \ + /*.iChannelNames = */ 0, \ + /*.iTerminal = */ 0, \ + + +/** + * @brief Initializer of @ref app_usbd_audio_output_terminal_desc_t + * + * @param terminal_id Terminal ID + * @param terminal_type Terminal type @ref app_usbd_audio_terminal_type_t + * @param source_id Source ID + * */ +#define APP_USBD_AUDIO_OUTPUT_TERMINAL_DSC(terminal_id, terminal_type, source_id) \ + /*.bLength = */ sizeof(app_usbd_audio_output_terminal_desc_t), \ + /*.bDescriptorType = */ APP_USBD_AUDIO_DESCRIPTOR_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_AUDIO_AC_IFACE_SUBTYPE_OUTPUT_TERNINAL, \ + /*.bTerminalID = */ (terminal_id), \ + /*.wTerminalType = */ APP_USBD_U16_TO_RAW_DSC(terminal_type), \ + /*.bAssocTerminal = */ 0, \ + /*.bSourceID = */ (source_id), \ + /*.iTerminal = */ 0, \ + + +/** + * @brief Initializer of @ref app_usbd_audio_feature_unit_desc_t + * + * @param unit_id Unit ID + * @param source_id Source ID + * @param ... List of controls + * */ +#define APP_USBD_AUDIO_FEATURE_UNIT_DSC(unit_id, source_id, ...) \ + /*.bLength = */ sizeof(app_usbd_audio_feature_unit_desc_t) + \ + 1 + (NUM_VA_ARGS(__VA_ARGS__)), \ + /*.bDescriptorType = */ APP_USBD_AUDIO_DESCRIPTOR_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_AUDIO_AC_IFACE_SUBTYPE_FEATURE_UNIT, \ + /*.bUnitID = */ (unit_id), \ + /*.bSourceID = */ (source_id), \ + /*.bControlSize = */ sizeof(uint16_t), \ + /*.bmaControls[] = */ __VA_ARGS__, \ + /*.iFeature = */ 0, \ + + +/** + * @brief Initializer of @ref app_usbd_audio_as_iface_desc_t + * + * @param terminal_link Terminal link + * @param delay Delay + * @param format_tag Format TAG + * */ +#define APP_USBD_AUDIO_AS_IFACE_DSC(terminal_link, delay, format_tag) \ + /*.bLength = */ sizeof(app_usbd_audio_as_iface_desc_t), \ + /*.bDescriptorType = */ APP_USBD_AUDIO_DESCRIPTOR_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_AUDIO_AS_IFACE_SUBTYPE_GENERAL, \ + /*.bTerminalLink = */ (terminal_link), \ + /*.bDelay = */ (delay), \ + /*.wFormatTag = */ APP_USBD_U16_TO_RAW_DSC(format_tag), \ + +/** + * @brief Initializer of @ref app_usbd_audio_as_format_type_one_desc_t + * + * @param nr_channels Number of channels + * @param subframe_size Subframe size + * @param resolution Bit resolution + * @param freq_type Frequency type + * @param ... List of frequencies + * */ +#define APP_USBD_AUDIO_AS_FORMAT_I_DSC(nr_channels, subframe_size, resolution, freq_type, ...) \ + /*.bLength = */ sizeof(app_usbd_audio_as_format_type_one_desc_t) + \ + (NUM_VA_ARGS(__VA_ARGS__)), \ + /*.bDescriptorType = */ APP_USBD_AUDIO_DESCRIPTOR_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_AUDIO_AS_IFACE_SUBTYPE_FORMAT_TYPE, \ + /*.bFormatType = */ (1), \ + /*.bNrChannels = */ (nr_channels), \ + /*.bSubframeSize = */ (subframe_size), \ + /*.bBitResolution = */ (resolution), \ + /*.bSamFreqType = */ (freq_type), \ + /*.tSamFreq = */ __VA_ARGS__, \ + +/** + * @brief Initializer of @ref app_usbd_audio_as_format_type_two_desc_t + * + * @param max_bitrate Maximum bitrate + * @param samples_per_frame Samples per frame + * @param freq_type Frequency type + * @param ... List of frequencies + * */ +#define APP_USBD_AUDIO_AS_FORMAT_II_DSC(max_bitrate, samples_per_frame, freq_type, ...) \ + /*.bLength = */ sizeof(app_usbd_audio_as_format_type_two_desc_t) + \ + (NUM_VA_ARGS(__VA_ARGS__)), \ + /*.bDescriptorType = */ APP_USBD_AUDIO_DESCRIPTOR_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_AUDIO_AS_IFACE_SUBTYPE_FORMAT_TYPE, \ + /*.bFormatType = */ (2), \ + /*.wMaxBitRate = */ APP_USBD_U16_TO_RAW_DSC(max_bitrate), \ + /*.wSamplesPerFrame = */ APP_USBD_U16_TO_RAW_DSC(samples_per_frame), \ + /*.bSamFreqType = */ (freq_type), \ + /*.tSamFreq = */ __VA_ARGS__, \ + +/** +* @brief Initializer of @ref app_usbd_audio_as_format_type_three_desc_t + * + * @param nr_channels Number of channels + * @param subframe_size Subframe size + * @param resolution Bit resolution + * @param freq_type Frequency type + * @param ... List of frequencies + * */ +#define APP_USBD_AUDIO_AS_FORMAT_III_DSC(nr_channels, subframe_size, resolution, freq_type, ...) \ + /*.bLength = */ sizeof(app_usbd_audio_as_format_type_three_desc_t) + \ + (NUM_VA_ARGS(__VA_ARGS__)), \ + /*.bDescriptorType = */ APP_USBD_AUDIO_DESCRIPTOR_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_AUDIO_AS_IFACE_SUBTYPE_FORMAT_TYPE, \ + /*.bFormatType = */ (3), \ + /*.bNrChannels = */ (nr_channels), \ + /*.bSubframeSize = */ (subframe_size), \ + /*.bBitResolution = */ (resolution), \ + /*.bSamFreqType = */ (freq_type), \ + /*.tSamFreq = */ __VA_ARGS__, \ + + +/** + * @brief Initializer of @ref app_usbd_audio_as_endpoint_desc_t + * + * @param attributes Endpoint attributes + * @param lock_delay_units Lock delay units + * @param lock_delay Lock delay + * */ +#define APP_USBD_AUDIO_EP_GENERAL_DSC(attributes, lock_delay_units, lock_delay) \ + /*.bLength = */ sizeof(app_usbd_audio_as_endpoint_desc_t), \ + /*.bDescriptorType = */ APP_USBD_AUDIO_DESCRIPTOR_ENDPOINT, \ + /*.bDescriptorSubtype = */ APP_USBD_AUDIO_EP_SUBTYPE_GENERAL, \ + /*.bmAttributes = */ (attributes), \ + /*.bLockDelayUnits = */ (lock_delay_units), \ + /*.wLockDelay = */ APP_USBD_U16_TO_RAW_DSC(lock_delay), \ + +/** + * @brief Macro to configure Audio Class control descriptor + * + * @param interface_number Interface number + * @param descriptor_list List of descriptors after Audio interface header descriptor + * @param interface_list List of interfaces passed to @ref APP_USBD_AUDIO_AC_IFACE_HEADER_DSC + * */ +#define APP_USBD_AUDIO_CONTROL_DSC(interface_number, descriptor_list, interface_list) \ + APP_USBD_AUDIO_INTERFACE_DSC(interface_number, 0, 0, APP_USBD_AUDIO_SUBCLASS_AUDIOCONTROL) \ + APP_USBD_AUDIO_AC_IFACE_HEADER_DSC(descriptor_list, BRACKET_EXTRACT(interface_list)) \ + BRACKET_EXTRACT(descriptor_list) + + +/** + * @brief Macro to configure Audio Class streaming descriptor + * + * @param interface_number Interface number + * @param alt_setting Alternate interface setting + * @param ep_num Number of endpoints + */ +#define APP_USBD_AUDIO_STREAMING_DSC(interface_number, alt_setting, ep_num) \ + APP_USBD_AUDIO_INTERFACE_DSC(interface_number, alt_setting, ep_num, \ + APP_USBD_AUDIO_SUBCLASS_AUDIOSTREAMING) + +/** + * @brief Macro to configure Audio Class MIDI streaming descriptor + * + * @param interface_number Interface number + * @param alt_setting Alternate interface setting + * @param ep_num Number of endpoints + */ +#define APP_USBD_AUDIO_MIDI_STREAMING_DSC(interface_number, alt_setting, ep_num) \ + APP_USBD_AUDIO_INTERFACE_DSC(interface_number, alt_setting, ep_num, \ + APP_USBD_AUDIO_SUBCLASS_MIDISTREAMING) + + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_AUDIO_DESC_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio_internal.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio_internal.h new file mode 100644 index 0000000..c3371b2 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio_internal.h @@ -0,0 +1,284 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_AUDIO_INTERNAL_H__ +#define APP_USBD_AUDIO_INTERNAL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_audio_internal USB Audio internals + * @brief @tagAPI52840 USB Audio class internals. + * @ingroup app_usbd_audio + * @{ + */ + +/** + * @brief Forward declaration of type defined by @ref APP_USBD_CLASS_TYPEDEF in audio class + * + */ +APP_USBD_CLASS_FORWARD(app_usbd_audio); + +/*lint -save -e165*/ +/** + * @brief Forward declaration of @ref app_usbd_audio_user_event_e + * + */ +enum app_usbd_audio_user_event_e; + +/*lint -restore*/ + +/** + * @brief User event handler + * + * @param[in] p_inst Class instance + * @param[in] event User event + * + */ +typedef void (*app_usbd_audio_user_ev_handler_t)(app_usbd_class_inst_t const * p_inst, + enum app_usbd_audio_user_event_e event); + + +/** +* @brief Audio subclass descriptor. +*/ + +typedef struct { + size_t size; + uint8_t type; + uint8_t const * const p_data; +} app_usbd_audio_subclass_desc_t; + +/** + * @brief Audio class part of class instance data + */ +typedef struct { + + app_usbd_audio_subclass_desc_t const * const p_format_dsc; //!< Audio class Format descriptor + app_usbd_audio_subclass_desc_t const * const p_input_dsc; //!< Audio class Input Terminal descriptor + app_usbd_audio_subclass_desc_t const * const p_output_dsc; //!< Audio class Output Terminal descriptor + app_usbd_audio_subclass_desc_t const * const p_feature_dsc; //!< Audio class Feature Unit descriptor + + uint8_t delay; //!< Streaming delay + uint16_t format; //!< FormatTag (@ref app_usbd_audio_as_iface_format_tag_t) + uint16_t ep_size; //!< Endpoint size + + app_usbd_audio_subclass_t type_streaming; //Streaming type MIDISTREAMING/AUDIOSTREAMING (@ref app_usbd_audio_subclass_t) + + app_usbd_audio_user_ev_handler_t user_ev_handler; //!< User event handler +} app_usbd_audio_inst_t; + +/** + * @brief Audio class request target + */ +typedef enum { + APP_USBD_AUDIO_CLASS_REQ_IN, /**< Audio class request IN */ + APP_USBD_AUDIO_CLASS_REQ_OUT, /**< Audio class request OUT */ + APP_USBD_AUDIO_EP_REQ_IN, /**< Audio class endpoint request IN */ + APP_USBD_AUDIO_EP_REQ_OUT, /**< Audio class endpoint request OUT */ +} app_usbd_audio_class_req_target_t; + +/** + * @brief Audio class specific request handled via control endpoint + */ +typedef struct { + app_usbd_audio_class_req_target_t req_target; //!< Request target + app_usbd_audio_req_type_t req_type; //!< Request type + + uint8_t control; //!< Request control field + uint8_t channel; //!< Channel ID + uint8_t interface; //!< Interface ID + uint8_t entity; //!< Entity ID + uint16_t length; //!< Request payload length + + uint8_t payload[64]; //!< Request payload +} app_usbd_audio_req_t; + + +/** + * @brief Audio class context + * + */ +typedef struct { + app_usbd_audio_req_t request; //!< Audio class request + bool streaming; //!< Streaming flag +} app_usbd_audio_ctx_t; + + +/** + * @brief Audio class configuration macro + * + * Used by @ref APP_USBD_AUDIO_GLOBAL_DEF + * + * @param iface_control Interface number of audio control + * @param iface_stream Interface number of audio stream + */ +#define APP_USBD_AUDIO_CONFIG(iface_control, iface_stream) \ + ((iface_control), \ + (iface_stream, 0)) + +/** + * @brief Only IN audio stream configuration + * + * @param iface_control Interface number of audio control + * @param iface_stream_in Interface number of audio stream on IN endpoint + */ +#define APP_USBD_AUDIO_CONFIG_IN(iface_control, iface_stream_in) \ + ((iface_control), (iface_stream_in, NRF_DRV_USBD_EPIN8)) + + +/** + * @brief Only OUT audio stream configuration + * + * @param iface_control Interface number of audio control + * @param iface_stream_out Interface number of audio stream on OUT endpoint + */ +#define APP_USBD_AUDIO_CONFIG_OUT(iface_control, iface_stream_out) \ + ((iface_control), (iface_stream_out, NRF_DRV_USBD_EPOUT8)) + +/** + * @brief Specific class constant data for audio class + * + * @ref app_usbd_audio_inst_t + */ +#define APP_USBD_AUDIO_INSTANCE_SPECIFIC_DEC app_usbd_audio_inst_t inst; + +/** + * @brief Configures audio class instance + * + * @param user_event_handler User event handler + * @param format_descriptor Audio class Format descriptor + * @param input_descriptor Audio class Input Terminal descriptor + * @param output_descriptor Audio class Output Terminal descriptor + * @param feature_descriptor Audio class Feature Unit descriptor + * @param dlay Streaming delay + * @param frmat FormatTag (@ref app_usbd_audio_as_iface_format_tag_t) + * @param ep_siz Endpoint size + * @param type_str Streaming type MIDISTREAMING/AUDIOSTREAMING + */ + #define APP_USBD_AUDIO_INST_CONFIG(user_event_handler, \ + format_descriptor, \ + input_descriptor, \ + output_descriptor, \ + feature_descriptor, \ + dlay, \ + frmat, \ + ep_siz, \ + type_str) \ + .inst = { \ + .user_ev_handler = user_event_handler, \ + .p_format_dsc = format_descriptor, \ + .p_input_dsc = input_descriptor, \ + .p_output_dsc = output_descriptor, \ + .p_feature_dsc = feature_descriptor, \ + .delay = dlay, \ + .format = frmat, \ + .ep_size = ep_siz, \ + .type_streaming = type_str \ + } + +/** + * @brief Specific class data for audio class + * + * @ref app_usbd_audio_ctx_t + */ +#define APP_USBD_AUDIO_DATA_SPECIFIC_DEC app_usbd_audio_ctx_t ctx; + + +/** + * @brief Audio class descriptors config macro + * + * @param interface_number Interface number + * @param ... Extracted endpoint list + */ +#define APP_USBD_AUDIO_DSC_CONFIG(interface_number, ...) { \ + APP_USBD_AUDIO_INTERFACE_DSC(interface_number, \ + 0, \ + 0, \ + APP_USBD_AUDIO_SUBCLASS_AUDIOCONTROL) \ + } + +/** + * @brief Public audio class interface + * + */ +extern const app_usbd_class_methods_t app_usbd_audio_class_methods; + +/** + * @brief Global definition of @ref app_usbd_audio_t class + * + */ + #define APP_USBD_AUDIO_GLOBAL_DEF_INTERNAL(instance_name, \ + interfaces_configs, \ + user_ev_handler, \ + format_descriptor, \ + input_descriptor, \ + output_descriptor, \ + feature_descriptor, \ + delay, \ + format, \ + ep_size, \ + type_str) \ + APP_USBD_CLASS_INST_GLOBAL_DEF( \ + instance_name, \ + app_usbd_audio, \ + &app_usbd_audio_class_methods, \ + interfaces_configs, \ + (APP_USBD_AUDIO_INST_CONFIG(user_ev_handler, \ + format_descriptor, \ + input_descriptor, \ + output_descriptor, \ + feature_descriptor, \ + delay, \ + format, \ + ep_size, \ + type_str)) \ + ) + + +/** @} */ + + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_AUDIO_INTERNAL_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio_types.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio_types.h new file mode 100644 index 0000000..57fd902 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/audio/app_usbd_audio_types.h @@ -0,0 +1,382 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_AUDIO_TYPES_H__ +#define APP_USBD_AUDIO_TYPES_H__ + +#include "app_util.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_audio_types USB Audio types + * @brief @tagAPI52840 Type definitions for the USB Audio class. + * @ingroup app_usbd_audio + * @{ + */ + +/** @brief Audio class definition in interface descriptor + * + * Fixed value, @ref app_usbd_descriptor_iface_t::bInterfaceClass + * */ +#define APP_USBD_AUDIO_CLASS 0x01 + +/** @brief Audio class protocol definition in interface descriptor + * + * Fixed value, @ref app_usbd_descriptor_iface_t::bInterfaceProtocol + * */ +#define APP_USBD_AUDIO_CLASS_PROTOCOL_UNDEFINED 0x00 + +/** + * @brief Audio subclass possible values + * + * @ref app_usbd_descriptor_iface_t::bInterfaceSubClass + */ +typedef enum { + APP_USBD_AUDIO_SUBCLASS_UNDEFINED = 0x00, /**< UNDEFINED subclass */ + APP_USBD_AUDIO_SUBCLASS_AUDIOCONTROL, /**< AUDIOCONTROL subclass */ + APP_USBD_AUDIO_SUBCLASS_AUDIOSTREAMING, /**< AUDIOSTREAMING subclass */ + APP_USBD_AUDIO_SUBCLASS_MIDISTREAMING /**< MIDISTREAMING subclass */ +} app_usbd_audio_subclass_t; + + +/** + * @brief Audio class specific descriptor types + */ +typedef enum { + APP_USBD_AUDIO_DESCRIPTOR_UNDEFINED = 0x20, /**< UNDEFINED descriptor type */ + APP_USBD_AUDIO_DESCRIPTOR_DEVICE = 0x21, /**< DEVICE descriptor type */ + APP_USBD_AUDIO_DESCRIPTOR_CONFIGURATION = 0x22, /**< CONFIGURATION descriptor type */ + APP_USBD_AUDIO_DESCRIPTOR_STRING = 0x23, /**< STRING descriptor type */ + APP_USBD_AUDIO_DESCRIPTOR_INTERFACE = 0x24, /**< INTERFACE descriptor type */ + APP_USBD_AUDIO_DESCRIPTOR_ENDPOINT = 0x25, /**< ENDPOINT descriptor type */ +} app_usbd_audio_descriptor_type_t; + + +/** + * @brief Audio control interface subtype + */ +typedef enum { + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_UNDEFINED = 0x00, /**< Audio control interface subtype UNDEFINED */ + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_HEADER, /**< Audio control interface subtype HEADER */ + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_INPUT_TERMINAL, /**< Audio control interface subtype INPUT_TERMINAL */ + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_OUTPUT_TERNINAL, /**< Audio control interface subtype OUTPUT_TERNINAL */ + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_MIXER_UNIT, /**< Audio control interface subtype MIXER_UNIT */ + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_SELECTOR_UNIT, /**< Audio control interface subtype SELECTOR_UNIT */ + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_FEATURE_UNIT, /**< Audio control interface subtype FEATURE_UNIT */ + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_PROCESSING_UNIT, /**< Audio control interface subtype PROCESSING_UNIT */ + APP_USBD_AUDIO_AC_IFACE_SUBTYPE_EXTENSION_UNIT, /**< Audio control interface subtype EXTENSION_UNIT */ +} app_usbd_audio_ac_iface_subtype_t; + +/** + * @brief Audio streaming interface subtype + */ +typedef enum { + APP_USBD_AUDIO_AS_IFACE_SUBTYPE_UNDEFINED = 0x00, /**< Audio streaming interface subtype UNDEFINED */ + APP_USBD_AUDIO_AS_IFACE_SUBTYPE_GENERAL, /**< Audio streaming interface subtype GENERAL */ + APP_USBD_AUDIO_AS_IFACE_SUBTYPE_FORMAT_TYPE, /**< Audio streaming interface subtype FORMAT_TYPE */ + APP_USBD_AUDIO_AS_IFACE_SUBTYPE_FORMAT_SPECIFIC, /**< Audio streaming interface subtype FORMAT_SPECIFIC*/ +} app_usbd_audio_as_iface_subtype_t; + + +/** + * @brief Audio class specific endpoint subtypes + */ +typedef enum { + APP_USBD_AUDIO_EP_SUBTYPE_UNDEFINED = 0x00, /**< APP_USBD_AUDIO_EP_SUBTYPE_UNDEFINED */ + APP_USBD_AUDIO_EP_SUBTYPE_GENERAL, /**< APP_USBD_AUDIO_EP_SUBTYPE_GENERAL */ +} app_usbd_audio_ep_subtype_t; + +/** + * @brief Audio class specific requests + * + * @ref nrf_drv_usbd_setup_t::bmRequestType + */ +typedef enum { + APP_USBD_AUDIO_REQ_UNDEFINED = 0x00, /**< UNDEFINED request*/ + + APP_USBD_AUDIO_REQ_SET_CUR = 0x01, /**< SET_CUR request */ + APP_USBD_AUDIO_REQ_SET_MIN = 0x02, /**< SET_MIN request */ + APP_USBD_AUDIO_REQ_SET_MAX = 0x03, /**< SET_MAX request */ + APP_USBD_AUDIO_REQ_SET_RES = 0x04, /**< SET_RES request */ + APP_USBD_AUDIO_REQ_SET_MEM = 0x05, /**< SET_MEM request */ + + APP_USBD_AUDIO_REQ_GET_CUR = 0x81, /**< GET_CUR request */ + APP_USBD_AUDIO_REQ_GET_MIN = 0x82, /**< GET_MIN request */ + APP_USBD_AUDIO_REQ_GET_MAX = 0x83, /**< GET_MAX request */ + APP_USBD_AUDIO_REQ_GET_RES = 0x84, /**< GET_RES request */ + APP_USBD_AUDIO_REQ_GET_MEM = 0x85, /**< GET_MEM request */ + + APP_USBD_AUDIO_REQ_GET_STAT = 0xFF, /**< GET_STAT request */ +} app_usbd_audio_req_type_t; + +/** + * @brief Audio class terminal types + * */ +typedef enum { + /*USB terminals*/ + APP_USBD_AUDIO_TERMINAL_USB_UNDEFINED = 0x0100, /**< USB_UNDEFINED*/ + APP_USBD_AUDIO_TERMINAL_USB_STREAMING = 0x0101, /**< USB_STREAMING */ + APP_USBD_AUDIO_TERMINAL_USB_VENDOR_SPEC = 0x01FF, /**< USB_VENDOR_SPEC*/ + + /*Input terminals*/ + APP_USBD_AUDIO_TERMINAL_IN_UNDEFINED = 0x0200, /**< UNDEFINED */ + APP_USBD_AUDIO_TERMINAL_IN_MICROPHONE = 0x0201, /**< MICROPHONE */ + APP_USBD_AUDIO_TERMINAL_IN_DESKTOP_MIC = 0x0202, /**< DESKTOP_MIC */ + APP_USBD_AUDIO_TERMINAL_IN_PERSONAL_MIC = 0x0203, /**< PERSONAL_MIC */ + APP_USBD_AUDIO_TERMINAL_IN_OM_DIR_MIC = 0x0204, /**< OM_DIR_MIC */ + APP_USBD_AUDIO_TERMINAL_IN_MIC_ARRAY = 0x0205, /**< MIC_ARRAY */ + APP_USBD_AUDIO_TERMINAL_IN_PROC_MIC_ARRAY = 0x0205, /**< PROC_MIC_ARRAY */ + + /*Output terminals*/ + APP_USBD_AUDIO_TERMINAL_OUT_UNDEFINED = 0x0300, /**< UNDEFINED */ + APP_USBD_AUDIO_TERMINAL_OUT_SPEAKER = 0x0301, /**< SPEAKER */ + APP_USBD_AUDIO_TERMINAL_OUT_HEADPHONES = 0x0302, /**< HEADPHONES */ + APP_USBD_AUDIO_TERMINAL_OUT_HEAD_AUDIO = 0x0303, /**< HEAD_AUDIO */ + APP_USBD_AUDIO_TERMINAL_OUT_DESKTOP_SPEAKER = 0x0304, /**< DESKTOP_SPEAKER */ + APP_USBD_AUDIO_TERMINAL_OUT_ROOM_SPEAKER = 0x0305, /**< ROOM_SPEAKER */ + APP_USBD_AUDIO_TERMINAL_OUT_COMM_SPEAKER = 0x0306, /**< COMM_SPEAKER */ + APP_USBD_AUDIO_TERMINAL_OUT_LOW_FREQ_SPEAKER = 0x0307, /**< LOW_FREQ_SPEAKER */ + + /*Input/Output terminals*/ + APP_USBD_AUDIO_TERMINAL_IO_UNDEFINED = 0x0400, /**< UNDEFINED */ + APP_USBD_AUDIO_TERMINAL_IO_HANDSET = 0x0401, /**< HANDSET */ + APP_USBD_AUDIO_TERMINAL_IO_HEADSET = 0x0402, /**< HEADSET */ + APP_USBD_AUDIO_TERMINAL_IO_SPEAKERPHONE_ECHO_NONE = 0x0403, /**< SPEAKERPHONE_ECHO_NONE */ + APP_USBD_AUDIO_TERMINAL_IO_SPEAKERPHONE_ECHO_SUP = 0x0404, /**< SPEAKERPHONE_ECHO_SUP */ + APP_USBD_AUDIO_TERMINAL_IO_SPEAKERPHONE_ECHO_CAN = 0x0405, /**< SPEAKERPHONE_ECHO_CAN */ +} app_usbd_audio_terminal_type_t; + +/** + * @brief Audio class control interface header descriptor + */ +typedef struct { + uint8_t bLength; //!< Length of the descriptor + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_AUDIO_DESCRIPTOR_INTERFACE + uint8_t bDescriptorSubType; //!< Descriptor subtype @ref APP_USBD_AUDIO_AC_IFACE_SUBTYPE_HEADER + uint8_t bcdADC[2]; //!< BCD ADC + uint8_t wTotalLength[2]; //!< Total interfaces length + uint8_t bInCollection; //!< Input collection + uint8_t baInterfaceNr[]; //!< Interface number list +} app_usbd_audio_ac_iface_header_desc_t; + + +/** + * @brief Possible values of input terminal channel config + * + * @ref app_usbd_audio_input_terminal_desc_t::wChannelConfig + * */ +typedef enum { + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_LEFT_FRONT = (1u << 0), /**< Channel config bit LEFT_FRONT */ + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_RIGHT_FRONT = (1u << 1), /**< Channel config bit RIGHT_FRONT */ + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_CENTER_FRONT = (1u << 2), /**< Channel config bit CENTER_FRONT */ + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_LOW_FREQ_ENH = (1u << 3), /**< Channel config bit LOW_FREQ_ENH */ + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_LEFT_SURROUND = (1u << 4), /**< Channel config bit LEFT_SURROUND */ + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_RIGHT_SURROUND = (1u << 5), /**< Channel config bit RIGHT_SURROUND */ + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_LEFT_OF_CENTER = (1u << 6), /**< Channel config bit LEFT_OF_CENTER */ + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_RIGHT_OF_CENTER = (1u << 7), /**< Channel config bit RIGHT_OF_CENTER */ + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_SURROUND = (1u << 8), /**< Channel config bit SURROUND */ + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_SIDE_LEFT = (1u << 9), /**< Channel config bit SIDE_LEFT */ + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_SIDE_RIGHT = (1u << 10), /**< Channel config bit SIDE_RIGHT */ + APP_USBD_AUDIO_IN_TERM_CH_CONFIG_TOP = (1u << 11), /**< Channel config bit TOP */ +} app_usbd_audio_in_term_ch_config_t; + +/** + * @brief Audio class input terminal descriptor + */ +typedef struct { + uint8_t bLength; //!< Length of the descriptor + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_AUDIO_DESCRIPTOR_INTERFACE + uint8_t bDescriptorSubType; //!< Descriptor subtype @ref APP_USBD_AUDIO_AC_IFACE_SUBTYPE_INPUT_TERMINAL + uint8_t bTerminalID; //!< Terminal ID + uint8_t wTerminalType[2]; //!< Terminal type + uint8_t bAssocTerminal; //!< Association terminal + uint8_t bNrChannels; //!< Number of channels + uint8_t wChannelConfig[2]; //!< Channel config + uint8_t iChannelNames; //!< Channel names + uint8_t iTerminal; //!< Terminal string ID +} app_usbd_audio_input_terminal_desc_t; + +/** + * @brief Audio class output terminal descriptor + */ +typedef struct { + uint8_t bLength; //!< Length of the descriptor + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_AUDIO_DESCRIPTOR_INTERFACE + uint8_t bDescriptorSubType; //!< Descriptor subtype @ref APP_USBD_AUDIO_AC_IFACE_SUBTYPE_OUTPUT_TERNINAL + uint8_t bTerminalID; //!< Terminal ID + uint8_t wTerminalType[2]; //!< Terminal type + uint8_t bAssocTerminal; //!< Association terminal + uint8_t bSourceID; //!< Source ID + uint8_t iTerminal; //!< Terminal string ID +} app_usbd_audio_output_terminal_desc_t; + +/** + * @brief Possible values of feature unit control field*/ +typedef enum { + APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE = (1u << 0), /**< Feature unit control bit MUTE */ + APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_VOLUME = (1u << 1), /**< Feature unit control bit VOLUME */ + APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_BASS = (1u << 2), /**< Feature unit control bit BASS */ + APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MID = (1u << 3), /**< Feature unit control bit MID */ + APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_TREBLE = (1u << 4), /**< Feature unit control bit TREBLE */ + APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_GRAPH_EQ = (1u << 5), /**< Feature unit control bit GRAPH_EQ */ + APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_AUTO_GAIN = (1u << 6), /**< Feature unit control bit AUTO_GAIN */ + APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_DELAY = (1u << 7), /**< Feature unit control bit DELAY */ + APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_BASS_BOOST = (1u << 8), /**< Feature unit control bit BASS_BOOST*/ + APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_LOUDNESS = (1u << 9), /**< Feature unit control bit LOUDNESS */ +} app_usbd_audio_feature_unit_control_t; + +/** + * @brief Audio class feature unit descriptor + */ +typedef struct { + uint8_t bLength; //!< Length of the descriptor + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_AUDIO_DESCRIPTOR_INTERFACE + uint8_t bDescriptorSubType; //!< Descriptor subtype @ref APP_USBD_AUDIO_AC_IFACE_SUBTYPE_FEATURE_UNIT + uint8_t bUnitID; //!< Unit ID + uint8_t bSourceID; //!< Source ID + uint8_t bControlSize; //!< Control size + uint8_t bmaControls[]; //!< Controls array +} app_usbd_audio_feature_unit_desc_t; + +/** + * @brief Format tag in audio streaming interface descriptor + * + * @ref app_usbd_audio_as_iface_desc_t::wFormatTag + * */ +typedef enum { + APP_USBD_AUDIO_AS_IFACE_FORMAT_TYPE_I_UNDEFINED = 0x0000, /**< AS format TYPE_I_UNDEFINED */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_PCM = 0x0001, /**< AS format PCM */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_PCM8 = 0x0002, /**< AS format PCM8 */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_IEEE_FLOAT = 0x0003, /**< AS format IEEE_FLOAT */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_ALAW = 0x0004, /**< AS format ALAW */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_MULAW = 0x0005, /**< AS format MULAW */ + + APP_USBD_AUDIO_AS_IFACE_FORMAT_TYPE_II_UNDEFINED = 0x1000, /**< AS format TYPE_II_UNDEFINED */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_MPEG = 0x1001, /**< AS format MPEG */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_AC3 = 0x1002, /**< AS format AC3 */ + + APP_USBD_AUDIO_AS_IFACE_FORMAT_TYPE_III_UNDEFINED = 0x2000, /**< AS format TYPE_III_UNDEFINED */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_IEC1937_AC_3 = 0x2001, /**< AS format IEC1937_AC_3 */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_IEC1937_MPEG_1_LAYER1 = 0x2002, /**< AS format IEC1937_MPEG_1_LAYER1 */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_IEC1937_MPEG_2_NOEXT = 0x2003, /**< AS format IEC1937_MPEG_2_NOEXT */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_IEC1937_MPEG_2_EXT = 0x2004, /**< AS format IEC1937_MPEG_2_EXT */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_IEC1937_MPEG_2_LAYER1_LS = 0x2005, /**< AS format IEC1937_MPEG_2_LAYER1_LS */ + APP_USBD_AUDIO_AS_IFACE_FORMAT_IEC1937_MPEG_2_LAYER23_LS = 0x2005, /**< AS format IEC1937_MPEG_2_LAYER23_LS */ +} app_usbd_audio_as_iface_format_tag_t; + +/** + * @brief Audio class audio streaming interface descriptor + */ +typedef struct { + uint8_t bLength; //!< Length of the descriptor + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_AUDIO_DESCRIPTOR_INTERFACE + uint8_t bDescriptorSubType; //!< Descriptor subtype @ref app_usbd_audio_ac_iface_subtype_t + uint8_t bTerminalLink; //!< Terminal link + uint8_t bDelay; //!< Delay + uint8_t wFormatTag[2]; //!< Format TAG +} app_usbd_audio_as_iface_desc_t; + +/** + * @brief Audio class audio streaming format type I descriptor + */ +typedef struct { + uint8_t bLength; //!< Length of the descriptor + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_AUDIO_DESCRIPTOR_INTERFACE + uint8_t bDescriptorSubType; //!< Descriptor subtype @ref app_usbd_audio_as_iface_subtype_t + uint8_t bFormatType; //!< Format type: fixed value 1 + uint8_t bNrChannels; //!< Number of channels + uint8_t bSubframeSize; //!< Subframe size + uint8_t bBitResolution; //!< Bit resolution + uint8_t bSamFreqType; //!< Number of supported sampling frequencies + uint8_t tSamFreq[]; //!< Number of supported sampling frequencies table (24 bit entries) +} app_usbd_audio_as_format_type_one_desc_t; + + +/** + * @brief Audio class audio streaming format type II descriptor + */ +typedef struct { + uint8_t bLength; //!< Length of the descriptor + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_AUDIO_DESCRIPTOR_INTERFACE + uint8_t bDescriptorSubType; //!< Descriptor subtype @ref app_usbd_audio_as_iface_subtype_t + uint8_t bFormatType; //!< Format type: fixed value 2 + uint8_t wMaxBitRate[2]; //!< Maximum bitrate + uint8_t wSamplesPerFrame[2]; //!< Samples per frame + uint8_t bSamFreqType; //!< Number of supported sampling frequencies + uint8_t tSamFreq[]; //!< Number of supported sampling frequencies table (24 bit entries) +} app_usbd_audio_as_format_type_two_desc_t; + +/** + * @brief Audio class audio streaming format type III descriptor + */ +typedef struct { + uint8_t bLength; //!< Length of the descriptor + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_AUDIO_DESCRIPTOR_INTERFACE + uint8_t bDescriptorSubType; //!< Descriptor subtype @ref app_usbd_audio_as_iface_subtype_t + uint8_t bFormatType; //!< Format type: fixed value 1 + uint8_t bNrChannels; //!< Number of channels + uint8_t bSubframeSize; //!< Subframe size + uint8_t bBitResolution; //!< Bit resolution + uint8_t bSamFreqType; //!< Number of supported sampling frequencies + uint8_t tSamFreq[]; //!< Number of supported sampling frequencies table (24 bit entries) +} app_usbd_audio_as_format_type_three_desc_t; + +/** + * @brief Audio class audio endpoint descriptor + */ +typedef struct { + uint8_t bLength; //!< Length of the descriptor + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_AUDIO_DESCRIPTOR_ENDPOINT + uint8_t bDescriptorSubType; //!< Descriptor subtype @ref APP_USBD_AUDIO_EP_SUBTYPE_GENERAL + uint8_t bmAttributes; //!< Audio endpoint attributes + uint8_t bLockDelayUnits; //!< Lock delay units + uint8_t wLockDelay[2]; //!< Lock delay value +} app_usbd_audio_as_endpoint_desc_t; + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_AUDIO_TYPES_H__ */ + diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/acm/app_usbd_cdc_acm.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/acm/app_usbd_cdc_acm.c new file mode 100644 index 0000000..3df941c --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/acm/app_usbd_cdc_acm.c @@ -0,0 +1,1190 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(APP_USBD_CDC_ACM) + +#include "app_usbd_cdc_acm.h" +#include <inttypes.h> + +/** + * @defgroup app_usbd_cdc_acm_internal CDC ACM internals + * @{ + * @ingroup app_usbd_cdc + * @internal + */ + + +#define NRF_LOG_MODULE_NAME cdc_acm + +#if APP_USBD_CDC_ACM_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL APP_USBD_CDC_ACM_CONFIG_LOG_LEVEL +#define NRF_LOG_INFO_COLOR APP_USBD_CDC_ACM_CONFIG_INFO_COLOR +#define NRF_LOG_DEBUG_COLOR APP_USBD_CDC_ACM_CONFIG_DEBUG_COLOR +#else //APP_USBD_CDC_ACM_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL 0 +#endif //APP_USBD_CDC_ACM_CONFIG_LOG_ENABLED +#include "nrf_log.h" +NRF_LOG_MODULE_REGISTER(); + +#define APP_USBD_CDC_ACM_COMM_IFACE_IDX 0 /**< CDC ACM class comm interface index. */ +#define APP_USBD_CDC_ACM_DATA_IFACE_IDX 1 /**< CDC ACM class data interface index. */ + +#define APP_USBD_CDC_ACM_COMM_EPIN_IDX 0 /**< CDC ACM comm class endpoint IN index. */ +#define APP_USBD_CDC_ACM_DATA_EPIN_IDX 0 /**< CDC ACM data class endpoint IN index. */ +#define APP_USBD_CDC_ACM_DATA_EPOUT_IDX 1 /**< CDC ACM data class endpoint OUT index. */ + +/** + * @brief Auxiliary function to access cdc_acm class instance data. + * + * @param[in] p_inst Class instance data. + * + * @return CDC ACM class instance. + */ +static inline app_usbd_cdc_acm_t const * cdc_acm_get(app_usbd_class_inst_t const * p_inst) +{ + ASSERT(p_inst != NULL); + return (app_usbd_cdc_acm_t const *)p_inst; +} + +/** + * @brief Auxiliary function to access cdc_acm class context data. + * + * @param[in] p_cdc_acm CDC ACM class instance data. + * + * @return CDC ACM class instance context. + */ +static inline app_usbd_cdc_acm_ctx_t * cdc_acm_ctx_get(app_usbd_cdc_acm_t const * p_cdc_acm) +{ + ASSERT(p_cdc_acm != NULL); + ASSERT(p_cdc_acm->specific.p_data != NULL); + return &p_cdc_acm->specific.p_data->ctx; +} + +/** + * @brief User event handler. + * + * @param[in] p_inst Class instance. + * @param[in] event user Event type. + */ +static inline void user_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_cdc_acm_user_event_t event) +{ + app_usbd_cdc_acm_t const * p_cdc_acm = cdc_acm_get(p_inst); + if (p_cdc_acm->specific.inst.user_ev_handler != NULL) + { + p_cdc_acm->specific.inst.user_ev_handler(p_inst, event); + } +} + +/** + * @brief Auxiliary function to access CDC ACM COMM IN endpoint address. + * + * @param[in] p_inst Class instance data. + * + * @return IN endpoint address. + */ +static inline nrf_drv_usbd_ep_t comm_ep_in_addr_get(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_class_iface_conf_t const * class_iface; + class_iface = app_usbd_class_iface_get(p_inst, APP_USBD_CDC_ACM_COMM_IFACE_IDX); + + app_usbd_class_ep_conf_t const * ep_cfg; + ep_cfg = app_usbd_class_iface_ep_get(class_iface, APP_USBD_CDC_ACM_COMM_EPIN_IDX); + + return app_usbd_class_ep_address_get(ep_cfg); +} + +/** + * @brief Auxiliary function to access CDC ACM DATA IN endpoint address. + * + * @param[in] p_inst Class instance data. + * + * @return IN endpoint address. + */ +static inline nrf_drv_usbd_ep_t data_ep_in_addr_get(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_class_iface_conf_t const * class_iface; + class_iface = app_usbd_class_iface_get(p_inst, APP_USBD_CDC_ACM_DATA_IFACE_IDX); + + app_usbd_class_ep_conf_t const * ep_cfg; + ep_cfg = app_usbd_class_iface_ep_get(class_iface, APP_USBD_CDC_ACM_DATA_EPIN_IDX); + + return app_usbd_class_ep_address_get(ep_cfg); +} + +/** + * @brief Auxiliary function to access CDC ACM DATA OUT endpoint address. + * + * @param[in] p_inst Class instance data. + * + * @return OUT endpoint address. + */ +static inline nrf_drv_usbd_ep_t data_ep_out_addr_get(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_class_iface_conf_t const * class_iface; + class_iface = app_usbd_class_iface_get(p_inst, APP_USBD_CDC_ACM_DATA_IFACE_IDX); + + app_usbd_class_ep_conf_t const * ep_cfg; + ep_cfg = app_usbd_class_iface_ep_get(class_iface, APP_USBD_CDC_ACM_DATA_EPOUT_IDX); + + return app_usbd_class_ep_address_get(ep_cfg); +} + +/** + * @brief Internal SETUP standard IN request handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t setup_req_std_in(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + /* Only Get Descriptor standard IN request is supported by CDC class */ + if ((app_usbd_setup_req_rec(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQREC_INTERFACE) + && + (p_setup_ev->setup.bmRequest == APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR)) + { + size_t dsc_len = 0; + size_t max_size; + + uint8_t * p_trans_buff = app_usbd_core_setup_transfer_buff_get(&max_size); + + /* Try to find descriptor in class internals*/ + ret_code_t ret = app_usbd_class_descriptor_find( + p_inst, + p_setup_ev->setup.wValue.hb, + p_setup_ev->setup.wValue.lb, + p_trans_buff, + &dsc_len); + + if (ret != NRF_ERROR_NOT_FOUND) + { + ASSERT(dsc_len < NRF_DRV_USBD_EPSIZE); + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_trans_buff, dsc_len); + } + } + + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Internal SETUP standard OUT request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code. + */ +static ret_code_t setup_req_std_out(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + + switch (p_setup_ev->setup.bmRequest) + { + default: + break; + } + + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Internal SETUP class IN request handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t setup_req_class_in(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_cdc_acm_t const * p_cdc_acm = cdc_acm_get(p_inst); + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_CDC_REQ_GET_LINE_CODING: + { + if (p_setup_ev->setup.wLength.w != sizeof(app_usbd_cdc_line_coding_t)) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + return app_usbd_core_setup_rsp(&p_setup_ev->setup, + &p_cdc_acm_ctx->line_coding, + sizeof(app_usbd_cdc_line_coding_t)); + } + default: + break; + } + + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Class specific OUT request data callback. + * + * @param status Endpoint status. + * @param p_context Context of transfer (set by @ref app_usbd_core_setup_data_handler_set). + * + * @return Standard error code. + */ +static ret_code_t cdc_acm_req_out_data_cb(nrf_drv_usbd_ep_status_t status, void * p_context) +{ + if (status != NRF_USBD_EP_OK) + { + return NRF_ERROR_INTERNAL; + } + + app_usbd_cdc_acm_t const * p_cdc_acm = p_context; + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + switch (p_cdc_acm_ctx->request.type) + { + case APP_USBD_CDC_REQ_SET_LINE_CODING: + { + memcpy(&p_cdc_acm_ctx->line_coding, + &p_cdc_acm_ctx->request.payload.line_coding, + sizeof(app_usbd_cdc_line_coding_t)); + + NRF_LOG_INFO("REQ_SET_LINE_CODING: baudrate: %"PRIu32", databits: %u, " + "format: %u, parity: %u", + uint32_decode(p_cdc_acm_ctx->line_coding.dwDTERate), + p_cdc_acm_ctx->line_coding.bDataBits, + p_cdc_acm_ctx->line_coding.bCharFormat, + p_cdc_acm_ctx->line_coding.bParityType); + break; + } + default: + return NRF_ERROR_NOT_SUPPORTED; + } + + return NRF_SUCCESS; +} + + +/** + * @brief Class specific request data stage setup. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t cdc_acm_req_out_datastage(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_cdc_acm_t const * p_cdc_acm = cdc_acm_get(p_inst); + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + p_cdc_acm_ctx->request.type = p_setup_ev->setup.bmRequest; + p_cdc_acm_ctx->request.len = p_setup_ev->setup.wLength.w; + + /*Request setup data*/ + NRF_DRV_USBD_TRANSFER_OUT(transfer, + &p_cdc_acm_ctx->request.payload, + p_cdc_acm_ctx->request.len); + ret_code_t ret; + CRITICAL_REGION_ENTER(); + ret = app_usbd_ep_transfer(NRF_DRV_USBD_EPOUT0, &transfer); + if (ret == NRF_SUCCESS) + { + const app_usbd_core_setup_data_handler_desc_t desc = { + .handler = cdc_acm_req_out_data_cb, + .p_context = (void*)p_cdc_acm + }; + + ret = app_usbd_core_setup_data_handler_set(NRF_DRV_USBD_EPOUT0, &desc); + } + CRITICAL_REGION_EXIT(); + + return ret; +} + +/** + * @brief Reset port to default state. + * + * @param p_inst Generic class instance. + */ +static void cdc_acm_reset_port(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_cdc_acm_t const * p_cdc_acm = cdc_acm_get(p_inst); + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + p_cdc_acm_ctx->line_state = 0; + + // Set rx transfers configuration to default state. + p_cdc_acm_ctx->rx_transfer[0].p_buf = NULL; + p_cdc_acm_ctx->rx_transfer[1].p_buf = NULL; + p_cdc_acm_ctx->bytes_left = 0; + p_cdc_acm_ctx->bytes_read = 0; + p_cdc_acm_ctx->last_read = 0; + p_cdc_acm_ctx->cur_read = 0; + p_cdc_acm_ctx->p_copy_pos = p_cdc_acm_ctx->internal_rx_buf; +} + +/** + * @brief Internal SETUP class OUT request handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t setup_req_class_out(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_cdc_acm_t const * p_cdc_acm = cdc_acm_get(p_inst); + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_CDC_REQ_SET_LINE_CODING: + { + if (p_setup_ev->setup.wLength.w != sizeof(app_usbd_cdc_line_coding_t)) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + return cdc_acm_req_out_datastage(p_inst, p_setup_ev); + } + case APP_USBD_CDC_REQ_SET_CONTROL_LINE_STATE: + { + if (p_setup_ev->setup.wLength.w != 0) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + NRF_LOG_INFO("REQ_SET_CONTROL_LINE_STATE: 0x%x", p_setup_ev->setup.wValue.w); + + bool old_dtr = (p_cdc_acm_ctx->line_state & APP_USBD_CDC_ACM_LINE_STATE_DTR) ? + true : false; + p_cdc_acm_ctx->line_state = p_setup_ev->setup.wValue.w; + + bool new_dtr = (p_cdc_acm_ctx->line_state & APP_USBD_CDC_ACM_LINE_STATE_DTR) ? + true : false; + + if (old_dtr == new_dtr) + { + return NRF_SUCCESS; + } + + const app_usbd_cdc_acm_user_event_t ev = new_dtr ? + APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN : APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE; + + user_event_handler(p_inst, ev); + + if (!new_dtr) + { + /*Abort DATA endpoints on port close */ + nrf_drv_usbd_ep_t ep; + ep = data_ep_in_addr_get(p_inst); + nrf_drv_usbd_ep_abort(ep); + ep = data_ep_out_addr_get(p_inst); + nrf_drv_usbd_ep_abort(ep); + + // Set rx transfers configuration to default state. + p_cdc_acm_ctx->rx_transfer[0].p_buf = NULL; + p_cdc_acm_ctx->rx_transfer[1].p_buf = NULL; + p_cdc_acm_ctx->bytes_left = 0; + p_cdc_acm_ctx->bytes_read = 0; + p_cdc_acm_ctx->last_read = 0; + p_cdc_acm_ctx->cur_read = 0; + p_cdc_acm_ctx->p_copy_pos = p_cdc_acm_ctx->internal_rx_buf; + } + + return NRF_SUCCESS; + } + default: + break; + } + + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Control endpoint handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t setup_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + ASSERT(p_inst != NULL); + ASSERT(p_setup_ev != NULL); + + if (app_usbd_setup_req_dir(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQDIR_IN) + { + switch (app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + case APP_USBD_SETUP_REQTYPE_STD: + return setup_req_std_in(p_inst, p_setup_ev); + case APP_USBD_SETUP_REQTYPE_CLASS: + return setup_req_class_in(p_inst, p_setup_ev); + default: + break; + } + } + else /*APP_USBD_SETUP_REQDIR_OUT*/ + { + switch (app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + case APP_USBD_SETUP_REQTYPE_STD: + return setup_req_std_out(p_inst, p_setup_ev); + case APP_USBD_SETUP_REQTYPE_CLASS: + return setup_req_class_out(p_inst, p_setup_ev); + default: + break; + } + } + + return NRF_ERROR_NOT_SUPPORTED; +} + + +/** + * @brief CDC ACM consumer + * + * @note See @ref nrf_drv_usbd_consumer_t + */ +static bool cdc_acm_consumer(nrf_drv_usbd_ep_transfer_t * p_next, + void * p_context, + size_t ep_size, + size_t data_size) +{ + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = (app_usbd_cdc_acm_ctx_t *) p_context; + p_next->size = data_size; + + if (data_size <= p_cdc_acm_ctx->rx_transfer[0].read_left) + { + p_next->p_data.rx = p_cdc_acm_ctx->rx_transfer[0].p_buf; + + p_cdc_acm_ctx->rx_transfer[0].p_buf += data_size; + p_cdc_acm_ctx->bytes_read += data_size; + p_cdc_acm_ctx->rx_transfer[0].read_left -= data_size; + NRF_LOG_DEBUG("Received %d bytes. Space left in user buffer: %d bytes.", + data_size, + p_cdc_acm_ctx->rx_transfer[0].read_left); + return (p_cdc_acm_ctx->rx_transfer[0].read_left) != 0; + } + else + { + p_next->p_data.rx = p_cdc_acm_ctx->internal_rx_buf; + + p_cdc_acm_ctx->cur_read = data_size; + NRF_LOG_DEBUG("Received %d bytes. Stored in internal buffer.", data_size); + return false; + } +} + +/** + * @brief CDC ACM single transfer consumer + * + * This function finalizes transfer after any received block + * + * @note See @ref nrf_drv_usbd_consumer_t + */ +static bool cdc_acm_single_shoot_consumer(nrf_drv_usbd_ep_transfer_t * p_next, + void * p_context, + size_t ep_size, + size_t data_size) +{ + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = (app_usbd_cdc_acm_ctx_t *) p_context; + p_next->size = data_size; + + if (data_size <= p_cdc_acm_ctx->rx_transfer[0].read_left) + { + p_next->p_data.rx = p_cdc_acm_ctx->rx_transfer[0].p_buf; + + p_cdc_acm_ctx->bytes_read = data_size; + p_cdc_acm_ctx->rx_transfer[0].read_left = data_size; + NRF_LOG_DEBUG("Received %d bytes. Space left in user buffer: %d bytes.", + data_size, + p_cdc_acm_ctx->rx_transfer[0].read_left); + } + else + { + p_next->p_data.rx = p_cdc_acm_ctx->internal_rx_buf; + p_cdc_acm_ctx->cur_read = data_size; + NRF_LOG_DEBUG("Received %d bytes. Stored in internal buffer.", data_size); + } + + return false; +} + +/** + * @brief Manage switching between user buffers and copying data from internal buffer. + * + * @param p_inst Generic USB class instance. + * + * @return Standard error code + */ +static ret_code_t cdc_acm_rx_block_finished(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_cdc_acm_t const * p_cdc_acm = cdc_acm_get(p_inst); + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + nrf_drv_usbd_ep_t ep = data_ep_out_addr_get(p_inst); + + nrf_drv_usbd_handler_desc_t handler_desc = { + .handler.consumer = cdc_acm_consumer, + .p_context = p_cdc_acm_ctx + }; + + if (p_cdc_acm_ctx->rx_transfer[0].read_left == 0) // Buffer completely filled by consumer + { + p_cdc_acm_ctx->last_read = p_cdc_acm_ctx->bytes_read; + p_cdc_acm_ctx->bytes_read = 0; + p_cdc_acm_ctx->bytes_left = 0; + + if (p_cdc_acm_ctx->rx_transfer[1].p_buf != NULL) + { + p_cdc_acm_ctx->rx_transfer[0] = p_cdc_acm_ctx->rx_transfer[1]; + p_cdc_acm_ctx->rx_transfer[1].p_buf = NULL; + return app_usbd_ep_handled_transfer(ep, &handler_desc); + } + else + { + p_cdc_acm_ctx->rx_transfer[0].p_buf = NULL; + return NRF_SUCCESS; + } + } + + size_t bytes_read = p_cdc_acm_ctx->cur_read; + size_t bytes_to_cpy = bytes_read; + + if (bytes_read > p_cdc_acm_ctx->rx_transfer[0].read_left) + { + bytes_to_cpy = p_cdc_acm_ctx->rx_transfer[0].read_left; + } + memcpy(p_cdc_acm_ctx->rx_transfer[0].p_buf, + p_cdc_acm_ctx->internal_rx_buf, + bytes_to_cpy); + + // First buffer is full + p_cdc_acm_ctx->last_read = p_cdc_acm_ctx->bytes_read + bytes_to_cpy; + p_cdc_acm_ctx->bytes_read = 0; + p_cdc_acm_ctx->bytes_left = bytes_read - bytes_to_cpy; + p_cdc_acm_ctx->p_copy_pos = p_cdc_acm_ctx->internal_rx_buf + bytes_to_cpy; + + if (p_cdc_acm_ctx->rx_transfer[1].p_buf != NULL) + { + // If there is second transfer, copy it to first + p_cdc_acm_ctx->rx_transfer[0] = p_cdc_acm_ctx->rx_transfer[1]; + p_cdc_acm_ctx->rx_transfer[1].p_buf = NULL; + + while (p_cdc_acm_ctx->bytes_left > 0) + { + + if (p_cdc_acm_ctx->bytes_left >= p_cdc_acm_ctx->rx_transfer[0].read_left) + { + // If there are enough bytes left in internal buffer to completely fill next transfer, + // we call user event handler to obtain next buffer and continue double buffering. + memcpy(p_cdc_acm_ctx->rx_transfer[0].p_buf, + p_cdc_acm_ctx->p_copy_pos, + p_cdc_acm_ctx->rx_transfer[0].read_left); + + p_cdc_acm_ctx->bytes_left -= p_cdc_acm_ctx->rx_transfer[0].read_left; + p_cdc_acm_ctx->p_copy_pos += p_cdc_acm_ctx->rx_transfer[0].read_left; + p_cdc_acm_ctx->last_read = p_cdc_acm_ctx->rx_transfer[0].read_left; + user_event_handler(p_inst, APP_USBD_CDC_ACM_USER_EVT_RX_DONE); + + if (p_cdc_acm_ctx->rx_transfer[1].p_buf != NULL) + { + p_cdc_acm_ctx->rx_transfer[0] = p_cdc_acm_ctx->rx_transfer[1]; + p_cdc_acm_ctx->rx_transfer[1].p_buf = NULL; + } + else + { + // If user does not specify a second buffer, all data transfers are done + // and data left in internal buffer is lost. + p_cdc_acm_ctx->rx_transfer[0].p_buf = NULL; + break; + } + } + else + { + memcpy(p_cdc_acm_ctx->rx_transfer[0].p_buf, + p_cdc_acm_ctx->p_copy_pos, + p_cdc_acm_ctx->bytes_left); + p_cdc_acm_ctx->bytes_read = p_cdc_acm_ctx->bytes_left; + + p_cdc_acm_ctx->rx_transfer[0].read_left -= p_cdc_acm_ctx->bytes_left; + p_cdc_acm_ctx->rx_transfer[0].p_buf += p_cdc_acm_ctx->bytes_left; + break; + } + } + + + } + else + { + p_cdc_acm_ctx->rx_transfer[0].p_buf = NULL; + } + + if (p_cdc_acm_ctx->rx_transfer[0].p_buf != NULL) + { + return app_usbd_ep_handled_transfer(ep, &handler_desc); + } + return NRF_SUCCESS; +} + +/** + * @brief Class specific endpoint transfer handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t cdc_acm_endpoint_ev(app_usbd_class_inst_t const * p_inst, + app_usbd_complex_evt_t const * p_event) +{ + if (comm_ep_in_addr_get(p_inst) == p_event->drv_evt.data.eptransfer.ep) + { + NRF_LOG_INFO("EPIN_COMM: notify"); + return NRF_SUCCESS; + } + ret_code_t ret; + if (NRF_USBD_EPIN_CHECK(p_event->drv_evt.data.eptransfer.ep)) + { + switch (p_event->drv_evt.data.eptransfer.status) + { + case NRF_USBD_EP_OK: + NRF_LOG_INFO("EPIN_DATA: %02x done", p_event->drv_evt.data.eptransfer.ep); + user_event_handler(p_inst, APP_USBD_CDC_ACM_USER_EVT_TX_DONE); + return NRF_SUCCESS; + case NRF_USBD_EP_ABORTED: + return NRF_SUCCESS; + default: + return NRF_ERROR_INTERNAL; + } + } + + if (NRF_USBD_EPOUT_CHECK(p_event->drv_evt.data.eptransfer.ep)) + { + switch (p_event->drv_evt.data.eptransfer.status) + { + case NRF_USBD_EP_OK: + ret = cdc_acm_rx_block_finished(p_inst); + NRF_LOG_INFO("EPOUT_DATA: %02x done", p_event->drv_evt.data.eptransfer.ep); + user_event_handler(p_inst, APP_USBD_CDC_ACM_USER_EVT_RX_DONE); + return ret; + case NRF_USBD_EP_WAITING: + case NRF_USBD_EP_ABORTED: + return NRF_SUCCESS; + default: + return NRF_ERROR_INTERNAL; + } + } + + return NRF_ERROR_NOT_SUPPORTED; +} + + +/** + * @brief @ref app_usbd_class_methods_t::event_handler + */ +static ret_code_t cdc_acm_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_complex_evt_t const * p_event) +{ + ASSERT(p_inst != NULL); + ASSERT(p_event != NULL); + + ret_code_t ret = NRF_SUCCESS; + switch (p_event->app_evt.type) + { + case APP_USBD_EVT_DRV_SOF: + break; + case APP_USBD_EVT_DRV_RESET: + cdc_acm_reset_port(p_inst); + break; + case APP_USBD_EVT_DRV_SETUP: + ret = setup_event_handler(p_inst, (app_usbd_setup_evt_t const *)p_event); + break; + case APP_USBD_EVT_DRV_EPTRANSFER: + ret = cdc_acm_endpoint_ev(p_inst, p_event); + break; + case APP_USBD_EVT_DRV_SUSPEND: + break; + case APP_USBD_EVT_DRV_RESUME: + break; + case APP_USBD_EVT_INST_APPEND: + break; + case APP_USBD_EVT_INST_REMOVE: + break; + case APP_USBD_EVT_STARTED: + break; + case APP_USBD_EVT_STOPPED: + break; + case APP_USBD_EVT_POWER_REMOVED: + cdc_acm_reset_port(p_inst); + break; + default: + ret = NRF_ERROR_NOT_SUPPORTED; + break; + } + + return ret; +} + + +static bool cdc_acm_feed_descriptors(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size) +{ + static uint8_t ifaces = 0; + ifaces = app_usbd_class_iface_count_get(p_inst); + app_usbd_cdc_acm_t const * p_cdc_acm = cdc_acm_get(p_inst); + + APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size); + + /* INTERFACE ASSOCIATION DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x08); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE_ASSOCIATION); // bDescriptorType = Interface Association + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_cdc_acm->specific.inst.comm_interface); // bFirstInterface + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x02); // bInterfaceCount + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_COMM_CLASS); // bFunctionClass + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_SUBCLASS_ACM); // bFunctionSubClass + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_cdc_acm->specific.inst.protocol); // bFunctionProtocol + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iFunction + + static uint8_t i = 0; + + for (i = 0; i < ifaces; i++) + { + /* INTERFACE DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE); // bDescriptorType = Interface + + static app_usbd_class_iface_conf_t const * p_cur_iface = NULL; + p_cur_iface = app_usbd_class_iface_get(p_inst, i); + + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_number_get(p_cur_iface)); // bInterfaceNumber + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bAlternateSetting + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_ep_count_get(p_cur_iface)); // bNumEndpoints + + if (p_cdc_acm->specific.inst.comm_interface == app_usbd_class_iface_number_get(p_cur_iface)) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_COMM_CLASS); // bInterfaceClass = CDC COMM + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_SUBCLASS_ACM); // bInterfaceSubclass + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_cdc_acm->specific.inst.protocol); // bInterfaceProtocol + } + else if (p_cdc_acm->specific.inst.data_interface == + app_usbd_class_iface_number_get(p_cur_iface)) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_DATA_CLASS); // bInterfaceClass = CDC DATA + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bInterfaceSubclass + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bInterfaceProtocol + } + else + { + ASSERT(0); + } + + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iInterface + + if (p_cdc_acm->specific.inst.comm_interface == app_usbd_class_iface_number_get(p_cur_iface)) + { + /* HEADER DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x05); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_CS_INTERFACE); // bDescriptorType = Class Specific Interface + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_SCS_HEADER); // bDescriptorSubtype = Header Functional Descriptor + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x10); // bcdCDC LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x01); // bcdCDC MSB + + /* CALL MANAGEMENT DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x05); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_CS_INTERFACE); // bDescriptorType = Class Specific Interface + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_SCS_CALL_MGMT); // bDescriptorSubtype = Call Management Functional Descriptor + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x03); // bmCapabilities + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_cdc_acm->specific.inst.data_interface); // bDataInterface + + /* ABSTRACT CONTROL MANAGEMENT DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x04); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_CS_INTERFACE); // bDescriptorType = Class Specific Interface + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_SCS_ACM); // bDescriptorSubtype = Abstract Control Management Functional Descriptor + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x02); // bmCapabilities + + /* UNION DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x05); + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_CS_INTERFACE); // bDescriptorType = Class Specific Interface + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_CDC_SCS_UNION); // bDescriptorSubtype = Union Functional Descriptor + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_cdc_acm->specific.inst.comm_interface); // bControlInterface + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_cdc_acm->specific.inst.data_interface); // bSubordinateInterface + } + else if (p_cdc_acm->specific.inst.data_interface == + app_usbd_class_iface_number_get(p_cur_iface)) + { + ; + } + else + { + ASSERT(0); + } + + /* ENDPOINT DESCRIPTORS */ + static uint8_t endpoints = 0; + endpoints = app_usbd_class_iface_ep_count_get(p_cur_iface); + + static uint8_t j = 0; + + for (j = 0; j < endpoints; j++) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x07); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_ENDPOINT); // bDescriptorType = Endpoint + + static app_usbd_class_ep_conf_t const * p_cur_ep = NULL; + p_cur_ep = app_usbd_class_iface_ep_get(p_cur_iface, j); + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_ep_address_get(p_cur_ep)); // bEndpointAddress + + if (p_cdc_acm->specific.inst.comm_interface == + app_usbd_class_iface_number_get(p_cur_iface)) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_INTERRUPT); // bmAttributes + } + else if (p_cdc_acm->specific.inst.data_interface == + app_usbd_class_iface_number_get(p_cur_iface)) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_BULK); // bmAttributes + } + else + { + ASSERT(0); + } + + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize MSB + + if (p_cdc_acm->specific.inst.comm_interface == + app_usbd_class_iface_number_get(p_cur_iface)) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(16); // bInterval + } + else if (p_cdc_acm->specific.inst.data_interface == + app_usbd_class_iface_number_get(p_cur_iface)) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bInterval + } + else + { + ASSERT(0); + } + + } + + } + + APP_USBD_CLASS_DESCRIPTOR_END(); +} + +/** + * @brief Public cdc_acm class interface + * + */ +const app_usbd_class_methods_t app_usbd_cdc_acm_class_methods = { + .event_handler = cdc_acm_event_handler, + .feed_descriptors = cdc_acm_feed_descriptors, +}; + +/** @} */ + +ret_code_t app_usbd_cdc_acm_write(app_usbd_cdc_acm_t const * p_cdc_acm, + const void * p_buf, + size_t length) +{ + app_usbd_class_inst_t const * p_inst = app_usbd_cdc_acm_class_inst_get(p_cdc_acm); + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + bool dtr_state = (p_cdc_acm_ctx->line_state & APP_USBD_CDC_ACM_LINE_STATE_DTR) ? + true : false; + if (!dtr_state) + { + /*Port is not opened*/ + return NRF_ERROR_INVALID_STATE; + } + + nrf_drv_usbd_ep_t ep = data_ep_in_addr_get(p_inst); + NRF_DRV_USBD_TRANSFER_IN(transfer, p_buf, length); + return app_usbd_ep_transfer(ep, &transfer); +} + +size_t app_usbd_cdc_acm_rx_size(app_usbd_cdc_acm_t const * p_cdc_acm) +{ + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + return p_cdc_acm_ctx->last_read; +} + +size_t app_usbd_cdc_acm_bytes_stored(app_usbd_cdc_acm_t const * p_cdc_acm) +{ + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + return p_cdc_acm_ctx->bytes_left; +} + +ret_code_t app_usbd_cdc_acm_read(app_usbd_cdc_acm_t const * p_cdc_acm, + void * p_buf, + size_t length) +{ + ASSERT(p_buf != NULL); + ret_code_t ret; + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + if (0U == (p_cdc_acm_ctx->line_state & APP_USBD_CDC_ACM_LINE_STATE_DTR)) + { + /*Port is not opened*/ + return NRF_ERROR_INVALID_STATE; + } + +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE == 0) + CRITICAL_REGION_ENTER(); +#endif // (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE == 0) + + if (p_cdc_acm_ctx->rx_transfer[0].p_buf == NULL) + { + if (p_cdc_acm_ctx->bytes_left >= length) + { + memcpy(p_buf, p_cdc_acm_ctx->p_copy_pos, length); + p_cdc_acm_ctx->bytes_left -= length; + p_cdc_acm_ctx->p_copy_pos += length; + p_cdc_acm_ctx->last_read = length; + ret = NRF_SUCCESS; + } + else + { + p_cdc_acm_ctx->rx_transfer[0].p_buf = p_buf; + p_cdc_acm_ctx->rx_transfer[0].read_left = length; + nrf_drv_usbd_ep_t ep = data_ep_out_addr_get(app_usbd_cdc_acm_class_inst_get(p_cdc_acm)); + nrf_drv_usbd_handler_desc_t const handler_desc = { + .handler.consumer = cdc_acm_consumer, + .p_context = p_cdc_acm_ctx + }; + + if (p_cdc_acm_ctx->bytes_left > 0) + { + memcpy(p_cdc_acm_ctx->rx_transfer[0].p_buf, + p_cdc_acm_ctx->p_copy_pos, + p_cdc_acm_ctx->bytes_left); + p_cdc_acm_ctx->rx_transfer[0].read_left -= p_cdc_acm_ctx->bytes_left; + p_cdc_acm_ctx->rx_transfer[0].p_buf += p_cdc_acm_ctx->bytes_left; + p_cdc_acm_ctx->bytes_read = p_cdc_acm_ctx->bytes_left; + p_cdc_acm_ctx->bytes_left = 0; + + } + ret = app_usbd_ep_handled_transfer(ep, &handler_desc); + if (ret == NRF_SUCCESS) + { + ret = NRF_ERROR_IO_PENDING; + } + } + } + else if (p_cdc_acm_ctx->rx_transfer[1].p_buf == NULL) + { + p_cdc_acm_ctx->rx_transfer[1].p_buf = p_buf; + p_cdc_acm_ctx->rx_transfer[1].read_left = length; + ret = NRF_ERROR_IO_PENDING; + } + else + { + ret = NRF_ERROR_BUSY; + } +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE == 0) + CRITICAL_REGION_EXIT(); +#endif // (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE == 0) + + return ret; +} + +ret_code_t app_usbd_cdc_acm_read_any(app_usbd_cdc_acm_t const * p_cdc_acm, + void * p_buf, + size_t length) +{ + ASSERT(p_buf != NULL); + ret_code_t ret; + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + if (0U == (p_cdc_acm_ctx->line_state & APP_USBD_CDC_ACM_LINE_STATE_DTR)) + { + /*Port is not opened*/ + return NRF_ERROR_INVALID_STATE; + } + +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE == 0) + CRITICAL_REGION_ENTER(); +#endif // (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE == 0) + if (p_cdc_acm_ctx->bytes_left > 0) + { + size_t to_copy = MIN(length, p_cdc_acm_ctx->bytes_left); + memcpy(p_buf, p_cdc_acm_ctx->p_copy_pos, to_copy); + p_cdc_acm_ctx->bytes_left -= to_copy; + p_cdc_acm_ctx->p_copy_pos += to_copy; + p_cdc_acm_ctx->last_read = to_copy; + ret = NRF_SUCCESS; + } + else + { + if (p_cdc_acm_ctx->rx_transfer[0].p_buf == NULL) + { + p_cdc_acm_ctx->rx_transfer[0].p_buf = p_buf; + p_cdc_acm_ctx->rx_transfer[0].read_left = length; + nrf_drv_usbd_ep_t ep = data_ep_out_addr_get(app_usbd_cdc_acm_class_inst_get(p_cdc_acm)); + nrf_drv_usbd_handler_desc_t const handler_desc = { + .handler.consumer = cdc_acm_single_shoot_consumer, + .p_context = p_cdc_acm_ctx + }; + + ret = app_usbd_ep_handled_transfer(ep, &handler_desc); + if (ret == NRF_SUCCESS) + { + ret = NRF_ERROR_IO_PENDING; + } + } + else + { + ret = NRF_ERROR_BUSY; + } + } + +#if (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE == 0) + CRITICAL_REGION_EXIT(); +#endif // (APP_USBD_CONFIG_EVENT_QUEUE_ENABLE == 0) + + return ret; +} + +static ret_code_t cdc_acm_serial_state_notify(app_usbd_cdc_acm_t const * p_cdc_acm) +{ + app_usbd_class_inst_t const * p_inst = app_usbd_cdc_acm_class_inst_get(p_cdc_acm); + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + nrf_drv_usbd_ep_t ep = comm_ep_in_addr_get(p_inst); + + NRF_DRV_USBD_TRANSFER_OUT(transfer, + &p_cdc_acm_ctx->request.payload, + sizeof(app_usbd_cdc_acm_notify_t)); + return app_usbd_ep_transfer(ep, &transfer); +} + +ret_code_t app_usbd_cdc_acm_serial_state_notify(app_usbd_cdc_acm_t const * p_cdc_acm, + app_usbd_cdc_acm_serial_state_t serial_state, + bool value) +{ + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + ret_code_t ret; + CRITICAL_REGION_ENTER(); + ret = NRF_SUCCESS; + switch (serial_state) + { + case APP_USBD_CDC_ACM_SERIAL_STATE_DCD: + case APP_USBD_CDC_ACM_SERIAL_STATE_DSR: + case APP_USBD_CDC_ACM_SERIAL_STATE_BREAK: + case APP_USBD_CDC_ACM_SERIAL_STATE_RING: + case APP_USBD_CDC_ACM_SERIAL_STATE_FRAMING: + case APP_USBD_CDC_ACM_SERIAL_STATE_PARITY: + case APP_USBD_CDC_ACM_SERIAL_STATE_OVERRUN: + + if (value) + { + p_cdc_acm_ctx->serial_state |= serial_state; + } + else + { + p_cdc_acm_ctx->serial_state &= ~serial_state; + } + + break; + default: + ret = NRF_ERROR_NOT_SUPPORTED; + break; + } + + if (ret == NRF_SUCCESS) + { + app_usbd_cdc_acm_notify_t * notify = &p_cdc_acm_ctx->request.payload.notify; + notify->cdc_notify.bmRequestType = app_usbd_setup_req_val(APP_USBD_SETUP_REQREC_INTERFACE, + APP_USBD_SETUP_REQTYPE_CLASS, + APP_USBD_SETUP_REQDIR_IN); + notify->cdc_notify.bmRequest = APP_USBD_CDC_NOTIF_SERIAL_STATE; + notify->cdc_notify.wValue = 0; + notify->cdc_notify.wIndex = 0; + notify->cdc_notify.wLength = sizeof(notify->serial_state); + + notify->serial_state = p_cdc_acm_ctx->serial_state; + + ret = cdc_acm_serial_state_notify(p_cdc_acm); + } + CRITICAL_REGION_EXIT(); + + return ret; +} + +ret_code_t app_usbd_cdc_acm_line_state_get(app_usbd_cdc_acm_t const * p_cdc_acm, + app_usbd_cdc_acm_line_state_t line_state, + uint32_t * value) +{ + app_usbd_cdc_acm_ctx_t * p_cdc_acm_ctx = cdc_acm_ctx_get(p_cdc_acm); + + ret_code_t ret; + CRITICAL_REGION_ENTER(); + ret = NRF_SUCCESS; + switch (line_state) + { + case APP_USBD_CDC_ACM_LINE_STATE_DTR: + case APP_USBD_CDC_ACM_LINE_STATE_RTS: + *value = (p_cdc_acm_ctx->line_state & line_state) != 0; + break; + default: + ret = NRF_ERROR_NOT_SUPPORTED; + break; + } + CRITICAL_REGION_EXIT(); + + return ret; +} + +#endif //NRF_MODULE_ENABLED(APP_USBD_CDC_ACM) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/acm/app_usbd_cdc_acm.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/acm/app_usbd_cdc_acm.h new file mode 100644 index 0000000..5af0226 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/acm/app_usbd_cdc_acm.h @@ -0,0 +1,362 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_CDC_ACM_H__ +#define APP_USBD_CDC_ACM_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdint.h> +#include <stdbool.h> + +#include "nrf_drv_usbd.h" +#include "app_usbd_class_base.h" +#include "app_usbd.h" +#include "app_usbd_core.h" +#include "app_usbd_descriptor.h" + +#include "app_usbd_cdc_desc.h" +#include "app_usbd_cdc_types.h" +#include "app_usbd_cdc_acm_internal.h" + +/** + * @defgroup app_usbd_cdc_acm USB CDC ACM class + * @ingroup app_usbd + * + * @brief @tagAPI52840 Module with types, definitions and API used by CDC ACM class. + * + * @details References: + * - "Universal Serial Bus Class Definitions for Communications Devices" + * Revision 1.2, November 3, 2010 + * - "Universal Serial Bus Communications Class Subclass Specification for PSTN Devices" + * Revision 1.2, February 9, 2007 + * + * @{ + */ + +#ifdef DOXYGEN +/** + * @brief CDC ACM class instance type + * + * @ref APP_USBD_CLASS_TYPEDEF + */ +typedef struct { } app_usbd_cdc_acm_t; +#else +/*lint -save -e10 -e26 -e123 -e505 */ +APP_USBD_CLASS_TYPEDEF(app_usbd_cdc_acm, \ + APP_USBD_CDC_ACM_CONFIG(0, 0, 0, 0, 0), \ + APP_USBD_CDC_ACM_INSTANCE_SPECIFIC_DEC, \ + APP_USBD_CDC_ACM_DATA_SPECIFIC_DEC \ +); +/*lint -restore*/ +#endif + + +/*lint -save -e407 */ + +/** + * @brief Events passed to user event handler. + * + * @note Example prototype of user event handler: + * + * @code + void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_cdc_acm_user_event_t event); + * @endcode + */ +typedef enum app_usbd_cdc_acm_user_event_e { + APP_USBD_CDC_ACM_USER_EVT_RX_DONE, /**< User event RX_DONE. */ + APP_USBD_CDC_ACM_USER_EVT_TX_DONE, /**< User event TX_DONE. */ + + APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN, /**< User event PORT_OPEN. */ + APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE, /**< User event PORT_CLOSE. */ +} app_usbd_cdc_acm_user_event_t; + +/*lint -restore*/ + +/** + * @brief Default CDC ACM descriptors. + * + * @param comm_interface COMM interface number. + * @param comm_epin COMM interface IN endpoint. + * @param data_interface DATA interface number. + * @param data_epin DATA interface IN endpoint. + * @param data_epout DATA interface OUT endpoint. + */ +#define APP_USBD_CDC_ACM_DEFAULT_DESC(comm_interface, \ + comm_epin, \ + data_interface, \ + data_epin, \ + data_epout) \ + APP_USBD_CDC_IAD_DSC(comm_interface, \ + APP_USBD_CDC_SUBCLASS_ACM, \ + APP_USBD_CDC_COMM_PROTOCOL_AT_V250) \ + APP_USBD_CDC_COMM_INTERFACE_DSC(comm_interface, \ + APP_USBD_CDC_SUBCLASS_ACM, \ + APP_USBD_CDC_COMM_PROTOCOL_AT_V250) \ + APP_USBD_CDC_HEADER_DSC(0x0110) \ + APP_USBD_CDC_CALL_MGMT_DSC(0x03, data_interface) \ + APP_USBD_CDC_ACM_DSC(0x02) \ + APP_USBD_CDC_UNION_DSC(comm_interface, data_interface) \ + APP_USBD_CDC_COM_EP_DSC(comm_epin, NRF_DRV_USBD_EPSIZE) \ + APP_USBD_CDC_DATA_INTERFACE_DSC(data_interface, 0, 0) \ + APP_USBD_CDC_DATA_EP_DSC(data_epin, data_epout, NRF_DRV_USBD_EPSIZE) + +/** + * @brief Global definition of app_usbd_cdc_acm_t class instance. + * + * @param instance_name Name of global instance. + * @param user_ev_handler User event handler (optional). + * @param comm_ifc Interface number of cdc_acm control. + * @param data_ifc Interface number of cdc_acm DATA. + * @param comm_ein COMM subclass IN endpoint. + * @param data_ein DATA subclass IN endpoint. + * @param data_eout DATA subclass OUT endpoint. + * @param cdc_protocol CDC protocol @ref app_usbd_cdc_comm_protocol_t + * + * @note This macro is just simplified version of @ref APP_USBD_CDC_ACM_GLOBAL_DEF_INTERNAL. + * + */ +#define APP_USBD_CDC_ACM_GLOBAL_DEF(instance_name, \ + user_ev_handler, \ + comm_ifc, \ + data_ifc, \ + comm_ein, \ + data_ein, \ + data_eout, \ + cdc_protocol) \ + APP_USBD_CDC_ACM_GLOBAL_DEF_INTERNAL(instance_name, \ + user_ev_handler, \ + comm_ifc, \ + data_ifc, \ + comm_ein, \ + data_ein, \ + data_eout, \ + cdc_protocol) \ + +/** + * @brief Helper function to get class instance from CDC ACM class. + * + * @param[in] p_cdc_acm CDC ACM class instance (defined by @ref APP_USBD_CDC_ACM_GLOBAL_DEF). + * + * @return Base class instance. + */ +static inline app_usbd_class_inst_t const * +app_usbd_cdc_acm_class_inst_get(app_usbd_cdc_acm_t const * p_cdc_acm) +{ + return &p_cdc_acm->base; +} + +/** + * @brief Helper function to get cdc_acm specific request from cdc_acm class. + * + * @param[in] p_cdc_acm CDC ACM class instance (defined by @ref APP_USBD_CDC_ACM_GLOBAL_DEF). + * + * @return CDC ACM class specific request. + */ +static inline app_usbd_cdc_acm_req_t * +app_usbd_cdc_acm_class_request_get(app_usbd_cdc_acm_t const * p_cdc_acm) +{ + return &p_cdc_acm->specific.p_data->ctx.request; +} + +/** + * @brief Helper function to get cdc_acm from base class instance. + * + * @param[in] p_inst Base class instance. + * + * @return CDC ACM class handle. + */ +static inline app_usbd_cdc_acm_t const * +app_usbd_cdc_acm_class_get(app_usbd_class_inst_t const * p_inst) +{ + return (app_usbd_cdc_acm_t const *)p_inst; +} + + +/** + * @brief Writes data to CDC ACM serial port. + * + * This is asynchronous call. User should wait for @ref APP_USBD_CDC_ACM_USER_EVT_TX_DONE event + * to be sure that all data has been sent and input buffer could be accessed again. + * + * @param[in] p_cdc_acm CDC ACM class instance (defined by @ref APP_USBD_CDC_ACM_GLOBAL_DEF). + * @param[in] p_buf Input buffer. + * @param[in] length Input buffer length. + * + * @return Standard error code. + */ +ret_code_t app_usbd_cdc_acm_write(app_usbd_cdc_acm_t const * p_cdc_acm, + const void * p_buf, + size_t length); + +/** + * @brief Returns the amount of data that was read. + * + * This function should be used on @ref APP_USBD_CDC_ACM_USER_EVT_RX_DONE event to get + * information how many bytes have been transfered into user buffer. + * + * @param[in] p_cdc_acm CDC ACM class instance (defined by @ref APP_USBD_CDC_ACM_GLOBAL_DEF). + * + * @return Amount of data transfered. + */ +size_t app_usbd_cdc_acm_rx_size(app_usbd_cdc_acm_t const * p_cdc_acm); + +/** + * @brief Returns the amount of data that was stored into internal buffer + * + * This function should be used on @ref APP_USBD_CDC_ACM_USER_EVT_RX_DONE event to get + * information how many bytes are waiting in internal buffer. + * + * @param[in] p_cdc_acm CDC ACM class instance (defined by @ref APP_USBD_CDC_ACM_GLOBAL_DEF). + * + * @return Amount of data waiting. + */ +size_t app_usbd_cdc_acm_bytes_stored(app_usbd_cdc_acm_t const * p_cdc_acm); + +/** + * @brief Reads data from CDC ACM serial port. + * + * This function uses internal buffer and double buffering for continuous transmission. + * + * If there is enough data in internal buffer to fill user buffer, NRF_SUCCESS is + * returned and data is immediately available in the user buffer. + * + * If not, up to two user buffers can be scheduled, function returns NRF_ERROR_IO_PENDING + * when first buffer is filled and @ref APP_USBD_CDC_ACM_USER_EVT_RX_DONE event is generated. + * + * @sa app_usbd_cdc_acm_read_any + * @sa app_usbd_cdc_acm_rx_size + * + * @param[in] p_cdc_acm CDC ACM class instance (defined by @ref APP_USBD_CDC_ACM_GLOBAL_DEF). + * @param[out] p_buf Output buffer. + * @param[in] length Number of bytes to read. + * + * @retval NRF_SUCCESS Data is stored into user buffer. + * @retval NRF_ERROR_IO_PENDING Awaiting transmission, when data is stored into user buffer, + * @ref APP_USBD_CDC_ACM_USER_EVT_RX_DONE event will be raised. + * @retval NRF_ERROR_BUSY There are already 2 buffers queued for transfers. + * @retval other Standard error code. + */ +ret_code_t app_usbd_cdc_acm_read(app_usbd_cdc_acm_t const * p_cdc_acm, + void * p_buf, + size_t length); + +/** + * @brief Read any data from CDC ACM port up to given buffer size + * + * This function is very similar to the @ref app_usbd_cdc_acm_read but it returns + * data as quick as any data is available, even if the given buffer was not totally full. + * + * @note This function cannot use double buffering. + * @note To check the number of bytes really read use @ref app_usbd_cdc_acm_rx_size + * function. + * + * @sa app_usbd_cdc_acm_read + * @sa app_usbd_cdc_acm_rx_size + * + * @param p_cdc_acm CDC ACM class instance (defined by @ref APP_USBD_CDC_ACM_GLOBAL_DEF). + * @param[out] p_buf Output buffer. + * @param[in] length Maximum number of bytes to read. + * + * @retval NRF_SUCCESS Data is stored into user buffer. + * @retval NRF_ERROR_IO_PENDING Awaiting transmission, when data is stored into user buffer, + * @ref APP_USBD_CDC_ACM_USER_EVT_RX_DONE event will be raised. + * @retval NRF_ERROR_BUSY There is already buffer set for a transfer. + * @retval other Standard error code. + */ +ret_code_t app_usbd_cdc_acm_read_any(app_usbd_cdc_acm_t const * p_cdc_acm, + void * p_buf, + size_t length); + +/** + * @brief Serial state notifications. + * */ +typedef enum { + APP_USBD_CDC_ACM_SERIAL_STATE_DCD = (1u << 0), /**< Notification bit DCD. */ + APP_USBD_CDC_ACM_SERIAL_STATE_DSR = (1u << 1), /**< Notification bit DSR. */ + APP_USBD_CDC_ACM_SERIAL_STATE_BREAK = (1u << 2), /**< Notification bit BREAK. */ + APP_USBD_CDC_ACM_SERIAL_STATE_RING = (1u << 3), /**< Notification bit RING. */ + APP_USBD_CDC_ACM_SERIAL_STATE_FRAMING = (1u << 4), /**< Notification bit FRAMING.*/ + APP_USBD_CDC_ACM_SERIAL_STATE_PARITY = (1u << 5), /**< Notification bit PARITY. */ + APP_USBD_CDC_ACM_SERIAL_STATE_OVERRUN = (1u << 6), /**< Notification bit OVERRUN.*/ +} app_usbd_cdc_acm_serial_state_t; + +/** + * @brief Serial line state. + */ +typedef enum { + APP_USBD_CDC_ACM_LINE_STATE_DTR = (1u << 0), /**< Line state bit DTR.*/ + APP_USBD_CDC_ACM_LINE_STATE_RTS = (1u << 1), /**< Line state bit RTS.*/ +} app_usbd_cdc_acm_line_state_t; + +/** + * @brief Serial state notification via IN interrupt endpoint. + * + * @param[in] p_cdc_acm CDC ACM class instance (defined by @ref APP_USBD_CDC_ACM_GLOBAL_DEF). + * @param[in] serial_state Serial state notification type. + * @param[in] value Serial state value. + * + * @return Standard error code. + */ +ret_code_t app_usbd_cdc_acm_serial_state_notify(app_usbd_cdc_acm_t const * p_cdc_acm, + app_usbd_cdc_acm_serial_state_t serial_state, + bool value); + +/** + * @brief Control line value get. + * + * @param[in] p_cdc_acm CDC ACM class instance (defined by @ref APP_USBD_CDC_ACM_GLOBAL_DEF). + * @param[in] line_state Line control value type. + * @param[out] value Line control value. + * + * @return Standard error code. + */ +ret_code_t app_usbd_cdc_acm_line_state_get(app_usbd_cdc_acm_t const * p_cdc_acm, + app_usbd_cdc_acm_line_state_t line_state, + uint32_t * value); + +/** @} */ +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_CDC_ACM_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/acm/app_usbd_cdc_acm_internal.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/acm/app_usbd_cdc_acm_internal.h new file mode 100644 index 0000000..5e09249 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/acm/app_usbd_cdc_acm_internal.h @@ -0,0 +1,268 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_CDC_ACM_INTERNAL_H__ +#define APP_USBD_CDC_ACM_INTERNAL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + + +#include "app_util.h" + +/** + * @defgroup app_usbd_cdc_acm_internal USB CDC ACM internals + * @ingroup app_usbd_cdc_acm + * @brief @tagAPI52840 Internals of the USB ACM class implementation. + * @{ + */ + +/** + * @brief Forward declaration of type defined by @ref APP_USBD_CLASS_TYPEDEF in cdc_acm class. + * + */ +APP_USBD_CLASS_FORWARD(app_usbd_cdc_acm); + +/*lint -save -e165*/ +/** + * @brief Forward declaration of @ref app_usbd_cdc_acm_user_event_e. + * + */ +enum app_usbd_cdc_acm_user_event_e; + +/*lint -restore*/ + +/** + * @brief User event handler. + * + * @param[in] p_inst Class instance. + * @param[in] event User event. + * + */ +typedef void (*app_usbd_cdc_acm_user_ev_handler_t)(app_usbd_class_inst_t const * p_inst, + enum app_usbd_cdc_acm_user_event_e event); + +/** + * @brief CDC ACM class part of class instance data. + */ +typedef struct { + uint8_t comm_interface; //!< Interface number of cdc_acm control. + uint8_t comm_epin; //!< COMM subclass IN endpoint. + uint8_t data_interface; //!< Interface number of cdc_acm DATA. + uint8_t data_epout; //!< DATA subclass OUT endpoint. + uint8_t data_epin; //!< DATA subclass IN endpoint. + + app_usbd_cdc_comm_protocol_t protocol; //!< User specified CDC protocol. + + app_usbd_cdc_acm_user_ev_handler_t user_ev_handler; //!< User event handler. +} app_usbd_cdc_acm_inst_t; + + +/** + * @brief CDC ACM serial state class notify + */ +typedef struct { + app_usbd_cdc_notify_t cdc_notify; //!< CDC notify. + uint16_t serial_state; //!< Serial port state. +} app_usbd_cdc_acm_notify_t; + +/** + * @brief CDC ACM class specific request handled via control endpoint. + */ +typedef struct { + uint8_t type; //!< Request type. + uint8_t len; //!< Request length. + + union { + app_usbd_cdc_line_coding_t line_coding; //!< CDC ACM current line coding. + app_usbd_cdc_acm_notify_t notify; //!< CDC ACM class notify. + } payload; +} app_usbd_cdc_acm_req_t; + +/** + * @brief CDC ACM rx transfer buffer + */ +typedef struct { + uint8_t * p_buf; //!< User buffer pointer. + size_t read_left; //!< Bytes left to read into buffer. +} cdc_rx_buffer_t; + +/** + * @brief CDC ACM class context + */ +typedef struct { + app_usbd_cdc_acm_req_t request; //!< CDC ACM class request. + app_usbd_cdc_line_coding_t line_coding; //!< CDC ACM current line coding. + + uint16_t line_state; //!< CDC ACM line state bitmap, DTE side. + uint16_t serial_state; //!< CDC ACM serial state bitmap, DCE side. + + cdc_rx_buffer_t rx_transfer[2]; //!< User receive transfers. + + uint8_t internal_rx_buf[NRF_DRV_USBD_EPSIZE]; //!< Internal receive buffer. + uint8_t * p_copy_pos; //!< Current copy position from internal buffer. + + size_t bytes_left; //!< Bytes left in internal buffer to copy. + size_t bytes_read; //!< Bytes currently written to user buffer. + size_t last_read; //!< Bytes read in last transfer. + size_t cur_read; //!< Bytes currently read to internal buffer. +} app_usbd_cdc_acm_ctx_t; + + +/** + * @brief CDC ACM class configuration macro. + * + * Used by @ref APP_USBD_CDC_ACM_GLOBAL_DEF + * + * @param iface_comm Interface number of cdc_acm control. + * @param epin_comm COMM subclass IN endpoint. + * @param iface_data Interface number of cdc_acm DATA. + * @param epin_data DATA subclass IN endpoint. + * @param epout_data DATA subclass OUT endpoint. + * + */ +#define APP_USBD_CDC_ACM_CONFIG(iface_comm, epin_comm, iface_data, epin_data, epout_data) \ + ((iface_comm, epin_comm), \ + (iface_data, epin_data, epout_data)) + + +/** + * @brief Specific class constant data for cdc_acm class. + * + * @ref app_usbd_cdc_acm_inst_t + */ +#define APP_USBD_CDC_ACM_INSTANCE_SPECIFIC_DEC app_usbd_cdc_acm_inst_t inst; + + +/** + * @brief Configures cdc_acm class instance. + * + * @param user_event_handler User event handler. + * @param comm_ifc Interface number of cdc_acm control. + * @param comm_ein COMM subclass IN endpoint. + * @param data_ifc Interface number of cdc_acm DATA. + * @param data_ein DATA subclass IN endpoint. + * @param data_eout DATA subclass OUT endpoint. + * @param cdc_protocol CDC protocol. + */ +#define APP_USBD_CDC_ACM_INST_CONFIG(user_event_handler, comm_ifc, comm_ein, data_ifc, data_ein, \ + data_eout, cdc_protocol) \ + .inst = { \ + .user_ev_handler = user_event_handler, \ + .comm_interface = comm_ifc, \ + .comm_epin = comm_ifc, \ + .data_interface = data_ifc, \ + .data_epin = data_ein, \ + .data_epout = data_eout, \ + .protocol = cdc_protocol \ + } + +/** + * @brief Specific class data for cdc_acm class. + * + * @ref app_usbd_cdc_acm_ctx_t + */ +#define APP_USBD_CDC_ACM_DATA_SPECIFIC_DEC app_usbd_cdc_acm_ctx_t ctx; + + +/** + * @brief CDC ACM class descriptors config macro. + * + * @param interface_number Interface number. + * @param ... Extracted endpoint list. + */ +#define APP_USBD_CDC_ACM_DSC_CONFIG(interface_number, ...) { \ + APP_USBD_CDC_ACM_INTERFACE_DSC(interface_number, \ + 0, \ + 0, \ + APP_USBD_CDC_ACM_SUBCLASS_CDC_ACMCONTROL) \ +} + +/** + * @brief Public cdc_acm class interface. + * + */ +extern const app_usbd_class_methods_t app_usbd_cdc_acm_class_methods; + +/** + * @brief Global definition of @ref app_usbd_cdc_acm_t class. + * + * @param instance_name Name of global instance. + * @param user_ev_handler User event handler. + * @param comm_ifc Interface number of cdc_acm control. + * @param data_ifc Interface number of cdc_acm DATA. + * @param comm_ein COMM subclass IN endpoint. + * @param data_ein DATA subclass IN endpoint. + * @param data_eout DATA subclass OUT endpoint. + * @param cdc_protocol CDC protocol @ref app_usbd_cdc_comm_protocol_t + */ +/*lint -save -emacro(26 64 123 505 651, APP_USBD_CDC_ACM_GLOBAL_DEF_INTERNAL)*/ +#define APP_USBD_CDC_ACM_GLOBAL_DEF_INTERNAL(instance_name, \ + user_ev_handler, \ + comm_ifc, \ + data_ifc, \ + comm_ein, \ + data_ein, \ + data_eout, \ + cdc_protocol) \ + APP_USBD_CLASS_INST_GLOBAL_DEF( \ + instance_name, \ + app_usbd_cdc_acm, \ + &app_usbd_cdc_acm_class_methods, \ + APP_USBD_CDC_ACM_CONFIG(comm_ifc, comm_ein, data_ifc, data_ein, data_eout), \ + (APP_USBD_CDC_ACM_INST_CONFIG(user_ev_handler, \ + comm_ifc, \ + comm_ein, \ + data_ifc, \ + data_ein, \ + data_eout, \ + cdc_protocol)) \ + ) +/*lint -restore*/ + +/** @} */ + + + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_CDC_ACM_INTERNAL_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/app_usbd_cdc_desc.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/app_usbd_cdc_desc.h new file mode 100644 index 0000000..091065d --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/app_usbd_cdc_desc.h @@ -0,0 +1,208 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_CDC_DESC_H__ +#define APP_USBD_CDC_DESC_H__ + +#include <stdint.h> +#include <stdbool.h> + +#include "app_usbd_descriptor.h" +#include "app_usbd_cdc_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_cdc_desc CDC class descriptors + * @brief @tagAPI52840 Descriptors used in the USB CDC class implementation. + * @ingroup app_usbd_cdc_acm + * + * A group of macros used to initialize CDC descriptors + * @{ + */ + +/** + * @brief Initializer of IAD descriptor for CDC class. + * + * @param interface_number Interface number. + * @param subclass Subclass, @ref app_usbd_cdc_subclass_t. + * @param protocol Protocol, @ref app_usbd_cdc_comm_protocol_t. + */ +#define APP_USBD_CDC_IAD_DSC(interface_number, subclass, protocol) \ + /*.bLength = */ sizeof(app_usbd_descriptor_iad_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_INTERFACE_ASSOCIATION, \ + /*.bFirstInterface = */ interface_number, \ + /*.bInterfaceCount = */ 2, \ + /*.bFunctionClass = */ APP_USBD_CDC_COMM_CLASS, \ + /*.bFunctionSubClass = */ subclass, \ + /*.bFunctionProtocol = */ protocol, \ + /*.iFunction = */ 0, \ + +/** + * @brief Initializer of interface descriptor for CDC COMM class. + * + * @param interface_number Interface number. + * @param subclass Subclass, @ref app_usbd_cdc_subclass_t. + * @param protocol Protocol, @ref app_usbd_cdc_comm_protocol_t. + */ +#define APP_USBD_CDC_COMM_INTERFACE_DSC(interface_number, subclass, protocol) \ + /*.bLength = */ sizeof(app_usbd_descriptor_iface_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_INTERFACE, \ + /*.bInterfaceNumber = */ interface_number, \ + /*.bAlternateSetting = */ 0x00, \ + /*.bNumEndpoints = */ 1, \ + /*.bInterfaceClass = */ APP_USBD_CDC_COMM_CLASS, \ + /*.bInterfaceSubClass = */ subclass, \ + /*.bInterfaceProtocol = */ protocol, \ + /*.iInterface = 0, */ 0x00, \ + + +/** + * @brief Initializer of interface descriptor for CDC DATA class. + * + * @param interface_number Interface number. + * @param subclass Subclass, @ref app_usbd_cdc_subclass_t. + * @param protocol Protocol, @ref app_usbd_cdc_data_protocol_t. + */ +#define APP_USBD_CDC_DATA_INTERFACE_DSC(interface_number, subclass, protocol) \ + /*.bLength = */ sizeof(app_usbd_descriptor_iface_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_INTERFACE, \ + /*.bInterfaceNumber = */ interface_number, \ + /*.bAlternateSetting = */ 0x00, \ + /*.bNumEndpoints = */ 2, \ + /*.bInterfaceClass = */ APP_USBD_CDC_DATA_CLASS, \ + /*.bInterfaceSubClass = */ subclass, \ + /*.bInterfaceProtocol = */ protocol, \ + /*.iInterface = 0, */ 0x00, \ + + + +/** + * @brief Initializer of endpoint descriptor for CDC COM class. + * + * @param endpoint_in IN endpoint. + * @param ep_size Endpoint size. + */ +#define APP_USBD_CDC_COM_EP_DSC(endpoint_in, ep_size) \ + /*.bLength = */ sizeof(app_usbd_descriptor_ep_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_ENDPOINT, \ + /*.bEndpointAddress = */ endpoint_in, \ + /*.bmAttributes = */ APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_INTERRUPT, \ + /*.wMaxPacketSize = */ APP_USBD_U16_TO_RAW_DSC(ep_size), \ + /*.bInterval = */ 16, \ + +/** + * @brief Initializer of endpoint descriptors for CDC DATA class. + * + * @param endpoint_in IN endpoint. + * @param endpoint_out OUT endpoint. + * @param ep_size Endpoint size. + */ +#define APP_USBD_CDC_DATA_EP_DSC(endpoint_in, endpoint_out, ep_size) \ + /*.bLength = */ sizeof(app_usbd_descriptor_ep_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_ENDPOINT, \ + /*.bEndpointAddress = */ endpoint_in, \ + /*.bmAttributes = */ APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_BULK, \ + /*.wMaxPacketSize = */ APP_USBD_U16_TO_RAW_DSC(ep_size), \ + /*.bInterval = */ 0, \ + /*.bLength = */ sizeof(app_usbd_descriptor_ep_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_ENDPOINT, \ + /*.bEndpointAddress = */ endpoint_out, \ + /*.bmAttributes = */ APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_BULK, \ + /*.wMaxPacketSize = */ APP_USBD_U16_TO_RAW_DSC(ep_size), \ + /*.bInterval = */ 0, \ + +/** + * @brief Initializer of endpoint descriptors for CDC header descriptor. + * + * @param bcd_cdc BCD CDC version. + */ +#define APP_USBD_CDC_HEADER_DSC(bcd_cdc) \ + /*.bLength = */ sizeof(app_usbd_cdc_desc_header_t), \ + /*.bDescriptorType = */ APP_USBD_CDC_CS_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_CDC_SCS_HEADER, \ + /*.bcdCDC = */ APP_USBD_U16_TO_RAW_DSC(bcd_cdc), \ + +/** + * @brief Initializer of endpoint descriptors for CDC call management descriptor. + * + * @param capabilities Capabilities. + * @param data_interface Data interface. + */ +#define APP_USBD_CDC_CALL_MGMT_DSC(capabilities, data_interface) \ + /*.bLength = */ sizeof(app_usbd_cdc_desc_call_mgmt_t), \ + /*.bDescriptorType = */ APP_USBD_CDC_CS_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_CDC_SCS_CALL_MGMT, \ + /*.bmCapabilities = */ capabilities, \ + /*.bDataInterface = */ data_interface, \ + + +/** + * @brief Initializer of endpoint descriptors for CDC DATA class. + * + * @param capabilities Capabilities. + */ +#define APP_USBD_CDC_ACM_DSC(capabilities) \ + /*.bLength = */ sizeof(app_usbd_cdc_desc_acm_t), \ + /*.bDescriptorType = */ APP_USBD_CDC_CS_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_CDC_SCS_ACM, \ + /*.bmCapabilities = */ capabilities, \ + +/** + * @brief Initializer of endpoint descriptors for CDC DATA class. + * + * @param control_interface Control interface. + * @param ... Subordinate interfaces list. + */ +#define APP_USBD_CDC_UNION_DSC(control_interface, ...) \ + /*.bLength = */ sizeof(app_usbd_cdc_desc_union_t) + (NUM_VA_ARGS(__VA_ARGS__)), \ + /*.bDescriptorType = */ APP_USBD_CDC_CS_INTERFACE, \ + /*.bDescriptorSubtype = */ APP_USBD_CDC_SCS_UNION, \ + /*.bControlInterface = */ control_interface, \ + /*.bSubordinateInterface = */ __VA_ARGS__, \ + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_CDC_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/app_usbd_cdc_types.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/app_usbd_cdc_types.h new file mode 100644 index 0000000..9b8a482 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/cdc/app_usbd_cdc_types.h @@ -0,0 +1,360 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_CDC_TYPES_H__ +#define APP_USBD_CDC_TYPES_H__ + +#include <stdint.h> +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @defgroup app_usbd_cdc_types CDC class types + * @ingroup app_usbd_cdc_acm + * + * @brief @tagAPI52840 Variable types used by the CDC class implementation. + * @{ + */ + +/** + * @brief Communications Interface Class code. + * + * Used for control interface in communication class. + * @ref app_usbd_descriptor_iface_t::bInterfaceClass + */ +#define APP_USBD_CDC_COMM_CLASS 0x02 + +/** + * @brief Data Class Interface code. + * + * Used for data interface in communication class. + * @ref app_usbd_descriptor_iface_t::bInterfaceClass + */ +#define APP_USBD_CDC_DATA_CLASS 0x0A + +/** + * @brief CDC subclass possible values. + * + * @ref app_usbd_descriptor_iface_t::bInterfaceSubClass + */ +typedef enum { + APP_USBD_CDC_SUBCLASS_RESERVED = 0x00, /**< Reserved in documentation. */ + APP_USBD_CDC_SUBCLASS_DLCM = 0x01, /**< Direct Line Control Model. */ + APP_USBD_CDC_SUBCLASS_ACM = 0x02, /**< Abstract Control Model. */ + APP_USBD_CDC_SUBCLASS_TCM = 0x03, /**< Telephone Control Model. */ + APP_USBD_CDC_SUBCLASS_MCCM = 0x04, /**< Multi-Channel Control Model. */ + APP_USBD_CDC_SUBCLASS_CAPI = 0x05, /**< CAPI Control Model. */ + APP_USBD_CDC_SUBCLASS_ENCM = 0x06, /**< Ethernet Networking Control Model. */ + APP_USBD_CDC_SUBCLASS_ATM = 0x07, /**< ATM Networking Control Model. */ + APP_USBD_CDC_SUBCLASS_WHCM = 0x08, /**< Wireless Handset Control Model. */ + APP_USBD_CDC_SUBCLASS_DM = 0x09, /**< Device Management. */ + APP_USBD_CDC_SUBCLASS_MDLM = 0x0A, /**< Mobile Direct Line Model. */ + APP_USBD_CDC_SUBCLASS_OBEX = 0x0B, /**< OBEX. */ + APP_USBD_CDC_SUBCLASS_EEM = 0x0C, /**< Ethernet Emulation Model. */ + APP_USBD_CDC_SUBCLASS_NCM = 0x0D /**< Network Control Model. */ +} app_usbd_cdc_subclass_t; + +/** + * @brief CDC protocol possible values. + * + * @ref app_usbd_descriptor_iface_t::bInterfaceProtocol + */ +typedef enum { + APP_USBD_CDC_COMM_PROTOCOL_NONE = 0x00, /**< No class specific protocol required. */ + APP_USBD_CDC_COMM_PROTOCOL_AT_V250 = 0x01, /**< AT Commands: V.250 etc. */ + APP_USBD_CDC_COMM_PROTOCOL_AT_PCCA101 = 0x02, /**< AT Commands defined by PCCA-101. */ + APP_USBD_CDC_COMM_PROTOCOL_AT_PCCA101_ANNEXO = 0x03, /**< AT Commands defined by PCCA-101 & Annex O. */ + APP_USBD_CDC_COMM_PROTOCOL_AT_GSM707 = 0x04, /**< AT Commands defined by GSM 07.07. */ + APP_USBD_CDC_COMM_PROTOCOL_AT_3GPP_27007 = 0x05, /**< AT Commands defined by 3GPP 27.007. */ + APP_USBD_CDC_COMM_PROTOCOL_AT_CDMA = 0x06, /**< AT Commands defined by TIA for CDMA. */ + APP_USBD_CDC_COMM_PROTOCOL_EEM = 0x07, /**< Ethernet Emulation Model. */ + APP_USBD_CDC_COMM_PROTOCOL_EXTERNAL = 0xFE, /**< External Protocol: Commands defined by Command Set Functional Descriptor. */ + APP_USBD_CDC_COMM_PROTOCOL_VENDOR = 0xFF /**< Vendor-specific. */ +} app_usbd_cdc_comm_protocol_t; + +/** + * @brief CDC data interface protocols possible values. + */ +typedef enum { + APP_USBD_CDC_DATA_PROTOCOL_NONE = 0x00, /**< No class specific protocol required. */ + APP_USBD_CDC_DATA_PROTOCOL_NTB = 0x01, /**< Network Transfer Block. */ + APP_USBD_CDC_DATA_PROTOCOL_ISDN_BRI = 0x30, /**< Physical interface protocol for ISDN BRI. */ + APP_USBD_CDC_DATA_PROTOCOL_HDLC = 0x31, /**< HDLC. */ + APP_USBD_CDC_DATA_PROTOCOL_TRANSPARENT = 0x32, /**< Transparent. */ + APP_USBD_CDC_DATA_PROTOCOL_Q921M = 0x50, /**< Management protocol for Q.921 data link protocol. */ + APP_USBD_CDC_DATA_PROTOCOL_Q921 = 0x51, /**< Data link protocol for Q.921. */ + APP_USBD_CDC_DATA_PROTOCOL_Q921TM = 0x52, /**< TEI-multiplexor for Q.921 data link protocol. */ + APP_USBD_CDC_DATA_PROTOCOL_V42BIS = 0x90, /**< Data compression procedures. */ + APP_USBD_CDC_DATA_PROTOCOL_Q931 = 0x91, /**< Euro-ISDN protocol control. */ + APP_USBD_CDC_DATA_PROTOCOL_V120 = 0x92, /**< V.24 rate adaptation to ISDN. */ + APP_USBD_CDC_DATA_PROTOCOL_CAPI20 = 0x93, /**< CAPI Commands. */ + APP_USBD_CDC_DATA_PROTOCOL_HOST = 0xFD, /**< Host based driver. + * @note This protocol code should only be used in messages + * between host and device to identify the host driver portion + * of a protocol stack. + */ + APP_USBD_CDC_DATA_PROTOCOL_EXTERNAL = 0xFE, /**< The protocol(s) are described using a Protocol Unit Functional + * Descriptors on Communications Class Interface. + */ + APP_USBD_CDC_DATA_PROTOCOL_VENDOR = 0xFF /**< Vendor-specific. */ +} app_usbd_cdc_data_protocol_t; + +/** + * @brief CDC Functional Descriptor types. + */ +typedef enum { + APP_USBD_CDC_CS_INTERFACE = 0x24, /**< Class specific interface descriptor type.*/ + APP_USBD_CDC_CS_ENDPOINT = 0x25 /**< Class specific endpoint descriptor type.*/ +} app_usbd_cdc_func_type_t; + +/** + * @brief CDC Functional Descriptor subtypes + */ +typedef enum { + APP_USBD_CDC_SCS_HEADER = 0x00, /**< Header Functional Descriptor, which marks the beginning of the concatenated set of functional descriptors for the interface. */ + APP_USBD_CDC_SCS_CALL_MGMT = 0x01, /**< Call Management Functional Descriptor. */ + APP_USBD_CDC_SCS_ACM = 0x02, /**< Abstract Control Management Functional Descriptor. */ + APP_USBD_CDC_SCS_DLM = 0x03, /**< Direct Line Management Functional Descriptor. */ + APP_USBD_CDC_SCS_TEL_R = 0x04, /**< Telephone Ringer Functional Descriptor. */ + APP_USBD_CDC_SCS_TEL_CAP = 0x05, /**< Telephone Call and Line State Reporting Capabilities Functional Descriptor. */ + APP_USBD_CDC_SCS_UNION = 0x06, /**< Union Functional Descriptor. */ + APP_USBD_CDC_SCS_COUNTRY_SEL = 0x07, /**< Country Selection Functional Descriptor. */ + APP_USBD_CDC_SCS_TEL_OM = 0x08, /**< Telephone Operational Modes Functional Descriptor. */ + APP_USBD_CDC_SCS_USB_TERM = 0x09, /**< USB Terminal Functional Descriptor. */ + APP_USBD_CDC_SCS_NCT = 0x0A, /**< Network Channel Terminal Descriptor. */ + APP_USBD_CDC_SCS_PU = 0x0B, /**< Protocol Unit Functional Descriptor. */ + APP_USBD_CDC_SCS_EU = 0x0C, /**< Extension Unit Functional Descriptor. */ + APP_USBD_CDC_SCS_MCM = 0x0D, /**< Multi-Channel Management Functional Descriptor. */ + APP_USBD_CDC_SCS_CAPI = 0x0E, /**< CAPI Control Management Functional Descriptor. */ + APP_USBD_CDC_SCS_ETH = 0x0F, /**< Ethernet Networking Functional Descriptor. */ + APP_USBD_CDC_SCS_ATM = 0x10, /**< ATM Networking Functional Descriptor. */ + APP_USBD_CDC_SCS_WHCM = 0x11, /**< Wireless Handset Control Model Functional Descriptor. */ + APP_USBD_CDC_SCS_MDLM = 0x12, /**< Mobile Direct Line Model Functional Descriptor. */ + APP_USBD_CDC_SCS_MDLM_DET = 0x13, /**< MDLM Detail Functional Descriptor. */ + APP_USBD_CDC_SCS_DMM = 0x14, /**< Device Management Model Functional Descriptor. */ + APP_USBD_CDC_SCS_OBEX = 0x15, /**< OBEX Functional Descriptor. */ + APP_USBD_CDC_SCS_CS = 0x16, /**< Command Set Functional Descriptor. */ + APP_USBD_CDC_SCS_CS_DET = 0x17, /**< Command Set Detail Functional Descriptor. */ + APP_USBD_CDC_SCS_TEL_CM = 0x18, /**< Telephone Control Model Functional Descriptor. */ + APP_USBD_CDC_SCS_OBEX_SI = 0x19, /**< OBEX Service Identifier Functional Descriptor. */ + APP_USBD_CDC_SCS_NCM = 0x1A /**< NCM Functional Descriptor. */ +} app_usbd_cdc_func_subtype_t; + +/* Make all descriptors packed */ +#pragma pack(push, 1) + +/** + * @brief Header Functional Descriptor + */ +typedef struct { + uint8_t bFunctionLength; //!< Size of this descriptor in bytes. + uint8_t bDescriptorType; //!< @ref APP_USBD_CDC_CS_INTERFACE descriptor type. + uint8_t bDescriptorSubtype; //!< Descriptor subtype @ref APP_USBD_CDC_SCS_HEADER. + uint8_t bcdCDC[2]; //!< USB Class Definitions for Communications Devices Specification release number in binary-coded decimal. +} app_usbd_cdc_desc_header_t; + +/** + * @brief Call management capabilities. + * + * @ref app_usbd_cdc_desc_call_mgmt_t::bmCapabilities bit + * */ +typedef enum { + APP_USBD_CDC_CALL_MGMT_SUPPORTED = (1 << 0), /**< Call management capability bit 0.*/ + APP_USBD_CDC_CALL_MGMT_OVER_DCI = (1 << 1), /**< Call management capability bit 1.*/ +} app_subd_cdc_call_mgmt_cap_t; + +/** + * @brief CDC Call Management Functional Descriptor. + */ +typedef struct { + uint8_t bFunctionLength; //!< Size of this functional descriptor, in bytes. + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_CDC_CS_INTERFACE. + uint8_t bDescriptorSubtype; //!< Descriptor subtype @ref APP_USBD_CDC_SCS_CALL_MGMT. + uint8_t bmCapabilities; //!< Capabilities @ref app_subd_cdc_call_mgmt_cap_t. + uint8_t bDataInterface; //!< Data interface number. +} app_usbd_cdc_desc_call_mgmt_t; + +/** + * @brief ACM capabilities. + * + * @ref app_usbd_cdc_desc_acm_t::bmCapabilities bit + * */ +typedef enum { + APP_USBD_CDC_ACM_FEATURE_REQUESTS = (1 << 0), /**< ACM capability bit FEATURE_REQUESTS. */ + APP_USBD_CDC_ACM_LINE_REQUESTS = (1 << 1), /**< ACM capability bit LINE_REQUESTS. */ + APP_USBD_CDC_ACM_SENDBREAK_REQUESTS = (1 << 2), /**< ACM capability bit SENDBREAK_REQUESTS.*/ + APP_USBD_CDC_ACM_NOTIFY_REQUESTS = (1 << 3), /**< ACM capability bit NOTIFY_REQUESTS. */ +} app_subd_cdc_acm_cap_t; + +/** + * @brief CDC ACM Functional Descriptor. + */ +typedef struct { + uint8_t bFunctionLength; //!< Size of this functional descriptor, in bytes. + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_CDC_CS_INTERFACE. + uint8_t bDescriptorSubtype; //!< Descriptor subtype @ref APP_USBD_CDC_SCS_ACM. + uint8_t bmCapabilities; //!< Capabilities @ref app_subd_cdc_acm_cap_t. +} app_usbd_cdc_desc_acm_t; + +/** + * @brief Union Functional Descriptor. + */ +typedef struct { + uint8_t bFunctionLength; //!< Size of this functional descriptor, in bytes. + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_CDC_CS_INTERFACE. + uint8_t bDescriptorSubtype; //!< Descriptor subtype @ref APP_USBD_CDC_SCS_UNION. + uint8_t bControlInterface; //!< The interface number of the Communications or Data Class interface, designated as the controlling interface for the union. + uint8_t bSubordinateInterface[]; //!< Interface number of subordinate interfaces in the union. Number of interfaced depends on descriptor size. +} app_usbd_cdc_desc_union_t; + +/** + * @brief Country Selection Functional Descriptor. + */ +typedef struct { + uint8_t bFunctionLength; //!< Size of this functional descriptor, in bytes. + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_CDC_CS_INTERFACE. + uint8_t bDescriptorSubtype; //!< Descriptor subtype @ref APP_USBD_CDC_SCS_COUNTRY_SEL. + uint8_t iCountryCodeRelDate; //!< Index of a string giving the release date for the implemented ISO 3166 Country Codes. +} app_usbd_cdc_desc_country_sel_t; + +/** + * @brief CDC Requests + * + */ +typedef enum { + /* CDC General */ + APP_USBD_CDC_REQ_SEND_ENCAPSULATED_COMMAND = 0x00, /**< This request is used to issue a command in the format of the supported control protocol of the Communications Class interface. */ + APP_USBD_CDC_REQ_GET_ENCAPSULATED_RESPONSE = 0x01, /**< This request is used to request a response in the format of the supported control protocol of the Communications Class interface. */ + /* CDC PSTN */ + APP_USBD_CDC_REQ_SET_COMM_FEATURE = 0x02, /**< This request controls the settings for a particular communications feature of a particular target. */ + APP_USBD_CDC_REQ_GET_COMM_FEATURE = 0x03, /**< This request returns the current settings for the communications feature as selected. */ + APP_USBD_CDC_REQ_CLEAR_COMM_FEATURE = 0x04, /**< This request controls the settings for a particular communications feature of a particular target, setting the selected feature to its default state. */ + APP_USBD_CDC_REQ_SET_AUX_LINE_STATE = 0x10, /**< This request is used to connect or disconnect a secondary jack to POTS circuit or CODEC, depending on hook state. */ + APP_USBD_CDC_REQ_SET_HOOK_STATE = 0x11, /**< This request is used to set the necessary PSTN line relay code for on-hook, off-hook, and caller ID states. */ + APP_USBD_CDC_REQ_PULSE_SETUP = 0x12, /**< This request is used to prepare for a pulse-dialing cycle. */ + APP_USBD_CDC_REQ_SEND_PULSE = 0x13, /**< This request is used to generate a specified number of make/break pulse cycles. */ + APP_USBD_CDC_REQ_SET_PULSE_TIME = 0x14, /**< This request sets the timing of the make and break periods for pulse dialing. */ + APP_USBD_CDC_REQ_RING_AUX_JACK = 0x15, /**< This request is used to generate a ring signal on a secondary phone jack. */ + APP_USBD_CDC_REQ_SET_LINE_CODING = 0x20, /**< This request allows the host to specify typical asynchronous line-character formatting properties. */ + APP_USBD_CDC_REQ_GET_LINE_CODING = 0x21, /**< This request allows the host to find out the currently configured line coding. */ + APP_USBD_CDC_REQ_SET_CONTROL_LINE_STATE = 0x22, /**< This request generates RS-232/V.24 style control signals. */ + APP_USBD_CDC_REQ_SEND_BREAK = 0x23, /**< This request sends special carrier modulation that generates an RS-232 style break. */ + APP_USBD_CDC_REQ_SET_RINGER_PARMS = 0x30, /**< This request configures the ringer for the communications device. */ + APP_USBD_CDC_REQ_GET_RINGER_PARMS = 0x31, /**< This request returns the ringer capabilities of the device and the current status of the device’s ringer. */ + APP_USBD_CDC_REQ_SET_OPERATION_PARMS = 0x32, /**< Sets the operational mode for the device, between a simple mode, standalone mode and a host centric mode. */ + APP_USBD_CDC_REQ_GET_OPERATION_PARMS = 0x33, /**< This request gets the current operational mode for the device. */ + APP_USBD_CDC_REQ_SET_LINE_PARMS = 0x34, /**< This request is used to change the state of the line, corresponding to the interface or master interface of a union to which the command was sent. */ + APP_USBD_CDC_REQ_GET_LINE_PARMS = 0x35, /**< This request is used to report the state of the line that corresponds to the interface or master interface of a union to which the command was sent. */ + APP_USBD_CDC_REQ_DIAL_DIGITS = 0x36, /**< This request dials the DTMF digits over the specified line. */ +} app_usbd_cdc_req_id_t; + +/** + * @brief CDC Notifications. + */ +typedef enum { + /* CDC General */ + APP_USBD_CDC_NOTIF_NETWORK_CONNECTION = 0x00, /**< This notification allows the device to notify the host about network connection status. */ + APP_USBD_CDC_NOTIF_RESPONSE_AVAILABLE = 0x01, /**< This notification allows the device to notify the host that a response is available. + * This response can be retrieved with a subsequent GetEncapsulatedResponse request. + _ */ + APP_USBD_CDC_NOTIF_CONNECTION_SPEED_CHANGE = 0x2A, /**< This notification allows the device to inform the host-networking driver + * that a change in either the up-link or the down-link bit rate of the connection has occurred. + */ + /* CDC PSTN */ + APP_USBD_CDC_NOTIF_AUX_JACK_HOOK_STATE = 0x08, /**< (DLM) This notification indicates the loop has changed on the auxiliary phone interface of the USB device. */ + APP_USBD_CDC_NOTIF_RING_DETECT = 0x09, /**< (DLM) This notification indicates ring voltage on the POTS line interface of the USB device. */ + APP_USBD_CDC_NOTIF_SERIAL_STATE = 0x20, /**< (ACM) This notification sends asynchronous notification of UART status. */ + APP_USBD_CDC_NOTIF_CALL_STATE_CHANGE = 0x28, /**< (TCM) This notification identifies that a change has occurred to the state of a call on the line corresponding to the interface or union for the line. */ + APP_USBD_CDC_NOTIF_LINE_STATE_CHANGE = 0x29 /**< (TCM) This notification identifies that a change has occurred to the state of the line corresponding to the interface or master interface of a union sending the notification message. */ +} app_usbd_cdc_notify_id_t; + +/** + * @brief Notification sent via CDC COMM endpoint. + * */ +typedef struct { + uint8_t bmRequestType; //!< Request type. + uint8_t bmRequest; //!< Request ID @ref app_usbd_cdc_req_id_t. + uint16_t wValue; //!< Value field. + uint16_t wIndex; //!< Index field. + uint16_t wLength; //!< Length of payload following. +} app_usbd_cdc_notify_t; + +/** + * @brief CDC line coding structure. + */ +typedef struct { + uint8_t dwDTERate[4]; //!< Line baudrate. + uint8_t bCharFormat; //!< Character format @ref app_usbd_cdc_line_stopbit_t. + uint8_t bParityType; //!< Parity bits @ref app_usbd_cdc_line_parity_t. + uint8_t bDataBits; //!< Number of data bits. +} app_usbd_cdc_line_coding_t; + +/** + * @brief Possible values of @ref app_usbd_cdc_line_coding_t::bCharFormat. + */ +typedef enum { + APP_USBD_CDC_LINE_STOPBIT_1 = 0, /**< 1 stop bit. */ + APP_USBD_CDC_LINE_STOPBIT_1_5 = 1, /**< 1.5 stop bits. */ + APP_USBD_CDC_LINE_STOPBIT_2 = 2, /**< 2 stop bits. */ +} app_usbd_cdc_line_stopbit_t; + +/** + * @brief Possible values of @ref app_usbd_cdc_line_coding_t::bParityType. + */ +typedef enum { + APP_USBD_CDC_LINE_PARITY_NONE = 0, /**< No parity. */ + APP_USBD_CDC_LINE_PARITY_ODD = 1, /**< Odd parity. */ + APP_USBD_CDC_LINE_PARITY_EVEN = 2, /**< Even parity. */ + APP_USBD_CDC_LINE_PARITY_MARK = 3, /**< Parity forced to 0 (space).*/ + APP_USBD_CDC_LINE_PARITY_SPACE = 4, /**< Parity forced to 1 (mark). */ +} app_usbd_cdc_line_parity_t; + + +#pragma pack(pop) + +/** @} */ +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_TYPES_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy.c new file mode 100644 index 0000000..51d8721 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy.c @@ -0,0 +1,107 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(APP_USBD_DUMMY) + +#include <string.h> +#include <ctype.h> +#include "app_usbd.h" +#include "app_usbd_dummy.h" +#include "app_usbd_string_desc.h" +#include "nrf_gpio.h" + +/** + * @defgroup app_usbd_dummy_internal USBD Dummy internals + * @{ + * @ingroup app_usbd_dummy + * @internal + */ + +#define NRF_LOG_MODULE_NAME usbd_dummy + +#if APP_USBD_DUMMY_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL APP_USBD_DUMMY_CONFIG_LOG_LEVEL +#define NRF_LOG_INFO_COLOR APP_USBD_DUMMY_CONFIG_INFO_COLOR +#define NRF_LOG_DEBUG_COLOR APP_USBD_DUMMY_CONFIG_DEBUG_COLOR +#else //APP_USBD_DUMMY_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL 0 +#endif //APP_USBD_DUMMY_CONFIG_LOG_ENABLED +#include "nrf_log.h" +NRF_LOG_MODULE_REGISTER(); + +/** @brief @ref app_usbd_class_methods_t::event_handler */ +static ret_code_t dummy_class_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_complex_evt_t const * p_event) +{ + return NRF_ERROR_NOT_SUPPORTED; +} + +/** @brief @ref app_usbd_class_methods_t::feed_descriptors */ +static bool dummy_class_feed_descriptors(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size) +{ + static app_usbd_class_iface_conf_t const * p_cur_iface = 0; + p_cur_iface = app_usbd_class_iface_get(p_inst, 0); + + APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size) + + /* INTERFACE DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(sizeof(app_usbd_descriptor_iface_t)); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE); // bDescriptorType + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_number_get(p_cur_iface)); // bInterfaceNumber + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bAlternateSetting + APP_USBD_CLASS_DESCRIPTOR_WRITE(0); // bNumEndpoints + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DUMMY_CLASS); // bInterfaceClass + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DUMMY_SUBCLASS); // bInterfaceSubClass + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DUMMY_PROTOCOL); // bInterfaceProtocol + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iInterface + + APP_USBD_CLASS_DESCRIPTOR_END(); +} + +const app_usbd_class_methods_t app_usbd_dummy_class_methods = { + .event_handler = dummy_class_event_handler, + .feed_descriptors = dummy_class_feed_descriptors, +}; + + +#endif //NRF_MODULE_ENABLED(APP_USBD_DUMMY) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy.h new file mode 100644 index 0000000..61fa819 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy.h @@ -0,0 +1,138 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_DUMMY_H__ +#define APP_USBD_DUMMY_H__ + +#include <stdint.h> +#include <stdbool.h> + +#include "nrf_drv_usbd.h" +#include "nrf_block_dev.h" +#include "app_usbd_class_base.h" +#include "app_usbd.h" +#include "app_usbd_core.h" +#include "app_usbd_descriptor.h" + +#include "app_usbd_dummy_types.h" +#include "app_usbd_dummy_internal.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_dummy USB Dummy class + * @ingroup app_usbd + * + * @brief @tagAPI52840 Module with types, definitions, + * and API used by the USB Dummy class. + * + * @note Class is used as a filler to provide correct alignement of interfaces. + * + * @{ + */ + +#ifdef DOXYGEN +/** + * @brief Dummy class instance type + * + * @ref APP_USBD_CLASS_TYPEDEF + */ +typedef struct { } app_usbd_dummy_t; +#else +/*lint -save -e10 -e26 -e123 -e505 */ +APP_USBD_CLASS_NO_EP_TYPEDEF(app_usbd_dummy, \ + APP_USBD_DUMMY_CONFIG(0), \ + APP_USBD_DUMMY_INSTANCE_SPECIFIC_DEC, \ + APP_USBD_DUMMY_DATA_SPECIFIC_DEC \ +); +#endif + +/*lint -restore*/ + +/** + * @brief Global definition of app_usbd_dummy_t class + * + * @param instance_name Name of global instance + * @param interface_number Unique interface number + * + * @note This macro is just simplified version of @ref APP_USBD_DUMMY_GLOBAL_DEF_INTERNAL + * + */ +/*lint -save -emacro(26 64 123 505 651, APP_USBD_DUMMY_GLOBAL_DEF)*/ +#define APP_USBD_DUMMY_GLOBAL_DEF(instance_name, interface_number) \ + APP_USBD_DUMMY_GLOBAL_DEF_INTERNAL(instance_name, interface_number) +/*lint -restore*/ + +/** + * @@brief Helper function to get class instance from dummy + * + * @param[in] p_dummy Dummy instance + * (declared by @ref APP_USBD_DUMMY_GLOBAL_DEF) + * + * @return Base class instance + */ +static inline app_usbd_class_inst_t const * +app_usbd_dummy_class_inst_get(app_usbd_dummy_t const * p_dummy) +{ + return &p_dummy->base; +} + +/** + * @brief Helper function to get dummy from base class instance + * + * @param[in] p_inst Base class instance + * + * @return Dummy class handle + */ +static inline app_usbd_dummy_t const * +app_usbd_dummy_class_get( app_usbd_class_inst_t const * p_inst) +{ + return (app_usbd_dummy_t const *)p_inst; +} + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_DUMMY_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy_internal.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy_internal.h new file mode 100644 index 0000000..5b48ae7 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy_internal.h @@ -0,0 +1,115 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_DUMMY_INTERNAL_H__ +#define APP_USBD_DUMMY_INTERNAL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_dummy_internals USB Dummy internals + * @ingroup app_usbd_dummy + * + * @brief @tagAPI52840 Internals of the USB Dummy class. + * @{ + */ + +/** @brief Forward declaration of Dummy Class type */ +APP_USBD_CLASS_FORWARD(app_usbd_dummy); + +/** @brief Dummy part of class instance data */ +typedef struct { + uint8_t none; +} app_usbd_dummy_inst_t; + +/** @brief Dummy context */ +typedef struct { + uint8_t none; +} app_usbd_dummy_ctx_t; + + +/** + * @brief Dummy configuration macro + * + * Used by @ref APP_USBD_DUMMY_GLOBAL_DEF + * + * @param iface Interface number + * */ +#define APP_USBD_DUMMY_CONFIG(iface) (iface) + + +/** + * @brief Specific class constant data for Dummy + * + * @ref app_usbd_dummy_inst_t + */ +#define APP_USBD_DUMMY_INSTANCE_SPECIFIC_DEC app_usbd_dummy_inst_t inst; + +/** + * @brief Specific class data for Dummy + * + * @ref app_usbd_dummy_ctx_t + * */ +#define APP_USBD_DUMMY_DATA_SPECIFIC_DEC app_usbd_dummy_ctx_t ctx; + + +/** @brief Public Dummy class interface */ +extern const app_usbd_class_methods_t app_usbd_dummy_class_methods; + +/** @brief Global definition of Dummy instance */ +#define APP_USBD_DUMMY_GLOBAL_DEF_INTERNAL(instance_name, interface_number) \ + APP_USBD_CLASS_INST_NO_EP_GLOBAL_DEF( \ + instance_name, \ + app_usbd_dummy, \ + &app_usbd_dummy_class_methods, \ + APP_USBD_DUMMY_CONFIG((interface_number)), \ + () \ + ) + + +/** @} */ + + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_DUMMY_INTERNAL_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy_types.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy_types.h new file mode 100644 index 0000000..1a624a9 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/dummy/app_usbd_dummy_types.h @@ -0,0 +1,75 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_DUMMY_TYPES_H__ +#define APP_USBD_DUMMY_TYPES_H__ + +#include "app_util.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_dummy_types USB Dummy types + * @ingroup app_usbd_dummy + * + * @brief @tagAPI52840 Types used in the USB Dummy class. + * @{ + */ + +/** @brief Dummy class definition in interface descriptor + * + * @ref app_usbd_descriptor_iface_t::bInterfaceClass + * */ +#define APP_USBD_DUMMY_CLASS 0xFF + +/** @brief Dummy subclass value */ +#define APP_USBD_DUMMY_SUBCLASS 0x00 + +/** @brief Dummy protocol value */ +#define APP_USBD_DUMMY_PROTOCOL 0x00 + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_DUMMY_TYPES_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/app_usbd_hid.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/app_usbd_hid.c new file mode 100644 index 0000000..d8ff316 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/app_usbd_hid.c @@ -0,0 +1,508 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(APP_USBD_HID) + +#include "app_usbd.h" +#include "app_usbd_core.h" +#include "app_usbd_hid.h" + +/** + * @ingroup app_usbd_hid_internals USBD HID internals + * @{ + * @ingroup app_usbd_hid + * @internal + */ + +/** + * @brief Test whether SOF HID transfer is required. + * + * This function handles idle period IN transfer. + * + * @param[in,out] p_hid_ctx Internal HID context. + * @param framecnt SOF event frame counter. + * + * @retval true Idle transfer is required. + * @retval false Idle transfer is not required. + */ +static bool hid_sof_required(app_usbd_hid_ctx_t * p_hid_ctx, uint16_t framecnt) +{ + if (p_hid_ctx->idle_rate == 0) + { + /* Infinite idle rate */ + return false; + } + + /*Idle rate has 4ms units. Every SOF event is generated with 1ms period.*/ + uint16_t rate_ms = p_hid_ctx->idle_rate * 4; + if ((framecnt % rate_ms) != 0) + { + /* Idle transfer not required yet*/ + return false; + } + + if (p_hid_ctx->access_lock) + { + /* Access to internal data locked. Buffer is BUSY. + * Don't send anything. Clear transfer flag. Next transfer will be triggered + * from API context.*/ + app_usbd_hid_state_flag_clr(p_hid_ctx, APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS); + return false; + } + + if (app_usbd_hid_trans_required(p_hid_ctx)) + { + /*New transfer need to be triggered*/ + return true; + } + + return false; +} + +/** + * @brief User event handler. + * + * @param[in] p_inst Class instance. + * @param[in] p_hinst HID class instance. + * @param[in] event user Event type. + */ +static inline void user_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_inst_t const * p_hinst, + app_usbd_hid_user_event_t event) +{ + if (p_hinst->user_event_handler != NULL) + { + p_hinst->user_event_handler(p_inst, event); + } +} + +/** + * @brief Internal SETUP standard IN request handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_hinst HID class instance. + * @param[in,out] p_hid_ctx HID context. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t setup_req_std_in(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_inst_t const * p_hinst, + app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_setup_evt_t const * p_setup_ev) +{ + /* Only Get Descriptor standard IN request is supported by HID class */ + if ((app_usbd_setup_req_rec(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQREC_INTERFACE) + && + (p_setup_ev->setup.bmRequest == APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR)) + { + size_t dsc_len = 0; + size_t max_size; + + uint8_t * p_trans_buff = app_usbd_core_setup_transfer_buff_get(&max_size); + + /* Try to find descriptor in class internals*/ + ret_code_t ret = app_usbd_class_descriptor_find( + p_inst, + p_setup_ev->setup.wValue.hb, + p_setup_ev->setup.wValue.lb, + p_trans_buff, + &dsc_len); + + if (ret != NRF_ERROR_NOT_FOUND) + { + ASSERT(dsc_len < NRF_DRV_USBD_EPSIZE); + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_trans_buff, dsc_len); + } + + /* HID specific descriptors*/ + + app_usbd_class_descriptor_ctx_t desiz; + APP_USBD_CLASS_DESCRIPTOR_INIT(&desiz); + uint32_t report_size = 0; + + while(p_hinst->p_hid_methods->feed_subclass_descriptor(&desiz, + p_inst, + NULL, + sizeof(uint8_t), + p_setup_ev->setup.wValue.lb) + ) + { + report_size++; + } + + ASSERT(report_size <= max_size); + + UNUSED_RETURN_VALUE( + p_hinst->p_hid_methods->feed_subclass_descriptor(&desiz, + p_inst, + p_trans_buff, + report_size, + p_setup_ev->setup.wValue.lb)); + + return app_usbd_core_setup_rsp( + &p_setup_ev->setup, + p_trans_buff, + report_size); + } + + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Internal SETUP standard OUT request handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_hinst HID class instance. + * @param[in,out] p_hid_ctx HID context. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t setup_req_std_out(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_inst_t const * p_hinst, + app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_setup_evt_t const * p_setup_ev) +{ + /*Only Set Descriptor standard OUT request is supported by HID class. However, it is optional + * and useless in HID cases.*/ + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Internal SETUP class IN request handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_hinst HID class instance. + * @param[in,out] p_hid_ctx HID context. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t setup_req_class_in(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_inst_t const * p_hinst, + app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_setup_evt_t const * p_setup_ev) +{ + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_HID_REQ_GET_REPORT: + { + if ((p_setup_ev->setup.wValue.hb == APP_USBD_HID_REPORT_TYPE_INPUT) || + (p_setup_ev->setup.wValue.hb == APP_USBD_HID_REPORT_TYPE_OUTPUT)) + { + return p_hinst->p_hid_methods->on_get_report(p_inst, p_setup_ev); + } + else + { + break; + } + } + case APP_USBD_HID_REQ_GET_IDLE: + { + return app_usbd_core_setup_rsp(&p_setup_ev->setup, + &p_hid_ctx->idle_rate, + sizeof(p_hid_ctx->idle_rate)); + } + case APP_USBD_HID_REQ_GET_PROTOCOL: + { + return app_usbd_core_setup_rsp(&p_setup_ev->setup, + &p_hid_ctx->boot_active, + sizeof(p_hid_ctx->boot_active)); + } + default: + break; + + } + + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Internal SETUP class OUT request handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_hinst HID class instance. + * @param[in,out] p_hid_ctx HID context. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t setup_req_class_out(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_inst_t const * p_hinst, + app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_setup_evt_t const * p_setup_ev) +{ + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_HID_REQ_SET_REPORT: + if (p_setup_ev->setup.wValue.hb != APP_USBD_HID_REPORT_TYPE_OUTPUT) + { + break; + } + + if (p_hinst->p_hid_methods->on_get_report == NULL) + { + break; + } + + return p_hinst->p_hid_methods->on_set_report(p_inst, p_setup_ev); + case APP_USBD_HID_REQ_SET_IDLE: + p_hid_ctx->idle_rate = p_setup_ev->setup.wValue.hb; + return NRF_SUCCESS; + case APP_USBD_HID_REQ_SET_PROTOCOL: + p_hid_ctx->boot_active = p_setup_ev->setup.wValue.w; + { + app_usbd_hid_user_event_t ev = (p_hid_ctx->boot_active == 0) ? + APP_USBD_HID_USER_EVT_SET_BOOT_PROTO : + APP_USBD_HID_USER_EVT_SET_REPORT_PROTO; + + user_event_handler(p_inst, p_hinst, ev); + } + return NRF_SUCCESS; + default: + break; + } + + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Internal SETUP event handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_hinst HID class instance. + * @param[in,out] p_hid_ctx HID context. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t setup_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_inst_t const * p_hinst, + app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_setup_evt_t const * p_setup_ev) +{ + ASSERT(p_hinst != NULL); + ASSERT(p_hid_ctx != NULL); + ASSERT(p_setup_ev != NULL); + + if (app_usbd_setup_req_dir(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQDIR_IN) + { + switch (app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + case APP_USBD_SETUP_REQTYPE_STD: + return setup_req_std_in(p_inst, p_hinst, p_hid_ctx, p_setup_ev); + case APP_USBD_SETUP_REQTYPE_CLASS: + return setup_req_class_in(p_inst, p_hinst, p_hid_ctx, p_setup_ev); + default: + break; + } + } + else /*APP_USBD_SETUP_REQDIR_OUT*/ + { + switch (app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + case APP_USBD_SETUP_REQTYPE_STD: + return setup_req_std_out(p_inst, p_hinst, p_hid_ctx, p_setup_ev); + case APP_USBD_SETUP_REQTYPE_CLASS: + return setup_req_class_out(p_inst, p_hinst, p_hid_ctx, p_setup_ev); + default: + break; + } + } + + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Endpoint IN event handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_hinst HID class instance. + * @param[in,out] p_hid_ctx HID context. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t endpoint_in_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_inst_t const * p_hinst, + app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_complex_evt_t const * p_event) +{ + + if (p_event->drv_evt.data.eptransfer.status == NRF_USBD_EP_OK) + { + /* Notify user about last successful transfer. */ + user_event_handler(p_inst, p_hinst, APP_USBD_HID_USER_EVT_IN_REPORT_DONE); + } + + if (app_usbd_hid_access_lock_test(p_hid_ctx)) + { + /* Access to internal data locked. Buffer is BUSY. + * Don't send anything. Clear transfer flag. Next transfer will be triggered + * from main loop context.*/ + app_usbd_hid_state_flag_clr(p_hid_ctx, APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS); + return NRF_SUCCESS; + } + + return p_hinst->p_hid_methods->ep_transfer_in(p_inst); +} + +/** + * @brief Endpoint OUT event handler. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_hinst HID class instance. + * @param[in,out] p_hid_ctx HID context. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ +static ret_code_t endpoint_out_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_inst_t const * p_hinst, + app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_complex_evt_t const * p_event) +{ + if (p_hinst->p_hid_methods->ep_transfer_out == NULL) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + if (p_event->drv_evt.data.eptransfer.status == NRF_USBD_EP_OK) + { + /* Notify user about last successful transfer. */ + user_event_handler(p_inst, p_hinst, APP_USBD_HID_USER_EVT_OUT_REPORT_READY); + } + + return p_hinst->p_hid_methods->ep_transfer_out(p_inst); +} + +/** @} */ + + +ret_code_t app_usbd_hid_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_inst_t const * p_hinst, + app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_complex_evt_t const * p_event) +{ + ret_code_t ret = NRF_SUCCESS; + switch (p_event->app_evt.type) + { + case APP_USBD_EVT_DRV_SOF: + if (!hid_sof_required(p_hid_ctx, p_event->drv_evt.data.sof.framecnt)) + { + break; + } + ret = p_hinst->p_hid_methods->ep_transfer_in(p_inst); + break; + case APP_USBD_EVT_DRV_RESET: + app_usbd_hid_state_flag_clr(p_hid_ctx, APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS); + break; + case APP_USBD_EVT_DRV_SETUP: + ret = setup_event_handler(p_inst, p_hinst, p_hid_ctx, &p_event->setup_evt); + break; + case APP_USBD_EVT_DRV_EPTRANSFER: + if (NRF_USBD_EPIN_CHECK(p_event->drv_evt.data.eptransfer.ep)) + { + ret = endpoint_in_event_handler(p_inst, p_hinst, p_hid_ctx, p_event); + } + else + { + ret = endpoint_out_event_handler(p_inst, p_hinst, p_hid_ctx, p_event); + } + break; + case APP_USBD_EVT_DRV_SUSPEND: + app_usbd_hid_state_flag_set(p_hid_ctx, APP_USBD_HID_STATE_FLAG_SUSPENDED); + break; + case APP_USBD_EVT_DRV_RESUME: + app_usbd_hid_state_flag_clr(p_hid_ctx, APP_USBD_HID_STATE_FLAG_SUSPENDED); + + /* Always try to trigger transfer on resume event*/ + ret = p_hinst->p_hid_methods->ep_transfer_in(p_inst); + break; + case APP_USBD_EVT_INST_APPEND: + /*SOF register: GetIdle/SetIdle support*/ + ret = app_usbd_class_sof_register(p_inst); + if (ret != NRF_SUCCESS) + { + break; + } + ret = app_usbd_class_rwu_register(p_inst); + if (ret != NRF_SUCCESS) + { + break; + } + app_usbd_hid_state_flag_set(p_hid_ctx, APP_USBD_HID_STATE_FLAG_APPENDED); + break; + case APP_USBD_EVT_INST_REMOVE: + /*SOF unregister: GetIdle/SetIdle support*/ + ret = app_usbd_class_sof_unregister(p_inst); + if (ret != NRF_SUCCESS) + { + break; + } + ret = app_usbd_class_rwu_unregister(p_inst); + if (ret != NRF_SUCCESS) + { + break; + } + app_usbd_hid_state_flag_clr(p_hid_ctx, APP_USBD_HID_STATE_FLAG_APPENDED); + break; + case APP_USBD_EVT_STARTED: + app_usbd_hid_state_flag_set(p_hid_ctx, APP_USBD_HID_STATE_FLAG_STARTED); + break; + case APP_USBD_EVT_STOPPED: + app_usbd_hid_state_flag_clr(p_hid_ctx, APP_USBD_HID_STATE_FLAG_STARTED); + break; + default: + ret = NRF_ERROR_NOT_SUPPORTED; + break; + } + + return ret; +} + +app_usbd_hid_report_buffer_t * app_usbd_hid_rep_buff_in_get(app_usbd_hid_inst_t const * p_hinst) +{ + ASSERT(p_hinst); + return p_hinst->p_rep_buffer_in; +} + +#endif //NRF_MODULE_ENABLED(APP_USBD_HID) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/app_usbd_hid.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/app_usbd_hid.h new file mode 100644 index 0000000..c4638ab --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/app_usbd_hid.h @@ -0,0 +1,515 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_HID_H__ +#define APP_USBD_HID_H__ + +#include <stdint.h> +#include <stdbool.h> + +#include "sdk_common.h" +#include "nrf_atomic.h" +#include "app_usbd_hid_types.h" +#include "app_usbd.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_hid USB HID class + * @ingroup app_usbd + * + * @brief @tagAPI52840 Module with generic HID event data processing. + * @{ + */ + +#define APP_USBD_HID_IFACE_IDX 0 /**< @brief HID instance interface index. */ +#define APP_USBD_HID_EPIN_IDX 0 /**< @brief HID instance endpoint IN index. */ +#define APP_USBD_HID_EPOUT_IDX 1 /**< @brief HID instance endpoint OUT index.*/ + +/** + * @brief HID context state flags. + * + * Bit numbers in @ref app_usbd_hid_ctx_t::state_flags. + */ +typedef enum { + APP_USBD_HID_STATE_FLAG_APPENDED = 0, /**< State flag APPENDED. */ + APP_USBD_HID_STATE_FLAG_STARTED = 1, /**< State flag STARTED. */ + APP_USBD_HID_STATE_FLAG_SUSPENDED = 2, /**< State flag SUSPENDED. */ + APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS = 3, /**< State flag TRANS_IN_PROGRESS. */ +} app_usbd_hid_state_flag_t; + +/** + * @brief Events passed to user event handler. + * + * @note Example prototype of user event handler: + @code + void hid_user_ev_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_mouse_user_event_t event); + @endcode + */ +typedef enum { + APP_USBD_HID_USER_EVT_SET_BOOT_PROTO, /**< Event SET_BOOT_PROTOCOL. */ + APP_USBD_HID_USER_EVT_SET_REPORT_PROTO, /**< Event SET_REPORT_PROTOCOL.*/ + APP_USBD_HID_USER_EVT_OUT_REPORT_READY, /**< Event OUT_REPORT_READY. */ + APP_USBD_HID_USER_EVT_IN_REPORT_DONE, /**< Event IN_REPORT_DONE. */ +} app_usbd_hid_user_event_t; + + +/** + * @brief User event handler. + * + * @param[in] p_inst Class instance. + * @param[in] event User event. + */ +typedef void (*app_usbd_hid_user_ev_handler_t)(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_user_event_t event); + +/**@brief HID unified interface*/ +typedef struct { + + /** + * @brief Function called on HID specific, GetReport request. + * + * This function should trigger data write to control pipe. + * + * @param[in] p_inst Class instance. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ + ret_code_t (*on_get_report)(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev); + + /** + * @brief Function called on HID specific, SetReport request. + * + * This function should trigger data read from control pipe. This function is not required and + * NULL could be pinned to this handler when output report is not defined in report descriptor. + * + * @param[in] p_inst Class instance. + * @param[in] p_setup_ev Setup event. + * + * @return Standard error code. + */ + ret_code_t (*on_set_report)(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev); + + /** + * @brief Function called on IN endpoint transfer. + * + * This function should trigger next endpoint IN transfer if required. + * + * @param[in] p_inst Class instance. + * + * @return Standard error code. + */ + ret_code_t (*ep_transfer_in)(app_usbd_class_inst_t const * p_inst); + + /** + * @brief Function called on OUT endpoint transfer. + * + * This function should should read data from OUT endpoint. This function is not required and + * NULL could be pinned to this handler when class doesn't have OUT endpoint. + * + * @param[in] p_inst Class instance. + * + * @return Standard error code. + */ + ret_code_t (*ep_transfer_out)(app_usbd_class_inst_t const * p_inst); + + /** + * @brief Instance feed subclass descriptor + * + * Feeds whole descriptor of the instance + * @param[in] p_ctx Class descriptor context + * @param[in,out] p_inst Instance of the class + * @param[out] buff Buffer for subclass descriptor + * @param[in] max_size Requested size of the subclass descriptor + * @param[in] index Index of the subclass descriptor + * + * @return True if not finished feeding the descriptor, false if done + */ + bool (*feed_subclass_descriptor)(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * buff, + size_t max_size, + uint8_t index); +} app_usbd_hid_methods_t; + +/** + * @brief HID report buffers. + */ +typedef struct { + uint8_t * p_buff; + size_t size; +} app_usbd_hid_report_buffer_t; + + + +/**@brief Define OUT report buffer structure @ref app_usbd_hid_report_buffer_t. + * + * @param name Instance name. + * @param rep_size Output report size. + */ +#define APP_USBD_HID_GENERIC_GLOBAL_OUT_REP_DEF(name, rep_size) \ + static uint8_t CONCAT_2(name, _buf)[(rep_size)]; \ + const app_usbd_hid_report_buffer_t name = { \ + .p_buff = CONCAT_2(name, _buf), \ + .size = sizeof(CONCAT_2(name, _buf)), \ + } + +/** + * @brief HID subclass descriptor. + */ + +typedef struct { + size_t size; + app_usbd_descriptor_t type; + uint8_t const * const p_data; +} app_usbd_hid_subclass_desc_t; + +/** + * @brief Initializer of HID report descriptor + * + * @param name Report descriptor name + * @param ... Report descriptor data + */ + +#define APP_USBD_HID_GENERIC_SUBCLASS_REPORT_DESC(name, ...) \ + static uint8_t const CONCAT_2(name, _data)[] = \ + __VA_ARGS__ \ + ; \ + static const app_usbd_hid_subclass_desc_t name = \ + { \ + sizeof(CONCAT_2(name, _data)), \ + APP_USBD_DESCRIPTOR_REPORT, \ + CONCAT_2(name,_data) \ + } + +/** + * @brief Initializer of HID physical descriptor + * + * @param name Physical descriptor name + * @param ... Physical descriptor data + */ + +#define APP_USBD_HID_GENERIC_SUBCLASS_PHYSICAL_DESC(name, ...) \ + static uint8_t const CONCAT_2(name, _data)[] = \ + __VA_ARGS__ \ + ; \ + static const app_usbd_hid_subclass_desc_t name = \ + { \ + sizeof(CONCAT_2(name, _data)), \ + APP_USBD_DESCRIPTOR_PHYSICAL, \ + CONCAT_2(name,_data) \ + } + + +/** + * @brief USB HID instance. + */ +typedef struct { + app_usbd_hid_subclass_desc_t const ** const p_subclass_desc; //!< HID subclass descriptors array. + size_t subclass_desc_count; //!< HID subclass descriptors count. + + app_usbd_hid_subclass_t subclass_boot; //!< Boot device (see HID definition) + app_usbd_hid_protocol_t protocol; //!< HID protocol (see HID definition) + + app_usbd_hid_report_buffer_t * p_rep_buffer_in; //!< Report buffer IN. + app_usbd_hid_report_buffer_t const * p_rep_buffer_out; //!< Report buffer OUT (only one instance). + app_usbd_hid_methods_t const * p_hid_methods; //!< Hid interface methods. + app_usbd_hid_user_ev_handler_t user_event_handler; //!< User event handler. +} app_usbd_hid_inst_t; + + +/** + * @brief USB HID instance initializer @ref app_usbd_hid_inst_t. + * + * @param subclass_dsc HID subclass descriptors. + * @param sub_boot Subclass boot. (@ref app_usbd_hid_subclass_t) + * @param protocl HID protocol. (@ref app_usbd_hid_protocol_t) + * @param report_buff_in Input report buffer list. + * @param report_buff_out Output report buffer. + * @param user_ev_handler @ref app_usbd_hid_user_ev_handler_t. + @param hid_methods @ref app_usbd_hid_methods_t. + * */ + +#define APP_USBD_HID_INST_CONFIG(subclass_dsc, \ + sub_boot, \ + protocl, \ + report_buff_in, \ + report_buff_out, \ + user_ev_handler, \ + hid_methods) \ + { \ + .p_subclass_desc = subclass_dsc, \ + .subclass_desc_count = ARRAY_SIZE(subclass_dsc), \ + .p_rep_buffer_in = report_buff_in, \ + .p_rep_buffer_out = report_buff_out, \ + .user_event_handler = user_ev_handler, \ + .p_hid_methods = hid_methods, \ + .subclass_boot = sub_boot, \ + .protocol = protocl \ + } + +/** + * @brief HID internal context. + * */ +typedef struct { + nrf_atomic_u32_t state_flags; //!< HID state flags @ref app_usbd_hid_state_flag_t. + nrf_atomic_flag_t access_lock; //!< Lock flag to internal data. + uint8_t idle_rate; //!< HID idle rate (4ms units). + uint8_t boot_active; //!< HID using boot protocol. +} app_usbd_hid_ctx_t; + + +/** + * @brief Locks internal hid context. + * + * Simple semaphore functionality to prevent concurrent access from application and + * interrupt to internal mouse data. + * + * @param[in] p_hid_ctx Internal hid context + */ +static inline void app_usbd_hid_access_lock(app_usbd_hid_ctx_t * p_hid_ctx) +{ + UNUSED_RETURN_VALUE(nrf_atomic_flag_set(&p_hid_ctx->access_lock)); + __DSB(); +} + + +/** + * @brief Unlocks internal hid context. + * + * Simple semaphore functionality to prevent concurrent access from application and + * interrupt to internal mouse data. + * + * @param[in] p_hid_ctx Internal hid context. + */ +static inline void app_usbd_hid_access_unlock(app_usbd_hid_ctx_t * p_hid_ctx) +{ + UNUSED_RETURN_VALUE(nrf_atomic_flag_clear(&p_hid_ctx->access_lock)); + __DSB(); +} + +/** + * @brief Tests whether internal lock is acquired. + * + * @param[in] p_hid_ctx Internal HID context. + * + * @retval true Locked. + * @retval false Unlocked. + */ +static inline bool app_usbd_hid_access_lock_test(app_usbd_hid_ctx_t * p_hid_ctx) +{ + return p_hid_ctx->access_lock != 0; +} + +/** + * @brief Set one of the HID internal state flags. + * + * @param[in] p_hid_ctx Internal HID context. + * @param[in] flag Flag to set. + */ +static inline void app_usbd_hid_state_flag_set(app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_hid_state_flag_t flag) +{ + UNUSED_RETURN_VALUE(nrf_atomic_u32_or(&p_hid_ctx->state_flags, 1u << flag)); +} + +/** + * @brief Clear one of the HID internal state flags. + * + * @param[in] p_hid_ctx Internal HID context. + * @param[in] flag Flag to clear. + */ +static inline void app_usbd_hid_state_flag_clr(app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_hid_state_flag_t flag) +{ + UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&p_hid_ctx->state_flags, ~(1u << flag))); +} + + +/** + * @brief Test one of the HID internal state flags. + * + * @param[in] p_hid_ctx Internal HID context. + * @param[in] flag Flag to test. + * + * @retval true Flag is set. + * @retval false Flag is not set. + */ +static inline bool app_usbd_hid_state_flag_test(app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_hid_state_flag_t flag) +{ + return ((p_hid_ctx->state_flags >> flag) & 1) == 1; +} + +/** + * @brief Checks whether HID endpoint transfer required. + * + * @param[in] p_hid_ctx Internal HID context. + * + * @retval true Input endpoint transfer required. + * @retval false Transfer in progress or not allowed. + */ +static inline bool app_usbd_hid_trans_required(app_usbd_hid_ctx_t * p_hid_ctx) +{ + if (app_usbd_hid_state_flag_test(p_hid_ctx, APP_USBD_HID_STATE_FLAG_SUSPENDED) != 0) + { + UNUSED_RETURN_VALUE(app_usbd_wakeup_req()); + return false; + } + + return app_usbd_hid_state_flag_test(p_hid_ctx, APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS) == 0; +} + +/** + * @brief Validates internal hid state. + * + * HID Mouse has to receive some USBD events before functions from this module could be used. + * + * @param[in] p_hid_ctx Internal hid context. + * + * @retval true State is valid. + * @retval false State is invalid. + */ +static inline bool app_usbd_hid_state_valid(app_usbd_hid_ctx_t * p_hid_ctx) +{ + /*Check whether internal flags allow to enable mouse*/ + if ((app_usbd_hid_state_flag_test(p_hid_ctx, APP_USBD_HID_STATE_FLAG_APPENDED) == 0) || + (app_usbd_hid_state_flag_test(p_hid_ctx, APP_USBD_HID_STATE_FLAG_STARTED) == 0)) + { + return false; + } + + return true; +} + +/** + * @brief HID generic event handler. + * + * This handler should process every class event after specific class handler. + * This approach allow to handle some events in the same way in all HID sub-classes. + * + * @param[in] p_inst Generic class instance. + * @param[in] p_hinst HID class instance. + * @param[in] p_hid_ctx HID context. + * @param[in] p_event Complex event structure. + * + * @return Standard error code. + */ +ret_code_t app_usbd_hid_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_hid_inst_t const * p_hinst, + app_usbd_hid_ctx_t * p_hid_ctx, + app_usbd_complex_evt_t const * p_event); + + +/** + * @brief Returns IN report buffer. + * + * @param[in] p_hinst HID class instance. + * + * @return Report buffer handle or NULL if report doesn't exist. + */ +app_usbd_hid_report_buffer_t * app_usbd_hid_rep_buff_in_get(app_usbd_hid_inst_t const * p_hinst); + +/** + * @brief Returns OUT report buffer. + * + * Output reports are handled in interrupt handler so only one buffer is required. Buffer returned by + * this function has predefined size, which should be equal (maximum OUTPUT report size + 1). To receive + * OUT report this function should be called on @ref APP_USBD_HID_USER_EVT_OUT_REPORT_READY event. + * + * @param[in] p_hinst HID class instance. + * + * @return Report buffer handle or NULL if report doesn't exist. + */ +static inline app_usbd_hid_report_buffer_t const * +app_usbd_hid_rep_buff_out_get(app_usbd_hid_inst_t const * p_hinst) +{ + ASSERT(p_hinst); + return p_hinst->p_rep_buffer_out; +} + +/** + * @brief Auxiliary function to access to HID IN endpoint address. + * + * @param[in] p_inst Class instance data. + * + * @return IN endpoint address. + */ +static inline nrf_drv_usbd_ep_t app_usbd_hid_epin_addr_get(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_class_iface_conf_t const * class_iface; + class_iface = app_usbd_class_iface_get(p_inst, APP_USBD_HID_IFACE_IDX); + + app_usbd_class_ep_conf_t const * ep_cfg; + ep_cfg = app_usbd_class_iface_ep_get(class_iface, APP_USBD_HID_EPIN_IDX); + + return app_usbd_class_ep_address_get(ep_cfg); +} + +/** + * @brief Auxiliary function to access to HID generic OUT endpoint address. + * + * @param[in] p_inst Class instance data. + * + * @return OUT endpoint address. + */ +static inline nrf_drv_usbd_ep_t app_usbd_hid_epout_addr_get(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_class_iface_conf_t const * class_iface; + class_iface = app_usbd_class_iface_get(p_inst, APP_USBD_HID_IFACE_IDX); + + app_usbd_class_ep_conf_t const * ep_cfg; + ep_cfg = app_usbd_class_iface_ep_get(class_iface, APP_USBD_HID_EPOUT_IDX); + + return app_usbd_class_ep_address_get(ep_cfg); +} + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_HID_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/app_usbd_hid_types.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/app_usbd_hid_types.h new file mode 100644 index 0000000..9fc3486 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/app_usbd_hid_types.h @@ -0,0 +1,281 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_HID_TYPES_H__ +#define APP_USBD_HID_TYPES_H__ + +#include <stdint.h> + +#include "app_usbd_types.h" +#include "sdk_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_hid_types USB HID class types + * @ingroup app_usbd_hid + * + * @brief @tagAPI52840 Module with types and definitions used by HID modules. + * @{ + */ + +/** + * @brief HID class definition in interface descriptor. + * + * @ref app_usbd_descriptor_iface_t::bInterfaceClass + * */ +#define APP_USBD_HID_CLASS 0x03 + +/** + * @brief HID subclass definition. + * + * @see HID 1.11 specification: Chapter 4.2 Subclass. + * @ref app_usbd_descriptor_iface_t::bInterfaceSubClass + * */ +typedef enum { + APP_USBD_HID_SUBCLASS_NONE = 0x00, /**< Undefined subclass. */ + APP_USBD_HID_SUBCLASS_BOOT = 0x01, /**< Boot subclass. */ +} app_usbd_hid_subclass_t; + +/** + * @brief HID protocol types defined by specification. + * + * Value need to be filled in interface descriptor. + * @ref app_usbd_descriptor_iface_t::bInterfaceProtocol + */ +typedef enum { + APP_USBD_HID_PROTO_GENERIC = 0x00, /**< GENERIC protocol. */ + APP_USBD_HID_PROTO_KEYBOARD = 0x01, /**< KEYBOARD protocol. */ + APP_USBD_HID_PROTO_MOUSE = 0x02, /**< MOUSE protocol. */ + APP_USBD_HID_PROTO_MULTITOUCH = 0x03, /**< MULTITOUCH protocol. */ +} app_usbd_hid_protocol_t; + + +/** + * @brief HID country code ID. + * + * Look into @ref app_usbd_hid_descriptor_t::bCountryCode. + */ +typedef enum { + APP_USBD_HID_COUNTRY_NOT_SUPPORTED = 0 , /**< NOT_SUPPORTED */ + APP_USBD_HID_COUNTRY_ARABIC = 1 , /**< ARABIC */ + APP_USBD_HID_COUNTRY_BELGIAN = 2 , /**< BELGIAN */ + APP_USBD_HID_COUNTRY_CANADIAN_BILINGUAL= 3 , /**< CANADIAN_BILINGUAL */ + APP_USBD_HID_COUNTRY_CANADIAN_FRENCH = 4 , /**< CANADIAN_FRENCH */ + APP_USBD_HID_COUNTRY_CZECH_REPUBLIC = 5 , /**< CZECH_REPUBLIC */ + APP_USBD_HID_COUNTRY_DANISH = 6 , /**< DANISH */ + APP_USBD_HID_COUNTRY_FINNISH = 7 , /**< FINNISH */ + APP_USBD_HID_COUNTRY_FRENCH = 8 , /**< FRENCH */ + APP_USBD_HID_COUNTRY_GERMAN = 9 , /**< GERMAN */ + APP_USBD_HID_COUNTRY_GREEK = 10, /**< GREEK */ + APP_USBD_HID_COUNTRY_HEBREW = 11, /**< HEBREW */ + APP_USBD_HID_COUNTRY_HUNGARY = 12, /**< HUNGARY */ + APP_USBD_HID_COUNTRY_INTERNATIONAL_ISO = 13, /**< INTERNATIONAL_ISO */ + APP_USBD_HID_COUNTRY_ITALIAN = 14, /**< ITALIAN */ + APP_USBD_HID_COUNTRY_JAPAN_KATAKANA = 15, /**< JAPAN_KATAKANA */ + APP_USBD_HID_COUNTRY_KOREAN = 16, /**< KOREAN */ + APP_USBD_HID_COUNTRY_LATIN_AMERICAN = 17, /**< LATIN_AMERICAN */ + APP_USBD_HID_COUNTRY_NETHERLANDS_DUTCH = 18, /**< NETHERLANDS_DUTCH */ + APP_USBD_HID_COUNTRY_NORWEGIAN = 19, /**< NORWEGIAN */ + APP_USBD_HID_COUNTRY_PERSIAN_FARSI = 20, /**< PERSIAN_FARSI */ + APP_USBD_HID_COUNTRY_POLAND = 21, /**< POLAND */ + APP_USBD_HID_COUNTRY_PORTUGUESE = 22, /**< PORTUGUESE */ + APP_USBD_HID_COUNTRY_RUSSIA = 23, /**< RUSSIA */ + APP_USBD_HID_COUNTRY_SLOVAKIA = 24, /**< SLOVAKIA */ + APP_USBD_HID_COUNTRY_SPANISH = 25, /**< SPANISH */ + APP_USBD_HID_COUNTRY_SWEDISH = 26, /**< SWEDISH */ + APP_USBD_HID_COUNTRY_SWISS_FRENCH = 27, /**< SWISS_FRENCH */ + APP_USBD_HID_COUNTRY_SWISS_GERMAN = 28, /**< SWISS_GERMAN */ + APP_USBD_HID_COUNTRY_SWITZERLAND = 29, /**< SWITZERLAND */ + APP_USBD_HID_COUNTRY_TAIWAN = 30, /**< TAIWAN */ + APP_USBD_HID_COUNTRY_TURKISH_Q = 31, /**< TURKISH_Q */ + APP_USBD_HID_COUNTRY_UK = 32, /**< UK */ + APP_USBD_HID_COUNTRY_US = 33, /**< US */ + APP_USBD_HID_COUNTRY_YUGOSLAVIA = 34, /**< YUGOSLAVIA */ + APP_USBD_HID_COUNTRY_TURKISH_F = 35, /**< TURKISH_F */ +} app_usbd_hid_country_code_t; + +/** + * @brief HID descriptor types. + * + * @ref app_usbd_hid_descriptor_t::bRDescriptorType + */ +typedef enum { + APP_USBD_HID_DESCRIPTOR_HID = 0x21, /**< HID descriptor. */ + APP_USBD_HID_DESCRIPTOR_REPORT = 0x22, /**< REPORT descriptor. */ + APP_USBD_HID_DESCRIPTOR_PHYSICAL = 0x23, /**< PHYSICAL descriptor. */ +} app_usbd_hid_descriptor_type_t; + +#pragma pack(push, 1) + +/** + * @brief HID report descriptor entry at the end of HID descriptor. + * + * @param size Report descriptor size. + */ +#define APP_USBD_HID_REPORT_ITEM(size) \ + APP_USBD_HID_DESCRIPTOR_REPORT, ((size) & 0xFF), ((size) / 256) + + +/** + * @brief HID physical descriptor entry at the end of HID descriptor. + * + * @param size Physical descriptor size. + */ +#define APP_USBD_HID_PHYSICAL_ITEM(size) \ + APP_USBD_HID_DESCRIPTOR_PHYSICAL, ((size) & 0xFF), ((size) / 256) + + +/** + * @brief HID descriptor, binary layout. + */ +typedef union { + struct { + uint8_t bLength; //!< Length of descriptor. + uint8_t bDescriptorType; //!< Descriptor type @ref APP_USBD_HID_DESCRIPTOR_HID. + uint16_t bcdHID; //!< HID release number (BCD format, little endian). + uint8_t bCountryCode; //!< Country code. + uint8_t bNumDescriptors; //!< Number of class descriptors. + struct { + uint8_t bRDescriptorType; //!< Class descriptor type. + uint16_t wDescriptorLength; //!< Class descriptor length (little endian). + } reports[]; + } raw; +} app_usbd_hid_descriptor_t; + +#pragma pack(pop) + + +/** + * @brief HID requests defined by specification. + */ +typedef enum { + APP_USBD_HID_REQ_GET_REPORT = 0x01, /**< REPORT: device -> host (required). */ + APP_USBD_HID_REQ_GET_IDLE = 0x02, /**< IDLE: device -> host (not required). */ + APP_USBD_HID_REQ_GET_PROTOCOL = 0x03, /**< PROTOCOL: device -> host (required for boot protocol). */ + APP_USBD_HID_REQ_SET_REPORT = 0x09, /**< REPORT: host -> device (not required). */ + APP_USBD_HID_REQ_SET_IDLE = 0x0A, /**< IDLE: no data stage (required for boot protocol). */ + APP_USBD_HID_REQ_SET_PROTOCOL = 0x0B, /**< PROTOCOL: no data stage(required for boot protocol). */ +} app_usbd_hid_req_t; + +/** + * @brief HID report type. + */ +typedef enum { + APP_USBD_HID_REPORT_TYPE_INPUT = 0x01,/**< INPUT report type */ + APP_USBD_HID_REPORT_TYPE_OUTPUT = 0x02,/**< OUTPUT report type */ + APP_USBD_HID_REPORT_TYPE_FEATURE = 0x03,/**< FEATURE report type */ +} app_usbd_hid_report_type_t; + + +/** + * @brief Hid version BCD value definition + * + * The version of the HID descriptors used. + */ +#define APP_USBD_HID_BCD_VER APP_USBD_BCD_VER_MAKE(1, 11) + +/** + * @brief HID version BCD value definition distributed into bytes + * + * This is a value written directly into @ref app_usbd_hid_descriptor_t::bcdHID. + * @sa APP_USBD_HID_BCD_VER + */ +#define APP_USBD_HID_BCD_VER_BYTES LSB_16(APP_USBD_HID_BCD_VER), MSB_16(APP_USBD_HID_BCD_VER) + +/** + * @brief Initializer of interface descriptor for HID classes. + * + * @param interface_number Interface number. + * @param endpoints_num Number of endpoints. + * @param subclass Subclass type @ref app_usbd_hid_subclass_t. + * @param protocol Protocol type @ref app_usbd_hid_protocol_t. + */ +#define APP_USBD_HID_INTERFACE_DSC(interface_number, endpoints_num, subclass, protocol) \ + /*.bLength = */ sizeof(app_usbd_descriptor_iface_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_INTERFACE, \ + /*.bInterfaceNumber = */ interface_number, \ + /*.bAlternateSetting = */ 0x00, \ + /*.bNumEndpoints = */ endpoints_num, \ + /*.bInterfaceClass = */ APP_USBD_HID_CLASS, \ + /*.bInterfaceSubClass = */ subclass, \ + /*.bInterfaceProtocol = */ protocol, \ + /*.iInterface = 0, */ 0x00, \ + + +/** + * @brief Initializer of HID descriptor for HID classes. + * + * @param ... Report/physical item list. + */ +#define APP_USBD_HID_HID_DSC(...) \ + /*.bLength = */ sizeof(app_usbd_hid_descriptor_t) + 3 * (NUM_VA_ARGS(__VA_ARGS__)), \ + /*.bDescriptorType = */ APP_USBD_HID_DESCRIPTOR_HID, \ + /*.bcdHID = */ APP_USBD_HID_BCD_VER_BYTES, \ + /*.bCountryCode = */ APP_USBD_HID_COUNTRY_NOT_SUPPORTED, \ + /*.bNumDescriptors = */ (NUM_VA_ARGS(__VA_ARGS__)), \ + /*.bRDescriptorType = */ APP_USBD_HID_REPORT_ITEM(sizeof(GET_VA_ARG_1_(__VA_ARGS__))), \ + /*.wDescriptorLength = */ + +/** + * @brief Initializer of endpoint descriptor for HID classes. + * + * @param endpoint Endpoint number. + * @param ep_size Endpoint size. + * @param ep_interval Endpoint interval (milliseconds). + */ +#define APP_USBD_HID_EP_DSC(endpoint, ep_size, ep_interval) \ + /*.bLength = */ sizeof(app_usbd_descriptor_ep_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_ENDPOINT, \ + /*.bEndpointAddress = */ endpoint, \ + /*.bmAttributes = */ APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_INTERRUPT, \ + /*.wMaxPacketSize = */ APP_USBD_U16_TO_RAW_DSC(ep_size), \ + /*.bInterval = */ ep_interval, \ + + + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_HID_TYPES_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic.c new file mode 100644 index 0000000..61dea15 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic.c @@ -0,0 +1,506 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(APP_USBD_HID_GENERIC) + +#include <string.h> +#include "app_usbd_hid_generic.h" +#include "app_util_platform.h" + + +/** + * @ingroup app_usbd_hid_generic + * + * Module with types, definitions and API used by HID generic. + * @{ + */ + +/** + * @brief Auxiliary function to access HID generic context data. + * + * @param[in] p_generic HID generic instance. + * + * @return HID generic class instance data context. + */ +static inline app_usbd_hid_generic_ctx_t * +hid_generic_ctx_get(app_usbd_hid_generic_t const * p_generic) +{ + ASSERT(p_generic != NULL); + ASSERT(p_generic->specific.p_data != NULL); + return &p_generic->specific.p_data->ctx; +} + + +/** + * @brief Auxiliary function to access HID generic instance data. + * + * @param[in] p_inst Class instance data. + * + * @return HID generic class instance data. + */ +static inline app_usbd_hid_generic_t const * +hid_generic_get(app_usbd_class_inst_t const * p_inst) +{ + ASSERT(p_inst != NULL); + return (app_usbd_hid_generic_t const *)p_inst; +} + + +/** + * @brief Returns report ID buffer descriptor. + * + * @param[in] p_generic Internal HID generic context. + * + * @return HID report buffer. + */ +static inline app_usbd_hid_report_buffer_t * +hid_generic_rep_buffer_get(app_usbd_hid_generic_t const * p_generic) +{ + ASSERT(p_generic != NULL); + app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst; + + return app_usbd_hid_rep_buff_in_get(p_hinst); +} + + +/** + * @brief Auxiliary function to prepare report transfer buffer to next transfer. + * + * @param[in] p_generic HID generic instance. + * + * @retval true Next transfer required. + * @retval false Next transfer not required. + */ +static inline bool hid_generic_transfer_next(app_usbd_hid_generic_t const * p_generic) +{ + nrf_queue_t const * p_rep_in_queue = p_generic->specific.inst.p_rep_in_queue; + + return !nrf_queue_is_empty(p_rep_in_queue); +} + + +/** + * @brief Triggers IN endpoint transfer. + * + * @param[in] p_generic HID generic instance. + * + * @return Standard error code. + */ +static inline ret_code_t hid_generic_transfer_set(app_usbd_hid_generic_t const * p_generic) +{ + app_usbd_class_inst_t const * p_inst = (app_usbd_class_inst_t const *)p_generic; + app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic); + + nrf_drv_usbd_ep_t ep_addr = app_usbd_hid_epin_addr_get(p_inst); + + app_usbd_hid_state_flag_clr(&p_generic_ctx->hid_ctx, + APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS); + + if (!hid_generic_transfer_next(p_generic)) + { + return NRF_SUCCESS; + } + + app_usbd_hid_report_buffer_t * p_rep_buff = hid_generic_rep_buffer_get(p_generic); + nrf_queue_t const * p_rep_in_queue = p_generic->specific.inst.p_rep_in_queue; + + ret_code_t ret = nrf_queue_pop(p_rep_in_queue, p_rep_buff); + ASSERT(ret == NRF_SUCCESS); + + NRF_DRV_USBD_TRANSFER_IN(transfer, p_rep_buff->p_buff, p_rep_buff->size); + CRITICAL_REGION_ENTER(); + ret = app_usbd_ep_transfer(ep_addr, &transfer); + if (ret == NRF_SUCCESS) + { + app_usbd_hid_state_flag_set(&p_generic_ctx->hid_ctx, + APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS); + } + CRITICAL_REGION_EXIT(); + + return ret; +} + + +ret_code_t app_usbd_hid_generic_in_report_set(app_usbd_hid_generic_t const * p_generic, + const void * p_buff, + size_t size) +{ + app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic); + nrf_queue_t const * p_rep_in_queue = p_generic->specific.inst.p_rep_in_queue; + const app_usbd_hid_report_buffer_t rep_buff = { + .p_buff = (void *)p_buff, + .size = size, + }; + + if (nrf_queue_push(p_rep_in_queue, &rep_buff) != NRF_SUCCESS) + { + return NRF_ERROR_BUSY; + } + + ret_code_t ret = NRF_SUCCESS; + if (app_usbd_hid_trans_required(&p_generic_ctx->hid_ctx)) + { + ret = hid_generic_transfer_set(p_generic); + } + + return ret; +} + + +const void * app_usbd_hid_generic_in_report_get(app_usbd_hid_generic_t const * p_generic, + size_t * p_size) +{ + app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst; + + *p_size = p_hinst->p_rep_buffer_in->size; + return p_hinst->p_rep_buffer_in->p_buff; +} + + +const void * app_usbd_hid_generic_out_report_get(app_usbd_hid_generic_t const * p_generic, + size_t * p_size) +{ + app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst; + + *p_size = p_hinst->p_rep_buffer_out->size; + return p_hinst->p_rep_buffer_out->p_buff; +} + + +/** + * @brief @ref app_usbd_hid_interface_t::on_get_report + */ +static ret_code_t hid_generic_on_get_report(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_hid_generic_t const * p_hinst = hid_generic_get(p_inst); + uint8_t const * p_rep_buffer = NULL; + size_t buffer_size = 0; + + if (p_setup_ev->setup.wValue.hb == APP_USBD_HID_REPORT_TYPE_INPUT) + { + p_rep_buffer = app_usbd_hid_generic_in_report_get(p_hinst, &buffer_size); + } + else if (p_setup_ev->setup.wValue.hb == APP_USBD_HID_REPORT_TYPE_OUTPUT) + { + p_rep_buffer = app_usbd_hid_generic_out_report_get(p_hinst, &buffer_size); + } + else + { + return NRF_ERROR_NOT_SUPPORTED; + } + + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_rep_buffer, buffer_size); +} + + +static ret_code_t hid_generic_on_set_report_data_cb(nrf_drv_usbd_ep_status_t status, + void * p_context) +{ + app_usbd_hid_user_ev_handler_t handler; + app_usbd_hid_generic_t const * p_generic = (app_usbd_hid_generic_t const *)p_context; + + if (status != NRF_USBD_EP_OK) + { + return NRF_ERROR_INTERNAL; + } + + handler = p_generic->specific.inst.hid_inst.user_event_handler; + handler((app_usbd_class_inst_t const *)p_generic, + APP_USBD_HID_USER_EVT_OUT_REPORT_READY); + return NRF_SUCCESS; +} + + +/** + * @brief @ref app_usbd_hid_interface_t::on_set_report + */ +static ret_code_t hid_generic_on_set_report(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst); + + /*Request setup data*/ + app_usbd_hid_report_buffer_t const * p_rep_buff; + + p_rep_buff = app_usbd_hid_rep_buff_out_get(&p_generic->specific.inst.hid_inst); + + p_rep_buff->p_buff[0] = p_setup_ev->setup.wValue.lb; + NRF_DRV_USBD_TRANSFER_OUT(transfer, p_rep_buff->p_buff + 1, p_rep_buff->size - 1); + + ret_code_t ret; + CRITICAL_REGION_ENTER(); + ret = app_usbd_ep_transfer(NRF_DRV_USBD_EPOUT0, &transfer); + if (ret == NRF_SUCCESS) + { + app_usbd_core_setup_data_handler_desc_t desc = { + .handler = hid_generic_on_set_report_data_cb, + .p_context = (void *)p_generic + }; + + ret = app_usbd_core_setup_data_handler_set(NRF_DRV_USBD_EPOUT0, &desc); + } + CRITICAL_REGION_EXIT(); + + return ret; +} + + +/** + * @brief @ref app_usbd_hid_interface_t::ep_transfer_in + */ +static ret_code_t hid_generic_ep_transfer_in(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst); + app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic); + + nrf_queue_t const * p_rep_in_queue = p_generic->specific.inst.p_rep_in_queue; + + if (nrf_queue_is_empty(p_rep_in_queue)) + { + app_usbd_hid_state_flag_clr(&p_generic_ctx->hid_ctx, + APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS); + return NRF_SUCCESS; + } + + /* Get next report to send */ + return hid_generic_transfer_set((app_usbd_hid_generic_t const *)p_inst); +} + + +/** + * @brief @ref app_usbd_hid_interface_t::ep_transfer_out + */ +static ret_code_t hid_generic_ep_transfer_out(app_usbd_class_inst_t const * p_inst) +{ + + app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst); + nrf_drv_usbd_ep_t ep_addr = app_usbd_hid_epout_addr_get(p_inst); + + /*Request setup data*/ + app_usbd_hid_report_buffer_t const * p_rep_buff; + + p_rep_buff = app_usbd_hid_rep_buff_out_get(&p_generic->specific.inst.hid_inst); + NRF_DRV_USBD_TRANSFER_OUT(transfer, p_rep_buff->p_buff, p_rep_buff->size); + + return app_usbd_ep_transfer(ep_addr, &transfer); +} + + +/** + * @brief @ref app_usbd_class_interface_t::event_handler + */ +static ret_code_t hid_generic_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_complex_evt_t const * p_event) +{ + ASSERT(p_inst != NULL); + ASSERT(p_event != NULL); + + app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst; + app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic); + app_usbd_hid_ctx_t * p_hid_ctx = &p_generic_ctx->hid_ctx; + + /*Try handle event by generic HID event handler*/ + return app_usbd_hid_event_handler(p_inst, p_hinst, p_hid_ctx, p_event); +} + + +static uint8_t hid_generic_get_class_descriptors_count(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst; + + return p_hinst->subclass_desc_count; +} + + +static app_usbd_descriptor_t hid_generic_get_class_descriptors_type( + app_usbd_class_inst_t const * p_inst, + uint8_t desc_num) +{ + app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst; + + return p_hinst->p_subclass_desc[desc_num]->type; +} + + +static size_t hid_generic_get_class_descriptors_length(app_usbd_class_inst_t const * p_inst, + uint8_t desc_num) +{ + app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst; + + return p_hinst->p_subclass_desc[desc_num]->size; +} + + +static uint8_t hid_generic_get_class_descriptors_data(app_usbd_class_inst_t const * p_inst, + uint8_t desc_num, + uint32_t cur_byte) +{ + app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst; + + return p_hinst->p_subclass_desc[desc_num]->p_data[cur_byte]; +} + + +/** + * @brief @ref app_usbd_class_interface_t::feed_descriptors + */ +static bool hid_generic_feed_descriptors(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size) +{ + static uint8_t ifaces = 0; + ifaces = app_usbd_class_iface_count_get(p_inst); + app_usbd_hid_generic_t const * p_generic = hid_generic_get(p_inst); + + APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size); + + static uint8_t i = 0; + + for (i = 0; i < ifaces; i++) + { + /* INTERFACE DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE); // bDescriptorType = Interface + + static app_usbd_class_iface_conf_t const * p_cur_iface = NULL; + p_cur_iface = app_usbd_class_iface_get(p_inst, i); + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_number_get(p_cur_iface)); // bInterfaceNumber + + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bAlternateSetting + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_ep_count_get(p_cur_iface)); // bNumEndpoints + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_CLASS); // bInterfaceClass = HID + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_generic->specific.inst.hid_inst.subclass_boot); // bInterfaceSubclass (Boot Interface) + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_generic->specific.inst.hid_inst.protocol); // bInterfaceProtocol + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iInterface + + /* HID DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_DESCRIPTOR_HID); // bDescriptorType = HID + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(APP_USBD_HID_BCD_VER)); // bcdHID LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(APP_USBD_HID_BCD_VER)); // bcdHID MSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_COUNTRY_NOT_SUPPORTED); // bCountryCode + APP_USBD_CLASS_DESCRIPTOR_WRITE(hid_generic_get_class_descriptors_count(p_inst)); // bNumDescriptors + + static uint8_t class_desc_cnt = 0; + class_desc_cnt = hid_generic_get_class_descriptors_count(p_inst); + static uint8_t j = 0; + static uint16_t class_desc_len = 0; + + for (j = 0; j < class_desc_cnt; j++) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(hid_generic_get_class_descriptors_type(p_inst, j)); // bDescriptorType + + class_desc_len = hid_generic_get_class_descriptors_length(p_inst, j); + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(class_desc_len)); // wDescriptorLength LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(class_desc_len)); // wDescriptorLength MSB + } + + static uint8_t endpoints = 0; + endpoints = app_usbd_class_iface_ep_count_get(p_cur_iface); + + for (j = 0; j < endpoints; j++) + { + /* ENDPOINT DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x07); // bLengths + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_ENDPOINT); // bDescriptorType = Endpoint + + static app_usbd_class_ep_conf_t const * p_cur_ep = NULL; + p_cur_ep = app_usbd_class_iface_ep_get(p_cur_iface, i); + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_ep_address_get(p_cur_ep)); // bEndpointAddress + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_INTERRUPT); // bmAttributes + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize MSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x01); // bInterval + } + } + + APP_USBD_CLASS_DESCRIPTOR_END(); +} + +/** + * @brief @ref app_usbd_hid_interface_t::feed_subclass_descriptor + */ + +static bool hid_generic_feed_subclass_descriptor(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size, + uint8_t index) +{ + APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size); + + /* PHYSICAL AND REPORT DESCRIPTORS */ + static uint32_t cur_byte = 0; + static size_t sub_desc_size = 0; + sub_desc_size = hid_generic_get_class_descriptors_length(p_inst, index); + + for (cur_byte = 0; cur_byte < sub_desc_size; cur_byte++) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(hid_generic_get_class_descriptors_data(p_inst, index, + cur_byte)); + } + + APP_USBD_CLASS_DESCRIPTOR_END(); +} + + +/** @} */ + +const app_usbd_hid_methods_t app_usbd_hid_generic_methods = { + .on_get_report = hid_generic_on_get_report, + .on_set_report = hid_generic_on_set_report, + .ep_transfer_in = hid_generic_ep_transfer_in, + .ep_transfer_out = hid_generic_ep_transfer_out, + .feed_subclass_descriptor = hid_generic_feed_subclass_descriptor, +}; + +const app_usbd_class_methods_t app_usbd_generic_class_methods = { + .event_handler = hid_generic_event_handler, + .feed_descriptors = hid_generic_feed_descriptors, +}; + +#endif //NRF_MODULE_ENABLED(APP_USBD_HID_GENERIC) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic.h new file mode 100644 index 0000000..3642b16 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic.h @@ -0,0 +1,223 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_HID_GENERIC_H__ +#define APP_USBD_HID_GENERIC_H__ + + + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdint.h> +#include <stdbool.h> + +#include "nrf_drv_usbd.h" +#include "app_usbd_class_base.h" +#include "app_usbd_hid_types.h" +#include "app_usbd_hid.h" +#include "app_usbd.h" +#include "app_usbd_core.h" +#include "app_usbd_descriptor.h" +#include "app_usbd_hid_generic_desc.h" +#include "app_usbd_hid_generic_internal.h" + +/** + * @defgroup app_usbd_hid_generic USB HID generic + * @ingroup app_usbd_hid + * + * @brief @tagAPI52840 Module with types, definitions, and API used by the HID generic class. + * @{ + */ + + +#ifdef DOXYGEN +/** + * @brief HID generic class instance type + * + * @ref APP_USBD_CLASS_TYPEDEF + */ +typedef struct { } app_usbd_hid_generic_t; +#else +/*lint -save -e10 -e26 -e123 -e505 */ +APP_USBD_CLASS_TYPEDEF(app_usbd_hid_generic, \ + APP_USBD_HID_GENERIC_CONFIG(0, (NRF_DRV_USBD_EPIN1, NRF_DRV_USBD_EPOUT1)), \ + APP_USBD_HID_GENERIC_INSTANCE_SPECIFIC_DEC, \ + APP_USBD_HID_GENERIC_DATA_SPECIFIC_DEC \ +); +/*lint -restore*/ +#endif + +/** + * @brief Global definition of app_usbd_hid_generic_t class. + * + * @param instance_name Name of global instance. + * @param interface_number Unique interface index. + * @param user_ev_handler User event handler (optional). + * @param endpoint_list Input endpoint list (@ref nrf_drv_usbd_ep_t). + * @param subclass_descriptors HID subclass descriptors. + * @param report_in_queue_size IN report queue size. + * @param report_out_maxsize Maximum output report size. + * @param subclass_boot Subclass boot. (@ref app_usbd_hid_subclass_t) + * @param protocol HID protocol. (@ref app_usbd_hid_protocol_t) + * + * @note This macro is just simplified version of @ref APP_USBD_HID_GENERIC_GLOBAL_DEF_INTERNAL. + * + * Example class definition: + * @code + + APP_USBD_HID_GENERIC_SUBCLASS_REPORT_DESC(mouse_desc,APP_USBD_HID_MOUSE_REPORT_DSC_BUTTON(2)); + + static const app_usbd_hid_subclass_desc_t * reps[] = {&mouse_desc}; + + #define ENDPOINT_LIST \ + ( \ + NRF_DRV_USBD_EPIN1 \ + ) + + #define REPORT_COUNT 1 + #define REPORT_OUT_MAXSIZE 0 + + APP_USBD_HID_GENERIC_GLOBAL_DEF(m_app_hid_generic, + 0, + hid_user_ev_handler, + ENDPOINT_LIST(), + reps, + REPORT_IN_QUEUE_SIZE, + REPORT_OUT_MAXSIZE, + APP_USBD_HID_SUBCLASS_BOOT, + APP_USBD_HID_PROTO_MOUSE); + @endcode + * + */ +#define APP_USBD_HID_GENERIC_GLOBAL_DEF(instance_name, \ + interface_number, \ + user_ev_handler, \ + endpoint_list, \ + subclass_descriptors, \ + report_in_queue_size, \ + report_out_maxsize, \ + subclass_boot, \ + protocol) \ + APP_USBD_HID_GENERIC_GLOBAL_DEF_INTERNAL(instance_name, \ + interface_number, \ + user_ev_handler, \ + endpoint_list, \ + subclass_descriptors, \ + report_in_queue_size, \ + report_out_maxsize, \ + subclass_boot, \ + protocol) + + +/** + * @brief Helper function to get class instance from HID generic class. + * + * @param[in] p_generic HID generic class instance. + * + * @return Base class instance. + */ +static inline app_usbd_class_inst_t const * +app_usbd_hid_generic_class_inst_get(app_usbd_hid_generic_t const * p_generic) +{ + return &p_generic->base; +} + +/** + * @brief Helper function to get HID generic from base class instance. + * + * @param[in] p_inst Base class instance. + * + * @return HID generic class handle. + */ +static inline app_usbd_hid_generic_t const * +app_usbd_hid_generic_class_get(app_usbd_class_inst_t const * p_inst) +{ + return (app_usbd_hid_generic_t const *)p_inst; +} + +/** + * @brief New IN report trigger + * + * + * @param[in] p_generic HID generic class instance. + * @param[in] p_buff Report buffer. + * @param[in] size Report size. + * + * @return Standard error code. + */ +ret_code_t app_usbd_hid_generic_in_report_set(app_usbd_hid_generic_t const * p_generic, + const void * p_buff, + size_t size); + +/** + * @brief Returns last successful transfered IN report. + * + * @note Use this call only on @ref APP_USBD_HID_USER_EVT_IN_REPORT_DONE event. + * + * @param[in] p_generic HID generic class instance. + * @param[out] p_size Last transfered IN report size. + * + * @return Last transfered report ID. + */ +const void * app_usbd_hid_generic_in_report_get(app_usbd_hid_generic_t const * p_generic, + size_t * p_size); + +/** + * @brief Returns last successful transfered OUT report. + * + * @warning Use this call only on @ref APP_USBD_HID_USER_EVT_OUT_REPORT_READY event. + * + * @param[in] p_generic HID generic class instance. + * @param[out] p_size Last transfered OUT report size. + * + * @return Last transfered OUT report. + */ +const void * app_usbd_hid_generic_out_report_get(app_usbd_hid_generic_t const * p_generic, + size_t * p_size); + + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_HID_GENERIC_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic_desc.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic_desc.h new file mode 100644 index 0000000..1ff4487 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic_desc.h @@ -0,0 +1,96 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_HID_GENERIC_DESC_H__ +#define APP_USBD_HID_GENERIC_DESC_H__ + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @defgroup app_usbd_hid_generic_desc USB HID generic descriptors + * @ingroup app_usbd_hid_generic + * + * @brief @tagAPI52840 Module with descriptors used by the HID generic class. + * @{ + */ + +/** + * @brief Initializer of interface descriptor for HID generic class. + * + * @param interface_number Interface number. + * @param endpoints_num Number of endpoints. + * @param subclass Subclass type @ref app_usbd_hid_subclass_t. + * @param protocol Protocol type @ref app_usbd_hid_protocol_t. + */ +#define APP_USBD_HID_GENERIC_INTERFACE_DSC(interface_number, \ + endpoints_num, \ + subclass, \ + protocol) \ + APP_USBD_HID_INTERFACE_DSC(interface_number, \ + endpoints_num, \ + subclass, \ + protocol) \ + +/** + * @brief Initializer of HID descriptor for HID generic class. + * + * @param ... Report descriptor item. + */ +#define APP_USBD_HID_GENERIC_HID_DSC(...) \ + APP_USBD_HID_HID_DSC(__VA_ARGS__) +/** + * @brief Initializer of endpoint descriptor for HID generic class. + * + * @param endpoint Endpoint number. + */ +#define APP_USBD_HID_GENERIC_EP_DSC(endpoint) \ + APP_USBD_HID_EP_DSC(endpoint, NRF_DRV_USBD_EPSIZE, 1) + + + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_HID_GENERIC_DESC_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic_internal.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic_internal.h new file mode 100644 index 0000000..4844fde --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic_internal.h @@ -0,0 +1,186 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_HID_GENERIC_INTERNAL_H__ +#define APP_USBD_HID_GENERIC_INTERNAL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "app_usbd_hid.h" +#include "nrf_queue.h" + +/** + * @defgroup app_usbd_hid_generic_internal USB HID generic internals + * @ingroup app_usbd_hid_generic + * + * @brief @tagAPI52840 Module with types, definitions, and API used by the HID generic protocol. + * @{ + */ + + +/** + * @brief Forward declaration of HID generic class type. + * + */ +APP_USBD_CLASS_FORWARD(app_usbd_hid_generic); + +/** + * @brief HID generic part of class instance data. + * + */ +typedef struct { + app_usbd_hid_inst_t hid_inst; //!< HID instance data. + nrf_queue_t const * p_rep_in_queue; //!< Input report queue. +} app_usbd_hid_generic_inst_t; + +/** + * @brief HID generic context + * + */ +typedef struct { + app_usbd_hid_ctx_t hid_ctx; //!< HID class context. +} app_usbd_hid_generic_ctx_t; + + +/** + * @brief HID generic configuration macro. + * + * Used by @ref APP_USBD_HID_GENERIC_GLOBAL_DEF. + * + * @param iface Interface number. + * @param endpoints Endpoint list. + */ +#define APP_USBD_HID_GENERIC_CONFIG(iface, endpoints) ((iface, BRACKET_EXTRACT(endpoints))) + + +/** + * @brief Specific class constant data for HID generic class. + */ +#define APP_USBD_HID_GENERIC_INSTANCE_SPECIFIC_DEC app_usbd_hid_generic_inst_t inst; + +/** + * @brief Specific class data for HID generic class. + */ +#define APP_USBD_HID_GENERIC_DATA_SPECIFIC_DEC app_usbd_hid_generic_ctx_t ctx; + +/** + * @brief Configure internal part of HID generic instance. + * + * @param report_buff_in Input report buffers array. + * @param report_buff_out Output report buffer. + * @param user_ev_handler User event handler. + * @param in_report_queue IN report queue. + * @param subclass_descriptors HID subclass descriptors. + * @param subclass_boot Subclass boot. (@ref app_usbd_hid_subclass_t) + * @param protocol HID protocol. (@ref app_usbd_hid_protocol_t) + */ +#define APP_USBD_HID_GENERIC_INST_CONFIG(report_buff_in, \ + report_buff_out, \ + user_ev_handler, \ + in_report_queue, \ + subclass_descriptors, \ + subclass_boot, \ + protocol) \ + .inst = { \ + .hid_inst = APP_USBD_HID_INST_CONFIG(subclass_descriptors, \ + subclass_boot, \ + protocol, \ + report_buff_in, \ + report_buff_out, \ + user_ev_handler, \ + &app_usbd_hid_generic_methods), \ + .p_rep_in_queue = in_report_queue, \ + } + +/** + * @brief Public HID generic interface. + */ +extern const app_usbd_hid_methods_t app_usbd_hid_generic_methods; + +/** + * @brief Public HID generic class interface. + */ +extern const app_usbd_class_methods_t app_usbd_generic_class_methods; + +/** + * @brief Global definition of @ref app_usbd_hid_generic_t class. + * + * @ref APP_USBD_HID_GENERIC_GLOBAL_DEF + */ +#define APP_USBD_HID_GENERIC_GLOBAL_DEF_INTERNAL(instance_name, \ + interface_number, \ + user_ev_handler, \ + endpoint_list, \ + subclass_descriptors, \ + report_in_queue_size, \ + report_out_maxsize, \ + subclass_boot, \ + protocol) \ + static app_usbd_hid_report_buffer_t CONCAT_2(instance_name, _in); \ + APP_USBD_HID_GENERIC_GLOBAL_OUT_REP_DEF(CONCAT_2(instance_name, _out), \ + report_out_maxsize + 1); \ + NRF_QUEUE_DEF(app_usbd_hid_report_buffer_t, \ + instance_name##_queue, \ + report_in_queue_size, \ + NRF_QUEUE_MODE_OVERFLOW); \ + APP_USBD_CLASS_INST_GLOBAL_DEF( \ + instance_name, \ + app_usbd_hid_generic, \ + &app_usbd_generic_class_methods, \ + APP_USBD_HID_GENERIC_CONFIG(interface_number, endpoint_list), \ + (APP_USBD_HID_GENERIC_INST_CONFIG(&CONCAT_2(instance_name, _in), \ + &CONCAT_2(instance_name, _out), \ + user_ev_handler, \ + &instance_name##_queue, \ + subclass_descriptors, \ + subclass_boot, \ + protocol)) \ + ) + + +/** @} */ + + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_HID_GENERIC_INTERNAL_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd.c new file mode 100644 index 0000000..53d03f5 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd.c @@ -0,0 +1,557 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(APP_USBD_HID_KBD) + +#include <string.h> +#include "app_usbd_hid_kbd.h" +#include "app_util_platform.h" + + +/** + * @defgroup app_usbd_hid_kbd_internals USB HID keyboard internals + * @{ + * @ingroup app_usbd_hid_kbd + * @internal + */ + +STATIC_ASSERT(sizeof(app_usbd_hid_descriptor_t) == 6); + +/** + * @brief Auxiliary function to access HID keyboard context data. + * + * @param[in] p_inst class instance data. + * + * @return HID keyboard instance data context. + */ +static inline app_usbd_hid_kbd_ctx_t * hid_kbd_ctx_get(app_usbd_hid_kbd_t const * p_kbd) +{ + ASSERT(p_kbd != NULL); + ASSERT(p_kbd->specific.p_data != NULL); + return &p_kbd->specific.p_data->ctx; +} + + +/** + * @brief Auxiliary function to access HID keyboard instance data. + * + * @param[in] p_inst class instance data. + * + * @return HID keyboard instance data. + */ +static inline app_usbd_hid_kbd_t const * hid_kbd_get(app_usbd_class_inst_t const * p_inst) +{ + ASSERT(p_inst != NULL); + return (app_usbd_hid_kbd_t const *)p_inst; +} + + +/** + * @brief Returns keyboard report buffer handle. + * + * @param[in] p_kbd HID keyboard instance. + * + * @return HID report buffer. + */ +static inline +app_usbd_hid_report_buffer_t const * hid_kbd_rep_buffer_get(app_usbd_hid_kbd_t const * p_kbd) +{ + ASSERT(p_kbd != NULL); + app_usbd_hid_inst_t const * p_hinst = &p_kbd->specific.inst.hid_inst; + app_usbd_hid_kbd_ctx_t * p_kbd_ctx = hid_kbd_ctx_get(p_kbd); + app_usbd_hid_report_buffer_t * p_rep_buff = app_usbd_hid_rep_buff_in_get(p_hinst); + + p_rep_buff->p_buff = p_kbd_ctx->report_buff; + p_rep_buff->size = sizeof(p_kbd_ctx->report_buff); + + /*Keyboard has only one report input/output report buffer */ + return p_rep_buff; +} + + +/** + * @brief Auxiliary function to prepare report transfer buffer to next transfer. + * + * @param[in] p_kbd HID keyboard instance. + * + * @retval true Next transfer is required. + * @retval false Next transfer is not required. + */ +static inline bool hid_kbd_transfer_next(app_usbd_hid_kbd_t const * p_kbd) +{ + /*Send report only when state has changed*/ + app_usbd_hid_report_buffer_t const * p_rep_buffer = hid_kbd_rep_buffer_get(p_kbd); + app_usbd_hid_kbd_ctx_t * p_kbd_ctx = hid_kbd_ctx_get(p_kbd); + + if (memcmp(p_rep_buffer->p_buff, &p_kbd_ctx->rep, p_rep_buffer->size)) + { + memcpy(p_rep_buffer->p_buff, &p_kbd_ctx->rep, p_rep_buffer->size); + return true; + } + + return false; +} + + +/** + * @brief Triggers IN endpoint transfer. + * + * @param[in] p_kbd HID keyboard instance. + * + * @return Standard error code. + */ +static inline ret_code_t hid_kbd_transfer_set(app_usbd_hid_kbd_t const * p_kbd) +{ + app_usbd_class_inst_t const * p_inst = (app_usbd_class_inst_t const *)p_kbd; + app_usbd_hid_kbd_ctx_t * p_kbd_ctx = hid_kbd_ctx_get(p_kbd); + + nrf_drv_usbd_ep_t ep_addr = app_usbd_hid_epin_addr_get(p_inst); + + app_usbd_hid_state_flag_clr(&p_kbd_ctx->hid_ctx, APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS); + + if (!hid_kbd_transfer_next(p_kbd)) + { + /* Transfer buffer hasn't changed since last transfer. No need to setup + * next transfer. + * */ + return NRF_SUCCESS; + } + + app_usbd_hid_report_buffer_t const * p_rep_buffer = hid_kbd_rep_buffer_get(p_kbd); + NRF_DRV_USBD_TRANSFER_IN(transfer, p_rep_buffer->p_buff, p_rep_buffer->size); + + ret_code_t ret; + CRITICAL_REGION_ENTER(); + ret = app_usbd_ep_transfer(ep_addr, &transfer); + if (ret == NRF_SUCCESS) + { + app_usbd_hid_state_flag_set(&p_kbd_ctx->hid_ctx, APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS); + } + CRITICAL_REGION_EXIT(); + + return ret; +} + + +ret_code_t app_usbd_hid_kbd_modifier_state_set(app_usbd_hid_kbd_t const * p_kbd, + app_usbd_hid_kbd_modifier_t modifier, + bool state) +{ + app_usbd_hid_kbd_ctx_t * p_kbd_ctx = hid_kbd_ctx_get(p_kbd); + bool actual_state = (p_kbd_ctx->rep.modifier & modifier) != 0; + + if (actual_state == state) + { + /*Modifier has already the same state*/ + return NRF_SUCCESS; + } + + app_usbd_hid_access_lock(&p_kbd_ctx->hid_ctx); + + if (state) + { + p_kbd_ctx->rep.modifier |= modifier; + } + else + { + p_kbd_ctx->rep.modifier &= ~modifier; + } + app_usbd_hid_access_unlock(&p_kbd_ctx->hid_ctx); + + if (app_usbd_hid_trans_required(&p_kbd_ctx->hid_ctx)) + { + /*New transfer need to be triggered*/ + return hid_kbd_transfer_set(p_kbd); + } + + return NRF_SUCCESS; +} + + +ret_code_t app_usbd_hid_kbd_key_control(app_usbd_hid_kbd_t const * p_kbd, + app_usbd_hid_kbd_codes_t key, + bool press) +{ + app_usbd_hid_kbd_ctx_t * p_kbd_ctx = hid_kbd_ctx_get(p_kbd); + uint8_t * destination = NULL; + + if (press) + { + for (size_t i = 0; i < ARRAY_SIZE(p_kbd_ctx->rep.key_table); ++i) + { + if (p_kbd_ctx->rep.key_table[i] == key) + { + /*Already pressed*/ + return NRF_SUCCESS; + } + + if ((destination == NULL) && (p_kbd_ctx->rep.key_table[i] == 0)) + { + destination = &p_kbd_ctx->rep.key_table[i]; + } + } + + if (destination == NULL) + { + return NRF_ERROR_BUSY; + } + } + else + { + /*Find if key is pressed*/ + for (size_t i = 0; i < ARRAY_SIZE(p_kbd_ctx->rep.key_table); ++i) + { + if (p_kbd_ctx->rep.key_table[i] == key) + { + destination = &p_kbd_ctx->rep.key_table[i]; + break; + } + } + + if (destination == NULL) + { + /*Key hasn't been pressed*/ + return NRF_SUCCESS; + } + } + + /*Save destination*/ + app_usbd_hid_access_lock(&p_kbd_ctx->hid_ctx); + *destination = press ? key : 0; + app_usbd_hid_access_unlock(&p_kbd_ctx->hid_ctx); + + if (app_usbd_hid_trans_required(&p_kbd_ctx->hid_ctx)) + { + /*New transfer need to be triggered*/ + return hid_kbd_transfer_set(p_kbd); + } + + return NRF_SUCCESS; +} + + +bool app_usbd_hid_kbd_led_state_get(app_usbd_hid_kbd_t const * p_kbd, + app_usbd_hid_kbd_led_t led) +{ + app_usbd_hid_kbd_ctx_t * p_kbd_ctx = hid_kbd_ctx_get(p_kbd); + + return (p_kbd_ctx->leds_state & led) != 0; +} + +const void * app_usbd_hid_kbd_in_report_get(app_usbd_hid_kbd_t const * p_kbd, + size_t * p_size) +{ + app_usbd_hid_inst_t const * p_kinst = &p_kbd->specific.inst.hid_inst; + *p_size = p_kinst->p_rep_buffer_in->size; + return p_kinst->p_rep_buffer_in->p_buff; +} + +const void * app_usbd_hid_kbd_out_report_get(app_usbd_hid_kbd_t const * p_kbd, + size_t * p_size) +{ + app_usbd_hid_inst_t const * p_kinst = &p_kbd->specific.inst.hid_inst; + *p_size = p_kinst->p_rep_buffer_out->size; + return p_kinst->p_rep_buffer_out->p_buff; +} + +/** + * @brief @ref app_usbd_hid_interface_t::on_get_report + */ +static ret_code_t hid_kbd_on_get_report(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_hid_kbd_t const * p_kinst = hid_kbd_get(p_inst); + uint8_t const * p_rep_buffer = NULL; + size_t buffer_size = 0; + + if (p_setup_ev->setup.wValue.hb == APP_USBD_HID_REPORT_TYPE_INPUT) + { + p_rep_buffer = app_usbd_hid_kbd_in_report_get(p_kinst, &buffer_size); + } + else if (p_setup_ev->setup.wValue.hb == APP_USBD_HID_REPORT_TYPE_OUTPUT) + { + p_rep_buffer = app_usbd_hid_kbd_out_report_get(p_kinst, &buffer_size); + } + else + { + return NRF_ERROR_NOT_SUPPORTED; + } + + /* Return LEDs state (only the second byte) */ + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_rep_buffer + 1, buffer_size - 1); +} + + +static ret_code_t hid_kbd_on_set_report_data_cb(nrf_drv_usbd_ep_status_t status, void * p_context) +{ + if (status != NRF_USBD_EP_OK) + { + return NRF_ERROR_INTERNAL; + } + + app_usbd_hid_kbd_t const * p_kbd = p_context; + app_usbd_hid_report_buffer_t const * p_rep_buff; + p_rep_buff = app_usbd_hid_rep_buff_out_get(&p_kbd->specific.inst.hid_inst); + + /*Update LEDs state*/ + app_usbd_hid_kbd_ctx_t * p_kbd_ctx = hid_kbd_ctx_get(p_kbd); + p_kbd_ctx->leds_state = p_rep_buff->p_buff[1]; + + app_usbd_hid_user_ev_handler_t handler = p_kbd->specific.inst.hid_inst.user_event_handler; + handler((app_usbd_class_inst_t const *)(p_kbd), APP_USBD_HID_USER_EVT_OUT_REPORT_READY); + return NRF_SUCCESS; +} + + +/** + * @brief @ref app_usbd_hid_interface_t::hid_kbd_on_set_report + */ +static ret_code_t hid_kbd_on_set_report(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_hid_kbd_t const * p_kbd = hid_kbd_get(p_inst); + + /*Request setup data*/ + app_usbd_hid_report_buffer_t const * p_rep_buff; + + p_rep_buff = app_usbd_hid_rep_buff_out_get(&p_kbd->specific.inst.hid_inst); + + p_rep_buff->p_buff[0] = 0; + NRF_DRV_USBD_TRANSFER_OUT(transfer, p_rep_buff->p_buff + 1, p_rep_buff->size - 1); + + ret_code_t ret; + CRITICAL_REGION_ENTER(); + ret = app_usbd_ep_transfer(NRF_DRV_USBD_EPOUT0, &transfer); + if (ret == NRF_SUCCESS) + { + app_usbd_core_setup_data_handler_desc_t desc = { + .handler = hid_kbd_on_set_report_data_cb, + .p_context = (app_usbd_hid_kbd_t *)p_kbd + }; + + ret = app_usbd_core_setup_data_handler_set(NRF_DRV_USBD_EPOUT0, &desc); + } + CRITICAL_REGION_EXIT(); + + return ret; +} + + +/** + * @brief @ref app_usbd_hid_interface_t::hid_kbd_ep_transfer_in + */ +static ret_code_t hid_kbd_ep_transfer_in(app_usbd_class_inst_t const * p_inst) +{ + return hid_kbd_transfer_set((app_usbd_hid_kbd_t const *)p_inst); +} + + +/** + * @brief @ref app_usbd_class_interface_t::event_handler + */ +static ret_code_t hid_kbd_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_complex_evt_t const * p_event) +{ + ASSERT(p_inst != NULL); + ASSERT(p_event != NULL); + + app_usbd_hid_kbd_t const * p_kbd = hid_kbd_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_kbd->specific.inst.hid_inst; + + app_usbd_hid_kbd_ctx_t * p_kbd_ctx = hid_kbd_ctx_get(p_kbd); + app_usbd_hid_ctx_t * p_hid_ctx = &p_kbd_ctx->hid_ctx; + + /*Try handle event by generic HID event handler*/ + return app_usbd_hid_event_handler(p_inst, p_hinst, p_hid_ctx, p_event); +} + + +static uint8_t hid_kbd_get_class_descriptors_count(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_hid_kbd_t const * p_kbd = hid_kbd_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_kbd->specific.inst.hid_inst; + + return p_hinst->subclass_desc_count; +} + + +static app_usbd_descriptor_t hid_kbd_get_class_descriptors_type( + app_usbd_class_inst_t const * p_inst, + uint8_t desc_num) +{ + app_usbd_hid_kbd_t const * p_kbd = hid_kbd_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_kbd->specific.inst.hid_inst; + + return p_hinst->p_subclass_desc[desc_num]->type; +} + + +static size_t hid_kbd_get_class_descriptors_length(app_usbd_class_inst_t const * p_inst, + uint8_t desc_num) +{ + app_usbd_hid_kbd_t const * p_kbd = hid_kbd_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_kbd->specific.inst.hid_inst; + + return p_hinst->p_subclass_desc[desc_num]->size; +} + + +static uint8_t hid_kbd_get_class_descriptors_data(app_usbd_class_inst_t const * p_inst, + uint8_t desc_num, + uint32_t cur_byte) +{ + app_usbd_hid_kbd_t const * p_kbd = hid_kbd_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_kbd->specific.inst.hid_inst; + + return p_hinst->p_subclass_desc[desc_num]->p_data[cur_byte]; +} + + +static bool hid_kbd_feed_descriptors(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size) +{ + static uint8_t ifaces = 0; + ifaces = app_usbd_class_iface_count_get(p_inst); + app_usbd_hid_kbd_t const * p_kbd = hid_kbd_get(p_inst); + + APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size); + + static uint8_t i = 0; + + for (i = 0; i < ifaces; i++) + { + /* INTERFACE DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE); // bDescriptorType = Interface + + static app_usbd_class_iface_conf_t const * p_cur_iface = NULL; + p_cur_iface = app_usbd_class_iface_get(p_inst, i); + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_number_get(p_cur_iface)); // bInterfaceNumber + + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bAlternateSetting + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_ep_count_get(p_cur_iface)); // bNumEndpoints + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_CLASS); // bInterfaceClass = HID + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_kbd->specific.inst.hid_inst.subclass_boot); // bInterfaceSubclass (Boot Interface) + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_kbd->specific.inst.hid_inst.protocol); // bInterfaceProtocol + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iInterface + + /* HID DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_DESCRIPTOR_HID); // bDescriptorType = HID + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(APP_USBD_HID_BCD_VER)); // bcdHID LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(APP_USBD_HID_BCD_VER)); // bcdHID MSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_COUNTRY_NOT_SUPPORTED); // bCountryCode + APP_USBD_CLASS_DESCRIPTOR_WRITE(hid_kbd_get_class_descriptors_count(p_inst)); // bNumDescriptors + + static uint8_t class_desc_cnt = 0; + class_desc_cnt = hid_kbd_get_class_descriptors_count(p_inst); + static uint8_t j = 0; + static uint16_t class_desc_len = 0; + + for (j = 0; j < class_desc_cnt; j++) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(hid_kbd_get_class_descriptors_type(p_inst, j)); // bDescriptorType + + class_desc_len = hid_kbd_get_class_descriptors_length(p_inst, j); + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(class_desc_len)); // wDescriptorLength LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(class_desc_len)); // wDescriptorLength MSB + } + + static uint8_t endpoints = 0; + endpoints = app_usbd_class_iface_ep_count_get(p_cur_iface); + + for (j = 0; j < endpoints; j++) + { + /* ENDPOINT DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x07); // bLengths + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_ENDPOINT); // bDescriptorType = Endpoint + + static app_usbd_class_ep_conf_t const * p_cur_ep = NULL; + p_cur_ep = app_usbd_class_iface_ep_get(p_cur_iface, i); + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_ep_address_get(p_cur_ep)); // bEndpointAddress + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_INTERRUPT); // bmAttributes + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize MSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x01); // bInterval + } + } + + APP_USBD_CLASS_DESCRIPTOR_END(); +} + +static bool hid_kbd_feed_subclass_descriptor(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size, + uint8_t index) +{ + APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size); + + /* PHYSICAL AND REPORT DESCRIPTORS */ + static uint32_t cur_byte = 0; + static size_t sub_desc_size = 0; + sub_desc_size = hid_kbd_get_class_descriptors_length(p_inst, index); + + for (cur_byte = 0; cur_byte <= sub_desc_size; cur_byte++) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(hid_kbd_get_class_descriptors_data(p_inst, index, cur_byte)); + } + + APP_USBD_CLASS_DESCRIPTOR_END(); +} + +/** @} */ + +const app_usbd_hid_methods_t app_usbd_hid_kbd_methods = { + .on_get_report = hid_kbd_on_get_report, + .on_set_report = hid_kbd_on_set_report, + .ep_transfer_in = hid_kbd_ep_transfer_in, + .ep_transfer_out = NULL, + .feed_subclass_descriptor = hid_kbd_feed_subclass_descriptor, +}; + +const app_usbd_class_methods_t app_usbd_hid_kbd_class_methods = { + .event_handler = hid_kbd_event_handler, + .feed_descriptors = hid_kbd_feed_descriptors, +}; + +#endif //NRF_MODULE_ENABLED(APP_USBD_HID_KBD) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd.h new file mode 100644 index 0000000..2343829 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd.h @@ -0,0 +1,309 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_HID_KBD_H__ +#define APP_USBD_HID_KBD_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdint.h> +#include <stdbool.h> + +#include "nrf_drv_usbd.h" +#include "app_usbd_class_base.h" +#include "app_usbd_hid_types.h" +#include "app_usbd_hid.h" +#include "app_usbd.h" +#include "app_usbd_core.h" +#include "app_usbd_descriptor.h" +#include "app_usbd_hid_kbd_desc.h" +#include "app_usbd_hid_kbd_internal.h" + + +/** + * @defgroup app_usbd_hid_kbd USB HID keyboard + * @ingroup app_usbd_hid + * + * @brief @tagAPI52840 Module with types, definitions, and API used by the HID keyboard class. + * @{ + */ + +/** + * @brief HID keyboard codes. + */ +typedef enum { + APP_USBD_HID_KBD_A = 4, /**<KBD_A code*/ + APP_USBD_HID_KBD_B = 5, /**<KBD_B code*/ + APP_USBD_HID_KBD_C = 6, /**<KBD_C code*/ + APP_USBD_HID_KBD_D = 7, /**<KBD_D code*/ + APP_USBD_HID_KBD_E = 8, /**<KBD_E code*/ + APP_USBD_HID_KBD_F = 9, /**<KBD_F code*/ + APP_USBD_HID_KBD_G = 10, /**<KBD_G code*/ + APP_USBD_HID_KBD_H = 11, /**<KBD_H code*/ + APP_USBD_HID_KBD_I = 12, /**<KBD_I code*/ + APP_USBD_HID_KBD_J = 13, /**<KBD_J code*/ + APP_USBD_HID_KBD_K = 14, /**<KBD_K code*/ + APP_USBD_HID_KBD_L = 15, /**<KBD_L code*/ + APP_USBD_HID_KBD_M = 16, /**<KBD_M code*/ + APP_USBD_HID_KBD_N = 17, /**<KBD_N code*/ + APP_USBD_HID_KBD_O = 18, /**<KBD_O code*/ + APP_USBD_HID_KBD_P = 19, /**<KBD_P code*/ + APP_USBD_HID_KBD_Q = 20, /**<KBD_Q code*/ + APP_USBD_HID_KBD_R = 21, /**<KBD_R code*/ + APP_USBD_HID_KBD_S = 22, /**<KBD_S code*/ + APP_USBD_HID_KBD_T = 23, /**<KBD_T code*/ + APP_USBD_HID_KBD_U = 24, /**<KBD_U code*/ + APP_USBD_HID_KBD_V = 25, /**<KBD_V code*/ + APP_USBD_HID_KBD_W = 26, /**<KBD_W code*/ + APP_USBD_HID_KBD_X = 27, /**<KBD_X code*/ + APP_USBD_HID_KBD_Y = 28, /**<KBD_Y code*/ + APP_USBD_HID_KBD_Z = 29, /**<KBD_Z code*/ + APP_USBD_HID_KBD_1 = 30, /**<KBD_1 code*/ + APP_USBD_HID_KBD_2 = 31, /**<KBD_2 code*/ + APP_USBD_HID_KBD_3 = 32, /**<KBD_3 code*/ + APP_USBD_HID_KBD_4 = 33, /**<KBD_4 code*/ + APP_USBD_HID_KBD_5 = 34, /**<KBD_5 code*/ + APP_USBD_HID_KBD_6 = 35, /**<KBD_6 code*/ + APP_USBD_HID_KBD_7 = 36, /**<KBD_7 code*/ + APP_USBD_HID_KBD_8 = 37, /**<KBD_8 code*/ + APP_USBD_HID_KBD_9 = 38, /**<KBD_9 code*/ + APP_USBD_HID_KBD_0 = 39, /**<KBD_0 code*/ + APP_USBD_HID_KBD_ENTER = 40, /**<KBD_ENTER code*/ + APP_USBD_HID_KBD_ESCAPE = 41, /**<KBD_ESCAPE code*/ + APP_USBD_HID_KBD_BACKSPACE = 42, /**<KBD_BACKSPACE code*/ + APP_USBD_HID_KBD_TAB = 43, /**<KBD_TAB code*/ + APP_USBD_HID_KBD_SPACEBAR = 44, /**<KBD_SPACEBAR code*/ + APP_USBD_HID_KBD_UNDERSCORE = 45, /**<KBD_UNDERSCORE code*/ + APP_USBD_HID_KBD_PLUS = 46, /**<KBD_PLUS code*/ + APP_USBD_HID_KBD_OPEN_BRACKET = 47, /**<KBD_OPEN_BRACKET code*/ + APP_USBD_HID_KBD_CLOSE_BRACKET = 48, /**<KBD_CLOSE_BRACKET code*/ + APP_USBD_HID_KBD_BACKSLASH = 49, /**<KBD_BACKSLASH code*/ + APP_USBD_HID_KBD_ASH = 50, /**<KBD_ASH code*/ + APP_USBD_HID_KBD_COLON = 51, /**<KBD_COLON code*/ + APP_USBD_HID_KBD_QUOTE = 52, /**<KBD_QUOTE code*/ + APP_USBD_HID_KBD_TILDE = 53, /**<KBD_TILDE code*/ + APP_USBD_HID_KBD_COMMA = 54, /**<KBD_COMMA code*/ + APP_USBD_HID_KBD_DOT = 55, /**<KBD_DOT code*/ + APP_USBD_HID_KBD_SLASH = 56, /**<KBD_SLASH code*/ + APP_USBD_HID_KBD_CAPS_LOCK = 57, /**<KBD_CAPS_LOCK code*/ + APP_USBD_HID_KBD_F1 = 58, /**<KBD_F1 code*/ + APP_USBD_HID_KBD_F2 = 59, /**<KBD_F2 code*/ + APP_USBD_HID_KBD_F3 = 60, /**<KBD_F3 code*/ + APP_USBD_HID_KBD_F4 = 61, /**<KBD_F4 code*/ + APP_USBD_HID_KBD_F5 = 62, /**<KBD_F5 code*/ + APP_USBD_HID_KBD_F6 = 63, /**<KBD_F6 code*/ + APP_USBD_HID_KBD_F7 = 64, /**<KBD_F7 code*/ + APP_USBD_HID_KBD_F8 = 65, /**<KBD_F8 code*/ + APP_USBD_HID_KBD_F9 = 66, /**<KBD_F9 code*/ + APP_USBD_HID_KBD_F10 = 67, /**<KBD_F10 code*/ + APP_USBD_HID_KBD_F11 = 68, /**<KBD_F11 code*/ + APP_USBD_HID_KBD_F12 = 69, /**<KBD_F12 code*/ + APP_USBD_HID_KBD_PRINTSCREEN = 70, /**<KBD_PRINTSCREEN code*/ + APP_USBD_HID_KBD_SCROLL_LOCK = 71, /**<KBD_SCROLL_LOCK code*/ + APP_USBD_HID_KBD_PAUSE = 72, /**<KBD_PAUSE code*/ + APP_USBD_HID_KBD_INSERT = 73, /**<KBD_INSERT code*/ + APP_USBD_HID_KBD_HOME = 74, /**<KBD_HOME code*/ + APP_USBD_HID_KBD_PAGEUP = 75, /**<KBD_PAGEUP code*/ + APP_USBD_HID_KBD_DELETE = 76, /**<KBD_DELETE code*/ + APP_USBD_HID_KBD_END = 77, /**<KBD_END code*/ + APP_USBD_HID_KBD_PAGEDOWN = 78, /**<KBD_PAGEDOWN code*/ + APP_USBD_HID_KBD_RIGHT = 79, /**<KBD_RIGHT code*/ + APP_USBD_HID_KBD_LEFT = 80, /**<KBD_LEFT code*/ + APP_USBD_HID_KBD_DOWN = 81, /**<KBD_DOWN code*/ + APP_USBD_HID_KBD_UP = 82, /**<KBD_UP code*/ + APP_USBD_HID_KBD_KEYPAD_NUM_LOCK = 83, /**<KBD_KEYPAD_NUM_LOCK code*/ + APP_USBD_HID_KBD_KEYPAD_DIVIDE = 84, /**<KBD_KEYPAD_DIVIDE code*/ + APP_USBD_HID_KBD_KEYPAD_AT = 85, /**<KBD_KEYPAD_AT code*/ + APP_USBD_HID_KBD_KEYPAD_MULTIPLY = 85, /**<KBD_KEYPAD_MULTIPLY code*/ + APP_USBD_HID_KBD_KEYPAD_MINUS = 86, /**<KBD_KEYPAD_MINUS code*/ + APP_USBD_HID_KBD_KEYPAD_PLUS = 87, /**<KBD_KEYPAD_PLUS code*/ + APP_USBD_HID_KBD_KEYPAD_ENTER = 88, /**<KBD_KEYPAD_ENTER code*/ + APP_USBD_HID_KBD_KEYPAD_1 = 89, /**<KBD_KEYPAD_1 code*/ + APP_USBD_HID_KBD_KEYPAD_2 = 90, /**<KBD_KEYPAD_2 code*/ + APP_USBD_HID_KBD_KEYPAD_3 = 91, /**<KBD_KEYPAD_3 code*/ + APP_USBD_HID_KBD_KEYPAD_4 = 92, /**<KBD_KEYPAD_4 code*/ + APP_USBD_HID_KBD_KEYPAD_5 = 93, /**<KBD_KEYPAD_5 code*/ + APP_USBD_HID_KBD_KEYPAD_6 = 94, /**<KBD_KEYPAD_6 code*/ + APP_USBD_HID_KBD_KEYPAD_7 = 95, /**<KBD_KEYPAD_7 code*/ + APP_USBD_HID_KBD_KEYPAD_8 = 96, /**<KBD_KEYPAD_8 code*/ + APP_USBD_HID_KBD_KEYPAD_9 = 97, /**<KBD_KEYPAD_9 code*/ + APP_USBD_HID_KBD_KEYPAD_0 = 98, /**<KBD_KEYPAD_0 code*/ +} app_usbd_hid_kbd_codes_t; + +/** + * @brief HID keyboard modifier + */ +typedef enum { + APP_USBD_HID_KBD_MODIFIER_NONE = 0x00, /**< MODIFIER_NONE bit*/ + APP_USBD_HID_KBD_MODIFIER_LEFT_CTRL = 0x01, /**< MODIFIER_LEFT_CTRL bit*/ + APP_USBD_HID_KBD_MODIFIER_LEFT_SHIFT = 0x02, /**< MODIFIER_LEFT_SHIFT bit*/ + APP_USBD_HID_KBD_MODIFIER_LEFT_ALT = 0x04, /**< MODIFIER_LEFT_ALT bit*/ + APP_USBD_HID_KBD_MODIFIER_LEFT_UI = 0x08, /**< MODIFIER_LEFT_UI bit*/ + APP_USBD_HID_KBD_MODIFIER_RIGHT_CTRL = 0x10, /**< MODIFIER_RIGHT_CTRL bit*/ + APP_USBD_HID_KBD_MODIFIER_RIGHT_SHIFT = 0x20, /**< MODIFIER_RIGHT_SHIFT bit*/ + APP_USBD_HID_KBD_MODIFIER_RIGHT_ALT = 0x40, /**< MODIFIER_RIGHT_ALT bit*/ + APP_USBD_HID_KBD_MODIFIER_RIGHT_UI = 0x80, /**< MODIFIER_RIGHT_UI bit*/ +} app_usbd_hid_kbd_modifier_t; + +/** + * @brief HID keyboard LEDs. + */ +typedef enum { + APP_USBD_HID_KBD_LED_NUM_LOCK = 0x01, /**< LED_NUM_LOCK id*/ + APP_USBD_HID_KBD_LED_CAPS_LOCK = 0x02, /**< LED_CAPS_LOCK id*/ + APP_USBD_HID_KBD_LED_SCROLL_LOCK = 0x04, /**< LED_SCROLL_LOCK id*/ + APP_USBD_HID_KBD_LED_COMPOSE = 0x08, /**< LED_COMPOSE id*/ + APP_USBD_HID_KBD_LED_KANA = 0x10, /**< LED_KANA id*/ +} app_usbd_hid_kbd_led_t; + + + +#ifdef DOXYGEN +/** + * @brief HID keyboard class instance type + * + * @ref APP_USBD_CLASS_TYPEDEF + */ +typedef struct { } app_usbd_hid_kbd_t; +#else +/*lint -save -e10 -e26 -e123 -e505 */ +APP_USBD_CLASS_TYPEDEF(app_usbd_hid_kbd, \ + APP_USBD_HID_KBD_CONFIG(0, NRF_DRV_USBD_EPIN1), \ + APP_USBD_HID_KBD_INSTANCE_SPECIFIC_DEC, \ + APP_USBD_HID_KBD_DATA_SPECIFIC_DEC \ +); +/*lint -restore*/ +#endif + +/** + * @brief Global definition of app_usbd_hid_kbd_t class. + * + * @param instance_name Name of global instance. + * @param interface_number Unique interface index. + * @param endpoint Input endpoint (@ref nrf_drv_usbd_ep_t). + * @param user_ev_handler User event handler (optional parameter: NULL might be passed here). + * @param subclass_boot Subclass boot. (@ref app_usbd_hid_subclass_t) + * + * Example class definition: + * @code + APP_USBD_HID_KBD_GLOBAL_DEF(my_awesome_kbd, 0, NRF_DRV_USBD_EPIN1, NULL, APP_USBD_HID_SUBCLASS_BOOT); + * @endcode + */ +#define APP_USBD_HID_KBD_GLOBAL_DEF(instance_name, interface_number, endpoint, user_ev_handler, subclass_boot) \ + APP_USBD_HID_GENERIC_SUBCLASS_REPORT_DESC(keyboard_desc, APP_USBD_HID_KBD_REPORT_DSC()); \ + static const app_usbd_hid_subclass_desc_t * keyboard_descs[] = {&keyboard_desc}; \ + APP_USBD_HID_KBD_GLOBAL_DEF_INTERNAL(instance_name, \ + interface_number, \ + endpoint, \ + user_ev_handler, \ + subclass_boot) + +/** + * @brief Helper function to get class instance from HID keyboard internals. + * + * @param[in] p_kbd Keyboard instance (declared by @ref APP_USBD_HID_KBD_GLOBAL_DEF). + * + * @return Base class instance. + */ +static inline app_usbd_class_inst_t const * +app_usbd_hid_kbd_class_inst_get(app_usbd_hid_kbd_t const * p_kbd) +{ + return &p_kbd->base; +} + +/** + * @brief Helper function to get HID keyboard from base class instance. + * + * @param[in] p_inst Base class instance. + * + * @return HID keyboard class handle. + */ +static inline app_usbd_hid_kbd_t const * +app_usbd_hid_kbd_class_get(app_usbd_class_inst_t const * p_inst) +{ + return (app_usbd_hid_kbd_t const *)p_inst; +} + +/** + * @brief Set HID keyboard modifier state. + * + * @param[in] p_kbd Keyboard instance (declared by @ref APP_USBD_HID_KBD_GLOBAL_DEF). + * @param[in] modifier Type of modifier. + * @param[in] state State, true active, false inactive. + * + * @return Standard error code. + */ +ret_code_t app_usbd_hid_kbd_modifier_state_set(app_usbd_hid_kbd_t const * p_kbd, + app_usbd_hid_kbd_modifier_t modifier, + bool state); + + +/** + * @brief Press/release HID keyboard key. + * + * @param[in] p_kbd Keyboard instance (declared by @ref APP_USBD_HID_KBD_GLOBAL_DEF). + * @param[in] key Keyboard key code. + * @param[in] press True -> key press, false -> release. + * + * @return Standard error code. + */ +ret_code_t app_usbd_hid_kbd_key_control(app_usbd_hid_kbd_t const * p_kbd, + app_usbd_hid_kbd_codes_t key, + bool press); +/** + * @brief HID Keyboard LEDs state get. + * + * @param[in] p_kbd Keyboard instance (declared by @ref APP_USBD_HID_KBD_GLOBAL_DEF). + * @param[in] led LED code. + * + * @retval true LED is set. + * @retval false LED is not set. + */ +bool app_usbd_hid_kbd_led_state_get(app_usbd_hid_kbd_t const * p_kbd, + app_usbd_hid_kbd_led_t led); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_HID_KBD_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd_desc.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd_desc.h new file mode 100644 index 0000000..1083b42 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd_desc.h @@ -0,0 +1,128 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_HID_KBD_DESC_H__ +#define APP_USBD_HID_KBD_DESC_H__ + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @defgroup app_usbd_hid_kbd_desc USB HID keyboard descriptors + * @ingroup app_usbd_hid_kbd + * + * @brief @tagAPI52840 Module with types, definitions, and API used by the HID keyboard class. + * @{ + */ + +/** + * @brief Initializer of interface descriptor for HID keyboard class. + * + * @param interface_number Interface number. + */ +#define APP_USBD_HID_KBD_INTERFACE_DSC(interface_number) \ + APP_USBD_HID_INTERFACE_DSC(interface_number, \ + 1, \ + APP_USBD_HID_SUBCLASS_BOOT, \ + APP_USBD_HID_PROTO_KEYBOARD) + +/** + * @brief Initializer of HID descriptor for HID keyboard class. + * + * @param ... Report descriptor item. + */ +#define APP_USBD_HID_KBD_HID_DSC(...) \ + APP_USBD_HID_HID_DSC(__VA_ARGS__) +/** + * @brief Initializer of endpoint descriptor for HID keyboard class. + * + * @param endpoint Endpoint number. + */ +#define APP_USBD_HID_KBD_EP_DSC(endpoint) \ + APP_USBD_HID_EP_DSC(endpoint, 8, 1) + + +/** + * @brief Example of USB HID keyboard report descriptor. + * + */ +#define APP_USBD_HID_KBD_REPORT_DSC() { \ + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */\ + 0x09, 0x06, /* USAGE (Keyboard) */\ + 0xa1, 0x01, /* COLLECTION (Application) */\ + 0x05, 0x07, /* USAGE_PAGE (Keyboard) */\ + 0x19, 0xe0, /* USAGE_MINIMUM (Keyboard LeftControl) */\ + 0x29, 0xe7, /* USAGE_MAXIMUM (Keyboard Right GUI) */\ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */\ + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */\ + 0x75, 0x01, /* REPORT_SIZE (1) */\ + 0x95, 0x08, /* REPORT_COUNT (8) */\ + 0x81, 0x02, /* INPUT (Data,Var,Abs) */\ + 0x95, 0x01, /* REPORT_COUNT (1) */\ + 0x75, 0x08, /* REPORT_SIZE (8) */\ + 0x81, 0x03, /* INPUT (Cnst,Var,Abs) */\ + 0x95, 0x05, /* REPORT_COUNT (5) */\ + 0x75, 0x01, /* REPORT_SIZE (1) */\ + 0x05, 0x08, /* USAGE_PAGE (LEDs) */\ + 0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */\ + 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */\ + 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */\ + 0x95, 0x01, /* REPORT_COUNT (1) */\ + 0x75, 0x03, /* REPORT_SIZE (3) */\ + 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */\ + 0x95, 0x06, /* REPORT_COUNT (6) */\ + 0x75, 0x08, /* REPORT_SIZE (8) */\ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */\ + 0x25, 0x65, /* LOGICAL_MAXIMUM (101) */\ + 0x05, 0x07, /* USAGE_PAGE (Keyboard) */\ + 0x19, 0x00, /* USAGE_MINIMUM (Reserved (no event indicated))*/\ + 0x29, 0x65, /* USAGE_MAXIMUM (Keyboard Application) */\ + 0x81, 0x00, /* INPUT (Data,Ary,Abs) */\ + 0xc0 /* END_COLLECTION */\ +} + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_HID_KBD_DESC_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd_internal.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd_internal.h new file mode 100644 index 0000000..06ab405 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd_internal.h @@ -0,0 +1,174 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_HID_KBD_INTERNAL_H__ +#define APP_USBD_HID_KBD_INTERNAL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @defgroup app_usbd_hid_kbd_internal USB HID keyboard internals + * @ingroup app_usbd_hid_kbd + * + * @brief @tagAPI52840 Module with types, definitions, and API used by the HID keyboard class. + * @{ + */ + +/** + * @brief Forward declaration of HID keyboard class type. + * + */ +APP_USBD_CLASS_FORWARD(app_usbd_hid_kbd); + + +/** + * @brief HID keyboard part of class instance data. + * + */ +typedef struct { + app_usbd_hid_inst_t hid_inst; //!< HID instance data. +} app_usbd_hid_kbd_inst_t; + +/** + * @brief HID keyboard context + * + */ +typedef struct { + app_usbd_hid_ctx_t hid_ctx; //!< HID class context. + + struct app_usbd_hid_kbd_ctx_internal_s { + uint8_t modifier; //!< Keyboard modifier state @ref app_usbd_hid_kbd_modifier_t. + uint8_t reserved; //!< Reserved value. + uint8_t key_table[6]; //!< Keyboard keys table @ref app_usbd_hid_kbd_codes_t. + } rep; + + uint8_t report_buff[8]; //!< Raw report buffer. + uint8_t leds_state; //!< Output report LEDs state. + uint8_t set_report; //!< Set report flag. +} app_usbd_hid_kbd_ctx_t; + +/** + * @brief HID keyboard configuration macro. + * + * Used by @ref APP_USBD_HID_KBD_GLOBAL_DEF. + * + * + */ +#define APP_USBD_HID_KBD_CONFIG(iface, ep) ((iface, ep)) + + +/** + * @brief Specific class constant data for HID keyboard class. + */ +#define APP_USBD_HID_KBD_INSTANCE_SPECIFIC_DEC app_usbd_hid_kbd_inst_t inst; + +/** + * @brief Specific class data for HID keyboard class. + */ +#define APP_USBD_HID_KBD_DATA_SPECIFIC_DEC app_usbd_hid_kbd_ctx_t ctx; + + +/** + * @brief Configure internal part of HID keyboard instance. + * + * @param report_buff_in Input report buffers array. + * @param report_buff_out Output report buffer. + * @param user_ev_handler User event handler. + * @param subclass_boot Subclass boot. (@ref app_usbd_hid_subclass_t) + */ +#define APP_USBD_HID_KBD_INST_CONFIG(report_buff_in, \ + report_buff_out, \ + user_ev_handler, \ + subclass_boot) \ + .inst = { \ + .hid_inst = APP_USBD_HID_INST_CONFIG(keyboard_descs, \ + subclass_boot, \ + APP_USBD_HID_PROTO_KEYBOARD, \ + report_buff_in, \ + report_buff_out, \ + user_ev_handler, \ + &app_usbd_hid_kbd_methods), \ +} + + +/** + * @brief Public HID keyboard interface. + */ +extern const app_usbd_hid_methods_t app_usbd_hid_kbd_methods; + +/** + * @brief Public HID keyboard class interface. + */ +extern const app_usbd_class_methods_t app_usbd_hid_kbd_class_methods; + +/** + * @brief Global definition of @ref app_usbd_hid_kbd_t class. + * + * @ref APP_USBD_HID_KBD_GLOBAL_DEF + */ +#define APP_USBD_HID_KBD_GLOBAL_DEF_INTERNAL(instance_name, \ + interface_number, \ + endpoint, \ + user_ev_handler, \ + subclass_boot) \ + static app_usbd_hid_report_buffer_t CONCAT_2(instance_name, _in)[1]; \ + APP_USBD_HID_GENERIC_GLOBAL_OUT_REP_DEF(CONCAT_2(instance_name, _out), 1 + 1); \ + APP_USBD_CLASS_INST_GLOBAL_DEF( \ + instance_name, \ + app_usbd_hid_kbd, \ + &app_usbd_hid_kbd_class_methods, \ + APP_USBD_HID_KBD_CONFIG(interface_number, endpoint), \ + (APP_USBD_HID_KBD_INST_CONFIG(CONCAT_2(instance_name, _in), \ + &CONCAT_2(instance_name, _out), \ + user_ev_handler, \ + subclass_boot)) \ + ) + + +/** @} */ + + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_HID_KBD_INTERNAL_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse.c new file mode 100644 index 0000000..7427c58 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse.c @@ -0,0 +1,620 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(APP_USBD_HID_MOUSE) + +#include <string.h> +#include "app_usbd_hid_mouse.h" +#include "app_util_platform.h" + + +/** + * @defgroup app_usbd_hid_mouse_internal USBD HID Mouse internals + * @{ + * @ingroup app_usbd_hid_mouse + * @internal + */ + +/** + * @brief Auxiliary function to access HID mouse context data. + * + * @param[in] p_inst class instance data. + * + * @return HID mouse instance data context. + */ +static inline app_usbd_hid_mouse_ctx_t * hid_mouse_ctx_get(app_usbd_hid_mouse_t const * p_mouse) +{ + ASSERT(p_mouse != NULL); + ASSERT(p_mouse->specific.p_data != NULL); + return &p_mouse->specific.p_data->ctx; +} + + +/** + * @brief Auxiliary function to access HID mouse instance data. + * + * @param[in] p_inst class instance data. + * + * @return HID mouse instance. + */ +static inline app_usbd_hid_mouse_t const * hid_mouse_get(app_usbd_class_inst_t const * p_inst) +{ + ASSERT(p_inst != NULL); + return (app_usbd_hid_mouse_t const *)p_inst; +} + + +/** + * @brief Returns mouse report buffer handle. + * + * @param[in] p_kbd HID keyboard instance. + * + * @return HID report buffer. + */ +static inline +app_usbd_hid_report_buffer_t const * hid_mouse_rep_buffer_get(app_usbd_hid_mouse_t const * p_mouse) +{ + ASSERT(p_mouse != NULL); + app_usbd_hid_inst_t const * p_hinst = &p_mouse->specific.inst.hid_inst; + app_usbd_hid_mouse_ctx_t * p_mouse_ctx = hid_mouse_ctx_get(p_mouse); + app_usbd_hid_report_buffer_t * p_rep_buff = app_usbd_hid_rep_buff_in_get(p_hinst); + + p_rep_buff->p_buff = p_mouse_ctx->report_buff; + p_rep_buff->size = sizeof(p_mouse_ctx->report_buff); + + /*Mouse has only one report input report buffer */ + return app_usbd_hid_rep_buff_in_get(p_hinst); +} + + +/**@brief Auxiliary function to get report value from internal accumulated value. + * + * @param[in] acc Accumulated XY axis or scroll. + * + * @return Offset value that could be written directly to report buffer. + */ +static inline int8_t hid_mouse_axis_acc_get(int16_t acc) +{ + if (acc > INT8_MAX) + { + return INT8_MAX; + } + + if (acc < INT8_MIN) + { + return INT8_MIN; + } + + return acc; +} + + +/**@brief Auxiliary function to prepare report transfer buffer to next transfer. + * + * @param[in] p_mouse_ctx Mouse internal context. + * + * @retval true Next transfer is required. + * @retval false Next transfer is not required. + */ +static inline bool hid_mouse_transfer_next(app_usbd_hid_mouse_t const * p_mouse) +{ + + app_usbd_hid_mouse_ctx_t * p_mouse_ctx = hid_mouse_ctx_get(p_mouse); + app_usbd_hid_report_buffer_t const * p_rep_buffer = hid_mouse_rep_buffer_get(p_mouse); + + uint8_t * p_buff = p_rep_buffer->p_buff; + + /*Save last buttons state*/ + uint8_t last_button_state = p_buff[0]; + + /*Button state*/ + p_buff[0] = p_mouse_ctx->button_state; + + /*Axis X*/ + int8_t val_x = hid_mouse_axis_acc_get(p_mouse_ctx->acc_x_axis); + p_mouse_ctx->acc_x_axis -= val_x; + p_buff[1] = val_x; + + /*Axis Y*/ + int8_t val_y = hid_mouse_axis_acc_get(p_mouse_ctx->acc_y_axis); + p_mouse_ctx->acc_y_axis -= val_y; + p_buff[2] = val_y; + + /*Scroll*/ + int8_t val_scroll = hid_mouse_axis_acc_get(p_mouse_ctx->acc_scroll); + p_mouse_ctx->acc_scroll -= val_scroll; + p_buff[3] = val_scroll; + + if (val_x || val_y || val_scroll) + { + /*New transfer is required if any of mouse relative position is not zero*/ + return true; + } + + if (last_button_state != p_buff[0]) + { + /*New transfer is required if button state has changed*/ + return true; + } + + return false; +} + + +/** + * @brief Triggers IN endpoint transfer. + * + * @param[in] p_mouse HID mouse instance. + * + * @return Standard error code. + */ +static inline ret_code_t hid_mouse_transfer_set(app_usbd_hid_mouse_t const * p_mouse) +{ + app_usbd_class_inst_t const * p_inst = (app_usbd_class_inst_t const *)p_mouse; + app_usbd_hid_mouse_ctx_t * p_mouse_ctx = hid_mouse_ctx_get(p_mouse); + + nrf_drv_usbd_ep_t ep_addr = app_usbd_hid_epin_addr_get(p_inst); + + app_usbd_hid_state_flag_clr(&p_mouse_ctx->hid_ctx, APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS); + if (!hid_mouse_transfer_next(p_mouse)) + { + /* Transfer buffer hasn't changed since last transfer. No need to setup + * next transfer. + * */ + return NRF_SUCCESS; + } + + app_usbd_hid_report_buffer_t const * p_rep_buffer = hid_mouse_rep_buffer_get(p_mouse); + + NRF_DRV_USBD_TRANSFER_IN(transfer, p_rep_buffer->p_buff, p_rep_buffer->size); + + ret_code_t ret; + CRITICAL_REGION_ENTER(); + ret = app_usbd_ep_transfer(ep_addr, &transfer); + if (ret == NRF_SUCCESS) + { + app_usbd_hid_state_flag_set(&p_mouse_ctx->hid_ctx, + APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS); + } + CRITICAL_REGION_EXIT(); + + return ret; +} + + +/** + * @brief Checks if adding would cause 16 bit signed integer overflow. + * + * @param[in] acc Signed 16 bit accumulator to test. + * @param[in] value Value to add to accumulator. + * + * @retval true Overflow detected. + * @retval false No overflow detected. + */ +static inline bool hid_mouse_acc_overflow_check(int16_t acc, int8_t val) +{ + if (((int32_t)acc + val) > INT16_MAX) + { + return true; + } + + if (((int32_t)acc + val) < INT16_MIN) + { + return true; + } + + return false; +} + + +ret_code_t app_usbd_hid_mouse_x_move(app_usbd_hid_mouse_t const * p_mouse, int8_t offset) +{ + app_usbd_hid_mouse_ctx_t * p_mouse_ctx = hid_mouse_ctx_get(p_mouse); + + if (!app_usbd_hid_state_valid(&p_mouse_ctx->hid_ctx)) + { + return NRF_ERROR_INVALID_STATE; + } + + if (offset == 0) + { + /*No position change*/ + return NRF_SUCCESS; + } + + if (hid_mouse_acc_overflow_check(p_mouse_ctx->acc_y_axis, offset)) + { + /*Overflow detected*/ + return NRF_ERROR_BUSY; + } + + app_usbd_hid_access_lock(&p_mouse_ctx->hid_ctx); + p_mouse_ctx->acc_x_axis += offset; + app_usbd_hid_access_unlock(&p_mouse_ctx->hid_ctx); + + if (app_usbd_hid_trans_required(&p_mouse_ctx->hid_ctx)) + { + /*New transfer need to be triggered*/ + return hid_mouse_transfer_set(p_mouse); + } + + return NRF_SUCCESS; +} + + +ret_code_t app_usbd_hid_mouse_y_move(app_usbd_hid_mouse_t const * p_mouse, int8_t offset) +{ + app_usbd_hid_mouse_ctx_t * p_mouse_ctx = hid_mouse_ctx_get(p_mouse); + + if (!app_usbd_hid_state_valid(&p_mouse_ctx->hid_ctx)) + { + return NRF_ERROR_INVALID_STATE; + } + + if (offset == 0) + { + /*No position change*/ + return NRF_SUCCESS; + } + + if (hid_mouse_acc_overflow_check(p_mouse_ctx->acc_y_axis, offset)) + { + /*Overflow detected*/ + return NRF_ERROR_BUSY; + } + + app_usbd_hid_access_lock(&p_mouse_ctx->hid_ctx); + p_mouse_ctx->acc_y_axis += offset; + app_usbd_hid_access_unlock(&p_mouse_ctx->hid_ctx); + + if (app_usbd_hid_trans_required(&p_mouse_ctx->hid_ctx)) + { + /*New transfer need to be triggered*/ + return hid_mouse_transfer_set(p_mouse); + } + + return NRF_SUCCESS; +} + + +ret_code_t app_usbd_hid_mouse_scroll_move(app_usbd_hid_mouse_t const * p_mouse, int8_t offset) +{ + app_usbd_hid_mouse_ctx_t * p_mouse_ctx = hid_mouse_ctx_get(p_mouse); + + if (!app_usbd_hid_state_valid(&p_mouse_ctx->hid_ctx)) + { + return NRF_ERROR_INVALID_STATE; + } + + if (offset == 0) + { + /*No position change*/ + return NRF_SUCCESS; + } + + if (hid_mouse_acc_overflow_check(p_mouse_ctx->acc_scroll, offset)) + { + /*Overflow detected*/ + return NRF_ERROR_BUSY; + } + + app_usbd_hid_access_lock(&p_mouse_ctx->hid_ctx); + p_mouse_ctx->acc_scroll += offset; + app_usbd_hid_access_unlock(&p_mouse_ctx->hid_ctx); + + if (app_usbd_hid_trans_required(&p_mouse_ctx->hid_ctx)) + { + /*New transfer need to be triggered*/ + return hid_mouse_transfer_set(p_mouse); + } + + return NRF_SUCCESS; +} + + +ret_code_t app_usbd_hid_mouse_button_state(app_usbd_hid_mouse_t const * p_mouse, + uint8_t button_id, + bool state) +{ + ASSERT(button_id < 8); + app_usbd_hid_mouse_ctx_t * p_mouse_ctx = hid_mouse_ctx_get(p_mouse); + + if (button_id >= p_mouse->specific.inst.button_count) + { + return NRF_ERROR_INVALID_PARAM; + } + + if (!app_usbd_hid_state_valid(&p_mouse_ctx->hid_ctx)) + { + return NRF_ERROR_INVALID_STATE; + } + + if (IS_SET(p_mouse_ctx->button_state, button_id) == (state ? 1 : 0)) + { + return NRF_SUCCESS; + } + + app_usbd_hid_access_lock(&p_mouse_ctx->hid_ctx); + if (state) + { + SET_BIT(p_mouse_ctx->button_state, button_id); + } + else + { + CLR_BIT(p_mouse_ctx->button_state, button_id); + } + app_usbd_hid_access_unlock(&p_mouse_ctx->hid_ctx); + + if (app_usbd_hid_trans_required(&p_mouse_ctx->hid_ctx)) + { + /*New transfer need to be triggered*/ + return hid_mouse_transfer_set(p_mouse); + } + + return NRF_SUCCESS; +} + + +/** + * @brief @ref app_usbd_hid_interface_t::on_get_report + */ +static ret_code_t hid_mouse_on_get_report(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + if (p_setup_ev->setup.wValue.hb != APP_USBD_HID_REPORT_TYPE_INPUT) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + app_usbd_hid_mouse_t const * p_mouse = hid_mouse_get(p_inst); + app_usbd_hid_report_buffer_t const * p_rep_buffer = hid_mouse_rep_buffer_get(p_mouse); + + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_rep_buffer->p_buff, p_rep_buffer->size); +} + + +/** + * @brief @ref app_usbd_hid_interface_t::on_set_report + */ +static ret_code_t hid_mouse_on_set_report(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + return NRF_ERROR_NOT_SUPPORTED; +} + + +/** + * @brief @ref app_usbd_hid_interface_t::ep_transfer_set + */ +static ret_code_t hid_mouse_ep_transfer_in(app_usbd_class_inst_t const * p_inst) +{ + return hid_mouse_transfer_set((app_usbd_hid_mouse_t const *)p_inst); +} + + +/** + * @brief @ref app_usbd_class_interface_t::event_handler + */ +static ret_code_t hid_mouse_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_complex_evt_t const * p_event) +{ + ASSERT(p_inst != NULL); + ASSERT(p_event != NULL); + + app_usbd_hid_mouse_t const * p_mouse = hid_mouse_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_mouse->specific.inst.hid_inst; + + app_usbd_hid_mouse_ctx_t * p_mouse_ctx = hid_mouse_ctx_get(p_mouse); + app_usbd_hid_ctx_t * p_hid_ctx = &p_mouse_ctx->hid_ctx; + + + ret_code_t ret = NRF_SUCCESS; + + switch (p_event->app_evt.type) + { + default: + ret = NRF_ERROR_NOT_SUPPORTED; + break; + } + + if (ret != NRF_ERROR_NOT_SUPPORTED) + { + /* Event was processed by specific handler */ + return ret; + } + + /*Try handle event by generic HID event handler*/ + return app_usbd_hid_event_handler(p_inst, p_hinst, p_hid_ctx, p_event); +} + + +static uint8_t hid_mouse_get_class_descriptors_count(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_hid_mouse_t const * p_mouse = hid_mouse_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_mouse->specific.inst.hid_inst; + + return p_hinst->subclass_desc_count; +} + + +static app_usbd_descriptor_t hid_mouse_get_class_descriptors_type( + app_usbd_class_inst_t const * p_inst, + uint8_t desc_num) +{ + app_usbd_hid_mouse_t const * p_mouse = hid_mouse_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_mouse->specific.inst.hid_inst; + + return p_hinst->p_subclass_desc[desc_num]->type; +} + + +static size_t hid_mouse_get_class_descriptors_length(app_usbd_class_inst_t const * p_inst, + uint8_t desc_num) +{ + app_usbd_hid_mouse_t const * p_mouse = hid_mouse_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_mouse->specific.inst.hid_inst; + + return p_hinst->p_subclass_desc[desc_num]->size; +} + + +static uint8_t hid_mouse_get_class_descriptors_data(app_usbd_class_inst_t const * p_inst, + uint8_t desc_num, + uint32_t cur_byte) +{ + app_usbd_hid_mouse_t const * p_mouse = hid_mouse_get(p_inst); + app_usbd_hid_inst_t const * p_hinst = &p_mouse->specific.inst.hid_inst; + + return p_hinst->p_subclass_desc[desc_num]->p_data[cur_byte]; +} + + +static bool hid_mouse_feed_descriptors(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size) +{ + static uint8_t ifaces = 0; + + ifaces = app_usbd_class_iface_count_get(p_inst); + app_usbd_hid_mouse_t const * p_mouse = hid_mouse_get(p_inst); + + APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size); + + static uint8_t i = 0; + + for (i = 0; i < ifaces; i++) + { + /* INTERFACE DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE); // bDescriptorType = Interface + + static app_usbd_class_iface_conf_t const * p_cur_iface = NULL; + p_cur_iface = app_usbd_class_iface_get(p_inst, i); + + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_number_get(p_cur_iface)); // bInterfaceNumber + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bAlternateSetting + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_ep_count_get(p_cur_iface)); // bNumEndpoints + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_CLASS); // bInterfaceClass = HID + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_mouse->specific.inst.hid_inst.subclass_boot); // bInterfaceSubclass (Boot Interface) + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_mouse->specific.inst.hid_inst.protocol); // bInterfaceProtocol + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iInterface + + /* HID DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_DESCRIPTOR_HID); // bDescriptorType = HID + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(APP_USBD_HID_BCD_VER)); // bcdHID LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(APP_USBD_HID_BCD_VER)); // bcdHID MSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_HID_COUNTRY_NOT_SUPPORTED); // bCountryCode + APP_USBD_CLASS_DESCRIPTOR_WRITE(hid_mouse_get_class_descriptors_count(p_inst)); // bNumDescriptors + + static uint8_t class_desc_cnt = 0; + class_desc_cnt = hid_mouse_get_class_descriptors_count(p_inst); + static uint8_t j = 0; + static uint16_t class_desc_len = 0; + + for (j = 0; j < class_desc_cnt; j++) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(hid_mouse_get_class_descriptors_type(p_inst, j)); // bDescriptorType + + class_desc_len = hid_mouse_get_class_descriptors_length(p_inst, j); + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(class_desc_len)); // wDescriptorLength LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(class_desc_len)); // wDescriptorLength MSB + } + + static uint8_t endpoints = 0; + endpoints = app_usbd_class_iface_ep_count_get(p_cur_iface); + + for (j = 0; j < endpoints; j++) + { + /* ENDPOINT DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x07); // bLengths + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_ENDPOINT); // bDescriptorType = Endpoint + + static app_usbd_class_ep_conf_t const * p_cur_ep = NULL; + p_cur_ep = app_usbd_class_iface_ep_get(p_cur_iface, i); + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_ep_address_get(p_cur_ep)); // bEndpointAddress + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_INTERRUPT); // bmAttributes + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize MSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x01); // bInterval + } + } + + APP_USBD_CLASS_DESCRIPTOR_END(); +} + +static bool hid_mouse_feed_subclass_descriptor(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size, + uint8_t index) +{ + APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size); + + /* PHYSICAL AND REPORT DESCRIPTORS */ + static uint32_t cur_byte = 0; + static size_t sub_desc_size = 0; + sub_desc_size = hid_mouse_get_class_descriptors_length(p_inst, index); + + for (cur_byte = 0; cur_byte <= sub_desc_size; cur_byte++) + { + APP_USBD_CLASS_DESCRIPTOR_WRITE(hid_mouse_get_class_descriptors_data(p_inst, index, cur_byte)); + } + + APP_USBD_CLASS_DESCRIPTOR_END(); +} + +/** @} */ + +const app_usbd_hid_methods_t app_usbd_hid_mouse_methods = { + .on_get_report = hid_mouse_on_get_report, + .on_set_report = hid_mouse_on_set_report, + .ep_transfer_in = hid_mouse_ep_transfer_in, + .ep_transfer_out = NULL, + .feed_subclass_descriptor = hid_mouse_feed_subclass_descriptor, +}; + +const app_usbd_class_methods_t app_usbd_hid_mouse_class_methods = { + .event_handler = hid_mouse_event_handler, + .feed_descriptors = hid_mouse_feed_descriptors, +}; + +#endif //NRF_MODULE_ENABLED(APP_USBD_HID_MOUSE) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse.h new file mode 100644 index 0000000..2293f3c --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse.h @@ -0,0 +1,184 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_HID_MOUSE_H__ +#define APP_USBD_HID_MOUSE_H__ + +#include <stdint.h> +#include <stdbool.h> + +#include "nrf_drv_usbd.h" +#include "app_usbd_class_base.h" +#include "app_usbd_hid_types.h" +#include "app_usbd_hid.h" +#include "app_usbd.h" +#include "app_usbd_core.h" +#include "app_usbd_descriptor.h" +#include "app_usbd_hid_mouse_desc.h" +#include "app_usbd_hid_mouse_internal.h" + +/** + * @defgroup app_usbd_hid_mouse USB HID mouse + * @ingroup app_usbd_hid + * + * @brief @tagAPI52840 Module with types, definitions, and API used by the HID mouse class. + * @{ + */ + +#ifdef DOXYGEN +/** + * @brief HID mouse class instance type + * + * @ref APP_USBD_CLASS_TYPEDEF + */ +typedef struct { } app_usbd_hid_mouse_t; +#else +/*lint -save -e10 -e26 -e123 -e505 */ +APP_USBD_CLASS_TYPEDEF(app_usbd_hid_mouse, \ + APP_USBD_HID_MOUSE_CONFIG(0, NRF_DRV_USBD_EPIN1), \ + APP_USBD_HID_MOUSE_INSTANCE_SPECIFIC_DEC, \ + APP_USBD_HID_MOUSE_DATA_SPECIFIC_DEC \ +); +/*lint -restore*/ +#endif + +/** + * @brief Global definition macro of app_usbd_hid_mouse_t class. + * + * @param instance_name Name of global instance. + * @param interface_number Unique interface number. + * @param endpoint Input endpoint (@ref nrf_drv_usbd_ep_t). + * @param bcnt Mouse button count (from 1 to 8). + * @param user_ev_handler User event handler (optional). + * @param subclass_boot Subclass boot. (@ref app_usbd_hid_subclass_t) + * + * @note This macro is just simplified version of @ref APP_USBD_HID_MOUSE_GLOBAL_DEF_INTERNAL. + * + * @code + APP_USBD_HID_MOUSE_GLOBAL_DEF(my_awesome_mouse, 0, NRF_DRV_USBD_EPIN1, 3, NULL); + * @endcode + */ +#define APP_USBD_HID_MOUSE_GLOBAL_DEF(instance_name, \ + interface_number, \ + endpoint, \ + bcnt, \ + user_ev_handler, \ + subclass_boot) \ + APP_USBD_HID_GENERIC_SUBCLASS_REPORT_DESC(mouse_desc, APP_USBD_HID_MOUSE_REPORT_DSC_BUTTON(bcnt)); \ + static const app_usbd_hid_subclass_desc_t * mouse_descs[] = {&mouse_desc}; \ + APP_USBD_HID_MOUSE_GLOBAL_DEF_INTERNAL(instance_name, \ + interface_number, \ + endpoint, \ + bcnt, \ + user_ev_handler, \ + subclass_boot) + + +/** + * @brief Helper function to get class instance from HID mouse internals. + * + * @param[in] p_mouse Mouse instance (declared by @ref APP_USBD_HID_MOUSE_GLOBAL_DEF). + * + * @return Base class instance. + */ +static inline app_usbd_class_inst_t const * +app_usbd_hid_mouse_class_inst_get(app_usbd_hid_mouse_t const * p_mouse) +{ + return &p_mouse->base; +} + +/** + * @brief Helper function to get HID mouse from base class instance. + * + * @param[in] p_inst Base class instance. + * + * @return HID mouse class handle. + */ +static inline app_usbd_hid_mouse_t const * +app_usbd_hid_mouse_class_get(app_usbd_class_inst_t const * p_inst) +{ + return (app_usbd_hid_mouse_t const *)p_inst; +} + +/** + * @brief Move mouse X axis. + * + * @param[in] p_mouse Mouse instance (declared by @ref APP_USBD_HID_MOUSE_GLOBAL_DEF). + * @param[in] offset Relative mouse position: allowed full int8_t range. + * + * @return Standard error code. + */ +ret_code_t app_usbd_hid_mouse_x_move(app_usbd_hid_mouse_t const * p_mouse, int8_t offset); + +/** + * @brief Move mouse Y axis. + * + * @param[in] p_mouse Mouse instance (declared by @ref APP_USBD_HID_MOUSE_GLOBAL_DEF). + * @param[in] offset Relative mouse position: allowed full int8_t range. + * + * @return Standard error code. + */ +ret_code_t app_usbd_hid_mouse_y_move(app_usbd_hid_mouse_t const * p_mouse, int8_t offset); + +/** + * @brief Move mouse scroll. + * + * @param[in] p_mouse Mouse instance (declared by @ref APP_USBD_HID_MOUSE_GLOBAL_DEF). + * @param[in] offset Relative mouse position: allowed full int8_t range. + * + * @return Standard error code. + */ +ret_code_t app_usbd_hid_mouse_scroll_move(app_usbd_hid_mouse_t const * p_mouse, int8_t offset); + +/** + * @brief Set mouse button state. + * + * @param[in] p_mouse Mouse instance (declared by @ref APP_USBD_HID_MOUSE_GLOBAL_DEF). + * @param[in] button_id Button number (0...7). + * @param[in] state Button state: true -> PRESSED, false -> RELEASED. + * + * @return Standard error code. + */ +ret_code_t app_usbd_hid_mouse_button_state(app_usbd_hid_mouse_t const * p_mouse, + uint8_t button_id, + bool state); + +/** @} */ + +#endif /* APP_USBD_HID_MOUSE_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse_desc.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse_desc.h new file mode 100644 index 0000000..e7517c2 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse_desc.h @@ -0,0 +1,117 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_HID_MOUSE_DESC_H__ +#define APP_USBD_HID_MOUSE_DESC_H__ + +/** + * @defgroup app_usbd_hid_mouse_desc USB HID mouse descriptors + * @ingroup app_usbd_hid_mouse + * + * @brief @tagAPI52840 Module with types, definitions, and API used by the HID mouse class. + * @{ + */ + +/** + * @brief Initializer of interface descriptor for HID mouse class. + * + * @param interface_number Interface number. + */ +#define APP_USBD_HID_MOUSE_INTERFACE_DSC(interface_number) \ + APP_USBD_HID_INTERFACE_DSC(interface_number, \ + 1, \ + APP_USBD_HID_SUBCLASS_BOOT, \ + APP_USBD_HID_PROTO_MOUSE) + +/** + * @brief Initializer of HID descriptor for HID mouse class. + * + * @param ... Descriptor list. + */ +#define APP_USBD_HID_MOUSE_HID_DSC(...) \ + APP_USBD_HID_HID_DSC(__VA_ARGS__) + +/** + * @brief Initializer of endpoint descriptor for HID mouse class. + * + * @param endpoint_number Endpoint number. + */ +#define APP_USBD_HID_MOUSE_EP_DSC(endpoint_number) \ + APP_USBD_HID_EP_DSC(endpoint_number, 8, 1) + + + +/** + * @brief Example of USB HID mouse report descriptor for n button mouse. + * + * @param bcnt Button count. Allowed values from 1 to 8. + */ +#define APP_USBD_HID_MOUSE_REPORT_DSC_BUTTON(bcnt) { \ + 0x05, 0x01, /* Usage Page (Generic Desktop), */ \ + 0x09, 0x02, /* Usage (Mouse), */ \ + 0xA1, 0x01, /* Collection (Application), */ \ + 0x09, 0x01, /* Usage (Pointer), */ \ + 0xA1, 0x00, /* Collection (Physical), */ \ + 0x05, 0x09, /* Usage Page (Buttons), */ \ + 0x19, 0x01, /* Usage Minimum (01), */ \ + 0x29, bcnt, /* Usage Maximum (bcnt), */ \ + 0x15, 0x00, /* Logical Minimum (0), */ \ + 0x25, 0x01, /* Logical Maximum (1), */ \ + 0x75, 0x01, /* Report Size (1), */ \ + 0x95, bcnt, /* Report Count (bcnt), */ \ + 0x81, 0x02, /* Input (Data, Variable, Absolute)*/ \ + 0x75, (8-(bcnt)), /* Report Size (8-(bcnt)), */ \ + 0x95, 0x01, /* Report Count (1), */ \ + 0x81, 0x01, /* Input (Constant), */ \ + 0x05, 0x01, /* Usage Page (Generic Desktop), */ \ + 0x09, 0x30, /* Usage (X), */ \ + 0x09, 0x31, /* Usage (Y), */ \ + 0x09, 0x38, /* Usage (Scroll), */ \ + 0x15, 0x81, /* Logical Minimum (-127), */ \ + 0x25, 0x7F, /* Logical Maximum (127), */ \ + 0x75, 0x08, /* Report Size (8), */ \ + 0x95, 0x03, /* Report Count (3), */ \ + 0x81, 0x06, /* Input (Data, Variable, Relative)*/ \ + 0xC0, /* End Collection, */ \ + 0xC0, /* End Collection */ \ +} + + /** @} */ + +#endif /* APP_USBD_HID_MOUSE_DESC_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse_internal.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse_internal.h new file mode 100644 index 0000000..64ccc7f --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse_internal.h @@ -0,0 +1,173 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_HID_MOUSE_INTERNAL_H__ +#define APP_USBD_HID_MOUSE_INTERNAL_H__ + +/** + * @defgroup app_usbd_hid_mouse_internals USB HID mouse internals + * @ingroup app_usbd_hid_mouse + * + * @brief @tagAPI52840 Module with types, definitions, and API used by the HID mouse class. + * @{ + */ + +/** + * @brief Forward declaration of HID mouse class type. + */ +APP_USBD_CLASS_FORWARD(app_usbd_hid_mouse); + + +/** + * @brief HID mouse part of class instance data. + */ +typedef struct { + app_usbd_hid_inst_t hid_inst; //!< HID instance data. + const uint8_t button_count; //!< Number of buttons mouse specific. +} app_usbd_hid_mouse_inst_t; + +/** + * @brief HID mouse context. + */ +typedef struct { + app_usbd_hid_ctx_t hid_ctx; //!< HID class context. + + int16_t acc_x_axis; //!< Mouse specific. Accumulated x axis offset. + int16_t acc_y_axis; //!< Mouse specific. Accumulated y axis offset. + int16_t acc_scroll; //!< Mouse specific. Accumulated scroll offset. + uint8_t button_state; //!< Mouse specific. Actual button state. Bitfield of maximum 8 buttons states. + uint8_t report_buff[4]; +} app_usbd_hid_mouse_ctx_t; + + +/** + * @brief HID mouse configuration macro. + * + * Used by @ref APP_USBD_HID_MOUSE_GLOBAL_DEF. + * + */ +#define APP_USBD_HID_MOUSE_CONFIG(iface, ep) ((iface, ep)) + + +/** + * @brief Specific class constant data for HID mouse class. + */ +#define APP_USBD_HID_MOUSE_INSTANCE_SPECIFIC_DEC app_usbd_hid_mouse_inst_t inst; + +/** + * @brief Specific class data for HID mouse class. + */ +#define APP_USBD_HID_MOUSE_DATA_SPECIFIC_DEC app_usbd_hid_mouse_ctx_t ctx; + + +/** + * @brief HID mouse descriptors config macro + * + * @ref app_usbd_hid_mouse_inst_t + * + */ +#define APP_USBD_HID_MOUSE_DSC_CONFIG(interface_number, endpoint, rep_desc) { \ + APP_USBD_HID_MOUSE_INTERFACE_DSC(interface_number) \ + APP_USBD_HID_MOUSE_HID_DSC(rep_desc) \ + APP_USBD_HID_MOUSE_EP_DSC(endpoint) \ +} + +/** + * @brief Configure internal part of HID mouse instance. + * + * @param report_buff_in Input report buffers array. + * @param report_buff_out Output report buffer. + * @param user_ev_handler User event handler. + * @param bcnt Mouse button count. + * @param subclass_boot Subclass boot. (@ref app_usbd_hid_subclass_t) + */ +#define APP_USBD_HID_MOUSE_INST_CONFIG(report_buff_in, \ + report_buff_out, \ + user_ev_handler, \ + bcnt, \ + subclass_boot) \ + .inst = { \ + .hid_inst = APP_USBD_HID_INST_CONFIG(mouse_descs, \ + subclass_boot, \ + APP_USBD_HID_PROTO_MOUSE, \ + report_buff_in, \ + report_buff_out, \ + user_ev_handler, \ + &app_usbd_hid_mouse_methods), \ + .button_count = bcnt, \ + } + +/** + * @brief Public HID mouse interface. + */ +extern const app_usbd_hid_methods_t app_usbd_hid_mouse_methods; + +/** + * @brief Public HID mouse class interface. + */ +extern const app_usbd_class_methods_t app_usbd_hid_mouse_class_methods; + +/** + * @brief Global definition of app_usbd_hid_mouse_t class. + * + * @ref APP_USBD_HID_MOUSE_GLOBAL_DEF + */ +#define APP_USBD_HID_MOUSE_GLOBAL_DEF_INTERNAL(instance_name, \ + interface_number, \ + endpoint, \ + bcnt, \ + user_ev_handler, \ + subclass_boot) \ + static app_usbd_hid_report_buffer_t CONCAT_2(instance_name, _in)[1]; \ + APP_USBD_CLASS_INST_GLOBAL_DEF( \ + instance_name, \ + app_usbd_hid_mouse, \ + &app_usbd_hid_mouse_class_methods, \ + APP_USBD_HID_MOUSE_CONFIG(interface_number, endpoint), \ + (APP_USBD_HID_MOUSE_INST_CONFIG(CONCAT_2(instance_name, _in), \ + NULL, \ + user_ev_handler, \ + bcnt, \ + subclass_boot)) \ + ) + + +/** @} */ + +#endif /* APP_USBD_HID_MOUSE_INTERNAL_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc.c new file mode 100644 index 0000000..a19de11 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc.c @@ -0,0 +1,2365 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(APP_USBD_MSC) + +#include <string.h> +#include <ctype.h> +#include "app_usbd.h" +#include "app_usbd_msc.h" +#include "app_usbd_string_desc.h" + +/** + * @defgroup app_usbd_msc_internal USBD MSC internals + * @{ + * @ingroup app_usbd_msc + * @internal + */ + +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_requestsense_t) == 6); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_requestsense_resp_t) == 18); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_inquiry_t) == 6); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_inquiry_resp_t) == 36); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_read6_t) == 6); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_write6_t) == 6); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_modesense6_t) == 6); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_modesense6_resp_t) == 4); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_readcapacity10_t) == 10); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_readcapacity10_resp_t) == 8); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_read10_t) == 10); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_write10_t) == 10); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_modesense10_t) == 10); +STATIC_ASSERT(sizeof(app_usbd_scsi_cmd_modesense10_resp_t) == 8); + +STATIC_ASSERT(sizeof(app_usbd_msc_cbw_t) == 31); +STATIC_ASSERT(sizeof(app_usbd_msc_csw_t) == 13); + +#define NRF_LOG_MODULE_NAME usbd_msc + +#if APP_USBD_MSC_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL APP_USBD_MSC_CONFIG_LOG_LEVEL +#define NRF_LOG_INFO_COLOR APP_USBD_MSC_CONFIG_INFO_COLOR +#define NRF_LOG_DEBUG_COLOR APP_USBD_MSC_CONFIG_DEBUG_COLOR +#else // APP_USBD_MSC_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL 0 +#endif // APP_USBD_MSC_CONFIG_LOG_ENABLED +#include "nrf_log.h" +NRF_LOG_MODULE_REGISTER(); + +#define APP_USBD_MSC_IFACE_IDX 0 /**< Mass storage class interface index */ +#define APP_USBD_MSC_EPIN_IDX 0 /**< Mass storage class endpoint IN index */ +#define APP_USBD_MSC_EPOUT_IDX 1 /**< Mass storage class endpoint OUT index */ + +/** + * @brief Set request buffer busy flag + * + * @param[in] val Bitmask to set + * @param[in] id Buffer id + * */ +#define APP_USBD_MSC_REQ_BUSY_SET(val, id) SET_BIT(val, id) + +/** + * @brief Clear request buffer busy flag + * + * @param[in] val Bitmask to set + * @param[in] id Buffer id + * */ +#define APP_USBD_MSC_REQ_BUSY_CLR(val, id) CLR_BIT(val, id) + +#define APP_USBD_MSC_REQ_BUSY_FULL_MASK (0x03) /**< Request busy mask */ + +static void msc_blockdev_ev_handler(nrf_block_dev_t const * p_blk_dev, + nrf_block_dev_event_t const * p_event); + +/** + * @brief Auxiliary function to access MSC instance data + * + * @param[in] p_inst Class instance data + * + * @return MSC instance data @ref app_usbd_msc_t + */ +static inline app_usbd_msc_t const * msc_get(app_usbd_class_inst_t const * p_inst) +{ + ASSERT(p_inst != NULL); + return (app_usbd_msc_t const *)p_inst; +} + + +/** + * @brief Auxiliary function to access MSC context data + * + * @param[in] p_msc MSC instance data + * @return MSC context data @ref app_usbd_msc_ctx_t + */ +static inline app_usbd_msc_ctx_t * msc_ctx_get(app_usbd_msc_t const * p_msc) +{ + ASSERT(p_msc != NULL); + ASSERT(p_msc->specific.p_data != NULL); + return &p_msc->specific.p_data->ctx; +} + + +/** + * @brief Auxiliary function to access MSC IN endpoint address + * + * @param[in] p_inst Class instance data + * + * @return IN endpoint address + */ +static inline nrf_drv_usbd_ep_t ep_in_addr_get(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_class_iface_conf_t const * class_iface; + + class_iface = app_usbd_class_iface_get(p_inst, APP_USBD_MSC_IFACE_IDX); + + app_usbd_class_ep_conf_t const * ep_cfg; + ep_cfg = app_usbd_class_iface_ep_get(class_iface, APP_USBD_MSC_EPIN_IDX); + + return app_usbd_class_ep_address_get(ep_cfg); +} + + +/** + * @brief Auxiliary function to access MSC OUT endpoint address + * + * @param[in] p_inst Class instance data + * + * @return OUT endpoint address + */ +static inline nrf_drv_usbd_ep_t ep_out_addr_get(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_class_iface_conf_t const * class_iface; + + class_iface = app_usbd_class_iface_get(p_inst, APP_USBD_MSC_IFACE_IDX); + + app_usbd_class_ep_conf_t const * ep_cfg; + ep_cfg = app_usbd_class_iface_ep_get(class_iface, APP_USBD_MSC_EPOUT_IDX); + + return app_usbd_class_ep_address_get(ep_cfg); +} + + +/** + * @brief Command Block Wrapper trigger + * + * @param[in] p_inst Generic class instance + * @param[in] state Next state transition + * + * @return Standard error code + * */ +static ret_code_t cbw_wait_start(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + nrf_drv_usbd_ep_t ep_addr_out = ep_out_addr_get(p_inst); + + NRF_LOG_DEBUG("cbw_wait_start"); + memset(&p_msc_ctx->cbw, 0, sizeof(app_usbd_msc_cbw_t)); + NRF_DRV_USBD_TRANSFER_OUT(cbw, &p_msc_ctx->cbw, sizeof(app_usbd_msc_cbw_t)); + ret_code_t ret = app_usbd_ep_transfer(ep_addr_out, &cbw); + if (ret == NRF_SUCCESS) + { + p_msc_ctx->state = APP_USBD_MSC_STATE_CBW; + } + + return ret; +} + + +/** + * @brief Command Status Wrapper trigger + * + * @param[in] p_inst Generic class instance + * @param[in] state Next state transition + * + * @return Standard error code + * */ +static ret_code_t csw_wait_start(app_usbd_class_inst_t const * p_inst, uint8_t status) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + nrf_drv_usbd_ep_t ep_addr_in = ep_in_addr_get(p_inst); + + memset(&p_msc_ctx->csw, 0, sizeof(app_usbd_msc_csw_t)); + memcpy(p_msc_ctx->csw.signature, APP_USBD_MSC_CSW_SIGNATURE, sizeof(p_msc_ctx->csw.signature)); + memcpy(p_msc_ctx->csw.tag, p_msc_ctx->cbw.tag, sizeof(p_msc_ctx->csw.tag)); + memcpy(p_msc_ctx->csw.residue, &p_msc_ctx->current.residue, sizeof(uint32_t)); + p_msc_ctx->csw.status = status; + + NRF_DRV_USBD_TRANSFER_IN(csw, &p_msc_ctx->csw, sizeof(app_usbd_msc_csw_t)); + ret_code_t ret = app_usbd_ep_transfer(ep_addr_in, &csw); + if (ret == NRF_SUCCESS) + { + p_msc_ctx->state = APP_USBD_MSC_STATE_CSW; + } + + return ret; +} + + +/** + * @brief IN transfer trigger + * + * @param[in] p_inst Generic class instance + * @param[in] p_buff IN transfer data buffer + * @param[in] size IN transfer size + * @param[in] state Next state transition + * + * @return Standard error code + * */ +static ret_code_t transfer_in_start(app_usbd_class_inst_t const * p_inst, + void const * p_buff, + size_t size, + app_usbd_msc_state_t state) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + NRF_LOG_DEBUG("transfer_in_start: p_buff: %p, size: %u", (uint32_t)p_buff, size); + + nrf_drv_usbd_ep_t ep_addr_in = ep_in_addr_get(p_inst); + + NRF_DRV_USBD_TRANSFER_IN(resp, p_buff, size); + ret_code_t ret = app_usbd_ep_transfer(ep_addr_in, &resp); + if (ret == NRF_SUCCESS) + { + p_msc_ctx->state = state; + } + return ret; +} + + +/** + * @brief MSC reset request handler @ref APP_USBD_MSC_REQ_BULK_RESET + * + * @param[in] p_inst Generic class instance + * + * */ +static void bulk_ep_reset(app_usbd_class_inst_t const * p_inst) +{ + nrf_drv_usbd_ep_t ep_addr_in = ep_in_addr_get(p_inst); + nrf_drv_usbd_ep_t ep_addr_out = ep_out_addr_get(p_inst); + + nrf_drv_usbd_ep_abort(ep_addr_in); + nrf_drv_usbd_ep_abort(ep_addr_out); +} + + +/** + * @brief OUT transfer trigger + * + * @param[in] p_inst Generic class instance + * @param[in] p_buff OUT transfer data buffer + * @param[in] size OUT transfer size + * @param[in] state Next state transition + * + * @return Standard error code + * */ +static ret_code_t transfer_out_start(app_usbd_class_inst_t const * p_inst, + void * p_buff, + size_t size, + app_usbd_msc_state_t state) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + NRF_LOG_DEBUG("transfer_out_start: p_buff: %p, size: %u", (uint32_t)p_buff, size); + nrf_drv_usbd_ep_t ep_addr_out = ep_out_addr_get(p_inst); + + NRF_DRV_USBD_TRANSFER_OUT(resp, p_buff, size); + ret_code_t ret = app_usbd_ep_transfer(ep_addr_out, &resp); + if (ret == NRF_SUCCESS) + { + p_msc_ctx->state = state; + } + return ret; +} + + +/** + * @brief Generic function to stall communication endpoints and mark error state + * + * Function used internally to stall all communication endpoints and mark current state. + * + * @param p_inst Generic class instance + * @param state State to set + */ +static void status_generic_error_stall(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_state_t state) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + nrf_drv_usbd_ep_t ep_in = ep_in_addr_get(p_inst); + nrf_drv_usbd_ep_t ep_out = ep_out_addr_get(p_inst); + + nrf_drv_usbd_ep_stall(ep_in); + nrf_drv_usbd_ep_stall(ep_out); + nrf_drv_usbd_ep_abort(ep_in); + nrf_drv_usbd_ep_abort(ep_out); + + p_msc_ctx->state = state; +} + + +/** + * @brief Start status stage of unsupported SCSI command + * + * @param[in,out] p_inst Generic class instance + * + * @return Standard error code + * + * @sa status_generic_error_stall + */ +static ret_code_t status_unsupported_start(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + bool data_stage = uint32_decode(p_msc_ctx->cbw.datlen) != 0; + + if (!data_stage) + { + /* Try to transfer the response now */ + ret_code_t ret; + ret = csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_FAIL); + if (ret == NRF_SUCCESS) + { + return ret; + } + } + + /* Cannot transfer failed response in current command - stall the endpoints and postpone the answer */ + status_generic_error_stall(p_inst, APP_USBD_MSC_STATE_UNSUPPORTED); + if (data_stage) + { + if (!(p_msc_ctx->cbw.flags & APP_USBD_MSC_CBW_DIRECTION_IN)) + { + nrf_drv_usbd_transfer_out_drop(ep_out_addr_get(p_inst)); + } + /* Unsupported command so we did not process any data - mark it in current residue value */ + p_msc_ctx->current.residue = uint32_decode(p_msc_ctx->cbw.datlen); + } + return NRF_SUCCESS; +} + + +/** + * @brief Start status stage of CBW invalid + * + * @param[in,out] p_inst Generic class instance + * + * @sa status_generic_error_stall + */ +static void status_cbwinvalid_start(app_usbd_class_inst_t const * p_inst) +{ + status_generic_error_stall(p_inst, APP_USBD_MSC_STATE_CBW_INVALID); +} + + +/** + * @brief Start status stage of internal device error + * + * Kind of error that requires bulk reset but does not stall endpoint permanently - the correct + * answer is possible. + * + * @param[in] p_inst Generic class instance + * + * @sa status_generic_error_stall + */ +static void status_deverror_start(app_usbd_class_inst_t const * p_inst) +{ + status_generic_error_stall(p_inst, APP_USBD_MSC_STATE_DEVICE_ERROR); +} + + +/** + * @brief Internal SETUP standard IN request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_req_std_in(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + /* Only Get Descriptor standard IN request is supported by MSC class */ + if ((app_usbd_setup_req_rec(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQREC_INTERFACE) + && + (p_setup_ev->setup.bmRequest == APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR)) + { + size_t dsc_len = 0; + size_t max_size; + + uint8_t * p_trans_buff = app_usbd_core_setup_transfer_buff_get(&max_size); + + /* Try to find descriptor in class internals*/ + ret_code_t ret = app_usbd_class_descriptor_find( + p_inst, + p_setup_ev->setup.wValue.hb, + p_setup_ev->setup.wValue.lb, + p_trans_buff, + &dsc_len); + + if (ret != NRF_ERROR_NOT_FOUND) + { + ASSERT(dsc_len < NRF_DRV_USBD_EPSIZE); + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_trans_buff, dsc_len); + } + } + return NRF_ERROR_NOT_SUPPORTED; +} + + +/** + * @brief Internal SETUP standard OUT request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + * @retval NRF_ERROR_FORBIDDEN if endpoint stall cannot be cleared because of internal state + */ +static ret_code_t setup_req_std_out(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + app_usbd_setup_reqrec_t req_rec = app_usbd_setup_req_rec(p_setup_ev->setup.bmRequestType); + + if ((req_rec == APP_USBD_SETUP_REQREC_ENDPOINT) && + (p_setup_ev->setup.bmRequest == APP_USBD_SETUP_STDREQ_CLEAR_FEATURE) && + (p_setup_ev->setup.wValue.w == APP_USBD_SETUP_STDFEATURE_ENDPOINT_HALT)) + { + if (p_msc_ctx->state == APP_USBD_MSC_STATE_CBW_INVALID) + { + return NRF_ERROR_FORBIDDEN; + } + ret_code_t ret = NRF_SUCCESS; + /* Clearing endpoint here. It is done normally inside app_usbd, but we are overwritting this functionality */ + nrf_drv_usbd_ep_t ep_addr = (nrf_drv_usbd_ep_t)(p_setup_ev->setup.wIndex.lb); + nrf_drv_usbd_ep_dtoggle_clear(ep_addr); + nrf_drv_usbd_ep_stall_clear(ep_addr); + if (NRF_USBD_EPIN_CHECK(ep_addr)) + { + switch (p_msc_ctx->state) + { + case APP_USBD_MSC_STATE_UNSUPPORTED: + { + nrf_drv_usbd_ep_stall_clear(ep_out_addr_get(p_inst)); + /*Unsupported command handle: status stage*/ + ret = csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_FAIL); + if (ret != NRF_SUCCESS) + { + NRF_LOG_ERROR("Unexpected csw_wait_start on ep clear: %d", ret); + } + break; + } + + case APP_USBD_MSC_STATE_DEVICE_ERROR: + { + nrf_drv_usbd_ep_stall_clear(ep_out_addr_get(p_inst)); + /*Unsupported command handle: status stage*/ + ret = csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PE); + if (ret != NRF_SUCCESS) + { + NRF_LOG_ERROR("Unexpected csw_wait_start on ep clear: %d", ret); + } + break; + } + + default: + break; + } + } + return ret; + } + return NRF_ERROR_NOT_SUPPORTED; +} + + +/** + * @brief Internal SETUP class IN request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_req_class_in(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_MSC_REQ_GET_MAX_LUN: + { + + if (p_setup_ev->setup.wValue.w != 0) + { + break; + } + + if (p_setup_ev->setup.wLength.w != 1) + { + break; + } + + size_t tx_size; + uint16_t * p_tx_buff = app_usbd_core_setup_transfer_buff_get(&tx_size); + ASSERT(p_msc->specific.inst.block_devs_count != 0); + p_tx_buff[0] = p_msc->specific.inst.block_devs_count - 1; + + ret_code_t ret = cbw_wait_start(p_inst); + UNUSED_VARIABLE(ret); + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_tx_buff, sizeof(uint8_t)); + } + + default: + break; + + } + + return NRF_ERROR_NOT_SUPPORTED; +} + + +/** + * @brief Internal SETUP class OUT request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_req_class_out(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_MSC_REQ_BULK_RESET: + { + if (p_setup_ev->setup.wValue.w != 0) + { + break; + } + + if (p_setup_ev->setup.wLength.w != 0) + { + break; + } + + /* + * Reset internal state to be ready for next CBW + */ + NRF_LOG_DEBUG("bulk ep reset"); + bulk_ep_reset(p_inst); + + if (p_msc_ctx->state != APP_USBD_MSC_STATE_CBW) + { + ret_code_t ret = cbw_wait_start(p_inst); + UNUSED_VARIABLE(ret); + } + + return NRF_SUCCESS; + } + + default: + break; + } + + return NRF_ERROR_NOT_SUPPORTED; +} + + +/** + * @brief Control endpoint handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + ASSERT(p_inst != NULL); + ASSERT(p_setup_ev != NULL); + + if (app_usbd_setup_req_dir(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQDIR_IN) + { + switch (app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + case APP_USBD_SETUP_REQTYPE_STD: + return setup_req_std_in(p_inst, p_setup_ev); + + case APP_USBD_SETUP_REQTYPE_CLASS: + return setup_req_class_in(p_inst, p_setup_ev); + + default: + break; + } + } + else /* APP_USBD_SETUP_REQDIR_OUT */ + { + switch (app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + case APP_USBD_SETUP_REQTYPE_STD: + return setup_req_std_out(p_inst, p_setup_ev); + + case APP_USBD_SETUP_REQTYPE_CLASS: + return setup_req_class_out(p_inst, p_setup_ev); + + default: + break; + } + } + + return NRF_ERROR_NOT_SUPPORTED; +} + + +/** + * @brief Handle read6/read10 command data stage + * + * @param[in] p_inst Generic class instance + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t state_data_in_handle(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + ret_code_t ret = NRF_SUCCESS; + + p_msc_ctx->current.trans_in_progress = false; + APP_USBD_MSC_REQ_BUSY_CLR(p_msc_ctx->current.req_busy_mask, p_msc_ctx->current.trans_req_id); + + if (p_msc_ctx->current.blk_datasize == 0 && p_msc_ctx->current.req_busy_mask == 0) + { + p_msc_ctx->current.blk_idx = p_msc_ctx->current.blk_size = 0; + ret = csw_wait_start(p_inst, p_msc_ctx->current.csw_status); + return ret; + } + + ASSERT(p_msc_ctx->current.blk_size != 0); + + if (p_msc_ctx->current.req_busy_mask == 0) + { + nrf_block_dev_t const * p_blkd = + p_msc->specific.inst.pp_block_devs[p_msc_ctx->current.lun]; + + /*Trigger new block read request*/ + p_msc_ctx->current.workbuff_pos ^= p_msc->specific.inst.block_buff_size; + uint32_t req_pos = p_msc_ctx->current.workbuff_pos != 0 ? 1 : 0; + APP_USBD_MSC_REQ_BUSY_SET(p_msc_ctx->current.req_busy_mask, req_pos); + + NRF_BLOCK_DEV_REQUEST(req, + p_msc_ctx->current.blk_idx, + p_msc_ctx->current.blk_count, + ((uint8_t *)p_msc->specific.inst.p_block_buff + + p_msc_ctx->current.workbuff_pos)); + + NRF_LOG_DEBUG("nrf_blk_dev_read_req 3: id: %u, count: %u, left: %u, ptr: %p", + req.blk_id, + req.blk_count, + p_msc_ctx->current.blk_datasize, + (uint32_t)req.p_buff); + + ret = nrf_blk_dev_read_req(p_blkd, &req); + NRF_LOG_DEBUG("nrf_blk_dev_read_req 3: ret: %u", ret); + return ret; + } + + uint32_t blk_size = p_msc_ctx->current.blk_size; + p_msc_ctx->current.trans_req_id ^= 1; + + nrf_block_req_t * p_req = &p_msc_ctx->current.req; + if (p_req->p_buff == NULL) + { + p_msc_ctx->current.trans_req_id ^= 1; + return NRF_SUCCESS; + } + + ret = transfer_in_start(p_inst, + p_req->p_buff, + p_req->blk_count * blk_size, + APP_USBD_MSC_STATE_DATA_IN); + + if (ret == NRF_SUCCESS) + { + /*Clear processed block request.*/ + memset(p_req, 0, sizeof(nrf_block_req_t)); + p_msc_ctx->current.trans_in_progress = true; + } + + return ret; +} + + +/** + * @brief Endpoint IN event handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t endpoint_in_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_complex_evt_t const * p_event) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + NRF_LOG_DEBUG("state: %d, ep in event, status: %d", + p_msc_ctx->state, + p_event->drv_evt.data.eptransfer.status); + + if (p_event->drv_evt.data.eptransfer.status != NRF_USBD_EP_OK) + { + return NRF_SUCCESS; + } + + ret_code_t ret = NRF_SUCCESS; + + switch (p_msc_ctx->state) + { + case APP_USBD_MSC_STATE_CMD_IN: + { + ret = csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PASS); + break; + } + + case APP_USBD_MSC_STATE_DATA_IN: + { + ret = state_data_in_handle(p_inst); + break; + } + + case APP_USBD_MSC_STATE_CSW: + { + break; + } + + case APP_USBD_MSC_STATE_DATA_OUT: + { + break; + } + + case APP_USBD_MSC_STATE_UNSUPPORTED: + { + ret = NRF_ERROR_NOT_SUPPORTED; + break; + } + + default: + { + ret = NRF_ERROR_INTERNAL; + break; + } + } + + return ret; +} + + +/** + * @brief Helper function to calculate next block transfer block count + * + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * + * @return Blocks to transfer + * */ +static uint32_t next_transfer_blkcnt_calc(app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + uint32_t blkcnt = p_msc->specific.inst.block_buff_size / p_msc_ctx->current.blk_size; + + if (blkcnt > (p_msc_ctx->current.blk_datasize / p_msc_ctx->current.blk_size)) + { + blkcnt = p_msc_ctx->current.blk_datasize / p_msc_ctx->current.blk_size; + } + + return blkcnt; +} + + +/** + * @brief Helper function to calculate next transfer size + * + * @param[in] p_msc_ctx MSC context + * + * @return Blocks to transfer + * */ +static uint32_t next_transfer_size_calc(app_usbd_msc_ctx_t * p_msc_ctx) +{ + uint32_t blk_cnt = p_msc_ctx->current.blk_count; + uint32_t blk_size = p_msc_ctx->current.blk_size; + + return p_msc_ctx->current.blk_datasize > blk_size * blk_cnt ? + blk_size * blk_cnt : p_msc_ctx->current.blk_datasize; +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_TESTUNITREADY handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_testunitready(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: TESTUNITREADY"); + if (uint32_decode(p_msc_ctx->cbw.datlen) != 0) + { + return status_unsupported_start(p_inst); + } + + if (p_msc_ctx->cbw.cdb_length != APP_USBD_SCSI_CMD_TESTUNITREADY_LEN) + { + return status_unsupported_start(p_inst); + } + + if (p_msc_ctx->cbw.lun >= p_msc->specific.inst.block_devs_count) + { + return status_unsupported_start(p_inst); + } + + return csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PASS); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_REQUESTSENSE handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_requestsense(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: REQUESTSENSE"); + app_usbd_scsi_cmd_requestsense_t const * p_reqs = (const void *)p_msc_ctx->cbw.cdb; + UNUSED_VARIABLE(p_reqs); + + if ((p_msc_ctx->cbw.cdb_length < sizeof(app_usbd_scsi_cmd_requestsense_t))) + { + return status_unsupported_start(p_inst); + } + + if (p_msc_ctx->cbw.lun >= p_msc->specific.inst.block_devs_count) + { + return status_unsupported_start(p_inst); + } + + if (uint32_decode(p_msc_ctx->cbw.datlen) > sizeof(app_usbd_scsi_cmd_requestsense_resp_t)) + { + return status_unsupported_start(p_inst); + } + + p_msc_ctx->resp_len = uint32_decode(p_msc_ctx->cbw.datlen); + + if (p_msc_ctx->resp_len == 0) + { + return csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PASS); + } + + memset(&p_msc_ctx->scsi_resp, 0, sizeof(app_usbd_scsi_cmd_requestsense_resp_t)); + p_msc_ctx->scsi_resp.requestsense.code = APP_USBD_SCSI_CMD_REQSENSE_CODE_VALID | + APP_USBD_SCSI_CMD_REQSENSE_CODE_CURRENT; + + p_msc_ctx->scsi_resp.requestsense.len = sizeof(app_usbd_scsi_cmd_requestsense_resp_t) - + offsetof(app_usbd_scsi_cmd_requestsense_resp_t, len); + + return transfer_in_start(p_inst, + &p_msc_ctx->scsi_resp, + p_msc_ctx->resp_len, + APP_USBD_MSC_STATE_CMD_IN); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_FORMAT_UNIT handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_formatunit(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: FORMAT_UNIT"); + return status_unsupported_start(p_inst); +} + + +static ret_code_t cmd_read_start(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + nrf_block_dev_t const * p_blkd = p_msc->specific.inst.pp_block_devs[p_msc_ctx->cbw.lun]; + + p_msc_ctx->current.trans_in_progress = false; + p_msc_ctx->current.req_busy_mask = 0; + p_msc_ctx->current.workbuff_pos = 0; + + APP_USBD_MSC_REQ_BUSY_SET(p_msc_ctx->current.req_busy_mask, 0); + NRF_BLOCK_DEV_REQUEST(req, + p_msc_ctx->current.blk_idx, + p_msc_ctx->current.blk_count, + ((uint8_t *)p_msc->specific.inst.p_block_buff + + p_msc_ctx->current.workbuff_pos)); + + NRF_LOG_DEBUG("cmd_read_start"); + NRF_LOG_DEBUG("nrf_blk_dev_read_req 1: id: %u, count: %u, left: %u, ptr: %p", + req.blk_id, + req.blk_count, + p_msc_ctx->current.blk_datasize, + (uint32_t)req.p_buff); + + ret_code_t ret = nrf_blk_dev_read_req(p_blkd, &req); + NRF_LOG_DEBUG("nrf_blk_dev_read_req 1: ret: %u", ret); + + return ret; +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_READ6 handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_read6(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: READ6"); + app_usbd_scsi_cmd_read6_t const * p_read6 = (const void *)p_msc_ctx->cbw.cdb; + if (p_msc_ctx->cbw.cdb_length < sizeof(app_usbd_scsi_cmd_read6_t)) + { + return status_unsupported_start(p_inst); + } + + if (p_msc_ctx->cbw.lun >= p_msc->specific.inst.block_devs_count) + { + return status_unsupported_start(p_inst); + } + + nrf_block_dev_t const * p_blkd = p_msc->specific.inst.pp_block_devs[p_msc_ctx->cbw.lun]; + + p_msc_ctx->current.lun = p_msc_ctx->cbw.lun; + p_msc_ctx->current.blk_idx = ((p_read6->mslba & 0x1F) << 16) | + uint16_big_decode(p_read6->lslba); + p_msc_ctx->current.blk_datasize = uint32_decode(p_msc_ctx->cbw.datlen); + p_msc_ctx->current.blk_size = nrf_blk_dev_geometry(p_blkd)->blk_size; + p_msc_ctx->current.blk_count = next_transfer_blkcnt_calc(p_msc, p_msc_ctx); + + if (p_msc_ctx->current.blk_datasize > p_msc_ctx->current.blk_size * p_read6->xfrlen) + { + p_msc_ctx->current.residue = p_msc_ctx->current.blk_datasize - + p_msc_ctx->current.blk_size * p_read6->xfrlen; + } + + return cmd_read_start(p_inst, p_msc, p_msc_ctx); +} + + +static ret_code_t cmd_write_start(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("cmd_write_start"); + ret_code_t ret = transfer_out_start(p_inst, + ((uint8_t *)p_msc->specific.inst.p_block_buff + + p_msc_ctx->current.workbuff_pos), + next_transfer_size_calc(p_msc_ctx), + APP_USBD_MSC_STATE_DATA_OUT); + + if (ret == NRF_SUCCESS) + { + p_msc_ctx->current.trans_req_id = 0; + APP_USBD_MSC_REQ_BUSY_SET(p_msc_ctx->current.req_busy_mask, + p_msc_ctx->current.trans_req_id); + p_msc_ctx->current.workbuff_pos ^= p_msc->specific.inst.block_buff_size; + p_msc_ctx->current.trans_in_progress = true; + } + + return ret; +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_WRITE6 handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_write6(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: WRITE6"); + app_usbd_scsi_cmd_write6_t const * p_write6 = (const void *)p_msc_ctx->cbw.cdb; + if (p_msc_ctx->cbw.cdb_length < sizeof(app_usbd_scsi_cmd_write6_t)) + { + return status_unsupported_start(p_inst); + } + + if (p_msc_ctx->cbw.lun >= p_msc->specific.inst.block_devs_count) + { + return status_unsupported_start(p_inst); + } + + nrf_block_dev_t const * p_blkd = p_msc->specific.inst.pp_block_devs[p_msc_ctx->cbw.lun]; + + p_msc_ctx->current.lun = p_msc_ctx->cbw.lun; + p_msc_ctx->current.blk_idx = (p_write6->mslba & 0x1F << 16) | + uint16_big_decode(p_write6->lslba); + p_msc_ctx->current.blk_datasize = uint32_decode(p_msc_ctx->cbw.datlen); + p_msc_ctx->current.blk_size = nrf_blk_dev_geometry(p_blkd)->blk_size; + p_msc_ctx->current.blk_count = next_transfer_blkcnt_calc(p_msc, p_msc_ctx); + + if (p_msc_ctx->current.blk_datasize > p_msc_ctx->current.blk_size * p_write6->xfrlen) + { + p_msc_ctx->current.residue = p_msc_ctx->current.blk_datasize - + p_msc_ctx->current.blk_size * p_write6->xfrlen; + } + + return cmd_write_start(p_inst, p_msc, p_msc_ctx); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_INQUIRY handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_inquiry(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: INQUIRY"); + app_usbd_scsi_cmd_inquiry_t const * p_inq = (const void *)p_msc_ctx->cbw.cdb; + if (p_inq->pagecode != 0) + { + NRF_LOG_WARNING("unsupported pagecode"); + return status_unsupported_start(p_inst); + } + + if (p_msc_ctx->cbw.lun >= p_msc->specific.inst.block_devs_count) + { + NRF_LOG_WARNING("unsupported LUN"); + return status_unsupported_start(p_inst); + } + + if (uint32_decode(p_msc_ctx->cbw.datlen) > sizeof(app_usbd_scsi_cmd_inquiry_resp_t)) + { + return status_unsupported_start(p_inst); + } + + p_msc_ctx->resp_len = uint32_decode(p_msc_ctx->cbw.datlen); + + if (p_msc_ctx->resp_len == 0) + { + return csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PASS); + } + + + p_msc_ctx->scsi_resp.inquiry.qualtype = APP_USBD_MSC_SCSI_INQ_QUAL_CONNECTED | + APP_USBD_MSC_SCSI_INQ_TYPE_DIR_ACCESS; + p_msc_ctx->scsi_resp.inquiry.flags1 = APP_USBD_MSC_SCSI_INQ_FLAG1_RMB; + p_msc_ctx->scsi_resp.inquiry.version = APP_USBD_SCSI_INQ_VER_SPC4; + p_msc_ctx->scsi_resp.inquiry.flags2 = APP_USBD_MSC_SCSI_INQ_FLAG2_RSP_SPC2 | + APP_USBD_MSC_SCSI_INQ_FLAG2_HISUP; + p_msc_ctx->scsi_resp.inquiry.len = sizeof(app_usbd_scsi_cmd_inquiry_resp_t) - + offsetof(app_usbd_scsi_cmd_inquiry_resp_t, len); + + nrf_block_dev_t const * p_blkd = + p_msc->specific.inst.pp_block_devs[p_msc_ctx->cbw.lun]; + nrf_block_dev_info_strings_t * p_strings = NULL; + UNUSED_RETURN_VALUE(nrf_blk_dev_ioctl(p_blkd, + NRF_BLOCK_DEV_IOCTL_REQ_INFO_STRINGS, + &p_strings)); + + if (p_strings) + { + UNUSED_RETURN_VALUE(strncpy((char *)p_msc_ctx->scsi_resp.inquiry.vendorid, + p_strings->p_vendor, + sizeof(p_msc_ctx->scsi_resp.inquiry.vendorid))); + + UNUSED_RETURN_VALUE(strncpy((char *)p_msc_ctx->scsi_resp.inquiry.productid, + p_strings->p_product, + sizeof(p_msc_ctx->scsi_resp.inquiry.productid))); + + UNUSED_RETURN_VALUE(strncpy((char *)p_msc_ctx->scsi_resp.inquiry.revision, + p_strings->p_revision, + sizeof(p_msc_ctx->scsi_resp.inquiry.revision))); + } + else + { + memset(p_msc_ctx->scsi_resp.inquiry.vendorid, + 0, + sizeof(p_msc_ctx->scsi_resp.inquiry.vendorid)); + memset(p_msc_ctx->scsi_resp.inquiry.productid, + 0, + sizeof(p_msc_ctx->scsi_resp.inquiry.productid)); + memset(p_msc_ctx->scsi_resp.inquiry.revision, + 0, + sizeof(p_msc_ctx->scsi_resp.inquiry.revision)); + } + + return transfer_in_start(p_inst, + &p_msc_ctx->scsi_resp, + p_msc_ctx->resp_len, + APP_USBD_MSC_STATE_CMD_IN); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_MODESELECT6 handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_modeselect6(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: MODESELECT6"); + return csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PASS); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_MODESENSE6 handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_modesense6(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: MODESENSE6"); + app_usbd_scsi_cmd_modesense6_t const * p_sense6 = (const void *)p_msc_ctx->cbw.cdb; + UNUSED_VARIABLE(p_sense6); + if (p_msc_ctx->cbw.cdb_length < sizeof(app_usbd_scsi_cmd_modesense6_t)) + { + return status_unsupported_start(p_inst); + } + + if (p_msc_ctx->cbw.lun >= p_msc->specific.inst.block_devs_count) + { + return status_unsupported_start(p_inst); + } + + if (uint32_decode(p_msc_ctx->cbw.datlen) > sizeof(app_usbd_scsi_cmd_modesense6_resp_t)) + { + return status_unsupported_start(p_inst); + } + + p_msc_ctx->resp_len = uint32_decode(p_msc_ctx->cbw.datlen); + + app_usbd_scsi_cmd_modesense6_resp_t * p_resp = &p_msc_ctx->scsi_resp.modesense6; + p_resp->mdlen = sizeof(app_usbd_scsi_cmd_modesense6_resp_t) - 1; + p_resp->type = 0; + p_resp->param = 0; + p_resp->bdlen = 0; + + return transfer_in_start(p_inst, + &p_msc_ctx->scsi_resp, + p_msc_ctx->resp_len, + APP_USBD_MSC_STATE_CMD_IN); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_STARTSTOPUNIT handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_startstopunit(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: STARTSTOPUNIT"); + return csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PASS); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_SENDDIAGNOSTIC handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_senddiagnostic(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: SENDDIAGNOSTIC"); + return csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PASS); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_PREVENTMEDIAREMOVAL handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_preventremoval(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: PREVENTMEDIAREMOVAL"); + return csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PASS); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_READCAPACITY10 handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_readcapacity10(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: READCAPACITY10"); + + app_usbd_scsi_cmd_readcapacity10_t const * p_cap10 = (const void *)p_msc_ctx->cbw.cdb; + UNUSED_VARIABLE(p_cap10); + if (p_msc_ctx->cbw.cdb_length < sizeof(app_usbd_scsi_cmd_readcapacity10_t)) + { + return status_unsupported_start(p_inst); + } + + if (p_msc_ctx->cbw.lun >= p_msc->specific.inst.block_devs_count) + { + return status_unsupported_start(p_inst); + } + + if (uint32_decode(p_msc_ctx->cbw.datlen) > sizeof(app_usbd_scsi_cmd_readcapacity10_resp_t)) + { + return status_unsupported_start(p_inst); + } + + p_msc_ctx->resp_len = uint32_decode(p_msc_ctx->cbw.datlen); + + if (p_msc_ctx->resp_len == 0) + { + return csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PASS); + } + + nrf_block_dev_t const * p_blkd = + p_msc->specific.inst.pp_block_devs[p_msc_ctx->cbw.lun]; + nrf_block_dev_geometry_t const * p_geometry = nrf_blk_dev_geometry(p_blkd); + + (void)uint32_big_encode(p_geometry->blk_count - 1, p_msc_ctx->scsi_resp.readcapacity10.lba); + (void)uint32_big_encode(p_geometry->blk_size, p_msc_ctx->scsi_resp.readcapacity10.blklen); + + return transfer_in_start(p_inst, + &p_msc_ctx->scsi_resp, + p_msc_ctx->resp_len, + APP_USBD_MSC_STATE_CMD_IN); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_READ10 handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_read10(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: READ10"); + app_usbd_scsi_cmd_read10_t const * p_read10 = (const void *)p_msc_ctx->cbw.cdb; + if (p_msc_ctx->cbw.cdb_length < sizeof(app_usbd_scsi_cmd_read10_t)) + { + return status_unsupported_start(p_inst); + } + + if (p_msc_ctx->cbw.lun >= p_msc->specific.inst.block_devs_count) + { + return status_unsupported_start(p_inst); + } + + if (uint32_decode(p_msc_ctx->cbw.datlen) == 0) + { + return csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_FAIL); + } + + if ((p_msc_ctx->cbw.flags & APP_USBD_MSC_CBW_DIRECTION_IN) == 0) + { + return status_unsupported_start(p_inst); + } + + nrf_block_dev_t const * p_blkd = p_msc->specific.inst.pp_block_devs[p_msc_ctx->cbw.lun]; + + p_msc_ctx->current.lun = p_msc_ctx->cbw.lun; + p_msc_ctx->current.blk_idx = uint32_big_decode(p_read10->lba); + p_msc_ctx->current.blk_datasize = uint32_decode(p_msc_ctx->cbw.datlen); + p_msc_ctx->current.blk_size = nrf_blk_dev_geometry(p_blkd)->blk_size; + p_msc_ctx->current.blk_count = next_transfer_blkcnt_calc(p_msc, p_msc_ctx); + + uint16_t blocks = uint16_big_decode(p_read10->xfrlen); + p_msc_ctx->current.csw_status = + uint32_decode(p_msc_ctx->cbw.datlen) < p_msc_ctx->current.blk_size * blocks ? + APP_USBD_MSC_CSW_STATUS_FAIL : APP_USBD_MSC_CSW_STATUS_PASS; + + if (p_msc_ctx->current.blk_datasize > p_msc_ctx->current.blk_size * blocks) + { + p_msc_ctx->current.residue = p_msc_ctx->current.blk_datasize - + p_msc_ctx->current.blk_size * blocks; + } + + return cmd_read_start(p_inst, p_msc, p_msc_ctx); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_WRITE10 handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_write10(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: WRITE10"); + app_usbd_scsi_cmd_write10_t const * p_write10 = (const void *)p_msc_ctx->cbw.cdb; + if (p_msc_ctx->cbw.cdb_length < sizeof(app_usbd_scsi_cmd_write10_t)) + { + return status_unsupported_start(p_inst); + } + + if (p_msc_ctx->cbw.lun >= p_msc->specific.inst.block_devs_count) + { + return status_unsupported_start(p_inst); + } + + if (uint32_decode(p_msc_ctx->cbw.datlen) == 0) + { + return csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_FAIL); + } + + + if ((p_msc_ctx->cbw.flags & APP_USBD_MSC_CBW_DIRECTION_IN) != 0) + { + return status_unsupported_start(p_inst); + } + + nrf_block_dev_t const * p_blkd = p_msc->specific.inst.pp_block_devs[p_msc_ctx->cbw.lun]; + + p_msc_ctx->current.lun = p_msc_ctx->cbw.lun; + p_msc_ctx->current.blk_idx = uint32_big_decode(p_write10->lba); + p_msc_ctx->current.blk_datasize = uint32_decode(p_msc_ctx->cbw.datlen); + p_msc_ctx->current.blk_size = nrf_blk_dev_geometry(p_blkd)->blk_size; + p_msc_ctx->current.blk_count = next_transfer_blkcnt_calc(p_msc, p_msc_ctx); + + uint16_t blocks = uint16_big_decode(p_write10->xfrlen); + p_msc_ctx->current.csw_status = + uint32_decode(p_msc_ctx->cbw.datlen) < p_msc_ctx->current.blk_size * blocks ? + APP_USBD_MSC_CSW_STATUS_FAIL : APP_USBD_MSC_CSW_STATUS_PASS; + + if (p_msc_ctx->current.blk_datasize > p_msc_ctx->current.blk_size * blocks) + { + p_msc_ctx->current.residue = p_msc_ctx->current.blk_datasize - + p_msc_ctx->current.blk_size * blocks; + } + + return cmd_write_start(p_inst, p_msc, p_msc_ctx); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_MODESELECT10 handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_modeselect10(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: MODESELECT10"); + return csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PASS); +} + + +/** + * @brief SCSI Command: @ref APP_USBD_SCSI_CMD_MODESENSE10 handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_msc MSC instance + * @param[in] p_msc_ctx MSC context + * @return Standard error code + * */ +static ret_code_t cmd_modesense10(app_usbd_class_inst_t const * p_inst, + app_usbd_msc_t const * p_msc, + app_usbd_msc_ctx_t * p_msc_ctx) +{ + NRF_LOG_DEBUG("CMD: MODESENSE10"); + app_usbd_scsi_cmd_modesense10_t const * p_sense10 = (const void *)p_msc_ctx->cbw.cdb; + UNUSED_VARIABLE(p_sense10); + if (p_msc_ctx->cbw.cdb_length < sizeof(app_usbd_scsi_cmd_modesense10_t)) + { + return status_unsupported_start(p_inst); + } + + if (p_msc_ctx->cbw.lun >= p_msc->specific.inst.block_devs_count) + { + return status_unsupported_start(p_inst); + } + + if (uint32_decode(p_msc_ctx->cbw.datlen) > sizeof(app_usbd_scsi_cmd_modesense6_resp_t)) + { + return status_unsupported_start(p_inst); + } + + p_msc_ctx->resp_len = uint32_decode(p_msc_ctx->cbw.datlen); + + app_usbd_scsi_cmd_modesense10_resp_t * p_resp = &p_msc_ctx->scsi_resp.modesense10; + + memset(p_resp, 0, sizeof(app_usbd_scsi_cmd_modesense10_resp_t)); + uint16_t len = sizeof(app_usbd_scsi_cmd_modesense10_resp_t) - sizeof(p_resp->mdlen); + p_resp->mdlen[1] = len & 0xff; + p_resp->mdlen[0] = (len >> 8) & 0xff; + + return transfer_in_start(p_inst, + &p_msc_ctx->scsi_resp, + p_msc_ctx->resp_len, + APP_USBD_MSC_STATE_CMD_IN); +} + + +/** + * @brief Get the size of the last OUT transfer + * + * @param p_inst Generic class instance + * + * @return Number of received bytes or 0 if none. + */ +static size_t get_last_out_size(app_usbd_class_inst_t const * p_inst) +{ + nrf_drv_usbd_ep_t ep = ep_out_addr_get(p_inst); + size_t size; + ret_code_t ret = nrf_drv_usbd_ep_status_get(ep, &size); + + if (ret != NRF_SUCCESS) + { + size = 0; + } + + return size; +} + + +/** + * @brief SCSI Command Block Wrapper handler + * + * @param[in] p_inst Generic class instance + * @return Standard error code + * */ +static ret_code_t state_cbw(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + memset(&p_msc_ctx->current, 0, sizeof(p_msc_ctx->current)); + + /*Verify the transfer size*/ + if (get_last_out_size(p_inst) != sizeof(app_usbd_msc_cbw_t)) + { + NRF_LOG_DEBUG("CMD: size error: %u", get_last_out_size(p_inst)); + status_cbwinvalid_start(p_inst); + return NRF_SUCCESS; + } + + /*Verify CBW signature*/ + if (memcmp(p_msc_ctx->cbw.signature, + APP_USBD_MSC_CBW_SIGNATURE, + sizeof(p_msc_ctx->cbw.signature)) != 0) + { + NRF_LOG_DEBUG("CMD: header error: 0x%02x%02x%02x%02x", + p_msc_ctx->cbw.signature[0], p_msc_ctx->cbw.signature[1], + p_msc_ctx->cbw.signature[2], p_msc_ctx->cbw.signature[3]); + + status_cbwinvalid_start(p_inst); + return NRF_SUCCESS; + } + + ret_code_t ret = NRF_SUCCESS; + + switch (p_msc_ctx->cbw.cdb[0]) + { + case APP_USBD_SCSI_CMD_TESTUNITREADY: + ret = cmd_testunitready(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_REQUESTSENSE: + ret = cmd_requestsense(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_FORMAT_UNIT: + ret = cmd_formatunit(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_READ6: + ret = cmd_read6(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_WRITE6: + ret = cmd_write6(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_INQUIRY: + ret = cmd_inquiry(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_MODESELECT6: + ret = cmd_modeselect6(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_MODESENSE6: + ret = cmd_modesense6(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_STARTSTOPUNIT: + ret = cmd_startstopunit(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_SENDDIAGNOSTIC: + ret = cmd_senddiagnostic(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_PREVENTMEDIAREMOVAL: + ret = cmd_preventremoval(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_READCAPACITY10: + ret = cmd_readcapacity10(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_READ10: + ret = cmd_read10(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_WRITE10: + ret = cmd_write10(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_MODESELECT10: + ret = cmd_modeselect10(p_inst, p_msc, p_msc_ctx); + break; + + case APP_USBD_SCSI_CMD_MODESENSE10: + ret = cmd_modesense10(p_inst, p_msc, p_msc_ctx); + break; + + default: + NRF_LOG_DEBUG("CMD: UNSUPPORTED"); + NRF_LOG_HEXDUMP_DEBUG(&(p_msc_ctx->cbw), sizeof(p_msc_ctx->cbw)); + if (uint32_decode(p_msc_ctx->cbw.datlen) != 0) + { + ret = status_unsupported_start(p_inst); + } + else + { + ret = csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_FAIL); + } + break; + } + + return ret; +} + + +/** + * @brief Handle write6/write10 command data stage + * + * @param[in] p_inst Generic class instance + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t state_data_out_handle(app_usbd_class_inst_t const * p_inst) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + ret_code_t ret = NRF_SUCCESS; + uint32_t blk_size = p_msc_ctx->current.blk_size; + + NRF_LOG_DEBUG("APP_USBD_MSC_STATE_DATA_OUT"); + + p_msc_ctx->current.trans_in_progress = false; + if ((p_msc_ctx->current.req_busy_mask != APP_USBD_MSC_REQ_BUSY_FULL_MASK) && + (p_msc_ctx->current.blk_datasize > p_msc_ctx->current.blk_count * blk_size)) + { + size_t size = p_msc_ctx->current.blk_datasize - p_msc_ctx->current.blk_count * blk_size; + if (size > p_msc_ctx->current.blk_count * blk_size) + { + size = p_msc_ctx->current.blk_count * blk_size; + } + + if (size) + { + ret = transfer_out_start(p_inst, + ((uint8_t *)p_msc->specific.inst.p_block_buff + + p_msc_ctx->current.workbuff_pos), + size, + APP_USBD_MSC_STATE_DATA_OUT); + if (ret == NRF_SUCCESS) + { + p_msc_ctx->current.trans_req_id ^= 1; + APP_USBD_MSC_REQ_BUSY_SET(p_msc_ctx->current.req_busy_mask, + p_msc_ctx->current.trans_req_id); + p_msc_ctx->current.workbuff_pos ^= p_msc->specific.inst.block_buff_size; + p_msc_ctx->current.trans_in_progress = true; + } + } + } + + if (!p_msc_ctx->current.block_req_in_progress) + { + nrf_block_dev_t const * p_blkd = + p_msc->specific.inst.pp_block_devs[p_msc_ctx->current.lun]; + + size_t pos = p_msc_ctx->current.workbuff_pos; + if (p_msc_ctx->current.req_busy_mask != APP_USBD_MSC_REQ_BUSY_FULL_MASK) + { + pos ^= p_msc->specific.inst.block_buff_size; + } + + NRF_BLOCK_DEV_REQUEST(req, + p_msc_ctx->current.blk_idx, + p_msc_ctx->current.blk_count, + ((uint8_t *)p_msc->specific.inst.p_block_buff + pos)); + + NRF_LOG_DEBUG("nrf_blk_dev_write_req 1: id: %u, count: %u, left: %u, ptr: %p", + req.blk_id, + req.blk_count, + p_msc_ctx->current.blk_datasize, + (uint32_t)req.p_buff); + + p_msc_ctx->current.block_req_in_progress = true; + ret = nrf_blk_dev_write_req(p_blkd, &req); + NRF_LOG_DEBUG("nrf_blk_dev_write_req 1: ret: %u", ret); + } + + return ret; +} + + +/** + * @brief Endpoint OUT event handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t endpoint_out_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_complex_evt_t const * p_event) +{ + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + NRF_LOG_DEBUG("state: %d, ep out event, status: %d", + p_msc_ctx->state, + p_event->drv_evt.data.eptransfer.status); + + ret_code_t ret = NRF_SUCCESS; + if (p_event->drv_evt.data.eptransfer.status == NRF_USBD_EP_WAITING) + { + if (p_msc_ctx->state == APP_USBD_MSC_STATE_DATA_OUT) + { + NRF_LOG_DEBUG("NRF_USBD_EP_WAITING"); + } + else if (p_msc_ctx->state == APP_USBD_MSC_STATE_CSW || + p_msc_ctx->state == APP_USBD_MSC_STATE_IDLE) + { + ret = cbw_wait_start(p_inst); + } + + return ret; + } + else if (p_event->drv_evt.data.eptransfer.status == NRF_USBD_EP_ABORTED) + { + p_msc_ctx->state = APP_USBD_MSC_STATE_IDLE; + return NRF_SUCCESS; + } + else if (p_event->drv_evt.data.eptransfer.status == NRF_USBD_EP_OVERLOAD) + { + NRF_LOG_ERROR("Data overload"); + + switch (p_msc_ctx->state) + { + case APP_USBD_MSC_STATE_DATA_OUT: + { + status_deverror_start(p_inst); + } + + /* Default action is the same like CBW - stall totally until bulk reset */ + case APP_USBD_MSC_STATE_CBW: + default: + { + status_cbwinvalid_start(p_inst); + } + } + return NRF_SUCCESS; + } + else /*NRF_USBD_EP_OK*/ + { + switch (p_msc_ctx->state) + { + case APP_USBD_MSC_STATE_CBW: + { + ret = state_cbw(p_inst); + break; + } + + case APP_USBD_MSC_STATE_CMD_OUT: + { + ret = csw_wait_start(p_inst, APP_USBD_MSC_CSW_STATUS_PASS); + break; + } + + case APP_USBD_MSC_STATE_DATA_OUT: + { + CRITICAL_REGION_ENTER(); + ret = state_data_out_handle(p_inst); + CRITICAL_REGION_EXIT(); + break; + } + + case APP_USBD_MSC_STATE_UNSUPPORTED: + { + ret = NRF_ERROR_NOT_SUPPORTED; + break; + } + + case APP_USBD_MSC_STATE_CSW: + break; + + default: + { + ASSERT(0); + ret = NRF_ERROR_NOT_SUPPORTED; + break; + } + } + } + + NRF_LOG_DEBUG("Ep proc status: %d", ret); + return ret; +} + + +/** + * @brief @ref app_usbd_class_methods_t::event_handler + */ +static ret_code_t msc_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_complex_evt_t const * p_event) +{ + ASSERT(p_inst != NULL); + ASSERT(p_event != NULL); + + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + UNUSED_VARIABLE(p_msc); + UNUSED_VARIABLE(p_msc_ctx); + + ret_code_t ret = NRF_SUCCESS; + + switch (p_event->app_evt.type) + { + case APP_USBD_EVT_DRV_SOF: + break; + + case APP_USBD_EVT_DRV_RESET: + break; + + case APP_USBD_EVT_DRV_SETUP: + ret = setup_event_handler(p_inst, (app_usbd_setup_evt_t const *)p_event); + break; + + case APP_USBD_EVT_DRV_EPTRANSFER: + if (NRF_USBD_EPIN_CHECK(p_event->drv_evt.data.eptransfer.ep)) + { + ret = endpoint_in_event_handler(p_inst, p_event); + } + else + { + ret = endpoint_out_event_handler(p_inst, p_event); + } + + break; + + case APP_USBD_EVT_DRV_SUSPEND: + { + /*Flush all block devices cache on suspend*/ + + for (size_t i = 0; i < p_msc->specific.inst.block_devs_count; ++i) + { + nrf_block_dev_t const * p_blkd = p_msc->specific.inst.pp_block_devs[i]; + (void)nrf_blk_dev_ioctl(p_blkd, NRF_BLOCK_DEV_IOCTL_REQ_CACHE_FLUSH, NULL); + } + + break; + } + + case APP_USBD_EVT_DRV_RESUME: + break; + + case APP_USBD_EVT_INST_APPEND: + { + /*Verify serial number string*/ + uint16_t const * p_serial_str = app_usbd_string_desc_get(APP_USBD_STRING_ID_SERIAL, 0); + if (p_serial_str == NULL) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + size_t len = app_usbd_string_desc_length(p_serial_str) / sizeof(uint16_t); + if (len < APP_USBD_MSC_MINIMAL_SERIAL_STRING_SIZE) + { + return NRF_ERROR_NOT_SUPPORTED; + } + + for (size_t i = 1; i < len; ++i) + { + if (isxdigit(p_serial_str[i]) == 0) + { + return NRF_ERROR_NOT_SUPPORTED; + } + } + + break; + } + + case APP_USBD_EVT_INST_REMOVE: + { + break; + } + + case APP_USBD_EVT_STARTED: + { + /*Initialize all block devices*/ + ASSERT(p_msc->specific.inst.block_devs_count <= 16); + + for (size_t i = 0; i < p_msc->specific.inst.block_devs_count; ++i) + { + nrf_block_dev_t const * p_blk_dev = p_msc->specific.inst.pp_block_devs[i]; + ret = nrf_blk_dev_init(p_blk_dev, msc_blockdev_ev_handler, p_msc); + if (ret != NRF_SUCCESS) + { + continue; + } + + p_msc_ctx->blk_dev_init_mask |= 1u << i; + ASSERT(nrf_blk_dev_geometry(p_blk_dev)->blk_size <= + p_msc->specific.inst.block_buff_size); + } + + break; + } + + case APP_USBD_EVT_STOPPED: + { + /*Un-initialize all block devices*/ + ASSERT(p_msc->specific.inst.block_devs_count <= 16); + size_t i; + + for (i = 0; i < p_msc->specific.inst.block_devs_count; ++i) + { + nrf_block_dev_t const * p_blk_dev = p_msc->specific.inst.pp_block_devs[i]; + ret = nrf_blk_dev_uninit(p_blk_dev); + if (ret != NRF_SUCCESS) + { + continue; + } + + p_msc_ctx->blk_dev_init_mask &= ~(1u << i); + } + + break; + } + + default: + ret = NRF_ERROR_NOT_SUPPORTED; + break; + } + + return ret; +} + + +/** + * @brief @ref app_usbd_class_methods_t::feed_descriptors + */ + +static bool msc_feed_descriptors(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size) +{ + static uint8_t ifaces = 0; + ifaces = app_usbd_class_iface_count_get(p_inst); + app_usbd_msc_t const * p_msc = msc_get(p_inst); + + APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size); + + static uint8_t i = 0; + + for (i = 0; i < ifaces; i++) + { + /* INTERFACE DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x09); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE); // bDescriptorType = Interface + + static app_usbd_class_iface_conf_t const * p_cur_iface = NULL; + p_cur_iface = app_usbd_class_iface_get(p_inst, i); + + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_number_get(p_cur_iface)); // bInterfaceNumber + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bAlternateSetting + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_ep_count_get(p_cur_iface)); // bNumEndpoints + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_MSC_CLASS); // bInterfaceClass = MSC + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_msc->specific.inst.subclass); // bInterfaceSubclass (Industry Standard Command Block) + APP_USBD_CLASS_DESCRIPTOR_WRITE(p_msc->specific.inst.protocol); // bInterfaceProtocol + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iInterface + + + static uint8_t endpoints = 0; + endpoints = app_usbd_class_iface_ep_count_get(p_cur_iface); + + static uint8_t j = 0; + + for (j = 0; j < endpoints; j++) + { + // ENDPOINT DESCRIPTOR + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x07); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_ENDPOINT); // bDescriptorType = Endpoint + + static app_usbd_class_ep_conf_t const * p_cur_ep = NULL; + p_cur_ep = app_usbd_class_iface_ep_get(p_cur_iface, j); + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_ep_address_get(p_cur_ep)); // bEndpointAddress + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_BULK); // bmAttributes + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(NRF_DRV_USBD_EPSIZE)); // wMaxPacketSize MSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bInterval + } + + } + + APP_USBD_CLASS_DESCRIPTOR_END(); +} + +const app_usbd_class_methods_t app_usbd_msc_class_methods = { + .event_handler = msc_event_handler, + .feed_descriptors = msc_feed_descriptors, +}; + + +/** + * @brief Block device read done event handler. + * + * @ref NRF_BLOCK_DEV_EVT_BLK_READ_DONE + * + * @param p_blk_dev Block device handle + * @param p_event Block device event + * + * */ +static void msc_blockdev_read_done_handler(nrf_block_dev_t const * p_blk_dev, + nrf_block_dev_event_t const * p_event) +{ + ret_code_t ret; + + app_usbd_class_inst_t const * p_inst = p_event->p_context; + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + uint32_t blk_cnt = p_msc_ctx->current.blk_count; + uint32_t blk_size = p_msc_ctx->current.blk_size; + + /*Save actual request*/ + size_t req_pos = (p_msc_ctx->current.workbuff_pos != 0) ? 1 : 0; + + p_msc_ctx->current.req = *p_event->p_blk_req; + + p_msc_ctx->current.block_req_in_progress = false; + + /*Decrement transfer counter*/ + p_msc_ctx->current.blk_idx += blk_cnt; + if (p_msc_ctx->current.blk_datasize > blk_size * blk_cnt) + { + p_msc_ctx->current.blk_datasize -= blk_size * blk_cnt; + } + else + { + p_msc_ctx->current.blk_datasize = 0; + } + + NRF_LOG_DEBUG("read_done_handler: p_buff: %p, size: %u data size: %u req_pos: %u", + (uint32_t)p_event->p_blk_req->p_buff, + p_event->p_blk_req->blk_count, + p_msc_ctx->current.blk_datasize, + req_pos); + + /*Calculate next block request size*/ + p_msc_ctx->current.blk_count = next_transfer_blkcnt_calc(p_msc, p_msc_ctx); + + if (!p_msc_ctx->current.trans_in_progress) + { + /*Trigger new transfer.*/ + p_msc_ctx->current.trans_req_id = req_pos; + nrf_block_req_t * p_req = &p_msc_ctx->current.req; + + ret = transfer_in_start(p_inst, + p_req->p_buff, + p_req->blk_count * blk_size, + APP_USBD_MSC_STATE_DATA_IN); + if (ret != NRF_SUCCESS) + { + UNUSED_RETURN_VALUE(status_unsupported_start(p_inst)); + } + else + { + /*Clear processed block request.*/ + memset(p_req, 0, sizeof(nrf_block_req_t)); + p_msc_ctx->current.trans_in_progress = true; + } + + } + + if (p_msc_ctx->current.req_busy_mask == APP_USBD_MSC_REQ_BUSY_FULL_MASK) + { + /* No need to perform next block read request. USB transfers need to catch-up + * block device readings */ + return; + } + + if (p_msc_ctx->current.blk_count == 0) + { + /*All data has been read. No need to trigger new block request.*/ + return; + } + + /*Trigger new block read request*/ + p_msc_ctx->current.workbuff_pos ^= p_msc->specific.inst.block_buff_size; + req_pos = p_msc_ctx->current.workbuff_pos != 0 ? 1 : 0; + + APP_USBD_MSC_REQ_BUSY_SET(p_msc_ctx->current.req_busy_mask, req_pos); + + NRF_BLOCK_DEV_REQUEST(req, + p_msc_ctx->current.blk_idx, + p_msc_ctx->current.blk_count, + ((uint8_t *)p_msc->specific.inst.p_block_buff + + p_msc_ctx->current.workbuff_pos)); + + + NRF_LOG_DEBUG("nrf_blk_dev_read_req 2: id: %u, count: %u, left: %u, ptr: %p", + req.blk_id, + req.blk_count, + p_msc_ctx->current.blk_datasize, + (uint32_t)req.p_buff); + + ret = nrf_blk_dev_read_req(p_blk_dev, &req); + NRF_LOG_DEBUG("nrf_blk_dev_read_req 2: ret: %u", ret); + + if (ret != NRF_SUCCESS) + { + UNUSED_RETURN_VALUE(status_unsupported_start(p_inst)); + } +} + + +/** + * @brief Block device write done event handler. + * + * @ref NRF_BLOCK_DEV_EVT_BLK_WRITE_DONE + * + * @param p_blk_dev Block device handle + * @param p_event Block device event + * + * */ +static void msc_blockdev_write_done_handler(nrf_block_dev_t const * p_blk_dev, + nrf_block_dev_event_t const * p_event) +{ + app_usbd_class_inst_t const * p_inst = p_event->p_context; + app_usbd_msc_t const * p_msc = msc_get(p_inst); + app_usbd_msc_ctx_t * p_msc_ctx = msc_ctx_get(p_msc); + + uint32_t blk_cnt = p_msc_ctx->current.blk_count; + uint32_t blk_size = p_msc_ctx->current.blk_size; + + /*Save actual request*/ + size_t req_pos = (p_event->p_blk_req->p_buff == p_msc->specific.inst.p_block_buff) ? 0 : 1; + + p_msc_ctx->current.req = *p_event->p_blk_req; + + APP_USBD_MSC_REQ_BUSY_CLR(p_msc_ctx->current.req_busy_mask, req_pos); + p_msc_ctx->current.block_req_in_progress = false; + + p_msc_ctx->current.blk_idx += blk_cnt; + + if (p_msc_ctx->current.blk_datasize > blk_size * blk_cnt) + { + p_msc_ctx->current.blk_datasize -= blk_size * blk_cnt; + } + else + { + p_msc_ctx->current.blk_datasize = 0; + } + + NRF_LOG_DEBUG("write_done_handler: p_buff: %p, size: %u data size: %u req_pos: %u", + (uint32_t)p_event->p_blk_req->p_buff, + p_event->p_blk_req->blk_count, + p_msc_ctx->current.blk_datasize, + req_pos); + + if (p_msc_ctx->current.blk_datasize == 0) + { + p_msc_ctx->current.blk_idx = p_msc_ctx->current.blk_size = 0; + UNUSED_RETURN_VALUE(csw_wait_start(p_inst, p_msc_ctx->current.csw_status)); + return; + } + + if (p_msc_ctx->current.blk_datasize <= p_msc_ctx->current.blk_count * blk_size) + { + size_t pos = p_msc_ctx->current.workbuff_pos; + if (p_msc_ctx->current.req_busy_mask != APP_USBD_MSC_REQ_BUSY_FULL_MASK) + { + pos ^= p_msc->specific.inst.block_buff_size; + } + + NRF_BLOCK_DEV_REQUEST(req, + p_msc_ctx->current.blk_idx, + p_msc_ctx->current.blk_count, + ((uint8_t *)p_msc->specific.inst.p_block_buff + pos)); + + NRF_LOG_DEBUG("nrf_blk_dev_write_req 2: id: %u, count: %u, left: %u, ptr: %p", + req.blk_id, + req.blk_count, + p_msc_ctx->current.blk_datasize, + (uint32_t)req.p_buff); + + p_msc_ctx->current.block_req_in_progress = true; + ret_code_t ret = nrf_blk_dev_write_req(p_blk_dev, &req); + NRF_LOG_DEBUG("nrf_blk_dev_write_req 2: ret: %u", ret); + + if (ret != NRF_SUCCESS) + { + UNUSED_RETURN_VALUE(status_unsupported_start(p_inst)); + } + + return; + } + + if (!p_msc_ctx->current.trans_in_progress && + (p_msc_ctx->current.blk_datasize > p_msc_ctx->current.blk_count * blk_size)) + { + size_t size = p_msc_ctx->current.blk_datasize - p_msc_ctx->current.blk_count * blk_size; + if (size > p_msc_ctx->current.blk_count * blk_size) + { + size = p_msc_ctx->current.blk_count * blk_size; + } + + if (size > 0) + { + /*Trigger new transfer.*/ + p_msc_ctx->current.blk_count = next_transfer_blkcnt_calc(p_msc, p_msc_ctx); + ret_code_t ret = transfer_out_start(p_inst, + ((uint8_t *)p_msc->specific.inst.p_block_buff + + p_msc_ctx->current.workbuff_pos), + size, + APP_USBD_MSC_STATE_DATA_OUT); + if (ret == NRF_SUCCESS) + { + p_msc_ctx->current.trans_req_id ^= 1; + APP_USBD_MSC_REQ_BUSY_SET(p_msc_ctx->current.req_busy_mask, + p_msc_ctx->current.trans_req_id); + p_msc_ctx->current.workbuff_pos ^= p_msc->specific.inst.block_buff_size; + p_msc_ctx->current.trans_in_progress = true; + } + else + { + UNUSED_RETURN_VALUE(status_unsupported_start(p_inst)); + } + } + } +} + + +/** + * @brief Block device event handler. + * + * Mass storage block device event handler. Need to be pined to all block devices + * from initializer list. + * + * @param p_blk_dev Block device handle + * @param p_event Block device event + * + * */ +static void msc_blockdev_ev_handler(nrf_block_dev_t const * p_blk_dev, + nrf_block_dev_event_t const * p_event) +{ + switch (p_event->ev_type) + { + case NRF_BLOCK_DEV_EVT_INIT: + break; + + case NRF_BLOCK_DEV_EVT_UNINIT: + break; + + case NRF_BLOCK_DEV_EVT_BLK_READ_DONE: + CRITICAL_REGION_ENTER(); + msc_blockdev_read_done_handler(p_blk_dev, p_event); + CRITICAL_REGION_EXIT(); + break; + + case NRF_BLOCK_DEV_EVT_BLK_WRITE_DONE: + CRITICAL_REGION_ENTER(); + msc_blockdev_write_done_handler(p_blk_dev, p_event); + CRITICAL_REGION_EXIT(); + break; + + default: + break; + } +} + +/** @} */ + +bool app_usbd_msc_sync(app_usbd_msc_t const * p_msc) +{ + bool rc = true; + ret_code_t ret = NRF_SUCCESS; + + for (size_t i = 0; i < p_msc->specific.inst.block_devs_count; ++i) + { + nrf_block_dev_t const * p_blkd = p_msc->specific.inst.pp_block_devs[i]; + bool flush_in_progress = true; + + ret = nrf_blk_dev_ioctl(p_blkd, + NRF_BLOCK_DEV_IOCTL_REQ_CACHE_FLUSH, + &flush_in_progress); + + if ((ret != NRF_SUCCESS) || flush_in_progress) + { + rc = false; + } + } + + return rc; +} + + +#endif //NRF_MODULE_ENABLED(APP_USBD_MSC) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc.h new file mode 100644 index 0000000..b7fcb6a --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc.h @@ -0,0 +1,193 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_MSC_H__ +#define APP_USBD_MSC_H__ + +#include <stdint.h> +#include <stdbool.h> + +#include "nrf_drv_usbd.h" +#include "nrf_block_dev.h" +#include "app_usbd_class_base.h" +#include "app_usbd.h" +#include "app_usbd_core.h" +#include "app_usbd_descriptor.h" + +#include "app_usbd_msc_types.h" +#include "app_usbd_msc_desc.h" +#include "app_usbd_msc_scsi.h" +#include "app_usbd_msc_internal.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_msc USB MSC class + * @ingroup app_usbd + * + * @brief @tagAPI52840 Module with types, definitions, and API used by the USB MSC class. + * + * @details References: + * - "Universal Serial Bus Mass Storage Class, Specification Overview," + * Revision 1.2, USB Implementer's Forum, June 23, 2003. + * - "Universal Serial Bus Mass Storage Class, Bulk-Only Transport," + * Revision 1.0, USB Implementer's Forum, September 31, 1999. + * + * @{ + */ + +#ifdef DOXYGEN +/** + * @brief Mass storage class instance type + * + * @ref APP_USBD_CLASS_TYPEDEF + */ +typedef struct { } app_usbd_msc_t; +#else +/*lint -save -e10 -e26 -e123 -e505 */ +APP_USBD_CLASS_TYPEDEF(app_usbd_msc, \ + APP_USBD_MSC_CONFIG(0, (NRF_DRV_USBD_EPIN1, NRF_DRV_USBD_EPOUT1)), \ + APP_USBD_MSC_INSTANCE_SPECIFIC_DEC, \ + APP_USBD_MSC_DATA_SPECIFIC_DEC \ +); +#endif + +/*lint -restore*/ + + +/*lint -save -e407 */ + +/** + * @brief Events passed to user event handler + * + * @note Example prototype of user event handler: + * + * void msc_user_ev_handler(app_usbd_class_inst_t const * p_inst, + * app_usbd_msc_user_event_t event); + */ +typedef enum app_usbd_msc_user_event_e { + APP_USBD_MSC_USER_EVT_NONE, /**< Dummy event to satisfy compilers. */ +} app_usbd_msc_user_event_t; + +/*lint -restore*/ + +/** + * @brief Helper macro for defining MSC endpoints + * + * @param in_number Input endpoint number + * @param out_number Output endpoint number + * */ +#define APP_USBD_MSC_ENDPOINT_LIST(in_number, out_number) ( \ + CONCAT_2(NRF_DRV_USBD_EPIN, in_number), \ + CONCAT_2(NRF_DRV_USBD_EPOUT, out_number) \ +) + +/** + * @brief Global definition of app_usbd_msc_t class + * + * @param instance_name Name of global instance + * @param interface_number Unique interface number + * @param user_ev_handler User event handler (optional) + * @param endpoint_list Input endpoint list (@ref nrf_drv_usbd_ep_t) + * @param blockdev_list Block device list + * @param workbuffer_size Work buffer size (need to fit into all block devices from + * block device list) + * + * @note This macro is just simplified version of @ref APP_USBD_MSC_GLOBAL_DEF_INTERNAL + * + */ +#define APP_USBD_MSC_GLOBAL_DEF(instance_name, \ + interface_number, \ + user_ev_handler, \ + endpoint_list, \ + blockdev_list, \ + workbuffer_size) \ + APP_USBD_MSC_GLOBAL_DEF_INTERNAL(instance_name, \ + interface_number, \ + user_ev_handler, \ + endpoint_list, \ + blockdev_list, \ + workbuffer_size) + + +/** + * @@brief Helper function to get class instance from MSC + * + * @param[in] p_msc MSC instance (declared by @ref APP_USBD_MSC_GLOBAL_DEF) + * + * @return Base class instance + */ +static inline app_usbd_class_inst_t const * +app_usbd_msc_class_inst_get(app_usbd_msc_t const * p_msc) +{ + return &p_msc->base; +} + +/** + * @brief Helper function to get MSC from base class instance + * + * @param[in] p_inst Base class instance + * + * @return MSC class handle + */ +static inline app_usbd_msc_t const * app_usbd_msc_class_get(app_usbd_class_inst_t const * p_inst) +{ + return (app_usbd_msc_t const *)p_inst; +} + +/** + * @brief Synchronization of all block devices pined to MSC + * + * @param[in] p_msc MSC instance (declared by @ref APP_USBD_MSC_GLOBAL_DEF) + * + * @retval true All block devices flushed data + * @retval false At least one block device has not flushed data + */ +bool app_usbd_msc_sync(app_usbd_msc_t const * p_msc); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_MSC_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_desc.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_desc.h new file mode 100644 index 0000000..83c3916 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_desc.h @@ -0,0 +1,104 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_MSC_DESC_H__ +#define APP_USBD_MSC_DESC_H__ + +#include "app_usbd_descriptor.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_msc_desc USB MSC descriptors + * @ingroup app_usbd_msc + * + * @brief @tagAPI52840 Descriptors for the USB MSC class. + * @{ + */ + +/** + * @brief Initializer of interface descriptor for MSC class + * + * @param interface_number Interface number + * @param subclass Subclass, @ref app_usbd_msc_subclass_t + * @param protocol Protocol, @ref app_usbd_msc_protocol_t + * */ +#define APP_USBD_MSC_INTERFACE_DSC(interface_number, subclass, protocol) \ + /*.bLength = */ sizeof(app_usbd_descriptor_iface_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_INTERFACE, \ + /*.bInterfaceNumber = */ interface_number, \ + /*.bAlternateSetting = */ 0x00, \ + /*.bNumEndpoints = */ 2, \ + /*.bInterfaceClass = */ APP_USBD_MSC_CLASS, \ + /*.bInterfaceSubClass = */ subclass, \ + /*.bInterfaceProtocol = */ protocol, \ + /*.iInterface = 0, */ 0x00, \ + + +/** + * @brief Initializer of endpoint descriptors for MSC class + * + * @param endpoint_in IN endpoint + * @param endpoint_out OUT endpoint + * @param ep_size Endpoint size + * */ +#define APP_USBD_MSC_EP_DSC(endpoint_in, endpoint_out, ep_size) \ + /*.bLength = */ sizeof(app_usbd_descriptor_ep_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_ENDPOINT, \ + /*.bEndpointAddress = */ endpoint_in, \ + /*.bmAttributes = */ APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_BULK, \ + /*.wMaxPacketSize = */ APP_USBD_U16_TO_RAW_DSC(ep_size), \ + /*.bInterval = */ 0, \ + /*.bLength = */ sizeof(app_usbd_descriptor_ep_t), \ + /*.bDescriptorType = */ APP_USBD_DESCRIPTOR_ENDPOINT, \ + /*.bEndpointAddress = */ endpoint_out, \ + /*.bmAttributes = */ APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_BULK, \ + /*.wMaxPacketSize = */ APP_USBD_U16_TO_RAW_DSC(ep_size), \ + /*.bInterval = */ 0, \ + + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_MSC_DESC_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_internal.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_internal.h new file mode 100644 index 0000000..c7036fa --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_internal.h @@ -0,0 +1,259 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_MSC_INTERNAL_H__ +#define APP_USBD_MSC_INTERNAL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_msc_internals USB MSC internals + * @ingroup app_usbd_msc + * + * @brief @tagAPI52840 Internals of the USB MSC class. + * @{ + */ + +/** + * @brief Minimal serial string descriptor length + * */ +#define APP_USBD_MSC_MINIMAL_SERIAL_STRING_SIZE (12 + 1) + +/** + * @brief Forward declaration of Mass Storage Class type + * + */ +APP_USBD_CLASS_FORWARD(app_usbd_msc); + +/*lint -save -e165*/ +/** + * @brief Forward declaration of @ref app_usbd_msc_user_event_e + * + */ +enum app_usbd_msc_user_event_e; + +/*lint -restore*/ + +/** + * @brief User event handler + * + * @param[in] p_inst Class instance + * @param[in] event User event + * + * */ +typedef void (*app_usbd_msc_user_ev_handler_t)(app_usbd_class_inst_t const * p_inst, + enum app_usbd_msc_user_event_e event); + +/** + * @brief MSC part of class instance data + */ +typedef struct { + void * p_block_buff; //!< Block buffer + size_t block_buff_size; //!< Block buffer size (typically 512 bytes) + + nrf_block_dev_t const ** pp_block_devs; //!< Block devices list + size_t block_devs_count; //!< Block device list size + + app_usbd_msc_user_ev_handler_t user_ev_handler; //!< User event handler + + app_usbd_msc_subclass_t subclass; //!< MSC subclass + app_usbd_msc_protocol_t protocol; //!< MSC protocol +} app_usbd_msc_inst_t; + +/** + * @brief Internal module state + */ +typedef enum { + APP_USBD_MSC_STATE_DISABLED, /**< Internal module state DISABLED */ + APP_USBD_MSC_STATE_IDLE, /**< Internal module state IDLE */ + APP_USBD_MSC_STATE_CBW, /**< Internal module state CBW */ + APP_USBD_MSC_STATE_CMD_IN, /**< Internal module state CMD_IN */ + APP_USBD_MSC_STATE_CMD_OUT, /**< Internal module state CMD_OUT */ + APP_USBD_MSC_STATE_DATA_IN, /**< Internal module state DATA_IN */ + APP_USBD_MSC_STATE_DATA_OUT, /**< Internal module state DATA_OUT */ + APP_USBD_MSC_STATE_DATA_OUT_WAIT, /**< Internal module state DATA_OUT_WAIT */ + APP_USBD_MSC_STATE_CSW, /**< Internal module state CSW */ + APP_USBD_MSC_STATE_UNSUPPORTED, /**< Internal module state UNSUPPORTED */ + APP_USBD_MSC_STATE_CBW_INVALID, /**< Endpoint is stalled until + * the command @ref APP_USBD_MSC_REQ_BULK_RESET */ + APP_USBD_MSC_STATE_DEVICE_ERROR, /**< Endpoint is stalled and it is required + * to send PE error when clearing */ +} app_usbd_msc_state_t; + +/** + * @brief MSC context + * + * */ +typedef struct { + app_usbd_msc_cbw_t cbw; //!< SCSI command block wrapper + app_usbd_msc_csw_t csw; //!< SCSI Command status wrapper + + app_usbd_msc_state_t state; //!< Internal module state + + struct { + uint8_t lun; //!< Current transfer blocks: logical unit + uint8_t csw_status; //!< Current CSW status + uint32_t blk_idx; //!< Current transfer: block index + uint32_t blk_datasize; //!< Current transfer: data size to transfer + uint32_t blk_size; //!< Current transfer: block size + uint32_t blk_count; //!< Current transfer: block count + uint32_t residue; //!< @ref app_usbd_msc_csw_t::residue + + bool trans_in_progress; //!< Transfer in progress flag + bool block_req_in_progress; //!< Block request in progress flag + size_t workbuff_pos; //!< Current buffer offset (double buffering mode) + uint8_t req_busy_mask; //!< Request bust mask (double buffering mode) + uint8_t trans_req_id; //!< Current transfered request (double buffering mode) + + nrf_block_req_t req; //!< Last processed block req (double buffering mode) + } current; + + size_t resp_len; //!< Response length + + /*SCSI response container*/ + union { + app_usbd_scsi_cmd_inquiry_resp_t inquiry; //!< @ref APP_USBD_SCSI_CMD_INQUIRY response + app_usbd_scsi_cmd_requestsense_resp_t requestsense; //!< @ref APP_USBD_SCSI_CMD_REQUESTSENSE response + app_usbd_scsi_cmd_readcapacity10_resp_t readcapacity10; //!< @ref APP_USBD_SCSI_CMD_READCAPACITY10 response + app_usbd_scsi_cmd_modesense6_resp_t modesense6; //!< @ref APP_USBD_SCSI_CMD_MODESENSE6 response + app_usbd_scsi_cmd_modesense10_resp_t modesense10; //!< @ref APP_USBD_SCSI_CMD_MODESENSE10 response + } scsi_resp; + + uint16_t blk_dev_init_mask; //!< Block devices init mask +} app_usbd_msc_ctx_t; + + +/** + * @brief MSC configuration macro + * + * Used by @ref APP_USBD_MSC_GLOBAL_DEF + * + * @param iface Interface number + * @param endpoints Endpoint list + * */ +#define APP_USBD_MSC_CONFIG(iface, endpoints) ((iface, BRACKET_EXTRACT(endpoints))) + + +/** + * @brief Specific class constant data for MSC + * + * @ref app_usbd_msc_inst_t + */ +#define APP_USBD_MSC_INSTANCE_SPECIFIC_DEC app_usbd_msc_inst_t inst; + +/** + * @brief Configures MSC instance + * + * @param block_devs Block devices list + * @param block_buff Block buffer + * @param user_event_handler User event handler + */ +#define APP_USBD_MSC_INST_CONFIG(block_devs, block_buff, user_event_handler) \ + .inst = { \ + .pp_block_devs = block_devs, \ + .block_devs_count = ARRAY_SIZE(block_devs), \ + .p_block_buff = block_buff, \ + .block_buff_size = sizeof(block_buff) / 2, \ + .user_ev_handler = user_event_handler, \ + .subclass = APP_USBD_MSC_SUBCLASS_TRANSPARENT, \ + .protocol = APP_USBD_MSC_PROTOCOL_BULK, \ + } + +/** + * @brief Specific class data for MSC + * + * @ref app_usbd_msc_ctx_t + * */ +#define APP_USBD_MSC_DATA_SPECIFIC_DEC app_usbd_msc_ctx_t ctx; + + +/** + * @brief MSC descriptors config macro + * + * @param interface_number Interface number + * @param ... Extracted endpoint list + * */ +#define APP_USBD_MSC_DSC_CONFIG(interface_number, ...) { \ + APP_USBD_MSC_INTERFACE_DSC(interface_number, \ + APP_USBD_MSC_SUBCLASS_TRANSPARENT, \ + APP_USBD_MSC_PROTOCOL_BULK) \ + APP_USBD_MSC_EP_DSC(GET_VA_ARG_1(__VA_ARGS__), \ + GET_VA_ARG_1(GET_ARGS_AFTER_1(__VA_ARGS__)), \ + 64) \ +} + +/** + * @brief Public MSC class interface + * + * */ +extern const app_usbd_class_methods_t app_usbd_msc_class_methods; + +/** + * @brief Global definition of mass storage class instance + */ +#define APP_USBD_MSC_GLOBAL_DEF_INTERNAL(instance_name, \ + interface_number, \ + user_ev_handler, \ + endpoint_list, \ + blockdev_list, \ + workbuffer_size) \ + static const nrf_block_dev_t * CONCAT_2(instance_name, _blkdevs)[] = \ + { BRACKET_EXTRACT(blockdev_list) }; \ + static uint32_t CONCAT_2(instance_name, _block)[2 *(workbuffer_size) / sizeof(uint32_t)]; \ + APP_USBD_CLASS_INST_GLOBAL_DEF( \ + instance_name, \ + app_usbd_msc, \ + &app_usbd_msc_class_methods, \ + APP_USBD_MSC_CONFIG(interface_number, endpoint_list), \ + (APP_USBD_MSC_INST_CONFIG(CONCAT_2(instance_name, _blkdevs), \ + CONCAT_2(instance_name, _block), \ + user_ev_handler)) \ + ) + + +/** @} */ + + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_MSC_INTERNAL_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_scsi.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_scsi.h new file mode 100644 index 0000000..8379a8c --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_scsi.h @@ -0,0 +1,329 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_MSC_SCSI_H__ +#define APP_USBD_MSC_SCSI_H__ + +#include "app_util.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_msc_scsi USB MSC SCSI data structures + * @ingroup app_usbd_msc + * + * @brief @tagAPI52840 USB MSC SCSI data structures. + * + * @details Reference specifications: + * - "Reduced Block Commands (Revision 10a)" American National Standard + * for Information Technology, August 18, 1999 + * - "SCSI Primary Commands - 4 (SPC-4)," American National Standard + * for Information Technology, July 19, 2008 + * - "SCSI Block Commands -2 (SBC-2)," American National Standard + * for Information Technology, November 13, 2004 + * - NuttX source code - Real-time Operating System: http://nuttx.org/ + * Gregory Nutt <gnutt@nuttx.org> + * + * @{ + */ + +/** + * @brief SCSI command set + * + * Mandatory (and some optional) commands required by SBC-2. + * + */ +typedef enum { + APP_USBD_SCSI_CMD_TESTUNITREADY = 0x00, /**< TESTUNITREADY */ + APP_USBD_SCSI_CMD_REQUESTSENSE = 0x03, /**< REQUESTSENSE */ + APP_USBD_SCSI_CMD_FORMAT_UNIT = 0x04, /**< FORMAT_UNIT */ + APP_USBD_SCSI_CMD_READ6 = 0x08, /**< READ6 */ + APP_USBD_SCSI_CMD_WRITE6 = 0x0a, /**< WRITE6 */ + APP_USBD_SCSI_CMD_INQUIRY = 0x12, /**< INQUIRY */ + APP_USBD_SCSI_CMD_MODESELECT6 = 0x15, /**< MODESELECT6 */ + APP_USBD_SCSI_CMD_MODESENSE6 = 0x1a, /**< MODESENSE6 */ + APP_USBD_SCSI_CMD_STARTSTOPUNIT = 0x1b, /**< STARTSTOPUNIT */ + APP_USBD_SCSI_CMD_SENDDIAGNOSTIC = 0x1d, /**< SENDDIAGNOSTIC */ + APP_USBD_SCSI_CMD_PREVENTMEDIAREMOVAL = 0x1e, /**< PREVENTMEDIAREMOVAL */ + APP_USBD_SCSI_CMD_READCAPACITY10 = 0x25, /**< READCAPACITY10 */ + APP_USBD_SCSI_CMD_READ10 = 0x28, /**< READ10 */ + APP_USBD_SCSI_CMD_WRITE10 = 0x2a, /**< WRITE10 */ + APP_USBD_SCSI_CMD_MODESELECT10 = 0x55, /**< MODESELECT10 */ + APP_USBD_SCSI_CMD_MODESENSE10 = 0x5a, /**< MODESENSE10 */ +} app_usbd_scsi_cmd_t; + + +#pragma pack(push, 1) + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_REQUESTSENSE command + */ +typedef struct { + uint8_t opcode; //!< @ref APP_USBD_SCSI_CMD_REQUESTSENSE + uint8_t flags; //!< Flags + uint8_t reserved[2]; //!< Reserved + uint8_t alloclen; //!< Allocation length + uint8_t control; //!< Control +} app_usbd_scsi_cmd_requestsense_t; + +#define APP_USBD_SCSI_CMD_REQSENSE_CODE_VALID 0x80 /**< @ref app_usbd_scsi_cmd_requestsense_resp_t::code */ +#define APP_USBD_SCSI_CMD_REQSENSE_CODE_CURRENT 0x70 /**< @ref app_usbd_scsi_cmd_requestsense_resp_t::code */ +#define APP_USBD_SCSI_CMD_REQSENSE_CODE_DEFERRED 0x71 /**< @ref app_usbd_scsi_cmd_requestsense_resp_t::code */ +#define APP_USBD_SCSI_CMD_REQSENSE_CODE_CURRENTDESC 0x72 /**< @ref app_usbd_scsi_cmd_requestsense_resp_t::code */ +#define APP_USBD_SCSI_CMD_REQSENSE_CODE_DEFERREDDESC 0x73 /**< @ref app_usbd_scsi_cmd_requestsense_resp_t::code */ + +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_FILEMARK 0x80 /**< Bits 7 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_EOM 0x40 /**< Bits 6 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_ILI 0x20 /**< Bits 5 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_RESERVED 0x10 /**< Bits 4 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ + +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_NOSENSE 0x00 /**< Bits 3...0 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_RECOVEREDERROR 0x01 /**< Bits 3...0 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_NOTREADY 0x02 /**< Bits 3...0 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_MEDIUMERROR 0x03 /**< Bits 3...0 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_HARDWAREERROR 0x04 /**< Bits 3...0 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_ILLEGALREQUEST 0x05 /**< Bits 3...0 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_UNITATTENTION 0x06 /**< Bits 3...0 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_DATAPROTECT 0x07 /**< Bits 3...0 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_BLANKCHECK 0x08 /**< Bits 3...0 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_VENDORSPECIFIC 0x09 /**< Bits 3...0 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ +#define APP_USBD_SCSI_CMD_REQSENSE_FLAG_ABORTEDCOMMAND 0x0b /**< Bits 3...0 @ref app_usbd_scsi_cmd_requestsense_resp_t::flags */ + + +#define APP_USBD_SCSI_CMD_TESTUNITREADY_LEN 6 /**< @ref APP_USBD_SCSI_CMD_TESTUNITREADY command length*/ + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_REQUESTSENSE response + */ +typedef struct { + uint8_t code; //!< Response code: APP_USBD_SCSI_CMD_REQSENSE_CODE_* + uint8_t obsolete; //!< Obsolete + uint8_t flags; //!< APP_USBD_SCSI_CMD_REQSENSE_FLAG_* + uint8_t info[4]; //!< Information + uint8_t len; //!< Additional length + uint8_t cmdinfo[4]; //!< Command-specific information + uint8_t code2; //!< Additional sense code + uint8_t qual2; //!< Additional sense code qualifier + uint8_t fru; //!< Field replacement unit code + uint8_t key[3]; //!< Sense key specific +} app_usbd_scsi_cmd_requestsense_resp_t; + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_INQUIRY command + */ +typedef struct { + uint8_t opcode; //!< @ref APP_USBD_SCSI_CMD_INQUIRY + uint8_t flags; //!< Command flags + uint8_t pagecode; //!< Page code + uint8_t alloclen[2]; //!< Allocation length + uint8_t control; //!< Control +} app_usbd_scsi_cmd_inquiry_t; + +#define APP_USBD_MSC_SCSI_INQ_QUAL_CONNECTED 0x00 /**< Peripheral connected */ +#define APP_USBD_MSC_SCSI_INQ_QUAL_NOT_CONN 0x20 /**< Peripheral not connected */ +#define APP_USBD_MSC_SCSI_INQ_QUAL_NOT_SUPP 0x60 /**< Peripheral not supported */ + +#define APP_USBD_MSC_SCSI_INQ_TYPE_DIR_ACCESS 0x00 /**< Direct Access (SBC) */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_SEQ_ACCESS 0x01 /**< Sequential Access */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_PRINTER 0x02 /**< Printer */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_PROCESSOR 0x03 /**< Processor device */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_WRITE_ONCE 0x04 /**< Write-once device */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_CD_DVD 0x05 /**< CD/DVD device */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_OPTICAL 0x07 /**< Optical Memory */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_MC 0x08 /**< Medium Changer */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_ARRAY 0x0c /**< Storage Array Controller */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_ENCLOSURE 0x0d /**< Enclosure Services */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_RBC 0x0e /**< Simplified Direct Access */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_OCRW 0x0f /**< Optical card reader/writer */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_BCC 0x10 /**< Bridge Controller Commands */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_OSD 0x11 /**< Object-based Storage */ +#define APP_USBD_MSC_SCSI_INQ_TYPE_NONE 0x1f /**< No Peripheral */ + + +#define APP_USBD_MSC_SCSI_INQ_FLAG1_RMB 0x80 /**< Removable Medium */ + +#define APP_USBD_SCSI_INQ_VER_NONE 0x00 /**< No standards conformance */ +#define APP_USBD_SCSI_INQ_VER_SPC 0x03 /**< SCSI Primary Commands (link to SBC) */ +#define APP_USBD_SCSI_INQ_VER_SPC2 0x04 /**< SCSI Primary Commands - 2 (link to SBC-2)*/ +#define APP_USBD_SCSI_INQ_VER_SPC3 0x05 /**< SCSI Primary Commands - 3 (link to SBC-2)*/ +#define APP_USBD_SCSI_INQ_VER_SPC4 0x06 /**< SCSI Primary Commands - 4 (link to SBC-3)*/ + +#define APP_USBD_MSC_SCSI_INQ_FLAG2_NORMACA 0x20 /**< Normal ACA Supported */ +#define APP_USBD_MSC_SCSI_INQ_FLAG2_HISUP 0x10 /**< Hierarchal LUN addressing */ +#define APP_USBD_MSC_SCSI_INQ_FLAG2_RSP_SPC2 0x02 /**< SPC-2 / SPC-3 response format*/ +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_INQUIRY response + */ +typedef struct { + uint8_t qualtype; //!< Bits 5-7: Peripheral qualifier; Bits 0-4: Peripheral device type + uint8_t flags1; //!< Flags 1 + uint8_t version; //!< Version + uint8_t flags2; //!< Flags 2 + uint8_t len; //!< Additional length + uint8_t flags3; //!< Flags 3 + uint8_t flags4; //!< Flags 4 + uint8_t flags5; //!< Flags 5 + uint8_t vendorid[8]; //!< T10 Vendor Identification + uint8_t productid[16]; //!< Product Identification + uint8_t revision[4]; //!< Product Revision Level +} app_usbd_scsi_cmd_inquiry_resp_t; + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_READ6 command + */ +typedef struct { + uint8_t opcode; //!< @ref APP_USBD_SCSI_CMD_READ6 + uint8_t mslba; //!< Bits 5-7: reserved; Bits 0-6: MS Logical Block Address (LBA) + uint8_t lslba[2]; //!< LS Logical Block Address (LBA) + uint8_t xfrlen; //!< Transfer length (in contiguous logical blocks) + uint8_t control; //!< Control +} app_usbd_scsi_cmd_read6_t; + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_WRITE6 command + */ +typedef struct { + uint8_t opcode; //!< @ref APP_USBD_SCSI_CMD_WRITE6 + uint8_t mslba; //!< Bits 5-7: reserved; Bits 0-6: MS Logical Block Address (LBA) + uint8_t lslba[2]; //!< LS Logical Block Address (LBA) + uint8_t xfrlen; //!< Transfer length (in contiguous logical blocks) + uint8_t control; //!< Control +} app_usbd_scsi_cmd_write6_t; + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_MODESENSE6 command + */ +typedef struct { + uint8_t opcode; //!<* @ref APP_USBD_SCSI_CMD_MODESENSE6 + uint8_t flags; //!<* Flags + uint8_t pcpgcode; //!<* Bits 6-7: PC, bits 0-5: page code + uint8_t subpgcode; //!<* subpage code + uint8_t alloclen; //!<* Allocation length + uint8_t control; //!<* Control +} app_usbd_scsi_cmd_modesense6_t; + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_MODESENSE6 response + */ +typedef struct { + uint8_t mdlen; //!< Mode data length + uint8_t type; //!< Medium type + uint8_t param; //!< Device-specific parameter + uint8_t bdlen; //!< Block descriptor length +} app_usbd_scsi_cmd_modesense6_resp_t; + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_READCAPACITY10 command + */ +typedef struct { + uint8_t opcode; //!< @ref APP_USBD_SCSI_CMD_READCAPACITY10 + uint8_t reserved1; //!< Reserved field + uint8_t lba[4]; //!< Logical block address (LBA) + uint8_t reserved2[2]; //!< Reserved field + uint8_t pmi; //!< Bits 1-7 Reserved; Bit 0: PMI + uint8_t control; //!< Control +} app_usbd_scsi_cmd_readcapacity10_t; + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_READCAPACITY10 response + */ +typedef struct { + uint8_t lba[4]; //!< Returned logical block address (LBA) + uint8_t blklen[4]; //!< Logical block length (in bytes) +} app_usbd_scsi_cmd_readcapacity10_resp_t; + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_READ10 command + */ +typedef struct { + uint8_t opcode; //!< @ref APP_USBD_SCSI_CMD_READ10 + uint8_t flags; //!< Command flags + uint8_t lba[4]; //!< Logical Block Address (LBA) + uint8_t groupno; //!< Bits 5-7: reserved; Bits 0-6: group number + uint8_t xfrlen[2]; //!< Transfer length (in contiguous logical blocks) + uint8_t control; //!< Control +} app_usbd_scsi_cmd_read10_t; + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_WRITE10 command + */ +typedef struct { + uint8_t opcode; //!< @ref APP_USBD_SCSI_CMD_WRITE10 + uint8_t flags; //!< Command flags + uint8_t lba[4]; //!< Logical Block Address (LBA) + uint8_t groupno; //!< Bits 5-7: reserved; Bits 0-6: group number + uint8_t xfrlen[2]; //!< Transfer length (in contiguous logical blocks) + uint8_t control; //!< Control +} app_usbd_scsi_cmd_write10_t; + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_MODESENSE10 command + */ +typedef struct { + uint8_t opcode; //!< @ref APP_USBD_SCSI_CMD_MODESENSE10 + uint8_t flags; //!< Flags + uint8_t pcpgcode; //!< Bits 6-7: PC, bits 0-5: page code + uint8_t subpgcode; //!< Subpage code + uint8_t reserved[3]; //!< Reserved + uint8_t alloclen[2]; //!< Allocation length + uint8_t control; //!< Control +} app_usbd_scsi_cmd_modesense10_t; + +/** + * @brief Payload of @ref APP_USBD_SCSI_CMD_MODESENSE10 response + */ +typedef struct { + uint8_t mdlen[2]; //!< Mode data length + uint8_t type; //!< Medium type + uint8_t param; //!< Device-specific parameter + uint8_t reserved[2]; //!< Reserved + uint8_t bdlen[2]; //!< Block descriptor length +} app_usbd_scsi_cmd_modesense10_resp_t; + +#pragma pack(pop) + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_MSC_SCSI_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_types.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_types.h new file mode 100644 index 0000000..96a6431 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/msc/app_usbd_msc_types.h @@ -0,0 +1,141 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_MSC_TYPES_H__ +#define APP_USBD_MSC_TYPES_H__ + +#include "app_util.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_msc_types USB MSC types + * @ingroup app_usbd_msc + * + * @brief @tagAPI52840 Types used in the USB MSC class. + * @{ + */ + +/** @brief MSC class definition in interface descriptor + * + * @ref app_usbd_descriptor_iface_t::bInterfaceClass + * */ +#define APP_USBD_MSC_CLASS 0x08 + +/** + * @brief MSC subclass possible value + */ +typedef enum { + APP_USBD_MSC_SUBCLASS_RBC = 0x01, /**< Reduced Block Commands */ + APP_USBD_MSC_SUBCLASS_ATAPI = 0x02, /**< CD/DVD devices */ + APP_USBD_MSC_SUBCLASS_QIC_157 = 0x03, /**< Tape devices */ + APP_USBD_MSC_SUBCLASS_UFI = 0x04, /**< Floppy disk drives */ + APP_USBD_MSC_SUBCLASS_SFF_8070I = 0x05, /**< Floppy disk drives */ + APP_USBD_MSC_SUBCLASS_TRANSPARENT = 0x06, /**< Determined by INQUIRY */ +} app_usbd_msc_subclass_t; + +/** + * @brief MSC protocol possible value + * + * @note The USB Mass Storage Class Control/Bulk/Interrupt (CBI) Transport specification is approved + * for use only with full-speed floppy disk drives. CBI shall not be used in high-speed + * capable devices. + */ +typedef enum { + APP_USBD_MSC_PROTOCOL_CBI = 0x00, /**< Command/Bulk/Interrupt */ + APP_USBD_MSC_PROTOCOL_CBI_ALT = 0x01, /**< W/o command completion */ + APP_USBD_MSC_PROTOCOL_BULK = 0x50, /**< Bulk-only */ +} app_usbd_msc_protocol_t; + +/** + * @brief MSC USB requests @ref nrf_drv_usbd_setup_t::bmRequestType + * + * @note Requests are limited only to @ref APP_USBD_MSC_PROTOCOL_BULK protocol type. + */ +typedef enum { + APP_USBD_MSC_REQ_BULK_RESET = 0xFF, /**< Mass Storage Reset */ + APP_USBD_MSC_REQ_GET_MAX_LUN = 0xFE, /**< Get Max LUN */ +} app_usbd_msc_req_t; + +#pragma pack(push, 1) + +#define APP_USBD_MSC_CBW_SIGNATURE "USBC" /**< CBW signature */ +#define APP_USBD_MSC_CBW_DIRECTION_IN (1u <<7) /**< CBW direction flag */ + +/** + * @brief Command Block Wrapper (CBW) + */ +typedef struct { + uint8_t signature[4]; /**< "USBC" (hex: 0x43425355 little-endian) */ + uint8_t tag[4]; /**< Unique command tag */ + uint8_t datlen[4]; /**< Number of bytes that host expects to transfer */ + uint8_t flags; /**< Bit 7: Direction=IN */ + uint8_t lun; /**< Logical Unit Number, equals to @ref app_usbd_msc_inst_t :: block_devs_count*/ + uint8_t cdb_length; /**< Length of cdb field */ + uint8_t cdb[16]; /**< Command Data Block payload */ +} app_usbd_msc_cbw_t; + +#define APP_USBD_MSC_CSW_SIGNATURE "USBS" /**< CSW signature */ + +#define APP_USBD_MSC_CSW_STATUS_PASS 0x00 /**< CSW status: Command Passed */ +#define APP_USBD_MSC_CSW_STATUS_FAIL 0x01 /**< CSW status: Command Failed */ +#define APP_USBD_MSC_CSW_STATUS_PE 0x02 /**< CSW status: Phase Error */ + +/** + * @brief Command Status Wrapper (CSW) + */ +typedef struct { + uint8_t signature[4]; /**< "USBS" (hex: 0x53425355 little-endian) */ + uint8_t tag[4]; /**< Unique command tag (@ref app_usbd_msc_cbw_t :: tag) */ + uint8_t residue[4]; /**< Amount not transferred */ + uint8_t status; /**< Status of transfer */ +} app_usbd_msc_csw_t; + +#pragma pack(pop) + + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_MSC_TYPES_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger.c new file mode 100644 index 0000000..3716ec0 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger.c @@ -0,0 +1,374 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(APP_USBD_NRF_DFU_TRIGGER) + +#include <string.h> +#include <ctype.h> +#include "app_usbd.h" +#include "app_usbd_nrf_dfu_trigger.h" +#include "app_usbd_string_desc.h" +#include "nrf_gpio.h" + +/** + * @defgroup app_usbd_nrf_dfu_trigger_internal USBD Nordic DFU Trigger internals + * @{ + * @ingroup app_usbd_nrf_dfu_trigger + * @internal + */ + +#define NRF_LOG_MODULE_NAME usbd_nrf_dfu_trigger + +#if APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_LEVEL +#define NRF_LOG_INFO_COLOR APP_USBD_NRF_DFU_TRIGGER_CONFIG_INFO_COLOR +#define NRF_LOG_DEBUG_COLOR APP_USBD_NRF_DFU_TRIGGER_CONFIG_DEBUG_COLOR +#else //APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_ENABLED +#define NRF_LOG_LEVEL 0 +#endif //APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_ENABLED +#include "nrf_log.h" +NRF_LOG_MODULE_REGISTER(); + +/** + * @brief Auxiliary function to access DFU Trigger instance data + * + * @param[in] p_inst Class instance data + * + * @return DFU Trigger instance data @ref app_usbd_nrf_dfu_trigger_t + */ +static inline app_usbd_nrf_dfu_trigger_t const * nrf_dfu_trigger_get(app_usbd_class_inst_t const * p_inst) +{ + ASSERT(p_inst != NULL); + return (app_usbd_nrf_dfu_trigger_t const *)p_inst; +} + +/** + * @brief Auxiliary function to access DFU Trigger context data + * + * @param[in] p_dfu DFU Trigger instance data + * + * @return DFU Trigger context data @ref app_usbd_nrf_dfu_trigger_ctx_t + */ +static inline app_usbd_nrf_dfu_trigger_ctx_t * nrf_dfu_trigger_ctx_get( + app_usbd_nrf_dfu_trigger_t const * p_dfu) +{ + ASSERT(p_dfu != NULL); + ASSERT(p_dfu->specific.p_data != NULL); + return &p_dfu->specific.p_data->ctx; +} + +/** + * @brief Internal SETUP standard IN request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_req_std_in(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + /* Only Get Descriptor standard IN request is supported by DFU class */ + if ((app_usbd_setup_req_rec(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQREC_INTERFACE) + && + (p_setup_ev->setup.bmRequest == APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR)) + { + size_t dsc_len = 0; + size_t max_size; + + uint8_t * p_trans_buff = app_usbd_core_setup_transfer_buff_get(&max_size); + /* Try to find descriptor in class internals*/ + ret_code_t ret = app_usbd_class_descriptor_find(p_inst, + p_setup_ev->setup.wValue.hb, + p_setup_ev->setup.wValue.lb, + p_trans_buff, + &dsc_len); + if (ret != NRF_ERROR_NOT_FOUND) + { + ASSERT(dsc_len < NRF_DRV_USBD_EPSIZE); + return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_trans_buff, dsc_len); + } + } + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Internal SETUP standard OUT request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + * @retval NRF_ERROR_FORBIDDEN if endpoint stall cannot be cleared because of internal state + */ +static ret_code_t setup_req_std_out(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + + switch (p_setup_ev->setup.bmRequest) + { + default: + break; + } + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief User event handler. + * + * @param[in] p_inst Class instance. + * @param[in] event user Event type. + */ +static inline void user_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_nrf_dfu_trigger_user_event_t event) +{ + app_usbd_nrf_dfu_trigger_t const * p_dfu = nrf_dfu_trigger_get(p_inst); + if (p_dfu->specific.inst.user_ev_handler != NULL) + { + p_dfu->specific.inst.user_ev_handler(p_inst, event); + } +} + +/** + * @brief Internal SETUP class IN request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_req_class_in(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_nrf_dfu_trigger_t const * p_dfu = nrf_dfu_trigger_get(p_inst); + + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_NRF_DFU_TRIGGER_REQ_NORDIC_INFO: + if (p_setup_ev->setup.wLength.w != sizeof(app_usbd_nrf_dfu_trigger_nordic_info_t)) + { + return NRF_ERROR_NOT_SUPPORTED; + } + return app_usbd_core_setup_rsp(&p_setup_ev->setup, + p_dfu->specific.inst.p_dfu_info, + sizeof(app_usbd_nrf_dfu_trigger_nordic_info_t)); + + case APP_USBD_NRF_DFU_TRIGGER_REQ_SEM_VER: + return app_usbd_core_setup_rsp(&p_setup_ev->setup, + p_dfu->specific.inst.p_sem_ver, + p_dfu->specific.inst.sem_ver_size); + + default: + break; + } + + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Internal SETUP class OUT request handler + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_req_class_out(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + app_usbd_nrf_dfu_trigger_t const * p_dfu = nrf_dfu_trigger_get(p_inst); + app_usbd_nrf_dfu_trigger_ctx_t * p_dfu_ctx = nrf_dfu_trigger_ctx_get(p_dfu); + + switch (p_setup_ev->setup.bmRequest) + { + case APP_USBD_NRF_DFU_TRIGGER_REQ_DETACH: + { + NRF_LOG_DEBUG("Entering DFU Mode"); + p_dfu_ctx->state = APP_USBD_NRF_DFU_TRIGGER_STATE_DETACH; + user_event_handler(p_inst, APP_USBD_NRF_DFU_TRIGGER_USER_EVT_DETACH); + return NRF_SUCCESS; + } + default: + break; + } + + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief Control endpoint handle + * + * @param[in] p_inst Generic class instance + * @param[in] p_setup_ev Setup event + * + * @return Standard error code + * @retval NRF_SUCCESS if request handled correctly + * @retval NRF_ERROR_NOT_SUPPORTED if request is not supported + */ +static ret_code_t setup_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_setup_evt_t const * p_setup_ev) +{ + ASSERT(p_inst != NULL); + ASSERT(p_setup_ev != NULL); + + if (app_usbd_setup_req_dir(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQDIR_IN) + { + switch (app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + case APP_USBD_SETUP_REQTYPE_STD: + return setup_req_std_in(p_inst, p_setup_ev); + case APP_USBD_SETUP_REQTYPE_CLASS: + return setup_req_class_in(p_inst, p_setup_ev); + default: + break; + } + } + else /* APP_USBD_SETUP_REQDIR_OUT */ + { + switch (app_usbd_setup_req_typ(p_setup_ev->setup.bmRequestType)) + { + case APP_USBD_SETUP_REQTYPE_STD: + return setup_req_std_out(p_inst, p_setup_ev); + case APP_USBD_SETUP_REQTYPE_CLASS: + return setup_req_class_out(p_inst, p_setup_ev); + default: + break; + } + } + + return NRF_ERROR_NOT_SUPPORTED; +} + +/** + * @brief @ref app_usbd_class_methods_t::event_handler + */ +static ret_code_t nrf_dfu_trigger_event_handler(app_usbd_class_inst_t const * p_inst, + app_usbd_complex_evt_t const * p_event) +{ + ASSERT(p_inst != NULL); + ASSERT(p_event != NULL); + + ret_code_t ret = NRF_SUCCESS; + switch (p_event->app_evt.type) + { + case APP_USBD_EVT_DRV_SOF: + break; + case APP_USBD_EVT_DRV_RESET: + break; + case APP_USBD_EVT_DRV_SETUP: + ret = setup_event_handler(p_inst, (app_usbd_setup_evt_t const *) p_event); + break; + case APP_USBD_EVT_DRV_EPTRANSFER: + break; + case APP_USBD_EVT_DRV_SUSPEND: + break; + case APP_USBD_EVT_DRV_RESUME: + break; + case APP_USBD_EVT_INST_APPEND: + break; + case APP_USBD_EVT_INST_REMOVE: + break; + case APP_USBD_EVT_STARTED: + break; + case APP_USBD_EVT_STOPPED: + break; + default: + ret = NRF_ERROR_NOT_SUPPORTED; + break; + } + + return ret; +} + +/** + * @brief @ref app_usbd_class_methods_t::feed_descriptors + */ +static bool nrf_dfu_trigger_feed_descriptors(app_usbd_class_descriptor_ctx_t * p_ctx, + app_usbd_class_inst_t const * p_inst, + uint8_t * p_buff, + size_t max_size) +{ + static app_usbd_class_iface_conf_t const * p_cur_iface = NULL; + p_cur_iface = app_usbd_class_iface_get(p_inst, 0); + app_usbd_nrf_dfu_trigger_t const * p_dfu = nrf_dfu_trigger_get(p_inst); + + APP_USBD_CLASS_DESCRIPTOR_BEGIN(p_ctx, p_buff, max_size) + + /* INTERFACE DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(sizeof(app_usbd_descriptor_iface_t)); // bLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_DESCRIPTOR_INTERFACE); // bDescriptorType + APP_USBD_CLASS_DESCRIPTOR_WRITE(app_usbd_class_iface_number_get(p_cur_iface)); // bInterfaceNumber + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // bAlternateSetting + APP_USBD_CLASS_DESCRIPTOR_WRITE(0); // bNumEndpoints + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_NRF_DFU_TRIGGER_CLASS); // bInterfaceClass + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_NRF_DFU_TRIGGER_SUBCLASS); // bInterfaceSubClass + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_NRF_DFU_TRIGGER_PROTOCOL_RUNTIME); // bInterfaceProtocol + APP_USBD_CLASS_DESCRIPTOR_WRITE(0x00); // iInterface + + /* FUNCTIONAL DESCRIPTOR */ + APP_USBD_CLASS_DESCRIPTOR_WRITE(sizeof(app_usbd_nrf_dfu_trigger_desc_func_t)); // bFunctionLength + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_NRF_DFU_TRIGGER_CS_FUNCTIONAL); // bDescriptorType + APP_USBD_CLASS_DESCRIPTOR_WRITE(APP_USBD_NRF_DFU_TRIGGER_BIT_CAN_DNLOAD | + APP_USBD_NRF_DFU_TRIGGER_BIT_WILL_DETACH); // bmAttribute + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(p_dfu->specific.inst.detach_timeout)); // wDetachTimeOut LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(p_dfu->specific.inst.detach_timeout)); // wDetachTimeOut MSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(p_dfu->specific.inst.transfer_size)); // wTransferSize LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(p_dfu->specific.inst.transfer_size)); // wTransferSize MSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(LSB_16(p_dfu->specific.inst.bcd_dfu)); // bcdDFUVersion LSB + APP_USBD_CLASS_DESCRIPTOR_WRITE(MSB_16(p_dfu->specific.inst.bcd_dfu)); // bcdDFUVersion MSB + + APP_USBD_CLASS_DESCRIPTOR_END(); +} + +const app_usbd_class_methods_t app_usbd_nrf_dfu_trigger_class_methods = { + .event_handler = nrf_dfu_trigger_event_handler, + .feed_descriptors = nrf_dfu_trigger_feed_descriptors, +}; + + +#endif //NRF_MODULE_ENABLED(APP_USBD_NRF_DFU_TRIGGER) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger.h new file mode 100644 index 0000000..b15ea96 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger.h @@ -0,0 +1,166 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_NRF_DFU_TRIGGER_H__ +#define APP_USBD_NRF_DFU_TRIGGER_H__ + +#include <stdint.h> +#include <stdbool.h> + +#include "nrf_drv_usbd.h" +#include "nrf_block_dev.h" +#include "app_usbd_class_base.h" +#include "app_usbd.h" +#include "app_usbd_core.h" +#include "app_usbd_descriptor.h" + +#include "app_usbd_nrf_dfu_trigger_types.h" +#include "app_usbd_nrf_dfu_trigger_internal.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_nrf_dfu_trigger USB Nordic DFU Trigger class + * @ingroup app_usbd + * + * @brief @tagAPI52840 Module with types, definitions, + * and API used by the USB Nordic DFU Trigger class. + * + * @{ + */ + +#ifdef DOXYGEN +/** + * @brief Nordic DFU Trigger class instance type + * + * @ref APP_USBD_CLASS_TYPEDEF + */ +typedef struct { } app_usbd_nrf_dfu_trigger_t; +#else +/*lint -save -e10 -e26 -e123 -e505 */ +APP_USBD_CLASS_NO_EP_TYPEDEF(app_usbd_nrf_dfu_trigger, \ + APP_USBD_NRF_DFU_TRIGGER_CONFIG(0), \ + APP_USBD_NRF_DFU_TRIGGER_INSTANCE_SPECIFIC_DEC, \ + APP_USBD_NRF_DFU_TRIGGER_DATA_SPECIFIC_DEC \ +); +#endif + +/*lint -restore*/ + + +/*lint -save -e407 */ + +/** + * @brief Events passed to user event handler + * + * @note Example prototype of user event handler: + * + * void dfu_user_ev_handler(app_usbd_class_inst_t const * p_inst, + * app_usbd_nrf_dfu_trigger_user_event_t event); + */ +typedef enum app_usbd_nrf_dfu_trigger_user_event_e { + APP_USBD_NRF_DFU_TRIGGER_USER_EVT_DETACH, +} app_usbd_nrf_dfu_trigger_user_event_t; + +/*lint -restore*/ + +/** + * @brief Global definition of app_usbd_nrf_dfu_trigger_t class + * + * @param instance_name Name of global instance + * @param interface_number Unique interface number + * @param p_nordic_dfu Pointer to @ref app_usbd_nrf_dfu_trigger_nordic_info_t structure + * @param sem_ver_str Semantic version string + * @param user_ev_handler User event handler (optional) + * + * @note This macro is just simplified version of @ref APP_USBD_NRF_DFU_TRIGGER_GLOBAL_DEF_INTERNAL + * + */ +/*lint -save -emacro(26 64 123 505 651, APP_USBD_NRF_DFU_TRIGGER_GLOBAL_DEF)*/ +#define APP_USBD_NRF_DFU_TRIGGER_GLOBAL_DEF(instance_name, \ + interface_number, \ + p_nordic_dfu, \ + sem_ver_str, \ + user_ev_handler) \ + APP_USBD_NRF_DFU_TRIGGER_GLOBAL_DEF_INTERNAL(instance_name, \ + interface_number, \ + 0, \ + 0, \ + p_nordic_dfu, \ + sem_ver_str, \ + user_ev_handler) +/*lint -restore*/ + +/** + * @@brief Helper function to get class instance from Nordic DFU Trigger + * + * @param[in] p_dfu Nordic DFU Trigger instance + * (declared by @ref APP_USBD_NRF_DFU_TRIGGER_GLOBAL_DEF) + * + * @return Base class instance + */ +static inline app_usbd_class_inst_t const * +app_usbd_nrf_dfu_trigger_class_inst_get(app_usbd_nrf_dfu_trigger_t const * p_dfu) +{ + return &p_dfu->base; +} + +/** + * @brief Helper function to get DFU from base class instance + * + * @param[in] p_inst Base class instance + * + * @return DFU class handle + */ +static inline app_usbd_nrf_dfu_trigger_t const * +app_usbd_nrf_dfu_trigger_class_get( app_usbd_class_inst_t const * p_inst) +{ + return (app_usbd_nrf_dfu_trigger_t const *)p_inst; +} + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_NRF_DFU_TRIGGER_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger_internal.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger_internal.h new file mode 100644 index 0000000..9195e2a --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger_internal.h @@ -0,0 +1,203 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_NRF_DFU_TRIGGER_INTERNAL_H__ +#define APP_USBD_NRF_DFU_TRIGGER_INTERNAL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_nrf_dfu_trigger_internals USB Nordic DFU Trigger internals + * @ingroup app_usbd_nrf_dfu_trigger + * + * @brief @tagAPI52840 Internals of the USB Nordic DFU Trigger class. + * @{ + */ + +/** + * @brief Forward declaration of Nordic DFU Trigger Class type + * + */ +APP_USBD_CLASS_FORWARD(app_usbd_nrf_dfu_trigger); + +/*lint -save -e165*/ +/** + * @brief Forward declaration of @ref app_usbd_nrf_dfu_trigger_user_event_e + * + */ +enum app_usbd_nrf_dfu_trigger_user_event_e; + +/*lint -restore*/ + +/** + * @brief User event handler + * + * @param[in] p_inst Class instance + * @param[in] event User event + * + * */ +typedef void (*app_usbd_nrf_dfu_trigger_user_ev_handler_t)( + app_usbd_class_inst_t const * p_inst, + enum app_usbd_nrf_dfu_trigger_user_event_e event); + +/** + * @brief Nordic DFU Trigger part of class instance data + */ +typedef struct { + size_t const sem_ver_size; //!< Semantic string size + uint16_t const detach_timeout; //!< Detach timeout in ms - for compatibility with DFU spec. + uint16_t const transfer_size; //!< Transfer size - for compatibility with DFU spec. + uint16_t const bcd_dfu; //!< DFU spec version. + uint8_t const * p_sem_ver; //!< Pointer to semantic version string + + app_usbd_nrf_dfu_trigger_nordic_info_t const * p_dfu_info; //!< Pointer to Nordic DFU Info + + app_usbd_nrf_dfu_trigger_user_ev_handler_t user_ev_handler; //!< User event handler +} app_usbd_nrf_dfu_trigger_inst_t; + +/** + * @brief Internal module state + * + * @note For future compatibility with DFU class. + */ +typedef enum { + APP_USBD_NRF_DFU_TRIGGER_STATE_IDLE, /**< Internal module state IDLE */ + APP_USBD_NRF_DFU_TRIGGER_STATE_DETACH, /**< Internal module state DETACH */ +} app_usbd_nrf_dfu_trigger_state_t; + +/** + * @brief Nordic DFU Trigger context + * + * */ +typedef struct { + app_usbd_nrf_dfu_trigger_state_t state; //!< Internal module state +} app_usbd_nrf_dfu_trigger_ctx_t; + + +/** + * @brief Nordic DFU Trigger configuration macro + * + * Used by @ref APP_USBD_NRF_DFU_TRIGGER_GLOBAL_DEF + * + * @param iface Interface number + * */ +#define APP_USBD_NRF_DFU_TRIGGER_CONFIG(iface) (iface) + + +/** + * @brief Specific class constant data for Nordic DFU Trigger + * + * @ref app_usbd_nrf_dfu_trigger_inst_t + */ +#define APP_USBD_NRF_DFU_TRIGGER_INSTANCE_SPECIFIC_DEC app_usbd_nrf_dfu_trigger_inst_t inst; + + +/** + * @brief Configures Nordic DFU Trigger instance + * + * @param user_event_handler User event handler + * @param p_nordic_dfu Pointer to @ref app_usbd_nrf_dfu_trigger_nordic_info_t structure + * @param sem_ver_str Semantic version string + * @param timeout Detach timeout in ms - left for compatibility with USB DFU spec. + * @param size Transfer size in bytes - left for compatibility with USB DFU spec. + * @param bcd USB DFU specification version. + */ +#define APP_USBD_NRF_DFU_TRIGGER_INST_CONFIG(user_event_handler, \ + p_nordic_dfu, \ + sem_ver_str, \ + timeout, \ + size, \ + bcd) \ + .inst = { \ + .user_ev_handler = user_event_handler, \ + .p_dfu_info = p_nordic_dfu, \ + .p_sem_ver = sem_ver_str, \ + .sem_ver_size = sizeof(sem_ver_str), \ + .transfer_size = size, \ + .detach_timeout = timeout, \ + .bcd_dfu = bcd, \ + } + +/** + * @brief Specific class data for Nordic DFU Trigger + * + * @ref app_usbd_nrf_dfu_trigger_ctx_t + * */ +#define APP_USBD_NRF_DFU_TRIGGER_DATA_SPECIFIC_DEC app_usbd_nrf_dfu_trigger_ctx_t ctx; + + +/** + * @brief Public Nordic DFU Trigger class interface + */ +extern const app_usbd_class_methods_t app_usbd_nrf_dfu_trigger_class_methods; + +/** + * @brief Global definition of Nordic DFU Trigger instance + */ +#define APP_USBD_NRF_DFU_TRIGGER_GLOBAL_DEF_INTERNAL(instance_name, \ + interface_number, \ + detach_timeout, \ + transfer_size, \ + p_nordic_dfu, \ + sem_ver_str, \ + user_ev_handler) \ + APP_USBD_CLASS_INST_NO_EP_GLOBAL_DEF( \ + instance_name, \ + app_usbd_nrf_dfu_trigger, \ + &app_usbd_nrf_dfu_trigger_class_methods, \ + APP_USBD_NRF_DFU_TRIGGER_CONFIG((interface_number)), \ + (APP_USBD_NRF_DFU_TRIGGER_INST_CONFIG(user_ev_handler, \ + p_nordic_dfu, \ + sem_ver_str, \ + detach_timeout, \ + transfer_size, \ + 0x0110)) \ + ) + + +/** @} */ + + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_NRF_DFU_TRIGGER_INTERNAL_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger_types.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger_types.h new file mode 100644 index 0000000..93f7fb9 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/class/nrf_dfu_trigger/app_usbd_nrf_dfu_trigger_types.h @@ -0,0 +1,145 @@ +/** + * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_NRF_DFU_TRIGGER_TYPES_H__ +#define APP_USBD_NRF_DFU_TRIGGER_TYPES_H__ + +#include "app_util.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup app_usbd_nrf_dfu_trigger_types USB Nordic DFU Trigger types + * @ingroup app_usbd_nrf_dfu_trigger + * + * @brief @tagAPI52840 Types used in the USB Nordic DFU Trigger class. + * @{ + */ + +/** @brief Nordic DFU Trigger class definition in interface descriptor + * + * @ref app_usbd_descriptor_iface_t::bInterfaceClass + * */ +#define APP_USBD_NRF_DFU_TRIGGER_CLASS 0xFF + +/** + * @brief Nordic DFU Trigger subclass possible value + */ +typedef enum { + APP_USBD_NRF_DFU_TRIGGER_SUBCLASS = 0x01, /**< Device Firmware Upgrade Code */ +} app_usbd_nrf_dfu_trigger_subclass_t; + +/** + * @brief Nordic DFU Trigger protocol possible value + * + * @note Only Run-time protocol is implemented, as DFU mode is covered by Nordic DFU. + */ +typedef enum { + APP_USBD_NRF_DFU_TRIGGER_PROTOCOL_RUNTIME = 0x01, /**< Run-time Protocol */ +} app_usbd_nrf_dfu_trigger_protocol_t; + +/** + * @brief Nordic DFU Trigger USB requests @ref nrf_drv_usbd_setup_t::bmRequestType + * + * @note Requests are limited only to @ref APP_USBD_NRF_DFU_TRIGGER_PROTOCOL_RUNTIME protocol type. + */ +typedef enum { + APP_USBD_NRF_DFU_TRIGGER_REQ_DETACH = 0x00, /**< Switch to DFU mode */ + APP_USBD_NRF_DFU_TRIGGER_REQ_NORDIC_INFO = 0x07, /**< Request Nordic DFU information */ + APP_USBD_NRF_DFU_TRIGGER_REQ_SEM_VER = 0x08, /**< Request semantic version information */ +} app_usbd_nrf_dfu_trigger_req_t; + +/** + * @brief Nordic DFU Trigger Functional Descriptor types. + */ +typedef enum { + APP_USBD_NRF_DFU_TRIGGER_CS_FUNCTIONAL = 0x21, /**< Functional descriptor type.*/ +} app_usbd_nrf_dfu_trigger_func_type_t; + +/** + * @brief Nordic DFU Trigger functional descriptor bmAttributes bit masks. + * + * @note For future compatibility with DFU class. + */ +typedef enum { + APP_USBD_NRF_DFU_TRIGGER_BIT_CAN_DNLOAD = 0x01, /**< bitCanDnload */ + APP_USBD_NRF_DFU_TRIGGER_BIT_CAB_UPLOAD = 0x02, /**< bitCanUpload */ + APP_USBD_NRF_DFU_TRIGGER_BIT_MANI_TOLERANT = 0x04, /**< bitManifestationTolerant */ + APP_USBD_NRF_DFU_TRIGGER_BIT_WILL_DETACH = 0x08, /**< bitWillDetach */ +} app_usbd_nrf_dfu_trigger_bm_attributes_t; + +#pragma pack(push, 1) + +/** + * @brief Nordic DFU Trigger Functional Descriptor + */ +typedef struct { + uint8_t bFunctionLength; //!< Size of this descriptor in bytes. + uint8_t bDescriptorType; //!< Descriptor type. + uint8_t bmAttributes; //!< Attributes bits. + uint16_t wDetachTimeOut; //!< Device detach timeout - left for compatibility with USB DFU spec. + uint16_t wTransferSize; //!< Transfer size - left for compatibility with USB DFU spec. + uint16_t bcdDFUVersion; //!< DFU version. +} app_usbd_nrf_dfu_trigger_desc_func_t; + +/** + * @brief Nordic custom DFU information. + */ +typedef struct { + uint32_t wAddress; //!< Firmware start address. + uint32_t wFirmwareSize; //!< Firmware size. + uint16_t wVersionMajor; //!< Firmware version major. + uint16_t wVersionMinor; //!< Firmware version minor. + uint32_t wFirmwareID; //!< Firmware ID. + uint32_t wFlashSize; //!< Flash size. + uint32_t wFlashPageSize; //!< Flash page size. +} app_usbd_nrf_dfu_trigger_nordic_info_t; + +#pragma pack(pop) + + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* APP_USBD_NRF_DFU_TRIGGER_TYPES_H__ */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/config/app_usbd_string_config.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/config/app_usbd_string_config.h new file mode 100644 index 0000000..3c9da6b --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/usbd/config/app_usbd_string_config.h @@ -0,0 +1,131 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef APP_USBD_STRING_CONFIG_H +#define APP_USBD_STRING_CONFIG_H + +/** + * @defgroup app_usbd_string_conf USBD string configuration + * @ingroup app_usbd_string_desc + * + * @brief @tagAPI52840 Configuration of the string module that can be easily affected by the final + * user. + * @{ + */ + +/** + * @brief Supported languages identifiers + * + * Comma separated list of supported languages. + */ +#define APP_USBD_STRINGS_LANGIDS \ + ((uint16_t)APP_USBD_LANG_ENGLISH | (uint16_t)APP_USBD_SUBLANG_ENGLISH_US) + +/** + * @brief Manufacturer name string descriptor + * + * Comma separated list of manufacturer names for each defined language. + * Use @ref APP_USBD_STRING_DESC macro to create string descriptor. + * + * The order of manufacturer names has to be the same like in + * @ref APP_USBD_STRINGS_LANGIDS. + */ +#define APP_USBD_STRINGS_MANUFACTURER \ + APP_USBD_STRING_DESC('N', 'o', 'r', 'd', 'i', 'c', ' ', 'S', 'e', 'm', 'i', 'c', 'o', 'n', 'd', 'u', 'c', 't', 'o', 'r') + +/** + * @brief Define whether @ref APP_USBD_STRINGS_MANUFACTURER is created by @ref APP_USBD_STRING_DESC + * or declared as global variable. + * */ +#define APP_USBD_STRINGS_MANUFACTURER_EXTERN 0 + +/** + * @brief Product name string descriptor + * + * List of product names defined the same way like in @ref APP_USBD_STRINGS_MANUFACTURER + */ +#define APP_USBD_STRINGS_PRODUCT \ + APP_USBD_STRING_DESC('n', 'R', 'F', '5', '2', ' ', 'U', 'S', 'B', ' ', 'D', 'e', 'm', 'o') + + +/** + * @brief Define whether @ref APP_USBD_STRINGS_PRODUCT is created by @ref APP_USBD_STRING_DESC + * or declared as global variable. + * */ +#define APP_USBD_STRINGS_PRODUCT_EXTERN 0 + +/** + * @brief Serial number string descriptor + * + * Create serial number string descriptor using @ref APP_USBD_STRING_DESC, + * or configure it to point to any internal variable pointer filled with descriptor. + * + * @note + * There is only one SERIAL number inside the library and it is Language independent. + */ +#define APP_USBD_STRING_SERIAL \ + APP_USBD_STRING_DESC('0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0') + +/** + * @brief Define whether @ref APP_USBD_STRING_SERIAL is created by @ref APP_USBD_STRING_DESC + * or declared as global variable. + * */ +#define APP_USBD_STRING_SERIAL_EXTERN 0 + +/** + * @brief User strings default values + * + * This value stores all application specific user strings with its default initialization. + * The setup is done by X-macros. + * Expected macro parameters: + * @code + * X(mnemonic, [=str_idx], ...) + * @endcode + * - @c mnemonic: Mnemonic of the string descriptor that would be added to + * @ref app_usbd_string_desc_idx_t enumerator. + * - @c str_idx : String index value, may be set or left empty. + * For example WinUSB driver requires descriptor to be present on 0xEE index. + * Then use X(USBD_STRING_WINUSB, =0xEE, (APP_USBD_STRING_DESC(...))) + * - @c ... : List of string descriptors for each defined language. + */ +#define APP_USBD_STRINGS_USER \ + X(APP_USER_1, , APP_USBD_STRING_DESC('U', 's', 'e', 'r', ' ', '1')) + +/** @} */ +#endif /* APP_USBD_STRING_CONFIG_H */ |