Author Topic: Ms resolution timer using dev board  (Read 2852 times)

0 Members and 2 Guests are viewing this topic.

Offline mianos

  • Regular Contributor
  • *
  • Posts: 56
  • Country: au
Re: Ms resolution timer using dev board
« Reply #25 on: May 13, 2024, 12:41:48 am »
I was doing a lot of stuff with the esp32 counters and timers a few months ago.

Some observations:

As the esp32 is running RTOS there is a lot of latency for external and internally triggered timers, occurring because many internal drivers are disabling interrupts all the time.

The PCNT devices on the esp32 can be triggered by a gpio trigger. This is 100% hardware, no interrupts. To count a long time you need to do some tricky mode changing as to use the GPIO to trigger the PCNT. You can use the normal GPIO interrupts to stop the time but this will be jittery and will be often have 5+ uS of misscount. You can use my code for this.

If you want to do a timer you need to use the new EVNT system or trigger the PCNT states with the PCNT hardware trigger.
I have a 10Mhz input from good GPSDO and a 1pps from a LEA-M8 and it does not miss any counts over days.
I count to a billion over 100 seconds gated by the GPS and it just prints out 1,000,000,000,000 over and over.

As far as a solution to your issue. I would probably try and use an Ublox  M8T to generate a secondary clock source, maybe at 1Mhz and trigger this with the PCNT. While there is some phase noise if you are only looking to 1mS resolution it's a non issue.
The alternative is to use the 1pps to keep the internal clocks of different devices coherent. This is a bit trickier but not a big deal for 1mS. (I'd rather skip this one).

As others have said, OCXOs are only 20-30 bucks. I have a few. Most are badly implemented with the trimming network wrong so they can't be trimmed. You can mod them. I got one, unlike any of the others with no voltage reference but a much better circuit. If you are counting from this you need to level convert the output to 3.3V somehow. Once you do, the esp32 can count this perfectly. The one on my counter at the moment is sitting at an average of 10,000,000.001 Mhz for the last few weeks, with no dynamic disciplining but I would lower my expectations if it needs to be mobile.
 

Offline alepagliaccioTopic starter

  • Contributor
  • Posts: 21
  • Country: it
Re: Ms resolution timer using dev board
« Reply #26 on: May 13, 2024, 07:01:32 am »
I was doing a lot of stuff with the esp32 counters and timers a few months ago.

Some observations:

As the esp32 is running RTOS there is a lot of latency for external and internally triggered timers, occurring because many internal drivers are disabling interrupts all the time.

The PCNT devices on the esp32 can be triggered by a gpio trigger. This is 100% hardware, no interrupts. To count a long time you need to do some tricky mode changing as to use the GPIO to trigger the PCNT. You can use the normal GPIO interrupts to stop the time but this will be jittery and will be often have 5+ uS of misscount. You can use my code for this.

If you want to do a timer you need to use the new EVNT system or trigger the PCNT states with the PCNT hardware trigger.
I have a 10Mhz input from good GPSDO and a 1pps from a LEA-M8 and it does not miss any counts over days.
I count to a billion over 100 seconds gated by the GPS and it just prints out 1,000,000,000,000 over and over.

As far as a solution to your issue. I would probably try and use an Ublox  M8T to generate a secondary clock source, maybe at 1Mhz and trigger this with the PCNT. While there is some phase noise if you are only looking to 1mS resolution it's a non issue.
The alternative is to use the 1pps to keep the internal clocks of different devices coherent. This is a bit trickier but not a big deal for 1mS. (I'd rather skip this one).

As others have said, OCXOs are only 20-30 bucks. I have a few. Most are badly implemented with the trimming network wrong so they can't be trimmed. You can mod them. I got one, unlike any of the others with no voltage reference but a much better circuit. If you are counting from this you need to level convert the output to 3.3V somehow. Once you do, the esp32 can count this perfectly. The one on my counter at the moment is sitting at an average of 10,000,000.001 Mhz for the last few weeks, with no dynamic disciplining but I would lower my expectations if it needs to be mobile.

This looks promising. I would like to see the code you mentioned as im new to pcnt. Also whats your setupt of the hardware that runs the code? Thanks you so much in advance.
 

Offline xvr

  • Frequent Contributor
  • **
  • Posts: 438
  • Country: ie
    • LinkedIn
Re: Ms resolution timer using dev board
« Reply #27 on: May 13, 2024, 10:03:58 am »
Yes, mesh network can work out for you. IDF has component for creating mesh network from ESP32. It should work without any setup in field, as long as mesh coverage could be archived.
Algorithm is a very simple - all nodes have its own counters, but main node is a synchronizing source. It send a message with its own counter to each node in sequence. When node received this message it send answer message back. Main node received answer message, calaculate difference between its time stamp counter at recieved message and at first (effectively round trip time), divide it by 2 (one way time) and send again to the same node. Node add this time to value of timestamp in very first message and use result as main node time stamp at time when it recieved first message. Then it substract its own timestamp (at time when it recieved first message) and got time shift from its own counter to main one. Now you can add this shift to node time counter at any time and got time on main node. This rime will be the same on all nodes.
Synchronizing procedure should be performed periodically.
 

Offline alepagliaccioTopic starter

  • Contributor
  • Posts: 21
  • Country: it
Re: Ms resolution timer using dev board
« Reply #28 on: May 13, 2024, 10:44:54 am »
Yes, mesh network can work out for you. IDF has component for creating mesh network from ESP32. It should work without any setup in field, as long as mesh coverage could be archived.
Algorithm is a very simple - all nodes have its own counters, but main node is a synchronizing source. It send a message with its own counter to each node in sequence. When node received this message it send answer message back. Main node received answer message, calaculate difference between its time stamp counter at recieved message and at first (effectively round trip time), divide it by 2 (one way time) and send again to the same node. Node add this time to value of timestamp in very first message and use result as main node time stamp at time when it recieved first message. Then it substract its own timestamp (at time when it recieved first message) and got time shift from its own counter to main one. Now you can add this shift to node time counter at any time and got time on main node. This rime will be the same on all nodes.
Synchronizing procedure should be performed periodically.

I understand it more now, still using this method I worry more about a node losing connection and causing a lot of issues with time sync, that's why I was more interested in having an actual good clock on each device instead of relying on external source, making it as dumb as possible to use. Won't the round trip time be different each time because of other stuff happening on the devices maybe? Will try this approach for sure for the syncing part. For the timer itself you just suggest a generic 10mhz ocxo then?
 

Offline mianos

  • Regular Contributor
  • *
  • Posts: 56
  • Country: au
Re: Ms resolution timer using dev board
« Reply #29 on: May 13, 2024, 10:58:57 am »

This looks promising. I would like to see the code you mentioned as im new to pcnt. Also whats your setupt of the hardware that runs the code? Thanks you so much in advance.

Funny, this is only a few months old and already so out of date to me.

Anyway, this is how you organise the PCNT to count full speed, for an unlimited time, gated by another pin using a state machine on the 1pps interrupt by
setting up the hardware PCNT trigger s between the states:

https://github.com/mianos/espc/blob/main/main/Counter.cpp

If you wanted to discipline the internal clock you can also create a 10Mhz ledc pwm signal (not really pwm as at that speed you only get 50% duty cycle, but that's what you want), and strap it to the PCNT and use a 1PPS from a $20 GPS to measure it.

You can probably paste this class into an arduino project to make it a bit simpler to use instead of the esp-idf.

Hardware, I have used this code in the ESP32, ESP32-C6 and just now put the GPS and clock onto an ESP32-C3.
 

Offline mianos

  • Regular Contributor
  • *
  • Posts: 56
  • Country: au
Re: Ms resolution timer using dev board
« Reply #30 on: May 13, 2024, 11:01:44 am »
For the timer itself you just suggest a generic 10mhz ocxo then?

We buy them from fleabay, second hand, recovered from old mobile phone towers (confirmed by me as I bough some more of one of the boards).
New ones are hundreds of dollars.
 

Offline xvr

  • Frequent Contributor
  • **
  • Posts: 438
  • Country: ie
    • LinkedIn
Re: Ms resolution timer using dev board
« Reply #31 on: May 13, 2024, 11:32:29 am »
I understand it more now, still using this method I worry more about a node losing connection and causing a lot of issues with time sync, that's why I was more interested in having an actual good clock on each device instead of relying on external source, making it as dumb as possible to use. Won't the round trip time be different each time because of other stuff happening on the devices maybe? Will try this approach for sure for the syncing part. For the timer itself you just suggest a generic 10mhz ocxo then?
Without synchronization you need not generic one ocxo, but quite good. Like this - https://www.digikey.ie/en/products/detail/ecs-inc/ECOC-9775-10-000-DN-TR/21725274 (cheapest in required precision)
With synchronization you can use generic quartz, even not txco. Just CPU quartz.

https://www.digikey.ie/en/products/filter/oscillators/172?s=N4IgjCBcoEwKwGYqgMZQGYEMA2BnApgDQgD2UA2iABxgwjEwAMYj94AnACyvFjsLs2YAVSECA7G04JOcAGySGczopAxlctjBjs4WxlU5D4e3nLiqWOtlXZ0AusQAOAFyggAyi4BOASwB2AOYgAL687LbIIGiQWHhEpBQgrPZhamBwrNDRGDgExGSQlCwABACyABIAXiCOIK7uAKr%2Bvi4A8uhl%2BJi4AK7e%2BKHEALR02TE%2BvQmFlHqpI4LjUJPTSRCpaWOUJCgAHmR1mtm%2BACbuwywQzm6QIGwuAJ5Og7c9aCEhQA
 

Offline alepagliaccioTopic starter

  • Contributor
  • Posts: 21
  • Country: it
Re: Ms resolution timer using dev board
« Reply #32 on: May 13, 2024, 11:39:32 am »

This looks promising. I would like to see the code you mentioned as im new to pcnt. Also whats your setupt of the hardware that runs the code? Thanks you so much in advance.

Funny, this is only a few months old and already so out of date to me.

Anyway, this is how you organise the PCNT to count full speed, for an unlimited time, gated by another pin using a state machine on the 1pps interrupt by
setting up the hardware PCNT trigger s between the states:

https://github.com/mianos/espc/blob/main/main/Counter.cpp

If you wanted to discipline the internal clock you can also create a 10Mhz ledc pwm signal (not really pwm as at that speed you only get 50% duty cycle, but that's what you want), and strap it to the PCNT and use a 1PPS from a $20 GPS to measure it.

You can probably paste this class into an arduino project to make it a bit simpler to use instead of the esp-idf.

Hardware, I have used this code in the ESP32, ESP32-C6 and just now put the GPS and clock onto an ESP32-C3.

Thank you, from the code you sent me, I'm just confused on what PCNT_INPUT_SIG_IO and PCNT_INPUT_SIG_TRIGGER are connected to, which one to the pps and which one the the 10mhz (from reading the code I think the trigger is the pps) ? why PCNT_HIGH_LIMIT is set at that value if I may ask, what does that represent?

In the end I should get an ocxo and a gps to get it to work
 

Offline alepagliaccioTopic starter

  • Contributor
  • Posts: 21
  • Country: it
Re: Ms resolution timer using dev board
« Reply #33 on: May 13, 2024, 11:40:39 am »
I understand it more now, still using this method I worry more about a node losing connection and causing a lot of issues with time sync, that's why I was more interested in having an actual good clock on each device instead of relying on external source, making it as dumb as possible to use. Won't the round trip time be different each time because of other stuff happening on the devices maybe? Will try this approach for sure for the syncing part. For the timer itself you just suggest a generic 10mhz ocxo then?
Without synchronization you need not generic one ocxo, but quite good. Like this - https://www.digikey.ie/en/products/detail/ecs-inc/ECOC-9775-10-000-DN-TR/21725274 (cheapest in required precision)
With synchronization you can use generic quartz, even not txco. Just CPU quartz.

https://www.digikey.ie/en/products/filter/oscillators/172?s=N4IgjCBcoEwKwGYqgMZQGYEMA2BnApgDQgD2UA2iABxgwjEwAMYj94AnACyvFjsLs2YAVSECA7G04JOcAGySGczopAxlctjBjs4WxlU5D4e3nLiqWOtlXZ0AusQAOAFyggAyi4BOASwB2AOYgAL687LbIIGiQWHhEpBQgrPZhamBwrNDRGDgExGSQlCwABACyABIAXiCOIK7uAKr%2Bvi4A8uhl%2BJi4AK7e%2BKHEALR02TE%2BvQmFlHqpI4LjUJPTSRCpaWOUJCgAHmR1mtm%2BACbuwywQzm6QIGwuAJ5Og7c9aCEhQA
Thank you I will check those out
 

Offline mianos

  • Regular Contributor
  • *
  • Posts: 56
  • Country: au
Re: Ms resolution timer using dev board
« Reply #34 on: May 13, 2024, 12:05:59 pm »
In the code, the 1PPS is  PCNT_INPUT_SIG_TRIGGER:

   ESP_ERROR_CHECK(gpio_isr_handler_add(PCNT_INPUT_SIG_TRIGGER, onePpsEdgeHandler, this));

and PCNT_INPUT_SIG_IO is my 10Mhz input:

 chan_a_config.edge_gpio_num = PCNT_INPUT_SIG_IO;

 

Offline mianos

  • Regular Contributor
  • *
  • Posts: 56
  • Country: au
Re: Ms resolution timer using dev board
« Reply #35 on: May 13, 2024, 12:09:21 pm »
Without synchronization you need not generic one ocxo, but quite good. Like this - https://www.digikey.ie/en/products/detail/ecs-inc/ECOC-9775-10-000-DN-TR/21725274 (cheapest in required precision)
With synchronization you can use generic quartz, even not txco. Just CPU quartz.

Thanks for that one. They look really good for the price and so small. I'm going to pick up up next time I make a digikey order.
 

Offline alepagliaccioTopic starter

  • Contributor
  • Posts: 21
  • Country: it
Re: Ms resolution timer using dev board
« Reply #36 on: May 13, 2024, 12:32:26 pm »
In the code, the 1PPS is  PCNT_INPUT_SIG_TRIGGER:

   ESP_ERROR_CHECK(gpio_isr_handler_add(PCNT_INPUT_SIG_TRIGGER, onePpsEdgeHandler, this));

and PCNT_INPUT_SIG_IO is my 10Mhz input:

 chan_a_config.edge_gpio_num = PCNT_INPUT_SIG_IO;

potentially if the gps is not getting any signal, the timer would still work pretty reliable? I do not understand what the pps does in the code
 

Offline mianos

  • Regular Contributor
  • *
  • Posts: 56
  • Country: au
Re: Ms resolution timer using dev board
« Reply #37 on: May 14, 2024, 12:16:22 am »
100% this depends on your design, as discussed. All I am providing is known working code to run a counter and gate from another line in the esp32 PCNT module.
This is not the code to solve your requirement, just close.
 

Offline alepagliaccioTopic starter

  • Contributor
  • Posts: 21
  • Country: it
Re: Ms resolution timer using dev board
« Reply #38 on: May 14, 2024, 07:10:39 pm »
100% this depends on your design, as discussed. All I am providing is known working code to run a counter and gate from another line in the esp32 PCNT module.
This is not the code to solve your requirement, just close.

Thank you for the code. One last question for now. I do already have an ocxo (CETC CTI OSC5A2B02 10MHz 5V), it's a 5v one. I would need to step down it's output to 3.3v to calculate the pulses from it with my esp32, right? Is a generic buck converter good enough or that would cause me to loses some pulses or something, like what would be a smart way to do this, if not buying an ocxo that operates at 3.3 directly.
 

Offline mianos

  • Regular Contributor
  • *
  • Posts: 56
  • Country: au
Re: Ms resolution timer using dev board
« Reply #39 on: May 14, 2024, 10:18:28 pm »
For my prototype I have a board with a few TLV3501 high speed comparators on it. I run them of the 3.3 and just send the signal from the OCXO into that. My signal is a sine wave.

I think if you have a square wave at 5V you may be able to just use a resistor divider.

Most everything else, like level cheapy level converters have too too much capacitance at 10Mhz,
A buck converter is not a thing for level converters. Maybe you mean something else?
 

Offline alepagliaccioTopic starter

  • Contributor
  • Posts: 21
  • Country: it
Re: Ms resolution timer using dev board
« Reply #40 on: May 14, 2024, 10:23:27 pm »
For my prototype I have a board with a few TLV3501 high speed comparators on it. I run them of the 3.3 and just send the signal from the OCXO into that. My signal is a sine wave.

I think if you have a square wave at 5V you may be able to just use a resistor divider.

Most everything else, like level cheapy level converters have too too much capacitance at 10Mhz,
A buck converter is not a thing for level converters. Maybe you mean something else?

Thank you so much, when the gps module arrives I will try with a resistor divider. Thank you again for all the help. I said buck converter because I'm ignorant on this, sorry, now I understand what I am doing tho ahah
 

Offline alepagliaccioTopic starter

  • Contributor
  • Posts: 21
  • Country: it
Re: Ms resolution timer using dev board
« Reply #41 on: June 30, 2024, 07:00:49 pm »

This looks promising. I would like to see the code you mentioned as im new to pcnt. Also whats your setupt of the hardware that runs the code? Thanks you so much in advance.

Funny, this is only a few months old and already so out of date to me.

Anyway, this is how you organise the PCNT to count full speed, for an unlimited time, gated by another pin using a state machine on the 1pps interrupt by
setting up the hardware PCNT trigger s between the states:

https://github.com/mianos/espc/blob/main/main/Counter.cpp

If you wanted to discipline the internal clock you can also create a 10Mhz ledc pwm signal (not really pwm as at that speed you only get 50% duty cycle, but that's what you want), and strap it to the PCNT and use a 1PPS from a $20 GPS to measure it.

You can probably paste this class into an arduino project to make it a bit simpler to use instead of the esp-idf.

Hardware, I have used this code in the ESP32, ESP32-C6 and just now put the GPS and clock onto an ESP32-C3.


Hello, I got time to check out your code. I wanted then to implement a simple pulse counter using pcnt myself. I have something working (it's pretty basic, no circular buffer or anything). I use a pps and I get raning from 9999999 to 10million pulses each second. After a while for 2 seconds in a row the pulses drop by about 200k pulses or something random like that. I tried running your code and the 10 period (which is actually 11 seconds) seem to work fine. I then try to run your code. I try to disconnect the pps (powering it off) and then after about then seconds powering it back on. It looks like the ocxo counts about 1 minute worth of pulses while the pps pin is disconnected and I can't understand what is going on. Could you input on how should I adapt your code to function like the interrupt on the level pin could be used to store the time when it gets pressed each time? I'm smashing my head over this for days now and I do not understand what I am missing. I tried removing all the state checking for it to always "COLLECT" but that register 1 digit less of pulses for some reason (and it is also off by 270 pulses if we take into account all the digit that it should have (10mil)

Edit: The 1 less digit happens when I make it stay in the collect state, I tried making the loop var to 1 and it prints the right amount of digits but between 1 pulse and the next it doesn't count new pulses, I'm trying to understand pcnt_channel_set_level_action but every change I make it breaks everything and I do not understand what changed. Still do not understand how to make it work with saving the time after 1 pps
« Last Edit: June 30, 2024, 07:24:42 pm by alepagliaccio »
 

Offline mianos

  • Regular Contributor
  • *
  • Posts: 56
  • Country: au
Re: Ms resolution timer using dev board
« Reply #42 on: July 07, 2024, 03:57:37 am »
You have to switch it to measure. The only way to stop it is to use the timer itself and hardware. I am back so maybe I can give you a hand to get it going in the next day or so.
I just fired it all up on my GPSDO yesterday so the code all works.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf