|
| 1 | +//This driver uses the ESP32 RMT peripheral to drive "Neopixel" compatible LEDs |
| 2 | +//The usage of the RMT peripheral has been implemented using work by JSchaenzie: |
| 3 | +//you can find his work at https://github.com/JSchaenzle/ESP32-NeoPixel-WS2812-RMT |
| 4 | + |
| 5 | +#include <sdkconfig.h> |
| 6 | + |
| 7 | +#include <stdbool.h> |
| 8 | +#include <stdint.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +#include <freertos/FreeRTOS.h> |
| 13 | +#include <freertos/task.h> |
| 14 | +#include <esp_err.h> |
| 15 | +#include <esp_log.h> |
| 16 | + |
| 17 | +#include <driver/gpio.h> |
| 18 | +#include <driver/rmt.h> |
| 19 | + |
| 20 | +static const char *TAG = "ws2812"; |
| 21 | + |
| 22 | +#define WS2812_RMT_CHANNEL RMT_CHANNEL_0 |
| 23 | + |
| 24 | +#define T0H 14 // 0 bit high time |
| 25 | +#define T1H 52 // 1 bit high time |
| 26 | +#define TL 52 // low time for either bit |
| 27 | + |
| 28 | +static bool gActive = false; |
| 29 | +rmt_item32_t* gBuffer = NULL; |
| 30 | +int gBufferLength = 0; |
| 31 | +gpio_num_t gPin; |
| 32 | + |
| 33 | +esp_err_t ws2812_init(gpio_num_t aGpioPin) { |
| 34 | + if (gActive) return ESP_OK; |
| 35 | + gpio_config_t io_conf = { |
| 36 | + .intr_type = GPIO_INTR_DISABLE, |
| 37 | + .mode = GPIO_MODE_OUTPUT, |
| 38 | + .pin_bit_mask = 1LL << aGpioPin, |
| 39 | + .pull_down_en = 0, |
| 40 | + .pull_up_en = 0, |
| 41 | + }; |
| 42 | + esp_err_t res = gpio_config(&io_conf); |
| 43 | + if (res != ESP_OK) return res; |
| 44 | + rmt_config_t config; |
| 45 | + config.rmt_mode = RMT_MODE_TX; |
| 46 | + config.channel = WS2812_RMT_CHANNEL; |
| 47 | + config.gpio_num = aGpioPin; |
| 48 | + config.mem_block_num = 3; |
| 49 | + config.tx_config.loop_en = false; |
| 50 | + config.tx_config.carrier_en = false; |
| 51 | + config.tx_config.idle_output_en = true; |
| 52 | + config.tx_config.idle_level = 0; |
| 53 | + config.clk_div = 2; |
| 54 | + res = rmt_config(&config); |
| 55 | + if (res != ESP_OK) return res; |
| 56 | + res = rmt_driver_install(config.channel, 0, 0); |
| 57 | + if (res != ESP_OK) return res; |
| 58 | + gActive = true; |
| 59 | + gPin = aGpioPin; |
| 60 | + return ESP_OK; |
| 61 | +} |
| 62 | + |
| 63 | +esp_err_t ws2812_deinit(void) { |
| 64 | + if (!gActive) return ESP_OK; |
| 65 | + esp_err_t res = rmt_driver_uninstall(WS2812_RMT_CHANNEL); |
| 66 | + if (res != ESP_OK) return res; |
| 67 | + gpio_config_t io_conf = { |
| 68 | + .intr_type = GPIO_INTR_DISABLE, |
| 69 | + .mode = GPIO_MODE_INPUT, |
| 70 | + .pin_bit_mask = 1LL << gPin, |
| 71 | + .pull_down_en = 0, |
| 72 | + .pull_up_en = 0, |
| 73 | + }; |
| 74 | + res = gpio_config(&io_conf); |
| 75 | + if (res != ESP_OK) return res; |
| 76 | + gActive = false; |
| 77 | + return ESP_OK; |
| 78 | +} |
| 79 | + |
| 80 | +esp_err_t ws2812_prepare_data(uint8_t *data, int len) |
| 81 | +{ |
| 82 | + if (gBuffer != NULL) return ESP_FAIL; |
| 83 | + gBuffer = calloc(len * 8, sizeof(rmt_item32_t)); |
| 84 | + if (gBuffer == NULL) return ESP_FAIL; |
| 85 | + gBufferLength = len * 8; |
| 86 | + for (uint32_t pos = 0; pos < len; pos++) { |
| 87 | + uint32_t mask = 1 << 7; |
| 88 | + for (uint8_t i = 0; i < 8; i++) { |
| 89 | + bool bit = data[pos] & mask; |
| 90 | + gBuffer[pos*8 + i] = bit ? |
| 91 | + (rmt_item32_t){{{T1H, 1, TL, 0}}} : |
| 92 | + (rmt_item32_t){{{T0H, 1, TL, 0}}}; |
| 93 | + mask >>= 1; |
| 94 | + } |
| 95 | + } |
| 96 | + return ESP_OK; |
| 97 | +} |
| 98 | + |
| 99 | +esp_err_t ws2812_free_data() { |
| 100 | + if (!gBuffer) return ESP_FAIL; |
| 101 | + free(gBuffer); |
| 102 | + gBuffer = NULL; |
| 103 | + gBufferLength = 0; |
| 104 | + return ESP_OK; |
| 105 | +} |
| 106 | + |
| 107 | +esp_err_t ws2812_send_data(uint8_t *data, int len) |
| 108 | +{ |
| 109 | + if (!gActive) return ESP_FAIL; |
| 110 | + esp_err_t res = ws2812_prepare_data(data, len); |
| 111 | + if (res != ESP_OK) return res; |
| 112 | + res = rmt_write_items(WS2812_RMT_CHANNEL, gBuffer, gBufferLength, false); |
| 113 | + if (res != ESP_OK) { |
| 114 | + ws2812_free_data(); |
| 115 | + return res; |
| 116 | + } |
| 117 | + res = rmt_wait_tx_done(WS2812_RMT_CHANNEL, portMAX_DELAY); |
| 118 | + if (res != ESP_OK) { |
| 119 | + ws2812_free_data(); |
| 120 | + return res; |
| 121 | + } |
| 122 | + res = ws2812_free_data(); |
| 123 | + return res; |
| 124 | +} |
0 commit comments