Sample code for software based timer into ESP32-idf

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Sample code for software based timer into ESP32-idf

Postby Ritesh » Tue Dec 20, 2016 5:42 pm

Hi,

As I have looked that there are total 2 hardware timers are used and also got samples of hardware timers as well.

But, I want to implement some code based on software timer to trigger that code into some particular time duration. I have tried to look samples for software based timers into ESP32-IDF SDK but not found ..

Does anyone has implemented that type of sample code?

Please let me know if anyone has sample code for that.
Regards,
Ritesh Prajapati

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Sample code for software based timer into ESP32-idf

Postby kolban » Tue Dec 20, 2016 5:56 pm

Not 100% sure what you mean by a software timer?

It is not uncommon to keep a list of "functions to be called when a timer interval has been reached". This becomes a matter of programming where one keeps a list of records where each record contains the time to fire and the function to call. These are ideally sorted in chronological order with the "next" record to fire being first in the list.

You then have some form of "master loop" which compare the time now against the first item in the list. If now >= fire time, you invoke the function and remove the record from the list. Optimizations of this that do not involve this kind of polling include using a hardware interrupt timer to automatically fire at the time of the first item in the list, when it does, you execute the function and re-initialize the hardware timer for the next item in the list. Challenges come in if the list is dynamic in nature in which case when a new timer is added that is sooner than the first timer in the list you need to cancel the timer and restart it based on the now new earliest time.

Is this close to what you were asking for?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Sample code for software based timer into ESP32-idf

Postby Ritesh » Tue Dec 20, 2016 6:06 pm

Hi,

Yes. We have one requirement in which we need to perform some tasks like LED should be blink in High frequency with predefined interval when device is working in such situations to detect device operating condition based on LED behavior.

That is why I am looking as we have used software timer in ESP8266 project.

Does hardware timer fulfill my requirements or not?

Let me know if you have sample code that.
Regards,
Ritesh Prajapati

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Sample code for software based timer into ESP32-idf

Postby Ritesh » Wed Dec 21, 2016 6:04 pm

Hi,

Right now, we have requirement to create timer for some milliseconds interval to blink LEDs based on condition. We have tried to look for software based time like ESP8266 but couldn't found any software based timer into ESP32.

So, we have tried for hardware based time but there is limitations like not able to execute timer for milliseconds interval as it is working for second based timers.

Please let me know if anyone has implemented milliseconds based timer into ESP32.
Regards,
Ritesh Prajapati

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: Sample code for software based timer into ESP32-idf

Postby loboris » Wed Dec 21, 2016 8:01 pm

You can use FreeRTOS timers, look at esp-idf/components/freertos/include/freertos/timers.h
and http://www.freertos.org/FreeRTOS-Softwa ... tions.html
It is quite simple to use it and it works well. Keep in mind that the timer interval is in FreeRTOS ticks.

Code: Select all

#include "freertos/timers.h"

TimerHandle_t tmr;
int id=1;
int interval = 100;
tmr = xTimerCreate("MyTimer", interval, pdTRUE, ( void * )id, vTimerCallback);
if( xTimerStart( timers[id].tmr, 10 ) != pdPASS ) {
 printf("Timer start error");
}
...

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Sample code for software based timer into ESP32-idf

Postby Ritesh » Thu Dec 22, 2016 4:08 am

loboris wrote:You can use FreeRTOS timers, look at esp-idf/components/freertos/include/freertos/timers.h
and http://www.freertos.org/FreeRTOS-Softwa ... tions.html
It is quite simple to use it and it works well. Keep in mind that the timer interval is in FreeRTOS ticks.

Code: Select all

#include "freertos/timers.h"

TimerHandle_t tmr;
int id=1;
int interval = 100;
tmr = xTimerCreate("MyTimer", interval, pdTRUE, ( void * )id, vTimerCallback);
if( xTimerStart( timers[id].tmr, 10 ) != pdPASS ) {
 printf("Timer start error");
}
...
Thanks for reply. We have tested above free RTOS timer but we have faced issue like system restarted randomly after that.

Still, i will check it again and will let you know its working or not
Regards,
Ritesh Prajapati

jumjum123
Posts: 199
Joined: Mon Oct 17, 2016 3:11 pm

Re: Sample code for software based timer into ESP32-idf

Postby jumjum123 » Thu Dec 22, 2016 6:23 am

Ritesh, would you please share your information about restarts after using timer ?
It could be helpful for others, and mostly they can help.
BTW, did you change stacksize to 2048 ?
For more information see http://www.esp32.com/viewtopic.php?f=14&t=743#p3136
There is also a recipe to get some more information around a crash http://www.esp32.com/viewtopic.php?t=263#p1131

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Sample code for software based timer into ESP32-idf

Postby kolban » Thu Dec 22, 2016 7:44 am

Ritesh,
Depending on your appetite for sophistication, another possibility is the "RMT" component (Remote Control). Although apparently designed for driving infra-red remote control transmitters and receivers, this component is extremely powerful. Specifically, one can set up timers that drive GPIO pins and say how long you would like them high and how long you would like them low. You can have up to 8 distinct parallel channels (i.e. 8 pins of output). You can also say that you want the pattern to repeat once sent. For example, you could define a channel that is 1 of 500 msecs and 0 for 500 msecs and then repeats. What you effectively have is a 1 second period square wave signal on a pin. Your durations can be arbitrary ...and your sequence of timings arbitrarily complex. You can start and stop these signals being generated at will and since the code for running them appears to be embedded in the silicon of the ESP32, you appear to get them at no application cost other than setup.

They aren't yet for the feint of heart and if you decide to use, you will be investing a few hours in their study and experimentation with them ... but they are definitely an option. For example, I am using this function to show the status of my ESP32 using a "neopixel" (WS2812) which is a 24bit color LED. So instead of multiple LEDs on my board, I have one LED that has a color indicating the board status.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

HardwireIO
Posts: 10
Joined: Mon Mar 06, 2017 5:34 pm

Re: Sample code for software based timer into ESP32-idf

Postby HardwireIO » Mon Mar 06, 2017 5:40 pm

Actually I'm facing the same problems with FreeRTOS timers: once timer is triggered, callback function is called correctly, but even though I defined an empty function, system restarts due to an access violation:

Code: Select all

Guru Meditation Error of type LoadProhibited occurred on core  0. Exception was unhandled.
Register dump:
PC      : 0x400856cb  PS      : 0x00060433  A0      : 0x800841c5  A1      : 0x3ffd2740  
A2      : 0x0000001d  A3      : 0x3ffd27d0  A4      : 0x00000010  A5      : 0x3ffcec1c  
A6      : 0x3ffd202c  A7      : 0x401196e8  A8      : 0x80083386  A9      : 0x3ffd2730  
A10     : 0x00000003  A11     : 0x00060423  A12     : 0x00060423  A13     : 0x3ffc0b90  
A14     : 0x00000001  A15     : 0x3ffc0bd0  SAR     : 0x00000018  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x0000002d  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000  

Backtrace: 0x400856cb:0x3ffd2740 0x400841c5:0x3ffd2760 0x4008379b:0x3ffd2780 0x4008501c:0x3ffd27c0 0x4011abac:0x3ffd27f0 0x40119851:0x3ffd2810 0x40119a34:0x3ffd2830 0x40133afa:0x3ffd2850 0x401198d9:0x3ffd2870
Honesty ... I'm using FreeRTOS since many years ago, and my impression is that there's something related to FreeRTOS implementation on latest ESP-IDF.

Is anybody using FreeRTOS timers?
Hardwire.io / The IoT Platform

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Sample code for software based timer into ESP32-idf

Postby ESP_Angus » Mon Mar 06, 2017 11:58 pm

HardwireIO wrote:Actually I'm facing the same problems with FreeRTOS timers
Hi HardwareIO,

I'm able to reproduce this. Will let you know once a fix is available.

Angus

Who is online

Users browsing this forum: No registered users and 56 guests