8. RGB color light strip
8.1. Experimental purpose
The SPI communication of STM32 is used to simulate the communication protocol of WS2812B module and drive the RGB light strip to display the effect.
8.2. Configure pin information
- Import ioc file from Beep project and name it RGB_Strip.
According to the schematic diagram, the pin of the RGB light bar connection is PB5. The driving mode of the RGB light bar can use timer PWM output or SPI output, and the PB5 pin can be redefined as timer PWM or SPI output. Considering the subsequent timer conflict problem, SPI communication mode is used here to drive the RGB light bar.

- Set the mode of SPI3 to Transmit Only Master, so that pin PB5 is automatically set to SPI3_MOSI.

- Modify the parameters of data sent by SPI3. Refer to the following picture for specific parameters.

- Add DMA Settings for SPI3_TX.

8.3. Analysis of experimental flow chart

8.4. Core code interpretation
- Create bsp_rgb.h and bsp_rgb.c, and add the following content to bsp_rgb.h:

Among them, the ws2812_t structure is used to store the cache data of the light bar.
- Create the following content in the bsp_rgb.c file: According to the communication protocol of WS2812, the three bits of SPI are simulated to the one bit of WS2812. When the value of SPI output three digits is 0b110, it corresponds to the high level of WS2812; when the value of SPI output three digits is 0b100, it corresponds to the low level of WS2812.

- WS2812_Set_Color_One() function sets a single RGB light color value, index=[0, MAX_RGB-1], RGB=[0x00000000, 0x00FFFFFF], RGB value permutation: red in the front, middle is green, the last is blue.

- RGB_Set_Color(index, r, g, b) and RGB_Set_Color_U32(index, color) Set the RGB strip color value. index=[0, MAX_RGB-1] controls the color of the corresponding lamp bead, and index=255 controls the color of all lamp bead.


5.RGB_Clear() Clears the RGB indicator cache.

- The RGB_Update() function refreshed the RGB light bar color. The RGB light must call the RGB_Update() function to refresh the display after changing the color, otherwise it will not produce the effect.

- Send data using DMA of SPI3.

8.5. Hardware connection
Since the RGB indicator bar needs to be connected to the position of the RGB indicator bar on the expansion board, the interface has been set for anti-reverse connection. You can insert the interface in the correct direction.

8.6. Experimental effect
After burning the program, the LED light flashes every 200 milliseconds, and the RGB light strip displays red, green, and blue in turn, switching a color every 500 milliseconds.