How can I use IRQHandler in Zephyr?

Hello, I would like to use RTC2_IRQHandler in Zephyr environment.



However, each time an RTC interrupt is called, the entire system shuts down and reboots.



How can I use the RTC_IRQHandler?



Here's my source code.

main.cpp

#include <zephyr/kernel.h>
#include <zephyr/irq.h>
#include <Configure.h>
#include <nRF52/RTC.h>

RTC CLOCK;

int main(void)
{
        NRF_GPIO->DIRSET = (1 << GPIO_LED_RED) | (1 << GPIO_LED_GREEN) | (1 << GPIO_LED_BLUE);

        CLOCK.Init(RTC2, 32);
        CLOCK.setEvent(RTC2, RTC_INTERRUPT_COMPARE0);
        CLOCK.setInterrupt(RTC2, RTC_INTERRUPT_COMPARE0);    
        CLOCK.Start(RTC2);
        CLOCK.setCompare(RTC2, RTC_COMPARE0, 3000);
        while(true){
                NRF_GPIO->OUTCLR = (1 << GPIO_LED_RED) | (1 << GPIO_LED_GREEN) | (1 << GPIO_LED_BLUE);
                NRF_GPIO->OUTSET = (1 << GPIO_LED_RED);
                printk("CLOCK : %d\n", CLOCK.getCounter(RTC2));
                k_msleep(1000);
                NRF_GPIO->OUTCLR = (1 << GPIO_LED_RED) | (1 << GPIO_LED_GREEN) | (1 << GPIO_LED_BLUE);
                NRF_GPIO->OUTSET = (1 << GPIO_LED_GREEN);
                printk("CLOCK : %d\n", CLOCK.getCounter(RTC2));
                k_msleep(1000);
                NRF_GPIO->OUTCLR = (1 << GPIO_LED_RED) | (1 << GPIO_LED_GREEN) | (1 << GPIO_LED_BLUE);
                NRF_GPIO->OUTSET = (1 << GPIO_LED_BLUE);
                printk("CLOCK : %d\n", CLOCK.getCounter(RTC2));
                k_msleep(1000);
        }
        return 0;
}

void RTC2_IRQHandler(void){
        if(NRF_RTC2->EVENTS_COMPARE[0] == 1){
                printk("TESt\n ");
                NRF_RTC2->EVENTS_COMPARE[0] = 0;
        }
}

RTC.cpp

#include "RTC.h"

void RTC::Init(uint8_t channel, uint16_t prescaler){
	if(!(NRF_CLOCK->LFCLKSTAT >> 16) & 0x01){
		NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_Xtal;
		NRF_CLOCK->TASKS_LFCLKSTART = 0x01;
		while(!NRF_CLOCK->EVENTS_LFCLKSTARTED){}
	}

    switch(channel){
        case RTC0:
            NRF_RTC0->TASKS_CLEAR = 1;
		    NRF_RTC0->TASKS_STOP = 1;
		    NRF_RTC0->PRESCALER = 32;
            printk("Settings : %d %d\n", NRF_RTC0->PRESCALER, prescaler);
            break;
        case RTC1:
            NRF_RTC1->TASKS_CLEAR = 1;
		    NRF_RTC1->TASKS_STOP = 1;
		    NRF_RTC1->PRESCALER = prescaler;
            break;
        case RTC2:
            NRF_RTC2->TASKS_CLEAR = 1;
		    NRF_RTC2->TASKS_STOP = 1;
		    NRF_RTC2->PRESCALER = prescaler;
            break;
        default:
            break;
    }
}
void RTC::Start(uint8_t channel){
    switch(channel){
        case RTC0:
            NRF_RTC0->TASKS_START = 1;
            break;
        case RTC1:
            NRF_RTC1->TASKS_START = 1;
            break;
        case RTC2:
            NRF_RTC2->TASKS_START = 1;
            break;
        default:
            break;
    }
}
void RTC::Stop(uint8_t channel){
    switch(channel){
        case RTC0:
            NRF_RTC0->TASKS_STOP = 1;
            NRF_RTC0->TASKS_CLEAR = 1;
            break;
        case RTC1:
            NRF_RTC1->TASKS_STOP = 1;
            NRF_RTC1->TASKS_CLEAR = 1;
            break;
        case RTC2:
            NRF_RTC2->TASKS_STOP = 1;
            NRF_RTC2->TASKS_CLEAR = 1;
            break;
        default:
            break;
    }
}
void RTC::setInterrupt(uint8_t channel, uint32_t interrupt){
    switch(channel){
        case RTC0:
            NRF_RTC0->INTENSET = interrupt;
            NVIC_SetPriority(RTC0_IRQn, 5);
	        NVIC_EnableIRQ(RTC0_IRQn);
            break;
        case RTC1:
            NRF_RTC1->INTENSET = interrupt;
            NVIC_SetPriority(RTC1_IRQn, 5);
	        NVIC_EnableIRQ(RTC1_IRQn);
            break;
        case RTC2:
            NRF_RTC2->INTENSET = interrupt;
            NVIC_SetPriority(RTC2_IRQn, 5);
	        NVIC_EnableIRQ(RTC2_IRQn);
            break;
        default:
            break;
    }
}
void RTC::clearInterrupt(uint8_t channel, uint32_t interrupt){
    switch(channel){
        case RTC0:
            NRF_RTC0->INTENCLR = interrupt;
            break;
        case RTC1:
            NRF_RTC1->INTENCLR = interrupt;
            break;
        case RTC2:
            NRF_RTC2->INTENCLR = interrupt;
            break;
        default:
            break;
    }
}
void RTC::setEvent(uint8_t channel, uint32_t event){
    switch(channel){
        case RTC0:
            NRF_RTC0->EVTENSET = event;
            break;
        case RTC1:
            NRF_RTC1->EVTENSET = event; 
            break;
        case RTC2:
            NRF_RTC2->EVTENSET = event;
            break;
        default:
            break;
    }
}
void RTC::clearEvent(uint8_t channel, uint32_t event){
    switch(channel){
        case RTC0:
            NRF_RTC0->EVTENCLR = event;
            break;
        case RTC1:
            NRF_RTC1->EVTENCLR = event;
            break;
        case RTC2:
            NRF_RTC2->EVTENCLR = event;
            break;
        default:
            break;
    }
}
void RTC::setCompare(uint8_t channel, uint8_t comp_no, uint32_t cc){
    if(comp_no > 3){
        return;
    }
    switch(channel){
        case RTC0:
            NRF_RTC0->CC[comp_no] = cc;
            break;
        case RTC1:
            NRF_RTC1->CC[comp_no] = cc;
            break;
        case RTC2:
            NRF_RTC2->CC[comp_no] = cc;
            break;
        default:
            break;
    }
}
uint32_t RTC::getCounter(uint8_t channel){
    static uint32_t output=0;
    switch(channel){
        case RTC0:
            output = NRF_RTC0->COUNTER;
            break;
        case RTC1:
            output = NRF_RTC1->COUNTER;
            break;
        case RTC2:
            output = NRF_RTC2->COUNTER;
            break;
        default:
            output = NRF_RTC0->COUNTER;
            break;
    }
    return output;
}

  • Here is my RTT viewer

    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0
    CLOCK : 993
    CLOCK : 1986
    CLOCK : 2979
    *** Booting Zephyr OS build v3.3.99-ncs1-1 ***
    RIDERLOG LTE Device Firmware : 0.9.5
    Board Version : Release 5
    CLOCK : 0

  • I don't see the IRQ_CONNECT() macro that connects the handler function to the actual interrupt.

    I suspect the device resets because it runs into an unhandled interrupt.

    You should also use the irq_enable() funtion instead of calling those NVIC_xxx functions directly. They can do more in zephyr OS.

  • Thank you. I solved it using IRQ_DIRECT_CONNECT.

Related