Skip to content

Commit

Permalink
use pstorage to store config
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Nov 18, 2024
1 parent 0cfacae commit c0a9d99
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@
# To explicitly override the above, define any exceptions here; e.g.:
# !my_customized_scatter_file.sct

EventRecorderStub.scvd
EventRecorderStub.scvd
JLinkLog.txt
89 changes: 69 additions & 20 deletions EPD/EPD_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#include "ble_srv_common.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf_nvmc.h"
#include "nrf_soc.h"
#include "nrf_log.h"
#include "pstorage.h"
#include "EPD_4in2.h"
#include "EPD_4in2_V2.h"
#include "EPD_4in2b_V2.h"
Expand All @@ -31,6 +31,8 @@
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#define EPD_CONFIG_SIZE (sizeof(epd_config_t) / sizeof(uint8_t))

static pstorage_handle_t m_flash_handle;

/** EPD drivers */
static epd_driver_t epd_drivers[] = {
{EPD_DRIVER_4IN2, EPD_4IN2_Init, EPD_4IN2_Clear,
Expand All @@ -56,15 +58,20 @@ static epd_driver_t *epd_driver_get(uint8_t id)
return NULL;
}

static void epd_config_load(epd_config_t *cfg)
static uint32_t epd_config_load(epd_config_t *cfg)
{
memcpy(cfg, (void *)BLE_EPD_CONFIG_ADDR, sizeof(epd_config_t));
return pstorage_load((uint8_t *)cfg, &m_flash_handle, sizeof(epd_config_t), 0);
}

static void epd_config_save(epd_config_t *cfg)
{
nrf_nvmc_page_erase(BLE_EPD_CONFIG_ADDR);
nrf_nvmc_write_bytes(BLE_EPD_CONFIG_ADDR, (uint8_t*)cfg, EPD_CONFIG_SIZE);
static uint32_t epd_config_save(epd_config_t *cfg)
{
uint32_t err_code;
err_code = pstorage_clear(&m_flash_handle, sizeof(epd_config_t));
if (err_code != NRF_SUCCESS)
{
return err_code;
}
return pstorage_store(&m_flash_handle, (uint8_t *)cfg, sizeof(epd_config_t), 0);
}

/**@brief Function for handling the @ref BLE_GAP_EVT_CONNECTED event from the S110 SoftDevice.
Expand All @@ -74,6 +81,10 @@ static void epd_config_save(epd_config_t *cfg)
*/
static void on_connect(ble_epd_t * p_epd, ble_evt_t * p_ble_evt)
{
if (p_epd->config.led_pin != 0xFF)
{
nrf_gpio_pin_toggle(p_epd->config.led_pin);
}
p_epd->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
DEV_Module_Init();
}
Expand All @@ -87,6 +98,10 @@ static void on_connect(ble_epd_t * p_epd, ble_evt_t * p_ble_evt)
static void on_disconnect(ble_epd_t * p_epd, ble_evt_t * p_ble_evt)
{
UNUSED_PARAMETER(p_ble_evt);
if (p_epd->config.led_pin != 0xFF)
{
nrf_gpio_pin_toggle(p_epd->config.led_pin);
}
p_epd->conn_handle = BLE_CONN_HANDLE_INVALID;
DEV_Module_Exit();
}
Expand All @@ -96,6 +111,8 @@ static void epd_service_process(ble_epd_t * p_epd, uint8_t * p_data, uint16_t le
if (p_data == NULL || length <= 0) return;
NRF_LOG_PRINTF("[EPD]: CMD=0x%02x, LEN=%d\n", p_data[0], length);

uint32_t err_code;

switch (p_data[0])
{
case EPD_CMD_SET_PINS:
Expand All @@ -110,7 +127,8 @@ static void epd_service_process(ble_epd_t * p_epd, uint8_t * p_data, uint16_t le
EPD_RST_PIN = p_epd->config.rst_pin = p_data[5];
EPD_BUSY_PIN = p_epd->config.busy_pin = p_data[6];
EPD_BS_PIN = p_epd->config.bs_pin = p_data[7];
epd_config_save(&p_epd->config);
err_code = epd_config_save(&p_epd->config);
NRF_LOG_PRINTF("epd_config_save: %d\n", err_code);

NRF_LOG_PRINTF("[EPD]: MOSI=0x%02x SCLK=0x%02x CS=0x%02x DC=0x%02x RST=0x%02x BUSY=0x%02x BS=0x%02x\n",
EPD_MOSI_PIN, EPD_SCLK_PIN, EPD_CS_PIN, EPD_DC_PIN, EPD_RST_PIN, EPD_BUSY_PIN, EPD_BS_PIN);
Expand All @@ -125,7 +143,8 @@ static void epd_service_process(ble_epd_t * p_epd, uint8_t * p_data, uint16_t le
{
p_epd->driver = driver;
p_epd->config.driver_id = driver->id;
epd_config_save(&p_epd->config);
err_code = epd_config_save(&p_epd->config);
NRF_LOG_PRINTF("epd_config_save: %d\n", err_code);
}
}

Expand Down Expand Up @@ -225,12 +244,10 @@ void ble_epd_on_ble_evt(ble_epd_t * p_epd, ble_evt_t * p_ble_evt)
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
nrf_gpio_pin_toggle(p_epd->config.led_pin);
on_connect(p_epd, p_ble_evt);
break;

case BLE_GAP_EVT_DISCONNECTED:
nrf_gpio_pin_toggle(p_epd->config.led_pin);
on_disconnect(p_epd, p_ble_evt);
break;

Expand Down Expand Up @@ -349,9 +366,24 @@ static void epd_config_init(ble_epd_t * p_epd)
void ble_epd_sleep_prepare(ble_epd_t * p_epd)
{
// Turn off led
nrf_gpio_pin_set(p_epd->config.led_pin);
if (p_epd->config.led_pin != 0xFF)
{
nrf_gpio_pin_set(p_epd->config.led_pin);
}
// Prepare wakeup pin
nrf_gpio_cfg_sense_input(p_epd->config.wakeup_pin, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_HIGH);
if (p_epd->config.wakeup_pin != 0xFF)
{
nrf_gpio_cfg_sense_input(p_epd->config.wakeup_pin, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_HIGH);
}
}

static void pstorage_callback(pstorage_handle_t * p_handle,
uint8_t op_code,
uint32_t result,
uint8_t * p_data,
uint32_t data_len)
{
NRF_LOG_PRINTF("pstorage_callback: op_code=%d, result=%d\n", op_code, result);
}

uint32_t ble_epd_init(ble_epd_t * p_epd)
Expand All @@ -364,16 +396,33 @@ uint32_t ble_epd_init(ble_epd_t * p_epd)
// Initialize the service structure.
p_epd->conn_handle = BLE_CONN_HANDLE_INVALID;
p_epd->is_notification_enabled = false;

uint32_t err_code;
pstorage_module_param_t param;

// Load epd config
epd_config_load(&p_epd->config);
epd_config_init(p_epd);
param.block_count = 1;
param.block_size = sizeof(epd_config_t);
param.cb = pstorage_callback;

err_code = pstorage_register(&param, &m_flash_handle);
if (err_code == NRF_SUCCESS)
{
// Load epd config
err_code = epd_config_load(&p_epd->config);
if (err_code == NRF_SUCCESS)
{
epd_config_init(p_epd);
}
}

// Init led pin
nrf_gpio_cfg_output(p_epd->config.led_pin);
nrf_gpio_pin_clear(p_epd->config.led_pin);
nrf_delay_ms(50);
nrf_gpio_pin_set(p_epd->config.led_pin);
if (p_epd->config.led_pin != 0xFF)
{
nrf_gpio_cfg_output(p_epd->config.led_pin);
nrf_gpio_pin_clear(p_epd->config.led_pin);
nrf_delay_ms(50);
nrf_gpio_pin_set(p_epd->config.led_pin);
}

// Add the service.
return epd_service_init(p_epd);
Expand Down
2 changes: 2 additions & 0 deletions EPD/EPD_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ typedef struct
uint8_t driver_id;
uint8_t wakeup_pin;
uint8_t led_pin;

uint8_t reserved[6];
} epd_config_t;

/**< EPD Service command IDs. */
Expand Down
32 changes: 16 additions & 16 deletions Keil/EPD.uvprojx
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@
<FileType>5</FileType>
<FilePath>..\config\nrf_drv_config.h</FilePath>
</File>
<File>
<FileName>pstorage_platform.h</FileName>
<FileType>5</FileType>
<FilePath>..\config\pstorage_platform.h</FilePath>
</File>
</Files>
</Group>
<Group>
Expand Down Expand Up @@ -478,11 +483,6 @@
<FileType>1</FileType>
<FilePath>..\components\drivers_nrf\spi_master\nrf_drv_spi.c</FilePath>
</File>
<File>
<FileName>nrf_nvmc.c</FileName>
<FileType>1</FileType>
<FilePath>..\components\drivers_nrf\hal\nrf_nvmc.c</FilePath>
</File>
<File>
<FileName>pstorage.c</FileName>
<FileType>1</FileType>
Expand Down Expand Up @@ -886,7 +886,7 @@
<MiscControls></MiscControls>
<Define>BLE_STACK_SUPPORT_REQD S110 SWI_DISABLE0 SOFTDEVICE_PRESENT NRF51 DEBUG NRF_LOG_USES_RTT=1</Define>
<Undefine></Undefine>
<IncludePath>..\config;..\EPD;..\components\toolchain;..\components\drivers_nrf\config;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\pstorage;..\components\drivers_nrf\pstorage\config;..\components\drivers_ext\segger_rtt;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s110\headers</IncludePath>
<IncludePath>..\config;..\EPD;..\components\toolchain;..\components\drivers_nrf\config;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\pstorage;..\components\drivers_ext\segger_rtt;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s110\headers</IncludePath>
</VariousControls>
</Cads>
<Aads>
Expand Down Expand Up @@ -945,6 +945,11 @@
<FileType>5</FileType>
<FilePath>..\config\nrf_drv_config.h</FilePath>
</File>
<File>
<FileName>pstorage_platform.h</FileName>
<FileType>5</FileType>
<FilePath>..\config\pstorage_platform.h</FilePath>
</File>
</Files>
</Group>
<Group>
Expand Down Expand Up @@ -1025,11 +1030,6 @@
<FileType>1</FileType>
<FilePath>..\components\drivers_nrf\spi_master\nrf_drv_spi.c</FilePath>
</File>
<File>
<FileName>nrf_nvmc.c</FileName>
<FileType>1</FileType>
<FilePath>..\components\drivers_nrf\hal\nrf_nvmc.c</FilePath>
</File>
<File>
<FileName>pstorage.c</FileName>
<FileType>1</FileType>
Expand Down Expand Up @@ -1492,6 +1492,11 @@
<FileType>5</FileType>
<FilePath>..\config\nrf_drv_config.h</FilePath>
</File>
<File>
<FileName>pstorage_platform.h</FileName>
<FileType>5</FileType>
<FilePath>..\config\pstorage_platform.h</FilePath>
</File>
</Files>
</Group>
<Group>
Expand Down Expand Up @@ -1572,11 +1577,6 @@
<FileType>1</FileType>
<FilePath>..\components\drivers_nrf\spi_master\nrf_drv_spi.c</FilePath>
</File>
<File>
<FileName>nrf_nvmc.c</FileName>
<FileType>1</FileType>
<FilePath>..\components\drivers_nrf\hal\nrf_nvmc.c</FilePath>
</File>
<File>
<FileName>pstorage.c</FileName>
<FileType>1</FileType>
Expand Down
78 changes: 78 additions & 0 deletions config/pstorage_platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/

/** @cond To make doxygen skip this file */

/** @file
* This header contains defines with respect persistent storage that are specific to
* persistent storage implementation and application use case.
*/
#ifndef PSTORAGE_PL_H__
#define PSTORAGE_PL_H__

#include <stdint.h>
#include "nrf.h"

static __INLINE uint16_t pstorage_flash_page_size()
{
return (uint16_t)NRF_FICR->CODEPAGESIZE;
}

#define PSTORAGE_FLASH_PAGE_SIZE pstorage_flash_page_size() /**< Size of one flash page. */
#define PSTORAGE_FLASH_EMPTY_MASK 0xFFFFFFFF /**< Bit mask that defines an empty address in flash. */

#ifdef NRF51
#define BOOTLOADER_ADDRESS (NRF_UICR->BOOTLOADERADDR)
#elif defined NRF52
#define BOOTLOADER_ADDRESS (PSTORAGE_FLASH_EMPTY_MASK)
#endif

static __INLINE uint32_t pstorage_flash_page_end()
{
uint32_t bootloader_addr = BOOTLOADER_ADDRESS;

return ((bootloader_addr != PSTORAGE_FLASH_EMPTY_MASK) ?
(bootloader_addr/ PSTORAGE_FLASH_PAGE_SIZE) : NRF_FICR->CODESIZE);
}

#define PSTORAGE_FLASH_PAGE_END pstorage_flash_page_end()

#define PSTORAGE_NUM_OF_PAGES 1 /**< Number of flash pages allocated for the pstorage module excluding the swap page, configurable based on system requirements. */
#define PSTORAGE_MIN_BLOCK_SIZE 0x0010 /**< Minimum size of block that can be registered with the module. Should be configured based on system requirements, recommendation is not have this value to be at least size of word. */

#define PSTORAGE_DATA_START_ADDR ((PSTORAGE_FLASH_PAGE_END - PSTORAGE_NUM_OF_PAGES - 1) \
* PSTORAGE_FLASH_PAGE_SIZE) /**< Start address for persistent data, configurable according to system requirements. */
#define PSTORAGE_DATA_END_ADDR ((PSTORAGE_FLASH_PAGE_END - 1) * PSTORAGE_FLASH_PAGE_SIZE) /**< End address for persistent data, configurable according to system requirements. */
#define PSTORAGE_SWAP_ADDR PSTORAGE_DATA_END_ADDR /**< Top-most page is used as swap area for clear and update. */

#define PSTORAGE_MAX_BLOCK_SIZE PSTORAGE_FLASH_PAGE_SIZE /**< Maximum size of block that can be registered with the module. Should be configured based on system requirements. And should be greater than or equal to the minimum size. */
#define PSTORAGE_CMD_QUEUE_SIZE 10 /**< Maximum number of flash access commands that can be maintained by the module for all applications. Configurable. */


/** Abstracts persistently memory block identifier. */
typedef uint32_t pstorage_block_t;

typedef struct
{
uint32_t module_id; /**< Module ID.*/
pstorage_block_t block_id; /**< Block ID.*/
} pstorage_handle_t;

typedef uint16_t pstorage_size_t; /** Size of length and offset fields. */

/**@brief Handles Flash Access Result Events. To be called in the system event dispatcher of the application. */
void pstorage_sys_event_handler (uint32_t sys_evt);

#endif // PSTORAGE_PL_H__

/** @} */
/** @endcond */
9 changes: 7 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
#include "ble_advertising.h"
#include "ble_conn_params.h"
#include "softdevice_handler.h"
#include "pstorage.h"
#include "app_error.h"
#include "app_timer.h"
#include "EPD_ble.h"

#define IS_SRVC_CHANGED_CHARACT_PRESENT 1 /**< Include or not the service_changed characteristic. if not enabled, the server's database cannot be changed for the lifetime of the device*/

#define DEVICE_NAME "NRF_EPD" /**< Name of device. Will be included in the advertising data. */
#define APP_ADV_INTERVAL 300 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
#define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout (in units of seconds). */
#define APP_ADV_INTERVAL 300 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
#define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout (in units of seconds). */
#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
#define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */

Expand Down Expand Up @@ -82,6 +83,9 @@ static void services_init(void)
{
uint32_t err_code;

err_code = pstorage_init();
APP_ERROR_CHECK(err_code);

memset(&m_epd, 0, sizeof(ble_epd_t));
err_code = ble_epd_init(&m_epd);
APP_ERROR_CHECK(err_code);
Expand Down Expand Up @@ -293,6 +297,7 @@ static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
*/
static void sys_evt_dispatch(uint32_t sys_evt)
{
pstorage_sys_event_handler(sys_evt);
ble_advertising_on_sys_evt(sys_evt);
}

Expand Down

0 comments on commit c0a9d99

Please sign in to comment.