Skip to content

Commit f3b8bc4

Browse files
committed
Add ISR for software serial
1 parent 1d58ae1 commit f3b8bc4

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

cores/arduino/ard_sup/gpio/ap3_gpio.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ extern "C" void am_gpio_isr(void)
133133
uint64_t gpio_int_mask = 0x00;
134134
am_hal_gpio_interrupt_status_get(true, &gpio_int_mask);
135135

136-
if (gpio_int_mask & softwareserial_pin_mask)
137-
{
138-
}
139-
140136
for (uint8_t indi = 0; indi < gpio_num_isr; indi++)
141137
{
142138
if (gpio_isr_entries[indi].callback != NULL)

libraries/SoftwareSerial/src/SoftwareSerial.cpp

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,66 @@
3535
#include "SoftwareSerial.h"
3636
#include "Arduino.h"
3737

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+
3860
//Constructor
3961
SoftwareSerial::SoftwareSerial(uint8_t rxPin, uint8_t txPin)
4062
{
63+
if( gSoftwareSerialNumObjs >= AP3_GPIO_MAX_PADS ){
64+
return; // Error -- no instances left to create
65+
}
66+
4167
_rxPin = rxPin;
4268
_txPin = txPin;
4369

4470
_txPad = ap3_gpio_pin2pad(_txPin);
4571
_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--;
4698
}
4799

48100
void SoftwareSerial::begin(uint32_t baudRate)
@@ -81,8 +133,7 @@ void SoftwareSerial::begin(uint32_t baudRate, HardwareSerial_Config_e SSconfig)
81133
//Clear compare interrupt
82134
am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREH);
83135

84-
attachInterrupt(digitalPinToInterrupt(_rxPin), rxBit, CHANGE);
85-
//attachInterruptArg(digitalPinToInterrupt(_rxPin), rxBit, (void *)this, CHANGE);
136+
attachInterrupt(digitalPinToInterrupt(_rxPin), _software_serial_isr, CHANGE);
86137
}
87138

88139
ap3_err_t SoftwareSerial::softwareserialSetConfig(HardwareSerial_Config_e SSconfig)

libraries/SoftwareSerial/src/SoftwareSerial.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class SoftwareSerial
4444
{
4545
public:
4646
SoftwareSerial(uint8_t rxPin, uint8_t txPin);
47+
~SoftwareSerial();
4748

4849
void begin(uint32_t baudRate);
4950
void begin(uint32_t baudRate, HardwareSerial_Config_e SSconfig);
@@ -52,6 +53,8 @@ class SoftwareSerial
5253

5354
void rxBit(void);
5455

56+
uint64_t _rxPadBitMask; // The AM HAL style pad bit mask associated with the RX pad
57+
5558
private:
5659
void startRXListening(void);
5760

@@ -70,6 +73,8 @@ class SoftwareSerial
7073
uint8_t _rxPin;
7174
uint8_t _txPin;
7275

76+
uint8_t _indexNumber; // The index number at which the pointer to this instance is stored in the global object table.
77+
7378
ap3_gpio_pad_t _txPad;
7479
ap3_gpio_pad_t _rxPad;
7580

0 commit comments

Comments
 (0)