Save space by only supporting 800khz neopixels.

This commit is contained in:
Scott Shawcroft 2017-02-19 17:22:42 +01:00
parent 710b5d8aff
commit f0b62a2b0e
6 changed files with 29 additions and 71 deletions

View File

@ -30,7 +30,7 @@
#include "asf/common2/services/delay/delay.h"
void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes, bool is800KHz) {
void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
// This is adapted directly from the Adafruit NeoPixel library SAMD21G18A code:
// https://github.com/adafruit/Adafruit_NeoPixel/blob/master/Adafruit_NeoPixel.cpp
uint8_t *ptr, *end, p, bitMask;
@ -54,53 +54,22 @@ void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout,
volatile uint32_t *set = &(port->OUTSET.reg),
*clr = &(port->OUTCLR.reg);
if(is800KHz) {
for(;;) {
*set = pinMask;
asm("nop; nop;");
if(p & bitMask) {
asm("nop; nop; nop; nop; nop; nop; nop;");
*clr = pinMask;
} else {
*clr = pinMask;
asm("nop; nop;");
}
if((bitMask >>= 1) != 0) {
asm("nop; nop; nop; nop; nop;");
} else {
if(ptr >= end) break;
p = *ptr++;
bitMask = 0x80;
}
}
} else { // 400 KHz bitstream
for(;;) {
*set = pinMask;
for(;;) {
*set = pinMask;
asm("nop; nop;");
if(p & bitMask) {
asm("nop; nop; nop; nop; nop; nop; nop;");
if(p & bitMask) {
asm("nop; nop; nop; nop; nop; nop; nop; nop;"
"nop; nop; nop; nop; nop; nop; nop; nop;"
"nop; nop; nop;");
*clr = pinMask;
} else {
*clr = pinMask;
asm("nop; nop; nop; nop; nop; nop; nop; nop;"
"nop; nop; nop; nop; nop; nop; nop; nop;"
"nop; nop; nop; nop; nop; nop; nop; nop;"
"nop; nop; nop;");
}
asm("nop; nop; nop; nop; nop; nop; nop; nop;"
"nop; nop; nop; nop; nop; nop; nop; nop;"
"nop; nop; nop; nop; nop; nop; nop; nop;"
"nop; nop; nop; nop; nop; nop; nop; nop;");
if(bitMask >>= 1) {
asm("nop; nop; nop; nop; nop; nop; nop;");
} else {
if(ptr >= end) break;
p = *ptr++;
bitMask = 0x80;
}
*clr = pinMask;
} else {
*clr = pinMask;
asm("nop; nop;");
}
if((bitMask >>= 1) != 0) {
asm("nop; nop; nop; nop; nop;");
} else {
if(ptr >= end) break;
p = *ptr++;
bitMask = 0x80;
}
}

View File

@ -28,6 +28,6 @@
#include "espneopixel.h"
void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes, bool is800KHz) {
esp_neopixel_write(digitalinout->pin->gpio_number, pixels, numBytes, is800KHz);
void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
esp_neopixel_write(digitalinout->pin->gpio_number, pixels, numBytes);
}

View File

@ -16,7 +16,7 @@
#define NEO_KHZ400 (1)
void /*ICACHE_RAM_ATTR*/ esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32_t numBytes, bool is800KHz) {
void /*ICACHE_RAM_ATTR*/ esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32_t numBytes) {
uint8_t *p, *end, pix, mask;
uint32_t t, time0, time1, period, c, startTime, pinMask;
@ -30,19 +30,10 @@ void /*ICACHE_RAM_ATTR*/ esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32
uint32_t fcpu = system_get_cpu_freq() * 1000000;
#ifdef NEO_KHZ400
if(is800KHz) {
#endif
time0 = fcpu / 2857143; // 0.35us
time1 = fcpu / 1250000; // 0.8us
period = fcpu / 800000; // 1.25us per bit
#ifdef NEO_KHZ400
} else { // 400 KHz bitstream
time0 = fcpu / 2000000; // 0.5uS
time1 = fcpu / 833333; // 1.2us
period = fcpu / 400000; // 2.5us per bit
}
#endif
time0 = fcpu / 2857143; // 0.35us
time1 = fcpu / 1250000; // 0.8us
period = fcpu / 800000; // 1.25us per bit
uint32_t irq_state = mp_hal_quiet_timing_enter();
for(t = time0;; t = time0) {

View File

@ -1 +1 @@
void esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32_t numBytes, bool is800KHz);
void esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32_t numBytes);

View File

@ -40,7 +40,7 @@
//| :platform: SAMD21
//|
//| The `neopixel_write` module contains a helper method to write out bytes in
//| the neopixel protocol.
//| the 800khz neopixel protocol.
//| .. method:: neopixel_write.neopixel_write(digitalinout, buf, is800KHz)
//|
@ -48,19 +48,17 @@
//|
//| :param ~nativeio.DigitalInOut gpio: the DigitalInOut to output with
//| :param bytearray buf: The bytes to clock out. No assumption is made about color order
//| :param bool is800KHz: True if the pixels are 800KHz, otherwise 400KHz is assumed.
//|
STATIC mp_obj_t neopixel_write_neopixel_write_(mp_obj_t digitalinout_obj, mp_obj_t buf, mp_obj_t is800k) {
STATIC mp_obj_t neopixel_write_neopixel_write_(mp_obj_t digitalinout_obj, mp_obj_t buf) {
// Convert parameters into expected types.
const nativeio_digitalinout_obj_t *digitalinout = MP_OBJ_TO_PTR(digitalinout_obj);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
// Call platform's neopixel write function with provided buffer and options.
common_hal_neopixel_write(digitalinout, (uint8_t*)bufinfo.buf, bufinfo.len,
mp_obj_is_true(is800k));
common_hal_neopixel_write(digitalinout, (uint8_t*)bufinfo.buf, bufinfo.len);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(neopixel_write_neopixel_write_obj, neopixel_write_neopixel_write_);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(neopixel_write_neopixel_write_obj, neopixel_write_neopixel_write_);
STATIC const mp_rom_map_elem_t neopixel_write_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write) },

View File

@ -32,6 +32,6 @@
#include "common-hal/nativeio/types.h"
extern void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes, bool is800KHz);
extern void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes);
#endif