123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607 |
- #ifndef APP_USBD_HID_H__
- #define APP_USBD_HID_H__
- #include <stdint.h>
- #include <stdbool.h>
- #include "sdk_common.h"
- #include "nrf_atomic.h"
- #include "app_usbd_hid_types.h"
- #include "app_usbd.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- #define APP_USBD_HID_IFACE_IDX 0
- #define APP_USBD_HID_EPIN_IDX 0
- #define APP_USBD_HID_EPOUT_IDX 1
- typedef enum {
- APP_USBD_HID_STATE_FLAG_APPENDED = 0,
- APP_USBD_HID_STATE_FLAG_STARTED = 1,
- APP_USBD_HID_STATE_FLAG_SUSPENDED = 2,
- APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS = 3,
- } app_usbd_hid_state_flag_t;
- typedef enum {
- APP_USBD_HID_USER_EVT_SET_BOOT_PROTO,
- APP_USBD_HID_USER_EVT_SET_REPORT_PROTO,
- APP_USBD_HID_USER_EVT_OUT_REPORT_READY,
- APP_USBD_HID_USER_EVT_IN_REPORT_DONE,
- APP_USBD_HID_USER_EVT_FEATURE_REPORT_READY,
- } app_usbd_hid_user_event_t;
- typedef void (*app_usbd_hid_user_ev_handler_t)(app_usbd_class_inst_t const * p_inst,
- app_usbd_hid_user_event_t event);
- typedef ret_code_t (*app_usbd_hid_idle_handler_t)(app_usbd_class_inst_t const * p_inst,
- uint8_t report_id);
- typedef struct {
-
- ret_code_t (*on_get_report)(app_usbd_class_inst_t const * p_inst,
- app_usbd_setup_evt_t const * p_setup_ev);
-
- ret_code_t (*on_set_report)(app_usbd_class_inst_t const * p_inst,
- app_usbd_setup_evt_t const * p_setup_ev);
-
- ret_code_t (*ep_transfer_in)(app_usbd_class_inst_t const * p_inst);
-
- ret_code_t (*ep_transfer_out)(app_usbd_class_inst_t const * p_inst);
-
- uint8_t (*subclass_count)(app_usbd_class_inst_t const * p_inst);
-
- size_t (*subclass_length)(app_usbd_class_inst_t const * p_inst,
- uint8_t desc_num);
-
- const uint8_t * (*subclass_data)(app_usbd_class_inst_t const * p_inst,
- uint8_t desc_num,
- uint32_t cur_byte);
-
- ret_code_t (*on_idle)(app_usbd_class_inst_t const * p_inst, uint8_t report_id);
-
- ret_code_t (*set_idle_handler)(app_usbd_class_inst_t const * p_inst,
- app_usbd_hid_idle_handler_t handler);
- } app_usbd_hid_methods_t;
- typedef struct {
- uint8_t * p_buff;
- size_t size;
- } app_usbd_hid_report_buffer_t;
- #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)), \
- }
- #define APP_USBD_HID_GENERIC_GLOBAL_FEATURE_REP_DEF(name, rep_size) \
- static uint8_t CONCAT_2(name, _feature_buf)[(rep_size)]; \
- const app_usbd_hid_report_buffer_t name = { \
- .p_buff = CONCAT_2(name, _feature_buf), \
- .size = sizeof(CONCAT_2(name, _feature_buf)), \
- }
- typedef struct {
- size_t size;
- app_usbd_descriptor_t type;
- uint8_t const * const p_data;
- } app_usbd_hid_subclass_desc_t;
- #define APP_USBD_HID_GENERIC_SUBCLASS_REPORT_DESC(name, ...) \
- static uint8_t const CONCAT_2(name, _data)[] = \
- __VA_ARGS__ \
- ; \
- static const app_usbd_hid_subclass_desc_t name = \
- { \
- sizeof(CONCAT_2(name, _data)), \
- APP_USBD_DESCRIPTOR_REPORT, \
- CONCAT_2(name,_data) \
- }
- #define APP_USBD_HID_GENERIC_SUBCLASS_PHYSICAL_DESC(name, ...) \
- static uint8_t const CONCAT_2(name, _data)[] = \
- __VA_ARGS__ \
- ; \
- static const app_usbd_hid_subclass_desc_t name = \
- { \
- sizeof(CONCAT_2(name, _data)), \
- APP_USBD_DESCRIPTOR_PHYSICAL, \
- CONCAT_2(name,_data) \
- }
- typedef struct {
- app_usbd_hid_subclass_desc_t const ** const p_subclass_desc;
- size_t subclass_desc_count;
- app_usbd_hid_subclass_t subclass_boot;
- app_usbd_hid_protocol_t protocol;
- app_usbd_hid_report_buffer_t * p_rep_buffer_in;
- app_usbd_hid_report_buffer_t const * p_rep_buffer_out;
- app_usbd_hid_report_buffer_t const * p_rep_buffer_feature;
- app_usbd_hid_methods_t const * p_hid_methods;
- app_usbd_hid_user_ev_handler_t user_event_handler;
- uint8_t * p_ep_interval;
- } app_usbd_hid_inst_t;
- #define APP_USBD_HID_INST_CONFIG(subclass_dsc, \
- sub_boot, \
- protocl, \
- report_buff_in, \
- report_buff_out, \
- report_buff_feature, \
- user_ev_handler, \
- hid_methods, \
- ep_list) \
- { \
- .p_subclass_desc = subclass_dsc, \
- .subclass_desc_count = ARRAY_SIZE(subclass_dsc), \
- .p_rep_buffer_in = report_buff_in, \
- .p_rep_buffer_out = report_buff_out, \
- .p_rep_buffer_feature = report_buff_feature, \
- .user_event_handler = user_ev_handler, \
- .p_hid_methods = hid_methods, \
- .subclass_boot = sub_boot, \
- .protocol = protocl, \
- .p_ep_interval = ep_list \
- }
- typedef struct {
- nrf_atomic_u32_t state_flags;
- nrf_atomic_flag_t access_lock;
- uint8_t idle_rate[APP_USBD_HID_REPORT_IDLE_TABLE_SIZE];
- app_usbd_hid_protocol_select_t selected_protocol;
- app_usbd_hid_idle_handler_t idle_handler;
- uint32_t first_idle[APP_USBD_HID_REPORT_IDLE_TABLE_SIZE];
- bool lock_idle[APP_USBD_HID_REPORT_IDLE_TABLE_SIZE];
- bool idle_on;
- bool idle_id_report;
- } app_usbd_hid_ctx_t;
- 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();
- }
- 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();
- }
- static inline bool app_usbd_hid_access_lock_test(app_usbd_hid_ctx_t * p_hid_ctx)
- {
- return p_hid_ctx->access_lock != 0;
- }
- 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));
- }
- 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)));
- }
- 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;
- }
- static inline bool app_usbd_hid_trans_required(app_usbd_hid_ctx_t * p_hid_ctx)
- {
- if (app_usbd_hid_state_flag_test(p_hid_ctx, APP_USBD_HID_STATE_FLAG_SUSPENDED) != 0)
- {
- UNUSED_RETURN_VALUE(app_usbd_wakeup_req());
- return false;
- }
- return app_usbd_hid_state_flag_test(p_hid_ctx, APP_USBD_HID_STATE_FLAG_TRANS_IN_PROGRESS) == 0;
- }
- static inline bool app_usbd_hid_state_valid(app_usbd_hid_ctx_t * p_hid_ctx)
- {
-
- 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;
- }
- 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);
- app_usbd_hid_report_buffer_t * app_usbd_hid_rep_buff_in_get(app_usbd_hid_inst_t const * p_hinst);
- 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;
- }
- app_usbd_hid_report_buffer_t const * app_usbd_hid_rep_buff_feature_get(app_usbd_hid_inst_t const * p_hinst);
- static inline app_usbd_hid_protocol_select_t app_usbd_hid_selected_protocol_get(app_usbd_hid_ctx_t * p_hid_ctx)
- {
- ASSERT(p_hid_ctx);
- return p_hid_ctx->selected_protocol;
- }
- 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);
- }
- 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
|