finish nffs demo
This commit is contained in:
1 parent
be9622716a
commit
5885868343
16 files changed
+202
-36
No files matched your search
@@ -55,6 +55,7 @@ uint32_t setLoopStacksize(void);
|
||||
|
||||
#include "delay.h"
|
||||
#include "binary.h"
|
||||
#include "debug.h"
|
||||
#include "common_inc.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -89,16 +89,17 @@
|
||||
#define malloc_named( name, size ) malloc ( size )
|
||||
#endif
|
||||
|
||||
#define PRINT_LOCATION() Serial.printf("%s: %d:\n", __PRETTY_FUNCTION__, __LINE__)
|
||||
#define PRTNT_HEAP() if (CFG_DEBUG == 3) Serial.printf("\n%s: %d: Heap free: %d\n", __PRETTY_FUNCTION__, __LINE__, util_heap_get_free_size())
|
||||
#define PRINT_INT(x) Serial.printf("%s: %d: " #x " = %ld\n" , __PRETTY_FUNCTION__, __LINE__, (uint32_t) (x) )
|
||||
#define PRINT_HEX(x) Serial.printf("%s: %d: " #x " = %08lx\n", __PRETTY_FUNCTION__, __LINE__, (uint32_t) (x) )
|
||||
#define PRINT_STR(x) Serial.printf("%s: %d: " #x " = %s\n" , __PRETTY_FUNCTION__, __LINE__, (char*)(x) )
|
||||
int cprintf(const char * format, ...);
|
||||
#define PRINT_LOCATION() cprintf("%s: %d:\n", __PRETTY_FUNCTION__, __LINE__)
|
||||
#define PRTNT_HEAP() if (CFG_DEBUG == 3) cprintf("\n%s: %d: Heap free: %d\n", __PRETTY_FUNCTION__, __LINE__, util_heap_get_free_size())
|
||||
#define PRINT_INT(x) cprintf("%s: %d: " #x " = %ld\n" , __PRETTY_FUNCTION__, __LINE__, (uint32_t) (x) )
|
||||
#define PRINT_HEX(x) cprintf("%s: %d: " #x " = %08lx\n", __PRETTY_FUNCTION__, __LINE__, (uint32_t) (x) )
|
||||
#define PRINT_STR(x) cprintf("%s: %d: " #x " = %s\n" , __PRETTY_FUNCTION__, __LINE__, (char*)(x) )
|
||||
#define PRINT_BUFFER(buf, n) \
|
||||
do {\
|
||||
uint8_t const* p8 = (uint8_t const*) (buf);\
|
||||
Serial.print(#buf ": ");\
|
||||
for(uint32_t i=0; i<(n); i++) Serial.printf("%02x ", p8[i]);\
|
||||
for(uint32_t i=0; i<(n); i++) cprintf("%02x ", p8[i]);\
|
||||
Serial.println();\
|
||||
}while(0)
|
||||
|
||||
@@ -165,6 +166,11 @@ static inline uint32_t align16 (uint32_t value)
|
||||
return (value & 0xFFFFFFF0UL);
|
||||
}
|
||||
|
||||
static inline uint32_t align4 (uint32_t value)
|
||||
{
|
||||
return (value & 0xFFFFFFFCUL);
|
||||
}
|
||||
|
||||
static inline uint32_t align_n (uint32_t alignment, uint32_t value)
|
||||
{
|
||||
return value & (~(alignment-1));
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
/**************************************************************************/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <malloc.h>
|
||||
#include "debug.h"
|
||||
|
||||
@@ -51,3 +52,21 @@ int dbgHeapUsed(void)
|
||||
{
|
||||
return (mallinfo()).uordblks;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
int cprintf(const char * format, ...)
|
||||
{
|
||||
char buf[256];
|
||||
int len;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
len = vsnprintf(buf, 256, format, ap);
|
||||
Serial.write(buf, len);
|
||||
|
||||
va_end(ap);
|
||||
return len;
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,8 @@
|
||||
#ifndef _DEBUG_H
|
||||
#define _DEBUG_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
int dbgHeapTotal(void);
|
||||
int dbgHeapUsed(void);
|
||||
|
||||
|
||||
+10
-2
@@ -41,6 +41,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include "common_func.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
@@ -48,7 +51,12 @@
|
||||
#include "queue.h"
|
||||
#include "semphr.h"
|
||||
|
||||
#define rtos_malloc(x) pvPortMalloc(x)
|
||||
#define rtos_free(x) vPortFree(x)
|
||||
#if 0
|
||||
#define rtos_malloc(_size) ({ cprintf("[malloc] %s:%d : %d bytes\r\n", __PRETTY_FUNCTION__, __LINE__, _size); pvPortMalloc(_size); })
|
||||
#define rtos_free(ptr) ({ cprintf("[free] %s:%d\r\n" ,__PRETTY_FUNCTION__, __LINE__/*malloc_usable_size(ptr)*/); vPortFree(ptr); })
|
||||
#else
|
||||
#define rtos_malloc(_size) pvPortMalloc(_size)
|
||||
#define rtos_free(ptr) vPortFree(ptr)
|
||||
|
||||
#endif
|
||||
#endif /* RTOS_H_ */
|
||||
@@ -52,7 +52,7 @@
|
||||
Adafruit_FIFO::Adafruit_FIFO(uint16_t depth, uint8_t item_size, bool overwrite)
|
||||
: _depth(depth), _item_size(item_size), _overwritable(overwrite)
|
||||
{
|
||||
_buffer = (uint8_t*) rtos_malloc(item_size*depth); // use malloc_name for debug
|
||||
_buffer = (uint8_t*) malloc(item_size*depth);
|
||||
_count = _wr_idx = _rd_idx = 0;
|
||||
|
||||
_mutex = xSemaphoreCreateMutex();
|
||||
|
||||
+3
-3
@@ -67,7 +67,7 @@ extern "C"
|
||||
//--------------------------------------------------------------------+
|
||||
#if CFG_DEBUG >= 1
|
||||
// #define VERIFY_MESS(format, ...) printf("[%08ld] %s: %d: verify failed\n", get_millis(), __func__, __LINE__)
|
||||
#define VERIFY_MESS(_status) Serial.printf("%s: %d: verify failed, status = 0x%04X\n", __PRETTY_FUNCTION__, __LINE__, _status);
|
||||
#define VERIFY_MESS(_status) cprintf("%s: %d: verify failed, status = 0x%04X\n", __PRETTY_FUNCTION__, __LINE__, _status);
|
||||
#else
|
||||
#define VERIFY_MESS(_status)
|
||||
#endif
|
||||
@@ -91,13 +91,13 @@ extern "C"
|
||||
*------------------------------------------------------------------*/
|
||||
#define VERIFY_STS_1ARGS(sts) \
|
||||
do { \
|
||||
err_t _status = (err_t)(sts); \
|
||||
uint32_t _status = (uint32_t)(sts); \
|
||||
VERIFY_DEFINE(_status, _status);\
|
||||
} while(0)
|
||||
|
||||
#define VERIFY_STS_2ARGS(sts, _error) \
|
||||
do { \
|
||||
err_t _status = (err_t)(sts); \
|
||||
uint32_t _status = (uint32_t)(sts); \
|
||||
VERIFY_DEFINE(_status, _error);\
|
||||
} while(0)
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@
|
||||
// To change this value, an adjustment for SRAM is required in linker script
|
||||
#define BLE_GATTS_ATTR_TABLE_SIZE 0x1000
|
||||
|
||||
#define BLUEFRUIT_TASK_STACKSIZE (1024*5)
|
||||
#define BLUEFRUIT_TASK_STACKSIZE (1024*1)
|
||||
|
||||
extern "C"
|
||||
{
|
||||
@@ -477,6 +477,7 @@ void AdafruitBluefruit::_poll(void)
|
||||
{
|
||||
case NRF_EVT_FLASH_OPERATION_SUCCESS:
|
||||
case NRF_EVT_FLASH_OPERATION_ERROR:
|
||||
// PRINT_INT(soc_evt);
|
||||
hal_flash_event_cb(soc_evt);
|
||||
break;
|
||||
|
||||
|
||||
@@ -20,12 +20,45 @@
|
||||
#include <bluefruit.h>
|
||||
#include <nffs_lib.h>
|
||||
|
||||
#define FILENAME "/adafruit.txt"
|
||||
#define CONTENTS "Bluefruit Feather52's NFFS file contents"
|
||||
|
||||
// the setup function runs once when you press reset or power the board
|
||||
void setup() {
|
||||
// initialize digital pin LED_BUILTIN as an output.
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
Serial.begin(115200);
|
||||
Serial.println("nffs example");
|
||||
|
||||
Bluefruit.begin();
|
||||
nffs_pkg_init();
|
||||
|
||||
struct fs_file *file;
|
||||
|
||||
// file existed
|
||||
if ( 0 == fs_open(FILENAME, FS_ACCESS_READ, &file))
|
||||
{
|
||||
Serial.println(FILENAME " file existed");
|
||||
|
||||
uint32_t readlen;
|
||||
char buffer[64] = { 0 };
|
||||
fs_read(file, sizeof(buffer), buffer, &readlen);
|
||||
|
||||
buffer[readlen] = 0;
|
||||
Serial.println(buffer);
|
||||
}else
|
||||
{
|
||||
Serial.print("Open " FILENAME " file to write ... ");
|
||||
const char *data = "thach dep trai";
|
||||
|
||||
if( 0 == fs_open(FILENAME, FS_ACCESS_WRITE, &file) )
|
||||
{
|
||||
Serial.println("OK");
|
||||
fs_write(file, CONTENTS, strlen(CONTENTS));
|
||||
fs_close(file);
|
||||
}else
|
||||
{
|
||||
Serial.println("Failed (hint: paht must start with '/') ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the loop function runs over and over again forever
|
||||
|
||||
@@ -26,18 +26,21 @@
|
||||
#include <hal/hal_flash_int.h>
|
||||
#include "rtos.h"
|
||||
|
||||
#include "common_func.h"
|
||||
#include "verify.h"
|
||||
|
||||
#define NRF52K_FLASH_SECTOR_SZ 4096
|
||||
|
||||
int hal_flash_read (uint32_t address, void *dst, uint32_t num_bytes);
|
||||
int hal_flash_write (uint32_t address, const void *src, uint32_t num_bytes);
|
||||
int hal_flash_erase_sector (uint32_t sector_address);
|
||||
int hal_flash_read (uint8_t id, uint32_t address, void *dst, uint32_t num_bytes);
|
||||
int hal_flash_write (uint8_t id, uint32_t address, const void *src, uint32_t num_bytes);
|
||||
int hal_flash_erase_sector (uint8_t id, uint32_t sector_address);
|
||||
int hal_flash_sector_info (int idx, uint32_t *address, uint32_t *sz);
|
||||
int hal_flash_init (void);
|
||||
|
||||
static const struct hal_flash_funcs nrf52k_flash_funcs = {
|
||||
.hff_read = hal_flash_read,
|
||||
.hff_write = hal_flash_write,
|
||||
.hff_erase_sector = hal_flash_erase_sector,
|
||||
// .hff_read = hal_flash_read,
|
||||
// .hff_write = hal_flash_write,
|
||||
// .hff_erase_sector = hal_flash_erase_sector,
|
||||
.hff_sector_info = hal_flash_sector_info,
|
||||
.hff_init = hal_flash_init
|
||||
};
|
||||
@@ -77,22 +80,102 @@ const struct hal_flash *hal_bsp_flash_dev(uint8_t flash_id)
|
||||
}
|
||||
|
||||
int
|
||||
hal_flash_read(uint32_t address, void *dst, uint32_t num_bytes)
|
||||
hal_flash_read(uint8_t id, uint32_t address, void *dst, uint32_t num_bytes)
|
||||
{
|
||||
(void) id;
|
||||
memcpy(dst, (void *)address, num_bytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
hal_flash_write(uint32_t address, const void *src, uint32_t num_bytes)
|
||||
static int write_and_wait(uint32_t addr, uint32_t const * const src, uint32_t size)
|
||||
{
|
||||
if (NRF_SUCCESS != sd_flash_write( (uint32_t*) address, src, num_bytes) ) return -1;
|
||||
|
||||
VERIFY_STATUS( sd_flash_write( (uint32_t*) addr, src, size) );
|
||||
xSemaphoreTake(_evt_sem, portMAX_DELAY);
|
||||
return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : 1;
|
||||
}
|
||||
|
||||
int
|
||||
hal_flash_write(uint8_t id, uint32_t address, const void *src, uint32_t num_bytes)
|
||||
{
|
||||
(void) id;
|
||||
|
||||
/* SD Flash requires
|
||||
* 1. src data must be word aligned
|
||||
* 2. address must be word aligned
|
||||
* 2. each write is 4 bytes
|
||||
*
|
||||
* This cause issue with string literal. Solution is break up to 3 sequence write
|
||||
* 1. First is required if address not started at word aligned
|
||||
* 2. Malloc aligned data for src if needed
|
||||
* 3. Trailing bytes
|
||||
*/
|
||||
|
||||
/*------------- Fixing non-aligned Address -------------*/
|
||||
// Address is not aligned
|
||||
uint32_t miss = address & 0x03;
|
||||
|
||||
if ( miss )
|
||||
{
|
||||
/* Address is not aligned. Read 4 byte from aligned position
|
||||
* then update and write back
|
||||
*/
|
||||
|
||||
uint32_t cnt = 4 - miss;
|
||||
uint32_t val;
|
||||
|
||||
memcpy( &val, (uint8_t*) align4(address), 4);
|
||||
memcpy( ((uint8_t*)&val)+miss, src, cnt);
|
||||
|
||||
VERIFY_STATUS( write_and_wait( align4(address), &val, 1) );
|
||||
|
||||
address += cnt;
|
||||
src += cnt;
|
||||
num_bytes -= cnt;
|
||||
}
|
||||
|
||||
/*------------- Main write -------------*/
|
||||
uint32_t count = align4(num_bytes);
|
||||
uint32_t* tempbuf;
|
||||
|
||||
// src is not word aligned
|
||||
miss = ((uint32_t)src) & 0x03;
|
||||
if (miss)
|
||||
{
|
||||
// Malloc and copy multiple of 4 bytes from src to write to flash
|
||||
tempbuf = (uint32_t*) rtos_malloc( count );
|
||||
VERIFY( tempbuf, NRF_ERROR_NO_MEM);
|
||||
|
||||
memcpy(tempbuf, src, count);
|
||||
}else
|
||||
{
|
||||
tempbuf = (uint32_t*) src;
|
||||
}
|
||||
|
||||
VERIFY_STATUS( write_and_wait(address, tempbuf, count / 4) );
|
||||
|
||||
if (miss)
|
||||
{
|
||||
free(tempbuf);
|
||||
}
|
||||
|
||||
address += count;
|
||||
src += count;
|
||||
num_bytes -= count;
|
||||
|
||||
/*------------- Trailing Bytes -------------*/
|
||||
if ( num_bytes )
|
||||
{
|
||||
uint32_t val;
|
||||
|
||||
memcpy( &val, (uint8_t*) address, 4);
|
||||
memcpy(&val, src, num_bytes);
|
||||
|
||||
VERIFY_STATUS( write_and_wait(address, &val, 1) );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes)
|
||||
{
|
||||
@@ -127,7 +210,7 @@ hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes)
|
||||
* If some region of eraseable area falls inside sector,
|
||||
* erase the sector.
|
||||
*/
|
||||
if (hal_flash_erase_sector(start)) {
|
||||
if (hal_flash_erase_sector(id, start)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -136,8 +219,10 @@ hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes)
|
||||
}
|
||||
|
||||
int
|
||||
hal_flash_erase_sector(uint32_t sector_address)
|
||||
hal_flash_erase_sector(uint8_t id, uint32_t sector_address)
|
||||
{
|
||||
(void) id;
|
||||
|
||||
if (NRF_SUCCESS != sd_flash_page_erase( sector_address / NRF52K_FLASH_SECTOR_SZ ) ) return -1;
|
||||
|
||||
xSemaphoreTake(_evt_sem, portMAX_DELAY);
|
||||
@@ -155,5 +240,11 @@ hal_flash_sector_info(int idx, uint32_t *address, uint32_t *sz)
|
||||
|
||||
int hal_flash_init(void)
|
||||
{
|
||||
return 0;
|
||||
_evt_sem = xSemaphoreCreateCounting(10, 0);
|
||||
return (_evt_sem != NULL) ? 0 : 1;
|
||||
}
|
||||
|
||||
uint8_t hal_flash_align(uint8_t flash_id)
|
||||
{
|
||||
return nrf52k_flash_dev.hf_align;
|
||||
}
|
||||
@@ -30,9 +30,9 @@ extern "C" {
|
||||
* API that flash driver has to implement.
|
||||
*/
|
||||
struct hal_flash_funcs {
|
||||
int (*hff_read)(uint32_t address, void *dst, uint32_t num_bytes);
|
||||
int (*hff_write)(uint32_t address, const void *src, uint32_t num_bytes);
|
||||
int (*hff_erase_sector)(uint32_t sector_address);
|
||||
// int (*hff_read)(uint32_t address, void *dst, uint32_t num_bytes);
|
||||
// int (*hff_write)(uint32_t address, const void *src, uint32_t num_bytes);
|
||||
// int (*hff_erase_sector)(uint32_t sector_address);
|
||||
int (*hff_sector_info)(int idx, uint32_t *address, uint32_t *size);
|
||||
int (*hff_init)(void);
|
||||
};
|
||||
|
||||
@@ -488,6 +488,7 @@ nffs_misc_desc_from_flash_area(int id, int *cnt, struct nffs_area_desc *nad)
|
||||
}
|
||||
|
||||
hf = hal_bsp_flash_dev(fa->fa_device_id);
|
||||
hf->hf_itf->hff_init();
|
||||
for (i = 0; i < hf->hf_sector_cnt; i++) {
|
||||
hf->hf_itf->hff_sector_info(i, &start, &size);
|
||||
if (start >= fa->fa_off && start < fa->fa_off + fa->fa_size) {
|
||||
|
||||
@@ -30,6 +30,8 @@
|
||||
#include "crc/crc16.h"
|
||||
//#include "stats/stats.h"
|
||||
|
||||
#include "common_func.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "hal/hal_flash.h"
|
||||
//#include "os/os_mempool.h"
|
||||
#include "os/os_mempool.h"
|
||||
#include "os/os_malloc.h"
|
||||
#include "nffs/nffs.h"
|
||||
#include "nffs_priv.h"
|
||||
|
||||
@@ -37,6 +37,8 @@
|
||||
#ifndef _NFFS_LIB_H_
|
||||
#define _NFFS_LIB_H_
|
||||
|
||||
#include "nffs/nffs.h"
|
||||
#include "nffs/nffs.h"
|
||||
#include "fs/fs.h"
|
||||
#include "fs/fsutil.h"
|
||||
|
||||
#endif /* _NFFS_LIB_H_ */
|
||||
@@ -63,7 +63,7 @@ static inline os_error_t os_mutex_init(struct os_mutex *mu)
|
||||
{
|
||||
mu->sem = xSemaphoreCreateMutex();
|
||||
|
||||
return (mu->sem == NULL) ? 0 : 1;
|
||||
return (mu->sem != NULL) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user