Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions apps/data_logger/VERSION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_MAJOR = 0
VERSION_MINOR = 0
PATCHLEVEL = 1
VERSION_MINOR = 1
PATCHLEVEL = 0
VERSION_TWEAK = 0
EXTRAVERSION = dev
34 changes: 34 additions & 0 deletions apps/data_logger/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
#include <zephyr/pm/device_runtime.h>
#include <zephyr/net/conn_mgr_connectivity.h>

#include <infuse/version.h>
#include <infuse/auto/time_sync_log.h>
#include <infuse/bluetooth/legacy_adv.h>
#include <infuse/drivers/watchdog.h>
#include <infuse/dfu/exfat.h>
#include <infuse/time/epoch.h>
#include <infuse/fs/kv_store.h>
#include <infuse/fs/kv_types.h>
Expand Down Expand Up @@ -155,11 +157,43 @@ TASK_RUNNER_TASKS_DEFINE(app_tasks, app_tasks_data, (TDF_LOGGER_TASK),
(BATTERY_TASK, DEVICE_DT_GET(DT_ALIAS(fuel_gauge0))),
(ENVIRONMENTAL_TASK, DEVICE_DT_GET(DT_ALIAS(environmental0))));

#ifdef CONFIG_INFUSE_DFU_EXFAT
static void dfu_progress_cb(size_t copied, size_t total)
{
/* Feed all the watchdogs */
infuse_watchdog_feed_all();
}

static void dfu_exfat_run(void)
{
const struct device *logger = DEVICE_DT_GET_ONE(embeint_data_logger_exfat);
uint8_t upgrade_partition = FIXED_PARTITION_ID(slot1_partition);
struct infuse_version upgrade_version;

if (dfu_exfat_app_upgrade_exists(logger, &upgrade_version) == 1) {
LOG_INF("Upgrade image to %d.%d.%d", upgrade_version.major, upgrade_version.minor,
upgrade_version.revision);
if (dfu_exfat_app_upgrade_copy(logger, upgrade_version, upgrade_partition,
dfu_progress_cb) == 0) {
LOG_INF("New image copied");
if (boot_request_upgrade_multi(0, 0) == 0) {
LOG_INF("Rebooting into new image");
infuse_reboot(INFUSE_REBOOT_EXFAT_DFU, 0x00, 0x00);
}
}
}
}
#endif /* CONFIG_INFUSE_DFU_EXFAT */

int main(void)
{
/* Start the watchdog */
(void)infuse_watchdog_start();

#ifdef CONFIG_INFUSE_DFU_EXFAT
dfu_exfat_run();
#endif /* CONFIG_INFUSE_DFU_EXFAT */

/* Log reboot events */
tdf_reboot_info_log(TDF_DATA_LOGGER_REMOVABLE | TDF_DATA_LOGGER_BT_ADV |
TDF_DATA_LOGGER_SERIAL | TDF_DATA_LOGGER_UDP);
Expand Down
7 changes: 7 additions & 0 deletions drivers/watchdog/watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ int infuse_watchdog_thread_state_lookup(int wdog_channel, uint32_t *info1, uint3
}
return 0;
}

void infuse_watchdog_feed_all(void)
{
for (int i = 0; i < 8; i++) {
(void)wdt_feed(INFUSE_WATCHDOG_DEV, i);
}
}
45 changes: 45 additions & 0 deletions include/infuse/data_logger/backend/exfat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @file
* @brief exFAT specific data logging interface
* @copyright 2024 Embeint Inc
* @author Jordan Yates <[email protected]>
*
* SPDX-License-Identifier: LicenseRef-Embeint
*/

#ifndef INFUSE_SDK_INCLUDE_INFUSE_DATA_LOGGER_BACKEND_EXFAT_H_
#define INFUSE_SDK_INCLUDE_INFUSE_DATA_LOGGER_BACKEND_EXFAT_H_

#include <zephyr/device.h>
#include <ff.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Claim exFAT filesystem
*
* @param dev exFAT logging device to claim filesysem for
* @param buf Block buffer that can be used for reads (Can be NULL if not needed)
* @param buf_size Size of the block buffer (Must be non-NULL if @a buf provided)
* @param timeout Duration to wait for filesystem object
*
* @retval disk name on success
* @retval NULL on timeout
*/
const char *logger_exfat_filesystem_claim(const struct device *dev, uint8_t **buf, size_t *buf_size,
k_timeout_t timeout);

/**
* @brief Release filesystem object claimed with @ref logger_exfat_filesystem_claim
*
* @param dev exFAT logging device to release
*/
void logger_exfat_filesystem_release(const struct device *dev);

#ifdef __cplusplus
}
#endif

#endif /* INFUSE_SDK_INCLUDE_INFUSE_DATA_LOGGER_BACKEND_EXFAT_H_ */
90 changes: 90 additions & 0 deletions include/infuse/dfu/exfat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @file
* @brief Firmware upgrade from mounted exFAT filesystem
* @copyright 2024 Embeint Inc
* @author Jordan Yates <[email protected]>
*
* SPDX-License-Identifier: LicenseRef-Embeint
*
* @details
* {version} == {major}_{minor}_{patch}
*
* Application firmware paths:
* /dfu/app/{v_new}.bin
*/

#ifndef INFUSE_SDK_INCLUDE_INFUSE_DFU_EXFAT_H_
#define INFUSE_SDK_INCLUDE_INFUSE_DFU_EXFAT_H_

#include <zephyr/device.h>
#include <zephyr/storage/flash_map.h>

#include <infuse/version.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @defgroup dfu_exfat_apis DFU from SD card APIs
* @{
*/

/**
* @brief Progress callback for DFU copy
*
* @param copied Number of bytes that have been copied so far
* @param total Total number of bytes to be copied
*/
typedef void (*dfu_exfat_progress_cb_t)(size_t copied, size_t total);

/**
* @brief Check whether a valid DFU file exists on the filesystem
*
* Expected usage:
* @code{.c}
* const struct device *logger = DEVICE_DT_GET_ONE(embeint_data_logger_exfat);
* uint8_t upgrade_partition = FIXED_PARTITION_ID(slot1_partition);
* struct infuse_version upgrade_version;
*
* if (dfu_exfat_app_upgrade_exists(logger, &upgrade_version) == 1) {
* if (dfu_exfat_app_upgrade_copy(logger, upgrade_version, upgrade_partition, NULL) == 0) {
* if (boot_request_upgrade_multi(0, 0) == 0) {
* infuse_reboot(INFUSE_REBOOT_EXFAT_DFU, 0x00, 0x00);
* }
* }
* }
* @endcode
*
* @param dev exFAT data logger device
* @param upgrade Storage location for upgrade version
*
* @retval 0 No valid upgrade file exists
* @retval 1 Valid upgrade file exists, @a upgrade is valid
* @retval -errno On error
*/
int dfu_exfat_app_upgrade_exists(const struct device *dev, struct infuse_version *upgrade);

/**
* @brief Copy DFU file from filesystem onto flash area
*
* @param dev exFAT data logger device
* @param upgrade New version from @ref dfu_exfat_app_upgrade_exists
* @param flash_area_id Output flash area ID
* @param progress_cb Optional progress callback
*
* @retval 0 On success
* @retval -errno On error
*/
int dfu_exfat_app_upgrade_copy(const struct device *dev, struct infuse_version upgrade,
uint8_t flash_area_id, dfu_exfat_progress_cb_t progress_cb);

/**
* @}
*/

#ifdef __cplusplus
}
#endif

#endif /* INFUSE_SDK_INCLUDE_INFUSE_DFU_EXFAT_H_ */
13 changes: 13 additions & 0 deletions include/infuse/drivers/watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ static inline void infuse_watchdog_feed(int wdog_channel)
}
}

/**
* @brief Feed all Infuse watchdog channels
*
* @note Should only be used in situations where the action of one thread
* could impact the timing of all watchdog channels. One example of
* this is erasing internal flash on nRF SoCs.
*/
void infuse_watchdog_feed_all(void);

#else

#define INFUSE_WATCHDOG_REGISTER_SYS_INIT(name, dependency, chan_name, period_name) \
Expand Down Expand Up @@ -201,6 +210,10 @@ static inline void infuse_watchdog_feed(int wdog_channel)
{
}

static inline void infuse_watchdog_feed_all(void)
{
}

#endif /* defined(CONFIG_INFUSE_WATCHDOG) || defined(__doxygen__) */

/**
Expand Down
2 changes: 2 additions & 0 deletions include/infuse/reboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ enum infuse_reboot_reason {
INFUSE_REBOOT_LTE_MODEM_FAULT,
/* MCUmgr request */
INFUSE_REBOOT_MCUMGR,
/* exFAT DFU triggered */
INFUSE_REBOOT_EXFAT_DFU,
/* Unknown reboot reason */
INFUSE_REBOOT_UNKNOWN = 255,
};
Expand Down
1 change: 1 addition & 0 deletions subsys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

add_subdirectory_ifdef(CONFIG_BT bluetooth)
add_subdirectory_ifdef(CONFIG_DATA_LOGGER data_logger)
add_subdirectory_ifdef(CONFIG_IMG_MANAGER dfu)
add_subdirectory_ifdef(CONFIG_EPACKET epacket)
add_subdirectory_ifdef(CONFIG_TDF tdf)
add_subdirectory_ifdef(CONFIG_INFUSE_RPC rpc)
Expand Down
1 change: 1 addition & 0 deletions subsys/Kconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rsource "data_logger/Kconfig"
rsource "dfu/Kconfig"
rsource "epacket/Kconfig"
rsource "tdf/Kconfig"
rsource "fs/Kconfig"
Expand Down
25 changes: 25 additions & 0 deletions subsys/data_logger/backends/exfat_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <stdio.h>

#include <zephyr/sys/__assert.h>
#include <zephyr/logging/log.h>
#include <zephyr/storage/disk_access.h>

Expand Down Expand Up @@ -136,3 +137,27 @@ int logger_exfat_filesystem_common_init(const struct device *dev)

return res == FR_OK ? 0 : -EIO;
}

const char *logger_exfat_filesystem_claim(const struct device *dev, uint8_t **buf, size_t *buf_size,
k_timeout_t timeout)
{
const struct dl_exfat_config *config = dev->config;
struct dl_exfat_data *data = dev->data;

if (k_sem_take(&data->filesystem_claim, timeout) != 0) {
return NULL;
}
if (buf != NULL) {
__ASSERT_NO_MSG(buf_size != NULL);
*buf = data->block_buffer;
*buf_size = sizeof(data->block_buffer);
}
return config->disk;
}

void logger_exfat_filesystem_release(const struct device *dev)
{
struct dl_exfat_data *data = dev->data;

k_sem_give(&data->filesystem_claim);
}
1 change: 1 addition & 0 deletions subsys/data_logger/backends/exfat_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct dl_exfat_config {

struct dl_exfat_data {
struct data_logger_common_data common;
struct k_sem filesystem_claim;
FATFS infuse_fatfs;
uint8_t block_buffer[DATA_LOGGER_EXFAT_BLOCK_SIZE];
uint32_t cached_file_num;
Expand Down
22 changes: 18 additions & 4 deletions subsys/data_logger/backends/exfat_multi_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <zephyr/pm/device_runtime.h>

#include <infuse/data_logger/logger.h>
#include <infuse/data_logger/backend/exfat.h>
#include <infuse/time/epoch.h>
#include <infuse/identifiers.h>

Expand Down Expand Up @@ -116,21 +117,25 @@ static int logger_exfat_write(const struct device *dev, uint32_t phy_block,
enum infuse_type data_type, const void *mem, uint16_t mem_len)
{
const struct dl_exfat_config *config = dev->config;
uint32_t disk_lba = disk_lba_from_block(dev, phy_block);
uint32_t disk_lba;
int rc;

__ASSERT(mem_len == DATA_LOGGER_EXFAT_BLOCK_SIZE, "Not full block");

(void)logger_exfat_filesystem_claim(dev, NULL, NULL, K_FOREVER);
disk_lba = disk_lba_from_block(dev, phy_block);

/* No memory left on filesystem */
if (disk_lba == LBA_NO_MEM) {
return -ENOMEM;
rc = -ENOMEM;
goto end;
}
/* File does not exist on filesystem */
else if (disk_lba == LBA_NO_FILE) {
/* Allocate the binary file on the filesystem */
rc = binary_container_create(dev, phy_block);
if (rc < 0) {
return rc;
goto end;
}
/* Recalculate the LBA */
disk_lba = disk_lba_from_block(dev, phy_block);
Expand All @@ -142,6 +147,8 @@ static int logger_exfat_write(const struct device *dev, uint32_t phy_block,
/* Sync on each write for now */
rc = disk_access_ioctl(config->disk, DISK_IOCTL_CTRL_SYNC, NULL);
}
end:
logger_exfat_filesystem_release(dev);
return rc;
}

Expand All @@ -150,10 +157,14 @@ static int logger_exfat_read(const struct device *dev, uint32_t phy_block, uint1
{
const struct dl_exfat_config *config = dev->config;
struct dl_exfat_data *data = dev->data;
uint32_t disk_lba = disk_lba_from_block(dev, phy_block);
uint32_t disk_lba;
int rc;

(void)logger_exfat_filesystem_claim(dev, NULL, NULL, K_FOREVER);
disk_lba = disk_lba_from_block(dev, phy_block);

if ((disk_lba == LBA_NO_FILE) || (disk_lba == LBA_NO_MEM)) {
logger_exfat_filesystem_release(dev);
/* File does not exist on filesystem, set data to 0xFF and return */
memset(mem, 0xFF, mem_len);
return 0;
Expand All @@ -165,6 +176,7 @@ static int logger_exfat_read(const struct device *dev, uint32_t phy_block, uint1
rc = disk_access_read(config->disk, data->block_buffer, disk_lba, 1);
/* Memcpy required data out */
memcpy(mem, data->block_buffer + block_offset, mem_len);
logger_exfat_filesystem_release(dev);
return rc;
}

Expand Down Expand Up @@ -208,6 +220,8 @@ int logger_exfat_init(const struct device *dev)
data->cached_file_num = UINT32_MAX;
data->cached_file_lba = UINT32_MAX;

k_sem_init(&data->filesystem_claim, 1, 1);

/* Initial mount attempt */
snprintf(disk_path, sizeof(disk_path), "%s:", config->disk);
res = f_mount(&data->infuse_fatfs, disk_path, 1);
Expand Down
Loading
Loading