-
Notifications
You must be signed in to change notification settings - Fork 2.1k
drivers/ws281x: simple DMA implementation through SPI #20218
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
hugueslarrive
wants to merge
10
commits into
RIOT-OS:master
Choose a base branch
from
hugueslarrive:drivers/ws281x/dma
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.
+230
−5
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b32008d
drivers/ws281x: simple DMA implementation through SPI
hugueslarrive 7c8d9ae
tests/drivers/ws281x_dma removed
hugueslarrive 391f441
makes DMA use optional
hugueslarrive e2cfda4
add compile time parameters
hugueslarrive 4361ac2
esp8266 support
hugueslarrive d2f0178
removes spiconf struct
hugueslarrive 0fb47e8
update documentation
hugueslarrive 7a94415
Merge branch 'master' into drivers/ws281x/dma
hugueslarrive 1fc0c7c
fixes for static tests
hugueslarrive 8efb332
Merge branch 'drivers/ws281x/dma' of github.com:hugueslarrive/RIOT in…
hugueslarrive 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
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
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,115 @@ | ||
/* | ||
* Copyright 2024 Hugues Larrive <hlarrive@pm.me> | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup drivers_ws281x | ||
* | ||
* @{ | ||
* | ||
* @file | ||
* @brief Implementation of `ws281x_write_buffer()` through SPI | ||
* | ||
* @author Hugues Larrive <hlarrive@pm.me> | ||
* | ||
* @} | ||
*/ | ||
#include <assert.h> | ||
#include <errno.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
#include "macros/units.h" | ||
#include "periph/spi.h" | ||
#include "ws281x.h" | ||
#include "ws281x_constants.h" | ||
#include "ws281x_params.h" | ||
|
||
#ifndef WS281X_SPI_DEV | ||
#define WS281X_SPI_DEV (0) | ||
#endif | ||
#ifndef WS281X_SPI_PATTERN_LENGTH | ||
#define WS281X_SPI_PATTERN_LENGTH (3) | ||
#endif | ||
#ifndef WS281X_SPI_CLK | ||
#define WS281X_SPI_CLK KHZ(800 * WS281X_SPI_PATTERN_LENGTH) | ||
#endif | ||
|
||
#if (WS281X_SPI_PATTERN_LENGTH == 3) | ||
/* The LSB of the 2nd byte (always 1) is the MSB of the pattern for bit 2, | ||
* which is encoded in the 3rd byte, so we will use a 4-bit pattern. */ | ||
#define WS281X_SPI_DATA_0_MASK (0b00001001) | ||
#define WS281X_SPI_DATA_1_MASK (0b00001101) | ||
/* 1u01u01u 01u01u01 u01u01u0 */ | ||
#define WS281X_SPI_SHIFTS {4,1,-2,3,0,5,2,-1} | ||
#elif (WS281X_SPI_PATTERN_LENGTH == 4) | ||
#define WS281X_SPI_DATA_0_MASK (0b00001000) | ||
#define WS281X_SPI_DATA_1_MASK (0b00001110) | ||
#define WS281X_SPI_SHIFTS {4,0,4,0,4,0,4,0} | ||
#elif (WS281X_SPI_PATTERN_LENGTH == 8) | ||
#define WS281X_SPI_DATA_0_MASK (0b11000000) | ||
#define WS281X_SPI_DATA_1_MASK (0b11111100) | ||
#define WS281X_SPI_SHIFTS {0,0,0,0,0,0,0,0} | ||
#endif | ||
|
||
static uint8_t spi_buf[WS281X_PARAM_NUMOF * WS281X_BYTES_PER_DEVICE * WS281X_SPI_PATTERN_LENGTH]; | ||
|
||
int ws281x_init(ws281x_t *dev, const ws281x_params_t *params) | ||
{ | ||
if (!dev || !params || !params->buf) { | ||
return -EINVAL; | ||
} | ||
|
||
memset(dev, 0, sizeof(ws281x_t)); | ||
dev->params = *params; | ||
|
||
return 0; | ||
} | ||
|
||
void ws281x_prepare_transmission(ws281x_t *dev) | ||
{ | ||
(void)dev; | ||
spi_acquire(SPI_DEV(WS281X_SPI_DEV), SPI_CS_UNDEF, SPI_MODE_0, WS281X_SPI_CLK); | ||
} | ||
|
||
void ws281x_end_transmission(ws281x_t *dev) | ||
{ | ||
(void)dev; | ||
spi_release(SPI_DEV(WS281X_SPI_DEV)); | ||
xtimer_usleep(WS281X_T_END_US); | ||
} | ||
|
||
void ws281x_write_buffer(ws281x_t *dev, const void *_buf, size_t size) | ||
{ | ||
assert(dev); | ||
|
||
const uint8_t *buf = _buf; | ||
const int8_t shift[8] = WS281X_SPI_SHIFTS; | ||
|
||
for (int i = 0; i < (int)size; ++i) { | ||
uint8_t byte = buf[i]; | ||
uint8_t offset = 0; | ||
spi_buf[i * WS281X_SPI_PATTERN_LENGTH] = 0; | ||
for (uint8_t cnt = 0; cnt < 8; ++cnt) { | ||
spi_buf[i * WS281X_SPI_PATTERN_LENGTH + offset] | ||
|= (byte & 0b10000000) | ||
? (shift[cnt] >= 0) | ||
? WS281X_SPI_DATA_1_MASK << shift[cnt] | ||
: WS281X_SPI_DATA_1_MASK >> -shift[cnt] | ||
: (shift[cnt] >= 0) | ||
? WS281X_SPI_DATA_0_MASK << shift[cnt] | ||
: WS281X_SPI_DATA_0_MASK >> -shift[cnt]; | ||
byte <<= 1; | ||
if (shift[cnt] <= 0) { | ||
++offset; | ||
spi_buf[i * WS281X_SPI_PATTERN_LENGTH + offset] = 0; | ||
} | ||
} | ||
} | ||
|
||
spi_transfer_bytes(SPI_DEV(WS281X_SPI_DEV), SPI_CS_UNDEF, false, spi_buf, NULL, sizeof(spi_buf)); | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.