ws2812.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "module.h"
  2. #include "lauxlib.h"
  3. #include "lmem.h"
  4. #include "platform.h"
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <string.h>
  8. #include "user_interface.h"
  9. #include "driver/uart.h"
  10. #include "osapi.h"
  11. #include "cpu_esp8266_irq.h"
  12. #include "pixbuf.h"
  13. #define MODE_SINGLE 0
  14. #define MODE_DUAL 1
  15. // Init UART1 to be able to stream WS2812 data to GPIO2 pin
  16. // If DUAL mode is selected, init UART0 to stream to TXD0 as well
  17. // You HAVE to redirect LUA's output somewhere else
  18. static int ws2812_init(lua_State* L) {
  19. const int mode = luaL_optinteger(L, 1, MODE_SINGLE);
  20. luaL_argcheck(L, mode == MODE_SINGLE || mode == MODE_DUAL, 1, "ws2812.SINGLE or ws2812.DUAL expected");
  21. // Configure UART1
  22. // Set baudrate of UART1 to 3200000
  23. WRITE_PERI_REG(UART_CLKDIV(1), UART_CLK_FREQ / 3200000);
  24. // Set UART Configuration No parity / 6 DataBits / 1 StopBits / Invert TX
  25. WRITE_PERI_REG(UART_CONF0(1), UART_TXD_INV | (1 << UART_STOP_BIT_NUM_S) | (1 << UART_BIT_NUM_S));
  26. if (mode == MODE_DUAL) {
  27. // Configure UART0
  28. // Set baudrate of UART0 to 3200000
  29. WRITE_PERI_REG(UART_CLKDIV(0), UART_CLK_FREQ / 3200000);
  30. // Set UART Configuration No parity / 6 DataBits / 1 StopBits / Invert TX
  31. WRITE_PERI_REG(UART_CONF0(0), UART_TXD_INV | (1 << UART_STOP_BIT_NUM_S) | (1 << UART_BIT_NUM_S));
  32. }
  33. // Pull GPIO2 down
  34. platform_gpio_mode(4, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
  35. platform_gpio_write(4, 0);
  36. // Waits 10us to simulate a reset
  37. os_delay_us(10);
  38. // Redirect UART1 to GPIO2
  39. // Disable GPIO2
  40. GPIO_REG_WRITE(GPIO_ENABLE_W1TC_ADDRESS, BIT2);
  41. // Enable Function 2 for GPIO2 (U1TXD)
  42. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
  43. return 0;
  44. }
  45. static bool
  46. ws2812_can_write(int uart)
  47. {
  48. // If something to send for first buffer and enough room
  49. // in FIFO buffer (we wants to write 4 bytes, so less than
  50. // 124 in the buffer)
  51. return (((READ_PERI_REG(UART_STATUS(uart)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT) <= 124);
  52. }
  53. static void
  54. ws2812_write_byte(int uart, uint8_t value)
  55. {
  56. // Data are sent LSB first, with a start bit at 0, an end bit at 1 and all inverted
  57. // 0b00110111 => 110111 => [0]111011[1] => 10001000 => 00
  58. // 0b00000111 => 000111 => [0]111000[1] => 10001110 => 01
  59. // 0b00110100 => 110100 => [0]001011[1] => 11101000 => 10
  60. // 0b00000100 => 000100 => [0]001000[1] => 11101110 => 11
  61. // Array declared as static const to avoid runtime generation
  62. // But declared in ".data" section to avoid read penalty from FLASH
  63. static const __attribute__((section(".data._uartData"))) uint8_t _uartData[4] = { 0b00110111, 0b00000111, 0b00110100, 0b00000100 };
  64. WRITE_PERI_REG(UART_FIFO(uart), _uartData[(value >> 6) & 3]);
  65. WRITE_PERI_REG(UART_FIFO(uart), _uartData[(value >> 4) & 3]);
  66. WRITE_PERI_REG(UART_FIFO(uart), _uartData[(value >> 2) & 3]);
  67. WRITE_PERI_REG(UART_FIFO(uart), _uartData[(value >> 0) & 3]);
  68. }
  69. // Stream data using UART1 routed to GPIO2
  70. // ws2812.init() should be called first
  71. //
  72. // NODE_DEBUG should not be activated because it also uses UART1
  73. void ICACHE_RAM_ATTR ws2812_write_data(const uint8_t *pixels, uint32_t length, const uint8_t *pixels2, uint32_t length2) {
  74. const uint8_t *end = pixels + length;
  75. const uint8_t *end2 = pixels2 + length2;
  76. /* Fill the UART fifos with IRQs disabled */
  77. uint32_t irq_state = esp8266_defer_irqs();
  78. while ((pixels < end) && ws2812_can_write(1)) {
  79. ws2812_write_byte(1, *pixels++);
  80. }
  81. while ((pixels2 < end2) && ws2812_can_write(0)) {
  82. ws2812_write_byte(0, *pixels2++);
  83. }
  84. esp8266_restore_irqs(irq_state);
  85. do {
  86. if (pixels < end && ws2812_can_write(1)) {
  87. ws2812_write_byte(1, *pixels++);
  88. }
  89. // Same for the second buffer
  90. if (pixels2 < end2 && ws2812_can_write(0)) {
  91. ws2812_write_byte(0, *pixels2++);
  92. }
  93. } while(pixels < end || pixels2 < end2); // Until there is still something to send
  94. }
  95. // Lua: ws2812.write("string")
  96. // Byte triples in the string are interpreted as G R B values.
  97. //
  98. // ws2812.init() should be called first
  99. //
  100. // ws2812.write(string.char(0, 255, 0)) sets the first LED red.
  101. // ws2812.write(string.char(0, 0, 255):rep(10)) sets ten LEDs blue.
  102. // ws2812.write(string.char(255, 0, 0, 255, 255, 255)) first LED green, second LED white.
  103. //
  104. // In DUAL mode 'ws2812.init(ws2812.DUAL)', you may pass a second string as parameter
  105. // It will be sent through TXD0 in parallel
  106. static int ws2812_write(lua_State* L) {
  107. size_t length1, length2;
  108. const char *buffer1, *buffer2;
  109. // First mandatory parameter
  110. int type = lua_type(L, 1);
  111. if (type == LUA_TNIL)
  112. {
  113. buffer1 = 0;
  114. length1 = 0;
  115. }
  116. else if(type == LUA_TSTRING)
  117. {
  118. buffer1 = lua_tolstring(L, 1, &length1);
  119. }
  120. #ifdef LUA_USE_MODULES_PIXBUF
  121. else if (type == LUA_TUSERDATA)
  122. {
  123. pixbuf *buffer = pixbuf_from_lua_arg(L, 1);
  124. luaL_argcheck(L, pixbuf_channels(buffer) == 3, 1, "Bad pixbuf format");
  125. buffer1 = buffer->values;
  126. length1 = pixbuf_size(buffer);
  127. }
  128. #endif
  129. else
  130. {
  131. luaL_argerror(L, 1, "pixbuf or string expected");
  132. }
  133. // Second optionnal parameter
  134. type = lua_type(L, 2);
  135. if (type == LUA_TNONE || type == LUA_TNIL)
  136. {
  137. buffer2 = 0;
  138. length2 = 0;
  139. }
  140. else if (type == LUA_TSTRING)
  141. {
  142. buffer2 = lua_tolstring(L, 2, &length2);
  143. }
  144. #ifdef LUA_USE_MODULES_PIXBUF
  145. else if (type == LUA_TUSERDATA)
  146. {
  147. pixbuf *buffer = pixbuf_from_lua_arg(L, 2);
  148. luaL_argcheck(L, pixbuf_channels(buffer) == 3, 2, "Bad pixbuf format");
  149. buffer2 = buffer->values;
  150. length2 = pixbuf_size(buffer);
  151. }
  152. #endif
  153. else
  154. {
  155. luaL_argerror(L, 2, "pixbuf or string expected");
  156. }
  157. // Send the buffers
  158. ws2812_write_data(buffer1, length1, buffer2, length2);
  159. return 0;
  160. }
  161. LROT_BEGIN(ws2812, NULL, 0)
  162. LROT_FUNCENTRY( init, ws2812_init )
  163. #ifdef LUA_USE_MODULES_PIXBUF
  164. LROT_FUNCENTRY( newBuffer, pixbuf_new_lua ) // backwards compatibility
  165. LROT_NUMENTRY( FADE_IN, PIXBUF_FADE_IN ) // BC
  166. LROT_NUMENTRY( FADE_OUT, PIXBUF_FADE_OUT ) // BC
  167. LROT_NUMENTRY( SHIFT_LOGICAL, PIXBUF_SHIFT_LOGICAL ) // BC
  168. LROT_NUMENTRY( SHIFT_CIRCULAR, PIXBUF_SHIFT_CIRCULAR ) // BC
  169. #endif
  170. LROT_FUNCENTRY( write, ws2812_write )
  171. LROT_NUMENTRY( MODE_SINGLE, MODE_SINGLE )
  172. LROT_NUMENTRY( MODE_DUAL, MODE_DUAL )
  173. LROT_END(ws2812, NULL, 0)
  174. static int luaopen_ws2812(lua_State *L) {
  175. // TODO: Make sure that the GPIO system is initialized
  176. return 0;
  177. }
  178. NODEMCU_MODULE(WS2812, "ws2812", ws2812, luaopen_ws2812);