Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/internal/NeoMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ License along with NeoPixel. If not, see
#include "methods/DotStarEsp32DmaSpiMethod.h"
#include "methods/NeoEsp32I2sXMethod.h"
#include "methods/NeoEsp32LcdXMethod.h"
#endif


#if defined(SOC_PARLIO_SUPPORTED)
#include "methods/Hd108Esp32ParlioMethod.h"
#endif

#include "methods/NeoEspBitBangMethod.h"

#elif defined(ARDUINO_ARCH_NRF52840) // must be before __arm__
Expand Down
204 changes: 204 additions & 0 deletions src/internal/methods/Hd108Esp32ParlioMethod.h
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 = {

Copy link
Copy Markdown
Owner

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).

.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)
17 changes: 9 additions & 8 deletions src/internal/methods/Hd108GenericMethod.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Owner

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 "all code blocks require wrapping curly braces" and "curly braces on their own line" format styles.
so this needs to change to something like...

for (int i = 0; i < 16; i++)
{
            _wire.transmitByte(0x00);
}


// 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();
}
Expand Down Expand Up @@ -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
Expand Down