Skip to content

Commit

Permalink
Merge pull request #21 from IRNAS/release/v1.4.0
Browse files Browse the repository at this point in the history
Release v1.4.0
  • Loading branch information
TjazVracko committed Aug 1, 2023
2 parents 0375f3f + d83c175 commit 706b3e2
Show file tree
Hide file tree
Showing 13 changed files with 649 additions and 76 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]

## [1.4.0] - 2023-08-01

### Added

- Option to disable default context storage implementation in the HAL and provide one's own implementation.
- `custom_context_storage` sample to demonstrate the use of custom context storage implementation.
- Function to re-attach radio irq callbacks if changed by the user during direct radio access.

## [1.3.0] - 2023-07-05

### Changed
Expand Down Expand Up @@ -71,7 +79,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- stream
- tx_rx_continous

[Unreleased]: https://github.com/IRNAS/SWL2001-Zephyr/compare/v1.3.0...HEAD
[Unreleased]: https://github.com/IRNAS/SWL2001-Zephyr/compare/v1.4.0...HEAD

[1.4.0]: https://github.com/IRNAS/SWL2001-Zephyr/compare/v1.3.0...v1.4.0

[1.3.0]: https://github.com/IRNAS/SWL2001-Zephyr/compare/v1.2.0...v1.3.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LoRa Basics Modem port for Zephyr.
2. Then in the `projects` section add at the bottom:

```yaml
- name: SWL001-Zephyr
- name: SWL2001-Zephyr
repo-path: SWL2001-Zephyr
path: irnas/SWL2001-Zephyr
remote: irnas
Expand Down
9 changes: 8 additions & 1 deletion drivers/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ menuconfig LORA_BASICS_MODEM
bool "LoRa Basics Modem library"
depends on LR11XX_LIB
depends on REBOOT
depends on SETTINGS
depends on SETTINGS || LORA_BASICS_MODEM_USER_STORAGE_IMPL
help
This library adds the lora basics modem.
Make sure you compile whit a libc implementation that
includes floorf, such as NEWLIB_LIBC.
Also make sure some source of randomness is used.

config LORA_BASICS_MODEM_USER_STORAGE_IMPL
bool "Enable user storage implementation"
default n
help
Enable if user storage implementation is required.
This disables the default storage implementation in the HAL
that uses the settings subsystem.

if LORA_BASICS_MODEM

Expand Down
1 change: 0 additions & 1 deletion drivers/apps_common/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ config LORA_BASICS_MODEM_PRINTERS
default y if LOG
default y if PRINTK
default n
depends on LOG || PRINTK
help
Enable this to get acecss to functions that take a lora basics
modem enum and return a static string, so it may be printed/logged
Expand Down
32 changes: 25 additions & 7 deletions drivers/apps_common/smtc_app/smtc_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ LOG_MODULE_REGISTER(smtc_app, CONFIG_SMTC_APP_LOG_LEVEL);
/* Number of leap seconds as of September 15th 2021 */
#define OFFSET_LEAP_SECONDS 18

void prv_event_process(void);
struct smtc_app_event_callbacks *prv_callbacks;
struct smtc_app_env_callbacks *prv_env_callbacks;
struct smtc_app_lorawan_cfg *prv_cfg;
static void prv_event_process(void);
static struct smtc_app_event_callbacks *prv_callbacks;
static struct smtc_app_env_callbacks *prv_env_callbacks;
static struct smtc_app_lorawan_cfg *prv_cfg;

/* Callbacks for HAL implementation */
static struct smtc_modem_hal_cb prv_hal_cb;

/* macro to ease repeated error checking in apps_modem_common_init */
#define SMTC_ERR_CHECK(func_name, rc) \
Expand Down Expand Up @@ -131,12 +134,27 @@ void smtc_app_init(const ralf_t *radio, struct smtc_app_event_callbacks *callbac
__ASSERT(radio, "radio must be provided");
__ASSERT(callbacks, "callbacks must be provided");
/* env_callbacks can be NULL */
#ifdef CONFIG_LORA_BASICS_MODEM_USER_STORAGE_IMPL
__ASSERT(env_callbacks->context_store, "context_store must be provided");
__ASSERT(env_callbacks->context_restore, "context_restore must be provided");

#endif
prv_callbacks = callbacks;
prv_env_callbacks = env_callbacks;

smtc_modem_hal_init((const struct device *)radio->ral.context, prv_get_battery_level_cb,
prv_get_temperature_cb, prv_get_voltage_cb);
/* Create callback structure for HAL impl */
prv_hal_cb = (struct smtc_modem_hal_cb){
.get_battery_level = prv_get_battery_level_cb,
.get_temperature = prv_get_temperature_cb,
.get_voltage = prv_get_voltage_cb,

#ifdef CONFIG_LORA_BASICS_MODEM_USER_STORAGE_IMPL
.context_store = env_callbacks->context_store,
.context_restore = env_callbacks->context_restore,
#endif
};

smtc_modem_hal_init((const struct device *)radio->ral.context, &prv_hal_cb);

smtc_modem_init(radio, &prv_event_process);
}
Expand Down Expand Up @@ -223,7 +241,7 @@ smtc_modem_return_code_t smtc_app_get_utc_time(uint32_t *utc_time)

/* PRIVATE EVENT PROCESSOR - this calls registered callbacks from app layer */

void prv_event_process(void)
static void prv_event_process(void)
{
smtc_modem_event_t current_event;
smtc_modem_return_code_t return_code = SMTC_MODEM_RC_OK;
Expand Down
37 changes: 35 additions & 2 deletions drivers/apps_common/smtc_app/smtc_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ struct smtc_app_event_callbacks {
* only if that DM info field is configrued to be sent.
*
* if the app does not provide the callbacks, a default (minimum) will be sent by the DM subsystem.
*
* If CONFIG_LORA_BASICS_MODEM_USER_STORAGE_IMPL=y, two additional callbacks MUST be implemented.
*/
struct smtc_app_env_callbacks {
/**
Expand Down Expand Up @@ -264,6 +266,33 @@ struct smtc_app_env_callbacks {
* level is available
*/
int (*get_voltage_level)(uint32_t *voltage_level);

#ifdef CONFIG_LORA_BASICS_MODEM_USER_STORAGE_IMPL

/**
* @brief Persistently store context from the lora basics modem
*
* The application should use some persistent storage to store the context.
*
* @param[in] ctx_id The ID of the context to store. Each ID must be stored separately.
* @param[in] buffer The buffer to store.
* @param[in] size The size of the buffer to store, in bytes.
*/
void (*context_store)(const uint8_t ctx_id, const uint8_t *buffer, const uint32_t size);

/**
* @brief Restore context to the lora basics modem
*
* The application should load the context from the persistent storage used in
* context_store.
*
* @param[in] ctx_id The ID of the context to restore.
* @param[in] buffer The buffer to read into.
* @param[in] size The size of the buffer, in bytes.
*/
void (*context_restore)(const uint8_t ctx_id, uint8_t *buffer, const uint32_t size);

#endif /* CONFIG_LORA_BASICS_MODEM_USER_STORAGE_IMPL */
};

/**
Expand All @@ -278,7 +307,8 @@ struct smtc_app_env_callbacks {
* be set to NULL.
* @param[in] env_callbacks Desired environment callbacks. Can be NULL. All callbacks that are not
* required by the application can be set to NULL.
* @return SMTC_MODEM_RC_OK if successfully, or other error code if unsuccessful
*
* @return SMTC_MODEM_RC_OK if successful, or other error code if unsuccessful
*/
void smtc_app_init(const ralf_t *radio, struct smtc_app_event_callbacks *callbacks,
struct smtc_app_env_callbacks *env_callbacks);
Expand All @@ -293,7 +323,8 @@ void smtc_app_init(const ralf_t *radio, struct smtc_app_event_callbacks *callbac
* @param[in] stack_id The stack ID to configure.
* @param[in] cfg The configuration to apply. The config struct must live for as long as the modem
* engine is in use.
* @return SMTC_MODEM_RC_OK if successfully, or other error code if unsuccessful
*
* @return SMTC_MODEM_RC_OK if successful, or other error code if unsuccessful
*/
smtc_modem_return_code_t smtc_app_configure_lorawan_params(uint8_t stack_id,
struct smtc_app_lorawan_cfg *cfg);
Expand All @@ -311,6 +342,7 @@ void smtc_app_display_versions(void);
* @brief Get time in gps epoch
*
* @param[out] gps_time The current gps time (number of seconds since the GPS epoch)
*
* @return smtc_modem_return_code_t SMTC_MODEM_RC_OK if time was read successfully, or other error
* code if unsuccessful
*/
Expand All @@ -320,6 +352,7 @@ smtc_modem_return_code_t smtc_app_get_gps_time(uint32_t *gps_time);
* @brief Get utc time
*
* @param[out] gps_time The current unix time (number of seconds since the unix epoch)
*
* @return SMTC_MODEM_RC_OK if time was read successfully, or other error
* code if unsuccessful
*/
Expand Down
106 changes: 77 additions & 29 deletions drivers/smtc_modem_hal_impl/smtc_modem_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ LOG_MODULE_REGISTER(smtc_modem_hal);
/* ------------ Local context ------------*/

/* lr11xx device pointer */
const struct device *prv_lr11xx_dev;
static const struct device *prv_lr11xx_dev;

/* environment getters */
static get_battery_level_cb_t prv_get_battery_level_cb;
static get_temperature_cb_t prv_get_temperature_cb;
static get_voltage_cb_t prv_get_voltage_cb;
/* External callbacks */
static struct smtc_modem_hal_cb *prv_hal_cb;

/* context and callback for modem_hal_timer */
static void *prv_smtc_modem_hal_timer_context;
Expand All @@ -55,34 +53,27 @@ static void *prv_smtc_modem_hal_radio_irq_context;
static void (*prv_smtc_modem_hal_radio_irq_callback)(void *context);
static bool prv_skip_next_radio_irq;

/* Context for loading from persistant storage */
static uint8_t *prv_load_into;
static uint32_t prv_load_size;

static int prv_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg);
static struct settings_handler prv_sh = {
.name = "smtc_modem_hal",
.h_set = prv_set,
};

/* ------------ Initialization ------------
*
* This function is defined in smtc_modem_hal_init.h
* and is used to set everything up in here.
*/

void smtc_modem_hal_init(const struct device *lr11xx, get_battery_level_cb_t get_battery,
get_temperature_cb_t get_temperature, get_voltage_cb_t get_voltage)
void smtc_modem_hal_init(const struct device *lr11xx, struct smtc_modem_hal_cb *hal_cb)
{
__ASSERT(lr11xx, "lr11xx must be provided");
__ASSERT(get_battery, "get_battery must be provided");
__ASSERT(get_temperature, "get_temperature must be provided");
__ASSERT(get_voltage, "get_voltage must be provided");
__ASSERT_NO_MSG(hal_cb);
__ASSERT_NO_MSG(hal_cb->get_battery_level);
__ASSERT_NO_MSG(hal_cb->get_temperature);
__ASSERT_NO_MSG(hal_cb->get_voltage);

#ifdef CONFIG_LORA_BASICS_MODEM_USER_STORAGE_IMPL
__ASSERT_NO_MSG(hal_cb->context_store);
__ASSERT_NO_MSG(hal_cb->context_restore);
#endif /* CONFIG_LORA_BASICS_MODEM_USER_STORAGE_IMPL */

prv_lr11xx_dev = lr11xx;
prv_get_battery_level_cb = get_battery;
prv_get_temperature_cb = get_temperature;
prv_get_voltage_cb = get_voltage;
prv_hal_cb = hal_cb;
}

/* ------------ Reset management ------------*/
Expand Down Expand Up @@ -231,6 +222,57 @@ void smtc_modem_hal_enable_modem_irq(void)

/* ------------ Context saving management ------------*/

#ifdef CONFIG_LORA_BASICS_MODEM_USER_STORAGE_IMPL

void smtc_modem_hal_context_restore(const modem_context_type_t ctx_type, uint8_t *buffer,
const uint32_t size)
{
prv_hal_cb->context_restore(ctx_type, buffer, size);
}

void smtc_modem_hal_context_store(const modem_context_type_t ctx_type, const uint8_t *buffer,
const uint32_t size)
{
prv_hal_cb->context_store(ctx_type, buffer, size);
}

void smtc_modem_hal_store_crashlog(uint8_t crashlog[CRASH_LOG_SIZE])
{
/* We use MODEM_CONTEXT_TYPE_SIZE as the ID so we do not overwrite any of the contexts */
prv_hal_cb->context_store(MODEM_CONTEXT_TYPE_SIZE, crashlog, CRASH_LOG_SIZE);
}

void smtc_modem_hal_restore_crashlog(uint8_t crashlog[CRASH_LOG_SIZE])
{
prv_hal_cb->context_restore(MODEM_CONTEXT_TYPE_SIZE, crashlog, CRASH_LOG_SIZE);
}

void smtc_modem_hal_set_crashlog_status(bool available)
{
prv_hal_cb->context_store(MODEM_CONTEXT_TYPE_SIZE + 1, (uint8_t *)&available,
sizeof(available));
}

bool smtc_modem_hal_get_crashlog_status(void)
{
bool available;
prv_hal_cb->context_restore(MODEM_CONTEXT_TYPE_SIZE + 1, (uint8_t *)&available,
sizeof(available));
return available;
}

#else

/* Context for loading from persistant storage */
static uint8_t *prv_load_into;
static uint32_t prv_load_size;

static int prv_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg);
static struct settings_handler prv_sh = {
.name = "smtc_modem_hal",
.h_set = prv_set,
};

/**
* @brief Called when settings_load_subtree in prv_load is called.
*
Expand Down Expand Up @@ -292,7 +334,6 @@ static void prv_store(char *path, const uint8_t *buffer, const uint32_t size)
*/
static void prv_load(char *path, uint8_t *buffer, const uint32_t size)
{

prv_settings_init();

prv_load_into = buffer;
Expand All @@ -303,7 +344,6 @@ static void prv_load(char *path, uint8_t *buffer, const uint32_t size)
void smtc_modem_hal_context_restore(const modem_context_type_t ctx_type, uint8_t *buffer,
const uint32_t size)
{

char path[30];
snprintk(path, sizeof(path), "smtc_modem_hal/context/%d", ctx_type);
prv_load(path, buffer, size);
Expand All @@ -312,7 +352,6 @@ void smtc_modem_hal_context_restore(const modem_context_type_t ctx_type, uint8_t
void smtc_modem_hal_context_store(const modem_context_type_t ctx_type, const uint8_t *buffer,
const uint32_t size)
{

char path[30];
snprintk(path, sizeof(path), "smtc_modem_hal/context/%d", ctx_type);
prv_store(path, buffer, size);
Expand Down Expand Up @@ -340,6 +379,8 @@ bool smtc_modem_hal_get_crashlog_status(void)
return available;
}

#endif /* CONFIG_LORA_BASICS_MODEM_USER_STORAGE_IMPL */

/* ------------ assert management ------------*/

void smtc_modem_hal_assert_fail(uint8_t *func, uint32_t line)
Expand Down Expand Up @@ -428,6 +469,13 @@ void smtc_modem_hal_irq_config_radio_irq(void (*callback)(void *context), void *
lr11xx_board_enable_interrupt(prv_lr11xx_dev);
}

void smtc_modem_hal_irq_reset_radio_irq(void)
{
lr11xx_board_disable_interrupt(prv_lr11xx_dev);
lr11xx_board_attach_interrupt(prv_lr11xx_dev, prv_lr11xx_event_cb);
lr11xx_board_enable_interrupt(prv_lr11xx_dev);
}

void smtc_modem_hal_radio_irq_clear_pending(void)
{
LOG_DBG("Clear pending radio irq");
Expand Down Expand Up @@ -464,7 +512,7 @@ uint32_t smtc_modem_hal_get_radio_tcxo_startup_delay_ms(void)
uint8_t smtc_modem_hal_get_battery_level(void)
{
uint32_t battery;
int ret = prv_get_battery_level_cb(&battery);
int ret = prv_hal_cb->get_battery_level(&battery);

if (ret) {
return 0;
Expand All @@ -482,7 +530,7 @@ uint8_t smtc_modem_hal_get_battery_level(void)
int8_t smtc_modem_hal_get_temperature(void)
{
int32_t temperature;
int ret = prv_get_temperature_cb(&temperature);
int ret = prv_hal_cb->get_temperature(&temperature);

if (ret) {
return -128;
Expand All @@ -501,7 +549,7 @@ int8_t smtc_modem_hal_get_temperature(void)
uint8_t smtc_modem_hal_get_voltage(void)
{
uint32_t voltage;
int ret = prv_get_voltage_cb(&voltage);
int ret = prv_hal_cb->get_voltage(&voltage);

if (ret) {
return 0;
Expand Down
Loading

0 comments on commit 706b3e2

Please sign in to comment.