Skip to content

Commit 25b7ef0

Browse files
miketeachmanleifbirger
authored andcommitted
stm32,esp32: In machine_i2s, make object reference arrays root pointers.
This change eliminates the risk of the IRQ callback accessing invalid data. Discussed here: micropython#7183 (comment) Signed-off-by: Mike Teachman <mike.teachman@gmail.com>
1 parent 13e05e5 commit 25b7ef0

File tree

8 files changed

+32
-26
lines changed

8 files changed

+32
-26
lines changed

ports/esp32/machine_i2s.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,9 @@ STATIC const int8_t i2s_frame_map[NUM_I2S_USER_FORMATS][I2S_RX_FRAME_SIZE_IN_BYT
147147
{ 4, 5, 6, 7, 0, 1, 2, 3 }, // Stereo, 32-bits
148148
};
149149

150-
STATIC machine_i2s_obj_t *machine_i2s_obj[I2S_NUM_MAX];
151-
152150
void machine_i2s_init0() {
153151
for (i2s_port_t p = 0; p < I2S_NUM_MAX; p++) {
154-
machine_i2s_obj[p] = NULL;
152+
MP_STATE_PORT(machine_i2s_obj)[p] = NULL;
155153
}
156154
}
157155

@@ -507,13 +505,13 @@ STATIC mp_obj_t machine_i2s_make_new(const mp_obj_type_t *type, size_t n_pos_arg
507505
}
508506

509507
machine_i2s_obj_t *self;
510-
if (machine_i2s_obj[port] == NULL) {
508+
if (MP_STATE_PORT(machine_i2s_obj)[port] == NULL) {
511509
self = m_new_obj(machine_i2s_obj_t);
512-
machine_i2s_obj[port] = self;
510+
MP_STATE_PORT(machine_i2s_obj)[port] = self;
513511
self->base.type = &machine_i2s_type;
514512
self->port = port;
515513
} else {
516-
self = machine_i2s_obj[port];
514+
self = MP_STATE_PORT(machine_i2s_obj)[port];
517515
machine_i2s_deinit(self);
518516
}
519517

ports/esp32/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <stdint.h>
88
#include <alloca.h>
99
#include "esp_system.h"
10+
#include "freertos/FreeRTOS.h"
11+
#include "driver/i2s.h"
1012

1113
// object representation and NLR handling
1214
#define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_A)
@@ -249,6 +251,7 @@ struct mp_bluetooth_nimble_root_pointers_t;
249251
const char *readline_hist[8]; \
250252
mp_obj_t machine_pin_irq_handler[40]; \
251253
struct _machine_timer_obj_t *machine_timer_obj_head; \
254+
struct _machine_i2s_obj_t *machine_i2s_obj[I2S_NUM_MAX]; \
252255
MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE
253256

254257
// type definitions for the specific machine

ports/stm32/boards/PYBD_SF2/mpconfigboard.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#define MICROPY_HW_ENABLE_SDCARD (1)
4141
#define MICROPY_HW_ENABLE_MMCARD (1)
4242
#define MICROPY_HW_ENABLE_RF_SWITCH (1)
43-
#define MICROPY_HW_ENABLE_I2S (1)
4443

4544
#define MICROPY_BOARD_EARLY_INIT board_early_init
4645
#define MICROPY_BOARD_ENTER_STOP board_sleep(1);

ports/stm32/boards/PYBV10/mpconfigboard.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define MICROPY_HW_ENABLE_DAC (1)
1212
#define MICROPY_HW_ENABLE_USB (1)
1313
#define MICROPY_HW_ENABLE_SDCARD (1)
14-
#define MICROPY_HW_ENABLE_I2S (1)
1514

1615
// HSE is 8MHz
1716
#define MICROPY_HW_CLK_PLLM (8)

ports/stm32/boards/PYBV11/mpconfigboard.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define MICROPY_HW_ENABLE_DAC (1)
1212
#define MICROPY_HW_ENABLE_USB (1)
1313
#define MICROPY_HW_ENABLE_SDCARD (1)
14-
#define MICROPY_HW_ENABLE_I2S (1)
1514

1615
// HSE is 12MHz
1716
#define MICROPY_HW_CLK_PLLM (12)

ports/stm32/machine_i2s.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@
8181
// 32 byte address boundary. Not all STM32 devices have a D-Cache. Buffer alignment
8282
// will still happen on these devices to keep this code simple.
8383

84-
#define MAX_I2S_STM32 (2)
85-
8684
// DMA ping-pong buffer size was empirically determined. It is a tradeoff between:
8785
// 1. memory use (smaller buffer size desirable to reduce memory footprint)
8886
// 2. interrupt frequency (larger buffer size desirable to reduce interrupt frequency)
@@ -164,11 +162,9 @@ STATIC const int8_t i2s_frame_map[NUM_I2S_USER_FORMATS][I2S_RX_FRAME_SIZE_IN_BYT
164162
{ 2, 3, 0, 1, 6, 7, 4, 5 }, // Stereo, 32-bits
165163
};
166164

167-
STATIC machine_i2s_obj_t *machine_i2s_obj[MAX_I2S_STM32];
168-
169165
void machine_i2s_init0() {
170-
for (uint8_t i = 0; i < MAX_I2S_STM32; i++) {
171-
machine_i2s_obj[i] = NULL;
166+
for (uint8_t i = 0; i < MICROPY_HW_MAX_I2S; i++) {
167+
MP_STATE_PORT(machine_i2s_obj)[i] = NULL;
172168
}
173169
}
174170

@@ -601,9 +597,9 @@ void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s) {
601597
void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s) {
602598
machine_i2s_obj_t *self;
603599
if (hi2s->Instance == I2S1) {
604-
self = machine_i2s_obj[0];
600+
self = MP_STATE_PORT(machine_i2s_obj)[0];
605601
} else {
606-
self = machine_i2s_obj[1];
602+
self = MP_STATE_PORT(machine_i2s_obj)[1];
607603
}
608604

609605
// bottom half of buffer now filled,
@@ -620,9 +616,9 @@ void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s) {
620616
void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s) {
621617
machine_i2s_obj_t *self;
622618
if (hi2s->Instance == I2S1) {
623-
self = machine_i2s_obj[0];
619+
self = MP_STATE_PORT(machine_i2s_obj)[0];
624620
} else {
625-
self = machine_i2s_obj[1];
621+
self = MP_STATE_PORT(machine_i2s_obj)[1];
626622
}
627623

628624
// top half of buffer now filled,
@@ -640,9 +636,9 @@ void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s) {
640636
machine_i2s_obj_t *self;
641637

642638
if (hi2s->Instance == I2S1) {
643-
self = machine_i2s_obj[0];
639+
self = MP_STATE_PORT(machine_i2s_obj)[0];
644640
} else {
645-
self = machine_i2s_obj[1];
641+
self = MP_STATE_PORT(machine_i2s_obj)[1];
646642
}
647643

648644
// for non-blocking operation, this IRQ-based callback handles
@@ -659,9 +655,9 @@ void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s) {
659655
void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s) {
660656
machine_i2s_obj_t *self;
661657
if (hi2s->Instance == I2S1) {
662-
self = machine_i2s_obj[0];
658+
self = MP_STATE_PORT(machine_i2s_obj)[0];
663659
} else {
664-
self = machine_i2s_obj[1];
660+
self = MP_STATE_PORT(machine_i2s_obj)[1];
665661
}
666662

667663
// for non-blocking operation, this IRQ-based callback handles
@@ -859,13 +855,13 @@ STATIC mp_obj_t machine_i2s_make_new(const mp_obj_type_t *type, size_t n_pos_arg
859855
}
860856

861857
machine_i2s_obj_t *self;
862-
if (machine_i2s_obj[i2s_id_zero_base] == NULL) {
858+
if (MP_STATE_PORT(machine_i2s_obj)[i2s_id_zero_base] == NULL) {
863859
self = m_new_obj(machine_i2s_obj_t);
864-
machine_i2s_obj[i2s_id_zero_base] = self;
860+
MP_STATE_PORT(machine_i2s_obj)[i2s_id_zero_base] = self;
865861
self->base.type = &machine_i2s_type;
866862
self->i2s_id = i2s_id;
867863
} else {
868-
self = machine_i2s_obj[i2s_id_zero_base];
864+
self = MP_STATE_PORT(machine_i2s_obj)[i2s_id_zero_base];
869865
machine_i2s_deinit(MP_OBJ_FROM_PTR(self));
870866
}
871867

ports/stm32/mpconfigboard_common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,15 @@
490490
#define MICROPY_HW_MAX_CAN (1)
491491
#endif
492492

493+
// Enable I2S if there are any peripherals defined
494+
#if defined(MICROPY_HW_I2S1) || defined(MICROPY_HW_I2S2)
495+
#define MICROPY_HW_ENABLE_I2S (1)
496+
#define MICROPY_HW_MAX_I2S (2)
497+
#else
498+
#define MICROPY_HW_ENABLE_I2S (0)
499+
#define MICROPY_HW_MAX_I2S (0)
500+
#endif
501+
493502
// Define MICROPY_HW_SDMMCx_CK values if that peripheral is used, so that make-pins.py
494503
// generates the relevant AF constants.
495504
#if MICROPY_HW_SDCARD_SDMMC == 1 || MICROPY_HW_SDIO_SDMMC == 1

ports/stm32/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ struct _mp_bluetooth_btstack_root_pointers_t;
342342
/* pointers to all CAN objects (if they have been created) */ \
343343
struct _pyb_can_obj_t *pyb_can_obj_all[MICROPY_HW_MAX_CAN]; \
344344
\
345+
/* pointers to all I2S objects (if they have been created) */ \
346+
struct _machine_i2s_obj_t *machine_i2s_obj[MICROPY_HW_MAX_I2S]; \
347+
\
345348
/* USB_VCP IRQ callbacks (if they have been set) */ \
346349
mp_obj_t pyb_usb_vcp_irq[MICROPY_HW_USB_CDC_NUM]; \
347350
\

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy