Using SPI and DMA to drive WS2812B colorful LED
Before starting the SPI drive WS2812B design, first understand that WS2812B is an intelligent external control LED light source that integrates control circuit and light-emitting circuit. Its appearance is the same as a 050LED lamp bead, and each component is a pixel. The pixel contains an intelligent digital interface data latch signal shaping and amplification drive circuit, a high-precision internal oscillator and a programmable constant current control part, which effectively ensures that the color of the pixel light is highly consistent. The data protocol adopts a single-line return-to-zero code communication method. After the pixel is powered on and reset, the DIN end receives the data transmitted from the controller. The first 24-bit data sent is extracted by the first pixel and sent to the data latch inside the pixel. The remaining data is shaped and amplified by the internal shaping processing circuit and then forwarded to the next cascaded pixel through the DO port. After each pixel is transmitted, the signal is reduced by 24 bits. The pixel adopts automatic shaping and forwarding technology, so that the number of cascades of the pixel is not limited by signal transmission, but only by the signal transmission speed requirement. This lamp is now widely used, and its size and pins are as follows.
There are still some requirements for the power supply voltage of the chip, the minimum is 3.7V. In order to better drive it, the 5V power supply on the board is also needed.
Data communication uses a 3.3V level. According to the description in its electrical parameter table, a high level of 3.3V is sufficient for its recognition.
In circuit design, you only need to connect them in series as follows
The data transmission of WS2812B adopts single-wire mode. The following are its data transmission time, timing waveform and data transmission method. It can be seen that the data 1 and 0 are determined by different high-level times. According to the time value, it can be estimated that the total time of data 1 and 0 can be the same, and the high-level time can be 2/3 and 1/3 of the total time respectively. The total time can be set to 1.25uS, which is equivalent to a square wave of about 800KHz, but the duty cycle is 2/3 and 1/3 respectively to wait for 1 and 0. However, we use the SPI drive mode. This drive mode needs to control the number of consecutive 1 bits in each byte of 8 bits to achieve the adjustment of the duty cycle. According to the following time, 5bit and 2bie bits can be used to represent the value 1 and the value 0 respectively, that is, 5/8 and 2/8, which is equivalent to 781nS and 313nS, which just meets the time of T1H and T0H required by the timing.
In the data transmission method, as mentioned above, the first 24-bit data sent is extracted by the first pixel and sent to the data latch inside the pixel. The remaining data is shaped and amplified by the internal shaping processing circuit and then forwarded to the next cascaded pixel through the DO port. After each pixel is transmitted, the signal is reduced by 24 bits. That is, each light only retains the first 24-bit data it receives, and the rest is transmitted to the next light.
At the same time, after all data are transmitted once, a RESET signal needs to be generated. The signal time is greater than 280uS. 320uS is used in the design to meet the timing change.
When data is sent, it is transmitted in the following order, that is, the high bit line is sent and the GRB order is sent.
Based on the above content, the power supply of WS2812B can be connected to the P5LP_VDD pin on the J6 pin header as shown below for power supply. This pin is 5V voltage and comes from the USB interface, which just meets the requirements.
According to the definition of chip pin function multiplexing, P10.0 and P10.1 can be selected as MISO and MISO
And select P10.2 as the SCLK pin
Of course, this design actually only needs MOSI.
The control only needs to connect to the 5V power supply and the MOSI pin of the SPI host. The 5V is connected to the 5V power pin on the board.
Open PSoC Creator, search by chip series and use SPI to filter, and you will find that there are still many demo codes. Here we choose the DMA+SPI MAter routine.
After opening, update the chip model to the model of the corresponding development board.
Research has found that this is an example of a chip that sends and receives data on its own. Its main function is for the Master to send commands to the Slave, and then control the LED lights on the Slave to light up in different colors.
Even the self-transmission and self-reception are on the two cores of a chip, with the M0 core acting as the SPI Slave device and the M4 core acting as the Master device. To be honest, it is very clever.
But in fact, in order to drive the light, it only needs to be the SPI of the Master, so the project needs to be modified appropriately.
The first thing is to remove the Slave
Just leave the Master
At this time, confirm that the correct pins have been selected for each SPI pin of the Master.
Double-click mSPI to edit the configuration
The final item is to configure the clock to 6.4MHz, save
SPI Master is changed to only need to send
Remove most of the code of M0's SPI Slave and leave the following main function
Next, we will design the light strip based on the WS2812B. First, the light strip contains 16 lights.
Therefore, according to the SPI driving light strip method introduced in the previous article, first define the relevant macro definitions and variables, including the number of lamp beads, the number of color bytes of each lamp bead, the number of bytes required for SPI to generate a color, the duration of the RESET signal required to update the color after the data and the equivalent number of SPI bytes, the three data structures of WS2812B, and the data cache of 16 lamp beads, etc. Data 1 uses 0x1F, which is a duty cycle of 5/8, and data 0 uses 0x03, which is a duty cycle of 2/8.
Implementing the data filling function actually converts the color into each byte of SPI data, and the last byte is filled with 0xFF, mainly to keep the chip's MOSI output closed and high.
In the main function, in addition to initializing the system and spi interface, the display colors of the 16 lamp beads are updated every 2 seconds in the main loop.
The SPI interface initialization operation is as follows, and the DMA mode is configured.
The key point below is to look at the DMA. The DMA of this chip is very powerful. It is configured using Descriptors. There can be multiple Descriptors, each with a maximum transfer of 65536 bytes. It can realize 2D data transmission of X and Y, as shown in the figure below. And the trigger is very flexible, and the data volume can be extremely large.
This time, DMA is used to drive 16 WS2812B chips via SPI. According to the variable array ws2812b_bit_buffer defined earlier, 624 data need to be transmitted, so 10 Descriptors are designed.
Each of the first 9 descriptors transmits 64 bytes, the data transmission unit is Byte, the bit width is Byte to Word, and the source address needs to be incremented by 1.
The tenth one transmits 624-64*9=48 bytes.
The first bit of each descriptor is connected, and the previous one will trigger the next one after completion, and finally the completion interrupt will be triggered after the entire descriptor chain is completed.
When DMA is initialized, the Descriptor needs to be initialized manually
And specify the cheap address of each Descriptor
The sending function calls ConfigureTxDma to complete the above DMA configuration and enable the DMA channel. The masterTranStatus variable is used to determine the status.
When the SPI transfer is completed, clear the above status in the interrupt
Compile, download and run
Using a logic analyzer, we can see the frequency of the DI pin of WS2812B after the SPI data is output, which is about 800K, as well as the duty cycle of data 1 and 0.
While waiting, you can switch the color between green, red and blue every 2 seconds.
Summary: Relatively speaking, DMA is indeed very powerful. It may feel troublesome when you use it for the first time, but later you will find that it is really good, very good and very fragrant. WS2812B is also very fun, and you can debug some breathing light and other effects later.