Skip to content

Instantly share code, notes, and snippets.

@Jeija
Created January 24, 2016 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeija/d4372cc3bd1c2be5883e to your computer and use it in GitHub Desktop.
Save Jeija/d4372cc3bd1c2be5883e to your computer and use it in GitHub Desktop.
ESP8266 implementation of WS2811 LED Strip protocol (without I²S / DMA)
// System
#include <ets_sys.h>
#include <osapi.h>
#include <gpio.h>
#include <os_type.h>
#include <user_interface.h>
// Project
#include "ws2811.h"
#define CPU_FREQ 26000000
#define CYCLES_PER_US CPU_FREQ/(2*1000000)
void __attribute__((optimize("O2"))) ws2811_putbit(bool bit) {
uint8_t i;
if (bit) {
i = 0.6 * CYCLES_PER_US;
while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, BIT(WSGPIO));
i = 0.65 * CYCLES_PER_US;
while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, BIT(WSGPIO));
} else {
i = 0.25 * CYCLES_PER_US;
while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, BIT(WSGPIO));
i = 1.0 * CYCLES_PER_US;
while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, BIT(WSGPIO));
}
}
void ICACHE_FLASH_ATTR ws2811_put(uint8_t pxbuf[][3], uint16_t pixels)
{
PIN_PULLUP_DIS(WSGPIO_MUX);
PIN_FUNC_SELECT(WSGPIO_MUX, WSGPIO_FUNC);
CLEAR_PERI_REG_MASK(WSGPIO_MUX, BIT6);
GPIO_OUTPUT_SET(GPIO_ID_PIN(WSGPIO), 0);
os_delay_us(1);
uint8_t px;
ets_intr_lock();
for (px = 0; px < pixels * 3; ++px) {
ws2811_putbit(pxbuf[px / 3][px % 3] & (1<<7));
ws2811_putbit(pxbuf[px / 3][px % 3] & (1<<6));
ws2811_putbit(pxbuf[px / 3][px % 3] & (1<<5));
ws2811_putbit(pxbuf[px / 3][px % 3] & (1<<4));
ws2811_putbit(pxbuf[px / 3][px % 3] & (1<<3));
ws2811_putbit(pxbuf[px / 3][px % 3] & (1<<2));
ws2811_putbit(pxbuf[px / 3][px % 3] & (1<<1));
ws2811_putbit(pxbuf[px / 3][px % 3] & (1<<0));
}
ets_intr_unlock();
}
/**
* Driver for both WS2811 (in fast mode, as used in plastic led pixels)
* and WS2812B (as used in LED Strips).
*/
#ifndef _WS2811_H
#define _WS2811_H
#define WSGPIO 14
#define WSGPIO_MUX PERIPHS_IO_MUX_MTMS_U
#define WSGPIO_FUNC FUNC_GPIO14
void ws2811_put(uint8_t buffer[][3], uint16_t pixels);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment