Skip to content

[RFC] Pluggable USB core for AVR #3304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
Jul 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
09460dd
Remove HID core library
facchinm Jun 5, 2015
5ee69a7
Move EP defines to header
facchinm Jun 5, 2015
2c5dd20
Use generic composite device descriptor
facchinm Jun 5, 2015
1f534ea
move _initEndpoints from PROGMEM to RAM
facchinm Jun 5, 2015
1aec25b
add PluggableUSB module
facchinm Jun 5, 2015
ec43b6c
remove useless variable
facchinm Jun 5, 2015
b2a6b61
add weak setupUSB() hook
facchinm Jun 5, 2015
018fb96
move HID to general external library
facchinm Jun 5, 2015
91ccab4
make CDC function non removable
facchinm Jun 8, 2015
2aa2332
add arduino header to HID library
facchinm Jun 8, 2015
f67318a
remove useless variables
facchinm Jun 8, 2015
7b5c25f
implement PUSB modules as linked list
facchinm Jun 8, 2015
4a55205
remove Mouse and Keyboard from HID module
facchinm Jun 8, 2015
c2a083b
standalone Mouse library
facchinm Jun 8, 2015
fe825c8
standalone Keyboard library
facchinm Jun 8, 2015
feaa14f
standalone MouseAndKeyboard library
facchinm Jun 8, 2015
344896e
Fix HID derived libraries and add automatic setupUSB() weak hook
facchinm Jun 8, 2015
8f0a433
export WEAK macro
facchinm Jun 8, 2015
a031921
add stub MIDIUSB library
facchinm Jun 8, 2015
f37547e
add numInterfaces field to PUSBCallbacks
facchinm Jun 8, 2015
ada0e4c
remove 3 endpoints to match at32u4 limit
facchinm Jun 12, 2015
89928b4
rework PUSBCallbacks initialization
facchinm Jun 23, 2015
0713231
enforce single use of HID submodule
facchinm Jun 23, 2015
e211f1e
remove setupUSB weak hook and replace with global constructors
facchinm Jun 23, 2015
91a115a
move HID library to AVR specific location
facchinm Jun 26, 2015
175240a
Add support for waking up a host via USB HID
facchinm Jun 26, 2015
a989b72
squash of Overhaul USB HID as a library
facchinm Jun 26, 2015
9074b1e
fix HID descriptors bigger than 127 bytes
facchinm Jun 26, 2015
dee43a1
fix HID headers
facchinm Jul 1, 2015
97a3771
rename Setup typedef struct to USBSetup
facchinm Jul 1, 2015
5defaea
rework HID class functions scopes
facchinm Jul 1, 2015
8a45883
save RAM content overridden by bootloader magic
facchinm Jul 1, 2015
6a9568d
fix pluggableUSB linked list
facchinm Jul 2, 2015
e1a0350
allow HID submodules to create runtime descriptors
facchinm Jul 2, 2015
efd329b
fix MIDIUSB and adapt CompleteHID to PluggableHID
facchinm Jul 2, 2015
8ebc5d0
remove stub MIDIUSB library
facchinm Jul 15, 2015
5cc7c10
remove CompleteHID library
facchinm Jul 15, 2015
9981435
add includes to USB examples
facchinm Jul 15, 2015
af290fc
rework HID-based libraries and add Due fallback
facchinm Jul 15, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#define WINDOWS 1
#define UBUNTU 2

#include "Keyboard.h"
#include "HID.h"

// change this to match your platform:
int platform = OSX;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
http://www.arduino.cc/en/Tutorial/KeyboardMessage
*/

#include "Keyboard.h"
#include "HID.h"

const int buttonPin = 4; // input pin for pushbutton
int previousButtonState = HIGH; // for checking the state of a pushButton
int counter = 0; // button push counter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
http://www.arduino.cc/en/Tutorial/KeyboardReprogram
*/

#include "Keyboard.h"
#include "HID.h"

// use this option for OSX.
// Comment it out if using Windows or Linux:
char ctrlKey = KEY_LEFT_GUI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
http://www.arduino.cc/en/Tutorial/KeyboardSerial
*/

#include "Keyboard.h"
#include "HID.h"

void setup() {
// open the serial port:
Serial.begin(9600);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

*/

#include "Keyboard.h"
#include "Mouse.h"
#include "HID.h"

// set pin numbers for the five buttons:
const int upButton = 2;
const int downButton = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

*/

#include "Mouse.h"
#include "HID.h"

// set pin numbers for the five buttons:
const int upButton = 2;
const int downButton = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

*/

#include "Mouse.h"
#include "HID.h"

// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 2; // switch to turn on and off mouse control
const int mouseButton = 3; // input pin for the mouse pushButton
Expand Down
9 changes: 4 additions & 5 deletions hardware/arduino/avr/cores/arduino/CDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <avr/wdt.h>

#if defined(USBCON)
#ifdef CDC_ENABLED

typedef struct
{
Expand Down Expand Up @@ -54,13 +53,13 @@ const CDCDescriptor _cdcInterface =
D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
};

int WEAK CDC_GetInterface(u8* interfaceNum)
int CDC_GetInterface(u8* interfaceNum)
{
interfaceNum[0] += 2; // uses 2
return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
}

bool WEAK CDC_Setup(Setup& setup)
bool CDC_Setup(USBSetup& setup)
{
u8 r = setup.bRequest;
u8 requestType = setup.bmRequestType;
Expand Down Expand Up @@ -96,6 +95,7 @@ bool WEAK CDC_Setup(Setup& setup)
// We check DTR state to determine if host port is open (bit 0 of lineState).
if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0)
{
*(uint16_t *)(RAMEND-1) = *(uint16_t *)0x0800;
*(uint16_t *)0x0800 = 0x7777;
wdt_enable(WDTO_120MS);
}
Expand All @@ -108,7 +108,7 @@ bool WEAK CDC_Setup(Setup& setup)

wdt_disable();
wdt_reset();
*(uint16_t *)0x0800 = 0x0;
*(uint16_t *)0x0800 = *(uint16_t *)(RAMEND-1);
}
}
return true;
Expand Down Expand Up @@ -207,5 +207,4 @@ Serial_::operator bool() {

Serial_ Serial;

#endif
#endif /* if defined(USBCON) */
100 changes: 100 additions & 0 deletions hardware/arduino/avr/cores/arduino/PluggableUSB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
PluggableUSB.cpp
Copyright (c) 2015 Arduino LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "USBAPI.h"
#include "PluggableUSB.h"

#if defined(USBCON)
#ifdef PLUGGABLE_USB_ENABLED

#define MAX_MODULES 6

static u8 lastIf = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT;
static u8 lastEp = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT;

extern u8 _initEndpoints[];

//PUSBCallbacks cbs[MAX_MODULES];
static u8 modules_count = 0;

static PUSBListNode* rootNode = NULL;

int PUSB_GetInterface(u8* interfaceNum)
{
int ret = 0;
PUSBListNode* node = rootNode;
for (u8 i=0; i<modules_count; i++) {
ret = node->cb->getInterface(interfaceNum);
node = node->next;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posted the comment in a commit, but still applies here. I am not sure if all those functions make so much sense with the retvalue. Also lastNode is not really used.

}
return ret;
}

int PUSB_GetDescriptor(int8_t t)
{
int ret = 0;
PUSBListNode* node = rootNode;
for (u8 i=0; i<modules_count && ret == 0; i++) {
ret = node->cb->getDescriptor(t);
node = node->next;
}
return ret;
}

bool PUSB_Setup(USBSetup& setup, u8 j)
{
bool ret = false;
PUSBListNode* node = rootNode;
for (u8 i=0; i<modules_count && ret == false; i++) {
ret = node->cb->setup(setup, j);
node = node->next;
}
return ret;
}

int8_t PUSB_AddFunction(PUSBListNode *node, u8* interface)
{
if (modules_count >= MAX_MODULES) {
return 0;
}

if (modules_count == 0) {
rootNode = node;
} else {
PUSBListNode *current = rootNode;
while(current->next != NULL) {
current = current->next;
}
current->next = node;
}

*interface = lastIf;
lastIf += node->cb->numInterfaces;
for ( u8 i = 0; i< node->cb->numEndpoints; i++) {
_initEndpoints[lastEp] = node->cb->endpointType[i];
lastEp++;
}
modules_count++;
return lastEp - node->cb->numEndpoints;
// restart USB layer???
}

#endif

#endif /* if defined(USBCON) */
63 changes: 63 additions & 0 deletions hardware/arduino/avr/cores/arduino/PluggableUSB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
PluggableUSB.h
Copyright (c) 2015 Arduino LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef PUSB_h
#define PUSB_h

#include "USBAPI.h"
#include <stdint.h>

#if defined(USBCON)

typedef struct __attribute__((packed))
{
bool (*setup)(USBSetup& setup, u8 i);
int (*getInterface)(u8* interfaceNum);
int (*getDescriptor)(int8_t t);
int8_t numEndpoints;
int8_t numInterfaces;
uint8_t *endpointType;
} PUSBCallbacks;

typedef struct
{
u8 interface;
u8 firstEndpoint;
} PUSBReturn;

class PUSBListNode {
public:
PUSBListNode *next = NULL;
PUSBCallbacks *cb;
PUSBListNode(PUSBCallbacks *ncb) {cb = ncb;}
};

int8_t PUSB_AddFunction(PUSBListNode *node, u8 *interface);

int PUSB_GetInterface(u8* interfaceNum);

int PUSB_GetDescriptor(int8_t t);

bool PUSB_Setup(USBSetup& setup, u8 i);

void PUSB_Begin();

#endif

#endif
Loading