I need to add USB HID support for nRF52833 chip in Arduino.

Hello, we are developing a keyboard with two types of connections, via USB and Bluetooth using the nRF52833 chip.

We have all the firmware developed in Arduino since it is an open hardware and open source product.

I have been able to compile some tests with the Arduino core arduino-nRF5 from sandeepmistry: https://github.com/sandeepmistry/arduino-nRF5

The problem is that we need USB support and this Arduino core doesn't have that yet.

Is there a quick way to add USB support? Perhaps porting the Nordic SDK libraries?

I have been looking at the Arduino USBAPI.h file and I think the way is to implement the following functions:

#ifndef __USBAPI__
#define __USBAPI__

#include <stdint.h>

namespace arduino {
//================================================================================
//================================================================================
//  Low level API

typedef struct __attribute__((packed))
{
  union {
    uint8_t bmRequestType;
    struct {
      uint8_t direction : 5;
      uint8_t type : 2;
      uint8_t transferDirection : 1;
    };
  };
	uint8_t 	bRequest;
	uint8_t 	wValueL;
	uint8_t 	wValueH;
	uint16_t 	wIndex;
	uint16_t 	wLength;
} USBSetup;

}

//================================================================================
// USB APIs (C scope)
//================================================================================

int USB_SendControl(uint8_t flags, const void* d, int len);
int USB_RecvControl(void* d, int len);
int USB_RecvControlLong(void* d, int len);

uint8_t	USB_Available(uint8_t ep);
uint8_t USB_SendSpace(uint8_t ep);
int USB_Send(uint8_t ep, const void* data, int len);	// blocking
int USB_Recv(uint8_t ep, void* data, int len);		// non-blocking
int USB_Recv(uint8_t ep);							// non-blocking
void USB_Flush(uint8_t ep);

#endif

I appreciate any suggestions as we have very little time left until the estimated production date so I don't have the luxury of trying multiple forms.
From already thank you very much.

  • You can download nRF5 SDK releases from this page. Just download and unzip the release to a location close to the root of your harddrive. Then follow the instructions in Getting started with nRF5 SDK and SES (nRF51 & nRF52 Series) and Getting Started to build examples.

  • I'm compiling the example "usbd_hid_composite" in SEGGER studio with no problem but since our project is written in C++, when renaming main.c to main.cpp the compiler throws the following error:

    2> C:/Trabajo/firmware/nRF52xx/nRF5_SDK_17.1.0/components/libraries/usbd/app_usbd_class_base.h:815:5: error: designator order for field 'app_usbd_hid_inst_t::p_hid_methods' does not match declaration order in 'app_usbd_hid_inst_t'
    2> C:/Trabajo/firmware/nRF52xx/nRF5_SDK_17.1.0/components/libraries/usbd/app_usbd_class_base.h:946:9: note: in expansion of macro 'APP_USBD_CLASS_INSTANCE_INITVAL'
    2> C:/Trabajo/firmware/nRF52xx/nRF5_SDK_17.1.0/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse_internal.h:174:5: note: in expansion of macro 'APP_USBD_CLASS_INST_GLOBAL_DEF'
    2> C:/Trabajo/firmware/nRF52xx/nRF5_SDK_17.1.0/components/libraries/usbd/class/hid/mouse/app_usbd_hid_mouse.h:105:5: note: in expansion of macro 'APP_USBD_HID_MOUSE_GLOBAL_DEF_INTERNAL'
    2> C:\Trabajo\firmware\nRF52xx\nRF5_SDK_17.1.0\defy_wireless_firmware\main.cpp:176:1: note: in expansion of macro 'APP_USBD_HID_MOUSE_GLOBAL_DEF'
    2> C:/Trabajo/firmware/nRF52xx/nRF5_SDK_17.1.0/components/libraries/usbd/app_usbd_class_base.h:815:5: error: designator order for field 'app_usbd_hid_inst_t::p_hid_methods' does not match declaration order in 'app_usbd_hid_inst_t'
    2> C:/Trabajo/firmware/nRF52xx/nRF5_SDK_17.1.0/components/libraries/usbd/app_usbd_class_base.h:946:9: note: in expansion of macro 'APP_USBD_CLASS_INSTANCE_INITVAL'
    2> C:/Trabajo/firmware/nRF52xx/nRF5_SDK_17.1.0/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd_internal.h:169:5: note: in expansion of macro 'APP_USBD_CLASS_INST_GLOBAL_DEF'
    2> C:/Trabajo/firmware/nRF52xx/nRF5_SDK_17.1.0/components/libraries/usbd/class/hid/kbd/app_usbd_hid_kbd.h:233:9: note: in expansion of macro 'APP_USBD_HID_KBD_GLOBAL_DEF_INTERNAL'
    2> C:\Trabajo\firmware\nRF52xx\nRF5_SDK_17.1.0\defy_wireless_firmware\main.cpp:189:1: note: in expansion of macro 'APP_USBD_HID_KBD_GLOBAL_DEF'

    I have tried to rewrite the HID for a composite keyboard + mouse device but the way the HID is defined in the library via macros makes it humanly impossible to decipher. Any suggestion?

  • The SDK is not written to support C++, only C. Unfortunately, this is not something we can support you on.

    There are a few threads regarding C++ on this forum already, you may check some of them out to see if it can help you resolve your issue.

Related