Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 20 additions & 50 deletions drivers/timer/nrf_grtc_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,7 @@ static inline uint64_t counter(void)

static inline int get_comparator(uint32_t chan, uint64_t *cc)
{
nrfx_err_t result;

result = nrfx_grtc_syscounter_cc_value_read(chan, cc);
if (result != NRFX_SUCCESS) {
if (result != NRFX_ERROR_INVALID_PARAM) {
return -EAGAIN;
}
return -EPERM;
}
return 0;
return nrfx_grtc_syscounter_cc_value_read(chan, cc);
}

/*
Expand Down Expand Up @@ -173,14 +164,14 @@ static void sys_clock_timeout_handler(int32_t id, uint64_t cc_val, void *p_conte
int32_t z_nrf_grtc_timer_chan_alloc(void)
{
uint8_t chan;
nrfx_err_t err_code;
int err_code;

/* Prevent allocating all available channels - one must be left for system purposes. */
if (ext_channels_allocated >= EXT_CHAN_COUNT) {
return -ENOMEM;
}
err_code = nrfx_grtc_channel_alloc(&chan);
if (err_code != NRFX_SUCCESS) {
if (err_code < 0) {
return -ENOMEM;
}
ext_channels_allocated++;
Expand All @@ -190,9 +181,9 @@ int32_t z_nrf_grtc_timer_chan_alloc(void)
void z_nrf_grtc_timer_chan_free(int32_t chan)
{
IS_CHANNEL_ALLOWED_ASSERT(chan);
nrfx_err_t err_code = nrfx_grtc_channel_free(chan);
int err_code = nrfx_grtc_channel_free(chan);

if (err_code == NRFX_SUCCESS) {
if (err_code == 0) {
ext_channels_allocated--;
}
}
Expand Down Expand Up @@ -249,19 +240,13 @@ int z_nrf_grtc_timer_compare_read(int32_t chan, uint64_t *val)
static int compare_set_nolocks(int32_t chan, uint64_t target_time,
z_nrf_grtc_timer_compare_handler_t handler, void *user_data)
{
nrfx_err_t result;

__ASSERT_NO_MSG(target_time < COUNTER_SPAN);
nrfx_grtc_channel_t user_channel_data = {
.handler = handler,
.p_context = user_data,
.channel = chan,
};
result = nrfx_grtc_syscounter_cc_absolute_set(&user_channel_data, target_time, true);
if (result != NRFX_SUCCESS) {
return -EPERM;
}
return 0;
return nrfx_grtc_syscounter_cc_absolute_set(&user_channel_data, target_time, true);
}

static int compare_set(int32_t chan, uint64_t target_time,
Expand Down Expand Up @@ -314,31 +299,22 @@ int z_nrf_grtc_timer_capture_prepare(int32_t chan)
.p_context = NULL,
.channel = chan,
};
nrfx_err_t result;

IS_CHANNEL_ALLOWED_ASSERT(chan);

/* Set the CC value to mark channel as not triggered and also to enable it
* (makes CCEN=1). COUNTER_SPAN is used so as not to fire an event unnecessarily
* - it can be assumed that such a large value will never be reached.
*/
result = nrfx_grtc_syscounter_cc_absolute_set(&user_channel_data, COUNTER_SPAN, false);

if (result != NRFX_SUCCESS) {
return -EPERM;
}

return 0;
return nrfx_grtc_syscounter_cc_absolute_set(&user_channel_data, COUNTER_SPAN, false);
}

int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time)
{
/* TODO: The implementation should probably go to nrfx_grtc and this
* should be just a wrapper for some nrfx_grtc_syscounter_capture_read.
*/

uint64_t capt_time;
nrfx_err_t result;
int result;

IS_CHANNEL_ALLOWED_ASSERT(chan);

Expand All @@ -349,14 +325,8 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time)
*/
return -EBUSY;
}
result = nrfx_grtc_syscounter_cc_value_read(chan, &capt_time);
if (result != NRFX_SUCCESS) {
return -EPERM;
}

__ASSERT_NO_MSG(capt_time < COUNTER_SPAN);

*captured_time = capt_time;
result = nrfx_grtc_syscounter_cc_value_read(chan, captured_time);
__ASSERT_NO_MSG(*captured_time < COUNTER_SPAN);

return 0;
}
Expand All @@ -369,7 +339,7 @@ uint64_t z_nrf_grtc_timer_startup_value_get(void)
#if defined(CONFIG_POWEROFF) && defined(CONFIG_NRF_GRTC_START_SYSCOUNTER)
int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
{
nrfx_err_t err_code;
int err_code;
static uint8_t systemoff_channel;
uint64_t now = counter();
nrfx_grtc_sleep_config_t sleep_cfg;
Expand All @@ -392,9 +362,9 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
k_spinlock_key_t key = k_spin_lock(&lock);

err_code = nrfx_grtc_channel_alloc(&systemoff_channel);
if (err_code != NRFX_SUCCESS) {
if (err_code < 0) {
k_spin_unlock(&lock, key);
return -ENOMEM;
return err_code;
}
(void)nrfx_grtc_syscounter_cc_int_disable(systemoff_channel);
ret = compare_set(systemoff_channel,
Expand Down Expand Up @@ -459,7 +429,7 @@ uint32_t sys_clock_elapsed(void)

static int sys_clock_driver_init(void)
{
nrfx_err_t err_code;
int err_code;

IRQ_CONNECT(DT_IRQN(GRTC_NODE), DT_IRQ(GRTC_NODE, priority), nrfx_isr,
nrfx_grtc_irq_handler, 0);
Expand All @@ -477,19 +447,19 @@ static int sys_clock_driver_init(void)
#endif

err_code = nrfx_grtc_init(0);
if (err_code != NRFX_SUCCESS) {
return -EPERM;
if (err_code < 0) {
return err_code;
}

#if defined(CONFIG_NRF_GRTC_START_SYSCOUNTER)
err_code = nrfx_grtc_syscounter_start(true, &system_clock_channel_data.channel);
if (err_code != NRFX_SUCCESS) {
return err_code == NRFX_ERROR_NO_MEM ? -ENOMEM : -EPERM;
if (err_code < 0) {
return err_code;
}
#else
err_code = nrfx_grtc_channel_alloc(&system_clock_channel_data.channel);
if (err_code != NRFX_SUCCESS) {
return -ENOMEM;
if (err_code < 0) {
return err_code;
}
#endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */

Expand Down
Loading