Calling k_event_post from Interrupt context in zephyr RTOS

Hello All, 

I am using RTC interrupt handle on the NRF5340 kit to set the event using k_event_post API from the interrupt context and when I try to wait on this event in the main function using k_event_wait it is never set the event and when I tried to debug using the NRF5340 kit debugger it works using the debugger but when the NRF controller run in freerun mode it didn't work.

and here is the example I am using to test this scenario. 

#include <zephyr.h>
#include <sys/util.h>
#include <sys/printk.h>
#include <string.h>
#include <stdlib.h>
#include <nrfx_rtc.h>
#include <nrfx_timer.h>
#include <nrfx_dppi.h>
#include <nrfx_gpiote.h>
#include <hal/nrf_gpiote.h>
#include <hal/nrf_timer.h>
#include <hal/nrf_gpio.h>


#define LED_0 28
#define LED_1 29
#define LED_2 30

#define COMPARE_COUNTERTIME 3

#define RTC_EVENT_PENDING  (1 << 2)

K_EVENT_DEFINE(RTC_events);

const nrfx_rtc_t rtc = NRFX_RTC_INSTANCE(0);
uint32_t  event_mask;

static void rtc_handler(nrfx_rtc_int_type_t int_type)
{
	if (int_type == NRFX_RTC_INT_COMPARE0) {
		nrf_gpio_pin_toggle(LED_0);
	} else if (int_type == NRFX_RTC_INT_TICK) {
		nrf_gpio_pin_toggle(LED_1);
		printk("Event LED_1!\n");
		k_event_post(&RTC_events, RTC_EVENT_PENDING);
	}
}

static void rtc_config(void)
{
	uint32_t err_code;

	nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
	config.prescaler = 4095;
	err_code = nrfx_rtc_init(&rtc, &config, rtc_handler);
	if (err_code != NRFX_SUCCESS) {
		printk("Failure in setup\n");
		return;
	}

	nrfx_rtc_tick_enable(&rtc, true);
	err_code = nrfx_rtc_cc_set(&rtc, 0, COMPARE_COUNTERTIME * 8, true);
	if (err_code != NRFX_SUCCESS) {
		printk("Failure in setup\n");
		return;
	}

	nrfx_rtc_enable(&rtc);
}

static void manual_isr_setup()
{
	IRQ_DIRECT_CONNECT(RTC0_IRQn, 0, nrfx_rtc_0_irq_handler, 0);
	irq_enable(RTC0_IRQn);
}


void main(void)
{
	printk("Starting rtc sample!\n");
	nrf_gpio_cfg_output(LED_0);
	nrf_gpio_cfg_output(LED_1);
	nrf_gpio_cfg_output(LED_2);
	rtc_config();
	manual_isr_setup();
while(1)
	{
	uint32_t   event_mask = k_event_wait(&RTC_events, RTC_EVENT_PENDING, true , K_FOREVER);	
    printk("Event Started!\n");
	if(event_mask & RTC_EVENT_PENDING)
    	{
            nrf_gpio_pin_toggle(LED_2);
			printk("Event LED_2!\n");
    	}
	}
}

Thanks, 

Related