ESP32-C3 introductory tutorial basics (5. RMT application - SK6812 full-color RGB LED light drive test)

测试第五课,本来是准备测试一下PWM驱动 SK6812 RGB灯,
但是研究了一段时间,发现在ESP32-C3 有更好而且现成的方式 实现 SK6812 的控制,
使用PWM也不是不可以,只是对于初学者,需要多花好多时间,
所以本文还是先以ESP32-C3内置的 RMT 进行 SK6812 的控制,毕竟有现成的示例

foreword

The following ESP32-C3 functional tests are based on the development board designed by ourselves:

Draw a development board of ESP32-C3 by yourself (the first time to use Lichuang EDA) (PCB is available)

The development environment is Espressif's official ESP-IDF, built based on the VScode plugin:

ESP32-C3 VScode development environment is built (based on Espressif's official ESP-IDF - Windows and Ubuntu dual environment)

On the development board, I drew a SK6812 RGB light. Because I didn't know much about SK6812 at that time, I wrote PWMLED:
insert image description here

1. Basic introduction of SK6812 LED

The SK6812 lamp bead is an intelligent externally controlled LED light source that integrates the control circuit and the light-emitting circuit. The shape is the same as the 5050 LED lamp beads. But unlike ordinary LEDs, it does not simply control the on and off through high and low levels, it can control the on and off of RGB three colors through a single line , using a communication method called unipolar return-to-zero code data protocol .

1.1 SK6812 control principle

The basic introduction is available in the manuals of the products used. Here are some pictures from the product manuals used by my development board to explain:
After the LED is powered on and reset, it receives the data from the controller through the DIN port. The 24bit data sent first is extracted by the first lamp bead.

For users, what we need to know is to understand this protocol, and then implement the "0" code and "1" code mentioned in the manual, and then each lamp bead is composed of a 24-bit data structure.
(Note that the 24 bit mentioned here is a data structure. For example, if we use SPI to implement "0" and "1" codes, the SPI bus sends a byte, but only implements a code, which is only the 24 bit data structure above. 1bit! This will be explained in more detail later)

We take screenshots of the parameters that need to be understood (determine the specific parameters according to the product specification book you choose):
insert image description hereinsert image description hereinsert image description here
add some additional explanations. The addition of different proportions of three primary colors to obtain colors is called additive color mixing, such as:

Red + Green = Yellow
Red + Blue = Purple
Blue + Green = Cyan
Red + Blue + Green = White

For example: display yellow with RGB values ​​of 255, 255, 0. Then the above 24bit data structure is:
1111 1111 1111 1111 0000 0000 (1 and 0 are the "1" code and "0" code mentioned above)

To sum up the above, no matter which method is used, it is necessary to give the high and low levels that meet the time requirements at the DIN end in order to control the SK6812 correctly:
insert image description here

1.2 SK6812 control scheme

Knowing the control principle of SK6812, on the development board, we use an IO port to connect the DIN terminal as the input terminal of the signal, then we need to implement this IO port to achieve high and low levels that meet the timing, so what are the implementation methods?

1.2.1 GPIO toggle

The ultimate goal is to achieve the high and low levels of the specified time, so the most direct thought is to directly set the GPIO flip speed to the maximum, and then directly set the reset GPIO to achieve the high and low levels. = =!

But think carefully that the above time requirement is at the us level, then for different chips, not only because of different main frequencies, but also the flipping speed of the IO port will of course be different. Check it on the Internet (for reference only):

1、STM32

  • I saw a blog post that directly operates the register at 222ns test the IO port flip speed STM32F103RCT6
  • Taking the main frequency of 72MHz as an example, the instruction controls the GPIO flip, up to 18MHz.
  • Direct operation of registers, single instruction cycle, when 407 is overclocked to 200M, the IO port is just 100M

The above events can be calculated by themselves through the formula: f=1/T. (T is in seconds (s), f is in hertz (Hz)).

2、ESP32

  • The speed of the IO port of ESP32, a simple search did not find the description.

3、ESP8266

  • It takes about 2.5us (0.4MHz) to effectively flip the GPIO of ESP8266
  • GPIO0 of ESP8266 has the fastest flip speed, which can be achieved with register operations

After all, at present, the development of MCU still has the conditions to directly control the level of the IO port. This simple and rude method requires repeated testing and adjustment, because many factors need to be considered for time control. So here is the introduction, but more exploration.

1.2.2 SPI mode

In SPI mode, the speed of SPI communication can reach several tens of Mbps. In general, the maximum clock frequency of the SPI module is 1/2 of the system clock frequency.
There are many basic knowledge of SPI on the Internet, and it is also recorded in my blog post "Bus Protocol Record".

So sending through the SPI bus is a more feasible way. As long as the SPI clock is adjusted to about 8MHz (less than or equal to 8Mhz), it can be realized under different MCUs.

Using 8Mhz SPI, the time required to send a byte is 1.25us, which meets the time of one bit of the 24bit data structure of the SK6812 above:

Then according to the "1" code < 1us && >0.6us high level, >0.2us low level, it is concluded that SPI sends a byte 11111100b, which means "1" code, 0XF0.

In the same way, the SPI send byte 11000000b means "0" code, 0xC0.

Then, according to the example given in the principle part above, the 24bit data structure showing yellow is: 1111 1111 1111 1111 0000 0000

Then the SPI bus sends the following 24 bytes of data (in hexadecimal) to make the LED display yellow:

F0 F0 F0 F0 F0 F0 F0 F0 F0 F0 F0 F0 F0 F0 F0 F0 C0 C0 C0 C0 C0 C0 C0 C0

1.2.2 PWM method

The PWM method is also a common way to control the high and low levels. From the above, we know that the ratio of the high and low levels of the "1" code and the "0" code is 3:1. Then it is to adjust the duty cycle, and the "1" code The duty cycle is 75%, and the duty cycle of the "0" code is 25%. Then the rest only needs to set the PWM period to the symbol period of SK6812 > 1.2us. Pay attention to the duty cycle, which can be adjusted according to the actual situation.

Two online conclusions are given for reference only:

  • cycle (cycle) = 1.2us, duty cycle = 50% is 1, duty cycle = 30% is 0;
    (833Khz, it feels okay)
  • The cycle is set to 3MHz, the duty cycle = 66% is 1, and the duty cycle = 33% is 0;
    (0.33us cycle, it is estimated that the writing is wrong, 1MHZ is almost the same, 1us)

Regarding the PWM method of ESP32-C3, I am not sure whether it can be used or not. After all, the PWM of ESP32-C3 has not been learned and tested according to our blog post process. The next blog post will write about the PWM learning test record of ESP32-C3.

1.2.3 RMT mode (ESP32)

RMT is an infrared sending and receiving controller unique to the ESP32 series. The infrared protocol is converted into a signal, which is reflected in the IO, which is the high and low levels.

Taking the "1" code and "0" code mentioned above as infrared signals, the control of SK6812 can also be realized. Let's first understand the RMT of ESP32-C3.

2. Introduction to ESP32-C3 RMT

2.1 Introduction to RMT Basics

There is a detailed introduction to RMT in Espressif's official ESP32-C3 chip manual "esp32-c3_technical_reference_manual_cn" document: There
ESP32-C3 manual RMT section
is also a detailed introduction to RMT-related APIs on the official website: Espressif's official ESP32-C3 RMT part description

So the detailed information can still be viewed through the above channels. Here we need to pay attention to:

How does RMT control SK6812?

Through the previous SK6812 control principle, we know that controlling the SK2812 is to achieve the high and low levels that meet the time requirements. Then in the ESP32-C3 chip manual, it is mentioned how the RMT achieves this function. The part is as follows: (Of course, if If you want to know more, you should check the official information carefully)

insert image description here
It is easier to understand with the pictures on the official website:
insert image description here

2.2 Introduction to RMT usage (API related)

The basic steps of using RMT are as follows, but in this article, we need to control SK6812, so we only need to understand the configuration and use of sending:
insert image description here

The first thing to understand is a structure, the structure of the transmission configuration rmt_tx_config_t:
insert image description here
the content of the above structure is: RMT carrier frequency, RMT output level, idle level state, duty cycle, maximum cycle count, carrier enable, cycle Send enable, idle level output enable.

Through the example of initialization structure, you can better understand:
insert image description here
The default configuration of RMT output structure is as follows:
insert image description here
For controlling SK6812, it is enough to know the input configuration of RMT.

3. RMT example test

3.1 IDF Example Test

In the IDF sample program, the official example of controlling WS2812 is provided RMT Transmit Example -- LED Strip:
insert image description here

The process of the program is relatively simple, the driver of SK6812 is the same as that of ws2812, and the relevant code is in the components/led_strip/src/led_strip_rmt_ws2812.cfile.

For your own development board, and then for the example project, you can see the effect by simply modifying it, because the example is the same for everyone, here is a screenshot to indicate what needs to be modified:

insert image description here

insert image description here

It's too fast in the example, EXAMPLE_CHASE_SPEED_MSand my eyes are blinking a bit, so I changed this time to 300:

#define EXAMPLE_CHASE_SPEED_MS (300)//

On my development board, there was really only one LED, but for testing, I soldered a flying lead:
insert image description here

The test results show that the phenomenon of the example is that the different colors of the LED flash alternately, and there is no gradient effect. Here are a few pictures to take a look:

insert image description here

3.2 Example to change the gradient effect

At first, I didn't analyze the driver code bit by bit. The sample code also looked at the configuration of the RMT. The driver part of the SK6812 was not carefully studied, so the test was a flickering effect. Later, I thought it was not good, and it did not flicker gradually. , this must not be blinding = =!

So I still have to change it, so I looked at the example, which is actually a simple modification (the last one vTaskDelay(50)is not needed, here is the code that was changed before and forgot to remove it):
insert image description here

According to the instructions in the above illustration, changing all the times to the following is the smoothest and fastest gradient based on the routine basis:
insert image description here

No video can not see = =! The above picture barely copes with it:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42328389/article/details/122558050