Skip to content

Commit

Permalink
add HW pin constants and serial pin config
Browse files Browse the repository at this point in the history
  • Loading branch information
soundanalogous committed Aug 24, 2015
1 parent 5b8bff7 commit 3120222
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 14 deletions.
43 changes: 42 additions & 1 deletion Boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,19 @@ writePort(port, value, bitmask): Write an 8 bit port.
#define TOTAL_ANALOG_PINS 16
#define TOTAL_PINS 70 // 54 digital + 16 analog
#define VERSION_BLINK_PIN 13
#define PIN_SERIAL1_RX 19
#define PIN_SERIAL1_TX 18
#define PIN_SERIAL2_RX 17
#define PIN_SERIAL2_TX 16
#define PIN_SERIAL3_RX 15
#define PIN_SERIAL3_TX 14
#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
#define IS_PIN_ANALOG(p) ((p) >= 54 && (p) < TOTAL_PINS)
#define IS_PIN_PWM(p) digitalPinHasPWM(p)
#define IS_PIN_SERVO(p) ((p) >= 2 && (p) - 2 < MAX_SERVOS)
#define IS_PIN_I2C(p) ((p) == 20 || (p) == 21)
#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
#define IS_PIN_SERIAL(p) ((p) > 13 && (p) < 20)
#define PIN_TO_DIGITAL(p) (p)
#define PIN_TO_ANALOG(p) ((p) - 54)
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
Expand All @@ -217,11 +224,18 @@ writePort(port, value, bitmask): Write an 8 bit port.
#define TOTAL_ANALOG_PINS 12
#define TOTAL_PINS 66 // 54 digital + 12 analog
#define VERSION_BLINK_PIN 13
#define PIN_SERIAL1_RX 19
#define PIN_SERIAL1_TX 18
#define PIN_SERIAL2_RX 17
#define PIN_SERIAL2_TX 16
#define PIN_SERIAL3_RX 15
#define PIN_SERIAL3_TX 14
#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
#define IS_PIN_ANALOG(p) ((p) >= 54 && (p) < TOTAL_PINS)
#define IS_PIN_PWM(p) digitalPinHasPWM(p)
#define IS_PIN_SERVO(p) ((p) >= 2 && (p) - 2 < MAX_SERVOS)
#define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // 70 71
#define IS_PIN_SERIAL(p) ((p) > 13 && (p) < 20)
#define PIN_TO_DIGITAL(p) (p)
#define PIN_TO_ANALOG(p) ((p) - 54)
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
Expand All @@ -233,12 +247,15 @@ writePort(port, value, bitmask): Write an 8 bit port.
#define TOTAL_ANALOG_PINS 0
#define TOTAL_PINS 21 // 21 digital + no analog
#define VERSION_BLINK_PIN 6
#define PIN_SERIAL1_RX 2
#define PIN_SERIAL1_TX 3
#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
#define IS_PIN_ANALOG(p) (0)
#define IS_PIN_PWM(p) digitalPinHasPWM(p)
#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
#define IS_PIN_I2C(p) (0)
#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
#define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3)
#define PIN_TO_DIGITAL(p) (p)
#define PIN_TO_ANALOG(p) (0)
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
Expand All @@ -250,12 +267,15 @@ writePort(port, value, bitmask): Write an 8 bit port.
#define TOTAL_ANALOG_PINS 12
#define TOTAL_PINS 25 // 11 digital + 12 analog
#define VERSION_BLINK_PIN 11
#define PIN_SERIAL1_RX 7
#define PIN_SERIAL1_TX 8
#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
#define IS_PIN_ANALOG(p) ((p) >= 11 && (p) <= 22)
#define IS_PIN_PWM(p) digitalPinHasPWM(p)
#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
#define IS_PIN_I2C(p) ((p) == 5 || (p) == 6)
#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
#define IS_PIN_SERIAL(p) ((p) == 7 || (p) == 8)
#define PIN_TO_DIGITAL(p) (p)
#define PIN_TO_ANALOG(p) (((p)<22)?21-(p):11)
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
Expand All @@ -267,11 +287,18 @@ writePort(port, value, bitmask): Write an 8 bit port.
#define TOTAL_ANALOG_PINS 14
#define TOTAL_PINS 38 // 24 digital + 10 analog-digital + 4 analog
#define VERSION_BLINK_PIN 13
#define PIN_SERIAL1_RX 0
#define PIN_SERIAL1_TX 1
#define PIN_SERIAL2_RX 9
#define PIN_SERIAL2_TX 10
#define PIN_SERIAL3_RX 7
#define PIN_SERIAL3_TX 8
#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 34)
#define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 34 && (p) <= 38))
#define IS_PIN_PWM(p) digitalPinHasPWM(p)
#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
#define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1))
#define PIN_TO_DIGITAL(p) (p)
#define PIN_TO_ANALOG(p) (((p)<=23)?(p)-14:(p)-24)
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
Expand All @@ -283,12 +310,15 @@ writePort(port, value, bitmask): Write an 8 bit port.
#define TOTAL_ANALOG_PINS 8
#define TOTAL_PINS 46 // 38 digital + 8 analog
#define VERSION_BLINK_PIN 6
#define PIN_SERIAL1_RX 2
#define PIN_SERIAL1_TX 3
#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
#define IS_PIN_ANALOG(p) ((p) >= 38 && (p) < TOTAL_PINS)
#define IS_PIN_PWM(p) digitalPinHasPWM(p)
#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
#define IS_PIN_I2C(p) ((p) == 0 || (p) == 1)
#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
#define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3)
#define PIN_TO_DIGITAL(p) (p)
#define PIN_TO_ANALOG(p) ((p) - 38)
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
Expand All @@ -300,28 +330,35 @@ writePort(port, value, bitmask): Write an 8 bit port.
#define TOTAL_ANALOG_PINS 12
#define TOTAL_PINS 30 // 14 digital + 12 analog + 4 SPI (D14-D17 on ISP header)
#define VERSION_BLINK_PIN 13
#define PIN_SERIAL1_RX 0
#define PIN_SERIAL1_TX 1
#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
#define IS_PIN_ANALOG(p) ((p) >= 18 && (p) < TOTAL_PINS)
#define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 13)
#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
#define IS_PIN_I2C(p) ((p) == 2 || (p) == 3)
#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
#define PIN_TO_DIGITAL(p) (p)
#define PIN_TO_ANALOG(p) (p) - 18
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
#define PIN_TO_SERVO(p) (p)


// Intel Galileo Board
#elif defined(ARDUINO_LINUX)
#define TOTAL_ANALOG_PINS 6
#define TOTAL_PINS 20 // 14 digital + 6 analog
#define VERSION_BLINK_PIN 13
#define PIN_SERIAL1_RX 0
#define PIN_SERIAL1_TX 1
#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19)
#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 19)
#define IS_PIN_PWM(p) digitalPinHasPWM(p)
#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS)
#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
#define PIN_TO_DIGITAL(p) (p)
#define PIN_TO_ANALOG(p) ((p) - 14)
#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
Expand Down Expand Up @@ -490,7 +527,7 @@ writePort(port, value, bitmask): Write an 8 bit port.
// Pic32 chipKIT MAX32
#elif defined(_BOARD_MEGA_) && defined(__PIC32) // NOTE: no _BOARD_MAX32_ to use
#define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 16
#define TOTAL_PINS NUM_DIGITAL_PINS // 87
#define TOTAL_PINS NUM_DIGITAL_PINS // 87
#define MAX_SERVOS NUM_DIGITAL_PINS
#define VERSION_BLINK_PIN PIN_LED1
#define IS_PIN_DIGITAL(p) ((p) >= 2)
Expand Down Expand Up @@ -534,6 +571,10 @@ writePort(port, value, bitmask): Write an 8 bit port.
#define IS_PIN_SPI(p) 0
#endif

#ifndef IS_PIN_SERIAL
#define IS_PIN_SERIAL(p) 0
#endif

/*==============================================================================
* readPort() - Read an 8 bit port
*============================================================================*/
Expand Down
4 changes: 3 additions & 1 deletion Firmata.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

// extended command set using sysex (0-127/0x00-0x7F)
/* 0x00-0x0F reserved for user-defined commands */
#define SERIAL_MESSAGE 0x60 // communicate with serial devices, including other boards
#define ENCODER_DATA 0x61 // reply with encoders current positions
#define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq
#define STRING_DATA 0x71 // a string message with 14-bits per char
Expand Down Expand Up @@ -79,8 +80,9 @@
#define ONEWIRE 0x07 // pin configured for 1-wire
#define STEPPER 0x08 // pin configured for stepper motor
#define ENCODER 0x09 // pin configured for rotary encoders
#define MODE_SERIAL 0x0A // pin configured for serial communication
#define IGNORE 0x7F // pin configured to be ignored by digitalWrite and capabilityResponse
#define TOTAL_PIN_MODES 11
#define TOTAL_PIN_MODES 12

extern "C" {
// callback function types
Expand Down
93 changes: 81 additions & 12 deletions examples/StandardFirmata/StandardFirmata.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@
#define SW_SERIAL2 0x0A
#define SW_SERIAL3 0x0B

#define SERIAL_MESSAGE 0x60
// map configuration query response resolution value to serial pin type
#define CONFIG_RX1 0x02
#define CONFIG_TX1 0x03
#define CONFIG_RX2 0x04
#define CONFIG_TX2 0x05
#define CONFIG_RX3 0x06
#define CONFIG_TX3 0x07

#define SERIAL_CONFIG 0x10
#define SERIAL_WRITE 0x20
#define SERIAL_READ 0x30
Expand All @@ -67,7 +74,7 @@
#define I2C_REGISTER_NOT_SPECIFIED -1

// the minimum interval for sampling analog input
#define MINIMUM_SAMPLING_INTERVAL 10
#define MINIMUM_SAMPLING_INTERVAL 10


/*==============================================================================
Expand Down Expand Up @@ -160,19 +167,19 @@ Stream* getPortFromId(byte portId)
// block use of Serial (typically pins 0 and 1) until ability to reclaim Serial is implemented
//return &Serial;
return NULL;
#if defined(UBRR1H) || defined(USBCON)
#if defined(PIN_SERIAL1_RX)
case HW_SERIAL1:
return &Serial1;
#endif
#if defined(UBRR2H) || defined(SERIAL_PORT_HARDWARE2)
#if defined(PIN_SERIAL2_RX)
case HW_SERIAL2:
return &Serial2;
#endif
#if defined(UBRR3H) || defined(SERIAL_PORT_HARDWARE3)
#if defined(PIN_SERIAL3_RX)
case HW_SERIAL3:
return &Serial3;
#endif
#if defined(ARDUINO_ARCH_AVR)
#if defined(SoftwareSerial_h)
case SW_SERIAL0:
if (swSerial0 != NULL) {
// instances of SoftwareSerial are already pointers so simply return the instance
Expand Down Expand Up @@ -218,7 +225,7 @@ void checkSerial()
if (serialPort == NULL) {
continue;
}
#if defined(ARDUINO_ARCH_AVR)
#if defined(SoftwareSerial_h)
// only the SoftwareSerial port that is "listening" can read data
if (portId > 7 && !((SoftwareSerial*)serialPort)->isListening()) {
continue;
Expand Down Expand Up @@ -249,6 +256,57 @@ void checkSerial()
}
}

/*
* Return the serial serial pin type (RX1, TX1, RX2, TX2, etc) for the specified pin
*/
byte getSerialPinType(byte pin) {
#if defined(PIN_SERIAL1_RX)
if (pin == PIN_SERIAL1_RX) return CONFIG_RX1;
if (pin == PIN_SERIAL1_TX) return CONFIG_TX1;
#endif
#if defined(PIN_SERIAL2_RX)
if (pin == PIN_SERIAL2_RX) return CONFIG_RX2;
if (pin == PIN_SERIAL2_TX) return CONFIG_TX2;
#endif
#if defined(PIN_SERIAL3_RX)
if (pin == PIN_SERIAL3_RX) return CONFIG_RX3;
if (pin == PIN_SERIAL3_TX) return CONFIG_TX3;
#endif
return 0;
}

byte configHWSerialPins(byte portId) {
byte rxPin, txPin;
switch (portId) {
#if defined(PIN_SERIAL1_RX)
case HW_SERIAL1:
rxPin = PIN_SERIAL1_RX;
txPin = PIN_SERIAL1_TX;
break;
#endif
#if defined(PIN_SERIAL2_RX)
case HW_SERIAL2:
rxPin = PIN_SERIAL2_RX;
txPin = PIN_SERIAL2_TX;
break;
#endif
#if defined(PIN_SERIAL3_RX)
case HW_SERIAL3:
rxPin = PIN_SERIAL3_RX;
txPin = PIN_SERIAL3_TX;
break;
#endif
default:
return 0;
}
setPinModeCallback(rxPin, MODE_SERIAL);
setPinModeCallback(txPin, MODE_SERIAL);

// Fixes an issue where some serial devices would not work properly with Arduino Due
// because all Arduino pins are set to OUTPUT by default in StandardFirmata.
pinMode(rxPin, INPUT);
}

void attachServo(byte pin, int minPulse, int maxPulse)
{
if (servoCount < MAX_SERVOS) {
Expand Down Expand Up @@ -439,6 +497,10 @@ void setPinModeCallback(byte pin, int mode)
pinConfig[pin] = I2C;
}
break;
case MODE_SERIAL:
// used for both HW and SW serial
pinConfig[pin] = MODE_SERIAL;
break;
default:
Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM
}
Expand Down Expand Up @@ -700,6 +762,10 @@ void sysexCallback(byte command, byte argc, byte *argv)
Firmata.write(I2C);
Firmata.write(1); // TODO: could assign a number to map to SCL or SDA
}
if (IS_PIN_SERIAL(pin)) {
Firmata.write(MODE_SERIAL);
Firmata.write(getSerialPinType(pin));
}
Firmata.write(127);
}
Firmata.write(END_SYSEX);
Expand Down Expand Up @@ -749,10 +815,11 @@ void sysexCallback(byte command, byte argc, byte *argv)
if (portId < 8) {
serialPort = getPortFromId(portId);
if (serialPort != NULL) {
configHWSerialPins(portId);
((HardwareSerial*)serialPort)->begin(baud);
}
} else {
#if defined(ARDUINO_ARCH_AVR)
#if defined(SoftwareSerial_h)
switch (portId) {
case SW_SERIAL0:
if (swSerial0 == NULL) {
Expand All @@ -777,6 +844,8 @@ void sysexCallback(byte command, byte argc, byte *argv)
}
serialPort = getPortFromId(portId);
if (serialPort != NULL) {
setPinModeCallback(rxPin, MODE_SERIAL);
setPinModeCallback(txPin, MODE_SERIAL);
((SoftwareSerial*)serialPort)->begin(baud);
}
#endif
Expand Down Expand Up @@ -830,7 +899,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
if (portId < 8) {
((HardwareSerial*)serialPort)->end();
} else {
#if defined(ARDUINO_ARCH_AVR)
#if defined(SoftwareSerial_h)
((SoftwareSerial*)serialPort)->end();
if (serialPort != NULL) {
free(serialPort);
Expand All @@ -846,7 +915,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
getPortFromId(portId)->flush();
}
break;
#if defined(ARDUINO_ARCH_AVR)
#if defined(SoftwareSerial_h)
case SERIAL_LISTEN:
// can only call listen() on software serial ports
if (portId > 7) {
Expand Down Expand Up @@ -902,7 +971,7 @@ void systemResetCallback()
disableI2CPins();
}

#if defined(ARDUINO_ARCH_AVR)
#if defined(SoftwareSerial_h)
// free memory allocated for SoftwareSerial ports
for (byte i = SW_SERIAL0; i < SW_SERIAL3 + 1; i++) {
serialPort = getPortFromId(i);
Expand Down Expand Up @@ -967,7 +1036,7 @@ void setup()
// Call begin(baud) on the alternate serial port and pass it to Firmata to begin like this:
// Serial1.begin(57600);
// Firmata.begin(Serial1);
// then comment out or remove lines 701 - 704 below
// However do not do this if you are using SERIAL_MESSAGE

Firmata.begin(57600);
while (!Serial) {
Expand Down

0 comments on commit 3120222

Please sign in to comment.