25
25
26
26
#include < Servo.h>
27
27
#include < Wire.h>
28
+ #include < ArduinoUnit.h>
29
+ #include < Firmata.h>
30
+
28
31
// SoftwareSerial is only supported for AVR-based boards
29
32
#if defined(ARDUINO_ARCH_AVR)
30
33
#include < SoftwareSerial.h>
31
34
#endif
32
- #include < ArduinoUnit.h>
33
- #include < Firmata.h>
34
-
35
- #define HW_SERIAL0 0x00
36
- #define HW_SERIAL1 0x01
37
- #define HW_SERIAL2 0x02
38
- #define HW_SERIAL3 0x03
39
-
40
- #define SW_SERIAL0 0x08
41
- #define SW_SERIAL1 0x09
42
- #define SW_SERIAL2 0x0A
43
- #define SW_SERIAL3 0x0B
44
-
45
- // map configuration query response resolution value to serial pin type
46
- #define CONFIG_RX1 0x02
47
- #define CONFIG_TX1 0x03
48
- #define CONFIG_RX2 0x04
49
- #define CONFIG_TX2 0x05
50
- #define CONFIG_RX3 0x06
51
- #define CONFIG_TX3 0x07
52
-
53
- #define SERIAL_CONFIG 0x10
54
- #define SERIAL_WRITE 0x20
55
- #define SERIAL_READ 0x30
56
- #define SERIAL_REPLY 0x40
57
- #define SERIAL_CLOSE 0x50
58
- #define SERIAL_FLUSH 0x60
59
- #define SERIAL_LISTEN 0x70
60
- #define SERIAL_READ_CONTINUOUSLY 0x00
61
- #define SERIAL_STOP_READING 0x01
62
- #define SERIAL_MODE_MASK 0xF0
63
- #define SERIAL_PORT_ID_MASK 0x0F
64
- #define MAX_SERIAL_PORTS 8
65
-
35
+ #include " utility/serialUtils.h"
66
36
67
37
#define I2C_WRITE B00000000
68
38
#define I2C_READ B00001000
@@ -134,7 +104,6 @@ boolean isResetting = false;
134
104
135
105
int memCheckCounter = 0 ;
136
106
char buffer[20 ];
137
- char debugBuffer[50 ];
138
107
139
108
/* utility functions */
140
109
void wireWrite (byte data)
@@ -256,57 +225,6 @@ void checkSerial()
256
225
}
257
226
}
258
227
259
- /*
260
- * Return the serial serial pin type (RX1, TX1, RX2, TX2, etc) for the specified pin
261
- */
262
- byte getSerialPinType (byte pin) {
263
- #if defined(PIN_SERIAL1_RX)
264
- if (pin == PIN_SERIAL1_RX) return CONFIG_RX1;
265
- if (pin == PIN_SERIAL1_TX) return CONFIG_TX1;
266
- #endif
267
- #if defined(PIN_SERIAL2_RX)
268
- if (pin == PIN_SERIAL2_RX) return CONFIG_RX2;
269
- if (pin == PIN_SERIAL2_TX) return CONFIG_TX2;
270
- #endif
271
- #if defined(PIN_SERIAL3_RX)
272
- if (pin == PIN_SERIAL3_RX) return CONFIG_RX3;
273
- if (pin == PIN_SERIAL3_TX) return CONFIG_TX3;
274
- #endif
275
- return 0 ;
276
- }
277
-
278
- byte configHWSerialPins (byte portId) {
279
- byte rxPin, txPin;
280
- switch (portId) {
281
- #if defined(PIN_SERIAL1_RX)
282
- case HW_SERIAL1:
283
- rxPin = PIN_SERIAL1_RX;
284
- txPin = PIN_SERIAL1_TX;
285
- break ;
286
- #endif
287
- #if defined(PIN_SERIAL2_RX)
288
- case HW_SERIAL2:
289
- rxPin = PIN_SERIAL2_RX;
290
- txPin = PIN_SERIAL2_TX;
291
- break ;
292
- #endif
293
- #if defined(PIN_SERIAL3_RX)
294
- case HW_SERIAL3:
295
- rxPin = PIN_SERIAL3_RX;
296
- txPin = PIN_SERIAL3_TX;
297
- break ;
298
- #endif
299
- default :
300
- return 0 ;
301
- }
302
- setPinModeCallback (rxPin, MODE_SERIAL);
303
- setPinModeCallback (txPin, MODE_SERIAL);
304
-
305
- // Fixes an issue where some serial devices would not work properly with Arduino Due
306
- // because all Arduino pins are set to OUTPUT by default in StandardFirmata.
307
- pinMode (rxPin, INPUT);
308
- }
309
-
310
228
void attachServo (byte pin, int minPulse, int maxPulse)
311
229
{
312
230
if (servoCount < MAX_SERVOS) {
@@ -804,6 +722,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
804
722
{
805
723
long baud = (long )argv[1 ] | ((long )argv[2 ] << 7 ) | ((long )argv[3 ] << 14 );
806
724
byte txPin, rxPin;
725
+ serial_pins pins;
807
726
808
727
serialBytesToRead[portId] = (int )argv[4 ] | ((int )argv[5 ] << 7 );
809
728
@@ -815,7 +734,14 @@ void sysexCallback(byte command, byte argc, byte *argv)
815
734
if (portId < 8 ) {
816
735
serialPort = getPortFromId (portId);
817
736
if (serialPort != NULL ) {
818
- configHWSerialPins (portId);
737
+ pins = getSerialPinNumbers (portId);
738
+ if (pins.rx != 0 && pins.tx != 0 ) {
739
+ setPinModeCallback (pins.rx , MODE_SERIAL);
740
+ setPinModeCallback (pins.tx , MODE_SERIAL);
741
+ // Fixes an issue where some serial devices would not work properly with Arduino Due
742
+ // because all Arduino pins are set to OUTPUT by default in StandardFirmata.
743
+ pinMode (pins.rx , INPUT);
744
+ }
819
745
((HardwareSerial*)serialPort)->begin (baud);
820
746
}
821
747
} else {
@@ -863,7 +789,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
863
789
data = argv[i] + (argv[i + 1 ] << 7 );
864
790
serialPort->write (data);
865
791
}
866
- break ;
792
+ break ; // SERIAL_WRITE
867
793
}
868
794
case SERIAL_READ:
869
795
if (argv[1 ] == SERIAL_READ_CONTINUOUSLY) {
@@ -892,7 +818,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
892
818
serialIndex--;
893
819
}
894
820
}
895
- break ;
821
+ break ; // SERIAL_READ
896
822
case SERIAL_CLOSE:
897
823
serialPort = getPortFromId (portId);
898
824
if (serialPort != NULL ) {
@@ -908,13 +834,13 @@ void sysexCallback(byte command, byte argc, byte *argv)
908
834
#endif
909
835
}
910
836
}
911
- break ;
837
+ break ; // SERIAL_CLOSE
912
838
case SERIAL_FLUSH:
913
839
serialPort = getPortFromId (portId);
914
840
if (serialPort != NULL ) {
915
841
getPortFromId (portId)->flush ();
916
842
}
917
- break ;
843
+ break ; // SERIAL_FLUSH
918
844
#if defined(SoftwareSerial_h)
919
845
case SERIAL_LISTEN:
920
846
// can only call listen() on software serial ports
@@ -924,7 +850,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
924
850
((SoftwareSerial*)serialPort)->listen ();
925
851
}
926
852
}
927
- break ;
853
+ break ; // SERIAL_LISTEN
928
854
#endif
929
855
}
930
856
break ;
0 commit comments