Help porting code from AVR to ARM

Hi, I'm just dipping my toe into the word of ARM MCUs via the arduino MKR and was wondering if someone with experience might be able to help me update some code which uses avr/io.h and avr/interrupt.h to implement timers and interupts? Example below, many thanks in advance.

void Meter::startTimer()
{
	overflow = 0;

	oldSREG = SREG;	// AR - save status register
	cli();			// AR - Disable interrupts
	TCNT1 = 0;
    SREG = oldSREG;	// AR - Restore status register

	TCCR1A = 0x00;	// OC1A/OC1B Disconnected + Normal Mode
    TIMSK1 = 0x01;	// TOIE1;	 // Enable interrupt overflow flag
    TCCR1B = 0x02;  // Normal mode + clk/8
    				// For UNO: 1 bit  =  8/16e6   = 500nS
					
}

Think you’ll need to at least put your whole foot in ..

Think I would look at the data sheets for the two processors and see how to implement on the new processor.
Probably easier , if you know what is required , to start over .

Hi Hammy, I think you're right.... it will have to be an immersion :slight_smile: I will look at availble books and have already started looking at the datasheet. My aim here was to try and something that I have previously used working as a small step and learning experience.

the arduino IDE approach of using libraries supporting interrupts and other I/O functions is the conventional way to make code portable.

I agree, in this case the code speed was optimised by writing directly to the registers so hoping to learn how to do the same in ARM as I understand its quite different?

i doubt configuration requires optimization.

Knowning just that the processor is an "ARM" is not sufficient for this task. Unlike an AVR device, ARM is just the processor core. Chip designers embed the ARM core and surround it with peripheral devices … Timers, USARTS, SPI, I2C, I2S, GPIO, etc. Different chips from different manufacturers implement this arrangement in different ways.

So, as already stated, you'll need to study the AVR code to understand exactly what it does. Then you'll need to study the datasheet for your particular ARM-based target processor to see how to implement the same functionality.

Another thought …. since most ARM-based chips run much faster than your typical AVR processor, you may be able to just do what you need using more generic C++ code.

Ah, I didn't realise the periferals are not part of the ARM architecture.... and good point about speed, perhaps I should try directly in Arduino C first and then come back to learn more. Thanks for the thoughts folks.

PS the MCU in this case is the SAMD21 Cortex-M0

there may be no need to optimize processing speeds. a samd21 runs at 48 MHz, an arduino uno at 16 MHz

I would focus on the original code. Ask yourself why is it using hardware timers directly, what is being timed? Could that have been done with millis() or micros()? If you can remove the use of timers and interrupts in favour of standard Arduino functions, then it will probably work on SAMD21 with little or no adjustment.

If the use of timers can't be replaced with standard functions, start looking for a library or libraries that can replace the code that access the timers directly, and check that those libraries run on samd21 as well. That might not be easy to find, but spending that time in order to avoid having to learn the intricacies of the samd21 timers would be worth it. What if you decide to migrate to another MCU in future? You could find yourself in the same situation again.