diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index 90b9bfce..8d9c66c5 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -55,6 +55,7 @@ uint32_t setLoopStacksize(void); #include "delay.h" #include "binary.h" +#include "debug.h" #include "common_inc.h" #ifdef __cplusplus diff --git a/cores/nRF5/common_func.h b/cores/nRF5/common_func.h index 1b3209bd..965d9ae1 100644 --- a/cores/nRF5/common_func.h +++ b/cores/nRF5/common_func.h @@ -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)); diff --git a/cores/nRF5/debug.cpp b/cores/nRF5/debug.cpp index 36c33122..823468f6 100644 --- a/cores/nRF5/debug.cpp +++ b/cores/nRF5/debug.cpp @@ -35,6 +35,7 @@ /**************************************************************************/ #include +#include #include #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; + } +} diff --git a/cores/nRF5/debug.h b/cores/nRF5/debug.h index 8451fab0..8e38d792 100644 --- a/cores/nRF5/debug.h +++ b/cores/nRF5/debug.h @@ -37,6 +37,8 @@ #ifndef _DEBUG_H #define _DEBUG_H +#include + int dbgHeapTotal(void); int dbgHeapUsed(void); diff --git a/cores/nRF5/rtos.h b/cores/nRF5/rtos.h index 19c14a6b..dbdfec34 100644 --- a/cores/nRF5/rtos.h +++ b/cores/nRF5/rtos.h @@ -41,6 +41,9 @@ #include #include #include +#include + +#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_ */ diff --git a/cores/nRF5/utility/adafruit_fifo.cpp b/cores/nRF5/utility/adafruit_fifo.cpp index 73d54883..b398e5c4 100644 --- a/cores/nRF5/utility/adafruit_fifo.cpp +++ b/cores/nRF5/utility/adafruit_fifo.cpp @@ -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(); diff --git a/cores/nRF5/verify.h b/cores/nRF5/verify.h index 864551a3..3d435686 100644 --- a/cores/nRF5/verify.h +++ b/cores/nRF5/verify.h @@ -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) diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 076acf92..dc5083db 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -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; diff --git a/libraries/nffs/examples/filesystem/filesystem.ino b/libraries/nffs/examples/filesystem/filesystem.ino index 970be81e..087aafb3 100644 --- a/libraries/nffs/examples/filesystem/filesystem.ino +++ b/libraries/nffs/examples/filesystem/filesystem.ino @@ -20,12 +20,45 @@ #include #include +#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 diff --git a/libraries/nffs/src/hal/hal_flash.c b/libraries/nffs/src/hal/hal_flash.c index e291a2c2..79216a08 100644 --- a/libraries/nffs/src/hal/hal_flash.c +++ b/libraries/nffs/src/hal/hal_flash.c @@ -26,18 +26,21 @@ #include #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; } diff --git a/libraries/nffs/src/hal/hal_flash_int.h b/libraries/nffs/src/hal/hal_flash_int.h index 7643eebf..f0ec446a 100644 --- a/libraries/nffs/src/hal/hal_flash_int.h +++ b/libraries/nffs/src/hal/hal_flash_int.h @@ -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); }; diff --git a/libraries/nffs/src/nffs/src/nffs_misc.c b/libraries/nffs/src/nffs/src/nffs_misc.c index cade0ef2..24557dd8 100644 --- a/libraries/nffs/src/nffs/src/nffs_misc.c +++ b/libraries/nffs/src/nffs/src/nffs_misc.c @@ -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) { diff --git a/libraries/nffs/src/nffs/src/nffs_priv.h b/libraries/nffs/src/nffs/src/nffs_priv.h index e3f7579e..982ca3e0 100644 --- a/libraries/nffs/src/nffs/src/nffs_priv.h +++ b/libraries/nffs/src/nffs/src/nffs_priv.h @@ -30,6 +30,8 @@ #include "crc/crc16.h" //#include "stats/stats.h" +#include "common_func.h" + #ifdef __cplusplus extern "C" { #endif diff --git a/libraries/nffs/src/nffs/src/nffs_restore.c b/libraries/nffs/src/nffs/src/nffs_restore.c index 5d122009..f0e6396f 100644 --- a/libraries/nffs/src/nffs/src/nffs_restore.c +++ b/libraries/nffs/src/nffs/src/nffs_restore.c @@ -21,7 +21,7 @@ #include #include #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" diff --git a/libraries/nffs/src/nffs_lib.h b/libraries/nffs/src/nffs_lib.h index d20a3938..75389ede 100755 --- a/libraries/nffs/src/nffs_lib.h +++ b/libraries/nffs/src/nffs_lib.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_ */ diff --git a/libraries/nffs/src/os/os_mutex.h b/libraries/nffs/src/os/os_mutex.h index b55af210..75108013 100644 --- a/libraries/nffs/src/os/os_mutex.h +++ b/libraries/nffs/src/os/os_mutex.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; }