Skip to content

Commit

Permalink
Take version from board overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
Kampi committed Jul 29, 2024
1 parent 8e2d9eb commit 3d1fe61
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 163 deletions.
31 changes: 16 additions & 15 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,30 @@ foreach(file ${files})
patch -p1 -d $ENV{ZEPHYR_BASE} -i ${CMAKE_CURRENT_SOURCE_DIR}/${file} -r - --no-backup-if-mismatch)
endforeach()

# Get the target hardware
string(REGEX MATCH "zswatch_nrf5340_cpuapp(_ns)?@([0-9]+)$" ZSWATCH_BOARD "${BOARD}")
if(ZSWATCH_BOARD)
set(ZSWATCH_REV ${CMAKE_MATCH_2})
if(${ZSWATCH_REV} EQUAL 2)
set(DFU_BUILD true)
set(PM_STATIC_YML_FILE ${CMAKE_CURRENT_SOURCE_DIR}/partition_mcuboot.yml)
set(OVERLAY_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/boards/dfu.conf)
set(mcuboot_DTC_OVERLAY_FILE ${CMAKE_CURRENT_SOURCE_DIR}/overlays/mcuboot.overlay)
elseif(${ZSWATCH_REV} GREATER_EQUAL 3)
string(CONCAT VERSION_STRING "\"" ${CMAKE_MATCH_2} "\"")
set(CONFIG_ZSWATCH_PCB_REV ${CMAKE_MATCH_2} CACHE INTERNAL "")
set(CONFIG_BT_DIS_HW_REV_STR ${VERSION_STRING} CACHE INTERNAL "")

message("PCB revision: " ${CONFIG_ZSWATCH_PCB_REV})

if(CONFIG_ZSWATCH_PCB_REV GREATER_EQUAL 3)
set(PM_STATIC_YML_FILE ${CMAKE_CURRENT_SOURCE_DIR}/partition_external.yml)
else()
set(PM_STATIC_YML_FILE ${CMAKE_CURRENT_SOURCE_DIR}/partition_internal.yml)
endif()
elseif("nrf5340dk_nrf5340_cpuapp" STREQUAL "${BOARD}")
set(PM_STATIC_YML_FILE ${CMAKE_CURRENT_SOURCE_DIR}/partition_external.yml)
endif()

if("nrf5340dk_nrf5340_cpuapp" STREQUAL "${BOARD}")
set(PM_STATIC_YML_FILE ${CMAKE_CURRENT_SOURCE_DIR}/partition_external.yml)
if(CONFIG_BOOTLOADER_MCUBOOT)
# TODO: Needed for MCUBoot
#set(DFU_BUILD true)
#set(PM_STATIC_YML_FILE ${CMAKE_CURRENT_SOURCE_DIR}/partition_mcuboot.yml)
#set(OVERLAY_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/boards/dfu.conf)
#set(mcuboot_DTC_OVERLAY_FILE ${CMAKE_CURRENT_SOURCE_DIR}/overlays/mcuboot.overlay)
endif()

set(BOARD_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down Expand Up @@ -62,13 +69,7 @@ else()
add_subdirectory(src/basic_battery)
endif()

# TODO
target_sources(app PRIVATE src/zsw_clock.c)
#if (CONFIG_RTC)
# target_sources(app PRIVATE src/zsw_rtc.c)
#else()
#endif()

target_sources(app PRIVATE src/main.c)
target_sources(app PRIVATE src/zsw_cpu_freq.c)
target_sources(app PRIVATE src/zsw_retained_ram_storage.c)
Expand Down
3 changes: 0 additions & 3 deletions app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_1.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
CONFIG_ZSWATCH_PCB_REV=1
CONFIG_BT_DIS_HW_REV_STR="1"

CONFIG_DEBUG_COREDUMP_BACKEND_LOGGING=y
3 changes: 0 additions & 3 deletions app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_2.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
CONFIG_ZSWATCH_PCB_REV=2
CONFIG_BT_DIS_HW_REV_STR="2"

CONFIG_DEBUG_COREDUMP_BACKEND_LOGGING=y
3 changes: 0 additions & 3 deletions app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_3.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
CONFIG_ZSWATCH_PCB_REV=3
CONFIG_BT_DIS_HW_REV_STR="3"

#---------------
# FLASH
#-----------------
Expand Down
3 changes: 0 additions & 3 deletions app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_4.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
CONFIG_ZSWATCH_PCB_REV=4
CONFIG_BT_DIS_HW_REV_STR="4"

#---------------
# FLASH
#-----------------
Expand Down
7 changes: 2 additions & 5 deletions app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_5.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
CONFIG_ZSWATCH_PCB_REV=5
CONFIG_BT_DIS_HW_REV_STR="5"

# FLASH
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
Expand Down Expand Up @@ -45,8 +42,8 @@ CONFIG_NVS=y

CONFIG_DEBUG_COREDUMP_BACKEND_OTHER=y

CONFIG_RTC=y
CONFIG_RTC_UPDATE=y
#CONFIG_RTC=y
#CONFIG_RTC_UPDATE=y

CONFIG_MISC_ENABLE_SYSTEM_RESET=n # Implemented in nPM, hence not needed in FW

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
aliases {
spi-flash0 = &mx25u51245g;
buzzer-pwm = &buzzer_pwm;
rtc = &rv_8263_c8;
};

longpress: longpress {
Expand Down
Binary file added app/lvgl_resources
Binary file not shown.
5 changes: 3 additions & 2 deletions app/src/ble/ble_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ BT_GATT_SERVICE_DEFINE(nus_service,
NULL, NULL, NULL),
BT_GATT_CCC(NULL,
#if CONFIG_BLE_DISABLE_PAIRING_REQUIRED
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE
#else
BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT),
BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT
#endif
),
BT_GATT_CHARACTERISTIC(BLE_TRANSPORT_UUID_RX,
BT_GATT_CHRC_WRITE |
BT_GATT_CHRC_WRITE_WITHOUT_RESP,
Expand Down
36 changes: 30 additions & 6 deletions app/src/events/zsw_periodic_event.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/zbus/zbus.h>

#if CONFIG_RTC
#include <zephyr/drivers/rtc.h>
#endif

#include "zsw_clock.h"
#include "events/periodic_event.h"

#define PERIODIC_FAST_INTERVAL_MS 100
#define PERIODIC_MID_INTERVAL_MS 1000
#define PERIODIC_SLOW_INTERVAL_MS 10000

static void handle_slow_timeout(struct k_work *item);
static void handle_fast_timeout(struct k_work *item);
static void handle_mid_timeout(struct k_work *item);
#if CONFIG_RTC
static const struct device *const rtc = DEVICE_DT_GET(DT_ALIAS(rtc));
#endif

ZBUS_CHAN_DECLARE(periodic_event_10s_chan);
ZBUS_CHAN_DECLARE(periodic_event_1s_chan);
ZBUS_CHAN_DECLARE(periodic_event_10s_chan);
ZBUS_CHAN_DECLARE(periodic_event_100ms_chan);

int zsw_periodic_chan_add_obs(const struct zbus_channel *chan, const struct zbus_observer *obs)
Expand Down Expand Up @@ -71,6 +76,15 @@ static void handle_slow_timeout(struct k_work *item)
zbus_chan_pub(&periodic_event_10s_chan, &evt, K_MSEC(250));
}

//#if CONFIG_RTC
//void handle_mid_timeout(const struct device *dev, void *user_data)
//{
// struct periodic_event evt = {
// };
//
// zbus_chan_pub(&periodic_event_1s_chan, &evt, K_MSEC(250));
//}
//#else
static void handle_mid_timeout(struct k_work *item)
{
struct periodic_event evt = {
Expand All @@ -83,6 +97,7 @@ static void handle_mid_timeout(struct k_work *item)

zbus_chan_pub(&periodic_event_1s_chan, &evt, K_MSEC(250));
}
//#endif

static void handle_fast_timeout(struct k_work *item)
{
Expand All @@ -100,15 +115,24 @@ static void handle_fast_timeout(struct k_work *item)
static int zsw_timer_init(void)
{
struct k_work_delayable *work = NULL;

work = (struct k_work_delayable *)zbus_chan_user_data(&periodic_event_10s_chan);
k_work_init_delayable(work, handle_slow_timeout);

// TODO: Use the RTC instead of the timer in rev 5 to generate this
//#if CONFIG_RTC
// if (!device_is_ready(rtc)) {
// return -EBUSY;
// }
//
// rtc_update_set_callback(rtc, handle_mid_timeout, NULL);
//#else
work = (struct k_work_delayable *)zbus_chan_user_data(&periodic_event_1s_chan);
k_work_init_delayable(work, handle_mid_timeout);
//#endif

work = (struct k_work_delayable *)zbus_chan_user_data(&periodic_event_100ms_chan);
k_work_init_delayable(work, handle_fast_timeout);

return 0;
}

Expand Down
16 changes: 16 additions & 0 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ static void run_wdt_work(struct k_work *item)

int main(void)
{
/*
while (1) {
struct i2c_msg msgs[1];
uint8_t dst;
for (uint8_t i = 0; i < 127; i++) {
msgs[0].buf = &dst;
msgs[0].len = 0U;
msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
if (i2c_transfer(i2c, &msgs[0], 1, i) == 0) {
LOG_INF("Found device at 0x%X", i);
}
}
k_msleep(1000);
}*/
#ifdef CONFIG_SPI_FLASH_LOADER
if (bootmode_check(ZSW_BOOT_MODE_RTT_FLASH_LOADER)) {
LOG_WRN("SPI Flash Loader Boot Mode");
Expand Down
81 changes: 63 additions & 18 deletions app/src/zsw_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/zbus/zbus.h>
#include <zephyr/logging/log.h>
#include <sys/time.h>
Expand All @@ -27,31 +27,83 @@
#include <unistd.h>

#include "zsw_clock.h"
#include "zsw_retained_ram_storage.h"
#include "events/zsw_periodic_event.h"

#if CONFIG_RTC
#include <zephyr/drivers/rtc.h>
#else
#include "zsw_retained_ram_storage.h"
#endif

#if CONFIG_RTC
static const struct device *const rtc = DEVICE_DT_GET(DT_ALIAS(rtc));
#else
static void zbus_periodic_slow_callback(const struct zbus_channel *chan);

ZBUS_CHAN_DECLARE(periodic_event_1s_chan);
ZBUS_LISTENER_DEFINE(zsw_clock_lis, zbus_periodic_slow_callback);
#endif

LOG_MODULE_REGISTER(zsw_clock, LOG_LEVEL_INF);

#ifndef CONFIG_RTC

static time_t zsw_clock_get_time_unix(void)
{
struct timeval tv;

gettimeofday(&tv, NULL);

return tv.tv_sec;
}

static void zbus_periodic_slow_callback(const struct zbus_channel *chan)
{
retained.current_time_seconds = zsw_clock_get_time_unix();
zsw_retained_ram_update();
}
#endif

void zsw_clock_set_time(zsw_timeval_t *ztm)
{
// Substract one from the month because we want to count from December instead of January
ztm->tm.tm_mon -= 1;

// Substract 1900 from the year because we want to count from 1900
ztm->tm.tm_year -= 1900;

#if CONFIG_RTC
rtc_set_time(rtc, &ztm->tm);
#else
struct timespec tspec;

tspec.tv_sec = ztm->tm.tm_sec;
#warning "Not implemented"
tspec.tv_sec = 0;
tspec.tv_nsec = 0;

clock_settime(CLOCK_REALTIME, &tspec);
if (strlen(retained.timezone) > 0) {
setenv("TZ", retained.timezone, 1);
tzset();
}
#endif
}

void zsw_clock_get_time(zsw_timeval_t *ztm)
{
struct timeval tv;
#if CONFIG_RTC
struct rtc_time tm;

rtc_get_time(rtc, &tm);
memcpy(ztm, &tm, sizeof(struct rtc_time));
#else
struct tm *tm;
struct timeval tv;

gettimeofday(&tv, NULL);
tm = localtime(&tv.tv_sec);
memcpy(ztm, tm, sizeof(struct tm));
#endif

// Add one to the month because we want to count from December instead of January
ztm->tm.tm_mon += 1;
Expand All @@ -60,22 +112,14 @@ void zsw_clock_get_time(zsw_timeval_t *ztm)
ztm->tm.tm_year += 1900;
}

time_t zsw_clock_get_time_unix(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);

return tv.tv_sec;
}

static void zbus_periodic_slow_callback(const struct zbus_channel *chan)
{
retained.current_time_seconds = zsw_clock_get_time_unix();
zsw_retained_ram_update();
}

static int zsw_clock_init(void)
{
#if CONFIG_RTC
if (!device_is_ready(rtc)) {
LOG_ERR("Device not ready!");
return -EBUSY;
}
#else
struct timespec tspec;

tspec.tv_sec = retained.current_time_seconds;
Expand All @@ -88,6 +132,7 @@ static int zsw_clock_init(void)
}

zsw_periodic_chan_add_obs(&periodic_event_1s_chan, &zsw_clock_lis);
#endif

return 0;
}
Expand Down
22 changes: 17 additions & 5 deletions app/src/zsw_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,32 @@

#pragma once

#include <zephyr/drivers/rtc.h>

#include <inttypes.h>
#include <time.h>

#if CONFIG_RTC
typedef void (*zsw_clock_on_update)(void);
#endif

typedef struct {
#if CONFIG_RTC
struct rtc_time
tm; /**< Modified time object with 1900 added to the year and the month increased by one. */
#else
struct tm tm; /**< Modified time object with 1900 added to the year and the month increased by one. */
#endif
uint32_t tv_usec;
} zsw_timeval_t;

void zsw_clock_add_update(zsw_clock_on_update callback);

/** @brief
* NOTE: This function needs the time as seconds
* @param ztm
*/
void zsw_clock_set_time(zsw_timeval_t *ztm);

void zsw_clock_get_time(zsw_timeval_t *ztm);

time_t zsw_clock_get_time_unix(void);
/** @brief
* @param ztm
*/
void zsw_clock_get_time(zsw_timeval_t *ztm);
Loading

0 comments on commit 3d1fe61

Please sign in to comment.