Jim Mussared
3 years ago
committed by
Damien George
7 changed files with 110 additions and 90 deletions
@ -1,74 +0,0 @@ |
|||
// Original version from https://github.com/adafruit/Adafruit_NeoPixel
|
|||
// Modifications by dpgeorge to support auto-CPU-frequency detection
|
|||
|
|||
// This is a mash-up of the Due show() code + insights from Michael Miller's
|
|||
// ESP8266 work for the NeoPixelBus library: github.com/Makuna/NeoPixelBus
|
|||
// Needs to be a separate .c file to enforce ICACHE_RAM_ATTR execution.
|
|||
|
|||
#include "py/mpconfig.h" |
|||
#include "py/mphal.h" |
|||
#include "modesp.h" |
|||
|
|||
void IRAM_ATTR esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32_t numBytes, uint8_t timing) { |
|||
uint8_t *p, *end, pix, mask; |
|||
uint32_t t, time0, time1, period, c, startTime, pinMask, gpio_reg_set, gpio_reg_clear; |
|||
|
|||
#if !CONFIG_IDF_TARGET_ESP32C3 |
|||
if (pin >= 32) { |
|||
pinMask = 1 << (pin - 32); |
|||
gpio_reg_set = GPIO_OUT1_W1TS_REG; |
|||
gpio_reg_clear = GPIO_OUT1_W1TC_REG; |
|||
} else |
|||
#endif |
|||
{ |
|||
pinMask = 1 << pin; |
|||
gpio_reg_set = GPIO_OUT_W1TS_REG; |
|||
gpio_reg_clear = GPIO_OUT_W1TC_REG; |
|||
} |
|||
p = pixels; |
|||
end = p + numBytes; |
|||
pix = *p++; |
|||
mask = 0x80; |
|||
startTime = 0; |
|||
|
|||
uint32_t fcpu = ets_get_cpu_frequency(); |
|||
|
|||
if (timing == 1) { |
|||
// 800 KHz
|
|||
time0 = (fcpu * 350) / 1000; // 0.35us
|
|||
time1 = (fcpu * 800) / 1000; // 0.8us
|
|||
period = (fcpu * 1250) / 1000; // 1.25us per bit
|
|||
} else { |
|||
// 400 KHz
|
|||
time0 = (fcpu * 500) / 1000; // 0.5us
|
|||
time1 = (fcpu * 1200) / 1000; // 1.2us
|
|||
period = (fcpu * 2500) / 1000; // 2.5us per bit
|
|||
} |
|||
|
|||
uint32_t irq_state = mp_hal_quiet_timing_enter(); |
|||
for (t = time0;; t = time0) { |
|||
if (pix & mask) { |
|||
t = time1; // Bit high duration
|
|||
} |
|||
while (((c = mp_hal_ticks_cpu()) - startTime) < period) { |
|||
; // Wait for bit start
|
|||
} |
|||
GPIO_REG_WRITE(gpio_reg_set, pinMask); // Set high
|
|||
startTime = c; // Save start time
|
|||
while (((c = mp_hal_ticks_cpu()) - startTime) < t) { |
|||
; // Wait high duration
|
|||
} |
|||
GPIO_REG_WRITE(gpio_reg_clear, pinMask); // Set low
|
|||
if (!(mask >>= 1)) { // Next bit/byte
|
|||
if (p >= end) { |
|||
break; |
|||
} |
|||
pix = *p++; |
|||
mask = 0x80; |
|||
} |
|||
} |
|||
while ((mp_hal_ticks_cpu() - startTime) < period) { |
|||
; // Wait for last bit
|
|||
} |
|||
mp_hal_quiet_timing_exit(irq_state); |
|||
} |
@ -0,0 +1,88 @@ |
|||
/*
|
|||
* This file is part of the MicroPython project, http://micropython.org/
|
|||
* |
|||
* The MIT License (MIT) |
|||
* |
|||
* Copyright (c) 2021 Jim Mussared |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in |
|||
* all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
* THE SOFTWARE. |
|||
*/ |
|||
|
|||
// This is a translation of the cycle counter implementation in ports/stm32/machine_bitstream.c.
|
|||
|
|||
#include "py/mpconfig.h" |
|||
#include "py/mphal.h" |
|||
|
|||
#if MICROPY_PY_MACHINE_BITSTREAM |
|||
|
|||
#define NS_TICKS_OVERHEAD (6) |
|||
|
|||
void IRAM_ATTR machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) { |
|||
uint32_t pin_mask, gpio_reg_set, gpio_reg_clear; |
|||
#if !CONFIG_IDF_TARGET_ESP32C3 |
|||
if (pin >= 32) { |
|||
pin_mask = 1 << (pin - 32); |
|||
gpio_reg_set = GPIO_OUT1_W1TS_REG; |
|||
gpio_reg_clear = GPIO_OUT1_W1TC_REG; |
|||
} else |
|||
#endif |
|||
{ |
|||
pin_mask = 1 << pin; |
|||
gpio_reg_set = GPIO_OUT_W1TS_REG; |
|||
gpio_reg_clear = GPIO_OUT_W1TC_REG; |
|||
} |
|||
|
|||
// Convert ns to cpu ticks [high_time_0, period_0, high_time_1, period_1].
|
|||
uint32_t fcpu_mhz = ets_get_cpu_frequency(); |
|||
for (size_t i = 0; i < 4; ++i) { |
|||
timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000; |
|||
if (timing_ns[i] > NS_TICKS_OVERHEAD) { |
|||
timing_ns[i] -= NS_TICKS_OVERHEAD; |
|||
} |
|||
if (i % 2 == 1) { |
|||
// Convert low_time to period (i.e. add high_time).
|
|||
timing_ns[i] += timing_ns[i - 1]; |
|||
} |
|||
} |
|||
|
|||
uint32_t irq_state = mp_hal_quiet_timing_enter(); |
|||
|
|||
for (size_t i = 0; i < len; ++i) { |
|||
uint8_t b = buf[i]; |
|||
for (size_t j = 0; j < 8; ++j) { |
|||
GPIO_REG_WRITE(gpio_reg_set, pin_mask); |
|||
uint32_t start_ticks = mp_hal_ticks_cpu(); |
|||
uint32_t *t = &timing_ns[b >> 6 & 2]; |
|||
uint32_t end_ticks = start_ticks + t[0]; |
|||
while (mp_hal_ticks_cpu() - start_ticks < t[0]) { |
|||
; |
|||
} |
|||
GPIO_REG_WRITE(gpio_reg_clear, pin_mask); |
|||
b <<= 1; |
|||
end_ticks += t[1]; |
|||
while (mp_hal_ticks_cpu() - start_ticks < t[1]) { |
|||
; |
|||
} |
|||
} |
|||
} |
|||
|
|||
mp_hal_quiet_timing_exit(irq_state); |
|||
} |
|||
|
|||
#endif // MICROPY_PY_MACHINE_BITSTREAM
|
Loading…
Reference in new issue