Skip to content

Commit 4f7e806

Browse files
committed
Disable IRAM ISRs and functions by default
1 parent 05db08e commit 4f7e806

20 files changed

+85
-65
lines changed

Kconfig.projbuild

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ config ARDUINO_UDP_RUNNING_CORE
8282
default 1 if ARDUINO_UDP_RUN_CORE1
8383
default -1 if ARDUINO_UDP_RUN_NO_AFFINITY
8484

85+
config ARDUINO_ISR_IRAM
86+
bool "Run interrupts in IRAM"
87+
default "n"
88+
help
89+
Enabling this option will Attach all interrupts with the IRAm flag.
90+
It will also make some HAL function, like, digitalRead/Write and more
91+
be loaded into IRAM for access inside ISRs.
92+
Beware that this is a very dangerous setting. Enable it only if you
93+
are fully aware of the consequences.
8594

8695
config DISABLE_HAL_LOCKS
8796
bool "Disable mutex locks for HAL"

cores/esp32/Esp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class EspClass
101101

102102
};
103103

104-
uint32_t IRAM_ATTR EspClass::getCycleCount()
104+
uint32_t ARDUINO_ISR_ATTR EspClass::getCycleCount()
105105
{
106106
uint32_t ccount;
107107
__asm__ __volatile__("esync; rsr %0,ccount":"=a" (ccount));

cores/esp32/FunctionalInterrupt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern "C"
1616
extern void __attachInterruptFunctionalArg(uint8_t pin, voidFuncPtrArg userFunc, void * arg, int intr_type, bool functional);
1717
}
1818

19-
void IRAM_ATTR interruptFunctional(void* arg)
19+
void ARDUINO_ISR_ATTR interruptFunctional(void* arg)
2020
{
2121
InterruptArgStructure* localArg = (InterruptArgStructure*)arg;
2222
if (localArg->interruptFunction)

cores/esp32/esp32-hal-dac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#error Target CONFIG_IDF_TARGET is not supported
3232
#endif
3333

34-
void IRAM_ATTR __dacWrite(uint8_t pin, uint8_t value)
34+
void ARDUINO_ISR_ATTR __dacWrite(uint8_t pin, uint8_t value)
3535
{
3636
if(pin < DAC1 || pin > DAC2){
3737
return;//not dac pin

cores/esp32/esp32-hal-gpio.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ static InterruptHandle_t __pinInterruptHandlers[GPIO_PIN_COUNT] = {0,};
157157

158158
#include "driver/rtc_io.h"
159159

160-
extern void IRAM_ATTR __pinMode(uint8_t pin, uint8_t mode)
160+
extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode)
161161
{
162162

163163
if(!digitalPinIsValid(pin)) {
@@ -254,7 +254,7 @@ extern void IRAM_ATTR __pinMode(uint8_t pin, uint8_t mode)
254254
GPIO.pin[pin].val = pinControl;
255255
}
256256

257-
extern void IRAM_ATTR __digitalWrite(uint8_t pin, uint8_t val)
257+
extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val)
258258
{
259259
if(val) {
260260
if(pin < 32) {
@@ -271,7 +271,7 @@ extern void IRAM_ATTR __digitalWrite(uint8_t pin, uint8_t val)
271271
}
272272
}
273273

274-
extern int IRAM_ATTR __digitalRead(uint8_t pin)
274+
extern int ARDUINO_ISR_ATTR __digitalRead(uint8_t pin)
275275
{
276276
if(pin < 32) {
277277
return (GPIO.in >> pin) & 0x1;
@@ -283,7 +283,7 @@ extern int IRAM_ATTR __digitalRead(uint8_t pin)
283283

284284
static intr_handle_t gpio_intr_handle = NULL;
285285

286-
static void IRAM_ATTR __onPinInterrupt()
286+
static void ARDUINO_ISR_ATTR __onPinInterrupt()
287287
{
288288
uint32_t gpio_intr_status_l=0;
289289
uint32_t gpio_intr_status_h=0;
@@ -331,7 +331,7 @@ extern void __attachInterruptFunctionalArg(uint8_t pin, voidFuncPtrArg userFunc,
331331

332332
if(!interrupt_initialized) {
333333
interrupt_initialized = true;
334-
esp_intr_alloc(ETS_GPIO_INTR_SOURCE, (int)ESP_INTR_FLAG_IRAM, __onPinInterrupt, NULL, &gpio_intr_handle);
334+
esp_intr_alloc(ETS_GPIO_INTR_SOURCE, (int)ARDUINO_ISR_FLAG, __onPinInterrupt, NULL, &gpio_intr_handle);
335335
}
336336

337337
// if new attach without detach remove old info

cores/esp32/esp32-hal-i2c.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ static i2c_t _i2c_bus_array[2] = {
242242

243243
/* Stickbreaker ISR mode debug support
244244
*/
245-
static void IRAM_ATTR i2cDumpCmdQueue(i2c_t *i2c)
245+
static void ARDUINO_ISR_ATTR i2cDumpCmdQueue(i2c_t *i2c)
246246
{
247247
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR)&&(defined ENABLE_I2C_DEBUG_BUFFER)
248248
static const char * const cmdName[] ={"RSTART","WRITE","READ","STOP","END"};
@@ -372,7 +372,7 @@ static void i2cDumpInts(uint8_t num)
372372
#endif
373373

374374
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)&&(defined ENABLE_I2C_DEBUG_BUFFER)
375-
static void IRAM_ATTR i2cDumpStatus(i2c_t * i2c){
375+
static void ARDUINO_ISR_ATTR i2cDumpStatus(i2c_t * i2c){
376376
typedef union {
377377
struct {
378378
uint32_t ack_rec: 1; /*This register stores the value of ACK bit.*/
@@ -446,7 +446,7 @@ if(i != fifoPos){// actual data
446446
}
447447
#endif
448448

449-
static void IRAM_ATTR i2cTriggerDumps(i2c_t * i2c, uint8_t trigger, const char locus[]){
449+
static void ARDUINO_ISR_ATTR i2cTriggerDumps(i2c_t * i2c, uint8_t trigger, const char locus[]){
450450
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)&&(defined ENABLE_I2C_DEBUG_BUFFER)
451451
if( trigger ){
452452
log_i("%s",locus);
@@ -493,7 +493,7 @@ static void i2cApbChangeCallback(void * arg, apb_change_ev_t ev_type, uint32_t o
493493
}
494494
/* End of CPU Clock change Support
495495
*/
496-
static void IRAM_ATTR i2cSetCmd(i2c_t * i2c, uint8_t index, uint8_t op_code, uint8_t byte_num, bool ack_val, bool ack_exp, bool ack_check)
496+
static void ARDUINO_ISR_ATTR i2cSetCmd(i2c_t * i2c, uint8_t index, uint8_t op_code, uint8_t byte_num, bool ack_val, bool ack_exp, bool ack_check)
497497
{
498498
I2C_COMMAND_t cmd;
499499
cmd.val=0;
@@ -506,7 +506,7 @@ static void IRAM_ATTR i2cSetCmd(i2c_t * i2c, uint8_t index, uint8_t op_code, uin
506506
}
507507

508508

509-
static void IRAM_ATTR fillCmdQueue(i2c_t * i2c, bool INTS)
509+
static void ARDUINO_ISR_ATTR fillCmdQueue(i2c_t * i2c, bool INTS)
510510
{
511511
/* this function is called on initial i2cProcQueue() or when a I2C_END_DETECT_INT occurs
512512
*/
@@ -664,7 +664,7 @@ static void IRAM_ATTR fillCmdQueue(i2c_t * i2c, bool INTS)
664664
}
665665
}
666666

667-
static void IRAM_ATTR fillTxFifo(i2c_t * i2c)
667+
static void ARDUINO_ISR_ATTR fillTxFifo(i2c_t * i2c)
668668
{
669669
/*
670670
12/01/2017 The Fifo's are independent, 32 bytes of tx and 32 bytes of Rx.
@@ -756,7 +756,7 @@ static void IRAM_ATTR fillTxFifo(i2c_t * i2c)
756756
}
757757

758758

759-
static void IRAM_ATTR emptyRxFifo(i2c_t * i2c)
759+
static void ARDUINO_ISR_ATTR emptyRxFifo(i2c_t * i2c)
760760
{
761761
uint32_t d, cnt=0, moveCnt;
762762

@@ -815,7 +815,7 @@ static void IRAM_ATTR emptyRxFifo(i2c_t * i2c)
815815
#endif
816816
}
817817

818-
static void IRAM_ATTR i2cIsrExit(i2c_t * i2c,const uint32_t eventCode,bool Fatal)
818+
static void ARDUINO_ISR_ATTR i2cIsrExit(i2c_t * i2c,const uint32_t eventCode,bool Fatal)
819819
{
820820

821821
switch(eventCode) {
@@ -860,7 +860,7 @@ static void IRAM_ATTR i2cIsrExit(i2c_t * i2c,const uint32_t eventCode,bool Fatal
860860

861861
}
862862

863-
static void IRAM_ATTR i2c_update_error_byte_cnt(i2c_t * i2c)
863+
static void ARDUINO_ISR_ATTR i2c_update_error_byte_cnt(i2c_t * i2c)
864864
{
865865
/* i2c_update_error_byte_cnt 07/18/2018
866866
Only called after an error has occurred, so, most of the time this function is never used.
@@ -907,7 +907,7 @@ static void IRAM_ATTR i2c_update_error_byte_cnt(i2c_t * i2c)
907907
i2c->errorByteCnt = bc;
908908
}
909909

910-
static void IRAM_ATTR i2c_isr_handler_default(void* arg)
910+
static void ARDUINO_ISR_ATTR i2c_isr_handler_default(void* arg)
911911
{
912912
i2c_t* p_i2c = (i2c_t*) arg; // recover data
913913
uint32_t activeInt = p_i2c->dev->int_status.val&0x7FF;
@@ -1254,7 +1254,7 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
12541254
if(!i2c->intr_handle) { // create ISR for either peripheral
12551255
// log_i("create ISR %d",i2c->num);
12561256
uint32_t ret = 0;
1257-
uint32_t flags = ESP_INTR_FLAG_IRAM | //< ISR can be called if cache is disabled
1257+
uint32_t flags = ARDUINO_ISR_FLAG | //< ISR can be called if cache is disabled
12581258
ESP_INTR_FLAG_LOWMED | //< Low and medium prio interrupts. These can be handled in C.
12591259
ESP_INTR_FLAG_SHARED; //< Reduce resource requirements, Share interrupts
12601260

cores/esp32/esp32-hal-matrix.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,27 @@
3232
#define MATRIX_DETACH_IN_LOW_PIN 0x30
3333
#define MATRIX_DETACH_IN_LOW_HIGH 0x38
3434

35-
void IRAM_ATTR pinMatrixOutAttach(uint8_t pin, uint8_t function, bool invertOut, bool invertEnable)
35+
void ARDUINO_ISR_ATTR pinMatrixOutAttach(uint8_t pin, uint8_t function, bool invertOut, bool invertEnable)
3636
{
3737
gpio_matrix_out(pin, function, invertOut, invertEnable);
3838
}
3939

40-
void IRAM_ATTR pinMatrixOutDetach(uint8_t pin, bool invertOut, bool invertEnable)
40+
void ARDUINO_ISR_ATTR pinMatrixOutDetach(uint8_t pin, bool invertOut, bool invertEnable)
4141
{
4242
gpio_matrix_out(pin, MATRIX_DETACH_OUT_SIG, invertOut, invertEnable);
4343
}
4444

45-
void IRAM_ATTR pinMatrixInAttach(uint8_t pin, uint8_t signal, bool inverted)
45+
void ARDUINO_ISR_ATTR pinMatrixInAttach(uint8_t pin, uint8_t signal, bool inverted)
4646
{
4747
gpio_matrix_in(pin, signal, inverted);
4848
}
4949

50-
void IRAM_ATTR pinMatrixInDetach(uint8_t signal, bool high, bool inverted)
50+
void ARDUINO_ISR_ATTR pinMatrixInDetach(uint8_t signal, bool high, bool inverted)
5151
{
5252
gpio_matrix_in(high?MATRIX_DETACH_IN_LOW_HIGH:MATRIX_DETACH_IN_LOW_PIN, signal, inverted);
5353
}
5454
/*
55-
void IRAM_ATTR intrMatrixAttach(uint32_t source, uint32_t inum){
55+
void ARDUINO_ISR_ATTR intrMatrixAttach(uint32_t source, uint32_t inum){
5656
intr_matrix_set(PRO_CPU_NUM, source, inum);
5757
}
5858
*/

cores/esp32/esp32-hal-misc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ BaseType_t xTaskCreateUniversal( TaskFunction_t pxTaskCode,
143143
#endif
144144
}
145145

146-
unsigned long IRAM_ATTR micros()
146+
unsigned long ARDUINO_ISR_ATTR micros()
147147
{
148148
return (unsigned long) (esp_timer_get_time());
149149
}
150150

151-
unsigned long IRAM_ATTR millis()
151+
unsigned long ARDUINO_ISR_ATTR millis()
152152
{
153153
return (unsigned long) (esp_timer_get_time() / 1000ULL);
154154
}
@@ -158,7 +158,7 @@ void delay(uint32_t ms)
158158
vTaskDelay(ms / portTICK_PERIOD_MS);
159159
}
160160

161-
void IRAM_ATTR delayMicroseconds(uint32_t us)
161+
void ARDUINO_ISR_ATTR delayMicroseconds(uint32_t us)
162162
{
163163
uint32_t m = micros();
164164
if(us){
@@ -239,7 +239,7 @@ void initArduino()
239239
}
240240

241241
//used by hal log
242-
const char * IRAM_ATTR pathToFileName(const char * path)
242+
const char * ARDUINO_ISR_ATTR pathToFileName(const char * path)
243243
{
244244
size_t i = 0;
245245
size_t pos = 0;

cores/esp32/esp32-hal-psram.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,34 @@ bool psramInit(){
7777
log_e("PSRAM could not be added to the heap!");
7878
return false;
7979
}
80+
#endif
81+
#if CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL
82+
heap_caps_malloc_extmem_enable(CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL);
8083
#endif
8184
spiramDetected = true;
8285
log_d("PSRAM enabled");
8386
return true;
8487
}
8588

86-
bool IRAM_ATTR psramFound(){
89+
bool ARDUINO_ISR_ATTR psramFound(){
8790
return spiramDetected;
8891
}
8992

90-
void IRAM_ATTR *ps_malloc(size_t size){
93+
void ARDUINO_ISR_ATTR *ps_malloc(size_t size){
9194
if(!spiramDetected){
9295
return NULL;
9396
}
9497
return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
9598
}
9699

97-
void IRAM_ATTR *ps_calloc(size_t n, size_t size){
100+
void ARDUINO_ISR_ATTR *ps_calloc(size_t n, size_t size){
98101
if(!spiramDetected){
99102
return NULL;
100103
}
101104
return heap_caps_calloc(n, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
102105
}
103106

104-
void IRAM_ATTR *ps_realloc(void *ptr, size_t size){
107+
void ARDUINO_ISR_ATTR *ps_realloc(void *ptr, size_t size){
105108
if(!spiramDetected){
106109
return NULL;
107110
}
@@ -114,19 +117,19 @@ bool psramInit(){
114117
return false;
115118
}
116119

117-
bool IRAM_ATTR psramFound(){
120+
bool ARDUINO_ISR_ATTR psramFound(){
118121
return false;
119122
}
120123

121-
void IRAM_ATTR *ps_malloc(size_t size){
124+
void ARDUINO_ISR_ATTR *ps_malloc(size_t size){
122125
return NULL;
123126
}
124127

125-
void IRAM_ATTR *ps_calloc(size_t n, size_t size){
128+
void ARDUINO_ISR_ATTR *ps_calloc(size_t n, size_t size){
126129
return NULL;
127130
}
128131

129-
void IRAM_ATTR *ps_realloc(void *ptr, size_t size){
132+
void ARDUINO_ISR_ATTR *ps_realloc(void *ptr, size_t size){
130133
return NULL;
131134
}
132135

cores/esp32/esp32-hal-rmt.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,17 @@ static void _initPin(int pin, int channel, bool tx_not_rx);
137137

138138
static bool _rmtSendOnce(rmt_obj_t* rmt, rmt_data_t* data, size_t size, bool continuous);
139139

140-
static void IRAM_ATTR _rmt_isr(void* arg);
140+
static void ARDUINO_ISR_ATTR _rmt_isr(void* arg);
141141

142142
static rmt_obj_t* _rmtAllocate(int pin, int from, int size);
143143

144144
static void _initPin(int pin, int channel, bool tx_not_rx);
145145

146-
static int IRAM_ATTR _rmt_get_mem_len(uint8_t channel);
146+
static int ARDUINO_ISR_ATTR _rmt_get_mem_len(uint8_t channel);
147147

148-
static void IRAM_ATTR _rmt_tx_mem_first(uint8_t ch);
148+
static void ARDUINO_ISR_ATTR _rmt_tx_mem_first(uint8_t ch);
149149

150-
static void IRAM_ATTR _rmt_tx_mem_second(uint8_t ch);
150+
static void ARDUINO_ISR_ATTR _rmt_tx_mem_second(uint8_t ch);
151151

152152

153153
/**
@@ -270,7 +270,7 @@ bool rmtWrite(rmt_obj_t* rmt, rmt_data_t* data, size_t size)
270270
RMT_MUTEX_LOCK(channel);
271271
// setup interrupt handler if not yet installed for half and full tx
272272
if (!intr_handle) {
273-
esp_intr_alloc(ETS_RMT_INTR_SOURCE, (int)ESP_INTR_FLAG_IRAM, _rmt_isr, NULL, &intr_handle);
273+
esp_intr_alloc(ETS_RMT_INTR_SOURCE, (int)ARDUINO_ISR_FLAG, _rmt_isr, NULL, &intr_handle);
274274
}
275275

276276
rmt->data_size = size - MAX_DATA_PER_ITTERATION;
@@ -569,7 +569,7 @@ rmt_obj_t* rmtInit(int pin, bool tx_not_rx, rmt_reserve_memsize_t memsize)
569569

570570
// install interrupt if at least one channel is active
571571
if (!intr_handle) {
572-
esp_intr_alloc(ETS_RMT_INTR_SOURCE, (int)ESP_INTR_FLAG_IRAM, _rmt_isr, NULL, &intr_handle);
572+
esp_intr_alloc(ETS_RMT_INTR_SOURCE, (int)ARDUINO_ISR_FLAG, _rmt_isr, NULL, &intr_handle);
573573
}
574574
RMT_MUTEX_UNLOCK(channel);
575575

@@ -637,7 +637,7 @@ static void _initPin(int pin, int channel, bool tx_not_rx)
637637
}
638638

639639

640-
static void IRAM_ATTR _rmt_isr(void* arg)
640+
static void ARDUINO_ISR_ATTR _rmt_isr(void* arg)
641641
{
642642
int intr_val = RMT.int_st.val;
643643
size_t ch;
@@ -727,7 +727,7 @@ static void IRAM_ATTR _rmt_isr(void* arg)
727727
}
728728
}
729729

730-
static void IRAM_ATTR _rmt_tx_mem_second(uint8_t ch)
730+
static void ARDUINO_ISR_ATTR _rmt_tx_mem_second(uint8_t ch)
731731
{
732732
DEBUG_INTERRUPT_START(4)
733733
uint32_t* data = g_rmt_objects[ch].data_ptr;
@@ -779,7 +779,7 @@ static void IRAM_ATTR _rmt_tx_mem_second(uint8_t ch)
779779
DEBUG_INTERRUPT_END(4);
780780
}
781781

782-
static void IRAM_ATTR _rmt_tx_mem_first(uint8_t ch)
782+
static void ARDUINO_ISR_ATTR _rmt_tx_mem_first(uint8_t ch)
783783
{
784784
DEBUG_INTERRUPT_START(2);
785785
uint32_t* data = g_rmt_objects[ch].data_ptr;
@@ -830,7 +830,7 @@ static void IRAM_ATTR _rmt_tx_mem_first(uint8_t ch)
830830
DEBUG_INTERRUPT_END(2);
831831
}
832832

833-
static int IRAM_ATTR _rmt_get_mem_len(uint8_t channel)
833+
static int ARDUINO_ISR_ATTR _rmt_get_mem_len(uint8_t channel)
834834
{
835835
int block_num = RMT.conf_ch[channel].conf0.mem_size;
836836
int item_block_len = block_num * 64;

0 commit comments

Comments
 (0)