2016-11-22 17:03:32 +01:00
|
|
|
/*
|
2017-02-13 16:31:53 +01:00
|
|
|
* Copyright (c) 2016-2017 Nordic Semiconductor ASA
|
2018-10-15 18:04:21 +02:00
|
|
|
* Copyright (c) 2018 Intel Corporation
|
2016-11-22 17:03:32 +01:00
|
|
|
*
|
2017-01-19 02:01:01 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-11-22 17:03:32 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <soc.h>
|
2019-06-25 21:53:47 +02:00
|
|
|
#include <drivers/clock_control.h>
|
2019-01-15 10:27:20 +01:00
|
|
|
#include <drivers/clock_control/nrf_clock_control.h>
|
2019-06-21 18:55:37 +02:00
|
|
|
#include <drivers/timer/system_timer.h>
|
2020-11-04 11:57:38 +01:00
|
|
|
#include <drivers/timer/nrf_rtc_timer.h>
|
kernel: tickless: Add tickless kernel support
Adds event based scheduling logic to the kernel. Updates
management of timeouts, timers, idling etc. based on
time tracked at events rather than periodic ticks. Provides
interfaces for timers to announce and get next timer expiry
based on kernel scheduling decisions involving time slicing
of threads, timeouts and idling. Uses wall time units instead
of ticks in all scheduling activities.
The implementation involves changes in the following areas
1. Management of time in wall units like ms/us instead of ticks
The existing implementation already had an option to configure
number of ticks in a second. The new implementation builds on
top of that feature and provides option to set the size of the
scheduling granurality to mili seconds or micro seconds. This
allows most of the current implementation to be reused. Due to
this re-use and co-existence with tick based kernel, the names
of variables may contain the word "tick". However, in the
tickless kernel implementation, it represents the currently
configured time unit, which would be be mili seconds or
micro seconds. The APIs that take time as a parameter are not
impacted and they continue to pass time in mili seconds.
2. Timers would not be programmed in periodic mode
generating ticks. Instead they would be programmed in one
shot mode to generate events at the time the kernel scheduler
needs to gain control for its scheduling activities like
timers, timeouts, time slicing, idling etc.
3. The scheduler provides interfaces that the timer drivers
use to announce elapsed time and get the next time the scheduler
needs a timer event. It is possible that the scheduler may not
need another timer event, in which case the system would wait
for a non-timer event to wake it up if it is idling.
4. New APIs are defined to be implemented by timer drivers. Also
they need to handler timer events differently. These changes
have been done in the HPET timer driver. In future other timers
that support tickles kernel should implement these APIs as well.
These APIs are to re-program the timer, update and announce
elapsed time.
5. Philosopher and timer_api applications have been enabled to
test tickless kernel. Separate configuration files are created
which define the necessary CONFIG flags. Run these apps using
following command
make pristine && make BOARD=qemu_x86 CONF_FILE=prj_tickless.conf qemu
Jira: ZEP-339 ZEP-1946 ZEP-948
Change-Id: I7d950c31bf1ff929a9066fad42c2f0559a2e5983
Signed-off-by: Ramesh Thomas <ramesh.thomas@intel.com>
2017-02-06 04:37:19 +01:00
|
|
|
#include <sys_clock.h>
|
2019-09-19 13:25:18 +02:00
|
|
|
#include <hal/nrf_rtc.h>
|
2018-10-15 18:04:21 +02:00
|
|
|
#include <spinlock.h>
|
2017-05-08 07:59:37 +02:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
|
|
|
|
#define EXT_CHAN_COUNT CONFIG_NRF_RTC_TIMER_USER_CHAN_COUNT
|
|
|
|
#define CHAN_COUNT (EXT_CHAN_COUNT + 1)
|
|
|
|
|
2018-10-15 18:04:21 +02:00
|
|
|
#define RTC NRF_RTC1
|
2020-05-21 13:46:48 +02:00
|
|
|
#define RTC_IRQn NRFX_IRQ_NUMBER_GET(RTC)
|
2020-10-06 09:25:12 +02:00
|
|
|
#define RTC_LABEL rtc1
|
2020-11-04 11:57:38 +01:00
|
|
|
#define RTC_CH_COUNT RTC1_CC_NUM
|
|
|
|
|
|
|
|
BUILD_ASSERT(CHAN_COUNT <= RTC_CH_COUNT, "Not enough compare channels");
|
2017-05-08 07:59:37 +02:00
|
|
|
|
2019-11-23 23:36:51 +01:00
|
|
|
#define COUNTER_SPAN BIT(24)
|
|
|
|
#define COUNTER_MAX (COUNTER_SPAN - 1U)
|
|
|
|
#define COUNTER_HALF_SPAN (COUNTER_SPAN / 2U)
|
2019-04-23 15:08:00 +02:00
|
|
|
#define CYC_PER_TICK (sys_clock_hw_cycles_per_sec() \
|
2018-10-15 18:04:21 +02:00
|
|
|
/ CONFIG_SYS_CLOCK_TICKS_PER_SEC)
|
2020-03-04 11:23:16 +01:00
|
|
|
#define MAX_TICKS ((COUNTER_HALF_SPAN - CYC_PER_TICK) / CYC_PER_TICK)
|
2019-11-22 14:47:07 +01:00
|
|
|
#define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK)
|
2019-03-07 23:18:17 +01:00
|
|
|
|
|
|
|
static struct k_spinlock lock;
|
2017-05-08 07:59:37 +02:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static uint32_t last_count;
|
2016-11-22 17:03:32 +01:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
struct z_nrf_rtc_timer_chan_data {
|
|
|
|
z_nrf_rtc_timer_compare_handler_t callback;
|
|
|
|
void *user_context;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct z_nrf_rtc_timer_chan_data cc_data[CHAN_COUNT];
|
|
|
|
static atomic_t int_mask;
|
|
|
|
static atomic_t alloc_mask;
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static uint32_t counter_sub(uint32_t a, uint32_t b)
|
2017-05-08 07:59:37 +02:00
|
|
|
{
|
2018-10-15 18:04:21 +02:00
|
|
|
return (a - b) & COUNTER_MAX;
|
2017-05-08 07:59:37 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
static void set_comparator(uint32_t chan, uint32_t cyc)
|
2017-05-08 07:59:37 +02:00
|
|
|
{
|
2020-11-04 11:57:38 +01:00
|
|
|
nrf_rtc_cc_set(RTC, chan, cyc & COUNTER_MAX);
|
2017-05-08 07:59:37 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
static uint32_t get_comparator(uint32_t chan)
|
2020-03-04 11:23:16 +01:00
|
|
|
{
|
2020-11-04 11:57:38 +01:00
|
|
|
return nrf_rtc_cc_get(RTC, chan);
|
2020-03-04 11:23:16 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
static void event_clear(uint32_t chan)
|
2020-03-04 11:23:16 +01:00
|
|
|
{
|
2020-11-04 11:57:38 +01:00
|
|
|
nrf_rtc_event_clear(RTC, RTC_CHANNEL_EVENT_ADDR(chan));
|
2020-03-04 11:23:16 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
static void event_enable(uint32_t chan)
|
2020-03-04 11:23:16 +01:00
|
|
|
{
|
2020-11-04 11:57:38 +01:00
|
|
|
nrf_rtc_event_enable(RTC, RTC_CHANNEL_INT_MASK(chan));
|
2020-03-04 11:23:16 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
static void event_disable(uint32_t chan)
|
2020-03-04 11:23:16 +01:00
|
|
|
{
|
2020-11-04 11:57:38 +01:00
|
|
|
nrf_rtc_event_disable(RTC, RTC_CHANNEL_INT_MASK(chan));
|
2020-03-04 11:23:16 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
static uint32_t counter(void)
|
2020-03-04 11:23:16 +01:00
|
|
|
{
|
2020-11-04 11:57:38 +01:00
|
|
|
return nrf_rtc_counter_get(RTC);
|
2020-03-04 11:23:16 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
uint32_t z_nrf_rtc_timer_read(void)
|
2016-11-22 17:03:32 +01:00
|
|
|
{
|
2018-10-15 18:04:21 +02:00
|
|
|
return nrf_rtc_counter_get(RTC);
|
2016-11-22 17:03:32 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
uint32_t z_nrf_rtc_timer_compare_evt_address_get(uint32_t chan)
|
2020-03-04 11:23:16 +01:00
|
|
|
{
|
2020-11-04 11:57:38 +01:00
|
|
|
__ASSERT_NO_MSG(chan < CHAN_COUNT);
|
|
|
|
return nrf_rtc_event_address_get(RTC, nrf_rtc_compare_event_get(chan));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool z_nrf_rtc_timer_compare_int_lock(uint32_t chan)
|
|
|
|
{
|
|
|
|
__ASSERT_NO_MSG(chan && chan < CHAN_COUNT);
|
|
|
|
|
|
|
|
atomic_val_t prev = atomic_and(&int_mask, ~BIT(chan));
|
|
|
|
|
|
|
|
nrf_rtc_int_disable(RTC, RTC_CHANNEL_INT_MASK(chan));
|
|
|
|
|
|
|
|
return prev & BIT(chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
void z_nrf_rtc_timer_compare_int_unlock(uint32_t chan, bool key)
|
|
|
|
{
|
|
|
|
__ASSERT_NO_MSG(chan && chan < CHAN_COUNT);
|
|
|
|
|
|
|
|
if (key) {
|
|
|
|
atomic_or(&int_mask, BIT(chan));
|
|
|
|
nrf_rtc_int_enable(RTC, RTC_CHANNEL_INT_MASK(chan));
|
2020-03-04 11:23:16 +01:00
|
|
|
}
|
2020-11-04 11:57:38 +01:00
|
|
|
}
|
2020-05-21 11:59:30 +02:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
uint32_t z_nrf_rtc_timer_compare_read(uint32_t chan)
|
|
|
|
{
|
|
|
|
__ASSERT_NO_MSG(chan < CHAN_COUNT);
|
|
|
|
|
|
|
|
return nrf_rtc_cc_get(RTC, chan);
|
2020-03-04 11:23:16 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
int z_nrf_rtc_timer_get_ticks(k_timeout_t t)
|
2020-03-04 11:23:16 +01:00
|
|
|
{
|
2020-11-04 11:57:38 +01:00
|
|
|
uint32_t curr_count;
|
|
|
|
int64_t curr_tick;
|
|
|
|
int64_t result;
|
|
|
|
int64_t abs_ticks;
|
|
|
|
|
|
|
|
do {
|
|
|
|
curr_count = counter();
|
|
|
|
curr_tick = z_tick_get();
|
|
|
|
} while (curr_count != counter());
|
|
|
|
|
|
|
|
abs_ticks = Z_TICK_ABS(t.ticks);
|
|
|
|
if (abs_ticks < 0) {
|
|
|
|
/* relative timeout */
|
|
|
|
return (t.ticks > COUNTER_HALF_SPAN) ?
|
|
|
|
-EINVAL : ((curr_count + t.ticks) & COUNTER_MAX);
|
2020-03-04 11:23:16 +01:00
|
|
|
}
|
2020-11-04 11:57:38 +01:00
|
|
|
|
|
|
|
/* absolute timeout */
|
|
|
|
result = abs_ticks - curr_tick;
|
2020-12-07 16:10:35 +01:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
if ((result > COUNTER_HALF_SPAN) ||
|
2020-12-07 16:10:35 +01:00
|
|
|
(result < -(int64_t)COUNTER_HALF_SPAN)) {
|
2020-11-04 11:57:38 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (curr_count + result) & COUNTER_MAX;
|
2020-03-04 11:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function safely sets absolute alarm. It assumes that provided value is
|
2020-11-04 11:57:38 +01:00
|
|
|
* less than COUNTER_HALF_SPAN from now. It detects late setting and also
|
|
|
|
* handle +1 cycle case.
|
2020-03-04 11:23:16 +01:00
|
|
|
*/
|
2020-11-04 11:57:38 +01:00
|
|
|
static void set_absolute_alarm(uint32_t chan, uint32_t abs_val)
|
2020-03-04 11:23:16 +01:00
|
|
|
{
|
2020-11-04 11:57:38 +01:00
|
|
|
uint32_t now;
|
|
|
|
uint32_t now2;
|
|
|
|
uint32_t cc_val = abs_val & COUNTER_MAX;
|
|
|
|
uint32_t prev_cc = get_comparator(chan);
|
2020-03-04 11:23:16 +01:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
do {
|
|
|
|
now = counter();
|
|
|
|
|
|
|
|
/* Handle case when previous event may generate an event.
|
2020-12-07 11:57:43 +01:00
|
|
|
* It is handled by setting CC to now (far in the future),
|
2020-11-04 11:57:38 +01:00
|
|
|
* in case previous event was set for next tick wait for half
|
|
|
|
* LF tick and clear event that may have been generated.
|
2020-03-04 11:23:16 +01:00
|
|
|
*/
|
2020-11-04 11:57:38 +01:00
|
|
|
set_comparator(chan, now);
|
|
|
|
if (counter_sub(prev_cc, now) == 1) {
|
|
|
|
k_busy_wait(15);
|
|
|
|
}
|
|
|
|
|
2020-12-07 11:57:43 +01:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
/* If requested cc_val is in the past or next tick, set to 2
|
|
|
|
* ticks from now. RTC may not generate event if CC is set for
|
|
|
|
* 1 tick from now.
|
|
|
|
*/
|
|
|
|
if (counter_sub(cc_val, now + 2) > COUNTER_HALF_SPAN) {
|
|
|
|
cc_val = now + 2;
|
|
|
|
}
|
|
|
|
|
2020-12-07 11:57:43 +01:00
|
|
|
event_clear(chan);
|
2020-11-04 11:57:38 +01:00
|
|
|
event_enable(chan);
|
|
|
|
set_comparator(chan, cc_val);
|
|
|
|
now2 = counter();
|
|
|
|
prev_cc = cc_val;
|
|
|
|
/* Rerun the algorithm if counter progressed during execution
|
|
|
|
* and cc_val is in the past or one tick from now. In such
|
|
|
|
* scenario, it is possible that event will not be generated.
|
|
|
|
* Reruning the algorithm will delay the alarm but ensure that
|
|
|
|
* event will be generated at the moment indicated by value in
|
|
|
|
* CC register.
|
|
|
|
*/
|
|
|
|
} while ((now2 != now) &&
|
|
|
|
(counter_sub(cc_val, now2 + 2) > COUNTER_HALF_SPAN));
|
2020-03-04 11:23:16 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
static void compare_set(uint32_t chan, uint32_t cc_value,
|
|
|
|
z_nrf_rtc_timer_compare_handler_t handler,
|
|
|
|
void *user_data)
|
2020-03-04 11:23:16 +01:00
|
|
|
{
|
2020-11-04 11:57:38 +01:00
|
|
|
cc_data[chan].callback = handler;
|
|
|
|
cc_data[chan].user_context = user_data;
|
2020-03-04 11:23:16 +01:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
set_absolute_alarm(chan, cc_value);
|
|
|
|
}
|
2020-03-04 11:23:16 +01:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
void z_nrf_rtc_timer_compare_set(uint32_t chan, uint32_t cc_value,
|
|
|
|
z_nrf_rtc_timer_compare_handler_t handler,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
__ASSERT_NO_MSG(chan && chan < CHAN_COUNT);
|
|
|
|
|
|
|
|
bool key = z_nrf_rtc_timer_compare_int_lock(chan);
|
|
|
|
|
|
|
|
compare_set(chan, cc_value, handler, user_data);
|
|
|
|
|
|
|
|
z_nrf_rtc_timer_compare_int_unlock(chan, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sys_clock_timeout_handler(uint32_t chan,
|
|
|
|
uint32_t cc_value,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
uint32_t dticks = counter_sub(cc_value, last_count) / CYC_PER_TICK;
|
|
|
|
|
|
|
|
last_count += dticks * CYC_PER_TICK;
|
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
|
|
|
|
/* protection is not needed because we are in the RTC interrupt
|
|
|
|
* so it won't get preempted by the interrupt.
|
|
|
|
*/
|
2020-12-11 11:55:36 +01:00
|
|
|
compare_set(chan, last_count + CYC_PER_TICK,
|
2020-11-04 11:57:38 +01:00
|
|
|
sys_clock_timeout_handler, NULL);
|
|
|
|
}
|
2020-03-04 11:23:16 +01:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
z_clock_announce(IS_ENABLED(CONFIG_TICKLESS_KERNEL) ?
|
|
|
|
dticks : (dticks > 0));
|
2020-03-04 11:23:16 +01:00
|
|
|
}
|
|
|
|
|
2018-10-15 18:04:21 +02:00
|
|
|
/* Note: this function has public linkage, and MUST have this
|
|
|
|
* particular name. The platform architecture itself doesn't care,
|
2019-10-01 16:02:27 +02:00
|
|
|
* but there is a test (tests/arch/arm_irq_vector_table) that needs
|
2018-10-15 18:04:21 +02:00
|
|
|
* to find it to it can set it in a custom vector table. Should
|
|
|
|
* probably better abstract that at some point (e.g. query and reset
|
|
|
|
* it by pointer at runtime, maybe?) so we don't have this leaky
|
|
|
|
* symbol.
|
2017-02-13 16:31:53 +01:00
|
|
|
*/
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
|
|
|
void rtc_nrf_isr(const void *arg)
|
2016-11-22 17:03:32 +01:00
|
|
|
{
|
2017-05-08 07:59:37 +02:00
|
|
|
ARG_UNUSED(arg);
|
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
for (uint32_t chan = 0; chan < CHAN_COUNT; chan++) {
|
|
|
|
if (nrf_rtc_int_enable_check(RTC, RTC_CHANNEL_INT_MASK(chan)) &&
|
|
|
|
nrf_rtc_event_check(RTC, RTC_CHANNEL_EVENT_ADDR(chan))) {
|
|
|
|
uint32_t cc_val;
|
|
|
|
z_nrf_rtc_timer_compare_handler_t handler;
|
|
|
|
|
|
|
|
event_clear(chan);
|
|
|
|
event_disable(chan);
|
|
|
|
cc_val = get_comparator(chan);
|
|
|
|
handler = cc_data[chan].callback;
|
|
|
|
cc_data[chan].callback = NULL;
|
|
|
|
if (handler) {
|
|
|
|
handler(chan, cc_val,
|
|
|
|
cc_data[chan].user_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-31 16:52:18 +02:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
int z_nrf_rtc_timer_chan_alloc(void)
|
|
|
|
{
|
|
|
|
int chan;
|
|
|
|
atomic_val_t prev;
|
|
|
|
do {
|
|
|
|
chan = alloc_mask ? 31 - __builtin_clz(alloc_mask) : -1;
|
|
|
|
if (chan < 0) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
prev = atomic_and(&alloc_mask, ~BIT(chan));
|
|
|
|
} while (!(prev & BIT(chan)));
|
|
|
|
|
|
|
|
return chan;
|
|
|
|
}
|
2018-10-15 18:04:21 +02:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
void z_nrf_rtc_timer_chan_free(uint32_t chan)
|
|
|
|
{
|
|
|
|
__ASSERT_NO_MSG(chan && chan < CHAN_COUNT);
|
2017-08-31 16:52:18 +02:00
|
|
|
|
2020-11-04 11:57:38 +01:00
|
|
|
atomic_or(&alloc_mask, BIT(chan));
|
2016-11-22 17:03:32 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
int z_clock_driver_init(const struct device *device)
|
2016-11-22 17:03:32 +01:00
|
|
|
{
|
|
|
|
ARG_UNUSED(device);
|
2020-08-04 13:56:21 +02:00
|
|
|
static const enum nrf_lfclk_start_mode mode =
|
|
|
|
IS_ENABLED(CONFIG_SYSTEM_CLOCK_NO_WAIT) ?
|
|
|
|
CLOCK_CONTROL_NRF_LF_START_NOWAIT :
|
|
|
|
(IS_ENABLED(CONFIG_SYSTEM_CLOCK_WAIT_FOR_AVAILABILITY) ?
|
|
|
|
CLOCK_CONTROL_NRF_LF_START_AVAILABLE :
|
|
|
|
CLOCK_CONTROL_NRF_LF_START_STABLE);
|
2016-11-22 17:03:32 +01:00
|
|
|
|
|
|
|
/* TODO: replace with counter driver to access RTC */
|
2018-10-15 18:04:21 +02:00
|
|
|
nrf_rtc_prescaler_set(RTC, 0);
|
2020-11-04 11:57:38 +01:00
|
|
|
for (uint32_t chan = 0; chan < CHAN_COUNT; chan++) {
|
|
|
|
nrf_rtc_int_enable(RTC, RTC_CHANNEL_INT_MASK(chan));
|
|
|
|
}
|
|
|
|
|
2020-05-21 13:46:48 +02:00
|
|
|
NVIC_ClearPendingIRQ(RTC_IRQn);
|
2017-02-28 15:41:18 +01:00
|
|
|
|
2020-10-06 09:25:12 +02:00
|
|
|
IRQ_CONNECT(RTC_IRQn, DT_IRQ(DT_NODELABEL(RTC_LABEL), priority),
|
|
|
|
rtc_nrf_isr, 0, 0);
|
2020-05-21 13:46:48 +02:00
|
|
|
irq_enable(RTC_IRQn);
|
2016-11-22 17:03:32 +01:00
|
|
|
|
2018-10-15 18:04:21 +02:00
|
|
|
nrf_rtc_task_trigger(RTC, NRF_RTC_TASK_CLEAR);
|
|
|
|
nrf_rtc_task_trigger(RTC, NRF_RTC_TASK_START);
|
|
|
|
|
2020-12-08 11:06:29 +01:00
|
|
|
int_mask = BIT_MASK(CHAN_COUNT);
|
2020-11-04 11:57:38 +01:00
|
|
|
if (CONFIG_NRF_RTC_TIMER_USER_CHAN_COUNT) {
|
|
|
|
alloc_mask = BIT_MASK(EXT_CHAN_COUNT) << 1;
|
|
|
|
}
|
|
|
|
|
2020-01-25 05:16:31 +01:00
|
|
|
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
|
2020-11-04 11:57:38 +01:00
|
|
|
compare_set(0, counter() + CYC_PER_TICK,
|
|
|
|
sys_clock_timeout_handler, NULL);
|
2018-10-15 18:04:21 +02:00
|
|
|
}
|
2016-11-22 17:03:32 +01:00
|
|
|
|
2020-08-04 13:56:21 +02:00
|
|
|
z_nrf_clock_control_lf_on(mode);
|
2020-07-07 14:12:12 +02:00
|
|
|
|
2016-11-28 04:35:52 +01:00
|
|
|
return 0;
|
2016-11-22 17:03:32 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
void z_clock_set_timeout(int32_t ticks, bool idle)
|
2016-11-22 17:03:32 +01:00
|
|
|
{
|
2018-10-15 18:04:21 +02:00
|
|
|
ARG_UNUSED(idle);
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t cyc;
|
2020-03-04 11:23:16 +01:00
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-30 17:34:28 +01:00
|
|
|
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks;
|
2020-10-27 12:27:25 +01:00
|
|
|
ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS);
|
2017-02-28 15:41:18 +01:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t unannounced = counter_sub(counter(), last_count);
|
2019-03-07 23:18:17 +01:00
|
|
|
|
2019-11-23 23:36:51 +01:00
|
|
|
/* If we haven't announced for more than half the 24-bit wrap
|
|
|
|
* duration, then force an announce to avoid loss of a wrap
|
|
|
|
* event. This can happen if new timeouts keep being set
|
|
|
|
* before the existing one triggers the interrupt.
|
|
|
|
*/
|
|
|
|
if (unannounced >= COUNTER_HALF_SPAN) {
|
|
|
|
ticks = 0;
|
|
|
|
}
|
|
|
|
|
2019-11-22 14:47:07 +01:00
|
|
|
/* Get the cycles from last_count to the tick boundary after
|
|
|
|
* the requested ticks have passed starting now.
|
|
|
|
*/
|
2019-11-23 23:36:51 +01:00
|
|
|
cyc = ticks * CYC_PER_TICK + 1 + unannounced;
|
2019-03-07 23:18:17 +01:00
|
|
|
cyc += (CYC_PER_TICK - 1);
|
|
|
|
cyc = (cyc / CYC_PER_TICK) * CYC_PER_TICK;
|
2019-11-22 14:47:07 +01:00
|
|
|
|
|
|
|
/* Due to elapsed time the calculation above might produce a
|
|
|
|
* duration that laps the counter. Don't let it.
|
|
|
|
*/
|
|
|
|
if (cyc > MAX_CYCLES) {
|
|
|
|
cyc = MAX_CYCLES;
|
|
|
|
}
|
|
|
|
|
2019-03-07 23:18:17 +01:00
|
|
|
cyc += last_count;
|
2020-11-04 11:57:38 +01:00
|
|
|
compare_set(0, cyc, sys_clock_timeout_handler, NULL);
|
2018-10-15 18:04:21 +02:00
|
|
|
}
|
2017-02-28 15:41:18 +01:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t z_clock_elapsed(void)
|
2018-10-15 18:04:21 +02:00
|
|
|
{
|
|
|
|
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-28 15:41:18 +01:00
|
|
|
|
2019-03-07 23:18:17 +01:00
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t ret = counter_sub(counter(), last_count) / CYC_PER_TICK;
|
2018-10-15 18:04:21 +02:00
|
|
|
|
2019-03-07 23:18:17 +01:00
|
|
|
k_spin_unlock(&lock, key);
|
2018-10-15 18:04:21 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t z_timer_cycle_get_32(void)
|
2018-10-15 18:04:21 +02:00
|
|
|
{
|
2019-03-07 23:18:17 +01:00
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t ret = counter_sub(counter(), last_count) + last_count;
|
2016-11-30 17:34:28 +01:00
|
|
|
|
2019-03-07 23:18:17 +01:00
|
|
|
k_spin_unlock(&lock, key);
|
2018-10-15 18:04:21 +02:00
|
|
|
return ret;
|
2016-11-30 17:34:28 +01:00
|
|
|
}
|