36
36
#include " Arduino.h"
37
37
38
38
// Global Table of SoftwareSerial Pointers
39
- SoftwareSerial* gpSoftwareSerialObjs[AP3_GPIO_MAX_PADS];
39
+ SoftwareSerial * gpSoftwareSerialObjs[AP3_GPIO_MAX_PADS];
40
40
uint8_t gSoftwareSerialNumObjs = 0 ;
41
41
42
-
43
42
// Software Serial ISR (To attach to pin change interrupts)
44
- void _software_serial_isr ( void ){
43
+ void _software_serial_isr (void )
44
+ {
45
45
uint64_t gpio_int_mask = 0x00 ;
46
46
am_hal_gpio_interrupt_status_get (true , &gpio_int_mask);
47
- SoftwareSerial* obj = NULL ;
48
- for (uint8_t indi = 0 ; indi < gSoftwareSerialNumObjs ; indi++){
47
+ SoftwareSerial *obj = NULL ;
48
+ for (uint8_t indi = 0 ; indi < gSoftwareSerialNumObjs ; indi++)
49
+ {
49
50
obj = gpSoftwareSerialObjs[indi];
50
- if (obj == NULL ){
51
+ if (obj == NULL )
52
+ {
51
53
break ; // there should not be any null pointers in the global object table
52
54
}
53
- if (obj->_rxPadBitMask & gpio_int_mask){
55
+ if (obj->_rxPadBitMask & gpio_int_mask)
56
+ {
54
57
obj->rxBit ();
55
58
}
56
59
}
57
60
}
58
61
59
-
60
62
// Constructor
61
- SoftwareSerial::SoftwareSerial (uint8_t rxPin, uint8_t txPin)
63
+ SoftwareSerial::SoftwareSerial (uint8_t rxPin, uint8_t txPin, bool invertLogic )
62
64
{
63
- if ( gSoftwareSerialNumObjs >= AP3_GPIO_MAX_PADS ){
65
+ if (gSoftwareSerialNumObjs >= AP3_GPIO_MAX_PADS)
66
+ {
64
67
return ; // Error -- no instances left to create
65
68
}
66
69
@@ -70,7 +73,9 @@ SoftwareSerial::SoftwareSerial(uint8_t rxPin, uint8_t txPin)
70
73
_txPad = ap3_gpio_pin2pad (_txPin);
71
74
_rxPad = ap3_gpio_pin2pad (_rxPin);
72
75
73
- _rxPadBitMask = ( 0x01 << _rxPad );
76
+ _invertLogic = invertLogic;
77
+
78
+ _rxPadBitMask = (0x01 << _rxPad);
74
79
75
80
// Add to the global array
76
81
_indexNumber = gSoftwareSerialNumObjs ;
@@ -81,19 +86,22 @@ SoftwareSerial::SoftwareSerial(uint8_t rxPin, uint8_t txPin)
81
86
// Destructor
82
87
SoftwareSerial::~SoftwareSerial ()
83
88
{
84
- if ( gSoftwareSerialNumObjs < 1 ){
89
+ if (gSoftwareSerialNumObjs < 1 )
90
+ {
85
91
return ; // error -- no instances left to destroy
86
92
}
87
93
88
94
// Remove from global pointer list by filtering others down:
89
95
uint8_t index = _indexNumber;
90
- do {
96
+ do
97
+ {
91
98
gpSoftwareSerialObjs[index] = NULL ;
92
- if ( index < (gSoftwareSerialNumObjs -1 ) ){
93
- gpSoftwareSerialObjs[index] = gpSoftwareSerialObjs[index+1 ];
99
+ if (index < (gSoftwareSerialNumObjs - 1 ))
100
+ {
101
+ gpSoftwareSerialObjs[index] = gpSoftwareSerialObjs[index + 1 ];
94
102
}
95
103
index++;
96
- }while ( index < gSoftwareSerialNumObjs );
104
+ } while ( index < gSoftwareSerialNumObjs );
97
105
gSoftwareSerialNumObjs --;
98
106
}
99
107
@@ -348,3 +356,88 @@ void SoftwareSerial::rxBit(void)
348
356
am_hal_gpio_output_clear (triggerPad);
349
357
#endif
350
358
}
359
+
360
+ void SoftwareSerial::endOfByte ()
361
+ {
362
+ // Finish out bytes that are less than 8 bits
363
+ #ifdef DEBUG
364
+ Serial.printf (" bitCounter: %d\n " , bitCounter);
365
+ Serial.printf (" incoming: 0x%02X\n " , incomingByte);
366
+ #endif
367
+ bitCounter--; // Remove start bit from count
368
+
369
+ // Edge case where we need to do an additional byte shift because we had data bits followed by a parity bit of same value
370
+ if (_parity)
371
+ {
372
+ bitCounter = bitCounter - _parityBits; // Remove parity bit from count
373
+ if (bitType == true )
374
+ bitCounter++;
375
+ }
376
+
377
+ #ifdef DEBUG
378
+ Serial.printf (" bitCounter: %d\n " , bitCounter);
379
+ #endif
380
+
381
+ while (bitCounter < 8 )
382
+ {
383
+ incomingByte >>= 1 ;
384
+ if (bitType == true )
385
+ if (bitCounter < _dataBits)
386
+ {
387
+ #ifdef DEBUG
388
+ Serial.println (" Add bit" );
389
+ #endif
390
+ incomingByte |= 0x80 ;
391
+ }
392
+ bitCounter++;
393
+ }
394
+
395
+ // TODO - Check parity bit if parity is enabled
396
+
397
+ if (_invertLogic)
398
+ incomingByte = ~incomingByte;
399
+
400
+ // See if we are going to overflow buffer
401
+ uint8_t nextSpot = (rxBufferHead + 1 ) % AP3_SS_BUFFER_SIZE;
402
+ if (nextSpot != rxBufferTail)
403
+ {
404
+ // Add this byte to the buffer
405
+ rxBuffer[nextSpot] = incomingByte;
406
+ rxBufferHead = nextSpot;
407
+ }
408
+ else
409
+ {
410
+ #ifdef DEBUG
411
+ am_hal_gpio_output_set (triggerPad);
412
+ am_hal_gpio_output_clear (triggerPad);
413
+ #endif
414
+ _rxBufferOverflow = true ;
415
+ }
416
+
417
+ lastBitTime = 0 ; // Reset for next byte
418
+
419
+ rxInUse = false ;
420
+
421
+ // Disable the timer interrupt in the NVIC.
422
+ NVIC_DisableIRQ (STIMER_CMPR7_IRQn);
423
+ }
424
+
425
+ // Called at the completion of bytes
426
+ extern " C" void am_stimer_cmpr7_isr (void )
427
+ {
428
+ #ifdef DEBUG
429
+ am_hal_gpio_output_set (triggerPad);
430
+ #endif
431
+
432
+ uint32_t ui32Status = am_hal_stimer_int_status_get (false );
433
+ if (ui32Status & AM_HAL_STIMER_INT_COMPAREH)
434
+ {
435
+ am_hal_stimer_int_clear (AM_HAL_STIMER_INT_COMPAREH);
436
+
437
+ // this->endOfByte();
438
+ }
439
+
440
+ #ifdef DEBUG
441
+ am_hal_gpio_output_clear (triggerPad);
442
+ #endif
443
+ }
0 commit comments