earlephilhower / arduino-pico

Raspberry Pi Pico Arduino core, for all RP2040 and RP2350 boards
GNU Lesser General Public License v2.1
2.03k stars 421 forks source link

Timer in rp2350 , raspberri pi pico 2 , not working #2421

Closed manecolooper closed 1 month ago

manecolooper commented 1 month ago

The following code that works in rp2040 does not compile when switching to rp2350

include <hardware/timer.h>

include <hardware/irq.h>

define ALARM_NUM 1

define ALARM_IRQ TIMER_IRQ_1

define ALARM_FREQ 32000

uint32_t alarmPeriod;

int ledval; static void alarm_irq(void) { hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM); alarm_in_us_arm(alarmPeriod);

ledval = !ledval; digitalWrite(7, ledval); }

static void alarm_in_us_arm(uint32_t delay_us) { uint64_t target = timer_hw->timerawl + delay_us; timer_hw->alarm[ALARM_NUM] = (uint32_t) target; }

static void alarm_in_us(uint32_t delay_us) { hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM); irq_set_exclusive_handler(ALARM_IRQ, alarm_irq); irq_set_enabled(ALARM_IRQ, true); alarm_in_us_arm(delay_us); }

void dumpTimerHwReg (char *str, unsigned long reg) { SerialUSB.print(str); SerialUSB.print("\t"); SerialUSB.print(reg); SerialUSB.print("\t"); SerialUSB.print(reg, HEX); SerialUSB.print("\n"); }

void dumpTimerHwRegs() { SerialUSB.println("-------------------------------"); dumpTimerHwReg("timehr", timer_hw->timehr); dumpTimerHwReg("timelr", timer_hw->timelr); dumpTimerHwReg("alarm0", timer_hw->alarm[0]); dumpTimerHwReg("alarm1", timer_hw->alarm[1]); dumpTimerHwReg("alarm2", timer_hw->alarm[2]); dumpTimerHwReg("alarm3", timer_hw->alarm[3]); dumpTimerHwReg("armed", timer_hw->armed); dumpTimerHwReg("timerawh", timer_hw->timerawh); dumpTimerHwReg("timerahl", timer_hw->timerawl); dumpTimerHwReg("dbgpause", timer_hw->dbgpause); dumpTimerHwReg("pause", timer_hw->pause); dumpTimerHwReg("intr", timer_hw->intr); dumpTimerHwReg("inte", timer_hw->inte); dumpTimerHwReg("intf", timer_hw->intf); dumpTimerHwReg("ints", timer_hw->ints); SerialUSB.println(""); }

void setup () { pinMode (7, OUTPUT); pinMode (LED_BUILTIN, OUTPUT); alarmPeriod = 1000000/ALARM_FREQ; alarm_in_us(alarmPeriod); SerialUSB.begin(9600); SerialUSB.println("Hello Start"); }

int ledval2; void loop () { ledval2 = !ledval2; digitalWrite(LED_BUILTIN, ledval2); delay(1000); dumpTimerHwRegs(); SerialUSB.print("Alarm Frequency: "); SerialUSB.print(ALARM_FREQ); SerialUSB.print("Period: "); SerialUSB.println(alarmPeriod); SerialUSB.println(""); }

referenced article

https://emalliab.wordpress.com/2021/04/18/raspberry-pi-pico-arduino-core-and-timers/

earlephilhower commented 1 month ago

You're using raw SDK calls and bit manipulation in registers (and SerialUSB which is not correct here), so there's nothing core related here. I suggest you try the raw SDK or the RPI forums for assistance.

manecolooper commented 1 month ago

Thanks so much! Indeed was a case of checking the rp2350 examples , in low level timer the changes needed are two lines

define ALARM_NUM 0

define ALARM_IRQ timer_hardware_alarm_get_irq_num(timer_hw, ALARM_NUM)