Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By dms
#13492 I have a project that requires 500 us events but the Ticker library as delivered only supports millisecond intervals. There is a SDK call for switching to microseconds so I added additional interfaces to Ticker to make the correct calls. When I first tied this it didn't work and it appears that before you can use the microseconds calls, the system timer has to be reset in the right at the start of usercode. Adding a call to system_timer_reinit (); in the user_init routine in core_esp8266_main.cpp did the trick!

Steps:
1) Add #define USE_US_TIMER at the beginning of osapi.h to make the microsecond timer call visible. osapi.h is in the ...\hardware\tools\esp8266\sdk\include directory
2) Add appropriate calls to the Ticker library (attached)
3) Add a call to system_timer_reinit () right at the beginning of the user_init function in core_esp8266_main.cpp. core_esp8266_main.cpp is in ...\hardware\esp8266com\esp8266\cores\esp8266
You do not have the required permissions to view the files attached to this post.
User avatar
By gerardwr
#13533 Here's a suggestion : The ticker function attach_ms also accepts floats as argument so you can set it to, for example, 0.1 for 100uSec.

I checked that it compiles OK, have not checked that the actual timing is OK.

Let us know if this works.
User avatar
By jh_
#22579 @gerardwr: Yes it compiles, but it does not work! (I tested it)

@dms: Thanks a lot for your work. Your post helped me a lot.
The Ticker-calls less than 1ms seems to have some flicker but apart from that it seems to work well.

I discovered, that your files may be out of date. But the little error which occures now, is easy to fix ->

In the osapi.h-file add the marked line:

#ifdef USE_OPTIMIZE_PRINTF
#define os_printf(fmt, ...) do { \
static const char flash_str[] ICACHE_RODATA_ATTR = fmt; \
os_printf_plus(flash_str, ##__VA_ARGS__); \
} while(0)
#else
extern int os_printf_plus(const char * format, ...) __attribute__ ((format (printf, 1, 2)));
#define os_printf os_printf_plus
#endif
User avatar
By gellpro
#48535 Hi

@dms Your information help me a lot! Thank you!

I want to ask you a question.
In the case using attach_us, is it the mimum value is about 300us?
I entered some smaller value (e.g. 100us or 300us), but they didn't work correctly.
The minimum of output value of following code was about 6300.
Namely, attach_us was not called earlier than 300us(it's about 3kHz).

Code: Select allvoid loop() {
  if(tickOccured == true) {
    time2 = micros();
    diff += time2 - time1;
    tickOccured = false;
   
    time1 = time2;
    count++;
   
    if(count>20) {
      Serial.println(diff);
      diff = 0;
      count = 0;
    }     
  }
  delay(0);
}


If there is some mistakes, please let me know.
I want to move ticker every 50us (20kHz).
Do you have any good ideas?

Thank you.