Skip to content

Commit

Permalink
ws2812: replace RGBLED_NUM with driver-owned constant to decouple dri…
Browse files Browse the repository at this point in the history
…ver from RGBLEDs/RGBMATRIX defines (#18036)
  • Loading branch information
Kriechi committed Oct 20, 2022
1 parent a7d6643 commit c4f99f0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
6 changes: 6 additions & 0 deletions drivers/ws2812.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
# define WS2812_TRST_US 280
#endif

#if defined(RGBLED_NUM)
# define WS2812_LED_COUNT RGBLED_NUM
#elif defined(RGB_MATRIX_LED_COUNT)
# define WS2812_LED_COUNT RGB_MATRIX_LED_COUNT
#endif

/* User Interface
*
* Input:
Expand Down
12 changes: 6 additions & 6 deletions platforms/chibios/drivers/vendor/RP/RP2040/ws2812_vendor.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ static int state_machine = -1;
// clang-format off
static const uint16_t ws2812_program_instructions[] = {
// .wrap_target
0x7221, // 0: out x, 1 side 1 [2]
0x0123, // 1: jmp !x, 3 side 0 [1]
0x0400, // 2: jmp 0 side 0 [4]
0xb442, // 3: nop side 1 [4]
0x7221, // 0: out x, 1 side 1 [2]
0x0123, // 1: jmp !x, 3 side 0 [1]
0x0400, // 2: jmp 0 side 0 [4]
0xb442, // 3: nop side 1 [4]
// .wrap
};

Expand All @@ -62,7 +62,7 @@ static const pio_program_t ws2812_program = {
.origin = -1,
};

static uint32_t WS2812_BUFFER[RGBLED_NUM];
static uint32_t WS2812_BUFFER[WS2812_LED_COUNT];
static const rp_dma_channel_t* WS2812_DMA_CHANNEL;

bool ws2812_init(void) {
Expand Down Expand Up @@ -154,7 +154,7 @@ static inline void sync_ws2812_transfer(void) {
// Abort the synchronization if we have to wait longer than the total
// count of LEDs in millisecounds. This is safely much longer than it
// would take to push all the data out.
if (unlikely(timer_elapsed_fast(start) > RGBLED_NUM)) {
if (unlikely(timer_elapsed_fast(start) > WS2812_LED_COUNT)) {
dprintln("ERROR: WS2812 DMA transfer has stalled, aborting!");
dmaChannelDisableX(WS2812_DMA_CHANNEL);
return;
Expand Down
24 changes: 12 additions & 12 deletions platforms/chibios/drivers/ws2812_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
*/
#define WS2812_COLOR_BITS (WS2812_CHANNELS * 8)
#define WS2812_RESET_BIT_N (1000 * WS2812_TRST_US / WS2812_TIMING)
#define WS2812_COLOR_BIT_N (RGBLED_NUM * WS2812_COLOR_BITS) /**< Number of data bits */
#define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */
#define WS2812_COLOR_BIT_N (WS2812_LED_COUNT * WS2812_COLOR_BITS) /**< Number of data bits */
#define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */

/**
* @brief High period for a zero, in ticks
Expand Down Expand Up @@ -133,7 +133,7 @@
/**
* @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given bit
*
* @param[in] led: The led index [0, @ref RGBLED_NUM)
* @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
* @param[in] byte: The byte number [0, 2]
* @param[in] bit: The bit number [0, 7]
*
Expand All @@ -147,7 +147,7 @@
*
* @note The red byte is the middle byte in the color packet
*
* @param[in] led: The led index [0, @ref RGBLED_NUM)
* @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
* @param[in] bit: The bit number [0, 7]
*
* @return The bit index
Expand All @@ -159,7 +159,7 @@
*
* @note The red byte is the first byte in the color packet
*
* @param[in] led: The led index [0, @ref RGBLED_NUM)
* @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
* @param[in] bit: The bit number [0, 7]
*
* @return The bit index
Expand All @@ -171,7 +171,7 @@
*
* @note The red byte is the last byte in the color packet
*
* @param[in] led: The led index [0, @ref RGBLED_NUM)
* @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
* @param[in] bit: The bit index [0, 7]
*
* @return The bit index
Expand All @@ -184,7 +184,7 @@
*
* @note The red byte is the middle byte in the color packet
*
* @param[in] led: The led index [0, @ref RGBLED_NUM)
* @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
* @param[in] bit: The bit number [0, 7]
*
* @return The bit index
Expand All @@ -196,7 +196,7 @@
*
* @note The red byte is the first byte in the color packet
*
* @param[in] led: The led index [0, @ref RGBLED_NUM)
* @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
* @param[in] bit: The bit number [0, 7]
*
* @return The bit index
Expand All @@ -208,7 +208,7 @@
*
* @note The red byte is the last byte in the color packet
*
* @param[in] led: The led index [0, @ref RGBLED_NUM)
* @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
* @param[in] bit: The bit index [0, 7]
*
* @return The bit index
Expand All @@ -221,7 +221,7 @@
*
* @note The red byte is the middle byte in the color packet
*
* @param[in] led: The led index [0, @ref RGBLED_NUM)
* @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
* @param[in] bit: The bit number [0, 7]
*
* @return The bit index
Expand All @@ -233,7 +233,7 @@
*
* @note The red byte is the first byte in the color packet
*
* @param[in] led: The led index [0, @ref RGBLED_NUM)
* @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
* @param[in] bit: The bit number [0, 7]
*
* @return The bit index
Expand All @@ -245,7 +245,7 @@
*
* @note The red byte is the last byte in the color packet
*
* @param[in] led: The led index [0, @ref RGBLED_NUM)
* @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
* @param[in] bit: The bit index [0, 7]
*
* @return The bit index
Expand Down
2 changes: 1 addition & 1 deletion platforms/chibios/drivers/ws2812_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
# define WS2812_CHANNELS 3
#endif
#define BYTES_FOR_LED (BYTES_FOR_LED_BYTE * WS2812_CHANNELS)
#define DATA_SIZE (BYTES_FOR_LED * RGBLED_NUM)
#define DATA_SIZE (BYTES_FOR_LED * WS2812_LED_COUNT)
#define RESET_SIZE (1000 * WS2812_TRST_US / (2 * WS2812_TIMING))
#define PREAMBLE_SIZE 4

Expand Down

0 comments on commit c4f99f0

Please sign in to comment.