-
-
Notifications
You must be signed in to change notification settings - Fork 284
HD108 method using PARLIO peripheral on ESP32-C6 #907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tommag
wants to merge
3
commits into
Makuna:master
Choose a base branch
from
tommag:hd108-parlio
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| /*------------------------------------------------------------------------- | ||
| NeoPixel library helper functions for HD108 using PARLIO peripheral on compatible ESP32 chips | ||
|
|
||
| Written by Tom Magnier, adapted from DotStarEsp32DmaSpiMethod | ||
|
|
||
| I invest time and resources providing this open source code, | ||
| please support me by donating (see https://github.com/Makuna/NeoPixelBus) | ||
|
|
||
| ------------------------------------------------------------------------- | ||
| This file is part of the Makuna/NeoPixelBus library. | ||
|
|
||
| NeoPixelBus is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU Lesser General Public License as | ||
| published by the Free Software Foundation, either version 3 of | ||
| the License, or (at your option) any later version. | ||
|
|
||
| NeoPixelBus is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU Lesser General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Lesser General Public | ||
| License along with NeoPixel. If not, see | ||
| <http://www.gnu.org/licenses/>. | ||
| -------------------------------------------------------------------------*/ | ||
|
|
||
| // Available on ESP32-C5 / C6 / H2 / H4 / P4 | ||
| #if defined(ARDUINO_ARCH_ESP32) && defined(SOC_PARLIO_SUPPORTED) | ||
|
|
||
| #pragma once | ||
|
|
||
| /* General Reference documentation for the APIs used in this implementation | ||
| LOW LEVEL: (what is actually used) | ||
| DOCS: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c6/api-reference/peripherals/parlio/parlio_tx.html | ||
| */ | ||
|
|
||
| extern "C" | ||
| { | ||
| #include <driver/parlio_tx.h> | ||
| } | ||
|
|
||
| template<typename T_SPEED> class Hd108Esp32ParlioMethodBase | ||
| { | ||
| public: | ||
| typedef typename T_SPEED::SettingsObject SettingsObject; | ||
|
|
||
| Hd108Esp32ParlioMethodBase(uint8_t clockPin, uint8_t dataPin, uint16_t pixelCount, size_t elementSize, size_t settingsSize) : | ||
| _clockPin((gpio_num_t)clockPin), | ||
| _dataPin((gpio_num_t)dataPin), | ||
| _sizeStartFrame(16), // 128 bits (16 bytes) | ||
| _sizePixelData(pixelCount * elementSize + settingsSize), | ||
| _sizeEndFrame((pixelCount + 7) / 8 < 8 ? 8 : (pixelCount + 7) / 8) // one clock per bit, no less than 8 bytes | ||
| { | ||
|
|
||
| _bufferSize = _sizeStartFrame + _sizePixelData + _sizeEndFrame; | ||
|
|
||
| // must have a 4 byte aligned buffer for DMA | ||
| uint32_t alignment = _bufferSize % 4; | ||
| if (alignment) | ||
| { | ||
| _bufferSize += 4 - alignment; | ||
| } | ||
|
|
||
| _data = static_cast<uint8_t*>(malloc(_bufferSize)); | ||
| _dmadata = static_cast<uint8_t*>(heap_caps_malloc(_bufferSize, MALLOC_CAP_DMA)); | ||
|
|
||
| // data cleared later in Begin() | ||
| } | ||
|
|
||
| ~Hd108Esp32ParlioMethodBase() | ||
| { | ||
| if (_parlio_tx_handle) | ||
| { | ||
| deinitParlio(); | ||
| } | ||
| free(_data); | ||
| heap_caps_free(_dmadata); | ||
| } | ||
|
|
||
| bool IsReadyToUpdate() const | ||
| { | ||
| esp_err_t ret = parlio_tx_unit_wait_all_done(_parlio_tx_handle, 0); | ||
|
|
||
| return (ret == ESP_OK); | ||
| } | ||
|
|
||
| void Initialize() | ||
| { | ||
| memset(_data, 0x00, _sizeStartFrame); //Init start frame | ||
| memset(_data + _sizeStartFrame + _sizePixelData, 0x00, _bufferSize - (_sizeStartFrame + _sizePixelData)); //Init end frame | ||
|
|
||
| initParlio(); | ||
| } | ||
|
|
||
| void Update(bool) | ||
| { | ||
| while(!IsReadyToUpdate()) | ||
| portYIELD(); | ||
|
|
||
| memcpy(_dmadata, _data, _bufferSize); | ||
|
|
||
| parlio_transmit_config_t transmit_config = { | ||
| .idle_value = 0x00, // All data lines are low in idle state | ||
| }; | ||
| ESP_ERROR_CHECK(parlio_tx_unit_transmit(_parlio_tx_handle, _dmadata, _bufferSize * 8, &transmit_config)); | ||
| } | ||
|
|
||
| bool AlwaysUpdate() | ||
| { | ||
| // this method requires update to be called only if changes to buffer | ||
| return false; | ||
| } | ||
|
|
||
| bool SwapBuffers() | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| uint8_t* getData() const | ||
| { | ||
| return _data + _sizeStartFrame; | ||
| }; | ||
|
|
||
| size_t getDataSize() const | ||
| { | ||
| return _sizePixelData; | ||
| }; | ||
|
|
||
| void applySettings([[maybe_unused]] const SettingsObject& settings) | ||
| { | ||
| _speed.applySettings(settings); | ||
| if (_parlio_tx_handle) | ||
| { | ||
| deinitParlio(); | ||
| initParlio(); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| void initParlio() | ||
| { | ||
| parlio_tx_unit_config_t config = { | ||
| .clk_src = PARLIO_CLK_SRC_DEFAULT, // Select the default clock source | ||
| .clk_in_gpio_num = (gpio_num_t)-1, // Don't use external clock source | ||
| .output_clk_freq_hz = _speed.Clock, // Output clock frequency | ||
| .data_width = 1, // Data width is 1 bit (single data pin) | ||
| .data_gpio_nums = { | ||
| _dataPin | ||
| }, | ||
| .clk_out_gpio_num = _clockPin, // Clock pin | ||
| .valid_gpio_num = (gpio_num_t)-1, // Don't use valid signal | ||
| .trans_queue_depth = 8, // Transaction queue depth (max count of pending transactions) | ||
| .max_transfer_size = _bufferSize, // Maximum transfer size (number of bytes per transaction) | ||
| .sample_edge = PARLIO_SAMPLE_EDGE_POS, // Sample data on the rising edge of the clock | ||
| .bit_pack_order = PARLIO_BIT_PACK_ORDER_MSB, //Data endianness : MSB first | ||
| .flags = { | ||
| //.clk_gate_en = 1, // NOT SUPPORTED ON C6 - Disable clock when not transmitting | ||
| }, | ||
| }; | ||
| ESP_LOGD("PARLIO", "Initializing PARLIO with clock freq %d Hz, source %d", config.output_clk_freq_hz, config.clk_src); | ||
|
|
||
| // Create TX unit instance | ||
| ESP_ERROR_CHECK(parlio_new_tx_unit(&config, &_parlio_tx_handle)); | ||
| // Enable TX unit | ||
| ESP_ERROR_CHECK(parlio_tx_unit_enable(_parlio_tx_handle)); | ||
| } | ||
|
|
||
| void deinitParlio() | ||
| { | ||
| while (!IsReadyToUpdate()) | ||
| portYIELD(); | ||
|
|
||
| ESP_ERROR_CHECK(parlio_tx_unit_disable(_parlio_tx_handle)); | ||
| ESP_ERROR_CHECK(parlio_del_tx_unit(_parlio_tx_handle)); | ||
| _parlio_tx_handle = NULL; | ||
| } | ||
|
|
||
| const size_t _sizeStartFrame; | ||
| const size_t _sizePixelData; // Size of '_data' buffer below, minus (_sizeStartFrame + _sizeEndFrame) | ||
| const size_t _sizeEndFrame; | ||
|
|
||
| size_t _bufferSize; | ||
| uint8_t* _data; // Holds start/end frames and LED color values | ||
| uint8_t* _dmadata; // Holds start/end frames and LED color values | ||
|
|
||
| parlio_tx_unit_handle_t _parlio_tx_handle = NULL; | ||
| T_SPEED _speed; | ||
| gpio_num_t _clockPin; | ||
| gpio_num_t _dataPin; | ||
| }; | ||
|
|
||
|
|
||
| typedef Hd108Esp32ParlioMethodBase<SpiSpeed40Mhz> Hd108Esp32Parlio40MhzMethod; | ||
| typedef Hd108Esp32ParlioMethodBase<SpiSpeed20Mhz> Hd108Esp32Parlio20MhzMethod; | ||
| typedef Hd108Esp32ParlioMethodBase<SpiSpeed10Mhz> Hd108Esp32Parlio10MhzMethod; | ||
| typedef Hd108Esp32ParlioMethodBase<SpiSpeed5Mhz> Hd108Esp32Parlio5MhzMethod; | ||
| typedef Hd108Esp32ParlioMethodBase<SpiSpeed2Mhz> Hd108Esp32Parlio2MhzMethod; | ||
| typedef Hd108Esp32ParlioMethodBase<SpiSpeed1Mhz> Hd108Esp32Parlio1MhzMethod; | ||
| typedef Hd108Esp32ParlioMethodBase<SpiSpeed500Khz> Hd108Esp32Parlio500KhzMethod; | ||
| typedef Hd108Esp32ParlioMethodBase<SpiSpeedHz> Hd108Esp32ParlioHzMethod; | ||
|
|
||
| typedef Hd108Esp32Parlio10MhzMethod Hd108Esp32ParlioMethod; | ||
|
|
||
| #endif // defined(ARDUINO_ARCH_ESP32) && defined(SOC_PARLIO_SUPPORTED) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ template<typename T_TWOWIRE> class Hd108MethodBase | |
|
|
||
| Hd108MethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize) : | ||
| _sizeData(pixelCount * elementSize + settingsSize), | ||
| _sizeEndFrame((pixelCount + 7) / 8), // ceiling of pixel count divided by 8 (one clock per bit) | ||
| _wire(pinClock, pinData) | ||
| { | ||
| _data = static_cast<uint8_t*>(malloc(_sizeData)); | ||
|
|
@@ -77,20 +78,19 @@ template<typename T_TWOWIRE> class Hd108MethodBase | |
| } | ||
|
|
||
| void Update(bool) | ||
| { | ||
| const uint8_t startFrame[16] = { 0x00 }; | ||
| const uint8_t endFrame[4] = { 0xff }; | ||
|
|
||
| { | ||
| _wire.beginTransaction(); | ||
|
|
||
| // start frame | ||
| _wire.transmitBytes(startFrame, sizeof(startFrame)); | ||
| //start frame : 128bits (16bytes) with DIN = 0 | ||
| for (int i = 0; i < 16; i++) | ||
| _wire.transmitByte(0x00); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. code style: |
||
|
|
||
| // data | ||
| _wire.transmitBytes(_data, _sizeData); | ||
|
|
||
| // end frame | ||
| _wire.transmitBytes(endFrame, sizeof(endFrame)); | ||
| //end frame : at least one bit per LED ; no less than 8 bytes | ||
| for (size_t i = 0; i < max(_sizeEndFrame,(size_t)8); i++) | ||
| _wire.transmitByte(0x00); | ||
|
|
||
| _wire.endTransaction(); | ||
| } | ||
|
|
@@ -123,6 +123,7 @@ template<typename T_TWOWIRE> class Hd108MethodBase | |
|
|
||
| private: | ||
| const size_t _sizeData; // Size of '_data' buffer below | ||
| const size_t _sizeEndFrame; | ||
|
|
||
| T_TWOWIRE _wire; | ||
| uint8_t* _data; // Holds LED color values | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code style:
This library uses the "curly braces on their own line" format style.
While in general initialization (like this line) are often not strictly inforced, it more important for branching statements EVEN if they are single line (see below).