Timers for Nano 33 BLE??

Gentlemen:

I have an Arduino Nano 33 BLE (plain, not "Sense") and need to use the timers and timer interrupts. Unfortunately, I guess every Arduino version has a radically different processor and the code is entirely different.

I tried using timer commands that I used for other Arduinos, like TCCR1B, but it doesn't like it. I have scoured the Web for examples of using timers on a Nano BLE, but I get almost nothing. The Arduino site has a search function that is virtually useless.

All I want to do is run timer1 at full clock speed (64 MHz) with an overflow interrupt handler and a way to read and write the clock value. I can handle it from there.

Where do I find some information and sample code about the timers for an Arduino Nano 33 BLE? Thanks for any help you can provide.

Don

If you are used to read/writing the registers directly, just check out the datasheet for the processor. The Nano 33 uses the Cortex M4.

So there are no "Arduino" timer libraries for this product? Wow, what's the use of using an Arduino? I had been wondering if you could write processor registers directly, or if that would mess up any Arduino code.

While we're on the subject, I'll also be using external interrupts. Can I expect the same lack of Arduino support for that as well?

Don

blh64:
If you are used to read/writing the registers directly, just check out the datasheet for the processor. The Nano 33 uses the Cortex M4.

No, That's a wild goose chase.

The Nano BLE's microcontroller is "an nRF52840 from Nordic Semiconductors, a 32-bit ARM® Cortex™-M4 CPU running at 64 MHz"

Compare that with "Adafruit ItsyBitsy M4 Express featuring the Microchip ATSAMD51! Small, powerful, with a ultra fast ATSAMD51 Cortex M4 processor running at 120 MHz"

Or "Teensy 3.6 features a (NXP mk66fx1m0vmd18) 32 bit 180 MHz ARM Cortex-M4 processor with floating point unit"

All 3 chips have a Cortex M4, but they are made by different manufacturers. The Cortex M4 is the CPU inside those chips, but peripherals like timers are not part of the CPU, and will be completely different between chips from different manufacturers. You won't find anything about timers in the Cortex M4 data sheet. You need to look at the nRF52840 data sheet for that, or the ATSAMD51 or mk66fx1m0vmd18 data sheet if you are using one of those. And the details will be different for each.

All I want to do is run timer1 at full clock speed (64 MHz) with an overflow interrupt handler and a way to read and write the clock value. I can handle it from there.

Why?

Please explain, because this could be an "X-Y problem".

I've done the same timing events with other processors: an ATTiny a few years ago (direct, non-Arduino) and a Silabs Gecko more recently. Figuring out all the registers, flags, enables, enables-of-enables and other esoteric BS was a nightmare.

I switched to Arduino for small prototypes because it was so much easier to use. Now I find out that they have apparently abandoned timers and interrupts for their Nano boards and I'm back to looking up all that arcane mess. I'm feeling kind of gypped.

Don

I'm feeling kind of gypped.

Where's the harm in letting us try to help you? That's why you posted here, isn't it? Try to answer my question from post #4.

OK, here's what I've got:

I need to time the period of seven different square waves on seven GPIO inputs, simultaneously, rising edge to rising edge. I don't have seven independent timers so I just use a single 16-bit timer that runs all the time by zeroing it every time it overflows. In the overflow interrupt I also increment an overflow integer

unsigned long timer_msb;
timer_msb ++;

So that I get a clock with a much bigger capacity. When a GPIO interrupts (rising edge), I calculate the period as

period = timer1 + (timer_msb << 16) - old_period;
old_period = period;

where timer1 is the current timer value. By just subtracting two arbitrary clock values, I don't need independent timers for each wave. I need to measure the seven waves for only a minute or so at a time, so the timer_msb never fills up...I zero it when measurement is done. I have used this method with other processors and it works well.

Is there an easy way to set up this timer? I need to know the name of the ISR and where to find the timer setup registers and the timer current value register. And thanks so much for the help.

Don

I need to time the period of seven different square waves on seven GPIO inputs, simultaneously, rising edge to rising edge

Why?

You've probably heard the theory that if you ask "why?" five times, you get the real reason. I don't know why they say it's five times... But that's two.

Why does it matter? It's for an invention. It's patent pending now, but I'm still a little wary of publishing too much about it until the patent issues.

I average the periods over time (about 100 samples per average) then Bluetooth the seven numbers to another Nano that is involved in adjusting the wave periods. All this unit does is measure and send.

Don

It matters because of the X-Y problem. But if the answer is a secret, no point persuing that, we will never know.

So what's the typical range of periods the square waves might have, and what precision of measurement do you need?

From what you've said so far, I guess you are aiming for precision of 1/64,000,000. Then you average 100 readings. It's that to gain further precision or because there is a natural variation the the periods being measured?

I don't understand what you mean by "X-Y problem". Perhaps you can answer the question "why?" this time.

I've already figured out what precision I need. I'm a mechanical engineer with 35 years' experience in industry. I have six US patents in my own name. You can make sport of it, but I'm quite serious about the need to keep things secret, both for legal reasons and because of competition. Your "twenty questions" game is frankly a little creepy, though I know you mean well.

As I mentioned before, I have already done this on a Silabs Gecko. I switched to the Arduino because the Bluetooth was much easier to figure out and use and the footprint is much smaller. Here is part of the ISR code from the Gecko program (it's a 32-bit processor):

#define TIMER_VALUE (TIMER0->CNT + (TIMER1->CNT << 16))

void GPIO_ODD_IRQHandler(void)
{
  uint32_t iflags;
  if(GPIO->IF & (1 << 9))												
  {
      GPIO_IntClear(1 << 9);
      testperiod = TIMER_VALUE - old_acount;							
      old_acount = TIMER_VALUE;											
      error = testperiod - ACORRECT;									
          	if(abs(error) < ACORRECT * 0.03)							
          	{
          		atotal += testperiod;									
          		acount++;
          		if(acount >= 10)
          		{
          			aperiod = atotal / 10;								
          			acount = 0;											
          			atotal = 0;
          			abad = 0;
          		}
          	}
          	else abad ++;

    }
...

As I said, this code already does exactly what I need. I'm just finding it difficult to find the equivalent registers, etc. for the Arduino Nano. I have looked for the equivalent of a Reference Manual for the Nordic, but all I have been able to find is this:

https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Ftimer.html

Which is very cryptic to me. If anyone can point me to a better source I would be forever grateful.

Don

Just to reassure you I'm not making this stuff up:

For the 33 BLE, at least, I think arduino is relying on the libraries /Api's supplied by ARM in the form of the mBed OS.

I managed to get an ISR being called every 20mS very simply, by using the Ticker class from mBed.
You can define the period down to the uS.