update flash_nrf52 to support sd
This commit is contained in:
1 parent
2c08159af8
commit
bed0a8f91f
6 files changed
+108
-76
No files matched your search
@@ -43,12 +43,12 @@
|
||||
//--------------------------------------------------------------------+
|
||||
static inline uint32_t page_addr_of (uint32_t addr)
|
||||
{
|
||||
return addr & ~(FLASH_API_PAGE_SIZE - 1);
|
||||
return addr & ~(FLASH_CACHE_PAGE_SIZE - 1);
|
||||
}
|
||||
|
||||
static inline uint32_t page_offset_of (uint32_t addr)
|
||||
{
|
||||
return addr & (FLASH_API_PAGE_SIZE - 1);
|
||||
return addr & (FLASH_CACHE_PAGE_SIZE - 1);
|
||||
}
|
||||
|
||||
uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, uint32_t len)
|
||||
@@ -62,7 +62,7 @@ uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, u
|
||||
uint32_t const page_addr = page_addr_of(dst);
|
||||
uint32_t const offset = page_offset_of(dst);
|
||||
|
||||
uint32_t wr_bytes = FLASH_API_PAGE_SIZE - offset;
|
||||
uint32_t wr_bytes = FLASH_CACHE_PAGE_SIZE - offset;
|
||||
wr_bytes = min32(remain, wr_bytes);
|
||||
|
||||
// Page changes, flush old and update new cache
|
||||
@@ -78,9 +78,9 @@ uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, u
|
||||
}
|
||||
|
||||
uint32_t const last_byte = offset + wr_bytes;
|
||||
if ( last_byte < FLASH_API_PAGE_SIZE )
|
||||
if ( last_byte < FLASH_CACHE_PAGE_SIZE )
|
||||
{
|
||||
fc->read(fc->cache_buf + last_byte, page_addr + last_byte, FLASH_API_PAGE_SIZE - last_byte);
|
||||
fc->read(fc->cache_buf + last_byte, page_addr + last_byte, FLASH_CACHE_PAGE_SIZE - last_byte);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,10 +99,10 @@ void flash_cache_flush (flash_cache_t* fc)
|
||||
if ( fc->cache_addr == FLASH_CACHE_INVALID_ADDR ) return;
|
||||
|
||||
// skip erase & program if verify() exists, and memory matches
|
||||
if ( !(fc->verify && fc->verify(fc->cache_addr, fc->cache_buf, FLASH_API_PAGE_SIZE)) )
|
||||
if ( !(fc->verify && fc->verify(fc->cache_addr, fc->cache_buf, FLASH_CACHE_PAGE_SIZE)) )
|
||||
{
|
||||
fc->erase(fc->cache_addr);
|
||||
fc->program(fc->cache_addr, fc->cache_buf, FLASH_API_PAGE_SIZE);
|
||||
fc->program(fc->cache_addr, fc->cache_buf, FLASH_CACHE_PAGE_SIZE);
|
||||
}
|
||||
|
||||
fc->cache_addr = FLASH_CACHE_INVALID_ADDR;
|
||||
|
||||
@@ -40,18 +40,18 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define FLASH_API_PAGE_SIZE 4096
|
||||
#define FLASH_CACHE_PAGE_SIZE 4096
|
||||
#define FLASH_CACHE_INVALID_ADDR 0xffffffff
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void (*erase) (uint32_t addr);
|
||||
bool (*erase) (uint32_t addr);
|
||||
uint32_t (*program) (uint32_t dst, void const * src, uint32_t len);
|
||||
uint32_t (*read) (void* dst, uint32_t src, uint32_t len);
|
||||
bool (*verify) (uint32_t addr, void const * buf, uint32_t len);
|
||||
|
||||
uint32_t cache_addr;
|
||||
uint8_t cache_buf[FLASH_API_PAGE_SIZE] __attribute__((aligned(4)));
|
||||
uint8_t cache_buf[FLASH_CACHE_PAGE_SIZE] __attribute__((aligned(4)));
|
||||
} flash_cache_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -34,16 +34,25 @@
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#include "../flash/flash_nrf52.h"
|
||||
|
||||
#include "../flash/flash_cache.h"
|
||||
#include "flash_nrf52.h"
|
||||
#include "flash_cache.h"
|
||||
#include "nrf_soc.h"
|
||||
#include "rtos.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
static SemaphoreHandle_t _sem = NULL;
|
||||
static volatile uint32_t _op_result;
|
||||
|
||||
void flash_nrf52_event_cb (uint32_t event)
|
||||
{
|
||||
_op_result = event;
|
||||
xSemaphoreGive(_sem);
|
||||
}
|
||||
|
||||
// Flash Abstraction Layer
|
||||
static void fal_erase (uint32_t addr);
|
||||
static bool fal_erase (uint32_t addr);
|
||||
static uint32_t fal_program (uint32_t dst, void const * src, uint32_t len);
|
||||
static uint32_t fal_read (void* dst, uint32_t src, uint32_t len);
|
||||
static bool fal_verify (uint32_t addr, void const * buf, uint32_t len);
|
||||
@@ -78,16 +87,44 @@ uint32_t flash_nrf52_read (void* dst, uint32_t src, uint32_t len)
|
||||
//--------------------------------------------------------------------+
|
||||
// HAL for caching
|
||||
//--------------------------------------------------------------------+
|
||||
static void fal_erase (uint32_t addr)
|
||||
static bool fal_erase (uint32_t addr)
|
||||
{
|
||||
// TODO SD enabled
|
||||
nrf_nvmc_page_erase(addr);
|
||||
// Init semaphore for first call
|
||||
if ( !_sem )
|
||||
{
|
||||
_sem = xSemaphoreCreateBinary();
|
||||
VERIFY(_sem);
|
||||
}
|
||||
|
||||
// delay and retry if busy
|
||||
uint32_t err;
|
||||
while ( NRF_ERROR_BUSY == (err = sd_flash_page_erase(addr / FLASH_CACHE_PAGE_SIZE)) )
|
||||
{
|
||||
delay(1);
|
||||
}
|
||||
VERIFY_STATUS(err);
|
||||
|
||||
// wait until complete
|
||||
xSemaphoreTake(_sem, portMAX_DELAY);
|
||||
VERIFY(_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint32_t fal_program (uint32_t dst, void const * src, uint32_t len)
|
||||
{
|
||||
// TODO SD enabled
|
||||
nrf_nvmc_write_words(dst, (uint32_t const *) src, len / 4);
|
||||
// delay and try again if busy
|
||||
uint32_t err;
|
||||
while ( NRF_ERROR_BUSY == (err = sd_flash_write((uint32_t*) dst, (uint32_t const *) src, len / 4)) )
|
||||
{
|
||||
delay(1);
|
||||
}
|
||||
VERIFY_STATUS(err);
|
||||
|
||||
// wait until complete
|
||||
xSemaphoreTake(_sem, portMAX_DELAY);
|
||||
VERIFY(_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,8 +37,7 @@
|
||||
#ifndef FLASH_NRF52_H_
|
||||
#define FLASH_NRF52_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "common_inc.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
||||
@@ -55,7 +55,7 @@ AdafruitBluefruit Bluefruit;
|
||||
*------------------------------------------------------------------*/
|
||||
extern "C"
|
||||
{
|
||||
void hal_flash_event_cb(uint32_t event) ATTR_WEAK;
|
||||
void flash_nrf52_event_cb (uint32_t event) ATTR_WEAK;
|
||||
}
|
||||
|
||||
void adafruit_ble_task(void* arg);
|
||||
@@ -801,7 +801,7 @@ void adafruit_soc_task(void* arg)
|
||||
// Flash
|
||||
case NRF_EVT_FLASH_OPERATION_SUCCESS:
|
||||
case NRF_EVT_FLASH_OPERATION_ERROR:
|
||||
if (hal_flash_event_cb) hal_flash_event_cb(soc_evt);
|
||||
if ( flash_nrf52_event_cb ) flash_nrf52_event_cb(soc_evt);
|
||||
break;
|
||||
|
||||
/*------------- usb power event handler -------------*/
|
||||
|
||||
@@ -80,9 +80,35 @@ void bond_init(void)
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Keys
|
||||
*------------------------------------------------------------------*/
|
||||
#if 0
|
||||
static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bond_data_t* bdata)
|
||||
{
|
||||
uint16_t const ediv = (role == BLE_GAP_ROLE_PERIPH) ? bdata->own_enc.master_id.ediv : bdata->peer_enc.master_id.ediv;
|
||||
|
||||
//------------- save keys -------------//
|
||||
uint32_t fl_addr = BOND_FLASH_ADDR;
|
||||
uint8_t const keylen = sizeof(bond_data_t);
|
||||
|
||||
fl_addr += flash_nrf52_write8(fl_addr, sizeof(bond_data_t));
|
||||
fl_addr += flash_nrf52_write(fl_addr, bdata, sizeof(bond_data_t));
|
||||
|
||||
//------------- save device name -------------//
|
||||
char devname[64] = { 0 };
|
||||
uint8_t namelen = Bluefruit.Gap.getPeerName(conn_hdl, devname, sizeof(devname));
|
||||
|
||||
// If couldn't get devname use peer mac address
|
||||
if ( namelen == 0 )
|
||||
{
|
||||
uint8_t* mac = bdata->peer_id.id_addr_info.addr;
|
||||
namelen = sprintf(devname, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
|
||||
}
|
||||
|
||||
fl_addr += flash_nrf52_write8(fl_addr, namelen + 1); // also include null char
|
||||
fl_addr += flash_nrf52_write(fl_addr, devname, namelen);
|
||||
fl_addr += flash_nrf52_write8(fl_addr, 0); // null char
|
||||
|
||||
LOG_LV2("BOND", "Keys for \"%s\" is saved", devname);
|
||||
|
||||
#if 0
|
||||
char filename[BOND_FNAME_LEN];
|
||||
get_fname(filename, role, role == BLE_GAP_ROLE_PERIPH ? bdata->own_enc.master_id.ediv : bdata->peer_enc.master_id.ediv);
|
||||
|
||||
@@ -120,48 +146,20 @@ static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bond_data_t* bd
|
||||
}
|
||||
|
||||
printBondDir(role);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool bond_save_keys (uint8_t role, uint16_t conn_hdl, bond_data_t* bdata)
|
||||
{
|
||||
uint16_t const ediv = (role == BLE_GAP_ROLE_PERIPH) ? bdata->own_enc.master_id.ediv : bdata->peer_enc.master_id.ediv;
|
||||
|
||||
//------------- save keys -------------//
|
||||
uint32_t fl_addr = BOND_FLASH_ADDR;
|
||||
uint8_t const keylen = sizeof(bond_data_t);
|
||||
|
||||
fl_addr += flash_nrf52_write8(fl_addr, sizeof(bond_data_t));
|
||||
fl_addr += flash_nrf52_write(fl_addr, bdata, sizeof(bond_data_t));
|
||||
|
||||
//------------- save device name -------------//
|
||||
char devname[64] = { 0 };
|
||||
uint8_t namelen = Bluefruit.Gap.getPeerName(conn_hdl, devname, sizeof(devname));
|
||||
|
||||
// If couldn't get devname use peer mac address
|
||||
if ( namelen == 0 )
|
||||
{
|
||||
uint8_t* mac = bdata->peer_id.id_addr_info.addr;
|
||||
namelen = sprintf(devname, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
|
||||
}
|
||||
|
||||
fl_addr += flash_nrf52_write8(fl_addr, namelen + 1); // also include null char
|
||||
fl_addr += flash_nrf52_write(fl_addr, devname, namelen);
|
||||
fl_addr += flash_nrf52_write8(fl_addr, 0); // null char
|
||||
|
||||
LOG_LV2("BOND", "Keys for \"%s\" is saved", devname);
|
||||
|
||||
return true;
|
||||
|
||||
#if 0
|
||||
uint8_t* buf = (uint8_t*) rtos_malloc( sizeof(bond_data_t) );
|
||||
VERIFY(buf, );
|
||||
VERIFY(buf);
|
||||
|
||||
memcpy(buf, bdata, sizeof(bond_data_t));
|
||||
|
||||
// queue to execute in Ada Callback thread
|
||||
ada_callback(buf, bond_save_keys_dfr, role, conn_hdl, buf);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata)
|
||||
@@ -195,9 +193,23 @@ bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata)
|
||||
/*------------------------------------------------------------------*/
|
||||
/* CCCD
|
||||
*------------------------------------------------------------------*/
|
||||
#if 0
|
||||
static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, uint16_t ediv)
|
||||
{
|
||||
uint16_t alen = 0;
|
||||
sd_ble_gatts_sys_attr_get(conn_hdl, NULL, &alen, SVC_CONTEXT_FLAG);
|
||||
|
||||
uint8_t attr[alen];
|
||||
|
||||
VERIFY(ERROR_NONE == sd_ble_gatts_sys_attr_get(conn_hdl, attr, &alen, SVC_CONTEXT_FLAG),);
|
||||
|
||||
(void) role;
|
||||
uint32_t fl_addr = BOND_FLASH_ADDR + 256;
|
||||
|
||||
flash_nrf52_write8(fl_addr++, alen);
|
||||
flash_nrf52_write(fl_addr, attr, alen);
|
||||
LOG_LV2("BOND", "CCCD setting is saved");
|
||||
|
||||
#if 0
|
||||
uint16_t len=0;
|
||||
sd_ble_gatts_sys_attr_get(conn_hdl, NULL, &len, SVC_CONTEXT_FLAG);
|
||||
|
||||
@@ -222,33 +234,17 @@ static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, uint16_t ediv)
|
||||
|
||||
rtos_free(sys_attr);
|
||||
printBondDir(role);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool bond_save_cccd (uint8_t role, uint16_t conn_hdl, uint16_t ediv)
|
||||
{
|
||||
VERIFY(ediv != 0xFFFF);
|
||||
|
||||
uint16_t alen = 0;
|
||||
sd_ble_gatts_sys_attr_get(conn_hdl, NULL, &alen, SVC_CONTEXT_FLAG);
|
||||
|
||||
uint8_t attr[alen];
|
||||
|
||||
VERIFY(ERROR_NONE == sd_ble_gatts_sys_attr_get(conn_hdl, attr, &alen, SVC_CONTEXT_FLAG));
|
||||
|
||||
(void) role;
|
||||
uint32_t fl_addr = BOND_FLASH_ADDR + 256;
|
||||
|
||||
flash_nrf52_write8(fl_addr++, alen);
|
||||
flash_nrf52_write(fl_addr, attr, alen);
|
||||
LOG_LV2("BOND", "CCCD setting is saved");
|
||||
// queue to execute in Ada Callback thread
|
||||
ada_callback(NULL, bond_save_cccd_dfr, role, conn_hdl, ediv);
|
||||
|
||||
return true;
|
||||
|
||||
#if 0
|
||||
// queue to execute in Ada Callback thread
|
||||
ada_callback(NULL, bond_save_cccd_dfr, role, cond_hdl, ediv);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user