35
35
#include " SoftwareSerial.h"
36
36
#include " Arduino.h"
37
37
38
- SoftwareSerial *ap3_serial_handle = 0 ;
38
+ SoftwareSerial *ap3_active_softwareserial_handle = 0 ;
39
39
40
40
// Uncomment to enable debug pulses and Serial.prints
41
- // #define DEBUG
41
+ #define DEBUG
42
42
43
43
#ifdef DEBUG
44
- #define SS_DEBUG_PIN 8
44
+ #define SS_DEBUG_PIN 9
45
45
ap3_gpio_pad_t debugPad = ap3_gpio_pin2pad(SS_DEBUG_PIN);
46
46
#endif
47
47
48
- inline void _software_serial_isr (void * arg)
48
+ inline void _software_serial_isr (void * arg)
49
49
{
50
- SoftwareSerial* handle = (SoftwareSerial*)arg;
50
+ SoftwareSerial * handle = (SoftwareSerial *)arg;
51
51
handle->rxBit ();
52
52
}
53
53
@@ -74,6 +74,34 @@ void SoftwareSerial::begin(uint32_t baudRate)
74
74
begin (baudRate, SERIAL_8N1);
75
75
}
76
76
77
+ void SoftwareSerial::listen ()
78
+ {
79
+ if (ap3_active_softwareserial_handle != NULL )
80
+ ap3_active_softwareserial_handle->stopListening (); // temp
81
+
82
+ // change handle to point to new PCI
83
+ ap3_active_softwareserial_handle = this ;
84
+
85
+ lastBitTime = 0 ; // Reset for next byte
86
+
87
+ attachInterruptArg (digitalPinToInterrupt (_rxPin), _software_serial_isr, (void *)this , CHANGE);
88
+ }
89
+
90
+ void SoftwareSerial::stopListening ()
91
+ {
92
+ // Disable the timer interrupt in the NVIC.
93
+ NVIC_DisableIRQ (STIMER_CMPR7_IRQn);
94
+
95
+ detachInterrupt (_rxPin);
96
+
97
+ ap3_active_softwareserial_handle == NULL ;
98
+ }
99
+
100
+ bool SoftwareSerial::isListening ()
101
+ {
102
+ return (this == ap3_active_softwareserial_handle);
103
+ }
104
+
77
105
void SoftwareSerial::begin (uint32_t baudRate, HardwareSerial_Config_e SSconfig)
78
106
{
79
107
pinMode (_txPin, OUTPUT);
@@ -83,6 +111,10 @@ void SoftwareSerial::begin(uint32_t baudRate, HardwareSerial_Config_e SSconfig)
83
111
if (_invertLogic == false )
84
112
pinMode (_rxPin, INPUT_PULLUP); // Enable external pullup if using normal logic
85
113
114
+ #ifdef DEBUG
115
+ pinMode (SS_DEBUG_PIN, OUTPUT);
116
+ #endif
117
+
86
118
// Enable C/T H=7
87
119
am_hal_stimer_int_enable (AM_HAL_STIMER_INT_COMPAREH);
88
120
@@ -107,14 +139,12 @@ void SoftwareSerial::begin(uint32_t baudRate, HardwareSerial_Config_e SSconfig)
107
139
// Clear compare interrupt
108
140
am_hal_stimer_int_clear (AM_HAL_STIMER_INT_COMPAREH);
109
141
110
- // Attach argument interrupt with 'this' as argument
111
- attachInterruptArg (digitalPinToInterrupt (_rxPin), _software_serial_isr, (void *)this , CHANGE);
112
-
113
- // temporary:
114
- ap3_serial_handle = this ; // right now this is needed for the cmpr7 isr.
142
+ // Begin PCI
143
+ listen ();
115
144
}
116
145
117
- void SoftwareSerial::end (void ){
146
+ void SoftwareSerial::end (void )
147
+ {
118
148
detachInterrupt (_rxPin);
119
149
// todo: anything else?
120
150
}
@@ -126,7 +156,8 @@ int SoftwareSerial::available()
126
156
127
157
int SoftwareSerial::read ()
128
158
{
129
- if (available () == 0 ) return (-1 );
159
+ if (available () == 0 )
160
+ return (-1 );
130
161
131
162
rxBufferTail++;
132
163
rxBufferTail %= AP3_SS_BUFFER_SIZE;
@@ -135,7 +166,8 @@ int SoftwareSerial::read()
135
166
136
167
int SoftwareSerial::peek ()
137
168
{
138
- if (available () == 0 ) return (-1 );
169
+ if (available () == 0 )
170
+ return (-1 );
139
171
140
172
uint8_t tempTail = rxBufferTail + 1 ;
141
173
tempTail %= AP3_SS_BUFFER_SIZE;
@@ -152,7 +184,6 @@ bool SoftwareSerial::overflow()
152
184
return (true );
153
185
}
154
186
return (false );
155
-
156
187
}
157
188
158
189
ap3_err_t SoftwareSerial::softwareserialSetConfig (HardwareSerial_Config_e SSconfig)
@@ -445,7 +476,7 @@ extern "C" void am_stimer_cmpr7_isr(void)
445
476
{
446
477
am_hal_stimer_int_clear (AM_HAL_STIMER_INT_COMPAREH);
447
478
448
- ap3_serial_handle ->endOfByte ();
479
+ ap3_active_softwareserial_handle ->endOfByte ();
449
480
}
450
481
451
482
#ifdef DEBUG
0 commit comments