<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Conflict between app_usbd_init(&amp;amp;usbd_config) and  app_error_check ().</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/88885/conflict-between-app_usbd_init-usbd_config-and-app_error_check</link><description>https://devzone.nordicsemi.com/f/nordic-q-a/69596/device-initialization-problems/285611#285611 
 had gone through above thread. 
 
 I am struck exactly at this point. 
 i am trying to combine USBD and USBD_Audio wth nrf52840 DK, SDK 17.1.0. 
 /* USB work</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 14 Jun 2022 04:00:04 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/88885/conflict-between-app_usbd_init-usbd_config-and-app_error_check" /><item><title>RE: Conflict between app_usbd_init(&amp;usbd_config) and  app_error_check ().</title><link>https://devzone.nordicsemi.com/thread/372252?ContentTypeID=1</link><pubDate>Tue, 14 Jun 2022 04:00:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7a3a47a1-b16c-418f-ba70-7964a58f91ee</guid><dc:creator>Deep_Bang</dc:creator><description>&lt;p&gt;Ok, Thank you Jorgen for the support .&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Deep&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Conflict between app_usbd_init(&amp;usbd_config) and  app_error_check ().</title><link>https://devzone.nordicsemi.com/thread/372177?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2022 13:42:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:be048fe8-3c94-4b4b-a1e3-2f5ad05bdebf</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The app_usbd library does not support co-existence with other users of nrf_drv_usbd. If you want to use these together, you need to rewrite the app_usbd library to pass the event handler from nrf_drv_usbd_init to the library and remove the call to &lt;span&gt;nrf_drv_usbd_init in&amp;nbsp;app_usbd_init.&lt;/span&gt;&amp;nbsp;You then need to pass along relevant events from nrf_drv_usbd event handler to the library in your own application (e.g. call the event handler internally in app_usbd from &lt;strong&gt;&lt;/strong&gt;&lt;span&gt;nrf_drv_usbd_init in your application with all events)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not aware of any examples that is showing how to do this.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Jørgen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Conflict between app_usbd_init(&amp;usbd_config) and  app_error_check ().</title><link>https://devzone.nordicsemi.com/thread/372105?ContentTypeID=1</link><pubDate>Mon, 13 Jun 2022 11:09:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:db642f71-144e-4c69-bd9c-906e6050d965</guid><dc:creator>Deep_Bang</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stddef.h&amp;gt;

#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;app_util.h&amp;quot;
#include &amp;quot;nrf_drv_usbd.h&amp;quot;
#include &amp;quot;nrf_drv_clock.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;nrf_drv_power.h&amp;quot;

#include &amp;quot;app_usbd.h&amp;quot;
#include &amp;quot;app_usbd_core.h&amp;quot;
#include &amp;quot;app_usbd_string_desc.h&amp;quot;
#include &amp;quot;app_usbd_audio.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;


#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;bsp.h&amp;quot;
//#include &amp;quot;bsp_cli.h&amp;quot;
//#include &amp;quot;nrf_cli.h&amp;quot;
//#include &amp;quot;nrf_cli_uart.h&amp;quot;

/**@file
 * @defgroup usbd_aduio_example main.c
 * @{
 * @ingroup usbd_audio_example
 * @brief USBD Audio class example
 *
 */

/*#define LED_USB_RESUME (BSP_BOARD_LED_0)
#define LED_USB_START  (BSP_BOARD_LED_1)
#define LED_AUDIO_RX   (BSP_BOARD_LED_2)
#define LED_AUDIO_TX   (BSP_BOARD_LED_3)
*/
/**
 * @brief USB audio samples size
 */
#define BUFFER_SIZE  (48)

/**
 * @brief Enable power USB detection
 *
 * Configure if example supports USB port connection
 */
#ifndef USBD_POWER_DETECTION
#define USBD_POWER_DETECTION true
#endif

static bool m_send_flag = 0;

#define BTN_DATA_SEND               0
#define BTN_DATA_KEY_RELEASE        (bsp_event_t)(BSP_EVENT_KEY_LAST + 1)
/**
 * @brief Button used to simulate mouse move
 *
 * Every button press would move the cursor one step in the square.
 */
#define BTN_MOUSE_MOVE BSP_BOARD_BUTTON_0
/**
 * @brief Button for system OFF request
 *
 * This button would set the request for system OFF.
 */
#define BTN_SYSTEM_OFF BSP_BOARD_BUTTON_1

/**
 * @brief Configuration status LED
 *
 * This LED would blink quickly (5&amp;#160;Hz) when device is not configured
 * or slowly (1&amp;#160;Hz) when configured and working properly.
 */
#define LED_USB_STATUS BSP_BOARD_LED_0
/**
 * @brief Power detect LED
 *
 * The LED is ON when connection is detected on USB port.
 * It is turned off when connection is removed.
 */
#define LED_USB_POWER BSP_BOARD_LED_1

/**
 * @brief Running LED
 *
 * LED that turns on when program is not sleeping
 */
#define LED_RUNNING BSP_BOARD_LED_2

/**
 * @brief Active LED
 *
 * LED that turns on when program is not in system OFF
 */
#define LED_ACTIVE BSP_BOARD_LED_3

/**
 * @brief Enable power USB detection
 *
 * Configure if example supports USB port connection
 */
 
 #define STARTUP_DELAY 50
 
 /** Maximum size of the packed transfered by EP0 */
#define EP0_MAXPACKETSIZE NRF_DRV_USBD_EPSIZE

/** Device descriptor */
#define USBD_DEVICE_DESCRIPTOR \
    0x12,                        /* bLength | size of descriptor                                                  */\
    0x01,                        /* bDescriptorType | descriptor type                                             */\
    0x00, 0x02,                  /* bcdUSB | USB spec release (ver 2.0)                                           */\
    0x00,                        /* bDeviceClass &amp;#166; class code (each interface specifies class information)        */\
    0x00,                        /* bDeviceSubClass &amp;#166; device sub-class (must be set to 0 because class code is 0) */\
    0x00,                        /* bDeviceProtocol | device protocol (no class specific protocol)                */\
    EP0_MAXPACKETSIZE,           /* bMaxPacketSize0 | maximum packet size (64 bytes)                              */\
    0x15, 0x19,                  /* vendor ID  (0x1915 Nordic)                                                    */\
    0x0A, 0x52,                  /* product ID (0x520A nRF52 HID mouse on nrf_drv)                                */\
    0x01, 0x01,                  /* bcdDevice | final device release number in BCD Format                         */\
    USBD_STRING_MANUFACTURER_IX, /* iManufacturer | index of manufacturer string                                  */\
    USBD_STRING_PRODUCT_IX,      /* iProduct | index of product string                                            */\
    USBD_STRING_SERIAL_IX,       /* iSerialNumber | Serial Number string                                          */\
    0x01                         /* bNumConfigurations | number of configurations                                 */

/** Configuration descriptor */
#define DEVICE_SELF_POWERED 1
#define REMOTE_WU           1

#define USBD_CONFIG_DESCRIPTOR_SIZE   9
#define USBD_CONFIG_DESCRIPTOR_FULL_SIZE   (9 + (9 + 9 + 7))
#define USBD_CONFIG_DESCRIPTOR  \
    0x09,         /* bLength | length of descriptor                                             */\
    0x02,         /* bDescriptorType | descriptor type (CONFIGURATION)                          */\
    USBD_CONFIG_DESCRIPTOR_FULL_SIZE, 0x00,    /* wTotalLength | total length of descriptor(s)  */\
    0x01,         /* bNumInterfaces                                                             */\
    0x01,         /* bConfigurationValue                                                        */\
    0x00,         /* index of string Configuration | configuration string index (not supported) */\
    0x80| (((DEVICE_SELF_POWERED) ? 1U:0U)&amp;lt;&amp;lt;6) | (((REMOTE_WU) ? 1U:0U)&amp;lt;&amp;lt;5), /* bmAttributes    */\
    49            /* maximum power in steps of 2mA (98mA)                                       */

#define USBD_INTERFACE0_DESCRIPTOR  \
    0x09,         /* bLength                                                                          */\
    0x04,         /* bDescriptorType | descriptor type (INTERFACE)                                    */\
    0x00,         /* bInterfaceNumber                                                                 */\
    0x00,         /* bAlternateSetting                                                                */\
    0x01,         /* bNumEndpoints | number of endpoints (1)                                          */\
    0x03,         /* bInterfaceClass | interface class (3..defined by USB spec: HID)                  */\
    0x00,         /* bInterfaceSubClass |interface sub-class (0.. no boot interface)                  */\
    0x02,         /* bInterfaceProtocol | interface protocol (1..defined by USB spec: mouse)          */\
    0x00          /* interface string index (not supported)                                           */

/**
 * HID Table must normally be between Interface and EndPoint Descriptor
 * as written in HID spec&amp;#167;7.1 but it doesn&amp;#39;t work with OSR2.1
 */
#define USBD_HID0_DESCRIPTOR  \
    0x09,         /* bLength | length of descriptor (9 bytes)                    */\
    0x21,         /* bHIDDescriptor | descriptor type (HID)                      */\
    0x11, 0x01,   /* HID wBcdHID | Spec version 01.11                            */\
    0x00,         /* bCountryCode | HW Target country                            */\
    0x01,         /* bNumDescriptors | Number of HID class descriptors to follow */\
    0x22,         /* bDescriptorType | Report descriptor type is 0x22 (report)   */\
    (uint8_t)(USBD_MOUSE_REPORT_DESCRIPTOR_SIZE),      /* Total length of Report descr., low byte */ \
    (uint8_t)(USBD_MOUSE_REPORT_DESCRIPTOR_SIZE / 256) /* Total length of Report descr., high byte */

#define USBD_ENDPOINT1_DESCRIPTOR  \
    0x07,         /* bLength | length of descriptor (7 bytes)                                     */\
    0x05,         /* bDescriptorType | descriptor type (ENDPOINT)                                 */\
    0x81,         /* bEndpointAddress | endpoint address (IN endpoint, endpoint 1)                */\
    0x03,         /* bmAttributes | endpoint attributes (interrupt)                               */\
    0x08,0x00,    /* bMaxPacketSizeLowByte,bMaxPacketSizeHighByte | maximum packet size (8 bytes) */\
    0x08          /* bInterval | polling interval (10ms)                                          */


/**
 * String config descriptor
 */
#define USBD_STRING_LANG_IX  0x00
#define USBD_STRING_LANG \
    0x04,         /* length of descriptor                   */\
    0x03,         /* descriptor type                        */\
    0x09,         /*                                        */\
    0x04          /* Supported LangID = 0x0409 (US-English) */

#define USBD_STRING_MANUFACTURER_IX  0x01
#define USBD_STRING_MANUFACTURER \
    42,           /* length of descriptor (? bytes)   */\
    0x03,         /* descriptor type                  */\
    &amp;#39;N&amp;#39;, 0x00,    /* Define Unicode String &amp;quot;Nordic Semiconductor  */\
    &amp;#39;o&amp;#39;, 0x00, \
    &amp;#39;r&amp;#39;, 0x00, \
    &amp;#39;d&amp;#39;, 0x00, \
    &amp;#39;i&amp;#39;, 0x00, \
    &amp;#39;c&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;S&amp;#39;, 0x00, \
    &amp;#39;e&amp;#39;, 0x00, \
    &amp;#39;m&amp;#39;, 0x00, \
    &amp;#39;i&amp;#39;, 0x00, \
    &amp;#39;c&amp;#39;, 0x00, \
    &amp;#39;o&amp;#39;, 0x00, \
    &amp;#39;n&amp;#39;, 0x00, \
    &amp;#39;d&amp;#39;, 0x00, \
    &amp;#39;u&amp;#39;, 0x00, \
    &amp;#39;c&amp;#39;, 0x00, \
    &amp;#39;t&amp;#39;, 0x00, \
    &amp;#39;o&amp;#39;, 0x00, \
    &amp;#39;r&amp;#39;, 0x00

#define USBD_STRING_PRODUCT_IX  0x02
#define USBD_STRING_PRODUCT \
    72,           /* length of descriptor (? bytes)         */\
    0x03,         /* descriptor type                        */\
    &amp;#39;n&amp;#39;, 0x00,    /* generic unicode string for all devices */\
    &amp;#39;R&amp;#39;, 0x00, \
    &amp;#39;F&amp;#39;, 0x00, \
    &amp;#39;5&amp;#39;, 0x00, \
    &amp;#39;2&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;U&amp;#39;, 0x00, \
    &amp;#39;S&amp;#39;, 0x00, \
    &amp;#39;B&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;H&amp;#39;, 0x00, \
    &amp;#39;I&amp;#39;, 0x00, \
    &amp;#39;D&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;m&amp;#39;, 0x00, \
    &amp;#39;o&amp;#39;, 0x00, \
    &amp;#39;u&amp;#39;, 0x00, \
    &amp;#39;s&amp;#39;, 0x00, \
    &amp;#39;e&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;o&amp;#39;, 0x00, \
    &amp;#39;n&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;n&amp;#39;, 0x00, \
    &amp;#39;r&amp;#39;, 0x00, \
    &amp;#39;f&amp;#39;, 0x00, \
    &amp;#39;_&amp;#39;, 0x00, \
    &amp;#39;d&amp;#39;, 0x00, \
    &amp;#39;r&amp;#39;, 0x00, \
    &amp;#39;v&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;D&amp;#39;, 0x00, \
    &amp;#39;e&amp;#39;, 0x00, \
    &amp;#39;m&amp;#39;, 0x00, \
    &amp;#39;o&amp;#39;, 0x00, \

#define USBD_STRING_SERIAL_IX  0x00

#define USBD_MOUSE_REPORT_DESCRIPTOR_SIZE  46
#define USBD_MOUSE_REPORT_DESCRIPTOR \
    0x05, 0x01,     /* usage page (generic desktop). Global item, applies to all subsequent items   */\
    0x09, 0x02,     /* usage (mouse). Local item                                                    */\
    0xA1, 0x01,     /* collection (application)                                                     */\
    0x09, 0x01,     /* usage (pointer)                                                              */\
    0xA1, 0x00,     /* collection (physical)                                                        */\
    0x05, 0x09,     /*   usage page (buttons). Global item, applies to all subsequent items         */\
    0x19, 0x01,     /*   usage minimum (1)                                                          */\
    0x29, 0x08,     /*   usage maximum (8)                                                          */\
    0x15, 0x00,     /*   logical minimum (0)                                                        */\
    0x25, 0x01,     /*   logical maximum (1)                                                        */\
    0x95, 0x08,     /*   report count (8)                                                           */\
    0x75, 0x01,     /*   report size (1)                                                            */\
    0x81, 0x02,     /*   input (data, var, abs)                                                     */\
    0x05, 0x01,     /*   usage page (generic desktop). Global item, applies to all subsequent items */\
    0x15, 0x81,     /*   logical minimum (-127)                                                     */\
    0x25, 0x7F,     /*   logical maximum (127)                                                      */\
    0x75, 0x08,     /*   report size (8)                                                            */\
    0x09, 0x30,     /*   usage (X)                                                                  */\
    0x09, 0x31,     /*   usage (Y)                                                                  */\
    0x09, 0x38,     /*   usage wheel                                                                */\
    0x95, 0x03,     /*   report count (3)                                                           */\
    0x81, 0x06,     /*   input (3 position bytes X, Y &amp;amp; roller)                                     */\
    0xC0,           /* end collection                                                               */\
    0xC0            /* End Collection                                                               */


static const uint8_t get_descriptor_device[] = {
    USBD_DEVICE_DESCRIPTOR
};

static const uint8_t get_descriptor_configuration[] = {
    USBD_CONFIG_DESCRIPTOR,
    USBD_INTERFACE0_DESCRIPTOR,
    USBD_HID0_DESCRIPTOR,
    USBD_ENDPOINT1_DESCRIPTOR
};
static const uint8_t get_descriptor_string_lang[] = {
    USBD_STRING_LANG
};
static const uint8_t get_descriptor_string_manuf[] = {
    USBD_STRING_MANUFACTURER
};
static const uint8_t get_descriptor_string_prod[] = {
    USBD_STRING_PRODUCT
};
static const uint8_t get_descriptor_report_interface_0[] = {
    USBD_MOUSE_REPORT_DESCRIPTOR
};

static const uint8_t get_config_resp_configured[]   = {1};
static const uint8_t get_config_resp_unconfigured[] = {0};

static const uint8_t get_status_device_resp_nrwu[] = {
    ((DEVICE_SELF_POWERED) ? 1 : 0), //LSB first: self-powered, no remoteWk
    0
};
static const uint8_t get_status_device_resp_rwu[]  = {
    ((DEVICE_SELF_POWERED) ? 1 : 0) | 2, //LSB first: self-powered, remoteWk
    0
};

static const uint8_t get_status_interface_resp[] = {0, 0};
static const uint8_t get_status_ep_halted_resp[] = {1, 0};
static const uint8_t get_status_ep_active_resp[] = {0, 0};


#define GET_CONFIG_DESC_SIZE    sizeof(get_descriptor_configuration)
#define GET_INTERFACE_DESC_SIZE 9
#define GET_HID_DESC_SIZE       9
#define GET_ENDPOINT_DESC_SIZE  7

#define get_descriptor_interface_0 \
    &amp;amp;get_descriptor_configuration[9]
#define get_descriptor_hid_0       \
    &amp;amp;get_descriptor_configuration[9+GET_INTERFACE_DESC_SIZE]
#define get_descriptor_endpoint_1  \
    &amp;amp;get_descriptor_configuration[9+GET_INTERFACE_DESC_SIZE+GET_HID_DESC_SIZE]

/**
 * @brief USB configured flag
 *
 * The flag that is used to mark the fact that USB is configured and ready
 * to transmit data
 */
static volatile bool m_usbd_configured = false;

/**
 * @brief USB suspended
 *
 * The flag that is used to mark the fact that USB is suspended and requires wake up
 * if new data is available.
 *
 * @note This variable is changed from the main loop.
 */
static bool m_usbd_suspended = false;

/**
 * @brief Mark the fact if remote wake up is enabled
 *
 * The internal flag that marks if host enabled the remote wake up functionality in this device.
 */
static
#if REMOTE_WU
    volatile // Disallow optimization only if Remote wakeup is enabled
#endif
bool m_usbd_rwu_enabled = false;

/**
 * @brief Current mouse position
 *
 * The index of current mouse position that would be changed to real offset.
 */
static volatile uint8_t m_mouse_position = 0;

/**
 * @brief The flag for mouse position send pending
 *
 * Setting this flag means that USB endpoint is busy by sending
 * last mouse position.
 */
static volatile bool m_send_mouse_position = false;

/**
 * @brief The requested suspend state
 *
 * The currently requested suspend state based on the events
 * received from USBD library.
 * If the value here is different than the @ref m_usbd_suspended
 * the state changing would be processed inside main loop.
 */
static volatile bool m_usbd_suspend_state_req = false;

/**
 * @brief System OFF request flag
 *
 * This flag is used in button event processing and marks the fact that
 * system OFF should be activated from main loop.
 */
static volatile bool m_system_off_req = false;


/**
 * @brief Setup all the endpoints for selected configuration
 *
 * Function sets all the endpoints for specific configuration.
 *
 * @note
 * Setting the configuration index 0 means technically disabling the HID interface.
 * Such configuration should be set when device is starting or USB reset is detected.
 *
 * @param index Configuration index
 *
 * @retval NRF_ERROR_INVALID_PARAM Invalid configuration
 * @retval NRF_SUCCESS             Configuration successfully set
 */
static ret_code_t ep_configuration(uint8_t index)
{
    if ( index == 1 )
    {
        nrf_drv_usbd_ep_dtoggle_clear(NRF_DRV_USBD_EPIN1);
        nrf_drv_usbd_ep_stall_clear(NRF_DRV_USBD_EPIN1);
        nrf_drv_usbd_ep_enable(NRF_DRV_USBD_EPIN1);
        m_usbd_configured = true;
        nrf_drv_usbd_setup_clear();
    }
    else if ( index == 0 )
    {
        nrf_drv_usbd_ep_disable(NRF_DRV_USBD_EPIN1);
        m_usbd_configured = false;
        nrf_drv_usbd_setup_clear();
    }
    else
    {
        return NRF_ERROR_INVALID_PARAM;
    }
    return NRF_SUCCESS;
}
/**
 * @name Processing setup requests
 *
 * @{
 */
/**
 * @brief Respond on ep 0
 *
 * Auxiliary function for sending respond on endpoint 0
 * @param[in] p_setup Pointer to setup data from current setup request.
 *                    It would be used to calculate the size of data to send.
 * @param[in] p_data  Pointer to the data to send.
 * @param[in] size    Number of bytes to send.
 * @note Data pointed by p_data has to be available till the USBD_EVT_BUFREADY event.
 */
static void respond_setup_data(
    nrf_drv_usbd_setup_t const * const p_setup,
    void const * p_data, size_t size)
{
    /* Check the size against required response size */
    if (size &amp;gt; p_setup-&amp;gt;wLength)
    {
        size = p_setup-&amp;gt;wLength;
    }
    ret_code_t ret;
    nrf_drv_usbd_transfer_t transfer =
    {
        .p_data = {.tx = p_data},
        .size = size
    };
    ret = nrf_drv_usbd_ep_transfer(NRF_DRV_USBD_EPIN0, &amp;amp;transfer);
    /*if (ret != NRF_SUCCESS)
    {
        NRF_LOG_ERROR(&amp;quot;Transfer starting failed: %d&amp;quot;, (uint32_t)ret);
    }*/
    ASSERT(ret == NRF_SUCCESS);
    UNUSED_VARIABLE(ret);
}


/** React to GetStatus */
static void usbd_setup_GetStatus(nrf_drv_usbd_setup_t const * const p_setup)
{
    switch (p_setup-&amp;gt;bmRequestType)
    {
    case 0x80: // Device
        if (((p_setup-&amp;gt;wIndex) &amp;amp; 0xff) == 0)
        {
            respond_setup_data(
                p_setup,
                m_usbd_rwu_enabled ? get_status_device_resp_rwu : get_status_device_resp_nrwu,
                sizeof(get_status_device_resp_nrwu));
            return;
        }
        break;
    case 0x81: // Interface
        if (m_usbd_configured) // Respond only if configured
        {
            if (((p_setup-&amp;gt;wIndex) &amp;amp; 0xff) == 0) // Only interface 0 supported
            {
                respond_setup_data(
                    p_setup,
                    get_status_interface_resp,
                    sizeof(get_status_interface_resp));
                return;
            }
        }
        break;
    case 0x82: // Endpoint
        if (((p_setup-&amp;gt;wIndex) &amp;amp; 0xff) == 0) // Endpoint 0
        {
            respond_setup_data(
                p_setup,
                get_status_ep_active_resp,
                sizeof(get_status_ep_active_resp));
            return;
        }
        if (m_usbd_configured) // Other endpoints responds if configured
        {
            if (((p_setup-&amp;gt;wIndex) &amp;amp; 0xff) == NRF_DRV_USBD_EPIN1)
            {
                if (nrf_drv_usbd_ep_stall_check(NRF_DRV_USBD_EPIN1))
                {
                    respond_setup_data(
                        p_setup,
                        get_status_ep_halted_resp,
                        sizeof(get_status_ep_halted_resp));
                    return;
                }
                else
                {
                    respond_setup_data(
                        p_setup,
                        get_status_ep_active_resp,
                        sizeof(get_status_ep_active_resp));
                    return;
                }
            }
        }
        break;
    default:
        break; // Just go to stall
    }
   // NRF_LOG_ERROR(&amp;quot;Unknown status: 0x%2x&amp;quot;, p_setup-&amp;gt;bmRequestType);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_ClearFeature(nrf_drv_usbd_setup_t const * const p_setup)
{
    if ((p_setup-&amp;gt;bmRequestType) == 0x02) // standard request, recipient=endpoint
    {
        if ((p_setup-&amp;gt;wValue) == 0)
        {
            if ((p_setup-&amp;gt;wIndex) == NRF_DRV_USBD_EPIN1)
            {
                nrf_drv_usbd_ep_stall_clear(NRF_DRV_USBD_EPIN1);
                nrf_drv_usbd_setup_clear();
                return;
            }
        }
    }
    else if ((p_setup-&amp;gt;bmRequestType) ==  0x0) // standard request, recipient=device
    {
        if (REMOTE_WU)
        {
            if ((p_setup-&amp;gt;wValue) == 1) // Feature Wakeup
            {
                m_usbd_rwu_enabled = false;
                nrf_drv_usbd_setup_clear();
                return;
            }
        }
    }
    //NRF_LOG_ERROR(&amp;quot;Unknown feature to clear&amp;quot;);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_SetFeature(nrf_drv_usbd_setup_t const * const p_setup)
{
    if ((p_setup-&amp;gt;bmRequestType) == 0x02) // standard request, recipient=endpoint
    {
        if ((p_setup-&amp;gt;wValue) == 0) // Feature HALT
        {
            if ((p_setup-&amp;gt;wIndex) == NRF_DRV_USBD_EPIN1)
            {
                nrf_drv_usbd_ep_stall(NRF_DRV_USBD_EPIN1);
                nrf_drv_usbd_setup_clear();
                return;
            }
        }
    }
    else if ((p_setup-&amp;gt;bmRequestType) ==  0x0) // standard request, recipient=device
    {
        if (REMOTE_WU)
        {
            if ((p_setup-&amp;gt;wValue) == 1) // Feature Wakeup
            {
                m_usbd_rwu_enabled = true;
                nrf_drv_usbd_setup_clear();
                return;
            }
        }
    }
    //NRF_LOG_ERROR(&amp;quot;Unknown feature to set&amp;quot;);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_GetDescriptor(nrf_drv_usbd_setup_t const * const p_setup)
{
    //determine which descriptor has been asked for
    switch ((p_setup-&amp;gt;wValue) &amp;gt;&amp;gt; 8)
    {
    case 1: // Device
        if ((p_setup-&amp;gt;bmRequestType) == 0x80)
        {
            respond_setup_data(
                p_setup,
                get_descriptor_device,
                sizeof(get_descriptor_device));
            return;
        }
        break;
    case 2: // Configuration
        if ((p_setup-&amp;gt;bmRequestType) == 0x80)
        {
            respond_setup_data(
                p_setup,
                get_descriptor_configuration,
                GET_CONFIG_DESC_SIZE);
            return;
        }
        break;
    case 3: // String
        if ((p_setup-&amp;gt;bmRequestType) == 0x80)
        {
            // Select the string
            switch ((p_setup-&amp;gt;wValue) &amp;amp; 0xFF)
            {
            case USBD_STRING_LANG_IX:
                respond_setup_data(
                    p_setup,
                    get_descriptor_string_lang,
                    sizeof(get_descriptor_string_lang));
                return;
            case USBD_STRING_MANUFACTURER_IX:
                respond_setup_data(
                    p_setup,
                    get_descriptor_string_manuf,
                    sizeof(get_descriptor_string_manuf));
                return;
            case USBD_STRING_PRODUCT_IX:
                respond_setup_data(p_setup,
                    get_descriptor_string_prod,
                    sizeof(get_descriptor_string_prod));
                return;
            default:
                break;
            }
        }
        break;
    case 4: // Interface
        if ((p_setup-&amp;gt;bmRequestType) == 0x80)
        {
            // Which interface?
            if ((((p_setup-&amp;gt;wValue) &amp;amp; 0xFF) == 0))
            {
                respond_setup_data(
                    p_setup,
                    get_descriptor_interface_0,
                    GET_INTERFACE_DESC_SIZE);
                return;
            }
        }
        break;
    case 5: // Endpoint
        if ((p_setup-&amp;gt;bmRequestType) == 0x80)
        {
            // Which endpoint?
            if (((p_setup-&amp;gt;wValue) &amp;amp; 0xFF) == 1)
            {
                respond_setup_data(
                    p_setup,
                    get_descriptor_endpoint_1,
                    GET_ENDPOINT_DESC_SIZE);
                return;
            }
        }
        break;
    case 0x21: // HID
        if ((p_setup-&amp;gt;bmRequestType) == 0x81)
        {
            // Which interface
            if (((p_setup-&amp;gt;wValue) &amp;amp; 0xFF) == 0)
            {
                respond_setup_data(
                    p_setup,
                    get_descriptor_hid_0,
                    GET_HID_DESC_SIZE);
                return;
            }
        }
        break;
    case 0x22: // HID report
        if ((p_setup-&amp;gt;bmRequestType) == 0x81)
        {
            // Which interface?
            if (((p_setup-&amp;gt;wValue) &amp;amp; 0xFF) == 0)
            {
                respond_setup_data(
                    p_setup,
                    get_descriptor_report_interface_0,
                    sizeof(get_descriptor_report_interface_0));
                return;
            }
        }
        break;
    default:
        break; // Not supported - go to stall
    }

    /*NRF_LOG_ERROR(&amp;quot;Unknown descriptor requested: 0x%2x, type: 0x%2x or value: 0x%2x&amp;quot;,
        p_setup-&amp;gt;wValue &amp;gt;&amp;gt; 8,
        p_setup-&amp;gt;bmRequestType,
        p_setup-&amp;gt;wValue &amp;amp; 0xFF);*/
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_GetConfig(nrf_drv_usbd_setup_t const * const p_setup)
{
    if (m_usbd_configured)
    {
        respond_setup_data(
            p_setup,
            get_config_resp_configured,
            sizeof(get_config_resp_configured));
    }
    else
    {
        respond_setup_data(
            p_setup,
            get_config_resp_unconfigured,
            sizeof(get_config_resp_unconfigured));
    }
}

static void usbd_setup_SetConfig(nrf_drv_usbd_setup_t const * const p_setup)
{
    if ((p_setup-&amp;gt;bmRequestType) == 0x00)
    {
        // accept only 0 and 1
        if (((p_setup-&amp;gt;wIndex) == 0) &amp;amp;&amp;amp; ((p_setup-&amp;gt;wLength) == 0) &amp;amp;&amp;amp;
            ((p_setup-&amp;gt;wValue) &amp;lt;= UINT8_MAX))
        {
            if (NRF_SUCCESS == ep_configuration((uint8_t)(p_setup-&amp;gt;wValue)))
            {
                nrf_drv_usbd_setup_clear();
                return;
            }
        }
    }
    /*NRF_LOG_ERROR(&amp;quot;Wrong configuration: Index: 0x%2x, Value: 0x%2x.&amp;quot;,
        p_setup-&amp;gt;wIndex,
        p_setup-&amp;gt;wValue);*/
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_SetIdle(nrf_drv_usbd_setup_t const * const p_setup)
{
    if (p_setup-&amp;gt;bmRequestType == 0x21)
    {
        //accept any value
        nrf_drv_usbd_setup_clear();
        return;
    }
   // NRF_LOG_ERROR(&amp;quot;Set Idle wrong type: 0x%2x.&amp;quot;, p_setup-&amp;gt;bmRequestType);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_SetInterface(
    nrf_drv_usbd_setup_t const * const p_setup)
{
    //no alternate setting is supported - STALL always
   // NRF_LOG_ERROR(&amp;quot;No alternate interfaces supported.&amp;quot;);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_SetProtocol(
    nrf_drv_usbd_setup_t const * const p_setup)
{
    if (p_setup-&amp;gt;bmRequestType == 0x21)
    {
        //accept any value
        nrf_drv_usbd_setup_clear();
        return;
    }
   // NRF_LOG_ERROR(&amp;quot;Set Protocol wrong type: 0x%2x.&amp;quot;, p_setup-&amp;gt;bmRequestType);
    nrf_drv_usbd_setup_stall();
}

/** @} */ /* End of processing setup requests functions */


static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event)
{
    switch (p_event-&amp;gt;type)
    {
    case NRF_DRV_USBD_EVT_SUSPEND:
       // NRF_LOG_INFO(&amp;quot;SUSPEND state detected&amp;quot;);
        m_usbd_suspend_state_req = true;
        break;
    case NRF_DRV_USBD_EVT_RESUME:
        //NRF_LOG_INFO(&amp;quot;RESUMING from suspend&amp;quot;);
        m_usbd_suspend_state_req = false;
        break;
    case NRF_DRV_USBD_EVT_WUREQ:
       // NRF_LOG_INFO(&amp;quot;RemoteWU initiated&amp;quot;);
        m_usbd_suspend_state_req = false;
        break;
    case NRF_DRV_USBD_EVT_RESET:
        {
            ret_code_t ret = ep_configuration(0);
            ASSERT(ret == NRF_SUCCESS);
            UNUSED_VARIABLE(ret);
            m_usbd_suspend_state_req = false;
            break;
        }
    case NRF_DRV_USBD_EVT_SOF:
        {
            static uint32_t cycle = 0;
            ++cycle;
            if ((cycle % (m_usbd_configured ? 500 : 100)) == 0)
            {
                bsp_board_led_invert(LED_USB_STATUS);
            }
            break;
        }
    case NRF_DRV_USBD_EVT_EPTRANSFER:
        if (NRF_DRV_USBD_EPIN1 == p_event-&amp;gt;data.eptransfer.ep)
        {
            m_send_mouse_position = false;
        }
        else
        if (NRF_DRV_USBD_EPIN0 == p_event-&amp;gt;data.eptransfer.ep)
        {
            if (NRF_USBD_EP_OK == p_event-&amp;gt;data.eptransfer.status)
            {
                /* Transfer ok - allow status stage */
                nrf_drv_usbd_setup_clear();
            }
            else if (NRF_USBD_EP_ABORTED == p_event-&amp;gt;data.eptransfer.status)
            {
                /* Just ignore */
               // NRF_LOG_INFO(&amp;quot;Transfer aborted event on EPIN0&amp;quot;);
							
            }
            else
            {
               // NRF_LOG_ERROR(&amp;quot;Transfer failed on EPIN0: %d&amp;quot;, p_event-&amp;gt;data.eptransfer.status);
                nrf_drv_usbd_setup_stall();
            }
        }
        else
        if (NRF_DRV_USBD_EPOUT0 == p_event-&amp;gt;data.eptransfer.ep)
        {
            /* NOTE: No EPOUT0 data transfers are used.
             * The code is here as a pattern how to support such a transfer. */
            if (NRF_USBD_EP_OK == p_event-&amp;gt;data.eptransfer.status)
            {
                /* Transfer ok - allow status stage */
                nrf_drv_usbd_setup_clear();
            }
            else if (NRF_USBD_EP_ABORTED == p_event-&amp;gt;data.eptransfer.status)
            {
                /* Just ignore */
               // NRF_LOG_INFO(&amp;quot;Transfer aborted event on EPOUT0&amp;quot;);
							
            }
            else
            {
                //NRF_LOG_ERROR(&amp;quot;Transfer failed on EPOUT0: %d&amp;quot;, p_event-&amp;gt;data.eptransfer.status);
                nrf_drv_usbd_setup_stall();
            }
        }
        else
        {
            /* Nothing to do */
        }
        break;
    case NRF_DRV_USBD_EVT_SETUP:
        {
            nrf_drv_usbd_setup_t setup;
            nrf_drv_usbd_setup_get(&amp;amp;setup);
            switch (setup.bRequest)
            {
            case 0x00: // GetStatus
                usbd_setup_GetStatus(&amp;amp;setup);
                break;
            case 0x01: // CleartFeature
                usbd_setup_ClearFeature(&amp;amp;setup);
                break;
            case 0x03: // SetFeature
                usbd_setup_SetFeature(&amp;amp;setup);
                break;
            case 0x05: // SetAddress
                //nothing to do, handled by hardware; but don&amp;#39;t STALL
                break;
            case 0x06: // GetDescriptor
                usbd_setup_GetDescriptor(&amp;amp;setup);
                break;
            case 0x08: // GetConfig
                usbd_setup_GetConfig(&amp;amp;setup);
                break;
            case 0x09: // SetConfig
                usbd_setup_SetConfig(&amp;amp;setup);
                break;
            //HID class
            case 0x0A: // SetIdle
                usbd_setup_SetIdle(&amp;amp;setup);
                break;
            case 0x0B: // SetProtocol or SetInterface
                if (setup.bmRequestType == 0x01) // standard request, recipient=interface
                {
                    usbd_setup_SetInterface(&amp;amp;setup);
                }
                else if (setup.bmRequestType == 0x21) // class request, recipient=interface
                {
                    usbd_setup_SetProtocol(&amp;amp;setup);
                }
                else
                {
                    NRF_LOG_ERROR(&amp;quot;Command 0xB. Unknown request: 0x%2x&amp;quot;, setup.bmRequestType);
                    nrf_drv_usbd_setup_stall();
                }
                break;
            default:
                NRF_LOG_ERROR(&amp;quot;Unknown request: 0x%2x&amp;quot;, setup.bRequest);
                nrf_drv_usbd_setup_stall();
                return;
            }
            break;
        }
    default:
        break;
    }
}


static void move_mouse_pointer(void)
{
    static uint32_t databuffer;

    if (!m_usbd_configured)
        return;
    if (!m_send_mouse_position)
    {
        switch (m_mouse_position &amp;amp; 0x3)
        {
        case 0:
            /* X = 10, rest all are unchanged */
            databuffer = 0x00000A00;
            break;
        case 1:
            /* Y = 10, rest all are unchanged */
            databuffer = 0x000A0000;
            break;
        case 2:
            /* X = -10, rest all are unchanged */
            databuffer = 0x0000F600;
            break;
        case 3:
            /* Y = -10, rest all are unchanged */
            databuffer = 0x00F60000;
            break;
        }
        m_mouse_position++;

        /* Send data */
        static const nrf_drv_usbd_transfer_t transfer =
        {
            .p_data = {.tx = &amp;amp;databuffer},
            .size = sizeof(databuffer)
        };
        m_send_mouse_position = true;
        UNUSED_RETURN_VALUE(nrf_drv_usbd_ep_transfer(
            NRF_DRV_USBD_EPIN1,
            &amp;amp;transfer));
    }
}

static void power_usb_event_handler(nrf_drv_power_usb_evt_t event)
{
    switch (event)
    {
    case NRF_DRV_POWER_USB_EVT_DETECTED:
       // NRF_LOG_INFO(&amp;quot;USB power detected&amp;quot;);
	     		
        if (!nrf_drv_usbd_is_enabled())
        {
            nrf_drv_usbd_enable();
        }
        break;
    case NRF_DRV_POWER_USB_EVT_REMOVED:
      //  NRF_LOG_INFO(&amp;quot;USB power removed&amp;quot;);
        m_usbd_configured = false;
        m_send_mouse_position = false;
        if (nrf_drv_usbd_is_started())
        {
            nrf_drv_usbd_stop();
        }
        if (nrf_drv_usbd_is_enabled())
        {
            nrf_drv_usbd_disable();
        }
        /* Turn OFF LEDs */
        bsp_board_led_off(LED_USB_STATUS);
        bsp_board_led_off(LED_USB_POWER);
        break;
    case NRF_DRV_POWER_USB_EVT_READY:
       // NRF_LOG_INFO(&amp;quot;USB ready&amp;quot;);
        bsp_board_led_on(LED_USB_POWER);
        if (!nrf_drv_usbd_is_started())
        {
            nrf_drv_usbd_start(true);
        }
				
        break;
    default:
        ASSERT(false);
    }
}

static void bsp_evt_handler(bsp_event_t evt)
{
    switch ((unsigned int)evt)
    {
    case BSP_EVENT_SYSOFF:
    {
        m_system_off_req = true;
        break;
    }
    case CONCAT_2(BSP_EVENT_KEY_, BTN_DATA_SEND):
    {
        m_send_flag = 1;
        break;
    }

    case BTN_DATA_KEY_RELEASE:
    {
        m_send_flag = 0;
        break;
    }
    default:
        return;
    }
}

//static void init_power_clock(void)
//{
//    ret_code_t ret;
//    /* Initializing power and clock */
//    ret = nrf_drv_clock_init();
//    APP_ERROR_CHECK(ret);
//    ret = nrf_drv_power_init(NULL);
//    APP_ERROR_CHECK(ret);
//    nrf_drv_clock_hfclk_request(NULL);
//    nrf_drv_clock_lfclk_request(NULL);
//    while (!(nrf_drv_clock_hfclk_is_running() &amp;amp;&amp;amp;
//           nrf_drv_clock_lfclk_is_running()))
//    {
 //       /* Just waiting */
   // }

    //ret = app_timer_init();
    //APP_ERROR_CHECK(ret);

    /* Avoid warnings if assertion is disabled */
   // UNUSED_VARIABLE(ret);
//} 

static void init_bsp(void)
{
    ret_code_t ret;
    ret = bsp_init(BSP_INIT_BUTTONS, bsp_evt_handler);
    APP_ERROR_CHECK(ret);

    ret = bsp_event_to_button_action_assign(
        BTN_SYSTEM_OFF,
        BSP_BUTTON_ACTION_RELEASE,
        BSP_EVENT_SYSOFF);
    APP_ERROR_CHECK(ret);
    ret = bsp_event_to_button_action_assign(BTN_DATA_SEND,
                                            BSP_BUTTON_ACTION_RELEASE,
                                            BTN_DATA_KEY_RELEASE);
    APP_ERROR_CHECK(ret);
    /* Avoid warnings if assertion is disabled */
    UNUSED_VARIABLE(ret);
}

/*static void init_cli(void)
{
    ret_code_t ret;
    ret = bsp_cli_init(bsp_evt_handler);
    APP_ERROR_CHECK(ret);
    nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG;
    uart_config.pseltxd = TX_PIN_NUMBER;
    uart_config.pselrxd = RX_PIN_NUMBER;
    uart_config.hwfc    = NRF_UART_HWFC_DISABLED;
    ret = nrf_cli_init(&amp;amp;m_cli_uart, &amp;amp;uart_config, true, true, NRF_LOG_SEVERITY_INFO);
    APP_ERROR_CHECK(ret);
    ret = nrf_cli_start(&amp;amp;m_cli_uart);
    APP_ERROR_CHECK(ret);
}
*/
static void log_resetreason(void)
{
    /* Reset reason */
    uint32_t rr = nrf_power_resetreas_get();
   // NRF_LOG_INFO(&amp;quot;Reset reasons:&amp;quot;);
    if (0 == rr)
    {
      //  NRF_LOG_INFO(&amp;quot;- NONE&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_RESETPIN_MASK))
    {
        NRF_LOG_INFO(&amp;quot;- RESETPIN&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_DOG_MASK     ))
    {
       // NRF_LOG_INFO(&amp;quot;- DOG&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_SREQ_MASK    ))
    {
        //NRF_LOG_INFO(&amp;quot;- SREQ&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_LOCKUP_MASK  ))
    {
       // NRF_LOG_INFO(&amp;quot;- LOCKUP&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_OFF_MASK     ))
    {
      //  NRF_LOG_INFO(&amp;quot;- OFF&amp;quot;);
    }
#if defined(NRF_POWER_RESETREAS_LPCOMP_MASK)
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_LPCOMP_MASK  ))
    {
        NRF_LOG_INFO(&amp;quot;- LPCOMP&amp;quot;);
    }
#endif
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_DIF_MASK     ))
    {
       // NRF_LOG_INFO(&amp;quot;- DIF&amp;quot;);
    }
#if defined(NRF_POWER_RESETREAS_NFC_MASK)
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_NFC_MASK     ))
    {
        NRF_LOG_INFO(&amp;quot;- NFC&amp;quot;);
    }
#endif
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_VBUS_MASK    ))
    {
        //NRF_LOG_INFO(&amp;quot;- VBUS&amp;quot;);
    }
}



/**
 * @brief Audio class user event handler
 */
static void hp_audio_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                     app_usbd_audio_user_event_t   event);
static void mic_audio_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                      app_usbd_audio_user_event_t   event);

/* Channels and feature controls configuration */

/**
 * @brief   Input terminal channel configuration
 */
#define HP_TERMINAL_CH_CONFIG()                                                                       \
        (APP_USBD_AUDIO_IN_TERM_CH_CONFIG_LEFT_FRONT | APP_USBD_AUDIO_IN_TERM_CH_CONFIG_RIGHT_FRONT)

/**
 * @brief   Feature controls
 *
 *      general
 *      channel 0
 *      channel 1
 */
#define HP_FEATURE_CONTROLS()                                               \
        APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE),  \
        APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE),  \
        APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE)



/**
 * @brief   Input terminal channel configuration
 */
#define MIC_TERMINAL_CH_CONFIG()                                                                       \
        (APP_USBD_AUDIO_IN_TERM_CH_CONFIG_LEFT_FRONT | APP_USBD_AUDIO_IN_TERM_CH_CONFIG_RIGHT_FRONT)

/**
 * @brief   Feature controls
 *
 *      general
 *      channel 0
 *      channel 1
 */
#define MIC_FEATURE_CONTROLS()                                                                     \
        APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE),                         \
        APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE),                         \
        APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE)


/* Microphone descriptors */

/**
 * @brief   Audio class specific format descriptor
 */
APP_USBD_AUDIO_FORMAT_DESCRIPTOR(m_mic_form_desc, 
                                 APP_USBD_AUDIO_AS_FORMAT_I_DSC(    /* Format type 1 descriptor */
                                    2,                              /* Number of channels */
                                    2,                              /* Subframe size */
                                    16,                             /* Bit resolution */
                                    1,                              /* Frequency type */
                                    APP_USBD_U24_TO_RAW_DSC(48000)) /* Frequency */
                                );

/**
 * @brief   Audio class input terminal descriptor
 */
APP_USBD_AUDIO_INPUT_DESCRIPTOR(m_mic_inp_desc, 
                                APP_USBD_AUDIO_INPUT_TERMINAL_DSC(
                                    1,                                     /* Terminal ID */
                                    APP_USBD_AUDIO_TERMINAL_IN_MICROPHONE, /* Terminal type */
                                    2,                                     /* Number of channels */
                                    MIC_TERMINAL_CH_CONFIG())              /* Channels config */
                                );

/**
 * @brief   Audio class output terminal descriptor
 */
APP_USBD_AUDIO_OUTPUT_DESCRIPTOR(m_mic_out_desc, 
                                 APP_USBD_AUDIO_OUTPUT_TERMINAL_DSC(
                                    3,                                     /* Terminal ID */
                                    APP_USBD_AUDIO_TERMINAL_USB_STREAMING, /* Terminal type */
                                    2)                                     /* Source ID */
                                );

/**
 * @brief   Audio class feature unit descriptor
 */
APP_USBD_AUDIO_FEATURE_DESCRIPTOR(m_mic_fea_desc, 
                                  APP_USBD_AUDIO_FEATURE_UNIT_DSC(
                                    2,                      /* Unit ID */
                                    1,                      /* Source ID */
                                    MIC_FEATURE_CONTROLS()) /* List of controls */
                                 );

/* Headphones descriptors */

/**
 * @brief   Audio class specific format III descriptor
 */
APP_USBD_AUDIO_FORMAT_DESCRIPTOR(m_hp_form_desc, 
                                 APP_USBD_AUDIO_AS_FORMAT_III_DSC(    /* Format type 3 descriptor */
                                    2,                                /* Number of channels */
                                    2,                                /* Subframe size */
                                    16,                               /* Bit resolution */
                                    1,                                /* Frequency type */
                                    APP_USBD_U24_TO_RAW_DSC(48000))   /* Frequency */
                                );

/**
 * @brief   Audio class input terminal descriptor
 */
APP_USBD_AUDIO_INPUT_DESCRIPTOR(m_hp_inp_desc, 
                                APP_USBD_AUDIO_INPUT_TERMINAL_DSC(
                                    1,                                     /* Terminal ID */
                                    APP_USBD_AUDIO_TERMINAL_USB_STREAMING, /* Terminal type */
                                    2,                                     /* Number of channels */
                                    HP_TERMINAL_CH_CONFIG())               /* Channels config */
                               );

/**
 * @brief   Audio class output terminal descriptor
 */
APP_USBD_AUDIO_OUTPUT_DESCRIPTOR(m_hp_out_desc, 
                                 APP_USBD_AUDIO_OUTPUT_TERMINAL_DSC(
                                    3,                                      /* Terminal ID */
                                    APP_USBD_AUDIO_TERMINAL_OUT_HEADPHONES, /* Terminal type */
                                    2)                                      /* Source ID */
                                );

/**
 * @brief   Audio class feature unit descriptor
 */
APP_USBD_AUDIO_FEATURE_DESCRIPTOR(m_hp_fea_desc, 
                                  APP_USBD_AUDIO_FEATURE_UNIT_DSC(
                                    2,                     /* Unit ID */
                                    1,                     /* Source ID */
                                    HP_FEATURE_CONTROLS()) /* List of controls */
                                 );

/* Interfaces lists */

/**
 * @brief Interfaces list passed to @ref APP_USBD_AUDIO_GLOBAL_DEF
 */
#define HP_INTERFACES_CONFIG() APP_USBD_AUDIO_CONFIG_OUT(0, 1)

/**
 * @brief Interfaces list passed to @ref APP_USBD_AUDIO_GLOBAL_DEF
 */
#define MIC_INTERFACES_CONFIG() APP_USBD_AUDIO_CONFIG_IN(2, 3)

/*lint -save -e26 -e64 -e123 -e505 -e651*/


/**
 * @brief Headphone Audio class instance
 */
APP_USBD_AUDIO_GLOBAL_DEF(m_app_audio_headphone,
                          HP_INTERFACES_CONFIG(),
                          hp_audio_user_ev_handler,
                          &amp;amp;m_hp_form_desc,
                          &amp;amp;m_hp_inp_desc,
                          &amp;amp;m_hp_out_desc,
                          &amp;amp;m_hp_fea_desc,
                          0,
                          APP_USBD_AUDIO_AS_IFACE_FORMAT_PCM,
                          192,
                          APP_USBD_AUDIO_SUBCLASS_AUDIOSTREAMING,
                          1
);



/**
 * @brief Microphone Audio class instance
 */
APP_USBD_AUDIO_GLOBAL_DEF(m_app_audio_microphone,
                          MIC_INTERFACES_CONFIG(),
                          mic_audio_user_ev_handler,
                          &amp;amp;m_mic_form_desc,
                          &amp;amp;m_mic_inp_desc,
                          &amp;amp;m_mic_out_desc,
                          &amp;amp;m_mic_fea_desc,
                          0,
                          APP_USBD_AUDIO_AS_IFACE_FORMAT_PCM,
                          192,
                          APP_USBD_AUDIO_SUBCLASS_AUDIOSTREAMING,
                          3
);


/*lint -restore*/

/**
 * @brief Internal audio temporary buffer
 */
static int16_t  m_temp_buffer[2 * BUFFER_SIZE];



/**
 * @brief The size of last received block from the microphone
 */
static size_t m_temp_buffer_size;

/**
 * @brief Actual headphones mute
 */
static uint8_t  m_mute_hp;

/**
 * @brief Actual sampling frequency
 */
static uint32_t m_freq_hp;

/**
 * @brief Actual microphone mute state
 */
static uint8_t  m_mute_mic;

/**
 * @brief Actual microphone sampling frequency
 */
static uint32_t m_freq_mic;

/**
 * @brief Audio class specific request handle (headphones)
 */
static void hp_audio_user_class_req(app_usbd_class_inst_t const * p_inst)
{
    app_usbd_audio_t const * p_audio = app_usbd_audio_class_get(p_inst);
    app_usbd_audio_req_t * p_req = app_usbd_audio_class_request_get(p_audio);

    UNUSED_VARIABLE(m_mute_hp);
    UNUSED_VARIABLE(m_freq_hp);

    switch (p_req-&amp;gt;req_target)
    {
        case APP_USBD_AUDIO_CLASS_REQ_IN:

            if (p_req-&amp;gt;req_type == APP_USBD_AUDIO_REQ_SET_CUR)
            {
                //Only mute control is defined
                p_req-&amp;gt;payload[0] = m_mute_hp;
            }

            break;
        case APP_USBD_AUDIO_CLASS_REQ_OUT:

            if (p_req-&amp;gt;req_type == APP_USBD_AUDIO_REQ_SET_CUR)
            {
                //Only mute control is defined
                m_mute_hp = p_req-&amp;gt;payload[0];
            }

            break;
        case APP_USBD_AUDIO_EP_REQ_IN:
            break;
        case APP_USBD_AUDIO_EP_REQ_OUT:

            if (p_req-&amp;gt;req_type == APP_USBD_AUDIO_REQ_SET_CUR)
            {
                //Only set frequency is supported
                m_freq_hp = uint24_decode(p_req-&amp;gt;payload);
            }

            break;
        default:
            break;
    }
}

/**
 * @brief Audio class specific request handle (microphone)
 */
static void mic_audio_user_class_req(app_usbd_class_inst_t const * p_inst)
{
    app_usbd_audio_t const * p_audio = app_usbd_audio_class_get(p_inst);
    app_usbd_audio_req_t * p_req = app_usbd_audio_class_request_get(p_audio);

    UNUSED_VARIABLE(m_mute_mic);
    UNUSED_VARIABLE(m_freq_mic);

    switch (p_req-&amp;gt;req_target)
    {
        case APP_USBD_AUDIO_CLASS_REQ_IN:

            if (p_req-&amp;gt;req_type == APP_USBD_AUDIO_REQ_SET_CUR)
            {
                //Only mute control is defined
                p_req-&amp;gt;payload[0] = m_mute_mic;
            }

            break;
        case APP_USBD_AUDIO_CLASS_REQ_OUT:

            if (p_req-&amp;gt;req_type == APP_USBD_AUDIO_REQ_SET_CUR)
            {
                //Only mute control is defined
                m_mute_mic = p_req-&amp;gt;payload[0];
            }

            break;
        case APP_USBD_AUDIO_EP_REQ_IN:
            break;
        case APP_USBD_AUDIO_EP_REQ_OUT:

            if (p_req-&amp;gt;req_type == APP_USBD_AUDIO_REQ_SET_CUR)
            {
                //Only set frequency is supported
                m_freq_mic = uint24_decode(p_req-&amp;gt;payload);
            }

            break;
        default:
            break;
    }
}

/**
 * @brief User event handler @ref app_usbd_audio_user_ev_handler_t (headphones)
 */
static void hp_audio_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                     app_usbd_audio_user_event_t   event)
{
    app_usbd_audio_t const * p_audio = app_usbd_audio_class_get(p_inst);
    UNUSED_VARIABLE(p_audio);
    switch (event)
    {
        case APP_USBD_AUDIO_USER_EVT_CLASS_REQ:
            hp_audio_user_class_req(p_inst);
            break;
        case APP_USBD_AUDIO_USER_EVT_RX_DONE:
        {
            ret_code_t ret;
            /* Block from headphones copied into buffer, send it into microphone input */
            ret = app_usbd_audio_class_tx_start(&amp;amp;m_app_audio_microphone.base, m_temp_buffer, m_temp_buffer_size);
					  APP_ERROR_CHECK(ret);
           /* if (NRF_SUCCESS == ret)
            {
                bsp_board_led_invert(LED_AUDIO_RX);
            }*/
            break;
        }
        default:
            break;
    }
}

/**
 * @brief User event handler @ref app_usbd_audio_user_ev_handler_t (microphone)
 */
static void mic_audio_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                      app_usbd_audio_user_event_t   event)
{
    app_usbd_audio_t const * p_audio = app_usbd_audio_class_get(p_inst);
    UNUSED_VARIABLE(p_audio);

    switch (event)
    {
        case APP_USBD_AUDIO_USER_EVT_CLASS_REQ:
            mic_audio_user_class_req(p_inst);
            break;
        case APP_USBD_AUDIO_USER_EVT_TX_DONE:
        {
           // bsp_board_led_invert(LED_AUDIO_TX);
					NRF_LOG_INFO(&amp;quot;Audio Txn done&amp;quot;);
            break;
        }
        default:
            break;
    }
}

static void hp_sof_ev_handler(uint16_t framecnt)
{
    UNUSED_VARIABLE(framecnt);
    if (APP_USBD_STATE_Configured != app_usbd_core_state_get())
    {
        return;
    }
    size_t rx_size = app_usbd_audio_class_rx_size_get(&amp;amp;m_app_audio_headphone.base);
    m_temp_buffer_size = rx_size;
    if (rx_size &amp;gt; 0)
    {
        ASSERT(rx_size &amp;lt;= sizeof(m_temp_buffer));
        ret_code_t ret;
        ret = app_usbd_audio_class_rx_start(&amp;amp;m_app_audio_headphone.base, m_temp_buffer, rx_size);
        if (NRF_SUCCESS != ret)
        {
            NRF_LOG_ERROR(&amp;quot;Cannot start RX transfer from headphone\r\n&amp;quot;);
        }
    }
}

/**
 * @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_SUSPEND:
            //bsp_board_leds_off();
            break;
        case APP_USBD_EVT_DRV_RESUME:
           // bsp_board_led_on(LED_USB_RESUME);
            break;
        case APP_USBD_EVT_STARTED:
            //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(&amp;quot;USB power detected&amp;quot;);

            if (!nrf_drv_usbd_is_enabled())
            {
                app_usbd_enable();
            }
            break;
        case APP_USBD_EVT_POWER_REMOVED:
            NRF_LOG_INFO(&amp;quot;USB power removed&amp;quot;);
            app_usbd_stop();
            break;
        case APP_USBD_EVT_POWER_READY:
            NRF_LOG_INFO(&amp;quot;USB ready&amp;quot;);
            app_usbd_start();
            break;
        default:
            break;
    }
}

int main(void)
{
    ret_code_t ret;
	static const app_usbd_config_t usbd_config = {
        .ev_state_proc = usbd_user_ev_handler,
        .enable_sof = true
    };
		
    ret = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(ret);
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    ret = nrf_drv_clock_init();
    APP_ERROR_CHECK(ret);
		ret = nrf_drv_power_init(NULL);
    APP_ERROR_CHECK(ret);
		ret = app_timer_init();
    APP_ERROR_CHECK(ret);
		// init_power_clock();
    init_bsp();
   // init_cli();
		
		 // NRF_LOG_INFO(&amp;quot;USDB example started.&amp;quot;);
    log_resetreason();
    nrf_power_resetreas_clear(nrf_power_resetreas_get());
		

	
	/* USB work starts right here */
		 ret = nrf_drv_usbd_init(usbd_event_handler);
     APP_ERROR_CHECK(ret);
	  

    /* Configure selected size of the packed on EP0 */
    nrf_drv_usbd_ep_max_packet_size_set(NRF_DRV_USBD_EPOUT0, EP0_MAXPACKETSIZE);
    nrf_drv_usbd_ep_max_packet_size_set(NRF_DRV_USBD_EPIN0, EP0_MAXPACKETSIZE);

    /* Configure LED and button */
    bsp_board_init(BSP_INIT_LEDS);
    bsp_board_led_on(LED_RUNNING);
    bsp_board_led_on(LED_ACTIVE);

  


    NRF_LOG_INFO(&amp;quot;USBD audio example started.&amp;quot;);

    // Initialize LEDs and buttons
  //  bsp_board_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS);

     ret = app_usbd_init(&amp;amp;usbd_config);
     APP_ERROR_CHECK(ret);

    app_usbd_class_inst_t const * class_inst_hp =
        app_usbd_audio_class_inst_get(&amp;amp;m_app_audio_headphone);

    ret = app_usbd_audio_sof_interrupt_register(class_inst_hp, hp_sof_ev_handler);
    APP_ERROR_CHECK(ret);

    ret = app_usbd_class_append(class_inst_hp);
    APP_ERROR_CHECK(ret);

    app_usbd_class_inst_t const * class_inst_mic =
        app_usbd_audio_class_inst_get(&amp;amp;m_app_audio_microphone);
    ret = app_usbd_class_append(class_inst_mic);
    APP_ERROR_CHECK(ret);

    if (USBD_POWER_DETECTION)
    {
        ret = app_usbd_power_events_enable();
        APP_ERROR_CHECK(ret);
			
			static const nrf_drv_power_usbevt_config_t config =
        {
            .handler = power_usb_event_handler
        };
        ret = nrf_drv_power_usbevt_init(&amp;amp;config);
        APP_ERROR_CHECK(ret);
    }
    else
    {
        NRF_LOG_INFO(&amp;quot;No USB power detection enabled\r\nStarting USB now&amp;quot;);

        app_usbd_enable();
        app_usbd_start();
			
			 nrf_delay_us(STARTUP_DELAY);
        if (!nrf_drv_usbd_is_enabled())
        {
            nrf_drv_usbd_enable();
            ret = ep_configuration(0);
            APP_ERROR_CHECK(ret);
        }
				 while (NRF_DRV_POWER_USB_STATE_CONNECTED
              ==
              nrf_drv_power_usbstatus_get())
        {
            /* Just waiting */
        }

        if (NRF_DRV_POWER_USB_STATE_READY == nrf_drv_power_usbstatus_get())
        {
            if (!nrf_drv_usbd_is_started())
            {
                nrf_drv_usbd_start(true);
            }
        }
        else
        {
            nrf_drv_usbd_disable();
        }
    }
		
		while (true)
    {
			
			 while (app_usbd_event_queue_process())
        {
            /* Nothing to do */
        }

        UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
				
        if (m_system_off_req)
        {
            //NRF_LOG_INFO(&amp;quot;Going to system OFF&amp;quot;);
           // NRF_LOG_FLUSH();
            bsp_board_led_off(LED_RUNNING);
            bsp_board_led_off(LED_ACTIVE);
            nrf_power_system_off();
        }
        if (m_usbd_suspended != m_usbd_suspend_state_req)
        {
            if (m_usbd_suspend_state_req)
            {
                m_usbd_suspended = nrf_drv_usbd_suspend();
                if (m_usbd_suspended)
                {
                    bsp_board_leds_off();
                }
            }
            else
            {
                m_usbd_suspended = false;
            }
        }

        if (m_usbd_configured)
        {
            if (m_send_flag)
            {
                if (m_usbd_suspended)
                {
                    if (m_usbd_rwu_enabled)
                    {
                        UNUSED_RETURN_VALUE(nrf_drv_usbd_wakeup_req());
                    }
                }
                else
                {
                  //  NRF_LOG_INFO(&amp;quot;   TX pointer&amp;quot;);
                    move_mouse_pointer();
                }
            }
        }

      //  nrf_cli_process(&amp;amp;m_cli_uart);
       // UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
        bsp_board_led_off(LED_RUNNING);
        /* Even if we miss an event enabling USB,
         * USB event would wake us up. */
        __WFE();
        /* Clear SEV flag if CPU was woken up by event */
        __SEV();
        __WFE();
        bsp_board_led_on(LED_RUNNING);
    }
     
}

/** @} */
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Combined USBD and USBD_AUDIO code for your reference.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>