2019-07-13 02:37:50 +03:00
|
|
|
// Support for bit-banging commands to WS2812 type "neopixel" LEDs
|
|
|
|
//
|
|
|
|
// Copyright (C) 2019 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
//
|
|
|
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
2020-09-28 20:44:36 +03:00
|
|
|
#include <string.h> // memcpy
|
2019-07-13 02:37:50 +03:00
|
|
|
#include "autoconf.h" // CONFIG_MACH_AVR
|
|
|
|
#include "board/gpio.h" // gpio_out_write
|
|
|
|
#include "board/irq.h" // irq_poll
|
|
|
|
#include "board/misc.h" // timer_read_time
|
|
|
|
#include "basecmd.h" // oid_alloc
|
|
|
|
#include "command.h" // DECL_COMMAND
|
2020-09-28 20:44:36 +03:00
|
|
|
#include "sched.h" // sched_shutdown
|
2019-07-13 02:37:50 +03:00
|
|
|
|
2019-08-07 22:34:08 +03:00
|
|
|
// The WS2812 uses a bit-banging protocol where each bit is
|
|
|
|
// transmitted as a gpio high pulse of variable length. The various
|
|
|
|
// specs are unclear, but it is believed the timing requirements are:
|
|
|
|
// - A zero bit must have a high pulse less than 500ns.
|
|
|
|
// - A one bit must have a high pulse longer than 650ns.
|
|
|
|
// - The total bit time (gpio high to following gpio high) must not
|
|
|
|
// exceed ~5000ns. The average bit time must be at least 1250ns.
|
|
|
|
// - The specs generally indicate a minimum high pulse and low pulse
|
|
|
|
// of 200ns, but the actual requirement might be smaller.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* Timing
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
typedef unsigned int neopixel_time_t;
|
|
|
|
|
|
|
|
static neopixel_time_t
|
|
|
|
nsecs_to_ticks(uint32_t ns)
|
|
|
|
{
|
|
|
|
return timer_from_us(ns * 1000) / 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2020-04-15 19:11:57 +03:00
|
|
|
neopixel_check_elapsed(neopixel_time_t t1, neopixel_time_t t2
|
|
|
|
, neopixel_time_t ticks)
|
2019-08-07 22:34:08 +03:00
|
|
|
{
|
2020-04-15 19:11:57 +03:00
|
|
|
return t2 - t1 >= ticks;
|
2019-08-07 22:34:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The AVR micro-controllers require specialized timing
|
|
|
|
#if CONFIG_MACH_AVR
|
|
|
|
|
|
|
|
#include <avr/interrupt.h> // TCNT1
|
|
|
|
|
|
|
|
static neopixel_time_t
|
|
|
|
neopixel_get_time(void)
|
|
|
|
{
|
|
|
|
return TCNT1;
|
|
|
|
}
|
|
|
|
|
2020-04-21 20:03:11 +03:00
|
|
|
#define neopixel_delay(start, ticks) (void)(ticks)
|
2019-08-07 22:34:08 +03:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static neopixel_time_t
|
|
|
|
neopixel_get_time(void)
|
|
|
|
{
|
|
|
|
return timer_read_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2020-04-15 19:11:57 +03:00
|
|
|
neopixel_delay(neopixel_time_t start, neopixel_time_t ticks)
|
2019-08-07 22:34:08 +03:00
|
|
|
{
|
2020-04-15 19:11:57 +03:00
|
|
|
while (!neopixel_check_elapsed(start, neopixel_get_time(), ticks))
|
2019-08-07 22:34:08 +03:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-04-15 19:11:57 +03:00
|
|
|
#define PULSE_LONG_TICKS nsecs_to_ticks(650)
|
|
|
|
#define PULSE_SHORT_TICKS nsecs_to_ticks(200)
|
|
|
|
#define BIT_MIN_TICKS nsecs_to_ticks(1250)
|
|
|
|
|
2019-08-07 22:34:08 +03:00
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* Neopixel interface
|
|
|
|
****************************************************************/
|
|
|
|
|
2019-07-13 02:37:50 +03:00
|
|
|
struct neopixel_s {
|
|
|
|
struct gpio_out pin;
|
2020-04-15 20:28:54 +03:00
|
|
|
neopixel_time_t bit_max_ticks;
|
|
|
|
uint32_t last_req_time, reset_min_ticks;
|
2020-09-28 20:44:36 +03:00
|
|
|
uint16_t data_size;
|
|
|
|
uint8_t data[0];
|
2019-07-13 02:37:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
command_config_neopixel(uint32_t *args)
|
|
|
|
{
|
|
|
|
struct gpio_out pin = gpio_out_setup(args[1], 0);
|
2020-09-28 20:44:36 +03:00
|
|
|
uint16_t data_size = args[2];
|
|
|
|
if (data_size & 0x8000)
|
|
|
|
shutdown("Invalid neopixel data_size");
|
2019-07-13 02:37:50 +03:00
|
|
|
struct neopixel_s *n = oid_alloc(args[0], command_config_neopixel
|
2020-09-28 20:44:36 +03:00
|
|
|
, sizeof(*n) + data_size);
|
2019-07-13 02:37:50 +03:00
|
|
|
n->pin = pin;
|
2020-09-28 20:44:36 +03:00
|
|
|
n->data_size = data_size;
|
|
|
|
n->bit_max_ticks = args[3];
|
|
|
|
n->reset_min_ticks = args[4];
|
2019-07-13 02:37:50 +03:00
|
|
|
}
|
2020-04-15 20:28:54 +03:00
|
|
|
DECL_COMMAND(command_config_neopixel, "config_neopixel oid=%c pin=%u"
|
2020-09-28 20:44:36 +03:00
|
|
|
" data_size=%hu bit_max_ticks=%u reset_min_ticks=%u");
|
2019-07-19 20:53:34 +03:00
|
|
|
|
2019-07-13 02:37:50 +03:00
|
|
|
static int
|
2020-09-28 20:44:36 +03:00
|
|
|
send_data(struct neopixel_s *n)
|
2019-07-13 02:37:50 +03:00
|
|
|
{
|
2020-04-15 20:28:54 +03:00
|
|
|
// Make sure the reset time has elapsed since last request
|
|
|
|
uint32_t last_req_time = n->last_req_time, rmt = n->reset_min_ticks;
|
|
|
|
uint32_t cur = timer_read_time();
|
|
|
|
while (cur - last_req_time < rmt) {
|
2019-07-13 02:37:50 +03:00
|
|
|
irq_poll();
|
|
|
|
cur = timer_read_time();
|
|
|
|
}
|
|
|
|
|
2020-04-15 20:28:54 +03:00
|
|
|
// Transmit data
|
2020-09-28 20:44:36 +03:00
|
|
|
uint8_t *data = n->data;
|
|
|
|
uint_fast16_t data_len = n->data_size;
|
2019-07-13 02:37:50 +03:00
|
|
|
struct gpio_out pin = n->pin;
|
2019-08-07 22:34:08 +03:00
|
|
|
neopixel_time_t last_start = neopixel_get_time();
|
2020-04-15 20:28:54 +03:00
|
|
|
neopixel_time_t bit_max_ticks = n->bit_max_ticks;
|
2019-07-17 23:36:28 +03:00
|
|
|
while (data_len--) {
|
|
|
|
uint_fast8_t byte = *data++;
|
|
|
|
uint_fast8_t bits = 8;
|
|
|
|
while (bits--) {
|
2019-07-19 20:53:34 +03:00
|
|
|
if (byte & 0x80) {
|
2019-08-07 22:34:08 +03:00
|
|
|
// Long pulse
|
2020-04-15 19:11:57 +03:00
|
|
|
neopixel_delay(last_start, BIT_MIN_TICKS);
|
2019-08-07 22:34:08 +03:00
|
|
|
irq_disable();
|
|
|
|
neopixel_time_t start = neopixel_get_time();
|
|
|
|
gpio_out_toggle_noirq(pin);
|
|
|
|
irq_enable();
|
|
|
|
|
2020-04-15 20:28:54 +03:00
|
|
|
if (neopixel_check_elapsed(last_start, start, bit_max_ticks))
|
2019-08-07 22:34:08 +03:00
|
|
|
goto fail;
|
|
|
|
last_start = start;
|
|
|
|
byte <<= 1;
|
|
|
|
|
2020-04-15 19:11:57 +03:00
|
|
|
neopixel_delay(start, PULSE_LONG_TICKS);
|
2019-08-07 22:34:08 +03:00
|
|
|
irq_disable();
|
|
|
|
gpio_out_toggle_noirq(pin);
|
|
|
|
irq_enable();
|
2020-04-21 20:03:11 +03:00
|
|
|
|
|
|
|
neopixel_delay(neopixel_get_time(), PULSE_SHORT_TICKS);
|
2019-07-19 20:53:34 +03:00
|
|
|
} else {
|
2019-08-07 22:34:08 +03:00
|
|
|
// Short pulse
|
2020-04-15 19:11:57 +03:00
|
|
|
neopixel_delay(last_start, BIT_MIN_TICKS);
|
2019-08-07 22:34:08 +03:00
|
|
|
irq_disable();
|
|
|
|
neopixel_time_t start = neopixel_get_time();
|
|
|
|
gpio_out_toggle_noirq(pin);
|
2020-04-15 19:11:57 +03:00
|
|
|
neopixel_delay(start, PULSE_SHORT_TICKS);
|
2019-08-07 22:34:08 +03:00
|
|
|
gpio_out_toggle_noirq(pin);
|
|
|
|
irq_enable();
|
|
|
|
|
2020-04-15 20:28:54 +03:00
|
|
|
if (neopixel_check_elapsed(last_start, start, bit_max_ticks))
|
2019-07-19 20:53:34 +03:00
|
|
|
goto fail;
|
2019-08-07 22:34:08 +03:00
|
|
|
last_start = start;
|
|
|
|
byte <<= 1;
|
2019-07-19 20:53:34 +03:00
|
|
|
}
|
2019-07-13 02:37:50 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 22:34:08 +03:00
|
|
|
n->last_req_time = timer_read_time();
|
2019-07-13 02:37:50 +03:00
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
// A hardware irq messed up the transmission - report a failure
|
2019-08-07 22:34:08 +03:00
|
|
|
gpio_out_write(pin, 0);
|
|
|
|
n->last_req_time = timer_read_time();
|
2019-07-13 02:37:50 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-09-28 20:44:36 +03:00
|
|
|
void
|
|
|
|
command_neopixel_update(uint32_t *args)
|
|
|
|
{
|
|
|
|
uint8_t oid = args[0];
|
|
|
|
struct neopixel_s *n = oid_lookup(oid, command_config_neopixel);
|
|
|
|
uint_fast16_t pos = args[1];
|
|
|
|
uint_fast8_t data_len = args[2];
|
2020-10-30 06:54:17 +03:00
|
|
|
uint8_t *data = command_decode_ptr(args[3]);
|
2020-09-28 20:44:36 +03:00
|
|
|
if (pos & 0x8000 || pos + data_len > n->data_size)
|
|
|
|
shutdown("Invalid neopixel update command");
|
|
|
|
memcpy(&n->data[pos], data, data_len);
|
|
|
|
}
|
|
|
|
DECL_COMMAND(command_neopixel_update,
|
|
|
|
"neopixel_update oid=%c pos=%hu data=%*s");
|
|
|
|
|
2019-07-13 02:37:50 +03:00
|
|
|
void
|
|
|
|
command_neopixel_send(uint32_t *args)
|
|
|
|
{
|
|
|
|
uint8_t oid = args[0];
|
|
|
|
struct neopixel_s *n = oid_lookup(oid, command_config_neopixel);
|
2020-09-28 20:44:36 +03:00
|
|
|
int ret = send_data(n);
|
2020-11-05 19:40:34 +03:00
|
|
|
sendf("neopixel_result oid=%c success=%c", oid, ret ? 0 : 1);
|
2019-07-13 02:37:50 +03:00
|
|
|
}
|
2020-09-28 20:44:36 +03:00
|
|
|
DECL_COMMAND(command_neopixel_send, "neopixel_send oid=%c");
|