Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Me No Dev
@me-no-dev
count per microsecond depend on cpu speed and it's either 80 or 160
Michael Miller
@Makuna

Use something like this and avoid constants so you don't care about 80, 160.

    uint32_t usToTicks(uint32_t us) const
    {
        return (clockCyclesPerMicrosecond() * us);     // converts microseconds to tick
    }
    uint32_t ticksToUs(uint32_t ticks) const
    {
        return (ticks / clockCyclesPerMicrosecond()); // converts from ticks back to microseconds
    }

This is what the servo timer stuff uses when using timer0.

Me No Dev
@me-no-dev
static uint32_t ticksPeriod = 500;//still in us

void onTimer0(){
  uint32_t ticksAtEnter = timer0_read();
  //do your thing
  // NO YIELD OR DELAY
  timer0_write(ticksAtEnter + ticksPeriod);
}

void setup() {
  //convert period in us to ticks
  ticksPeriod *= clockCyclesPerMicrosecond();
  uint32_t ticksAtInit = timer0_read();
  timer0_attachInterrupt(&onTimer0);
  timer0_write(ticksAtInit + ticksPeriod);
}
@Makuna is this correct?
woops missed attachInterrupt
Hagai Shatz
@hagai-shatz
@Makuna, @me-no-dev - thanks for the examples, will give it a go.
Are there more differences between timer0 and timer1?
Can I break some other functionality by using them? For example, unable to use the servo library?
Me No Dev
@me-no-dev
above code does not run for timer0
I'm looking into why timer0_read is returning 0;
timer0 is used by the Servi lib
timer1 is used by analogWrite
so those will not work
Hagai Shatz
@hagai-shatz
Not good. But since I'm not planning on using Servi lib for now, this might be okay. As long as the timer0_read() works! :smile:
Me No Dev
@me-no-dev
ok I should have read @Makuna better and use ccount
also missed to init the interrupt
static uint32_t ticksPeriod = 500;//still in us

void onTimer0(){
  uint32_t ticksAtEnter = ESP.getCycleCount();
  //do your thing
  // NO YIELD OR DELAY
  timer0_write(ticksAtEnter + ticksPeriod);
}

void setup() {
  //convert period in us to ticks
  ticksPeriod *= clockCyclesPerMicrosecond();
  timer0_isr_init();
  timer0_attachInterrupt(&onTimer0);
  timer0_write(ESP.getCycleCount() + ticksPeriod);
}
both run with the same accuracy as far as I can tell
Hagai Shatz
@hagai-shatz
Thanks
Michael Miller
@Makuna
Last time I checked, timer0 was only used by servo library so in general it is open for use. Servo library can be told to use timer1 only also.
@me-no-dev esp.getcyclecount aND timer0_read should be the samething.
Me No Dev
@me-no-dev
@Makuna ccompare and ccount are not the same thing :)
Michael Miller
@Makuna
Sorry, my hands typed one thing and my mind was thinking something else, I meant they both use the same type, cycle counts.
Hagai Shatz
@hagai-shatz
Is there a similar interrupt I can setup on a specific pin? That is, instead of using timer, my ADC can set an external pin to high when data is ready to read (every 500us). How can I attach interrupt handler that will have good priority to ensure execution?
Me No Dev
@me-no-dev
use attachInterrupt(pin, fn, type);
Hagai Shatz
@hagai-shatz
Do I need to use attachInterrupt(digitalPinToInterrupt(pin), fn, type)?
Me No Dev
@me-no-dev
digitalPinToInterrupt no
pin and interrupt match here
Hagai Shatz
@hagai-shatz
Okay, thanks.
Ivan Grokhotkov
@igrr
regarding priority: the core inside esp8266 only has three interrupt levels. all "normal" interrupts are at level 1. Debug interrupt is level 2, and NMI is level 3.
so GPIO interrupt will not preempt other level 1 interrupts (e.g. timer interrupts)
Hagai Shatz
@hagai-shatz
@igrr , thanks for the clarifications.
Holger Lembke
@holgerlembke
so finally found some time to finish my flash check program for changing the flash chip.
Me No Dev
@me-no-dev
@holgerlembke I spent some time myself and can confirm that space above 4M does not work
Holger Lembke
@holgerlembke
and the results of the German jury are: if I write to it, I can read it back without errors....
@me-no-dev Hmmm. I write a integer into it and read that same value back
Me No Dev
@me-no-dev
@holgerlembke even though you might be able to access it from within the firmware
the ESP thinks that the FLASH size is 512K and puts the conf there
also SPIFFS does not work
Holger Lembke
@holgerlembke
hmmm.
Me No Dev
@me-no-dev
overall your option is to set 4M config and use the space above from the sketch
Holger Lembke
@holgerlembke
let me sum it up: use as program space: no. use as storage from my sketch: yes. right?
Me No Dev
@me-no-dev
right
Holger Lembke
@holgerlembke
so SPIFFS does not work why?
Me No Dev
@me-no-dev
ESP understands flash up to 4MB and assumes 512K is magic does not match to 4M
the ESP will put at the end of whatever it thinks the size is the config secttors
it's kinda hard for me to formulate and explain it all
Holger Lembke
@holgerlembke
me not understand that
SPIFFS is entirely sketch driven. right?
Me No Dev
@me-no-dev
|--sketch--|--ota--|--spiffs--|--eeprom--|--config--|
not entirely ;) address and size are defined in linker
Holger Lembke
@holgerlembke
so as the current flash sizes are... I had to change flashchip->chip_size