10000 Merge pull request #1963 from dhalbert/nrf-neopixel-fix · flummer/circuitpython@ecf2442 · GitHub
[go: up one dir, main page]

Skip to content

Commit ecf2442

Browse files
authored
Merge pull request adafruit#1963 from dhalbert/nrf-neopixel-fix
nrf: fix neopixel_write pwm buf size calc
2 parents 076a3f8 + 23bd861 commit ecf2442

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,23 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout
111111
//
112112
// If there is not enough memory, we will fall back to cycle counter
113113
// using DWT
114-
uint32_t pattern_size = numBytes * 8 * sizeof(uint16_t) + 2 * sizeof(uint16_t);
114+
115+
#define PATTERN_SIZE(numBytes) (numBytes * 8 * sizeof(uint16_t) + 2 * sizeof(uint16_t))
116+
117+
uint32_t pattern_size = PATTERN_SIZE(numBytes);
115118
uint16_t* pixels_pattern = NULL;
116119
bool pattern_on_heap = false;
117120

118121
// Use the stack to store 1 pixels worth of PWM data for the status led. uint32_t to ensure alignment.
119-
uint32_t one_pixel[8 * sizeof(uint16_t) + 1];
122+
// Make it at least as big as PATTERN_SIZE(3), for one pixel of RGB data.
123+
// PATTERN_SIZE is a multiple of 4, so we don't need round up to make sure one_pixel is large enough.
124+
uint32_t one_pixel[PATTERN_SIZE(3)/sizeof(uint32_t)];
120125

121126
NRF_PWM_Type* pwm = find_free_pwm();
122127

123128
// only malloc if there is PWM device available
124129
if ( pwm != NULL ) {
125-
if (pattern_size <= sizeof(one_pixel) * sizeof(uint32_t)) {
130+
if (pattern_size <= sizeof(one_pixel)) {
126131
pixels_pattern = (uint16_t *) one_pixel;
127132
} else {
128133
pixels_pattern = (uint16_t *) m_malloc_maybe(pattern_size, false);

0 commit comments

Comments
 (0)
0