#define LED 1
#define MEM32(address) (*(volatile uint32_t*)(address))
#define GPIO_OUT_CLR MEM32(0xd0000000+0x018)
#define GPIO_OUT_XOR MEM32(0xd0000000+0x01c)
#define TIMER_REG(offset) MEM32(0x40054000+offset)
#define TIMELR TIMER_REG(0x0c)
#define ALARM1 TIMER_REG(0x14)
#define ARMED TIMER_REG(0x20)
#define INTR TIMER_REG(0x34)
#define INTE TIMER_REG(0x38)
#define NVIC_ISER MEM32(0xe0000000 + 0xe100)
#define VTOR MEM32(0xe0000000 + 0xed08)
#define TIMER_IRQ_1 1
void alarm1_irq() {
ALARM1 = TIMELR + 250000;
GPIO_OUT_XOR = bit(LED);
// clear the interrupt in INTR:
INTR = bit(1);
}
void setup() {
Serial1.begin(115200);
Serial1.println(VTOR, HEX);
pinMode(LED, OUTPUT);
for (int i = 2; i < 8; i++) {
pinMode(i, OUTPUT);
}
// 1. Define the exception/interrupt handler
MEM32(VTOR + (16 + TIMER_IRQ_1) * 4) = (uint32_t)alarm1_irq;
// 2. Enable the interrupt in the Timer (INTE) register
INTE |= bit(1);
// 3. Enable the NVIC interrupt (NVIC_ISER)
NVIC_ISER = bit(TIMER_IRQ_1);
// 4. Set the ALARM1 register to the target time
ALARM1 = TIMELR + 250000;
}
void loop() {
for (int i = 2; i < 8; i++) {
digitalWrite(i, !digitalRead(i));
delay(50);
}
}