38
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
44
#define SS_DEBUG_PIN 9
@@ -84,6 +84,12 @@ void SoftwareSerial::listen()
84
84
85
85
lastBitTime = 0 ; // Reset for next byte
86
86
87
+ // Clear pin change interrupt
88
+ am_hal_gpio_interrupt_clear (AM_HAL_GPIO_BIT (_rxPad));
89
+
90
+ // Clear compare interrupt
91
+ am_hal_stimer_int_clear (AM_HAL_STIMER_INT_COMPAREH);
92
+
87
93
// Attach this instance RX pin to PCI
88
94
attachInterruptArg (digitalPinToInterrupt (_rxPin), _software_serial_isr, (void *)this , CHANGE);
89
95
}
@@ -138,12 +144,6 @@ void SoftwareSerial::begin(uint32_t baudRate, HardwareSerial_Config_e SSconfig)
138
144
139
145
txSysTicksPerStopBit = txSysTicksPerBit * _stopBits;
140
146
141
- // Clear pin change interrupt
142
- am_hal_gpio_interrupt_clear (AM_HAL_GPIO_BIT (_rxPad));
143
-
144
- // Clear compare interrupt
145
- am_hal_stimer_int_clear (AM_HAL_STIMER_INT_COMPAREH);
146
-
147
147
// Begin PCI
148
148
listen ();
149
149
}
@@ -195,6 +195,14 @@ bool SoftwareSerial::overflow()
195
195
// Required for print
196
196
size_t SoftwareSerial::write (uint8_t toSend)
197
197
{
198
+ // As soon as user wants to send something, turn off RX interrupts
199
+ if (txInUse == false )
200
+ {
201
+ detachInterrupt (_rxPin);
202
+
203
+ rxInUse = false ;
204
+ }
205
+
198
206
// See if we are going to overflow buffer
199
207
uint8_t nextSpot = (txBufferHead + 1 ) % AP3_SS_BUFFER_SIZE;
200
208
if (nextSpot != txBufferTail)
@@ -222,6 +230,7 @@ size_t SoftwareSerial::write(uint8_t toSend)
222
230
223
231
beginTX ();
224
232
}
233
+ return (1 );
225
234
}
226
235
227
236
size_t SoftwareSerial::write (const uint8_t *buffer, size_t size)
@@ -245,8 +254,6 @@ void SoftwareSerial::beginTX()
245
254
{
246
255
bitCounter = 0 ;
247
256
248
- am_hal_gpio_output_set (debugPad);
249
-
250
257
// Initiate start bit
251
258
if (_invertLogic == false )
252
259
{
@@ -257,14 +264,15 @@ void SoftwareSerial::beginTX()
257
264
am_hal_gpio_output_set (_txPad);
258
265
}
259
266
267
+ // Clear compare interrupt
268
+ am_hal_stimer_int_clear (AM_HAL_STIMER_INT_COMPAREH);
269
+
260
270
// Setup ISR to trigger when we are in middle of start bit
261
271
// am_hal_stimer_compare_delta_set(7, txSsysTicksPerBit);
262
272
AM_REGVAL (AM_REG_STIMER_COMPARE (0 , 7 )) = txSysTicksPerBit; // Direct reg write to decrease execution time
263
273
264
274
// Enable the timer interrupt in the NVIC.
265
275
NVIC_EnableIRQ (STIMER_CMPR7_IRQn);
266
-
267
- am_hal_gpio_output_clear (debugPad);
268
276
}
269
277
270
278
// Assumes the global variables have been set: _parity, _dataBits, outgoingByte
@@ -468,7 +476,7 @@ void SoftwareSerial::rxBit(void)
468
476
if (partialBits > rxSysTicksPartialBit)
469
477
{
470
478
#ifdef DEBUG
471
- Serial.println (" Partial!" );
479
+ // Serial.println("Partial!");
472
480
#endif
473
481
numberOfBits++;
474
482
}
@@ -483,7 +491,7 @@ void SoftwareSerial::rxBit(void)
483
491
if (numberOfBits + bitCounter > _dataBits + _parityBits)
484
492
{
485
493
#ifdef DEBUG
486
- Serial.println (" Exclude" );
494
+ // Serial.println("Exclude");
487
495
#endif
488
496
numberOfBits--; // Exclude parity bit from byte shift
489
497
}
@@ -509,8 +517,8 @@ void SoftwareSerial::rxEndOfByte()
509
517
{
510
518
// Finish out bytes that are less than 8 bits
511
519
#ifdef DEBUG
512
- Serial.printf (" bitCounter: %d\n " , bitCounter);
513
- Serial.printf (" incoming: 0x%02X\n " , incomingByte);
520
+ // Serial.printf("bitCounter: %d\n", bitCounter);
521
+ // Serial.printf("incoming: 0x%02X\n", incomingByte);
514
522
#endif
515
523
bitCounter--; // Remove start bit from count
516
524
@@ -523,7 +531,7 @@ void SoftwareSerial::rxEndOfByte()
523
531
}
524
532
525
533
#ifdef DEBUG
526
- Serial.printf (" bitCounter: %d\n " , bitCounter);
534
+ // Serial.printf("bitCounter: %d\n", bitCounter);
527
535
#endif
528
536
529
537
while (bitCounter < 8 )
@@ -533,7 +541,7 @@ void SoftwareSerial::rxEndOfByte()
533
541
if (bitCounter < _dataBits)
534
542
{
535
543
#ifdef DEBUG
536
- Serial.println (" Add bit" );
544
+ // Serial.println("Add bit");
537
545
#endif
538
546
incomingByte |= 0x80 ;
539
547
}
@@ -630,6 +638,9 @@ void SoftwareSerial::txHandler()
630
638
631
639
// All done!
632
640
txInUse = false ;
641
+
642
+ // Reattach PCI so we can hear rx bits coming in
643
+ listen ();
633
644
}
634
645
else
635
646
{
@@ -651,6 +662,9 @@ void SoftwareSerial::txHandler()
651
662
652
663
// All done!
653
664
txInUse = false ;
665
+
666
+ // Reattach PCI so we can hear rx bits coming in
667
+ listen ();
654
668
}
655
669
else
656
670
{
0 commit comments