|
35 | 35 | #include "SoftwareSerial.h" |
36 | 36 | #include "Arduino.h" |
37 | 37 |
|
| 38 | +// Global Table of SoftwareSerial Pointers |
| 39 | +SoftwareSerial* gpSoftwareSerialObjs[AP3_GPIO_MAX_PADS]; |
| 40 | +uint8_t gSoftwareSerialNumObjs = 0; |
| 41 | + |
| 42 | + |
| 43 | +// Software Serial ISR (To attach to pin change interrupts) |
| 44 | +void _software_serial_isr( void ){ |
| 45 | + uint64_t gpio_int_mask = 0x00; |
| 46 | + am_hal_gpio_interrupt_status_get(true, &gpio_int_mask); |
| 47 | + SoftwareSerial* obj = NULL; |
| 48 | + for(uint8_t indi = 0; indi < gSoftwareSerialNumObjs; indi++){ |
| 49 | + obj = gpSoftwareSerialObjs[indi]; |
| 50 | + if(obj == NULL){ |
| 51 | + break; // there should not be any null pointers in the global object table |
| 52 | + } |
| 53 | + if(obj->_rxPadBitMask & gpio_int_mask){ |
| 54 | + obj->rxBit(); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | + |
38 | 60 | //Constructor |
39 | 61 | SoftwareSerial::SoftwareSerial(uint8_t rxPin, uint8_t txPin) |
40 | 62 | { |
| 63 | + if( gSoftwareSerialNumObjs >= AP3_GPIO_MAX_PADS ){ |
| 64 | + return; // Error -- no instances left to create |
| 65 | + } |
| 66 | + |
41 | 67 | _rxPin = rxPin; |
42 | 68 | _txPin = txPin; |
43 | 69 |
|
44 | 70 | _txPad = ap3_gpio_pin2pad(_txPin); |
45 | 71 | _rxPad = ap3_gpio_pin2pad(_rxPin); |
| 72 | + |
| 73 | + _rxPadBitMask = ( 0x01 << _rxPad ); |
| 74 | + |
| 75 | + // Add to the global array |
| 76 | + _indexNumber = gSoftwareSerialNumObjs; |
| 77 | + gpSoftwareSerialObjs[_indexNumber] = this; |
| 78 | + gSoftwareSerialNumObjs++; |
| 79 | +} |
| 80 | + |
| 81 | +// Destructor |
| 82 | +SoftwareSerial::~SoftwareSerial() |
| 83 | +{ |
| 84 | + if( gSoftwareSerialNumObjs < 1 ){ |
| 85 | + return; // error -- no instances left to destroy |
| 86 | + } |
| 87 | + |
| 88 | + // Remove from global pointer list by filtering others down: |
| 89 | + uint8_t index = _indexNumber; |
| 90 | + do{ |
| 91 | + gpSoftwareSerialObjs[index] = NULL; |
| 92 | + if( index < (gSoftwareSerialNumObjs-1) ){ |
| 93 | + gpSoftwareSerialObjs[index] = gpSoftwareSerialObjs[index+1]; |
| 94 | + } |
| 95 | + index++; |
| 96 | + }while( index < gSoftwareSerialNumObjs ); |
| 97 | + gSoftwareSerialNumObjs--; |
46 | 98 | } |
47 | 99 |
|
48 | 100 | void SoftwareSerial::begin(uint32_t baudRate) |
@@ -81,8 +133,7 @@ void SoftwareSerial::begin(uint32_t baudRate, HardwareSerial_Config_e SSconfig) |
81 | 133 | //Clear compare interrupt |
82 | 134 | am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREH); |
83 | 135 |
|
84 | | - attachInterrupt(digitalPinToInterrupt(_rxPin), rxBit, CHANGE); |
85 | | - //attachInterruptArg(digitalPinToInterrupt(_rxPin), rxBit, (void *)this, CHANGE); |
| 136 | + attachInterrupt(digitalPinToInterrupt(_rxPin), _software_serial_isr, CHANGE); |
86 | 137 | } |
87 | 138 |
|
88 | 139 | ap3_err_t SoftwareSerial::softwareserialSetConfig(HardwareSerial_Config_e SSconfig) |
|
0 commit comments