|
3 | 3 | * |
4 | 4 | * The MIT License (MIT) |
5 | 5 | * |
6 | | - * Copyright (c) 2018 hathach for Adafruit Industries |
| 6 | + * Copyright (c) 2020 Lucian Copeland for Adafruit Industries |
7 | 7 | * |
8 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | 9 | * of this software and associated documentation files (the "Software"), to deal |
|
24 | 24 | * THE SOFTWARE. |
25 | 25 | */ |
26 | 26 |
|
| 27 | +/* Uses code from Espressif RGB LED Strip demo and drivers |
| 28 | + * Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD |
| 29 | + * |
| 30 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 31 | + * you may not use this file except in compliance with the License. |
| 32 | + * You may obtain a copy of the License at |
| 33 | + * |
| 34 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 35 | + * |
| 36 | + * Unless required by applicable law or agreed to in writing, software |
| 37 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 38 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 39 | + * See the License for the specific language governing permissions and |
| 40 | + * limitations under the License. |
| 41 | + */ |
| 42 | + |
27 | 43 | #include "py/mphal.h" |
28 | 44 | #include "shared-bindings/neopixel_write/__init__.h" |
| 45 | +#include "driver/rmt.h" |
| 46 | +#include "rmt.h" |
| 47 | + |
| 48 | +#define WS2812_T0H_NS (350) |
| 49 | +#define WS2812_T0L_NS (1000) |
| 50 | +#define WS2812_T1H_NS (1000) |
| 51 | +#define WS2812_T1L_NS (350) |
| 52 | +#define WS2812_RESET_US (280) |
| 53 | + |
| 54 | +static uint32_t ws2812_t0h_ticks = 0; |
| 55 | +static uint32_t ws2812_t1h_ticks = 0; |
| 56 | +static uint32_t ws2812_t0l_ticks = 0; |
| 57 | +static uint32_t ws2812_t1l_ticks = 0; |
| 58 | + |
| 59 | +static void IRAM_ATTR ws2812_rmt_adapter(const void *src, rmt_item32_t *dest, size_t src_size, |
| 60 | + size_t wanted_num, size_t *translated_size, size_t *item_num) |
| 61 | +{ |
| 62 | + if (src == NULL || dest == NULL) { |
| 63 | + *translated_size = 0; |
| 64 | + *item_num = 0; |
| 65 | + return; |
| 66 | + } |
| 67 | + const rmt_item32_t bit0 = {{{ ws2812_t0h_ticks, 1, ws2812_t0l_ticks, 0 }}}; //Logical 0 |
| 68 | + const rmt_item32_t bit1 = {{{ ws2812_t1h_ticks, 1, ws2812_t1l_ticks, 0 }}}; //Logical 1 |
| 69 | + size_t size = 0; |
| 70 | + size_t num = 0; |
| 71 | + uint8_t *psrc = (uint8_t *)src; |
| 72 | + rmt_item32_t *pdest = dest; |
| 73 | + while (size < src_size && num < wanted_num) { |
| 74 | + for (int i = 0; i < 8; i++) { |
| 75 | + // MSB first |
| 76 | + if (*psrc & (1 << (7 - i))) { |
| 77 | + pdest->val = bit1.val; |
| 78 | + } else { |
| 79 | + pdest->val = bit0.val; |
| 80 | + } |
| 81 | + num++; |
| 82 | + pdest++; |
| 83 | + } |
| 84 | + size++; |
| 85 | + psrc++; |
| 86 | + } |
| 87 | + *translated_size = size; |
| 88 | + *item_num = num; |
| 89 | +} |
29 | 90 |
|
30 | 91 | void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) { |
31 | | - (void)digitalinout; |
32 | | - (void)numBytes; |
| 92 | + // Reserve channel |
| 93 | + uint8_t number = digitalinout->pin->number; |
| 94 | + rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt(); |
| 95 | + |
| 96 | + // Configure Channel |
| 97 | + rmt_config_t config = RMT_DEFAULT_CONFIG_TX(number, channel); |
| 98 | + config.clk_div = 2; // set counter clock to 40MHz |
| 99 | + rmt_config(&config); |
| 100 | + rmt_driver_install(config.channel, 0, 0); |
| 101 | + |
| 102 | + // Convert NS timings to ticks |
| 103 | + uint32_t counter_clk_hz = 0; |
| 104 | + if (rmt_get_counter_clock(config.channel, &counter_clk_hz) != ESP_OK) { |
| 105 | + mp_raise_ValueError(translate("Could not retrieve clock")); |
| 106 | + } |
| 107 | + float ratio = (float)counter_clk_hz / 1e9; |
| 108 | + ws2812_t0h_ticks = (uint32_t)(ratio * WS2812_T0H_NS); |
| 109 | + ws2812_t0l_ticks = (uint32_t)(ratio * WS2812_T0L_NS); |
| 110 | + ws2812_t1h_ticks = (uint32_t)(ratio * WS2812_T1H_NS); |
| 111 | + ws2812_t1l_ticks = (uint32_t)(ratio * WS2812_T1L_NS); |
| 112 | + |
| 113 | + // Initialize automatic timing translator |
| 114 | + rmt_translator_init(config.channel, ws2812_rmt_adapter); |
| 115 | + |
| 116 | + // Write and wait to finish |
| 117 | + if(rmt_write_sample(config.channel, pixels, (size_t)numBytes, true) != ESP_OK) { |
| 118 | + mp_raise_ValueError(translate("Input/output error")); |
| 119 | + } |
| 120 | + rmt_wait_tx_done(config.channel, pdMS_TO_TICKS(100)); |
| 121 | + |
| 122 | + // Free channel again |
| 123 | + esp32s2_peripherals_free_rmt(config.channel); |
33 | 124 | } |
0 commit comments