Device continually resets in my RMT example

orbitcoms
Posts: 149
Joined: Fri Aug 03, 2018 10:08 pm
Location: Sydney, Australia

Device continually resets in my RMT example

Postby orbitcoms » Thu Oct 22, 2020 10:27 pm

I am learning to use the RMT module to create a coded pulse stream.
The code was based on a post that was based on Kolban's ESP32 book and is copied below.

When I attempt to run the code, the processor continually resets.

Code: Select all

#include <stdio.h>
#include "driver/rmt.h"
#include "freertos/freeRTOS.h"
#include "freertos/task.h"

rmt_config_t config;

rmt_item32_t items[1];

#define RMT_GPIO GPIO_NUM_18

static void rmt_tx_init(void)
{
  config.rmt_mode = RMT_MODE_TX;
  config.channel = RMT_CHANNEL_0;
  config.gpio_num = RMT_GPIO;
  config.mem_block_num = 1;
  config.tx_config.loop_en = 0;
  config.tx_config.carrier_en = 0;
  config.tx_config.idle_output_en = 1;
  config.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
  config.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
  config.clk_div = 80; // 80MHx / 80 = 1MHz 0r 1uS per count
 
  rmt_config(rmt_config(&config));
  rmt_driver_install(config.channel, 0, 0);  //  rmt_driver_install(rmt_channel_t channel, size_t rx_buf_size, int rmt_intr_num)
   
  items[0].duration0 = 977;
  items[0].level0 = 1;
  items[0].duration1 = 977;
  items[0].level1 = 0; 

  items[1].duration0 = 0;
  items[1].level0 = 1;
  items[1].duration1 = 0;
  items[1].level1 = 0; 

}

void do_test(void)
{
  rmt_write_items(config.channel, items, 2,true);
}
void app_main(void)
{
  rmt_tx_init();
  while(1)
  {
    do_test();
    vTaskDelay(1000/portTICK_PERIOD_MS);
  }
}
Attachments
rmt error.jpg
rmt error.jpg (325.82 KiB) Viewed 2911 times

phillipdimond
Posts: 8
Joined: Thu Oct 22, 2020 9:08 am

Re: Device continually resets in my RMT example

Postby phillipdimond » Fri Oct 23, 2020 1:12 am

You declare ..

Code: Select all

rmt_item32_t items[1];
.. then set two elements of a 1 element array ..

Code: Select all

items[0].level1 = 0; 

  items[1].duration0 = 0;
Standard C bug - array overflow.

orbitcoms
Posts: 149
Joined: Fri Aug 03, 2018 10:08 pm
Location: Sydney, Australia

Re: Device continually resets in my RMT example

Postby orbitcoms » Fri Oct 23, 2020 2:10 am

Thanks

Shame the compiler does not flag the error

phillipdimond
Posts: 8
Joined: Thu Oct 22, 2020 9:08 am

Re: Device continually resets in my RMT example

Postby phillipdimond » Fri Oct 23, 2020 4:35 am

You're welcome, and we've *all* done it and I still do it after 40 years of coding.

That's just C. It's powerful, it's down on the metal, and it doesn't protect you from yourself. It's a nicer way to write assembler than writing assembler, and assembler won't protect you either. However, the ESP-32 has nicely trapped the error for you and a careful inspection of the dump or a heap traceback will reveal the issue. C alone would have just happily set that byte and not cared that it trashed something.

ESP IDF can be coded in C++, and if you follow C++ coding conventions you wouldn't have that issue (but performance will be affected, a problem on these low resource IOT devices).

Remember, C is a 1970's language, written for a PDP-11 when that computer was the size of a fridge and significantly less powerful than your ESP-32. Ahhh, I remember building PDP-11 systems and hand-entering bootstraps to boot RSX-11 from 8" floppies. 20MB cartridge disks were cool when we could get them!

Who is online

Users browsing this forum: Bing [Bot] and 116 guests