I found a solution! May be there are better solutions, but the Arduino Due super programmers seem to be absent from this forum, so we will never know.
Put the following code in the setup() function:
void setup(){
// --------------------------------------------------------------------------------------
// This will set 5V to the native USB port even if an external power supply is connected.
PIOB->PIO_PER |= 1<<10; // Never do this once Serial.begin() is called,
PIOB->PIO_OER |= 1<<10; // it will disconnect the programming USB port from the PC.
PIOB->PIO_SODR |= 1<<10; // UOTGVBOF HIGH
// --------------------------------------------------------------------------------------
Serial.begin(9600);
}
The solution code conflicts with the Serial() methods, but if one does it before calling a Serial() method, it will work.
Here a short sketch which shows (in the Serial Monitor) the keys pressed on the keyboard connected to the native USB port.
And the Arduino Due LED can be switched on/off by pressing h or l on the keyboard attached to the native USB port or by pressing h or l on the PC keyboard in case the Serial Monitor is running.
The LED is also switched by directly accessing the registers in the SAM3X8E ARM Cortex-M3 CPU.
/*
Keyboard Controller
Shows the output of a USB Keyboard connected to the
native USB controller of an Arduino Due Board.
*/
#include <address.h>
#include <adk.h>
#include <confdescparser.h>
#include <hid.h>
#include <hidboot.h>
#include <hidusagestr.h>
#include <KeyboardController.h>
#include <MouseController.h>
#include <parsetools.h>
#include <Usb.h>
#include <usb_ch9.h>
char inByte = 0; // incoming character from PC
char Key = 0; // incoming character from keyboard
int rawKey = 0; // incomming key as integer from keyboard
USBHost usb; // USB Controller
KeyboardController keyboard(usb); // Attach Keyboard controller to USB
void setup(){
// --------------------------------------------------------------------------------------
// This will set 5V to the native USB port even if an external power supply is connected.
PIOB->PIO_PER |= 1<<10; // Never do this once Serial.begin() is called,
PIOB->PIO_OER |= 1<<10; // it will disconnect the programming USB port from the PC.
PIOB->PIO_SODR |= 1<<10; // UOTGVBOF HIGH
// --------------------------------------------------------------------------------------
Serial.begin(9600);
}
void loop(){
if (Serial.available() > 0) {
inByte = Serial.read();
Serial.print("from PC : ");
Serial.println(inByte);
if (inByte == 'h') {
PIOB->PIO_PER |= 1<<27;
PIOB->PIO_OER |= 1<<27;
PIOB->PIO_SODR |= 1<<27; // LED HIGH, direct access to Pin 27 at I/O PortB
Serial.println("from PC LED ON");
}
if (inByte == 'l') {
PIOB->PIO_PER |= 1<<27;
PIOB->PIO_OER |= 1<<27;
PIOB->PIO_CODR |= 1<<27; // LED LOW, direct access to Pin 27 at I/O PortB
Serial.println("from PC LED OUT");
}
}
usb.Task();
}
void keyPressed() {
Serial.print("Raw Key: ");
rawKey = keyboard.getKey();
Serial.print(rawKey);
Serial.print(" - Character: ");
Key = rawKey;
Serial.print(Key);
Serial.print(" - OEM: ");
Serial.print(keyboard.getOemKey());
Serial.print(" - mod: ");
Serial.print(keyboard.getModifiers());
Serial.println();
if (Key == 'h') {
PIOB->PIO_PER |= 1<<27;
PIOB->PIO_OER |= 1<<27;
PIOB->PIO_SODR |= 1<<27; // LED HIGH, direct access to Pin 27 at I/O PortB
Serial.println("from KB LED ON");
}
if (Key == 'l') {
PIOB->PIO_PER |= 1<<27;
PIOB->PIO_OER |= 1<<27;
PIOB->PIO_CODR |= 1<<27; // LED LOW, direct access to Pin 27 at I/O PortB
Serial.println("from KB LED OUT");
}
}
In the directory (on Winows PCs) C:\arduino-1.5.1r2\hardware\arduino\sam\system\CMSIS\Device\ATMEL\sam3xa\html one finds many very useful register references and explications.
Greetings, Conrad