Programming The ESP32 In C - The S3's RGB LED
Written by Harry Fairhead   
Tuesday, 24 September 2024
Article Index
Programming The ESP32 In C - The S3's RGB LED
The Program

There are examples of driving the NeoPixel but they use either assembler or the RMT peripheral however the ESP32 S3 is just fast enough to be able to create the pulse times within the specified range:

#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
void writeGRB(int pin, int GRB)
{
    int mask = 0x800000;
    volatile int duty = 0;
    for (int j = 0; j < 24; j++)
    {
        gpio_set_level(pin, 1);
        if ((GRB & mask))
        {
            for (volatile int i = 0; i < 3; i++)
            {
            }
            duty = 0;
            duty = 0;
            gpio_set_level(pin, 0);
            duty++;
            duty = 0;
            duty = 0;
            duty = 0;
        }
        else
        {
            duty = 0;
            gpio_set_level(pin, 0);
            for (volatile int i = 0; i < 5; i++)
            {
            }
        }
        mask = mask >> 1;
    }
    vTaskDelay(60 / portTICK_PERIOD_MS);
}
void app_main(void)
{
    int pin = 48;
    gpio_reset_pin(pin);
    gpio_set_direction(pin, GPIO_MODE_OUTPUT);
    gpio_set_level(pin, 0);
    vTaskDelay(10 / portTICK_PERIOD_MS);
    int color = 0x0000FF;
    writeGRB(pin, color);
    vTaskDelay(1000 / portTICK_PERIOD_MS);
    color = 0x00FF00;
    writeGRB(pin, color);
    vTaskDelay(1000 / portTICK_PERIOD_MS);
    color = 0xFF0000;
    writeGRB(pin, color);
    vTaskDelay(1000 / portTICK_PERIOD_MS);
}

All of the work is done by the writeGRB function which uses time wasting instructions to set the pulse times as near as possible to the specification. Setting a variable to zero is usually the smallest time delay you can create and incrementing a variable takes longer. The code was manually tuned by sending the signal to another GPIO line and using a logic analyzer to adjust the timings.

If you try this out then on an ESP32 S3 the timings are 350ns, 720ns and 1.23μsfor a T0H, T1H and the total time respectively. These are within the permitted error.

The same approach works with an ESP32 with a NeoPixel connected to a GPIO line but as it is slightly slower the for loop’s timing has to be modified:

    for (int j = 0; j < 24; j++)
    {
        gpio_set_level(pin, 1);
        if ((GRB & mask))
        {
            for (volatile int i = 0; i < 2; i++)
            {
            }
            duty = 0;          
            gpio_set_level(pin, 0);         
            duty = 0;
            duty = 0;
            duty = 0;
            duty = 0;
        }
        else
        {
            gpio_set_level(pin, 0);
            for (volatile int i = 0; i < 3; i++)
            {
            }
            duty = 0;
        }

With these changes the timings are 370ns, 690ns and 1.22μsfor a T0H, T1H and the total time respectively. These are within the permitted error.

Programming The ESP32 In C
Using The Espressif IDF

By Harry Fairhead

espC360

To be published in October 2024. Sign up to our newsletter or RSS feed to be notified.

Contents

       Preface

  1. The ESP32 – Before We Begin
  2. Getting Started
  3. Getting Started With The GPIO 
  4. Simple Output
         
    Extract: The S3's RGB LED ***NEW!
  5. Some Electronics
  6. Simple Input
  7. Advanced Input – Interrupts
  8. Pulse Width Modulation
  9. Controlling Motors And Servos
  10. Getting Started With The SPI Bus
  11. Using Analog Sensors
  12. Using The I2C Bus
  13. One-Wire Protocols
  14. The Serial Port
  15. Using WiFi
  16. Direct To The Hardware
  17. Free RTOS 

<ASIN:187196282X>

kotlin book

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner



Last Updated ( Tuesday, 24 September 2024 )