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
38
SoftwareSerial *ap3_serial_handle = 0 ;
43
39
44
40
// Uncomment to enable debug pulses and Serial.prints
@@ -49,74 +45,29 @@ SoftwareSerial *ap3_serial_handle = 0;
49
45
ap3_gpio_pad_t debugPad = ap3_gpio_pin2pad(SS_DEBUG_PIN);
50
46
#endif
51
47
52
- // Software Serial ISR (To attach to pin change interrupts)
53
- /* void _software_serial_isr(void)
54
- {
55
- uint64_t gpio_int_mask = 0x00;
56
- am_hal_gpio_interrupt_status_get(true, &gpio_int_mask);
57
- SoftwareSerial *obj = NULL;
58
- for (uint8_t indi = 0; indi < gSoftwareSerialNumObjs; indi++)
59
- {
60
- obj = gpSoftwareSerialObjs[indi];
61
- if (obj == NULL)
62
- {
63
- break; // there should not be any null pointers in the global object table
64
- }
65
- if (obj->_rxPadBitMask & gpio_int_mask)
66
- {
67
- obj->rxBit();
68
- }
69
- }
70
- }*/
71
- inline void _software_serial_isr (void )
48
+ inline void _software_serial_isr (void * arg)
72
49
{
73
- ap3_serial_handle->rxBit ();
50
+ SoftwareSerial* handle = (SoftwareSerial*)arg;
51
+ handle->rxBit ();
74
52
}
75
53
76
54
// Constructor
77
55
SoftwareSerial::SoftwareSerial (uint8_t rxPin, uint8_t txPin, bool invertLogic)
78
56
{
79
- if (gSoftwareSerialNumObjs >= AP3_GPIO_MAX_PADS)
80
- {
81
- return ; // Error -- no instances left to create
82
- }
83
-
84
57
_rxPin = rxPin;
85
58
_txPin = txPin;
86
59
87
60
_txPad = ap3_gpio_pin2pad (_txPin);
88
61
_rxPad = ap3_gpio_pin2pad (_rxPin);
89
62
90
63
_invertLogic = invertLogic;
91
-
92
- _rxPadBitMask = (0x01 << _rxPad);
93
-
94
- // Add to the global array
95
- _indexNumber = gSoftwareSerialNumObjs ;
96
- gpSoftwareSerialObjs[_indexNumber] = this ;
97
- gSoftwareSerialNumObjs ++;
98
64
}
99
65
100
66
// Destructor
101
67
SoftwareSerial::~SoftwareSerial ()
102
68
{
103
- if (gSoftwareSerialNumObjs < 1 )
104
- {
105
- return ; // error -- no instances left to destroy
106
- }
107
-
108
- // Remove from global pointer list by filtering others down:
109
- uint8_t index = _indexNumber;
110
- do
111
- {
112
- gpSoftwareSerialObjs[index] = NULL ;
113
- if (index < (gSoftwareSerialNumObjs - 1 ))
114
- {
115
- gpSoftwareSerialObjs[index] = gpSoftwareSerialObjs[index + 1 ];
116
- }
117
- index++;
118
- } while (index < gSoftwareSerialNumObjs );
119
- gSoftwareSerialNumObjs --;
69
+ // Todo: call an "end" function that will detach the interrupt before removing the object
70
+ // (otherwise rx interrupts will try to access an invalid object)
120
71
}
121
72
122
73
void SoftwareSerial::begin (uint32_t baudRate)
@@ -157,10 +108,8 @@ void SoftwareSerial::begin(uint32_t baudRate, HardwareSerial_Config_e SSconfig)
157
108
// Clear compare interrupt
158
109
am_hal_stimer_int_clear (AM_HAL_STIMER_INT_COMPAREH);
159
110
160
- // Register the class into the local list
161
- ap3_serial_handle = this ;
162
-
163
- attachInterrupt (digitalPinToInterrupt (_rxPin), _software_serial_isr, CHANGE);
111
+ // Attach argument interrupt with 'this' as argument
112
+ attachInterruptArg (digitalPinToInterrupt (_rxPin), _software_serial_isr, (void *)this , CHANGE);
164
113
}
165
114
166
115
int SoftwareSerial::available ()
0 commit comments