Dear nRF DevZone,
I have question regarding Zephyr k_sleep() function and NRF52840 timers.
I am developing an application for nRF52840 SOC and for that I am using nRF Connect SDK V1.9.1. in VS Code IDE.
As this is a custom board that was developed by another person and given to me for sw development I am currently going through the board bring-up process.
I used simple blinky as an example just to blink LED using GPIO P1.04..
As this is a custom board, I followed the process described at Nordic Webinar (https://www.youtube.com/watch?v=KSivO9Cf1TE) and based my devicetree on NRF52840 DK board.I managed to set up the board and flash an LED, but the problem is that k_sleep() and k_msleep() are not working properly.
If I give it a 1000 ms Delay, it takes more than 2000ms.
I checked the schematic for the developed board and it is using 32Mhz 20 ppm crystal oscilator.
Now I do not know what could be the problem, is it hardware or software.
Are the timers maybe configured wrong?
Could you give me suggestion where should I start looking as the cause of the problem.
Below is the code of this basic example.
Thank you in advance.
Sincerely,
Matej
#include <zephyr.h>
#include <device.h>
#include <devicetree.h>
#include <drivers/gpio.h>
/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS 1000
/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)
#if DT_NODE_HAS_STATUS(LED0_NODE, okay)
#define LED0 DT_GPIO_LABEL(LED0_NODE, gpios)
#define PIN DT_GPIO_PIN(LED0_NODE, gpios)
#define FLAGS DT_GPIO_FLAGS(LED0_NODE, gpios)
#else
/* A build error here means your board isn't set up to blink an LED. */
#error "Unsupported board: led0 devicetree alias is not defined"
#define LED0 ""
#define PIN 0
#define FLAGS 0
#endif
void main(void)
{
const struct device *dev;
bool led_is_on = true;
int ret;
dev = device_get_binding(LED0);
if (dev == NULL) {
return;
}
ret = gpio_pin_configure(dev, PIN, GPIO_OUTPUT | GPIO_ACTIVE_LOW);
if (ret < 0) {
return;
}
while (1) {
gpio_pin_set(dev, PIN, (int)led_is_on);
led_is_on = !led_is_on;
k_msleep((SLEEP_TIME_MS));
}
}