shitty start to usb hid
This commit is contained in:
parent
f0e2daa5c7
commit
593e74407d
@ -25,7 +25,40 @@ SRC_FILES += \
|
|||||||
$(SDK_ROOT)/integration/nrfx/legacy/nrf_drv_clock.c \
|
$(SDK_ROOT)/integration/nrfx/legacy/nrf_drv_clock.c \
|
||||||
$(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_rtc.c \
|
$(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_rtc.c \
|
||||||
$(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_clock.c \
|
$(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_clock.c \
|
||||||
|
\
|
||||||
|
$(SDK_ROOT)/components/libraries/button/app_button.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/fifo/app_fifo.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/scheduler/app_scheduler.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/timer/app_timer.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/uart/app_uart_fifo.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/app_usbd.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/app_usbd_core.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/class/hid/app_usbd_hid.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/class/hid/generic/app_usbd_hid_generic.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/app_usbd_string_desc.c \
|
||||||
|
$(SDK_ROOT)/external/fnmatch/fnmatch.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/hardfault/nrf52/handler/hardfault_handler_gcc.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/hardfault/hardfault_implementation.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/atomic_fifo/nrf_atfifo.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/atomic/nrf_atomic.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/balloc/nrf_balloc.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/experimental_memobj/nrf_memobj.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/pwr_mgmt/nrf_pwr_mgmt.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/queue/nrf_queue.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/experimental_ringbuf/nrf_ringbuf.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/experimental_section_vars/nrf_section_iter.c \
|
||||||
|
$(SDK_ROOT)/integration/nrfx/legacy/nrf_drv_power.c \
|
||||||
|
$(SDK_ROOT)/components/drivers_nrf/usbd/nrf_drv_usbd.c \
|
||||||
|
$(SDK_ROOT)/components/drivers_nrf/nrf_soc_nosd/nrf_nvic.c \
|
||||||
|
$(SDK_ROOT)/components/drivers_nrf/nrf_soc_nosd/nrf_soc.c \
|
||||||
|
$(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_gpiote.c \
|
||||||
|
$(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_power.c \
|
||||||
|
$(SDK_ROOT)/modules/nrfx/drivers/src/nrfx_power_clock.c \
|
||||||
|
$(SDK_ROOT)/modules/nrfx/drivers/src/prs/nrfx_prs.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/bsp/bsp.c \
|
||||||
|
$(SDK_ROOT)/components/libraries/bsp/bsp_cli.c \
|
||||||
|
|
||||||
|
|
||||||
# Include folders common to all targets
|
# Include folders common to all targets
|
||||||
@ -51,7 +84,47 @@ INC_FOLDERS += \
|
|||||||
$(SDK_ROOT)/components/libraries/util \
|
$(SDK_ROOT)/components/libraries/util \
|
||||||
$(SDK_ROOT)/integration/nrfx/legacy \
|
$(SDK_ROOT)/integration/nrfx/legacy \
|
||||||
$(SDK_ROOT)/modules/nrfx/drivers/include \
|
$(SDK_ROOT)/modules/nrfx/drivers/include \
|
||||||
|
\
|
||||||
|
$(SDK_ROOT)/components/libraries/cli \
|
||||||
|
$(SDK_ROOT)/components/libraries/scheduler \
|
||||||
|
$(SDK_ROOT)/components/libraries/experimental_log \
|
||||||
|
$(SDK_ROOT)/components/libraries/queue \
|
||||||
|
$(SDK_ROOT)/components/libraries/pwr_mgmt \
|
||||||
|
$(SDK_ROOT)/components/libraries/fifo \
|
||||||
|
$(SDK_ROOT)/components/toolchain/cmsis/include \
|
||||||
|
$(SDK_ROOT)/components/libraries/timer \
|
||||||
|
$(SDK_ROOT)/components/libraries/bsp \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/class/hid/generic \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/class/hid/kbd \
|
||||||
|
$(SDK_ROOT)/components/libraries/balloc \
|
||||||
|
$(SDK_ROOT)/components/drivers_nrf/usbd \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/class/hid \
|
||||||
|
$(SDK_ROOT)/components/libraries/hardfault/nrf52 \
|
||||||
|
$(SDK_ROOT)/components/libraries/cli/uart \
|
||||||
|
$(SDK_ROOT)/components/libraries/hardfault \
|
||||||
|
$(SDK_ROOT)/components/libraries/uart \
|
||||||
|
$(SDK_ROOT)/external/fnmatch \
|
||||||
|
$(SDK_ROOT)/components/libraries/button \
|
||||||
|
$(SDK_ROOT)/components/libraries/experimental_section_vars \
|
||||||
|
$(SDK_ROOT)/integration/nrfx/legacy \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd \
|
||||||
|
$(SDK_ROOT)/components/libraries/mutex \
|
||||||
|
$(PROJ_DIR) \
|
||||||
|
$(SDK_ROOT)/components/libraries/experimental_log/src \
|
||||||
|
$(SDK_ROOT)/components/libraries/delay \
|
||||||
|
$(SDK_ROOT)/external/segger_rtt \
|
||||||
|
$(SDK_ROOT)/components/libraries/atomic_fifo \
|
||||||
|
$(SDK_ROOT)/components/libraries/experimental_ringbuf \
|
||||||
|
$(SDK_ROOT)/components/libraries/atomic \
|
||||||
|
$(SDK_ROOT)/components/boards \
|
||||||
|
$(SDK_ROOT)/components/libraries/experimental_memobj \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/config \
|
||||||
|
$(SDK_ROOT)/integration/nrfx \
|
||||||
|
$(SDK_ROOT)/components/drivers_nrf/nrf_soc_nosd \
|
||||||
|
$(SDK_ROOT)/components/libraries/usbd/class/hid/mouse \
|
||||||
|
$(SDK_ROOT)/modules/nrfx/drivers/include \
|
||||||
|
$(SDK_ROOT)/modules/nrfx/hal \
|
||||||
|
$(SDK_ROOT)/external/fprintf \
|
||||||
|
|
||||||
# Libraries common to all targets
|
# Libraries common to all targets
|
||||||
LIB_FILES += \
|
LIB_FILES += \
|
||||||
@ -64,7 +137,7 @@ OPT = -O3 -g3
|
|||||||
# C flags common to all targets
|
# C flags common to all targets
|
||||||
CFLAGS += $(OPT)
|
CFLAGS += $(OPT)
|
||||||
CFLAGS += -DBOARD_PCA10056
|
CFLAGS += -DBOARD_PCA10056
|
||||||
CFLAGS += -DBSP_DEFINES_ONLY
|
#CFLAGS += -DBSP_DEFINES_ONLY
|
||||||
CFLAGS += -DCONFIG_GPIO_AS_PINRESET
|
CFLAGS += -DCONFIG_GPIO_AS_PINRESET
|
||||||
CFLAGS += -DFLOAT_ABI_HARD
|
CFLAGS += -DFLOAT_ABI_HARD
|
||||||
CFLAGS += -DNRF52840_XXAA
|
CFLAGS += -DNRF52840_XXAA
|
||||||
|
548
nrf52840/main.c
548
nrf52840/main.c
@ -112,6 +112,7 @@ static void rtc_config(void)
|
|||||||
nrf_drv_rtc_enable(&rtc);
|
nrf_drv_rtc_enable(&rtc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int usb_main(void);
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
/* Configure board. */
|
/* Configure board. */
|
||||||
@ -123,6 +124,7 @@ int main(void)
|
|||||||
uint32_t count;
|
uint32_t count;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
|
usb_main();
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
@ -141,6 +143,550 @@ int main(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "nrf.h"
|
||||||
|
#include "app_util_platform.h"
|
||||||
|
#include "nrf_drv_usbd.h"
|
||||||
|
#include "nrf_drv_clock.h"
|
||||||
|
#include "nrf_gpio.h"
|
||||||
|
#include "nrf_drv_power.h"
|
||||||
|
|
||||||
|
#include "app_timer.h"
|
||||||
|
#include "app_usbd.h"
|
||||||
|
#include "app_usbd_core.h"
|
||||||
|
#include "app_usbd_hid_generic.h"
|
||||||
|
#include "app_usbd_hid_mouse.h"
|
||||||
|
#include "app_usbd_hid_kbd.h"
|
||||||
|
#include "app_error.h"
|
||||||
|
#include "bsp.h"
|
||||||
|
#include "bsp_config.h"
|
||||||
|
|
||||||
|
#include "bsp_cli.h"
|
||||||
|
#include "nrf_cli.h"
|
||||||
|
|
||||||
|
#define NRF_LOG_INFO(info) SEGGER_RTT_printf(0,"%s\r\n",info)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*@}
|
* @brief Enable USB power detection
|
||||||
|
*/
|
||||||
|
#ifndef USBD_POWER_DETECTION
|
||||||
|
#define USBD_POWER_DETECTION true
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief HID generic class interface number.
|
||||||
* */
|
* */
|
||||||
|
#define HID_GENERIC_INTERFACE 0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief HID generic class endpoint number.
|
||||||
|
* */
|
||||||
|
#define HID_GENERIC_EPIN NRF_DRV_USBD_EPIN1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Mouse speed (value sent via HID when board button is pressed).
|
||||||
|
* */
|
||||||
|
#define CONFIG_MOUSE_MOVE_SPEED (3)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Mouse move repeat time in milliseconds
|
||||||
|
*/
|
||||||
|
#define CONFIG_MOUSE_MOVE_TIME_MS (5)
|
||||||
|
|
||||||
|
|
||||||
|
/* GPIO used as LED & buttons in this example */
|
||||||
|
#define LED_USB_START (BSP_BOARD_LED_0)
|
||||||
|
#define LED_HID_REP_IN (BSP_BOARD_LED_2)
|
||||||
|
|
||||||
|
#define BTN_MOUSE_X_POS 0
|
||||||
|
#define BTN_MOUSE_Y_POS 1
|
||||||
|
#define BTN_MOUSE_LEFT 2
|
||||||
|
#define BTN_MOUSE_RIGHT 3
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Left button mask in buttons report
|
||||||
|
*/
|
||||||
|
#define HID_BTN_LEFT_MASK (1U << 0)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Right button mask in buttons report
|
||||||
|
*/
|
||||||
|
#define HID_BTN_RIGHT_MASK (1U << 1)
|
||||||
|
|
||||||
|
/* HID report layout */
|
||||||
|
#define HID_BTN_IDX 0 /**< Button bit mask position */
|
||||||
|
#define HID_X_IDX 1 /**< X offset position */
|
||||||
|
#define HID_Y_IDX 2 /**< Y offset position */
|
||||||
|
#define HID_W_IDX 3 /**< Wheel position */
|
||||||
|
#define HID_REP_SIZE 4 /**< The size of the report */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Number of reports defined in report descriptor.
|
||||||
|
*/
|
||||||
|
#define REPORT_IN_QUEUE_SIZE 1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Size of maximum output report. HID generic class will reserve
|
||||||
|
* this buffer size + 1 memory space. */
|
||||||
|
#define REPORT_OUT_MAXSIZE 0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief HID generic class endpoints count.
|
||||||
|
* */
|
||||||
|
#define HID_GENERIC_EP_COUNT 1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief List of HID generic class endpoints.
|
||||||
|
* */
|
||||||
|
#define ENDPOINT_LIST() \
|
||||||
|
( \
|
||||||
|
HID_GENERIC_EPIN \
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Additional key release events
|
||||||
|
*
|
||||||
|
* This example needs to process release events of used buttons
|
||||||
|
*/
|
||||||
|
enum {
|
||||||
|
BSP_USER_EVENT_RELEASE_0 = BSP_EVENT_KEY_LAST + 1, /**< Button 0 released */
|
||||||
|
BSP_USER_EVENT_RELEASE_1, /**< Button 1 released */
|
||||||
|
BSP_USER_EVENT_RELEASE_2, /**< Button 2 released */
|
||||||
|
BSP_USER_EVENT_RELEASE_3, /**< Button 3 released */
|
||||||
|
BSP_USER_EVENT_RELEASE_4, /**< Button 4 released */
|
||||||
|
BSP_USER_EVENT_RELEASE_5, /**< Button 5 released */
|
||||||
|
BSP_USER_EVENT_RELEASE_6, /**< Button 6 released */
|
||||||
|
BSP_USER_EVENT_RELEASE_7, /**< Button 7 released */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief HID generic mouse action types
|
||||||
|
*/
|
||||||
|
typedef enum {
|
||||||
|
HID_GENERIC_MOUSE_X,
|
||||||
|
HID_GENERIC_MOUSE_Y,
|
||||||
|
HID_GENERIC_MOUSE_BTN_LEFT,
|
||||||
|
HID_GENERIC_MOUSE_BTN_RIGHT,
|
||||||
|
} hid_generic_mouse_action_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief User event handler.
|
||||||
|
* */
|
||||||
|
static void hid_user_ev_handler(app_usbd_class_inst_t const * p_inst,
|
||||||
|
app_usbd_hid_user_event_t event);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reuse HID mouse report descriptor for HID generic class
|
||||||
|
*/
|
||||||
|
APP_USBD_HID_GENERIC_SUBCLASS_REPORT_DESC(mouse_desc,APP_USBD_HID_MOUSE_REPORT_DSC_BUTTON(2));
|
||||||
|
|
||||||
|
static const app_usbd_hid_subclass_desc_t * reps[] = {&mouse_desc};
|
||||||
|
|
||||||
|
/*lint -save -e26 -e64 -e123 -e505 -e651*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Global HID generic instance
|
||||||
|
*/
|
||||||
|
APP_USBD_HID_GENERIC_GLOBAL_DEF(m_app_hid_generic,
|
||||||
|
HID_GENERIC_INTERFACE,
|
||||||
|
hid_user_ev_handler,
|
||||||
|
ENDPOINT_LIST(),
|
||||||
|
reps,
|
||||||
|
REPORT_IN_QUEUE_SIZE,
|
||||||
|
REPORT_OUT_MAXSIZE,
|
||||||
|
APP_USBD_HID_SUBCLASS_BOOT,
|
||||||
|
APP_USBD_HID_PROTO_MOUSE);
|
||||||
|
|
||||||
|
/*lint -restore*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Mouse state
|
||||||
|
*
|
||||||
|
* Current mouse status
|
||||||
|
*/
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
int16_t acc_x; /**< Accumulated x state */
|
||||||
|
int16_t acc_y; /**< Accumulated y state */
|
||||||
|
uint8_t btn; /**< Current btn state */
|
||||||
|
uint8_t last_btn; /**< Last transfered button state */
|
||||||
|
}m_mouse_state;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Mark the ongoing transmission
|
||||||
|
*
|
||||||
|
* Marks that the report buffer is busy and cannot be used until transmission finishes
|
||||||
|
* or invalidates (by USB reset or suspend event).
|
||||||
|
*/
|
||||||
|
static bool m_report_pending;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Timer to repeat mouse move
|
||||||
|
*/
|
||||||
|
APP_TIMER_DEF(m_mouse_move_timer);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get maximal allowed accumulated value
|
||||||
|
*
|
||||||
|
* Function gets maximal value from the accumulated input.
|
||||||
|
* @sa m_mouse_state::acc_x, m_mouse_state::acc_y
|
||||||
|
*/
|
||||||
|
static int8_t hid_acc_for_report_get(int16_t acc)
|
||||||
|
{
|
||||||
|
if(acc > INT8_MAX)
|
||||||
|
{
|
||||||
|
return INT8_MAX;
|
||||||
|
}
|
||||||
|
else if(acc < INT8_MIN)
|
||||||
|
{
|
||||||
|
return INT8_MIN;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return (int8_t)(acc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Internal function that process mouse state
|
||||||
|
*
|
||||||
|
* This function checks current mouse state and tries to send
|
||||||
|
* new report if required.
|
||||||
|
* If report sending was successful it clears accumulated positions
|
||||||
|
* and mark last button state that was transfered.
|
||||||
|
*/
|
||||||
|
static void hid_generic_mouse_process_state(void)
|
||||||
|
{
|
||||||
|
if (m_report_pending)
|
||||||
|
return;
|
||||||
|
if ((m_mouse_state.acc_x != 0) ||
|
||||||
|
(m_mouse_state.acc_y != 0) ||
|
||||||
|
(m_mouse_state.btn != m_mouse_state.last_btn))
|
||||||
|
{
|
||||||
|
ret_code_t ret;
|
||||||
|
static uint8_t report[HID_REP_SIZE];
|
||||||
|
/* We have some status changed that we need to transfer */
|
||||||
|
report[HID_BTN_IDX] = m_mouse_state.btn;
|
||||||
|
report[HID_X_IDX] = (uint8_t)hid_acc_for_report_get(m_mouse_state.acc_x);
|
||||||
|
report[HID_Y_IDX] = (uint8_t)hid_acc_for_report_get(m_mouse_state.acc_y);
|
||||||
|
/* Start the transfer */
|
||||||
|
ret = app_usbd_hid_generic_in_report_set(
|
||||||
|
&m_app_hid_generic,
|
||||||
|
report,
|
||||||
|
sizeof(report));
|
||||||
|
if (ret == NRF_SUCCESS)
|
||||||
|
{
|
||||||
|
m_report_pending = true;
|
||||||
|
m_mouse_state.last_btn = report[HID_BTN_IDX];
|
||||||
|
CRITICAL_REGION_ENTER();
|
||||||
|
/* This part of the code can fail if interrupted by BSP keys processing.
|
||||||
|
* Lock interrupts to be safe */
|
||||||
|
m_mouse_state.acc_x -= (int8_t)report[HID_X_IDX];
|
||||||
|
m_mouse_state.acc_y -= (int8_t)report[HID_Y_IDX];
|
||||||
|
CRITICAL_REGION_EXIT();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief HID generic IN report send handling
|
||||||
|
* */
|
||||||
|
static void hid_generic_mouse_action(hid_generic_mouse_action_t action, int8_t param)
|
||||||
|
{
|
||||||
|
CRITICAL_REGION_ENTER();
|
||||||
|
/*
|
||||||
|
* Update mouse state
|
||||||
|
*/
|
||||||
|
switch (action)
|
||||||
|
{
|
||||||
|
case HID_GENERIC_MOUSE_X:
|
||||||
|
m_mouse_state.acc_x += param;
|
||||||
|
break;
|
||||||
|
case HID_GENERIC_MOUSE_Y:
|
||||||
|
m_mouse_state.acc_y += param;
|
||||||
|
break;
|
||||||
|
case HID_GENERIC_MOUSE_BTN_RIGHT:
|
||||||
|
if(param == 1)
|
||||||
|
{
|
||||||
|
m_mouse_state.btn |= HID_BTN_RIGHT_MASK;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_mouse_state.btn &= ~HID_BTN_RIGHT_MASK;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case HID_GENERIC_MOUSE_BTN_LEFT:
|
||||||
|
if(param == 1)
|
||||||
|
{
|
||||||
|
m_mouse_state.btn |= HID_BTN_LEFT_MASK;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_mouse_state.btn &= ~HID_BTN_LEFT_MASK;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
CRITICAL_REGION_EXIT();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Class specific event handler.
|
||||||
|
*
|
||||||
|
* @param p_inst Class instance.
|
||||||
|
* @param event Class specific event.
|
||||||
|
* */
|
||||||
|
static void hid_user_ev_handler(app_usbd_class_inst_t const * p_inst,
|
||||||
|
app_usbd_hid_user_event_t event)
|
||||||
|
{
|
||||||
|
switch (event)
|
||||||
|
{
|
||||||
|
case APP_USBD_HID_USER_EVT_OUT_REPORT_READY:
|
||||||
|
{
|
||||||
|
/* No output report defined for this example.*/
|
||||||
|
ASSERT(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case APP_USBD_HID_USER_EVT_IN_REPORT_DONE:
|
||||||
|
{
|
||||||
|
m_report_pending = false;
|
||||||
|
hid_generic_mouse_process_state();
|
||||||
|
bsp_board_led_invert(LED_HID_REP_IN);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case APP_USBD_HID_USER_EVT_SET_BOOT_PROTO:
|
||||||
|
{
|
||||||
|
NRF_LOG_INFO("SET_BOOT_PROTO");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case APP_USBD_HID_USER_EVT_SET_REPORT_PROTO:
|
||||||
|
{
|
||||||
|
NRF_LOG_INFO("SET_REPORT_PROTO");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief USBD library specific event handler.
|
||||||
|
*
|
||||||
|
* @param event USBD library event.
|
||||||
|
* */
|
||||||
|
static void usbd_user_ev_handler(app_usbd_event_type_t event)
|
||||||
|
{
|
||||||
|
switch (event)
|
||||||
|
{
|
||||||
|
case APP_USBD_EVT_DRV_SOF:
|
||||||
|
break;
|
||||||
|
case APP_USBD_EVT_DRV_RESET:
|
||||||
|
m_report_pending = false;
|
||||||
|
break;
|
||||||
|
case APP_USBD_EVT_DRV_SUSPEND:
|
||||||
|
m_report_pending = false;
|
||||||
|
app_usbd_suspend_req(); // Allow the library to put the peripheral into sleep mode
|
||||||
|
bsp_board_leds_off();
|
||||||
|
break;
|
||||||
|
case APP_USBD_EVT_DRV_RESUME:
|
||||||
|
m_report_pending = false;
|
||||||
|
bsp_board_led_on(LED_USB_START);
|
||||||
|
break;
|
||||||
|
case APP_USBD_EVT_STARTED:
|
||||||
|
m_report_pending = false;
|
||||||
|
bsp_board_led_on(LED_USB_START);
|
||||||
|
break;
|
||||||
|
case APP_USBD_EVT_STOPPED:
|
||||||
|
app_usbd_disable();
|
||||||
|
bsp_board_leds_off();
|
||||||
|
break;
|
||||||
|
case APP_USBD_EVT_POWER_DETECTED:
|
||||||
|
NRF_LOG_INFO("USB power detected");
|
||||||
|
if (!nrf_drv_usbd_is_enabled())
|
||||||
|
{
|
||||||
|
app_usbd_enable();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case APP_USBD_EVT_POWER_REMOVED:
|
||||||
|
NRF_LOG_INFO("USB power removed");
|
||||||
|
app_usbd_stop();
|
||||||
|
break;
|
||||||
|
case APP_USBD_EVT_POWER_READY:
|
||||||
|
NRF_LOG_INFO("USB ready");
|
||||||
|
app_usbd_start();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mouse_move_timer_handler(void * p_context)
|
||||||
|
{
|
||||||
|
UNUSED_PARAMETER(p_context);
|
||||||
|
bool used = false;
|
||||||
|
|
||||||
|
if (bsp_button_is_pressed(BTN_MOUSE_X_POS))
|
||||||
|
{
|
||||||
|
hid_generic_mouse_action(HID_GENERIC_MOUSE_X, CONFIG_MOUSE_MOVE_SPEED);
|
||||||
|
used = true;
|
||||||
|
}
|
||||||
|
if (bsp_button_is_pressed(BTN_MOUSE_Y_POS))
|
||||||
|
{
|
||||||
|
hid_generic_mouse_action(HID_GENERIC_MOUSE_Y, CONFIG_MOUSE_MOVE_SPEED);
|
||||||
|
used = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!used)
|
||||||
|
{
|
||||||
|
UNUSED_RETURN_VALUE(app_timer_stop(m_mouse_move_timer));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void bsp_event_callback(bsp_event_t ev)
|
||||||
|
{
|
||||||
|
switch ((unsigned int)ev)
|
||||||
|
{
|
||||||
|
case CONCAT_2(BSP_EVENT_KEY_, BTN_MOUSE_X_POS):
|
||||||
|
hid_generic_mouse_action(HID_GENERIC_MOUSE_X, CONFIG_MOUSE_MOVE_SPEED);
|
||||||
|
UNUSED_RETURN_VALUE(app_timer_start(m_mouse_move_timer, APP_TIMER_TICKS(CONFIG_MOUSE_MOVE_TIME_MS), NULL));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CONCAT_2(BSP_EVENT_KEY_, BTN_MOUSE_Y_POS):
|
||||||
|
hid_generic_mouse_action(HID_GENERIC_MOUSE_Y, CONFIG_MOUSE_MOVE_SPEED);
|
||||||
|
UNUSED_RETURN_VALUE(app_timer_start(m_mouse_move_timer, APP_TIMER_TICKS(CONFIG_MOUSE_MOVE_TIME_MS), NULL));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CONCAT_2(BSP_EVENT_KEY_, BTN_MOUSE_RIGHT):
|
||||||
|
hid_generic_mouse_action(HID_GENERIC_MOUSE_BTN_RIGHT, 1);
|
||||||
|
break;
|
||||||
|
case CONCAT_2(BSP_USER_EVENT_RELEASE_, BTN_MOUSE_RIGHT):
|
||||||
|
hid_generic_mouse_action(HID_GENERIC_MOUSE_BTN_RIGHT, -1);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CONCAT_2(BSP_EVENT_KEY_, BTN_MOUSE_LEFT):
|
||||||
|
hid_generic_mouse_action(HID_GENERIC_MOUSE_BTN_LEFT, 1);
|
||||||
|
break;
|
||||||
|
case CONCAT_2(BSP_USER_EVENT_RELEASE_, BTN_MOUSE_LEFT):
|
||||||
|
hid_generic_mouse_action(HID_GENERIC_MOUSE_BTN_LEFT, -1);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return; // no implementation needed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Auxiliary internal macro
|
||||||
|
*
|
||||||
|
* Macro used only in @ref init_bsp to simplify the configuration
|
||||||
|
*/
|
||||||
|
#define INIT_BSP_ASSIGN_RELEASE_ACTION(btn) \
|
||||||
|
APP_ERROR_CHECK( \
|
||||||
|
bsp_event_to_button_action_assign( \
|
||||||
|
btn, \
|
||||||
|
BSP_BUTTON_ACTION_RELEASE, \
|
||||||
|
(bsp_event_t)CONCAT_2(BSP_USER_EVENT_RELEASE_, btn)) \
|
||||||
|
)
|
||||||
|
|
||||||
|
static void init_bsp(void)
|
||||||
|
{
|
||||||
|
ret_code_t ret;
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
ret = bsp_init(BSP_INIT_BUTTONS, bsp_event_callback);
|
||||||
|
APP_ERROR_CHECK(ret);
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
|
||||||
|
INIT_BSP_ASSIGN_RELEASE_ACTION(BTN_MOUSE_LEFT );
|
||||||
|
INIT_BSP_ASSIGN_RELEASE_ACTION(BTN_MOUSE_RIGHT);
|
||||||
|
|
||||||
|
/* Configure LEDs */
|
||||||
|
bsp_board_init(BSP_INIT_LEDS);
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_cli(void)
|
||||||
|
{
|
||||||
|
ret_code_t ret;
|
||||||
|
ret = bsp_cli_init(bsp_event_callback);
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
APP_ERROR_CHECK(ret);
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
}
|
||||||
|
|
||||||
|
int usb_main(void)
|
||||||
|
{
|
||||||
|
ret_code_t ret;
|
||||||
|
static const app_usbd_config_t usbd_config = {
|
||||||
|
.ev_state_proc = usbd_user_ev_handler
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
/*ret = nrf_drv_clock_init();*/
|
||||||
|
/*APP_ERROR_CHECK(ret);*/
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
|
||||||
|
nrf_drv_clock_lfclk_request(NULL);
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
|
||||||
|
while(!nrf_drv_clock_lfclk_is_running())
|
||||||
|
{
|
||||||
|
/* Just waiting */
|
||||||
|
}
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
|
||||||
|
ret = app_timer_init();
|
||||||
|
APP_ERROR_CHECK(ret);
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
|
||||||
|
ret = app_timer_create(&m_mouse_move_timer, APP_TIMER_MODE_REPEATED, mouse_move_timer_handler);
|
||||||
|
APP_ERROR_CHECK(ret);
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
|
||||||
|
init_bsp();
|
||||||
|
init_cli();
|
||||||
|
NRF_LOG_INFO("Hello USB!");
|
||||||
|
SEGGER_RTT_printf(0, "USB %d\n", __LINE__);
|
||||||
|
|
||||||
|
ret = app_usbd_init(&usbd_config);
|
||||||
|
APP_ERROR_CHECK(ret);
|
||||||
|
|
||||||
|
NRF_LOG_INFO("USBD HID generic example started.");
|
||||||
|
|
||||||
|
app_usbd_class_inst_t const * class_inst_generic;
|
||||||
|
class_inst_generic = app_usbd_hid_generic_class_inst_get(&m_app_hid_generic);
|
||||||
|
ret = app_usbd_class_append(class_inst_generic);
|
||||||
|
APP_ERROR_CHECK(ret);
|
||||||
|
|
||||||
|
if (USBD_POWER_DETECTION)
|
||||||
|
{
|
||||||
|
ret = app_usbd_power_events_enable();
|
||||||
|
APP_ERROR_CHECK(ret);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
|
||||||
|
|
||||||
|
app_usbd_enable();
|
||||||
|
app_usbd_start();
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
while (app_usbd_event_queue_process())
|
||||||
|
{
|
||||||
|
/* Nothing to do */
|
||||||
|
}
|
||||||
|
hid_generic_mouse_process_state();
|
||||||
|
/*nrf_cli_process(&m_cli_uart);*/
|
||||||
|
|
||||||
|
UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
|
||||||
|
/* Sleep CPU only if there was no interrupt since last loop processing */
|
||||||
|
__WFE();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1335,7 +1335,7 @@
|
|||||||
// <e> GPIOTE_ENABLED - nrf_drv_gpiote - GPIOTE peripheral driver - legacy layer
|
// <e> GPIOTE_ENABLED - nrf_drv_gpiote - GPIOTE peripheral driver - legacy layer
|
||||||
//==========================================================
|
//==========================================================
|
||||||
#ifndef GPIOTE_ENABLED
|
#ifndef GPIOTE_ENABLED
|
||||||
#define GPIOTE_ENABLED 0
|
#define GPIOTE_ENABLED 1
|
||||||
#endif
|
#endif
|
||||||
// <o> GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS - Number of lower power input pins
|
// <o> GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS - Number of lower power input pins
|
||||||
#ifndef GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS
|
#ifndef GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS
|
||||||
@ -4537,7 +4537,7 @@
|
|||||||
// <e> POWER_ENABLED - nrf_drv_power - POWER peripheral driver - legacy layer
|
// <e> POWER_ENABLED - nrf_drv_power - POWER peripheral driver - legacy layer
|
||||||
//==========================================================
|
//==========================================================
|
||||||
#ifndef POWER_ENABLED
|
#ifndef POWER_ENABLED
|
||||||
#define POWER_ENABLED 0
|
#define POWER_ENABLED 1
|
||||||
#endif
|
#endif
|
||||||
// <o> POWER_CONFIG_IRQ_PRIORITY - Interrupt priority
|
// <o> POWER_CONFIG_IRQ_PRIORITY - Interrupt priority
|
||||||
|
|
||||||
@ -5619,7 +5619,7 @@
|
|||||||
// <e> USBD_ENABLED - nrf_drv_usbd - USB driver
|
// <e> USBD_ENABLED - nrf_drv_usbd - USB driver
|
||||||
//==========================================================
|
//==========================================================
|
||||||
#ifndef USBD_ENABLED
|
#ifndef USBD_ENABLED
|
||||||
#define USBD_ENABLED 0
|
#define USBD_ENABLED 1
|
||||||
#endif
|
#endif
|
||||||
// <o> USBD_CONFIG_IRQ_PRIORITY - Interrupt priority
|
// <o> USBD_CONFIG_IRQ_PRIORITY - Interrupt priority
|
||||||
|
|
||||||
@ -5793,7 +5793,7 @@
|
|||||||
// <e> APP_TIMER_ENABLED - app_timer - Application timer functionality
|
// <e> APP_TIMER_ENABLED - app_timer - Application timer functionality
|
||||||
//==========================================================
|
//==========================================================
|
||||||
#ifndef APP_TIMER_ENABLED
|
#ifndef APP_TIMER_ENABLED
|
||||||
#define APP_TIMER_ENABLED 0
|
#define APP_TIMER_ENABLED 1
|
||||||
#endif
|
#endif
|
||||||
// <o> APP_TIMER_CONFIG_RTC_FREQUENCY - Configure RTC prescaler.
|
// <o> APP_TIMER_CONFIG_RTC_FREQUENCY - Configure RTC prescaler.
|
||||||
|
|
||||||
@ -5891,7 +5891,7 @@
|
|||||||
// <e> APP_USBD_ENABLED - app_usbd - USB Device library
|
// <e> APP_USBD_ENABLED - app_usbd - USB Device library
|
||||||
//==========================================================
|
//==========================================================
|
||||||
#ifndef APP_USBD_ENABLED
|
#ifndef APP_USBD_ENABLED
|
||||||
#define APP_USBD_ENABLED 0
|
#define APP_USBD_ENABLED 1
|
||||||
#endif
|
#endif
|
||||||
// <o> APP_USBD_VID - Vendor ID <0x0000-0xFFFF>
|
// <o> APP_USBD_VID - Vendor ID <0x0000-0xFFFF>
|
||||||
|
|
||||||
@ -6058,28 +6058,28 @@
|
|||||||
|
|
||||||
|
|
||||||
#ifndef APP_USBD_HID_ENABLED
|
#ifndef APP_USBD_HID_ENABLED
|
||||||
#define APP_USBD_HID_ENABLED 0
|
#define APP_USBD_HID_ENABLED 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// <q> APP_USBD_HID_GENERIC_ENABLED - app_usbd_hid_generic - USB HID generic
|
// <q> APP_USBD_HID_GENERIC_ENABLED - app_usbd_hid_generic - USB HID generic
|
||||||
|
|
||||||
|
|
||||||
#ifndef APP_USBD_HID_GENERIC_ENABLED
|
#ifndef APP_USBD_HID_GENERIC_ENABLED
|
||||||
#define APP_USBD_HID_GENERIC_ENABLED 0
|
#define APP_USBD_HID_GENERIC_ENABLED 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// <q> APP_USBD_HID_KBD_ENABLED - app_usbd_hid_kbd - USB HID keyboard
|
// <q> APP_USBD_HID_KBD_ENABLED - app_usbd_hid_kbd - USB HID keyboard
|
||||||
|
|
||||||
|
|
||||||
#ifndef APP_USBD_HID_KBD_ENABLED
|
#ifndef APP_USBD_HID_KBD_ENABLED
|
||||||
#define APP_USBD_HID_KBD_ENABLED 0
|
#define APP_USBD_HID_KBD_ENABLED 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// <q> APP_USBD_HID_MOUSE_ENABLED - app_usbd_hid_mouse - USB HID mouse
|
// <q> APP_USBD_HID_MOUSE_ENABLED - app_usbd_hid_mouse - USB HID mouse
|
||||||
|
|
||||||
|
|
||||||
#ifndef APP_USBD_HID_MOUSE_ENABLED
|
#ifndef APP_USBD_HID_MOUSE_ENABLED
|
||||||
#define APP_USBD_HID_MOUSE_ENABLED 0
|
#define APP_USBD_HID_MOUSE_ENABLED 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// <q> APP_USBD_MSC_ENABLED - app_usbd_msc - USB MSC class
|
// <q> APP_USBD_MSC_ENABLED - app_usbd_msc - USB MSC class
|
||||||
@ -6844,7 +6844,7 @@
|
|||||||
// <e> NRF_QUEUE_ENABLED - nrf_queue - Queue module
|
// <e> NRF_QUEUE_ENABLED - nrf_queue - Queue module
|
||||||
//==========================================================
|
//==========================================================
|
||||||
#ifndef NRF_QUEUE_ENABLED
|
#ifndef NRF_QUEUE_ENABLED
|
||||||
#define NRF_QUEUE_ENABLED 0
|
#define NRF_QUEUE_ENABLED 1
|
||||||
#endif
|
#endif
|
||||||
// <q> NRF_QUEUE_CLI_CMDS - Enable CLI commands specific to the module
|
// <q> NRF_QUEUE_CLI_CMDS - Enable CLI commands specific to the module
|
||||||
|
|
||||||
@ -6948,7 +6948,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#ifndef BUTTON_ENABLED
|
#ifndef BUTTON_ENABLED
|
||||||
#define BUTTON_ENABLED 0
|
#define BUTTON_ENABLED 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// <q> BUTTON_HIGH_ACCURACY_ENABLED - Enables GPIOTE high accuracy for buttons
|
// <q> BUTTON_HIGH_ACCURACY_ENABLED - Enables GPIOTE high accuracy for buttons
|
||||||
|
Loading…
x
Reference in New Issue
Block a user