3838SoftwareSerial *ap3_active_softwareserial_handle = 0 ;
3939
4040// Uncomment to enable debug pulses and Serial.prints
41- #define DEBUG
41+ // #define DEBUG
4242
4343#ifdef DEBUG
4444#define SS_DEBUG_PIN 9
@@ -84,6 +84,12 @@ void SoftwareSerial::listen()
8484
8585 lastBitTime = 0 ; // Reset for next byte
8686
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+
8793 // Attach this instance RX pin to PCI
8894 attachInterruptArg (digitalPinToInterrupt (_rxPin), _software_serial_isr, (void *)this , CHANGE);
8995}
@@ -138,12 +144,6 @@ void SoftwareSerial::begin(uint32_t baudRate, HardwareSerial_Config_e SSconfig)
138144
139145 txSysTicksPerStopBit = txSysTicksPerBit * _stopBits;
140146
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-
147147 // Begin PCI
148148 listen ();
149149}
@@ -195,6 +195,14 @@ bool SoftwareSerial::overflow()
195195// Required for print
196196size_t SoftwareSerial::write (uint8_t toSend)
197197{
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+
198206 // See if we are going to overflow buffer
199207 uint8_t nextSpot = (txBufferHead + 1 ) % AP3_SS_BUFFER_SIZE;
200208 if (nextSpot != txBufferTail)
@@ -222,6 +230,7 @@ size_t SoftwareSerial::write(uint8_t toSend)
222230
223231 beginTX ();
224232 }
233+ return (1 );
225234}
226235
227236size_t SoftwareSerial::write (const uint8_t *buffer, size_t size)
@@ -245,8 +254,6 @@ void SoftwareSerial::beginTX()
245254{
246255 bitCounter = 0 ;
247256
248- am_hal_gpio_output_set (debugPad);
249-
250257 // Initiate start bit
251258 if (_invertLogic == false )
252259 {
@@ -257,14 +264,15 @@ void SoftwareSerial::beginTX()
257264 am_hal_gpio_output_set (_txPad);
258265 }
259266
267+ // Clear compare interrupt
268+ am_hal_stimer_int_clear (AM_HAL_STIMER_INT_COMPAREH);
269+
260270 // Setup ISR to trigger when we are in middle of start bit
261271 // am_hal_stimer_compare_delta_set(7, txSsysTicksPerBit);
262272 AM_REGVAL (AM_REG_STIMER_COMPARE (0 , 7 )) = txSysTicksPerBit; // Direct reg write to decrease execution time
263273
264274 // Enable the timer interrupt in the NVIC.
265275 NVIC_EnableIRQ (STIMER_CMPR7_IRQn);
266-
267- am_hal_gpio_output_clear (debugPad);
268276}
269277
270278// Assumes the global variables have been set: _parity, _dataBits, outgoingByte
@@ -468,7 +476,7 @@ void SoftwareSerial::rxBit(void)
468476 if (partialBits > rxSysTicksPartialBit)
469477 {
470478#ifdef DEBUG
471- Serial.println (" Partial!" );
479+ // Serial.println("Partial!");
472480#endif
473481 numberOfBits++;
474482 }
@@ -483,7 +491,7 @@ void SoftwareSerial::rxBit(void)
483491 if (numberOfBits + bitCounter > _dataBits + _parityBits)
484492 {
485493#ifdef DEBUG
486- Serial.println (" Exclude" );
494+ // Serial.println("Exclude");
487495#endif
488496 numberOfBits--; // Exclude parity bit from byte shift
489497 }
@@ -509,8 +517,8 @@ void SoftwareSerial::rxEndOfByte()
509517{
510518 // Finish out bytes that are less than 8 bits
511519#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);
514522#endif
515523 bitCounter--; // Remove start bit from count
516524
@@ -523,7 +531,7 @@ void SoftwareSerial::rxEndOfByte()
523531 }
524532
525533#ifdef DEBUG
526- Serial.printf (" bitCounter: %d\n " , bitCounter);
534+ // Serial.printf("bitCounter: %d\n", bitCounter);
527535#endif
528536
529537 while (bitCounter < 8 )
@@ -533,7 +541,7 @@ void SoftwareSerial::rxEndOfByte()
533541 if (bitCounter < _dataBits)
534542 {
535543#ifdef DEBUG
536- Serial.println (" Add bit" );
544+ // Serial.println("Add bit");
537545#endif
538546 incomingByte |= 0x80 ;
539547 }
@@ -630,6 +638,9 @@ void SoftwareSerial::txHandler()
630638
631639 // All done!
632640 txInUse = false ;
641+
642+ // Reattach PCI so we can hear rx bits coming in
643+ listen ();
633644 }
634645 else
635646 {
@@ -651,6 +662,9 @@ void SoftwareSerial::txHandler()
651662
652663 // All done!
653664 txInUse = false ;
665+
666+ // Reattach PCI so we can hear rx bits coming in
667+ listen ();
654668 }
655669 else
656670 {
0 commit comments