move components to SDK dir

This commit is contained in:
Shuanglei Tao
2025-03-03 09:06:26 +08:00
parent 20d1297e57
commit f4f4c9e60d
1021 changed files with 58 additions and 35059 deletions

View File

@@ -0,0 +1,509 @@
/**
* Copyright (c) 2016 - 2017, 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_CLASS_HID_ENABLED
#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 if Idle transfer is required, false otherwise
* */
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 @ref app_usbd_hid_user_event_t
* */
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)
{
ASSERT(p_hinst->user_event_handler != NULL);
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
* @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_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 (p_setup_ev->setup.bmRequest != APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR)
{
return NRF_ERROR_NOT_SUPPORTED;
}
size_t dsc_len = 0;
/* Try to find descriptor in class internals*/
void const * p_dsc = app_usbd_class_descriptor_find(p_inst,
p_setup_ev->setup.wValue.hb,
p_setup_ev->setup.wValue.lb,
&dsc_len);
if (p_dsc != NULL)
{
return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_dsc, dsc_len);
}
/* HID specific descriptors*/
switch (p_setup_ev->setup.wValue.hb)
{
case APP_USBD_HID_DESCRIPTOR_REPORT:
{
return app_usbd_core_setup_rsp(&p_setup_ev->setup,
p_hinst->p_report_dsc,
p_hinst->report_dsc_size);
}
case APP_USBD_HID_DESCRIPTOR_PHYSICAL:
/*Not supported*/
break;
default:
/*Not supported*/
break;
}
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
* @retval NRF_SUCCESS if request handled correctly
* @retval NRF_ERROR_NOT_SUPPORTED if request is not supported
*/
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
* @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_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:
{
/*Only input report is supported. Same format as over IN pipe.*/
if (p_setup_ev->setup.wValue.hb != APP_USBD_HID_REPORT_TYPE_INPUT)
{
break;
}
return p_hinst->p_hid_methods->on_get_report(p_inst, p_setup_ev);
}
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->protocol,
sizeof(p_hid_ctx->protocol));
}
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
* @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_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->protocol = p_setup_ev->setup.wValue.w;
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
* @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_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_rec(p_setup_ev->setup.bmRequestType) == APP_USBD_SETUP_REQREC_ENDPOINT)
{
return app_usbd_endpoint_std_req_handle(p_inst, p_setup_ev);
}
ret_code_t ret = app_usbd_interface_std_req_handle(p_inst, p_setup_ev);
if (ret == NRF_SUCCESS || ret != NRF_ERROR_NOT_SUPPORTED)
{
return ret;
}
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
* @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_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
* @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_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);
user_event_handler(p_inst, p_hinst, APP_USBD_HID_USER_EVT_SUSPEND);
break;
case APP_USBD_EVT_DRV_RESUME:
app_usbd_hid_state_flag_clr(p_hid_ctx, APP_USBD_HID_STATE_FLAG_SUSPENDED);
user_event_handler(p_inst, p_hinst, APP_USBD_HID_USER_EVT_RESUME);
/* 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;
}
app_usbd_core_class_rwu_register(p_inst);
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;
}
app_usbd_core_class_rwu_unregister(p_inst);
app_usbd_hid_state_flag_clr(p_hid_ctx, APP_USBD_HID_STATE_FLAG_APPENDED);
break;
case APP_USBD_EVT_START:
app_usbd_hid_state_flag_set(p_hid_ctx, APP_USBD_HID_STATE_FLAG_STARTED);
user_event_handler(p_inst, p_hinst, APP_USBD_HID_USER_EVT_START);
break;
case APP_USBD_EVT_STOP:
app_usbd_hid_state_flag_clr(p_hid_ctx, APP_USBD_HID_STATE_FLAG_STARTED);
user_event_handler(p_inst, p_hinst, APP_USBD_HID_USER_EVT_STOP);
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,
size_t report_id)
{
ASSERT(p_hinst);
if (report_id >= p_hinst->rep_buffers_count_in)
{
return NULL;
}
return &p_hinst->p_rep_buffers_in[report_id];
}
#endif // APP_USBD_CLASS_HID_ENABLED

View File

@@ -0,0 +1,446 @@
/**
* Copyright (c) 2016 - 2017, 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__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
#include "sdk_common.h"
#include "nrf_atomic.h"
#include "app_usbd_hid_types.h"
#include "app_usbd_core.h"
/**
* @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 /**< HID instance interface index */
#define APP_USBD_HID_EPIN_IDX 0 /**< HID instance endpoint IN index */
#define APP_USBD_HID_EPOUT_IDX 1 /**< HID instance endpoint OUT index */
/**
* @brief HID context 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_SUSPEND = 0, /**< Event SUSPEND */
APP_USBD_HID_USER_EVT_RESUME, /**< Event RESUME */
APP_USBD_HID_USER_EVT_START, /**< Event START */
APP_USBD_HID_USER_EVT_STOP, /**< Event STOP */
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);
} 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 USB HID instance
* */
typedef struct {
uint8_t const * p_raw_desc; //!< HID class descriptors, raw array
size_t raw_desc_size; //!< HID class descriptors size, raw array size
uint8_t const * p_report_dsc; //!< Report descriptor, raw array
size_t report_dsc_size; //!< Report descriptor, raw array size
app_usbd_hid_report_buffer_t * p_rep_buffers_in; //!< Report buffers IN
size_t rep_buffers_count_in; //!< Report count 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 descriptors Interface, hid, endpoint packed descriptor structure
* @param report_dsc Report descriptor (raw uint8_t array)
* @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(descriptors, \
report_dsc, \
report_buff_in, \
report_buff_out, \
user_ev_handler, \
hid_methods) \
{ \
.p_raw_desc = descriptors, \
.raw_desc_size = ARRAY_SIZE(descriptors), \
.p_report_dsc = report_dsc, \
.report_dsc_size = ARRAY_SIZE(report_dsc), \
.p_rep_buffers_in = report_buff_in, \
.rep_buffers_count_in = ARRAY_SIZE(report_buff_in), \
.p_rep_buffer_out = report_buff_out, \
.user_event_handler = user_ev_handler, \
.p_hid_methods = hid_methods, \
}
/**
* @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 protocol; //!< HID protocol type
} 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
*/
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)
{
app_usbd_core_class_rwu_pend();
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
*
* @retval NRF_SUCCESS If operation was successful.
* */
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
* @param[in] report_id Report ID
*
* @return Report buffer 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,
size_t report_id);
/**
* @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 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__ */

View File

@@ -0,0 +1,273 @@
/**
* Copyright (c) 2016 - 2017, 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 "sdk_common.h"
/**
* @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 descriptor types
*
* 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 protocol types defined by specification
*
*/
/** @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 Helper macro for HID BCD release
*
* @param major Hid release number major
* @param minor Hid release number minor
*
* @ref app_usbd_hid_descriptor_t::bcdHID
* @ref APP_USBD_HID_BCD_V1_11
*/
#define APP_USBD_HID_BCD_MAKE(major, minor) 0x##minor, 0x##major
/** @brief HID 1.11 BCD value definition
*
* @ref app_usbd_hid_descriptor_t::bcdHID
*/
#define APP_USBD_HID_BCD_V1_11 APP_USBD_HID_BCD_MAKE(1, 11)
/**
* @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_V1_11, \
/*.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, \
/** @} */
#endif /* APP_USBD_HID_TYPES_H__ */

View File

@@ -0,0 +1,380 @@
/**
* Copyright (c) 2016 - 2017, 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_HID_GENERIC_ENABLED
#include <string.h>
#include "sdk_common.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_inst Class instance data
* @return HID generic instance data @ref app_usbd_hid_generic_ctx_t
*/
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 instance data @ref app_usbd_hid_generic_t
*/
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
*
* @*/
static inline app_usbd_hid_report_buffer_t const *
hid_generic_rep_buffer_get(app_usbd_hid_generic_t const * p_generic, uint8_t rep_id)
{
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, rep_id);
}
/**@brief Auxiliary function to prepare report transfer buffer to next transfer
*
* @param[in] p_generic_ctx Internal HID generic context
* @retval true if next transfer is required
* @retval false if next transfer is not required
*/
static inline bool hid_generic_transfer_next(app_usbd_hid_generic_ctx_t * p_generic_ctx,
uint8_t rep_id)
{
ASSERT(rep_id < 32);
return IS_SET(p_generic_ctx->rep_request_mask, rep_id);
}
/**
* @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,
uint8_t rep_id)
{
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_ctx, rep_id))
{
/* 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_buff = hid_generic_rep_buffer_get(p_generic, rep_id);
NRF_DRV_USBD_TRANSFER_IN(transfer, p_rep_buff->p_buff, p_rep_buff->size);
ret_code_t ret;
CRITICAL_REGION_ENTER();
ret = app_usbd_core_ep_transfer(ep_addr, &transfer, NULL);
if (ret == NRF_SUCCESS)
{
p_generic_ctx->rep_in_index = rep_id;
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_report_in_set(app_usbd_hid_generic_t const * p_generic,
uint8_t rep_id,
const void * p_buff,
size_t size)
{
app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
app_usbd_hid_inst_t const * p_hinst = &p_generic->specific.inst.hid_inst;
if (!app_usbd_hid_generic_report_in_done(p_generic, rep_id))
{
return NRF_ERROR_BUSY;
}
/* Get HID report buffer */
app_usbd_hid_report_buffer_t * p_rep_buff = app_usbd_hid_rep_buff_in_get(p_hinst, rep_id);
ASSERT(p_rep_buff != NULL);
p_rep_buff->p_buff = (uint8_t *)p_buff;
p_rep_buff->size = size;
/* Set report ID for reports other than default */
if (rep_id != 0)
{
p_rep_buff->p_buff[0] = rep_id;
}
(void)nrf_atomic_u32_or(&p_generic_ctx->rep_request_mask, 1u << rep_id);
if (app_usbd_hid_trans_required(&p_generic_ctx->hid_ctx))
{
/*New transfer need to be triggered*/
return hid_generic_transfer_set(p_generic, rep_id);
}
return NRF_SUCCESS;
}
bool app_usbd_hid_generic_report_in_done(app_usbd_hid_generic_t const * p_generic, uint8_t rep_id)
{
app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
return IS_SET(p_generic_ctx->rep_request_mask, rep_id) == 0;
}
uint8_t app_usbd_hid_generic_in_report_last_id(app_usbd_hid_generic_t const * p_generic)
{
app_usbd_hid_generic_ctx_t * p_generic_ctx = hid_generic_ctx_get(p_generic);
return p_generic_ctx->rep_in_index;
}
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_generic = hid_generic_get(p_inst);
app_usbd_hid_report_buffer_t const * p_rep_buffer = hid_generic_rep_buffer_get(p_generic,
p_setup_ev->setup.wValue.lb);
if (p_rep_buffer == NULL)
{
return NRF_ERROR_NOT_SUPPORTED;
}
return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_rep_buffer->p_buff, p_rep_buffer->size);
}
static ret_code_t hid_generic_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_generic_t const * p_generic = (app_usbd_hid_generic_t const *)p_context;
app_usbd_hid_user_ev_handler_t 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_core_setup_data_transfer(NRF_DRV_USBD_EPOUT0, &transfer, NULL);
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);
UNUSED_RETURN_VALUE(nrf_atomic_u32_and(&p_generic_ctx->rep_request_mask,
~(1u << p_generic_ctx->rep_in_index)));
uint32_t pending_reports = p_generic_ctx->rep_request_mask;
if (pending_reports == 0)
{
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 */
uint32_t rep_id = __CLZ(__RBIT(pending_reports));
return hid_generic_transfer_set((app_usbd_hid_generic_t const *)p_inst, rep_id);
}
/**
* @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_core_ep_transfer(ep_addr, &transfer, NULL);
}
/**
* @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;
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);
}
/**
* @brief @ref app_usbd_class_interface_t::get_descriptors
*/
static const void * hid_generic_get_descriptors(app_usbd_class_inst_t const * p_inst,
size_t * p_size)
{
ASSERT(p_size != 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;
*p_size = p_hinst->raw_desc_size;
return p_hinst->p_raw_desc;
}
/** @} */
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,
};
const app_usbd_class_methods_t app_usbd_generic_class_methods = {
.event_handler = hid_generic_event_handler,
.get_descriptors = hid_generic_get_descriptors,
};
#endif // APP_USBD_HID_GENERIC_ENABLED

View File

@@ -0,0 +1,231 @@
/**
* Copyright (c) 2016 - 2017, 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 hid_dsc_list HID descriptor item list @ref APP_USBD_HID_REPORT_ITEM, @ref APP_USBD_HID_PHYSICAL_ITEM
* @param report_cnt Report count
* @param report_out_maxsize Maximum output report size
*
* @note This macro is just simplified version of @ref APP_USBD_HID_GENERIC_GLOBAL_DEF_INTERNAL
*
* Example class definition:
* @code
static uint8_t m_generic_rep_dsc[] = APP_USBD_HID_MOUSE_REPORT_DSC_BUTTON(2);
#define HID_DESCRIPTOR_ITEM_LIST \
( \
m_generic_rep_dsc, \
)
#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,
HID_DESCRIPTOR_ITEM_LIST,
REPORT_COUNT,
REPORT_OUT_MAXSIZE);
@endcode
*
*/
#define APP_USBD_HID_GENERIC_GLOBAL_DEF(instance_name, \
interface_number, \
user_ev_handler, \
endpoint_list, \
hid_dsc_list, \
report_cnt, \
report_out_maxsize) \
APP_USBD_HID_GENERIC_GLOBAL_DEF_INTERNAL(instance_name, \
interface_number, \
user_ev_handler, \
endpoint_list, \
hid_dsc_list, \
report_cnt, \
report_out_maxsize)
/**
* @brief Helper function to get class instance from HID generic class
*
* @param[in] p_generic HID generic class instance (defined by @ref APP_USBD_HID_GENERIC_GLOBAL_DEF)
*
* @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 (defined by @ref APP_USBD_HID_GENERIC_GLOBAL_DEF)
* @param[in] rep_id Input report ID
* @param[in] p_buff Report buffer
* @param[in] size Report size
*
* @return Standard error code
*/
ret_code_t app_usbd_hid_generic_report_in_set(app_usbd_hid_generic_t const * p_generic,
uint8_t rep_id,
const void * p_buff,
size_t size);
/**
* @brief Checks whether last IN report transfer has been done
*
* Helper function to check, whether that last IN report transfer is done.
*
* @param[in] p_generic HID generic class instance (defined by @ref APP_USBD_HID_GENERIC_GLOBAL_DEF)
* @param rep_id input Report ID
*
* @return true if transfer done, false otherwise
*/
bool app_usbd_hid_generic_report_in_done(app_usbd_hid_generic_t const * p_generic, uint8_t rep_id);
/**
* @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 (defined by @ref APP_USBD_HID_GENERIC_GLOBAL_DEF)
* @return Last transfered report ID.
*/
uint8_t app_usbd_hid_generic_in_report_last_id(app_usbd_hid_generic_t const * p_generic);
/**
* @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 (defined by @ref APP_USBD_HID_GENERIC_GLOBAL_DEF)
* @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__ */

View File

@@ -0,0 +1,90 @@
/**
* Copyright (c) 2016 - 2017, 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 Endpoint number
* */
#define APP_USBD_HID_GENERIC_INTERFACE_DSC(interface_number, endpoints_num) \
APP_USBD_HID_INTERFACE_DSC(interface_number, endpoints_num, \
APP_USBD_HID_SUBCLASS_NONE, APP_USBD_HID_PROTO_GENERIC)
/**
* @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__ */

View File

@@ -0,0 +1,216 @@
/**
* Copyright (c) 2016 - 2017, 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
/**
* @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
} app_usbd_hid_generic_inst_t;
/**
* @brief HID generic context
*
* */
typedef struct {
app_usbd_hid_ctx_t hid_ctx; //!< HID class context
nrf_atomic_u32_t rep_request_mask; //!< Input report requests
uint8_t rep_in_index; //!< Current IN report ID
uint8_t rep_out_index; //!< Current OUT report ID
} 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 HID generic configuration for one endpoint
*
* @ref APP_USBD_HID_GENERIC_DSC_CONFIG
* */
#define APP_USBD_HID_GENERIC_DSC_CONFIG_1(interface_number, report_list, ...) { \
APP_USBD_HID_GENERIC_INTERFACE_DSC(interface_number, NUM_VA_ARGS(__VA_ARGS__)) \
APP_USBD_HID_GENERIC_HID_DSC(BRACKET_EXTRACT(report_list)) \
APP_USBD_HID_GENERIC_EP_DSC(GET_VA_ARG_1(__VA_ARGS__)) \
}
/**
* @brief HID generic configuration for two endpoints
*
* @ref APP_USBD_HID_GENERIC_DSC_CONFIG
* */
#define APP_USBD_HID_GENERIC_DSC_CONFIG_2(interface_number, report_list, ...) { \
APP_USBD_HID_GENERIC_INTERFACE_DSC(interface_number, NUM_VA_ARGS(__VA_ARGS__)) \
APP_USBD_HID_GENERIC_HID_DSC(BRACKET_EXTRACT(report_list)) \
APP_USBD_HID_GENERIC_EP_DSC(GET_VA_ARG_1(__VA_ARGS__)) \
APP_USBD_HID_GENERIC_EP_DSC(GET_VA_ARG_1(GET_ARGS_AFTER_1_(__VA_ARGS__))) \
}
/**
* @brief HID generic descriptors config macro
*
* @ref app_usbd_hid_generic_inst_t
*
* @param interface_number Interface number
* @param report_list Report list
* @param ... Endpoint list
*
* */
#define APP_USBD_HID_GENERIC_DSC_CONFIG(interface_number, report_list, ...) \
CONCAT_2(APP_USBD_HID_GENERIC_DSC_CONFIG_, NUM_VA_ARGS(__VA_ARGS__)) \
(interface_number, report_list, __VA_ARGS__)
/**
* @brief Configure internal part of HID generic instance
*
* @param descriptors Raw descriptors buffer
* @param report_buff_in Input report buffers array
* @param report_buff_out Output report buffer
* @param user_ev_handler User event handler
* @param ... Hid descriptors list
*/
#define APP_USBD_HID_GENERIC_INST_CONFIG(descriptors, \
report_buff_in, \
report_buff_out, \
user_ev_handler, \
...) \
.inst = { \
.hid_inst = APP_USBD_HID_INST_CONFIG(descriptors, \
GET_VA_ARG_1(__VA_ARGS__), \
report_buff_in, \
report_buff_out, \
user_ev_handler, \
&app_usbd_hid_generic_methods), \
}
/**
* @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, \
hid_dsc_list, \
report_cnt, \
report_out_maxsize) \
static const uint8_t CONCAT_2(instance_name, _dsc)[] = \
APP_USBD_HID_GENERIC_DSC_CONFIG(interface_number, \
hid_dsc_list, \
BRACKET_EXTRACT(endpoint_list)); \
static app_usbd_hid_report_buffer_t CONCAT_2(instance_name, _in)[report_cnt]; \
APP_USBD_HID_GENERIC_GLOBAL_OUT_REP_DEF(CONCAT_2(instance_name, _out), \
report_out_maxsize + 1); \
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, _dsc), \
CONCAT_2(instance_name, _in), \
&CONCAT_2(instance_name, _out), \
user_ev_handler, \
BRACKET_EXTRACT(hid_dsc_list))) \
)
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* APP_USBD_HID_GENERIC_INTERNAL_H__ */

View File

@@ -0,0 +1,402 @@
/**
* Copyright (c) 2016 - 2017, 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_HID_KBD_ENABLED
#include <string.h>
#include "sdk_common.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
* @internals
*/
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 @ref app_usbd_hid_kbd_ctx_t
*/
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 @ref app_usbd_hid_kbd_t
*/
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 @ref app_usbd_hid_report_buffer_t
* */
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, 0);
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 if next transfer is required
* @retval false if 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_core_ep_transfer(ep_addr, &transfer, NULL);
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;
}
/**
* @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_kbd = hid_kbd_get(p_inst);
app_usbd_hid_report_buffer_t const * p_rep_buffer = hid_kbd_rep_buffer_get(p_kbd);
return app_usbd_core_setup_rsp(&(p_setup_ev->setup), p_rep_buffer->p_buff, p_rep_buffer->size);
}
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_core_setup_data_transfer(NRF_DRV_USBD_EPOUT0, &transfer, NULL);
if (ret == NRF_SUCCESS)
{
app_usbd_core_setup_data_handler_desc_t desc = {
.handler = hid_kbd_on_set_report_data_cb,
.p_context = (void*)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;
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);
}
/**
* @brief @ref app_usbd_class_interface_t::get_descriptors
*/
static const void * hid_kbd_get_descriptors(app_usbd_class_inst_t const * p_inst,
size_t * p_size)
{
ASSERT(p_size != 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;
*p_size = p_hinst->raw_desc_size;
return p_hinst->p_raw_desc;
}
/** @} */
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,
};
const app_usbd_class_methods_t app_usbd_hid_kbd_class_methods = {
.event_handler = hid_kbd_event_handler,
.get_descriptors = hid_kbd_get_descriptors,
};
#endif // APP_USBD_HID_KBD_ENABLED

View File

@@ -0,0 +1,300 @@
/**
* Copyright (c) 2016 - 2017, 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)
*
* Example class definition:
* @code
APP_USBD_HID_KBD_GLOBAL_DEF(my_awesome_kbd, 0, NRF_DRV_USBD_EPIN1, NULL)
* @endcode
*/
#define APP_USBD_HID_KBD_GLOBAL_DEF(instance_name, interface_number, endpoint, user_ev_handler) \
APP_USBD_HID_KBD_GLOBAL_DEF_INTERNAL(instance_name, \
interface_number, \
endpoint, \
user_ev_handler)
/**
* @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
* @return true if LED is set, false otherwise
* */
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__ */

View File

@@ -0,0 +1,129 @@
/**
* Copyright (c) 2016 - 2017, 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__ */

View File

@@ -0,0 +1,197 @@
/**
* Copyright (c) 2016 - 2017, 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 HID keyboard descriptors config macro
*
* @ref app_usbd_hid_kbd_inst_t
*
* @param interface_number Interface number
* @param endpoint Endpoint number
* @param rep_descriptor Keyboard report descriptor
* */
#define APP_USBD_HID_KBD_DSC_CONFIG(interface_number, endpoint, rep_descriptor) { \
APP_USBD_HID_KBD_INTERFACE_DSC(interface_number) \
APP_USBD_HID_KBD_HID_DSC(rep_descriptor) \
APP_USBD_HID_KBD_EP_DSC(endpoint) \
}
/**
* @brief Configure internal part of HID keyboard instance
*
* @param descriptors Raw descriptors buffer
* @param report_desc Report descriptor
* @param report_buff_in Input report buffers array
* @param report_buff_out Output report buffer
* @param user_ev_handler User event handler
*/
#define APP_USBD_HID_KBD_INST_CONFIG(descriptors, \
report_desc, \
report_buff_in, \
report_buff_out, \
user_ev_handler) \
.inst = { \
.hid_inst = APP_USBD_HID_INST_CONFIG(descriptors, \
report_desc, \
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) \
static const uint8_t CONCAT_2(instance_name, _rep_dsc)[] = \
APP_USBD_HID_KBD_REPORT_DSC(); \
static const uint8_t CONCAT_2(instance_name, _dsc)[] = \
APP_USBD_HID_KBD_DSC_CONFIG(interface_number, \
endpoint, \
CONCAT_2(instance_name, _rep_dsc)); \
APP_USBD_HID_GENERIC_GLOBAL_OUT_REP_DEF(CONCAT_2(instance_name, _out), 1 + 1); \
static app_usbd_hid_report_buffer_t CONCAT_2(instance_name, _in)[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, _dsc), \
CONCAT_2(instance_name, _rep_dsc), \
CONCAT_2(instance_name, _in), \
&CONCAT_2(instance_name, _out), \
user_ev_handler)) \
)
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* APP_USBD_HID_KBD_INTERNAL_H__ */

View File

@@ -0,0 +1,461 @@
/**
* Copyright (c) 2016 - 2017, 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_HID_MOUSE_ENABLED
#include <string.h>
#include "sdk_common.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 @ref app_usbd_hid_mouse_ctx_t
*/
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 data @ref app_usbd_hid_mouse_t
*/
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 @ref app_usbd_hid_report_buffer_t
* */
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, 0);
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, 0);
}
/**@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 if next transfer is required
* @retval false if 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_core_ep_transfer(ep_addr, &transfer, NULL);
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] signed 16 bit accumulator to test
* @param[in] value value to add to accumulator
* @return true if overflow detected, false otherwise
*/
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 (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)
{
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);
}
/**
* @brief @ref app_usbd_class_interface_t::get_descriptors
*/
static const void * hid_mouse_get_descriptors(app_usbd_class_inst_t const * p_inst,
size_t * p_size)
{
ASSERT(p_size != 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;
*p_size = p_hinst->raw_desc_size;
return p_hinst->p_raw_desc;
}
/** @} */
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,
};
const app_usbd_class_methods_t app_usbd_hid_mouse_class_methods = {
.event_handler = hid_mouse_event_handler,
.get_descriptors = hid_mouse_get_descriptors,
};
#endif // APP_USBD_HID_MOUSE_ENABLED

View File

@@ -0,0 +1,179 @@
/**
* Copyright (c) 2016 - 2017, 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 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)
*
* @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) \
APP_USBD_HID_MOUSE_GLOBAL_DEF_INTERNAL(instance_name, \
interface_number, \
endpoint, \
bcnt, \
user_ev_handler)
/**
* @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__ */

View File

@@ -0,0 +1,119 @@
/**
* Copyright (c) 2016 - 2017, 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__ */

View File

@@ -0,0 +1,186 @@
/**
* Copyright (c) 2016 - 2017, 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 descriptors Raw descriptors buffer
* @param report_desc Report descriptor
* @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
*/
#define APP_USBD_HID_MOUSE_INST_CONFIG(descriptors, \
report_desc, \
report_buff_in, \
report_buff_out, \
user_ev_handler, \
bcnt) \
.inst = { \
.hid_inst = APP_USBD_HID_INST_CONFIG(descriptors, \
report_desc, \
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) \
static const uint8_t CONCAT_2(instance_name, _rep_dsc)[] = \
APP_USBD_HID_MOUSE_REPORT_DSC_BUTTON(bcnt); \
static const uint8_t CONCAT_2(instance_name, _dsc)[] = \
APP_USBD_HID_MOUSE_DSC_CONFIG(interface_number, \
endpoint, \
CONCAT_2(instance_name, \
_rep_dsc)); \
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, _dsc), \
CONCAT_2(instance_name, _rep_dsc), \
CONCAT_2(instance_name, _in), \
NULL, \
user_ev_handler, \
bcnt)) \
)
/** @} */
#endif /* APP_USBD_HID_MOUSE_INTERNAL_H__ */