Timer doesnt work on NRF5340

Hello everyone,

Im using nrf5340dk for the first time, i have built project for nrf5340dk_cpuapp and i think code is good but for some reasone i dont see a led diodes turnin on. 

/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include <nrfx_timer.h>

/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS   1000

/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)
#define LED1_NODE DT_ALIAS(led1)
#define LED2_NODE DT_ALIAS(led2)
#define LED3_NODE DT_ALIAS(led3)

static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(LED1_NODE, gpios);
static const struct gpio_dt_spec led2 = GPIO_DT_SPEC_GET(LED2_NODE, gpios);
static const struct gpio_dt_spec led3 = GPIO_DT_SPEC_GET(LED3_NODE, gpios);



// Timer configuration for TIMER2
static const nrfx_timer_config_t timer2_config = {
	.frequency = NRF_TIMER_FREQ_1MHz,
	.mode = NRF_TIMER_MODE_TIMER,
	.bit_width = NRF_TIMER_BIT_WIDTH_32,
	.interrupt_priority = 7,
	.p_context = NULL
};
// Timer instance handle for TIMER2
static const nrfx_timer_t timer2 = NRFX_TIMER_INSTANCE(2);

void timer2_event_handler(nrf_timer_event_t event_type, void *p_context)
{
	if (event_type == NRF_TIMER_EVENT_COMPARE0) {
		// Only led0 ON (active low), others OFF
		gpio_pin_set_dt(&led0, 0); // ON
		gpio_pin_set_dt(&led1, 1); // OFF
		gpio_pin_set_dt(&led2, 1); // OFF
		gpio_pin_set_dt(&led3, 1); // OFF
	}
	else if (event_type == NRF_TIMER_EVENT_COMPARE1) {
		// Only led1 ON (active low), others OFF
		gpio_pin_set_dt(&led0, 1); // OFF
		gpio_pin_set_dt(&led1, 0); // ON
		gpio_pin_set_dt(&led2, 1); // OFF
		gpio_pin_set_dt(&led3, 1); // OFF
	}
	else if (event_type == NRF_TIMER_EVENT_COMPARE2) {
		// Only led2 ON (active low), others OFF
		gpio_pin_set_dt(&led0, 1); // OFF
		gpio_pin_set_dt(&led1, 1); // OFF
		gpio_pin_set_dt(&led2, 0); // ON
		gpio_pin_set_dt(&led3, 1); // OFF
	}
	else if (event_type == NRF_TIMER_EVENT_COMPARE3) {
		// Only led3 ON (active low), others OFF
		gpio_pin_set_dt(&led0, 1); // OFF
		gpio_pin_set_dt(&led1, 1); // OFF
		gpio_pin_set_dt(&led2, 1); // OFF
		gpio_pin_set_dt(&led3, 0); // ON
		nrfx_timer_clear(&timer2); // Clears the timer count to 0
	}
}
int main(void)
{
    int ret;

    if (!gpio_is_ready_dt(&led0)) {
        return 0;
    }
    if (!gpio_is_ready_dt(&led1)) {
        return 0;
    }
    if (!gpio_is_ready_dt(&led2)) {
        return 0;
    }
    if (!gpio_is_ready_dt(&led3)) {
        return 0;
    }
    nrfx_timer_init(&timer2, &timer2_config, timer2_event_handler);

    ret = gpio_pin_configure_dt(&led0, GPIO_OUTPUT_INACTIVE);
    if (ret < 0) {
        return 0;
    }
    ret = gpio_pin_configure_dt(&led1, GPIO_OUTPUT_INACTIVE);
    if (ret < 0) {
        return 0;
    }
    ret = gpio_pin_configure_dt(&led2, GPIO_OUTPUT_INACTIVE);
    if (ret < 0) {
        return 0;
    }
    ret = gpio_pin_configure_dt(&led3, GPIO_OUTPUT_INACTIVE);
    if (ret < 0) {
        return 0;
    }

    nrfx_timer_compare(&timer2, NRF_TIMER_CC_CHANNEL0, 500000, true);
    nrfx_timer_compare(&timer2, NRF_TIMER_CC_CHANNEL1, 1500000, true);
    nrfx_timer_compare(&timer2, NRF_TIMER_CC_CHANNEL2, 2500000, true);
    nrfx_timer_compare(&timer2, NRF_TIMER_CC_CHANNEL3, 3500000, true);

    nrfx_timer_enable(&timer2);
    while (1) {
        k_msleep(SLEEP_TIME_MS);
    }
    return 0;
}
does anybode know how can i fix this? 

Thanks in advance.

Related