[136] | 1 | /*
|
---|
| 2 | Arduino Y炭n USB-to-Serial
|
---|
| 3 |
|
---|
| 4 | Allows you to use the Y炭n's 32U4 processor as a
|
---|
| 5 | serial terminal for the Linux side on the Y炭n.
|
---|
| 6 |
|
---|
| 7 | Upload this to an Arduino Y炭n via serial (not WiFi) then open
|
---|
| 8 | the serial monitor at 115200 to see the boot process of Linux.
|
---|
| 9 | You can also use the serial monitor as a basic command line
|
---|
| 10 | interface for Linux using this sketch.
|
---|
| 11 |
|
---|
| 12 | From the serial monitor the following commands can be issued:
|
---|
| 13 |
|
---|
| 14 | '~' followed by '0' -> Set the UART speed to 57600 baud
|
---|
| 15 | '~' followed by '1' -> Set the UART speed to 115200 baud
|
---|
| 16 | '~' followed by '2' -> Set the UART speed to 250000 baud
|
---|
| 17 | '~' followed by '3' -> Set the UART speed to 500000 baud
|
---|
| 18 | '~' followed by '~' -> Sends the bridge's shutdown command to
|
---|
| 19 | obtain the console.
|
---|
| 20 |
|
---|
| 21 | The circuit:
|
---|
| 22 | * Arduino Y炭n
|
---|
| 23 |
|
---|
| 24 | created March 2013
|
---|
| 25 | by Massimo Banzi
|
---|
| 26 | modified by Cristian Maglie
|
---|
| 27 |
|
---|
| 28 | This example code is in the public domain.
|
---|
| 29 |
|
---|
| 30 | http://arduino.cc/en/Tutorial/YunSerialTerminal
|
---|
| 31 |
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | long linuxBaud = 250000;
|
---|
| 36 | const int handshake = 7; // the number of the HANDSHAKE pin
|
---|
| 37 |
|
---|
| 38 | void setup() {
|
---|
| 39 | Serial.begin(115200); // open serial connection via USB-Serial
|
---|
| 40 | Serial1.begin(linuxBaud); // open serial connection to Linux
|
---|
| 41 | pinMode(handshake, OUTPUT);
|
---|
| 42 | digitalWrite(handshake, LOW);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | boolean commandMode = false;
|
---|
| 46 |
|
---|
| 47 | void loop() {
|
---|
| 48 | // copy from USB-CDC to UART
|
---|
| 49 | int c = Serial.read(); // read from USB-CDC
|
---|
| 50 | if (c != -1) { // got anything?
|
---|
| 51 | if (commandMode == false) { // if we aren't in command mode...
|
---|
| 52 | if (c == '~') { // Tilde '~' key pressed?
|
---|
| 53 | commandMode = true; // enter in command mode
|
---|
| 54 | } else {
|
---|
| 55 | Serial1.write(c); // otherwise write char to UART
|
---|
| 56 | }
|
---|
| 57 | } else { // if we are in command mode...
|
---|
| 58 | if (c == '0') { // '0' key pressed?
|
---|
| 59 | Serial1.begin(57600); // set speed to 57600
|
---|
| 60 | Serial.println("Speed set to 57600");
|
---|
| 61 | } else if (c == '1') { // '1' key pressed?
|
---|
| 62 | Serial1.begin(115200); // set speed to 115200
|
---|
| 63 | Serial.println("Speed set to 115200");
|
---|
| 64 | } else if (c == '2') { // '2' key pressed?
|
---|
| 65 | Serial1.begin(250000); // set speed to 250000
|
---|
| 66 | Serial.println("Speed set to 250000");
|
---|
| 67 | } else if (c == '3') { // '3' key pressed?
|
---|
| 68 | Serial1.begin(500000); // set speed to 500000
|
---|
| 69 | Serial.println("Speed set to 500000");
|
---|
| 70 | } else if (c == '~') { // '~` key pressed?
|
---|
| 71 | // send "bridge shutdown" command
|
---|
| 72 | Serial1.write((uint8_t *)"\xff\0\0\x05XXXXX\x7f\xf9", 11);
|
---|
| 73 | Serial.println("Sending bridge's shutdown command");
|
---|
| 74 | } else { // any other key pressed?
|
---|
| 75 | Serial1.write('~'); // write '~' to UART
|
---|
| 76 | Serial1.write(c); // write char to UART
|
---|
| 77 | }
|
---|
| 78 | commandMode = false; // in all cases exit from command mode
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | // copy from UART to USB-CDC
|
---|
| 83 | c = Serial1.read(); // read from UART
|
---|
| 84 | if (c != -1) { // got anything?
|
---|
| 85 | Serial.write(c); // write to USB-CDC
|
---|
| 86 | }
|
---|
| 87 | }
|
---|