Skip to content

Commit 03f1eea

Browse files
committed
revamp and add documentation
1 parent 13c0392 commit 03f1eea

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

docs/ws2812_driver.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ Configure the hardware via your config.h:
7777
7878
You must also turn on the SPI feature in your halconf.h and mcuconf.h
7979
80+
#### Circular Buffer Mode
81+
Some boards may flicker while in the normal buffer mode. To fix this issue, circular buffer mode may be used to rectify the issue.
82+
83+
By default, the circular buffer mode is disabled.
84+
85+
To enable this alternative buffer mode, place this into your `config.h` file:
86+
```c
87+
#define WS2812_SPI_USE_CIRCULAR_BUFFER
88+
```
89+
90+
#### Setting baudrate with divisor
91+
To adjust the baudrate at which the SPI peripheral is configured, users will need to derive the target baudrate from the clock tree provided by STM32CubeMX.
92+
93+
Only divisors of 2, 4, 8, 16, 32, 64, 128 and 256 are supported by hardware.
94+
95+
|Define |Default|Description |
96+
|--------------------|-------|-------------------------------------|
97+
|`WS2812_SPI_DIVISOR`|`16` |SPI source clock peripheral divisor |
98+
8099
#### Testing Notes
81100

82101
While not an exhaustive list, the following table provides the scenarios that have been partially validated:

drivers/chibios/ws2812_spi.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,31 @@
3636
// baudrate should target 3.2MHz
3737
// F072 fpclk = 48MHz
3838
// 48/16 = 3Mhz
39-
#ifndef WS2812_SPI_DIVISOR
40-
# if defined(STM32L4XX)
41-
# define WS2812_SPI_DIVISOR (SPI_CR1_BR_2) // fpclk/32
42-
# else
43-
# define WS2812_SPI_DIVISOR (SPI_CR1_BR_1 | SPI_CR1_BR_0) // fpclk/16
44-
# endif
39+
#if WS2812_SPI_DIVISOR == 2
40+
# define WS2812_SPI_DIVISOR (0)
41+
#elif WS2812_SPI_DIVISOR == 4
42+
# define WS2812_SPI_DIVISOR (SPI_CR1_BR_0)
43+
#elif WS2812_SPI_DIVISOR == 8
44+
# define WS2812_SPI_DIVISOR (SPI_CR1_BR_1)
45+
#elif WS2812_SPI_DIVISOR == 16 //same as default
46+
# define WS2812_SPI_DIVISOR (SPI_CR1_BR_1 | SPI_CR1_BR_0)
47+
#elif WS2812_SPI_DIVISOR == 32
48+
# define WS2812_SPI_DIVISOR (SPI_CR1_BR_2)
49+
#elif WS2812_SPI_DIVISOR == 64
50+
# define WS2812_SPI_DIVISOR (SPI_CR1_BR_2 | SPI_CR1_BR_0)
51+
#elif WS2812_SPI_DIVISOR == 128
52+
# define WS2812_SPI_DIVISOR (SPI_CR1_BR_2 | SPI_CR1_BR_1)
53+
#elif WS2812_SPI_DIVISOR == 256
54+
# define WS2812_SPI_DIVISOR (SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0)
55+
#else
56+
# define WS2812_SPI_DIVISOR (SPI_CR1_BR_1 | SPI_CR1_BR_0) //default
4557
#endif
4658

47-
// Define SPI circular buffer
48-
#ifndef WS2812_SPI_CIRCULAR_BUFFER
49-
# if defined(STM32L4XX)
50-
# define WS2812_SPI_CIRCULAR_BUFFER 1
51-
# else
52-
# define WS2812_SPI_CIRCULAR_BUFFER 0
53-
# endif
59+
// Use SPI circular buffer
60+
#ifdef WS2812_SPI_USE_CIRCULAR_BUFFER
61+
# define WS2812_SPI_BUFFER_MODE 1 //circular buffer
62+
#else
63+
# define WS2812_SPI_BUFFER_MODE 0 //normal buffer
5464
#endif
5565

5666
#define BYTES_FOR_LED_BYTE 4
@@ -103,14 +113,14 @@ void ws2812_init(void) {
103113

104114
// TODO: more dynamic baudrate
105115
static const SPIConfig spicfg = {
106-
WS2812_SPI_CIRCULAR_BUFFER, NULL, PAL_PORT(RGB_DI_PIN), PAL_PAD(RGB_DI_PIN),
116+
WS2812_SPI_BUFFER_MODE, NULL, PAL_PORT(RGB_DI_PIN), PAL_PAD(RGB_DI_PIN),
107117
WS2812_SPI_DIVISOR
108118
};
109119

110120
spiAcquireBus(&WS2812_SPI); /* Acquire ownership of the bus. */
111121
spiStart(&WS2812_SPI, &spicfg); /* Setup transfer parameters. */
112122
spiSelect(&WS2812_SPI); /* Slave Select assertion. */
113-
#if WS2812_SPI_CIRCULAR_BUFFER == 1
123+
#ifdef WS2812_SPI_USE_CIRCULAR_BUFFER
114124
spiStartSend(&WS2812_SPI, sizeof(txbuf) / sizeof(txbuf[0]), txbuf);
115125
#endif
116126
}
@@ -131,7 +141,7 @@ void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) {
131141
#ifdef WS2812_SPI_SYNC
132142
spiSend(&WS2812_SPI, sizeof(txbuf) / sizeof(txbuf[0]), txbuf);
133143
#else
134-
# if WS2812_SPI_CIRCULAR_BUFFER == 0
144+
# ifndef WS2812_SPI_USE_CIRCULAR_BUFFER
135145
spiStartSend(&WS2812_SPI, sizeof(txbuf) / sizeof(txbuf[0]), txbuf);
136146
# endif
137147
#endif

0 commit comments

Comments
 (0)