Skip to content

Commit 9cf46ec

Browse files
committed
put neopixel_write buffer in root pointers
1 parent 23d6a3d commit 9cf46ec

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

ports/nrf/common-hal/neopixel_write/__init__.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "py/mphal.h"
28+
#include "py/mpstate.h"
2829
#include "shared-bindings/neopixel_write/__init__.h"
2930
#include "nrf_pwm.h"
3031

@@ -97,14 +98,11 @@ static NRF_PWM_Type* find_free_pwm (void) {
9798
return NULL;
9899
}
99100

100-
static uint16_t* pixels_pattern_heap = NULL;
101101
static size_t pixels_pattern_heap_size = 0;
102-
static bool pattern_on_heap = false;
103102
// Called during reset_port() to free the pattern buffer
104103
void neopixel_write_reset(void) {
105-
pixels_pattern_heap = NULL;
104+
MP_STATE_VM(pixels_pattern_heap) = NULL;
106105
pixels_pattern_heap_size = 0;
107-
pattern_on_heap = false;
108106
}
109107

110108
uint64_t next_start_tick_ms = 0;
@@ -148,35 +146,37 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout
148146
(void) sd_softdevice_is_enabled(&sd_en);
149147

150148
if (pixels_pattern_heap_size < pattern_size) {
151-
if (pattern_on_heap) {
152-
m_free(pixels_pattern_heap);
149+
// Current heap buffer is too small.
150+
if (MP_STATE_VM(pixels_pattern_heap)) {
151+
// Old pixels_pattern_heap will be gc'd; don't free it.
153152
pixels_pattern = NULL;
154-
pixels_pattern_heap = NULL;
153+
MP_STATE_VM(pixels_pattern_heap) = NULL;
155154
pixels_pattern_heap_size = 0;
156155
}
157156

158157
if (sd_en) {
159158
// If the soft device is enabled then we must use PWM to
160159
// transmit. This takes a bunch of memory to do so raise an
161160
// exception if we can't.
162-
pixels_pattern_heap = (uint16_t *) m_malloc(pattern_size, false);
161+
MP_STATE_VM(pixels_pattern_heap) = (uint16_t *) m_malloc(pattern_size, false);
163162
} else {
164-
pixels_pattern_heap = (uint16_t *) m_malloc_maybe(pattern_size, false);
163+
// Might return NULL.
164+
MP_STATE_VM(pixels_pattern_heap) = (uint16_t *) m_malloc_maybe(pattern_size, false);
165165
}
166-
if (pixels_pattern_heap) {
167-
pattern_on_heap = true;
166+
if (MP_STATE_VM(pixels_pattern_heap)) {
168167
pixels_pattern_heap_size = pattern_size;
169168
}
170169
}
171-
pixels_pattern = pixels_pattern_heap;
170+
// Might be NULL, which means we failed to allocate.
171+
pixels_pattern = MP_STATE_VM(pixels_pattern_heap);
172172
}
173173
}
174174

175175
// Wait to make sure we don't append onto the last transmission.
176176
wait_until(next_start_tick_ms, next_start_tick_us);
177177

178178
// Use the identified device to choose the implementation
179-
// If a PWM device is available use DMA
179+
// If a PWM device is available and we have a buffer, use DMA.
180180
if ( (pixels_pattern != NULL) && (pwm != NULL) ) {
181181
uint16_t pos = 0; // bit position
182182

ports/nrf/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164

165165
#define MICROPY_PORT_ROOT_POINTERS \
166166
CIRCUITPY_COMMON_ROOT_POINTERS \
167+
uint16_t* pixels_pattern_heap; \
167168
ble_drv_evt_handler_entry_t* ble_drv_evt_handler_entries; \
168169

169170

0 commit comments

Comments
 (0)