2020-05-05 01:23:59 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Manivannan Sadhasivam
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 15:58:46 +08:00
|
|
|
#include <zephyr/kernel.h>
|
2020-05-05 01:23:59 +08:00
|
|
|
|
|
|
|
#include <timer.h>
|
|
|
|
|
2020-07-06 00:00:46 +08:00
|
|
|
static void timer_work_handler(struct k_work *work);
|
|
|
|
K_WORK_DEFINE(timer_work, timer_work_handler);
|
|
|
|
|
2020-05-05 01:23:59 +08:00
|
|
|
static uint32_t saved_time;
|
2020-07-06 00:07:37 +08:00
|
|
|
/* TODO: Use Non-volatile memory for backup */
|
|
|
|
static volatile uint32_t backup_reg[2];
|
2020-05-05 01:23:59 +08:00
|
|
|
|
2020-07-06 00:00:46 +08:00
|
|
|
static void timer_work_handler(struct k_work *work)
|
|
|
|
{
|
|
|
|
TimerIrqHandler();
|
|
|
|
}
|
|
|
|
|
2020-05-05 01:23:59 +08:00
|
|
|
static void timer_callback(struct k_timer *_timer)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(_timer);
|
|
|
|
|
2020-07-06 00:00:46 +08:00
|
|
|
k_work_submit(&timer_work);
|
2020-05-05 01:23:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
K_TIMER_DEFINE(lora_timer, timer_callback, NULL);
|
|
|
|
|
2020-07-06 00:07:37 +08:00
|
|
|
void RtcBkupWrite(uint32_t data0, uint32_t data1)
|
|
|
|
{
|
|
|
|
backup_reg[0] = data0;
|
|
|
|
backup_reg[1] = data1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RtcBkupRead(uint32_t *data0, uint32_t *data1)
|
|
|
|
{
|
|
|
|
*data0 = backup_reg[0];
|
|
|
|
*data1 = backup_reg[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t RtcGetCalendarTime(uint16_t *milliseconds)
|
|
|
|
{
|
2021-10-05 17:15:33 +08:00
|
|
|
int64_t now = k_uptime_get();
|
2020-07-06 00:07:37 +08:00
|
|
|
|
2021-10-05 17:15:33 +08:00
|
|
|
*milliseconds = now % MSEC_PER_SEC;
|
2020-07-06 00:07:37 +08:00
|
|
|
|
|
|
|
/* Return in seconds */
|
|
|
|
return now / MSEC_PER_SEC;
|
|
|
|
}
|
|
|
|
|
2020-05-05 01:23:59 +08:00
|
|
|
uint32_t RtcGetTimerValue(void)
|
|
|
|
{
|
|
|
|
return k_uptime_get_32();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t RtcGetTimerElapsedTime(void)
|
|
|
|
{
|
|
|
|
return (k_uptime_get_32() - saved_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t RtcGetMinimumTimeout(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RtcStopAlarm(void)
|
|
|
|
{
|
|
|
|
k_timer_stop(&lora_timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RtcSetAlarm(uint32_t timeout)
|
|
|
|
{
|
|
|
|
k_timer_start(&lora_timer, K_MSEC(timeout), K_NO_WAIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t RtcSetTimerContext(void)
|
|
|
|
{
|
|
|
|
saved_time = k_uptime_get_32();
|
|
|
|
|
|
|
|
return saved_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For us, 1 tick = 1 milli second. So no need to do any conversion here */
|
|
|
|
uint32_t RtcGetTimerContext(void)
|
|
|
|
{
|
|
|
|
return saved_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DelayMsMcu(uint32_t ms)
|
|
|
|
{
|
|
|
|
k_sleep(K_MSEC(ms));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t RtcMs2Tick(uint32_t milliseconds)
|
|
|
|
{
|
|
|
|
return milliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t RtcTick2Ms(uint32_t tick)
|
|
|
|
{
|
|
|
|
return tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoardCriticalSectionBegin(uint32_t *mask)
|
|
|
|
{
|
|
|
|
*mask = irq_lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoardCriticalSectionEnd(uint32_t *mask)
|
|
|
|
{
|
|
|
|
irq_unlock(*mask);
|
|
|
|
}
|