diff options
| author | Dan Halbert <halbert@halwitz.org> | 2019-06-20 18:02:14 -0400 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2019-06-20 18:02:14 -0400 |
| commit | 23bd861c9ac27644c1568a0c89be6bc1d48362a4 (patch) | |
| tree | b3eed47a1f489a4fe23e1456eb22148c7de52dd4 | |
| parent | 076a3f8a9b1a80d5a038aa181e4a4ac6e317ba8c (diff) | |
nrf: fix neopixel_write pwm buf size calc
| -rw-r--r-- | ports/nrf/common-hal/neopixel_write/__init__.c | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/ports/nrf/common-hal/neopixel_write/__init__.c b/ports/nrf/common-hal/neopixel_write/__init__.c index fe2bab999..78e0038e8 100644 --- a/ports/nrf/common-hal/neopixel_write/__init__.c +++ b/ports/nrf/common-hal/neopixel_write/__init__.c @@ -111,18 +111,23 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout // // If there is not enough memory, we will fall back to cycle counter // using DWT - uint32_t pattern_size = numBytes * 8 * sizeof(uint16_t) + 2 * sizeof(uint16_t); + +#define PATTERN_SIZE(numBytes) (numBytes * 8 * sizeof(uint16_t) + 2 * sizeof(uint16_t)) + + uint32_t pattern_size = PATTERN_SIZE(numBytes); uint16_t* pixels_pattern = NULL; bool pattern_on_heap = false; // Use the stack to store 1 pixels worth of PWM data for the status led. uint32_t to ensure alignment. - uint32_t one_pixel[8 * sizeof(uint16_t) + 1]; + // Make it at least as big as PATTERN_SIZE(3), for one pixel of RGB data. + // PATTERN_SIZE is a multiple of 4, so we don't need round up to make sure one_pixel is large enough. + uint32_t one_pixel[PATTERN_SIZE(3)/sizeof(uint32_t)]; NRF_PWM_Type* pwm = find_free_pwm(); // only malloc if there is PWM device available if ( pwm != NULL ) { - if (pattern_size <= sizeof(one_pixel) * sizeof(uint32_t)) { + if (pattern_size <= sizeof(one_pixel)) { pixels_pattern = (uint16_t *) one_pixel; } else { pixels_pattern = (uint16_t *) m_malloc_maybe(pattern_size, false); |
