Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By soroosh
#70840 Hi,
I want to generate 4uS square wave with 50% duty cycle and I use os_timer_arm_us function but that generate 4mS square wave same as os_timer_arm function!!
what is my mistake? Thank you
Code: Select all#define out_pin 5

volatile unsigned long timeSpent=1;
unsigned long prevMillis=0;
unsigned long current_time=0;

extern "C" {
#define USE_US_TIMER
#include "user_interface.h"
#include "osapi.h"
#define os_timer_arm_us(a, b, c) ets_timer_arm_new(a, b, c, 0)
}

os_timer_t t0;


void myIsrTimer(void*z)
{
  current_time = micros();
  timeSpent = current_time - prevMillis;
  prevMillis =  current_time; 
  //toggle = (toggle == 1) ? 0 : 1;
  //digitalWrite(out_pin,toggle); 
}

void setup() {
  pinMode(out_pin, OUTPUT); 

  os_timer_setfn(&t0, myIsrTimer, NULL);
  os_timer_arm_us(&t0, 4, true);
 
  Serial.begin(115200); 
}

void loop() {
 
  Serial.print(timeSpent);Serial.print(" ");
}
User avatar
By urbanze
#70858
soroosh wrote:Hi,
I want to generate 4uS square wave with 50% duty cycle and I use os_timer_arm_us function but that generate 4mS square wave same as os_timer_arm function!!
what is my mistake? Thank you
Code: Select all#define out_pin 5

volatile unsigned long timeSpent=1;
unsigned long prevMillis=0;
unsigned long current_time=0;

extern "C" {
#define USE_US_TIMER
#include "user_interface.h"
#include "osapi.h"
#define os_timer_arm_us(a, b, c) ets_timer_arm_new(a, b, c, 0)
}

os_timer_t t0;


void myIsrTimer(void*z)
{
  current_time = micros();
  timeSpent = current_time - prevMillis;
  prevMillis =  current_time; 
  //toggle = (toggle == 1) ? 0 : 1;
  //digitalWrite(out_pin,toggle); 
}

void setup() {
  pinMode(out_pin, OUTPUT); 

  os_timer_setfn(&t0, myIsrTimer, NULL);
  os_timer_arm_us(&t0, 4, true);
 
  Serial.begin(115200); 
}

void loop() {
 
  Serial.print(timeSpent);Serial.print(" ");
}



I remember, maximum frequency what I can do is ~250kHz, exactly 4uS. However, in your ISR function, has many slow commands, remove all and leave just toggle pin (digitalwrite or GPO..). If you need other commands in functions, it's cool to make an flag to do this.
User avatar
By soroosh
#70866
urbanze wrote:I remember, maximum frequency what I can do is ~250kHz, exactly 4uS. However, in your ISR function, has many slow commands, remove all and leave just toggle pin (digitalwrite or GPO..). If you need other commands in functions, it's cool to make a flag to do this.


Thank you for your reply.
Unfortunately, that does not work truly even just toggle the pin in ISR function.
User avatar
By urbanze
#70892
soroosh wrote:
urbanze wrote:I remember, maximum frequency what I can do is ~250kHz, exactly 4uS. However, in your ISR function, has many slow commands, remove all and leave just toggle pin (digitalwrite or GPO..). If you need other commands in functions, it's cool to make a flag to do this.


Thank you for your reply.
Unfortunately, that does not work truly even just toggle the pin in ISR function.


I do not know if it is possible, by hardware to convert pwm to another frequency, if so, you can use the special function of GPIO to "release" the frequency of the internal oscillator of 26MHz. Maybe with the ESP32 you get 250kHz.