From 39b0048ed54901b2b268b074a69065a3bd0ba4cd Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Tue, 6 Jul 2021 09:46:32 +0100 Subject: [PATCH 01/12] Fix bcdHID value. --- examples/AN00129_hid_class/src/endpoint0.xc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/AN00129_hid_class/src/endpoint0.xc b/examples/AN00129_hid_class/src/endpoint0.xc index 60bbafee..6aa13dae 100644 --- a/examples/AN00129_hid_class/src/endpoint0.xc +++ b/examples/AN00129_hid_class/src/endpoint0.xc @@ -81,8 +81,8 @@ static unsigned char cfgDesc[] = { 0x09, /* 0 bLength. Note this is currently replicated in hidDescriptor[] below */ 0x21, /* 1 bDescriptorType (HID) */ - 0x10, /* 2 bcdHID */ - 0x11, /* 3 bcdHID */ + 0x11, /* 2 bcdHID */ + 0x01, /* 3 bcdHID */ 0x00, /* 4 bCountryCode */ 0x01, /* 5 bNumDescriptors */ 0x22, /* 6 bDescriptorType[0] (Report) */ @@ -102,8 +102,8 @@ static unsigned char hidDescriptor[] = { 0x09, /* 0 bLength */ 0x21, /* 1 bDescriptorType (HID) */ - 0x10, /* 2 bcdHID */ - 0x11, /* 3 bcdHID */ + 0x11, /* 2 bcdHID */ + 0x01, /* 3 bcdHID */ 0x00, /* 4 bCountryCode */ 0x01, /* 5 bNumDescriptors */ 0x22, /* 6 bDescriptorType[0] (Report) */ From a6e4f859986b5ffca2697c5227cdca49b913c248 Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Wed, 7 Jul 2021 17:13:36 +0100 Subject: [PATCH 02/12] Remove unneeded .xn file. --- .../AN00129_hid_class/src/mic_array_ref.xn | 84 ------------------- 1 file changed, 84 deletions(-) delete mode 100644 examples/AN00129_hid_class/src/mic_array_ref.xn diff --git a/examples/AN00129_hid_class/src/mic_array_ref.xn b/examples/AN00129_hid_class/src/mic_array_ref.xn deleted file mode 100644 index dc790657..00000000 --- a/examples/AN00129_hid_class/src/mic_array_ref.xn +++ /dev/null @@ -1,84 +0,0 @@ - - - Board - XS2 MC Audio - - tileref tile[2] - tileref usb_tile - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From ee48ba5f945ffffa6080ceda6d06601d228b7e9d Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Wed, 7 Jul 2021 17:19:44 +0100 Subject: [PATCH 03/12] Modified makefile configs to match other demos. --- examples/AN00129_hid_class/Makefile | 38 ++++++++++++++--------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/examples/AN00129_hid_class/Makefile b/examples/AN00129_hid_class/Makefile index 5d8ac82e..8b92460a 100644 --- a/examples/AN00129_hid_class/Makefile +++ b/examples/AN00129_hid_class/Makefile @@ -1,17 +1,7 @@ -# The TARGET variable determines what target system the application is -# compiled for. It either refers to an XN file in the source directories -# or a valid argument for the --target option when compiling. - -# In this case, the target depends on the build configuration. -ifeq ($(CONFIG),X200) -TARGET = XCORE-200-EXPLORER -else -TARGET = XCORE-AI-EXPLORER-NO-DDR.xn -endif - # The APP_NAME variable determines the name of the final .xe file. It should -# not include the .xe postfix. If left blank the name will default to +# not include the .xe postfix. If left blank the name will default to # the project name + APP_NAME = app_hid_mouse_demo # The flags passed to xcc when building the application @@ -19,16 +9,26 @@ APP_NAME = app_hid_mouse_demo # # XCC_XC_FLAGS, XCC_C_FLAGS, XCC_ASM_FLAGS, XCC_CPP_FLAGS # -# If the variable XCC_MAP_FLAGS is set it overrides the flags passed to +# If the variable XCC_MAP_FLAGS is set it overrides the flags passed to # xcc for the final link (mapping) stage. -# These flags define two build configurations - one for U-series and one for -# the xCORE-200 series. +BUILD_FLAGS = -O3 -g -report + +XCC_FLAGS_xcore200 = $(BUILD_FLAGS) -DXUD_SERIES_SUPPORT=XUD_X200_SERIES +XCC_FLAGS_xcoreai = $(BUILD_FLAGS) -XCC_FLAGS_X200 = -O3 -report -DXUD_SERIES_SUPPORT=XUD_X200_SERIES -XCC_FLAGS_AI = -O3 -report +# The TARGET variable determines what target system the application is +# compiled for. It either refers to an XN file in the source directories +# or a valid argument for the --target option when compiling. + +ifeq ($(CONFIG),xcoreai) +TARGET = XCORE-AI-EXPLORER-NO-DDR +else +TARGET = XCORE-200-EXPLORER +endif + +# The USED_MODULES variable lists other module used by the application. -# The USED_MODULES variable lists other module used by the application. USED_MODULES = lib_xud #============================================================================= @@ -37,5 +37,3 @@ USED_MODULES = lib_xud XMOS_MAKE_PATH ?= ../.. include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile.common - - From eb7f80b512c4c0e44d3f743895334d3e57445a27 Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Wed, 14 Jul 2021 18:32:15 +0100 Subject: [PATCH 04/12] HID demo fixed on linux (x86_64 & arm), tested on mac, win, and across xcore{ai,200} arches. --- examples/AN00129_hid_class/src/endpoint0.xc | 72 +++++++++------------ 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/examples/AN00129_hid_class/src/endpoint0.xc b/examples/AN00129_hid_class/src/endpoint0.xc index 6aa13dae..3f235f48 100644 --- a/examples/AN00129_hid_class/src/endpoint0.xc +++ b/examples/AN00129_hid_class/src/endpoint0.xc @@ -86,7 +86,7 @@ static unsigned char cfgDesc[] = { 0x00, /* 4 bCountryCode */ 0x01, /* 5 bNumDescriptors */ 0x22, /* 6 bDescriptorType[0] (Report) */ - 0x48, /* 7 wDescriptorLength */ + 0x32, /* 7 wDescriptorLength */ 0x00, /* 8 wDescriptorLength */ 0x07, /* 0 bLength */ @@ -95,7 +95,7 @@ static unsigned char cfgDesc[] = { 0x03, /* 3 bmAttributes */ 0x40, /* 4 wMaxPacketSize */ 0x00, /* 5 wMaxPacketSize */ - 0x01 /* 6 bInterval */ + 0x0a /* 6 bInterval */ }; static unsigned char hidDescriptor[] = @@ -107,50 +107,42 @@ static unsigned char hidDescriptor[] = 0x00, /* 4 bCountryCode */ 0x01, /* 5 bNumDescriptors */ 0x22, /* 6 bDescriptorType[0] (Report) */ - 0x48, /* 7 wDescriptorLength */ + 0x32, /* 7 wDescriptorLength */ 0x00, /* 8 wDescriptorLength */ }; /* HID Report Descriptor */ static unsigned char hidReportDescriptor[] = { - 0x05, 0x01, // Usage page (desktop) - 0x09, 0x02, // Usage (mouse) - 0xA1, 0x01, // Collection (app) - 0x05, 0x09, // Usage page (buttons) - 0x19, 0x01, - 0x29, 0x03, - 0x15, 0x00, // Logical min (0) - 0x25, 0x01, // Logical max (1) - 0x95, 0x03, // Report count (3) - 0x75, 0x01, // Report size (1) - 0x81, 0x02, // Input (Data, Absolute) - 0x95, 0x01, // Report count (1) - 0x75, 0x05, // Report size (5) - 0x81, 0x03, // Input (Absolute, Constant) - 0x05, 0x01, // Usage page (desktop) - 0x09, 0x01, // Usage (pointer) - 0xA1, 0x00, // Collection (phys) - 0x09, 0x30, // Usage (x) - 0x09, 0x31, // Usage (y) - 0x15, 0x81, // Logical min (-127) - 0x25, 0x7F, // Logical max (127) - 0x75, 0x08, // Report size (8) - 0x95, 0x02, // Report count (2) - 0x81, 0x06, // Input (Data, Rel=0x6, Abs=0x2) - 0xC0, // End collection - 0x09, 0x38, // Usage (Wheel) - 0x95, 0x01, // Report count (1) - 0x81, 0x02, // Input (Data, Relative) - 0x09, 0x3C, // Usage (Motion Wakeup) - 0x15, 0x00, // Logical min (0) - 0x25, 0x01, // Logical max (1) - 0x75, 0x01, // Report size (1) - 0x95, 0x01, // Report count (1) - 0xB1, 0x22, // Feature (No preferred, Variable) - 0x95, 0x07, // Report count (7) - 0xB1, 0x01, // Feature (Constant) - 0xC0 // End collection + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x02, /* Usage (Mouse) */ + 0xA1, 0x01, /* Collection (Application) */ + 0x09, 0x01, /* Usage (Pointer) */ + 0xA1, 0x00, /* Collection (Physical) */ + 0x05, 0x09, /* Usage Page (Buttons) */ + 0x19, 0x01, /* Usage Minimum (01) */ + 0x29, 0x03, /* Usage Maximum (03) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x01, /* Logical Maximum (1) */ + 0x95, 0x03, /* Report Count (3) */ + 0x75, 0x01, /* Report Size (1) */ + 0x81, 0x02, /* Input (Data,Variable,Absolute); 3 button bits */ + 0x95, 0x01, /* Report Count (1) */ + 0x75, 0x05, /* Report Size (5) */ + 0x81, 0x01, /* Input(Constant); 5 bit padding */ + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x30, /* Usage (X) */ + 0x09, 0x31, /* Usage (Y) */ + 0x15, 0x81, /* Logical Minimum (-127) */ + 0x25, 0x7F, /* Logical Maximum (127) */ + 0x75, 0x08, /* Report Size (8) */ + 0x95, 0x02, /* Report Count (2) */ + 0x81, 0x06, /* Input (Data,Variable,Relative); 2 position bytes (X & Y) */ + //0x95, 0x01, /* Report Count (1) */ + //0x75, 0x05, /* Report Size (8) */ + //0x81, 0x01, /* Input(Constant); 8 bit padding */ + 0xC0, /* End Collection */ + 0xC0 /* End Collection */ }; unsafe{ From c55491b28221a4a364139c867f327abd1b5aef95 Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Wed, 14 Jul 2021 18:34:04 +0100 Subject: [PATCH 05/12] Remove commented data. --- examples/AN00129_hid_class/src/endpoint0.xc | 3 --- 1 file changed, 3 deletions(-) diff --git a/examples/AN00129_hid_class/src/endpoint0.xc b/examples/AN00129_hid_class/src/endpoint0.xc index 3f235f48..836a4457 100644 --- a/examples/AN00129_hid_class/src/endpoint0.xc +++ b/examples/AN00129_hid_class/src/endpoint0.xc @@ -138,9 +138,6 @@ static unsigned char hidReportDescriptor[] = 0x75, 0x08, /* Report Size (8) */ 0x95, 0x02, /* Report Count (2) */ 0x81, 0x06, /* Input (Data,Variable,Relative); 2 position bytes (X & Y) */ - //0x95, 0x01, /* Report Count (1) */ - //0x75, 0x05, /* Report Size (8) */ - //0x81, 0x01, /* Input(Constant); 8 bit padding */ 0xC0, /* End Collection */ 0xC0 /* End Collection */ }; From 902d4188c59a131c0ca06e55149a065668635b42 Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Thu, 15 Jul 2021 09:29:55 +0100 Subject: [PATCH 06/12] Remove duplicate HID definitions. --- examples/AN00129_hid_class/src/endpoint0.xc | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/examples/AN00129_hid_class/src/endpoint0.xc b/examples/AN00129_hid_class/src/endpoint0.xc index 836a4457..da18beca 100644 --- a/examples/AN00129_hid_class/src/endpoint0.xc +++ b/examples/AN00129_hid_class/src/endpoint0.xc @@ -12,27 +12,6 @@ #define VENDOR_ID 0x20B1 #define PRODUCT_ID 0x1010 -/* Standard HID Request Defines */ - -/* 7. Requests */ - -/* 7.1 Standard Requests - Class Descriptor Types - High byte of wValue - * The following defines valid types of Class descriptors */ - -#define HID_HID 0x2100 -#define HID_REPORT 0x2200 -#define HID_PHYSICAL_DESCRIPTOR 0x2300 -/* 0x24 - 0x2F: Reserved */ - -/* 7.2 Class-Specific Requests - bRequest values */ -#define HID_GET_REPORT 0x01 /* Mandatory */ -#define HID_GET_IDLE 0x02 -#define HID_GET_PROTOCOL 0x03 /* Required only for boot devices */ -/* Ox04 - 0x08 reserved */ -#define HID_SET_REPORT 0x09 -#define HID_SET_IDLE 0x0A -#define HID_SET_PROTOCOL 0x0B /* Required only for boot devices */ - /* Device Descriptor */ static unsigned char devDesc[] = { From fa21dafd06dbc67e828930573e3f319640231596 Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Thu, 15 Jul 2021 09:57:16 +0100 Subject: [PATCH 07/12] Fix hid_mouse() to send correct buffer length, and only when data has changed. --- examples/AN00129_hid_class/src/main.xc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/AN00129_hid_class/src/main.xc b/examples/AN00129_hid_class/src/main.xc index 0235a212..bb18b233 100644 --- a/examples/AN00129_hid_class/src/main.xc +++ b/examples/AN00129_hid_class/src/main.xc @@ -68,10 +68,10 @@ void hid_mouse(chanend chan_ep_hid) p_reportBuffer[2] = -40; state = 0; } - } - /* Send the buffer off to the host. Note this will return when complete */ - XUD_SetBuffer(ep_hid, (char *) p_reportBuffer, 4); + /* Send the buffer off to the host. Note this will return when complete */ + XUD_SetBuffer(ep_hid, (char *) p_reportBuffer, 3); + } } } } From 278935891ce1733240812ee1dea150565ab17a2a Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Thu, 15 Jul 2021 09:59:33 +0100 Subject: [PATCH 08/12] Corrected code indentation. --- examples/AN00129_hid_class/src/endpoint0.xc | 8 -- examples/AN00129_hid_class/src/main.xc | 85 ++++++++++----------- 2 files changed, 42 insertions(+), 51 deletions(-) diff --git a/examples/AN00129_hid_class/src/endpoint0.xc b/examples/AN00129_hid_class/src/endpoint0.xc index da18beca..4b3f0ee3 100644 --- a/examples/AN00129_hid_class/src/endpoint0.xc +++ b/examples/AN00129_hid_class/src/endpoint0.xc @@ -295,11 +295,3 @@ void Endpoint0(chanend chan_ep0_out, chanend chan_ep0_in) } } } -//: - - - - - - - diff --git a/examples/AN00129_hid_class/src/main.xc b/examples/AN00129_hid_class/src/main.xc index bb18b233..0e410312 100644 --- a/examples/AN00129_hid_class/src/main.xc +++ b/examples/AN00129_hid_class/src/main.xc @@ -32,47 +32,47 @@ void hid_mouse(chanend chan_ep_hid) while (1) { - /* Unsafe region so we can use shared memory. */ - unsafe { - char * unsafe p_reportBuffer = g_reportBuffer; - int x; - p_reportBuffer[1] = 0; - p_reportBuffer[2] = 0; - - /* Move the pointer around in a square (relative) */ - counter++; - if (counter >= 500) - { - counter = 0; - if (state == 0) + /* Unsafe region so we can use shared memory. */ + unsafe { + char * unsafe p_reportBuffer = g_reportBuffer; + int x; + p_reportBuffer[1] = 0; + p_reportBuffer[2] = 0; + + /* Move the pointer around in a square (relative) */ + counter++; + if (counter >= 500) { - p_reportBuffer[1] = 40; - p_reportBuffer[2] = 0; - state+=1; + counter = 0; + if (state == 0) + { + p_reportBuffer[1] = 40; + p_reportBuffer[2] = 0; + state+=1; + } + else if (state == 1) + { + p_reportBuffer[1] = 0; + p_reportBuffer[2] = 40; + state+=1; + } + else if (state == 2) + { + p_reportBuffer[1] = -40; + p_reportBuffer[2] = 0; + state+=1; + } + else if (state == 3) + { + p_reportBuffer[1] = 0; + p_reportBuffer[2] = -40; + state = 0; + } + + /* Send the buffer off to the host. Note this will return when complete */ + XUD_SetBuffer(ep_hid, (char *) p_reportBuffer, 3); } - else if (state == 1) - { - p_reportBuffer[1] = 0; - p_reportBuffer[2] = 40; - state+=1; - } - else if (state == 2) - { - p_reportBuffer[1] = -40; - p_reportBuffer[2] = 0; - state+=1; - } - else if (state == 3) - { - p_reportBuffer[1] = 0; - p_reportBuffer[2] = -40; - state = 0; - } - - /* Send the buffer off to the host. Note this will return when complete */ - XUD_SetBuffer(ep_hid, (char *) p_reportBuffer, 3); } - } } } @@ -88,13 +88,12 @@ int main() par { - on tile[0]: XUD_Main(c_ep_out, EP_COUNT_OUT, c_ep_in, EP_COUNT_IN, - null, epTypeTableOut, epTypeTableIn, XUD_SPEED_HS, XUD_PWR_BUS); - - on tile[0]: Endpoint0(c_ep_out[0], c_ep_in[0]); + on tile[0]: XUD_Main(c_ep_out, EP_COUNT_OUT, c_ep_in, EP_COUNT_IN, null, + epTypeTableOut, epTypeTableIn, XUD_SPEED_HS, XUD_PWR_BUS); - on tile[0]: hid_mouse(c_ep_in[1]); + on tile[0]: Endpoint0(c_ep_out[0], c_ep_in[0]); + on tile[0]: hid_mouse(c_ep_in[1]); } return 0; From 13af8c9a581ac70606ce98a3c2da1afebfa8e269 Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Thu, 15 Jul 2021 10:21:58 +0100 Subject: [PATCH 09/12] Refactored global buffer definition. --- examples/AN00129_hid_class/src/endpoint0.xc | 50 +++++++++++---------- examples/AN00129_hid_class/src/hid_defs.h | 9 ++++ examples/AN00129_hid_class/src/main.xc | 7 +-- 3 files changed, 40 insertions(+), 26 deletions(-) create mode 100644 examples/AN00129_hid_class/src/hid_defs.h diff --git a/examples/AN00129_hid_class/src/endpoint0.xc b/examples/AN00129_hid_class/src/endpoint0.xc index 4b3f0ee3..c0050f67 100644 --- a/examples/AN00129_hid_class/src/endpoint0.xc +++ b/examples/AN00129_hid_class/src/endpoint0.xc @@ -6,6 +6,7 @@ #include #include "xud_device.h" #include "hid.h" +#include "hid_defs.h" /* USB HID Device Product Defines */ #define BCD_DEVICE 0x1000 @@ -96,28 +97,28 @@ static unsigned char hidReportDescriptor[] = 0x05, 0x01, /* Usage Page (Generic Desktop) */ 0x09, 0x02, /* Usage (Mouse) */ 0xA1, 0x01, /* Collection (Application) */ - 0x09, 0x01, /* Usage (Pointer) */ - 0xA1, 0x00, /* Collection (Physical) */ - 0x05, 0x09, /* Usage Page (Buttons) */ - 0x19, 0x01, /* Usage Minimum (01) */ - 0x29, 0x03, /* Usage Maximum (03) */ - 0x15, 0x00, /* Logical Minimum (0) */ - 0x25, 0x01, /* Logical Maximum (1) */ - 0x95, 0x03, /* Report Count (3) */ - 0x75, 0x01, /* Report Size (1) */ - 0x81, 0x02, /* Input (Data,Variable,Absolute); 3 button bits */ - 0x95, 0x01, /* Report Count (1) */ - 0x75, 0x05, /* Report Size (5) */ - 0x81, 0x01, /* Input(Constant); 5 bit padding */ - 0x05, 0x01, /* Usage Page (Generic Desktop) */ - 0x09, 0x30, /* Usage (X) */ - 0x09, 0x31, /* Usage (Y) */ - 0x15, 0x81, /* Logical Minimum (-127) */ - 0x25, 0x7F, /* Logical Maximum (127) */ - 0x75, 0x08, /* Report Size (8) */ - 0x95, 0x02, /* Report Count (2) */ - 0x81, 0x06, /* Input (Data,Variable,Relative); 2 position bytes (X & Y) */ - 0xC0, /* End Collection */ + 0x09, 0x01, /* Usage (Pointer) */ + 0xA1, 0x00, /* Collection (Physical) */ + 0x05, 0x09, /* Usage Page (Buttons) */ + 0x19, 0x01, /* Usage Minimum (01) */ + 0x29, 0x03, /* Usage Maximum (03) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x01, /* Logical Maximum (1) */ + 0x95, 0x03, /* Report Count (3) */ + 0x75, 0x01, /* Report Size (1) */ + 0x81, 0x02, /* Input (Data,Variable,Absolute); 3 button bits */ + 0x95, 0x01, /* Report Count (1) */ + 0x75, 0x05, /* Report Size (5) */ + 0x81, 0x01, /* Input(Constant); 5 bit padding */ + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x30, /* Usage (X) */ + 0x09, 0x31, /* Usage (Y) */ + 0x15, 0x81, /* Logical Minimum (-127) */ + 0x25, 0x7F, /* Logical Maximum (127) */ + 0x75, 0x08, /* Report Size (8) */ + 0x95, 0x02, /* Report Count (2) */ + 0x81, 0x06, /* Input (Data,Variable,Relative); 2 position bytes (X & Y) */ + 0xC0, /* End Collection */ 0xC0 /* End Collection */ }; @@ -132,7 +133,10 @@ static char * unsafe stringDescriptors[]= }; } -extern unsigned char g_reportBuffer[4]; +/* It is essential that HID_REPORT_BUFFER_SIZE, defined in hid_defs.h, matches the */ +/* infered length of the report described in hidReportDescriptor above. In this case */ +/* it is three bytes, three button bits padded to a byte, plus a byte each for X & Y */ +unsigned char g_reportBuffer[HID_REPORT_BUFFER_SIZE] = {0, 0, 0}; /* HID Class Requests */ XUD_Result_t HidInterfaceClassRequests(XUD_ep c_ep0_out, XUD_ep c_ep0_in, USB_SetupPacket_t sp) diff --git a/examples/AN00129_hid_class/src/hid_defs.h b/examples/AN00129_hid_class/src/hid_defs.h new file mode 100644 index 00000000..2867394c --- /dev/null +++ b/examples/AN00129_hid_class/src/hid_defs.h @@ -0,0 +1,9 @@ + +#ifndef HID_DEFS_H +#define HID_DEFS_H + +/* Global report buffer */ +#define HID_REPORT_BUFFER_SIZE 3 +extern char g_reportBuffer[HID_REPORT_BUFFER_SIZE]; + +#endif // HID_DEFS_H diff --git a/examples/AN00129_hid_class/src/main.xc b/examples/AN00129_hid_class/src/main.xc index 0e410312..bd356684 100644 --- a/examples/AN00129_hid_class/src/main.xc +++ b/examples/AN00129_hid_class/src/main.xc @@ -1,6 +1,7 @@ // Copyright 2015-2021 XMOS LIMITED. // This Software is subject to the terms of the XMOS Public Licence: Version 1. #include "xud_device.h" +#include "hid_defs.h" /* Number of Endpoints used by this app */ #define EP_COUNT_OUT 1 @@ -16,7 +17,7 @@ XUD_EpType epTypeTableIn[EP_COUNT_IN] = {XUD_EPTYPE_CTL | XUD_STATUS_ENABLE, X void Endpoint0(chanend c_ep0_out, chanend c_ep0_in); /* Global report buffer, global since used by Endpoint0 core */ -unsigned char g_reportBuffer[4] = {0, 0, 0, 0}; +extern unsigned char g_reportBuffer[]; /* * This function responds to the HID requests @@ -30,7 +31,7 @@ void hid_mouse(chanend chan_ep_hid) XUD_ep ep_hid = XUD_InitEp(chan_ep_hid); - while (1) + for(;;) { /* Unsafe region so we can use shared memory. */ unsafe { @@ -70,7 +71,7 @@ void hid_mouse(chanend chan_ep_hid) } /* Send the buffer off to the host. Note this will return when complete */ - XUD_SetBuffer(ep_hid, (char *) p_reportBuffer, 3); + XUD_SetBuffer(ep_hid, (char *) p_reportBuffer, sizeof(g_reportBuffer)); } } } From 98ef49af4983feef5c4236564a11716983317a3c Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Thu, 15 Jul 2021 10:45:38 +0100 Subject: [PATCH 10/12] Refactor hid_mouse(). * Use symbolic constants. * Reduce scope of unsafe code. --- examples/AN00129_hid_class/src/main.xc | 86 +++++++++++++------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/examples/AN00129_hid_class/src/main.xc b/examples/AN00129_hid_class/src/main.xc index bd356684..439b2147 100644 --- a/examples/AN00129_hid_class/src/main.xc +++ b/examples/AN00129_hid_class/src/main.xc @@ -16,9 +16,6 @@ XUD_EpType epTypeTableIn[EP_COUNT_IN] = {XUD_EPTYPE_CTL | XUD_STATUS_ENABLE, X /* Prototype for Endpoint0 function in endpoint0.xc */ void Endpoint0(chanend c_ep0_out, chanend c_ep0_in); -/* Global report buffer, global since used by Endpoint0 core */ -extern unsigned char g_reportBuffer[]; - /* * This function responds to the HID requests * - It draws a square using the mouse moving 40 pixels in each direction @@ -26,66 +23,71 @@ extern unsigned char g_reportBuffer[]; */ void hid_mouse(chanend chan_ep_hid) { - int counter = 0; - int state = 0; - + unsigned int counter = 0; + enum {RIGHT, DOWN, LEFT, UP} state = RIGHT; + XUD_ep ep_hid = XUD_InitEp(chan_ep_hid); for(;;) { - /* Unsafe region so we can use shared memory. */ - unsafe { - char * unsafe p_reportBuffer = g_reportBuffer; + /* Move the pointer around in a square (relative) */ + if(counter++ >= 500) + { int x; - p_reportBuffer[1] = 0; - p_reportBuffer[2] = 0; + int y; + + switch(state) { + case RIGHT: + x = 40; + y = 0; + state = DOWN; + break; + + case DOWN: + x = 0; + y = 40; + state = LEFT; + break; + + case LEFT: + x = -40; + y = 0; + state = UP; + break; + + case UP: + default: + x = 0; + y = -40; + state = RIGHT; + break; + } - /* Move the pointer around in a square (relative) */ - counter++; - if (counter >= 500) - { - counter = 0; - if (state == 0) - { - p_reportBuffer[1] = 40; - p_reportBuffer[2] = 0; - state+=1; - } - else if (state == 1) - { - p_reportBuffer[1] = 0; - p_reportBuffer[2] = 40; - state+=1; - } - else if (state == 2) - { - p_reportBuffer[1] = -40; - p_reportBuffer[2] = 0; - state+=1; - } - else if (state == 3) - { - p_reportBuffer[1] = 0; - p_reportBuffer[2] = -40; - state = 0; - } + /* Unsafe region so we can use shared memory. */ + unsafe { + /* global buffer 'g_reportBuffer' defined in hid_defs.h */ + char * unsafe p_reportBuffer = g_reportBuffer; + + p_reportBuffer[1] = x; + p_reportBuffer[2] = y; /* Send the buffer off to the host. Note this will return when complete */ XUD_SetBuffer(ep_hid, (char *) p_reportBuffer, sizeof(g_reportBuffer)); + counter = 0; } } } } - /* The main function runs three cores: the XUD manager, Endpoint 0, and a HID endpoint. An array of * channels is used for both IN and OUT endpoints, endpoint zero requires both, HID requires just an * IN endpoint to send HID reports to the host. */ int main() { - chan c_ep_out[EP_COUNT_OUT], c_ep_in[EP_COUNT_IN]; + chan c_ep_out[EP_COUNT_OUT]; + chan c_ep_in[EP_COUNT_IN]; par { From 345eae9087dc518aeeb5a1a15400ac91a41191c1 Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Thu, 15 Jul 2021 11:05:40 +0100 Subject: [PATCH 11/12] Update copyright info in hid_defs.h. --- examples/AN00129_hid_class/src/hid_defs.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/AN00129_hid_class/src/hid_defs.h b/examples/AN00129_hid_class/src/hid_defs.h index 2867394c..d246a16e 100644 --- a/examples/AN00129_hid_class/src/hid_defs.h +++ b/examples/AN00129_hid_class/src/hid_defs.h @@ -1,4 +1,9 @@ +// Copyright 2021 XMOS LIMITED. +// This Software is subject to the terms of the XMOS Public Licence: Version 1. +/* + * @brief Defines shared data for HID example threads. + */ #ifndef HID_DEFS_H #define HID_DEFS_H From 87859dfe9b95bb98296b527167971fa7d4fa91d2 Mon Sep 17 00:00:00 2001 From: Dave Atkinson Date: Thu, 15 Jul 2021 14:30:51 +0100 Subject: [PATCH 12/12] Fix incorrect line references in .rst file. --- examples/AN00129_hid_class/doc/rst/AN00129.rst | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/examples/AN00129_hid_class/doc/rst/AN00129.rst b/examples/AN00129_hid_class/doc/rst/AN00129.rst index fadec4df..18582b7e 100644 --- a/examples/AN00129_hid_class/doc/rst/AN00129.rst +++ b/examples/AN00129_hid_class/doc/rst/AN00129.rst @@ -70,15 +70,9 @@ To start using the USB library, you need to add ``lib_xud`` to your ``Makefile`` USED_MODULES = ... lib_xud -You also have to add a define to the build to say which variant of the -USB library you want. In this case, the example is for the U-Series -USB library. So the following is in the ``Makefile``:: +You can then access the USB functions in your source code via the xud_device.h header file:: - XCC_FLAGS = ... -DXUD_SERIES_SUPPORT=XUD_U_SERIES - -You can then access the USB functions in your source code via the usb.h header file:: - - #include + #include "xud_device.h" The application main() function ............................... @@ -197,7 +191,7 @@ USB HID Class requests Inside ``endpoint0.xc`` there is a function for handling the USB HID device class specific requests. The code for handling these requests is shown as follows: .. literalinclude:: endpoint0.xc - :lines: 167-224 + :lines: 142-198 These HID specific requests are implemented by the application as they do not form part of the standard requests which have to be accepted by all device classes via endpoint0. @@ -209,7 +203,7 @@ USB HID Class Endpoint0 The function ``Endpoint0()`` contains the code for dealing with device requests made from the host to the standard endpoint0 which is present in all USB devices. In addition to requests required for all devices, the code handles the requests specific to the HID class. .. literalinclude:: endpoint0.xc - :lines: 260-306 + :lines: 201-301 |newpage| @@ -219,7 +213,7 @@ Reporting HID mouse data to the host The application endpoint for reporting mouse movement data to the host machine is implemented in the file ``main.xc``. This is contained within the function ``hid_mouse()`` which is shown below: .. literalinclude:: main.xc - :lines: 22-81 + :lines: 24-80 From this you can see the following.