From bd188d7ad6d97cc24ae6fb81ef1207f5e86c42ef Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 14 Jan 2017 01:02:23 +0700 Subject: [PATCH] finally able to build --- cores/nRF5/new.cpp | 9 +- cores/nRF5/rtos.h | 3 + cores/nRF5/utility/adafruit_fifo.cpp | 4 +- libraries/Bluefruit52Lib/src/bluefruit.cpp | 253 +++++---- .../nffs/examples/filesystem/filesystem.ino | 2 + libraries/nffs/src/flash_map/flash_map.h | 98 ++++ libraries/nffs/src/fs/src/fs_cli.c | 2 +- libraries/nffs/src/fs/src/fs_mount.c | 2 +- libraries/nffs/src/fs/src/fs_priv.h | 2 +- libraries/nffs/src/hal/hal_flash.c | 159 ++++++ libraries/nffs/src/hal/hal_flash.h | 45 ++ libraries/nffs/src/hal/hal_flash_int.h | 58 ++ libraries/nffs/src/nffs/nffs.h | 2 + libraries/nffs/src/nffs/src/nffs.c | 76 +-- libraries/nffs/src/nffs/src/nffs_gc.c | 2 +- libraries/nffs/src/nffs/src/nffs_inode.c | 4 +- libraries/nffs/src/nffs/src/nffs_misc.c | 18 +- libraries/nffs/src/nffs/src/nffs_priv.h | 56 +- libraries/nffs/src/nffs/src/nffs_restore.c | 4 +- libraries/nffs/src/nffs/src/nffs_write.c | 2 +- libraries/nffs/src/os/os.h | 116 ++++ libraries/nffs/src/os/os_malloc.h | 43 ++ libraries/nffs/src/os/os_mempool.c | 250 +++++++++ libraries/nffs/src/os/os_mempool.h | 104 ++++ libraries/nffs/src/os/os_mutex.h | 86 +++ libraries/nffs/src/os/queue.h | 522 ++++++++++++++++++ libraries/nffs/src/syscfg/syscfg.h | 52 ++ 27 files changed, 1802 insertions(+), 172 deletions(-) create mode 100644 libraries/nffs/src/flash_map/flash_map.h create mode 100644 libraries/nffs/src/hal/hal_flash.c create mode 100644 libraries/nffs/src/hal/hal_flash.h create mode 100644 libraries/nffs/src/hal/hal_flash_int.h create mode 100644 libraries/nffs/src/os/os.h create mode 100644 libraries/nffs/src/os/os_malloc.h create mode 100644 libraries/nffs/src/os/os_mempool.c create mode 100644 libraries/nffs/src/os/os_mempool.h create mode 100644 libraries/nffs/src/os/os_mutex.h create mode 100644 libraries/nffs/src/os/queue.h create mode 100644 libraries/nffs/src/syscfg/syscfg.h diff --git a/cores/nRF5/new.cpp b/cores/nRF5/new.cpp index 7a6f416c..7c21c120 100644 --- a/cores/nRF5/new.cpp +++ b/cores/nRF5/new.cpp @@ -17,19 +17,20 @@ */ #include +#include "rtos.h" void *operator new(size_t size) { - return malloc(size); + return rtos_malloc(size); } void *operator new[](size_t size) { - return malloc(size); + return rtos_malloc(size); } void operator delete(void * ptr) { - free(ptr); + rtos_free(ptr); } void operator delete[](void * ptr) { - free(ptr); + rtos_free(ptr); } diff --git a/cores/nRF5/rtos.h b/cores/nRF5/rtos.h index ec986ab8..19c14a6b 100644 --- a/cores/nRF5/rtos.h +++ b/cores/nRF5/rtos.h @@ -48,4 +48,7 @@ #include "queue.h" #include "semphr.h" +#define rtos_malloc(x) pvPortMalloc(x) +#define rtos_free(x) vPortFree(x) + #endif /* RTOS_H_ */ diff --git a/cores/nRF5/utility/adafruit_fifo.cpp b/cores/nRF5/utility/adafruit_fifo.cpp index a746eed3..73d54883 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*) malloc(item_size*depth); // use malloc_name for debug + _buffer = (uint8_t*) rtos_malloc(item_size*depth); // use malloc_name for debug _count = _wr_idx = _rd_idx = 0; _mutex = xSemaphoreCreateMutex(); @@ -66,7 +66,7 @@ Adafruit_FIFO::Adafruit_FIFO(uint16_t depth, uint8_t item_size, bool overwrite) Adafruit_FIFO::~Adafruit_FIFO() { vSemaphoreDelete(_mutex); - free(_buffer); // use free_name for debug + rtos_free(_buffer); // use free_name for debug } bool Adafruit_FIFO::_mutex_lock(bool isr) diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 927c0e73..076acf92 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -66,7 +66,12 @@ #define BLUEFRUIT_TASK_STACKSIZE (1024*5) -extern "C" void SD_EVT_IRQHandler(void); +extern "C" +{ + void SD_EVT_IRQHandler(void); + void hal_flash_event_cb(uint32_t event); +} + void adafruit_bluefruit_task(void* arg); AdafruitBluefruit Bluefruit; @@ -449,115 +454,161 @@ void AdafruitBluefruit::_poll(void) if ( xSemaphoreTake(_ble_event_sem, portMAX_DELAY) ) { - uint32_t ev_buf[BLE_STACK_EVT_MSG_BUF_SIZE/4 + 4]; - uint16_t ev_len = sizeof(ev_buf); - ble_evt_t* evt = (ble_evt_t*) ev_buf; + bool out_of_soc = false; + bool out_of_ble = false; - if( NRF_SUCCESS == sd_ble_evt_get((uint8_t*)ev_buf, &ev_len)) + while( !(out_of_soc && out_of_ble) ) { - switch ( evt->header.evt_id ) + uint32_t err; + + /*------------- SOC Event -------------*/ + uint32_t soc_evt; + + err = sd_evt_get(&soc_evt); + + if ( NRF_ERROR_NOT_FOUND == err ) { - case BLE_GAP_EVT_CONNECTED: - if (_led_conn) digitalWrite(LED_CONN, HIGH); - - _conn_hdl = evt->evt.gap_evt.conn_handle; - _peer_addr = evt->evt.gap_evt.params.connected.peer_addr; - - uint8_t txbuf_max; - (void) sd_ble_tx_packet_count_get(_conn_hdl, &txbuf_max); - _txbuf_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max); - break; - - case BLE_GAP_EVT_DISCONNECTED: - if (_led_conn) digitalWrite(LED_CONN, LOW); - - _conn_hdl = BLE_GATT_HANDLE_INVALID; - - vSemaphoreDelete(_txbuf_sem); - - startAdvertising(); - break; - - case BLE_GAP_EVT_TIMEOUT: - if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING) - { - // Restart Advertising - startAdvertising(); - } - break; - - case BLE_EVT_TX_COMPLETE: - for(uint8_t i=0; ievt.common_evt.params.tx_complete.count; i++) - { - xSemaphoreGive(_txbuf_sem); - } - break; - - #if 0 - case BLE_GAP_EVT_SEC_INFO_REQUEST: - if (_enc_key.master_id.ediv == evt->evt.gap_evt.params.sec_info_request.master_id.ediv) - { - sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_enc_key.enc_info, NULL, NULL); - } else - { - sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL); - } - break; - - case BLE_GAP_EVT_SEC_PARAMS_REQUEST: + out_of_soc = true; + } + else if (NRF_SUCCESS == err) + { + // handling SOC + switch (soc_evt) { - ble_gap_sec_params_t sec_para = - { - .bond = SEC_PARAM_BOND , - .mitm = 0, //CFG_PIN_ENABLED ? nvm_data.core.passkey_enable : 0, - .lesc = 0, - .keypress = 0, - .io_caps = BLE_GAP_IO_CAPS_NONE, // (CFG_PIN_ENABLED && nvm_data.core.passkey_enable) ? BLE_GAP_IO_CAPS_DISPLAY_ONLY : BLE_GAP_IO_CAPS_NONE , - .oob = SEC_PARAM_OOB , - .min_key_size = SEC_PARAM_MIN_KEY_SIZE , - .max_key_size = SEC_PARAM_MAX_KEY_SIZE - }; + case NRF_EVT_FLASH_OPERATION_SUCCESS: + case NRF_EVT_FLASH_OPERATION_ERROR: + hal_flash_event_cb(soc_evt); + break; - ble_gap_sec_keyset_t keyset = - { - .keys_own = { - .p_enc_key = &_enc_key, - .p_id_key = NULL, - .p_sign_key = NULL, - .p_pk = NULL - }, - - .keys_peer = { NULL, NULL, NULL, NULL } - }; - - VERIFY_STATUS(sd_ble_gap_sec_params_reply(evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &sec_para, &keyset), - RETURN_VOID); + default: break; } - break; - - case BLE_GAP_EVT_AUTH_STATUS: - // Bonding succeeded --> save encryption keys - if (BLE_GAP_SEC_STATUS_SUCCESS == evt->evt.gap_evt.params.auth_status.auth_status) - { - - } - break; - - case BLE_GATTS_EVT_SYS_ATTR_MISSING: - sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0); - break; - #endif - - case BLE_GAP_EVT_CONN_SEC_UPDATE: - break; - - default: break; + } + else + { + // Error, do nothing nowx } - // GATTs characteristics event handler - for(int i=0; i<_chars_count; i++) + /*------------- BLE Event -------------*/ + uint32_t ev_buf[BLE_STACK_EVT_MSG_BUF_SIZE/4 + 4]; + uint16_t ev_len = sizeof(ev_buf); + ble_evt_t* evt = (ble_evt_t*) ev_buf; + + err = sd_ble_evt_get((uint8_t*)ev_buf, &ev_len); + + if ( NRF_ERROR_NOT_FOUND == err ) { - _chars_list[i]->eventHandler(evt); + out_of_ble = true; + } + else if( NRF_SUCCESS == err) + { + switch ( evt->header.evt_id ) + { + case BLE_GAP_EVT_CONNECTED: + if (_led_conn) digitalWrite(LED_CONN, HIGH); + + _conn_hdl = evt->evt.gap_evt.conn_handle; + _peer_addr = evt->evt.gap_evt.params.connected.peer_addr; + + uint8_t txbuf_max; + (void) sd_ble_tx_packet_count_get(_conn_hdl, &txbuf_max); + _txbuf_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max); + break; + + case BLE_GAP_EVT_DISCONNECTED: + if (_led_conn) digitalWrite(LED_CONN, LOW); + + _conn_hdl = BLE_GATT_HANDLE_INVALID; + + vSemaphoreDelete(_txbuf_sem); + + startAdvertising(); + break; + + case BLE_GAP_EVT_TIMEOUT: + if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING) + { + // Restart Advertising + startAdvertising(); + } + break; + + case BLE_EVT_TX_COMPLETE: + for(uint8_t i=0; ievt.common_evt.params.tx_complete.count; i++) + { + xSemaphoreGive(_txbuf_sem); + } + break; + + #if 0 + case BLE_GAP_EVT_SEC_INFO_REQUEST: + if (_enc_key.master_id.ediv == evt->evt.gap_evt.params.sec_info_request.master_id.ediv) + { + sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_enc_key.enc_info, NULL, NULL); + } else + { + sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL); + } + break; + + case BLE_GAP_EVT_SEC_PARAMS_REQUEST: + { + ble_gap_sec_params_t sec_para = + { + .bond = SEC_PARAM_BOND , + .mitm = 0, //CFG_PIN_ENABLED ? nvm_data.core.passkey_enable : 0, + .lesc = 0, + .keypress = 0, + .io_caps = BLE_GAP_IO_CAPS_NONE, // (CFG_PIN_ENABLED && nvm_data.core.passkey_enable) ? BLE_GAP_IO_CAPS_DISPLAY_ONLY : BLE_GAP_IO_CAPS_NONE , + .oob = SEC_PARAM_OOB , + .min_key_size = SEC_PARAM_MIN_KEY_SIZE , + .max_key_size = SEC_PARAM_MAX_KEY_SIZE + }; + + ble_gap_sec_keyset_t keyset = + { + .keys_own = { + .p_enc_key = &_enc_key, + .p_id_key = NULL, + .p_sign_key = NULL, + .p_pk = NULL + }, + + .keys_peer = { NULL, NULL, NULL, NULL } + }; + + VERIFY_STATUS(sd_ble_gap_sec_params_reply(evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &sec_para, &keyset), + RETURN_VOID); + } + break; + + case BLE_GAP_EVT_AUTH_STATUS: + // Bonding succeeded --> save encryption keys + if (BLE_GAP_SEC_STATUS_SUCCESS == evt->evt.gap_evt.params.auth_status.auth_status) + { + + } + break; + + case BLE_GATTS_EVT_SYS_ATTR_MISSING: + sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0); + break; + #endif + + case BLE_GAP_EVT_CONN_SEC_UPDATE: + break; + + default: break; + } + + // GATTs characteristics event handler + for(int i=0; i<_chars_count; i++) + { + _chars_list[i]->eventHandler(evt); + } + } + else + { + // Error, do nothing now } } } diff --git a/libraries/nffs/examples/filesystem/filesystem.ino b/libraries/nffs/examples/filesystem/filesystem.ino index bebf0429..970be81e 100644 --- a/libraries/nffs/examples/filesystem/filesystem.ino +++ b/libraries/nffs/examples/filesystem/filesystem.ino @@ -24,6 +24,8 @@ void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); + + nffs_pkg_init(); } // the loop function runs over and over again forever diff --git a/libraries/nffs/src/flash_map/flash_map.h b/libraries/nffs/src/flash_map/flash_map.h new file mode 100644 index 00000000..cdcee32e --- /dev/null +++ b/libraries/nffs/src/flash_map/flash_map.h @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_UTIL_FLASH_MAP_ +#define H_UTIL_FLASH_MAP_ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * + * Provides abstraction of flash regions for type of use. + * I.e. dude where's my image? + * + * System will contain a map which contains flash areas. Every + * region will contain flash identifier, offset within flash and length. + * + * 1. This system map could be in a file within filesystem (Initializer + * must know/figure out where the filesystem is at). + * 2. Map could be at fixed location for project (compiled to code) + * 3. Map could be at specific place in flash (put in place at mfg time). + * + * Note that the map you use must be valid for BSP it's for, + * match the linker scripts when platform executes from flash, + * and match the target offset specified in download script. + */ +#include + +struct flash_area { + uint8_t fa_id; + uint8_t fa_device_id; + uint16_t pad16; + uint32_t fa_off; + uint32_t fa_size; +}; + +#if 0 +extern const struct flash_area *flash_map; +extern int flash_map_entries; + +/* + * Initializes flash map. Memory will be referenced by flash_map code + * from this on. + */ +void flash_map_init(void); + +/* + * Start using flash area. + */ +static int flash_area_open(uint8_t id, const struct flash_area **); + +void flash_area_close(const struct flash_area *); + +/* + * Read/write/erase. Offset is relative from beginning of flash area. + */ +int flash_area_read(const struct flash_area *, uint32_t off, void *dst, + uint32_t len); +int flash_area_write(const struct flash_area *, uint32_t off, const void *src, + uint32_t len); +int flash_area_erase(const struct flash_area *, uint32_t off, uint32_t len); + +/* + * Alignment restriction for flash writes. + */ +uint8_t flash_area_align(const struct flash_area *); + +/* + * Given flash map index, return info about sectors within the area. + */ +int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret); + +int flash_area_id_from_image_slot(int slot); +int flash_area_id_to_image_slot(int area_id); +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* H_UTIL_FLASH_MAP_ */ diff --git a/libraries/nffs/src/fs/src/fs_cli.c b/libraries/nffs/src/fs/src/fs_cli.c index 9dc9351f..10ff22de 100644 --- a/libraries/nffs/src/fs/src/fs_cli.c +++ b/libraries/nffs/src/fs/src/fs_cli.c @@ -17,7 +17,7 @@ * under the License. */ -//#include "syscfg/syscfg.h" +#include "syscfg/syscfg.h" #if MYNEWT_VAL(FS_CLI) diff --git a/libraries/nffs/src/fs/src/fs_mount.c b/libraries/nffs/src/fs/src/fs_mount.c index a05afcfe..9645d1ad 100644 --- a/libraries/nffs/src/fs/src/fs_mount.c +++ b/libraries/nffs/src/fs/src/fs_mount.c @@ -17,7 +17,7 @@ * under the License. */ -//#include "syscfg/syscfg.h" +#include "syscfg/syscfg.h" #include "fs/fs.h" #include "fs/fs_if.h" #include "fs_priv.h" diff --git a/libraries/nffs/src/fs/src/fs_priv.h b/libraries/nffs/src/fs/src/fs_priv.h index 030ac089..57337337 100644 --- a/libraries/nffs/src/fs/src/fs_priv.h +++ b/libraries/nffs/src/fs/src/fs_priv.h @@ -19,7 +19,7 @@ #ifndef __FS_PRIV_H__ #define __FS_PRIV_H__ -//#include "syscfg/syscfg.h" +#include "syscfg/syscfg.h" #ifdef __cplusplus extern "C" { diff --git a/libraries/nffs/src/hal/hal_flash.c b/libraries/nffs/src/hal/hal_flash.c new file mode 100644 index 00000000..e291a2c2 --- /dev/null +++ b/libraries/nffs/src/hal/hal_flash.c @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include + +#include "nrf.h" +//#include "mcu/nrf52_hal.h" + +#include +#include "rtos.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_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_sector_info = hal_flash_sector_info, + .hff_init = hal_flash_init +}; + + +const struct hal_flash nrf52k_flash_dev = { + .hf_itf = &nrf52k_flash_funcs, + .hf_base_addr = 0x00000000, + .hf_size = 512 * 1024, /* XXX read from factory info? */ + .hf_sector_cnt = 128, /* XXX read from factory info? */ + .hf_align = 1 +}; + +#define NRF52K_FLASH_READY() (NRF_NVMC->READY == NVMC_READY_READY_Ready) + +static SemaphoreHandle_t _evt_sem = NULL; +static uint32_t _op_result; + +void hal_flash_event_cb(uint32_t event) +{ + _op_result = event; + xSemaphoreGive(_evt_sem); +} + +static int +hal_flash_check_addr(const struct hal_flash *hf, uint32_t addr) +{ + if (addr < hf->hf_base_addr || addr > hf->hf_base_addr + hf->hf_size) { + return -1; + } + return 0; +} + +const struct hal_flash *hal_bsp_flash_dev(uint8_t flash_id) +{ + return &nrf52k_flash_dev; +} + +int +hal_flash_read(uint32_t address, void *dst, uint32_t num_bytes) +{ + memcpy(dst, (void *)address, num_bytes); + return 0; +} + + +int +hal_flash_write(uint32_t address, const void *src, uint32_t num_bytes) +{ + if (NRF_SUCCESS != sd_flash_write( (uint32_t*) address, src, num_bytes) ) return -1; + + xSemaphoreTake(_evt_sem, portMAX_DELAY); + return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : 1; +} + +int +hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes) +{ + const struct hal_flash *hf; + uint32_t start, size; + uint32_t end; + uint32_t end_area; + int i; + int rc; + + hf = &nrf52k_flash_dev; + + if (hal_flash_check_addr(hf, address) || + hal_flash_check_addr(hf, address + num_bytes)) { + return -1; + } + + end = address + num_bytes; + if (end <= address) { + /* + * Check for wrap-around. + */ + return -1; + } + + for (i = 0; i < hf->hf_sector_cnt; i++) { + rc = hal_flash_sector_info(i, &start, &size); + assert(rc == 0); + end_area = start + size; + if (address < end_area && end > start) { + /* + * If some region of eraseable area falls inside sector, + * erase the sector. + */ + if (hal_flash_erase_sector(start)) { + return -1; + } + } + } + return 0; +} + +int +hal_flash_erase_sector(uint32_t sector_address) +{ + if (NRF_SUCCESS != sd_flash_page_erase( sector_address / NRF52K_FLASH_SECTOR_SZ ) ) return -1; + + xSemaphoreTake(_evt_sem, portMAX_DELAY); + return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : 1; +} + +int +hal_flash_sector_info(int idx, uint32_t *address, uint32_t *sz) +{ + assert(idx < nrf52k_flash_dev.hf_sector_cnt); + *address = idx * NRF52K_FLASH_SECTOR_SZ; + *sz = NRF52K_FLASH_SECTOR_SZ; + return 0; +} + +int hal_flash_init(void) +{ + return 0; +} diff --git a/libraries/nffs/src/hal/hal_flash.h b/libraries/nffs/src/hal/hal_flash.h new file mode 100644 index 00000000..79ba79d0 --- /dev/null +++ b/libraries/nffs/src/hal/hal_flash.h @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_HAL_FLASH_ +#define H_HAL_FLASH_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +int hal_flash_read(uint8_t flash_id, uint32_t address, void *dst, + uint32_t num_bytes); +int hal_flash_write(uint8_t flash_id, uint32_t address, const void *src, + uint32_t num_bytes); +int hal_flash_erase_sector(uint8_t flash_id, uint32_t sector_address); +int hal_flash_erase(uint8_t flash_id, uint32_t address, uint32_t num_bytes); +uint8_t hal_flash_align(uint8_t flash_id); +int hal_flash_init(void); + +struct hal_flash; +const struct hal_flash *hal_bsp_flash_dev(uint8_t flash_id); + +#ifdef __cplusplus +} +#endif + +#endif /* H_HAL_FLASH_ */ diff --git a/libraries/nffs/src/hal/hal_flash_int.h b/libraries/nffs/src/hal/hal_flash_int.h new file mode 100644 index 00000000..7643eebf --- /dev/null +++ b/libraries/nffs/src/hal/hal_flash_int.h @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_HAL_FLASH_INT_ +#define H_HAL_FLASH_INT_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/* + * 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_sector_info)(int idx, uint32_t *address, uint32_t *size); + int (*hff_init)(void); +}; + +struct hal_flash { + const struct hal_flash_funcs *hf_itf; + uint32_t hf_base_addr; + uint32_t hf_size; + int hf_sector_cnt; + int hf_align; /* Alignment requirement. 1 if unrestricted. */ +}; + +/* + * Return size of the flash sector. sec_idx is index to hf_sectors array. + */ +uint32_t hal_flash_sector_size(const struct hal_flash *hf, int sec_idx); + + +#ifdef __cplusplus +} +#endif + +#endif /* H_HAL_FLASH_INT_ */ diff --git a/libraries/nffs/src/nffs/nffs.h b/libraries/nffs/src/nffs/nffs.h index 35baeb5a..f49114ca 100644 --- a/libraries/nffs/src/nffs/nffs.h +++ b/libraries/nffs/src/nffs/nffs.h @@ -65,6 +65,8 @@ int nffs_format(const struct nffs_area_desc *area_descs); int nffs_misc_desc_from_flash_area(int idx, int *cnt, struct nffs_area_desc *nad); +void nffs_pkg_init(void); + #ifdef __cplusplus } #endif diff --git a/libraries/nffs/src/nffs/src/nffs.c b/libraries/nffs/src/nffs/src/nffs.c index a42a69f6..4006b968 100644 --- a/libraries/nffs/src/nffs/src/nffs.c +++ b/libraries/nffs/src/nffs/src/nffs.c @@ -22,14 +22,26 @@ #include #include +#define NFFS_AREA_MAX 8 + +#define SYSINIT_ASSERT_ACTIVE() +#define SYSINIT_PANIC_ASSERT(rc) do \ +{ \ + if (!(rc)) { \ + SYSINIT_PANIC(); \ + } \ +} while (0) +#define SYSINIT_PANIC() return // assert(0) + +#include "syscfg/syscfg.h" //#include "sysinit/sysinit.h" //#include "sysflash/sysflash.h" //#include "bsp/bsp.h" -//#include "hal/hal_flash.h" +#include "hal/hal_flash.h" //#include "flash_map/flash_map.h" -//#include "os/os_mempool.h" -//#include "os/os_mutex.h" -//#include "os/os_malloc.h" +#include "os/os_mempool.h" +#include "os/os_mutex.h" +#include "os/os_malloc.h" //#include "stats/stats.h" #include "nffs_priv.h" #include "nffs/nffs.h" @@ -60,7 +72,7 @@ struct nffs_inode_entry *nffs_lost_found_dir; static struct os_mutex nffs_mutex; -struct log nffs_log; +//struct log nffs_log; static int nffs_open(const char *path, uint8_t access_flags, struct fs_file **out_file); @@ -105,28 +117,28 @@ static const struct fs_ops nffs_ops = { .f_name = "nffs" }; -STATS_SECT_DECL(nffs_stats) nffs_stats; -STATS_NAME_START(nffs_stats) - STATS_NAME(nffs_stats, nffs_hashcnt_ins) - STATS_NAME(nffs_stats, nffs_hashcnt_rm) - STATS_NAME(nffs_stats, nffs_object_count) - STATS_NAME(nffs_stats, nffs_iocnt_read) - STATS_NAME(nffs_stats, nffs_iocnt_write) - STATS_NAME(nffs_stats, nffs_gccnt) - STATS_NAME(nffs_stats, nffs_readcnt_data) - STATS_NAME(nffs_stats, nffs_readcnt_block) - STATS_NAME(nffs_stats, nffs_readcnt_crc) - STATS_NAME(nffs_stats, nffs_readcnt_copy) - STATS_NAME(nffs_stats, nffs_readcnt_format) - STATS_NAME(nffs_stats, nffs_readcnt_gccollate) - STATS_NAME(nffs_stats, nffs_readcnt_inode) - STATS_NAME(nffs_stats, nffs_readcnt_inodeent) - STATS_NAME(nffs_stats, nffs_readcnt_rename) - STATS_NAME(nffs_stats, nffs_readcnt_update) - STATS_NAME(nffs_stats, nffs_readcnt_filename) - STATS_NAME(nffs_stats, nffs_readcnt_object) - STATS_NAME(nffs_stats, nffs_readcnt_detect) -STATS_NAME_END(nffs_stats) +//STATS_SECT_DECL(nffs_stats) nffs_stats; +//STATS_NAME_START(nffs_stats) +// STATS_NAME(nffs_stats, nffs_hashcnt_ins) +// STATS_NAME(nffs_stats, nffs_hashcnt_rm) +// STATS_NAME(nffs_stats, nffs_object_count) +// STATS_NAME(nffs_stats, nffs_iocnt_read) +// STATS_NAME(nffs_stats, nffs_iocnt_write) +// STATS_NAME(nffs_stats, nffs_gccnt) +// STATS_NAME(nffs_stats, nffs_readcnt_data) +// STATS_NAME(nffs_stats, nffs_readcnt_block)36 +// STATS_NAME(nffs_stats, nffs_readcnt_crc) +// STATS_NAME(nffs_stats, nffs_readcnt_copy) +// STATS_NAME(nffs_stats, nffs_readcnt_format) +// STATS_NAME(nffs_stats, nffs_readcnt_gccollate) +// STATS_NAME(nffs_stats, nffs_readcnt_inode) +// STATS_NAME(nffs_stats, nffs_readcnt_inodeent) +// STATS_NAME(nffs_stats, nffs_readcnt_rename) +// STATS_NAME(nffs_stats, nffs_readcnt_update) +// STATS_NAME(nffs_stats, nffs_readcnt_filename) +// STATS_NAME(nffs_stats, nffs_readcnt_object) +// STATS_NAME(nffs_stats, nffs_readcnt_detect) +//STATS_NAME_END(nffs_stats) static void nffs_lock(void) @@ -150,11 +162,11 @@ static int nffs_stats_init(void) { int rc = 0; - rc = stats_init_and_reg( - STATS_HDR(nffs_stats), - STATS_SIZE_INIT_PARMS(nffs_stats, STATS_SIZE_32), - STATS_NAME_INIT_PARMS(nffs_stats), - "nffs_stats"); +// rc = stats_init_and_reg( +// STATS_HDR(nffs_stats), +// STATS_SIZE_INIT_PARMS(nffs_stats, STATS_SIZE_32), +// STATS_NAME_INIT_PARMS(nffs_stats), +// "nffs_stats"); if (rc) { if (rc < 0) { /* multiple initializations are okay */ diff --git a/libraries/nffs/src/nffs/src/nffs_gc.c b/libraries/nffs/src/nffs/src/nffs_gc.c index 829a30fc..d717ae6e 100644 --- a/libraries/nffs/src/nffs/src/nffs_gc.c +++ b/libraries/nffs/src/nffs/src/nffs_gc.c @@ -20,7 +20,7 @@ #include #include #include "os/os_malloc.h" -#include "testutil/testutil.h" +//#include "testutil/testutil.h" #include "nffs_priv.h" #include "nffs/nffs.h" diff --git a/libraries/nffs/src/nffs/src/nffs_inode.c b/libraries/nffs/src/nffs/src/nffs_inode.c index be614d82..afb03554 100644 --- a/libraries/nffs/src/nffs/src/nffs_inode.c +++ b/libraries/nffs/src/nffs/src/nffs_inode.c @@ -20,8 +20,8 @@ #include #include #include -#include "testutil/testutil.h" -#include "os/os_mempool.h" +//#include "testutil/testutil.h" +//#include "os/os_mempool.h" #include "nffs/nffs.h" #include "nffs_priv.h" diff --git a/libraries/nffs/src/nffs/src/nffs_misc.c b/libraries/nffs/src/nffs/src/nffs_misc.c index 168749e4..cade0ef2 100644 --- a/libraries/nffs/src/nffs/src/nffs_misc.c +++ b/libraries/nffs/src/nffs/src/nffs_misc.c @@ -19,12 +19,28 @@ #include #include "flash_map/flash_map.h" -#include "hal/hal_bsp.h" +#include "hal/hal_flash.h" #include "hal/hal_flash_int.h" #include "os/os_malloc.h" #include "nffs/nffs.h" #include "nffs_priv.h" +static const struct flash_area sysflash_map_dfl = +{ + .fa_id = 0, + .fa_device_id = 0, + .fa_off = 0x6D000, + .fa_size = 7*4096, /* 28 kB */ +}; + +static int +flash_area_open(uint8_t id, const struct flash_area **fap) +{ + (void) id; + *fap = &sysflash_map_dfl; + return 0; +} + /** * Determines if the file system contains a valid root directory. For the root * directory to be valid, it must be present and have the following traits: diff --git a/libraries/nffs/src/nffs/src/nffs_priv.h b/libraries/nffs/src/nffs/src/nffs_priv.h index 0b579795..e3f7579e 100644 --- a/libraries/nffs/src/nffs/src/nffs_priv.h +++ b/libraries/nffs/src/nffs/src/nffs_priv.h @@ -22,8 +22,9 @@ #include //#include "log/log.h" -//#include "os/queue.h" -//#include "os/os_mempool.h" + +#include "os/queue.h" +#include "os/os_mempool.h" #include "nffs/nffs.h" #include "fs/fs.h" #include "crc/crc16.h" @@ -246,28 +247,31 @@ struct nffs_dir { struct nffs_dirent nd_dirent; }; -STATS_SECT_START(nffs_stats) - STATS_SECT_ENTRY(nffs_hashcnt_ins) - STATS_SECT_ENTRY(nffs_hashcnt_rm) - STATS_SECT_ENTRY(nffs_object_count) - STATS_SECT_ENTRY(nffs_iocnt_read) - STATS_SECT_ENTRY(nffs_iocnt_write) - STATS_SECT_ENTRY(nffs_gccnt) - STATS_SECT_ENTRY(nffs_readcnt_data) - STATS_SECT_ENTRY(nffs_readcnt_block) - STATS_SECT_ENTRY(nffs_readcnt_crc) - STATS_SECT_ENTRY(nffs_readcnt_copy) - STATS_SECT_ENTRY(nffs_readcnt_format) - STATS_SECT_ENTRY(nffs_readcnt_gccollate) - STATS_SECT_ENTRY(nffs_readcnt_inode) - STATS_SECT_ENTRY(nffs_readcnt_inodeent) - STATS_SECT_ENTRY(nffs_readcnt_rename) - STATS_SECT_ENTRY(nffs_readcnt_update) - STATS_SECT_ENTRY(nffs_readcnt_filename) - STATS_SECT_ENTRY(nffs_readcnt_object) - STATS_SECT_ENTRY(nffs_readcnt_detect) -STATS_SECT_END -extern STATS_SECT_DECL(nffs_stats) nffs_stats; +//STATS_SECT_START(nffs_stats) +// STATS_SECT_ENTRY(nffs_hashcnt_ins) +// STATS_SECT_ENTRY(nffs_hashcnt_rm) +// STATS_SECT_ENTRY(nffs_object_count) +// STATS_SECT_ENTRY(nffs_iocnt_read) +// STATS_SECT_ENTRY(nffs_iocnt_write) +// STATS_SECT_ENTRY(nffs_gccnt) +// STATS_SECT_ENTRY(nffs_readcnt_data) +// STATS_SECT_ENTRY(nffs_readcnt_block) +// STATS_SECT_ENTRY(nffs_readcnt_crc) +// STATS_SECT_ENTRY(nffs_readcnt_copy) +// STATS_SECT_ENTRY(nffs_readcnt_format) +// STATS_SECT_ENTRY(nffs_readcnt_gccollate) +// STATS_SECT_ENTRY(nffs_readcnt_inode) +// STATS_SECT_ENTRY(nffs_readcnt_inodeent) +// STATS_SECT_ENTRY(nffs_readcnt_rename) +// STATS_SECT_ENTRY(nffs_readcnt_update) +// STATS_SECT_ENTRY(nffs_readcnt_filename) +// STATS_SECT_ENTRY(nffs_readcnt_object) +// STATS_SECT_ENTRY(nffs_readcnt_detect) +//STATS_SECT_END +//extern STATS_SECT_DECL(nffs_stats) nffs_stats; + +#define STATS_INC(x, y) +#define ASSERT_IF_TEST(x) extern void *nffs_file_mem; extern void *nffs_block_entry_mem; @@ -522,8 +526,12 @@ int nffs_write_to_file(struct nffs_file *file, const void *data, int len); #endif #endif /* 0 */ +#if 0 #define NFFS_LOG(lvl, ...) \ LOG_ ## lvl(&nffs_log, LOG_MODULE_NFFS, __VA_ARGS__) +#endif + +#define NFFS_LOG(lvl, ...) #ifdef __cplusplus } diff --git a/libraries/nffs/src/nffs/src/nffs_restore.c b/libraries/nffs/src/nffs/src/nffs_restore.c index a40460cb..5d122009 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" @@ -1202,6 +1202,7 @@ nffs_restore_corrupt_scratch(void) static void nffs_log_contents(void) { +#if 0 #if MYNEWT_VAL(LOG_LEVEL) > LOG_LEVEL_DEBUG return; #endif @@ -1270,6 +1271,7 @@ nffs_log_contents(void) } } } +#endif } /** diff --git a/libraries/nffs/src/nffs/src/nffs_write.c b/libraries/nffs/src/nffs/src/nffs_write.c index ea48e06b..95dce124 100644 --- a/libraries/nffs/src/nffs/src/nffs_write.c +++ b/libraries/nffs/src/nffs/src/nffs_write.c @@ -18,7 +18,7 @@ */ #include -#include "testutil/testutil.h" +//#include "testutil/testutil.h" #include "nffs/nffs.h" #include "nffs_priv.h" diff --git a/libraries/nffs/src/os/os.h b/libraries/nffs/src/os/os.h new file mode 100644 index 00000000..e6a63586 --- /dev/null +++ b/libraries/nffs/src/os/os.h @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _OS_H +#define _OS_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef min +#define min(a, b) ((a)<(b)?(a):(b)) +#endif + +#ifndef max +#define max(a, b) ((a)>(b)?(a):(b)) +#endif + +#define os_get_return_addr() (__builtin_return_address(0)) + +#define OS_ALIGN(__n, __a) ( \ + (((__n) & ((__a) - 1)) == 0) ? \ + (__n) : \ + ((__n) + ((__a) - ((__n) & ((__a) - 1)))) \ + ) + + +#define CTASSERT(x) typedef int __ctasssert ## __LINE__[(x) ? 1 : -1] + +#if 0 +/** + * Whether or not the operating system has been started. Set to + * 1 right before first task is run. + */ +extern int g_os_started; + +int os_info_init(void); + +/** + * Returns 1 if the OS has been started, 0 if it has not yet been + * been started. + */ +int os_started(void); +#endif + +/* OS error enumerations */ +enum os_error { + OS_OK = 0, + OS_ENOMEM = 1, + OS_EINVAL = 2, + OS_INVALID_PARM = 3, + OS_MEM_NOT_ALIGNED = 4, + OS_BAD_MUTEX = 5, + OS_TIMEOUT = 6, + OS_ERR_IN_ISR = 7, /* Function cannot be called from ISR */ + OS_ERR_PRIV = 8, /* Privileged access error */ + OS_NOT_STARTED = 9, /* Operating must be started to call this function, but isn't */ + OS_ENOENT = 10, /* No such thing */ + OS_EBUSY = 11, /* Resource busy */ + OS_ERROR = 12, /* Generic Error */ +}; + +#define OS_WAIT_FOREVER (-1) + +typedef enum os_error os_error_t; + +#if 0 +#define OS_IDLE_PRIO (0xff) + +void os_init(void); +void os_start(void); + +/* XXX: Not sure if this should go here; I want to differentiate API that + * should be called by application developers as those that should not. */ +void os_init_idle_task(void); +#endif + +//#include "os/endian.h" +#include "os/os_arch.h" +//#include "os/os_callout.h" +//#include "os/os_dev.h" +//#include "os/os_eventq.h" +//#include "os/os_heap.h" +//#include "os/os_mbuf.h" +#include "os/os_mempool.h" +#include "os/os_mutex.h" +//#include "os/os_sanity.h" +//#include "os/os_sched.h" +//#include "os/os_sem.h" +//#include "os/os_task.h" +//#include "os/os_time.h" + +#ifdef __cplusplus +} +#endif + +#endif /* _OS_H */ diff --git a/libraries/nffs/src/os/os_malloc.h b/libraries/nffs/src/os/os_malloc.h new file mode 100644 index 00000000..5f53bea5 --- /dev/null +++ b/libraries/nffs/src/os/os_malloc.h @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_OS_MALLOC_ +#define H_OS_MALLOC_ + +//#include "os/os_heap.h" +#include "rtos.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#undef malloc +#define malloc rtos_malloc + +#undef free +#define free rtos_free + +//#undef realloc +//#define realloc rtos_realloc + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libraries/nffs/src/os/os_mempool.c b/libraries/nffs/src/os/os_mempool.c new file mode 100644 index 00000000..66468267 --- /dev/null +++ b/libraries/nffs/src/os/os_mempool.c @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "os/os.h" + +#include +#include + +/** + * @addtogroup OSKernel + * @{ + * @defgroup OSMempool Memory Pools + * @{ + */ + +#define OS_MEMPOOL_TRUE_BLOCK_SIZE(bsize) OS_ALIGN(bsize, OS_ALIGNMENT) + +STAILQ_HEAD(, os_mempool) g_os_mempool_list = + STAILQ_HEAD_INITIALIZER(g_os_mempool_list); + +/** + * os mempool init + * + * Initialize a memory pool. + * + * @param mp Pointer to a pointer to a mempool + * @param blocks The number of blocks in the pool + * @param blocks_size The size of the block, in bytes. + * @param membuf Pointer to memory to contain blocks. + * @param name Name of the pool. + * + * @return os_error_t + */ +os_error_t +os_mempool_init(struct os_mempool *mp, int blocks, int block_size, + void *membuf, char *name) +{ + int true_block_size; + uint8_t *block_addr; + struct os_memblock *block_ptr; + + /* Check for valid parameters */ + if ((!mp) || (blocks < 0) || (block_size <= 0)) { + return OS_INVALID_PARM; + } + + if ((!membuf) && (blocks != 0)) { + return OS_INVALID_PARM; + } + + if (membuf != NULL) { + /* Blocks need to be sized properly and memory buffer should be + * aligned + */ + if (((uint32_t)membuf & (OS_ALIGNMENT - 1)) != 0) { + return OS_MEM_NOT_ALIGNED; + } + } + true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(block_size); + + /* Initialize the memory pool structure */ + mp->mp_block_size = block_size; + mp->mp_num_free = blocks; + mp->mp_min_free = blocks; + mp->mp_num_blocks = blocks; + mp->mp_membuf_addr = (uint32_t)membuf; + mp->name = name; + SLIST_FIRST(mp) = membuf; + + /* Chain the memory blocks to the free list */ + block_addr = (uint8_t *)membuf; + block_ptr = (struct os_memblock *)block_addr; + while (blocks > 1) { + block_addr += true_block_size; + SLIST_NEXT(block_ptr, mb_next) = (struct os_memblock *)block_addr; + block_ptr = (struct os_memblock *)block_addr; + --blocks; + } + + /* Last one in the list should be NULL */ + SLIST_NEXT(block_ptr, mb_next) = NULL; + + STAILQ_INSERT_TAIL(&g_os_mempool_list, mp, mp_list); + + return OS_OK; +} + +/** + * Checks if a memory block was allocated from the specified mempool. + * + * @param mp The mempool to check as parent. + * @param block_addr The memory block to check as child. + * + * @return 0 if the block does not belong to the mempool; + * 1 if the block does belong to the mempool. + */ +int +os_memblock_from(struct os_mempool *mp, void *block_addr) +{ + uint32_t true_block_size; + uint32_t baddr32; + uint32_t end; + + _Static_assert(sizeof block_addr == sizeof baddr32, + "Pointer to void must be 32-bits."); + + baddr32 = (uint32_t)block_addr; + true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(mp->mp_block_size); + end = mp->mp_membuf_addr + (mp->mp_num_blocks * true_block_size); + + /* Check that the block is in the memory buffer range. */ + if ((baddr32 < mp->mp_membuf_addr) || (baddr32 >= end)) { + return 0; + } + + /* All freed blocks should be on true block size boundaries! */ + if (((baddr32 - mp->mp_membuf_addr) % true_block_size) != 0) { + return 0; + } + + return 1; +} + +/** + * os memblock get + * + * Get a memory block from a memory pool + * + * @param mp Pointer to the memory pool + * + * @return void* Pointer to block if available; NULL otherwise + */ +void * +os_memblock_get(struct os_mempool *mp) +{ + os_sr_t sr; + struct os_memblock *block; + + /* Check to make sure they passed in a memory pool (or something) */ + block = NULL; + if (mp) { + OS_ENTER_CRITICAL(sr); + /* Check for any free */ + if (mp->mp_num_free) { + /* Get a free block */ + block = SLIST_FIRST(mp); + + /* Set new free list head */ + SLIST_FIRST(mp) = SLIST_NEXT(block, mb_next); + + /* Decrement number free by 1 */ + mp->mp_num_free--; + if (mp->mp_min_free > mp->mp_num_free) { + mp->mp_min_free = mp->mp_num_free; + } + } + OS_EXIT_CRITICAL(sr); + } + + return (void *)block; +} + +/** + * os memblock put + * + * Puts the memory block back into the pool + * + * @param mp Pointer to memory pool + * @param block_addr Pointer to memory block + * + * @return os_error_t + */ +os_error_t +os_memblock_put(struct os_mempool *mp, void *block_addr) +{ + os_sr_t sr; + struct os_memblock *block; + + /* Make sure parameters are valid */ + if ((mp == NULL) || (block_addr == NULL)) { + return OS_INVALID_PARM; + } + + /* Check that the block we are freeing is a valid block! */ + if (!os_memblock_from(mp, block_addr)) { + return OS_INVALID_PARM; + } + + block = (struct os_memblock *)block_addr; + OS_ENTER_CRITICAL(sr); + + /* Chain current free list pointer to this block; make this block head */ + SLIST_NEXT(block, mb_next) = SLIST_FIRST(mp); + SLIST_FIRST(mp) = block; + + /* XXX: Should we check that the number free <= number blocks? */ + /* Increment number free */ + mp->mp_num_free++; + + OS_EXIT_CRITICAL(sr); + + return OS_OK; +} + + +struct os_mempool * +os_mempool_info_get_next(struct os_mempool *mp, struct os_mempool_info *omi) +{ + struct os_mempool *cur; + + if (mp == NULL) { + cur = STAILQ_FIRST(&g_os_mempool_list); + } else { + cur = STAILQ_NEXT(mp, mp_list); + } + + if (cur == NULL) { + return (NULL); + } + + omi->omi_block_size = cur->mp_block_size; + omi->omi_num_blocks = cur->mp_num_blocks; + omi->omi_num_free = cur->mp_num_free; + omi->omi_min_free = cur->mp_min_free; + strncpy(omi->omi_name, cur->name, sizeof(omi->omi_name)); + + return (cur); +} + + +/** + * @} OSMempool + * @} OSKernel + */ diff --git a/libraries/nffs/src/os/os_mempool.h b/libraries/nffs/src/os/os_mempool.h new file mode 100644 index 00000000..2c2b0c34 --- /dev/null +++ b/libraries/nffs/src/os/os_mempool.h @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _OS_MEMPOOL_H_ +#define _OS_MEMPOOL_H_ + +#include "os/os.h" +#include "os/queue.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * A memory block structure. This simply contains a pointer to the free list + * chain and is only used when the block is on the free list. When the block + * has been removed from the free list the entire memory block is usable by the + * caller. + */ +struct os_memblock { + SLIST_ENTRY(os_memblock) mb_next; +}; + +/* XXX: Change this structure so that we keep the first address in the pool? */ +/* XXX: add memory debug structure and associated code */ +/* XXX: Change how I coded the SLIST_HEAD here. It should be named: + SLIST_HEAD(,os_memblock) mp_head; */ + +/* Memory pool */ +struct os_mempool { + int mp_block_size; /* Size of the memory blocks, in bytes. */ + int mp_num_blocks; /* The number of memory blocks. */ + int mp_num_free; /* The number of free blocks left */ + int mp_min_free; /* The lowest number of free blocks seen */ + uint32_t mp_membuf_addr; /* Address of memory buffer used by pool */ + STAILQ_ENTRY(os_mempool) mp_list; + SLIST_HEAD(,os_memblock); /* Pointer to list of free blocks */ + char *name; /* Name for memory block */ +}; + +#define OS_MEMPOOL_INFO_NAME_LEN (32) + +struct os_mempool_info { + int omi_block_size; + int omi_num_blocks; + int omi_num_free; + int omi_min_free; + char omi_name[OS_MEMPOOL_INFO_NAME_LEN]; +}; + +struct os_mempool *os_mempool_info_get_next(struct os_mempool *, + struct os_mempool_info *); + +/* + * To calculate size of the memory buffer needed for the pool. NOTE: This size + * is NOT in bytes! The size is the number of os_membuf_t elements required for + * the memory pool. + */ +#if (OS_CFG_ALIGNMENT == OS_CFG_ALIGN_4) +#define OS_MEMPOOL_SIZE(n,blksize) ((((blksize) + 3) / 4) * (n)) +typedef uint32_t os_membuf_t; +#else +#define OS_MEMPOOL_SIZE(n,blksize) ((((blksize) + 7) / 8) * (n)) +typedef uint64_t os_membuf_t; +#endif + +/** Calculates the number of bytes required to initialize a memory pool. */ +#define OS_MEMPOOL_BYTES(n,blksize) \ + (sizeof (os_membuf_t) * OS_MEMPOOL_SIZE((n), (blksize))) + +/* Initialize a memory pool */ +os_error_t os_mempool_init(struct os_mempool *mp, int blocks, int block_size, + void *membuf, char *name); + +/* Checks if a memory block was allocated from the specified mempool. */ +int os_memblock_from(struct os_mempool *mp, void *block_addr); + +/* Get a memory block from the pool */ +void *os_memblock_get(struct os_mempool *mp); + +/* Put the memory block back into the pool */ +os_error_t os_memblock_put(struct os_mempool *mp, void *block_addr); + +#ifdef __cplusplus +} +#endif + +#endif /* _OS_MEMPOOL_H_ */ diff --git a/libraries/nffs/src/os/os_mutex.h b/libraries/nffs/src/os/os_mutex.h new file mode 100644 index 00000000..b55af210 --- /dev/null +++ b/libraries/nffs/src/os/os_mutex.h @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _OS_MUTEX_H_ +#define _OS_MUTEX_H_ + +#include "os/os.h" +//#include "os/queue.h" + +#include "rtos.h" + +#ifdef __cplusplus +extern "C" { +#endif + +//struct os_mutex +//{ +// SLIST_HEAD(, os_task) mu_head; /* chain of waiting tasks */ +// uint8_t _pad; +// uint8_t mu_prio; /* owner's default priority*/ +// uint16_t mu_level; /* call nesting level */ +// struct os_task *mu_owner; /* owners task */ +//}; + +/* + XXX: NOTES + -> Should we add a magic number or flag to the mutex structure so + that we know that this is a mutex? We can use it for double checking + that a proper mutex was passed in to the API. + -> What debug information should we add to this structure? Who last + acquired the mutex? File/line where it was released? + -> Should we add a name to the mutex? + -> Should we add a "os_mutex_inspect() api? +*/ + +/* XXX: api to create +os_mutex_inspect(); +*/ + +struct os_mutex +{ + SemaphoreHandle_t sem; +}; + +/* Initialize a mutex */ +static inline os_error_t os_mutex_init(struct os_mutex *mu) +{ + mu->sem = xSemaphoreCreateMutex(); + + return (mu->sem == NULL) ? 0 : 1; +} + + +/* Release a mutex */ +static inline os_error_t os_mutex_release(struct os_mutex *mu) +{ + return xSemaphoreGive(mu->sem) ? 0 : 1; +} + +/* Pend (wait) for a mutex */ +static inline os_error_t os_mutex_pend(struct os_mutex *mu, uint32_t timeout) +{ + return xSemaphoreTake(mu->sem, timeout) ? 0 : 1; +} + +#ifdef __cplusplus +} +#endif + +#endif /* _OS_MUTEX_H_ */ diff --git a/libraries/nffs/src/os/queue.h b/libraries/nffs/src/os/queue.h new file mode 100644 index 00000000..faffd851 --- /dev/null +++ b/libraries/nffs/src/os/queue.h @@ -0,0 +1,522 @@ +/* + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)queue.h 8.5 (Berkeley) 8/20/94 + * $FreeBSD: src/sys/sys/queue.h,v 1.32.2.7 2002/04/17 14:21:02 des Exp $ + */ + +#ifndef _QUEUE_H_ +#define _QUEUE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * This file defines five types of data structures: singly-linked lists, + * singly-linked tail queues, lists, tail queues, and circular queues. + * + * A singly-linked list is headed by a single forward pointer. The elements + * are singly linked for minimum space and pointer manipulation overhead at + * the expense of O(n) removal for arbitrary elements. New elements can be + * added to the list after an existing element or at the head of the list. + * Elements being removed from the head of the list should use the explicit + * macro for this purpose for optimum efficiency. A singly-linked list may + * only be traversed in the forward direction. Singly-linked lists are ideal + * for applications with large datasets and few or no removals or for + * implementing a LIFO queue. + * + * A singly-linked tail queue is headed by a pair of pointers, one to the + * head of the list and the other to the tail of the list. The elements are + * singly linked for minimum space and pointer manipulation overhead at the + * expense of O(n) removal for arbitrary elements. New elements can be added + * to the list after an existing element, at the head of the list, or at the + * end of the list. Elements being removed from the head of the tail queue + * should use the explicit macro for this purpose for optimum efficiency. + * A singly-linked tail queue may only be traversed in the forward direction. + * Singly-linked tail queues are ideal for applications with large datasets + * and few or no removals or for implementing a FIFO queue. + * + * A list is headed by a single forward pointer (or an array of forward + * pointers for a hash table header). The elements are doubly linked + * so that an arbitrary element can be removed without a need to + * traverse the list. New elements can be added to the list before + * or after an existing element or at the head of the list. A list + * may only be traversed in the forward direction. + * + * A tail queue is headed by a pair of pointers, one to the head of the + * list and the other to the tail of the list. The elements are doubly + * linked so that an arbitrary element can be removed without a need to + * traverse the list. New elements can be added to the list before or + * after an existing element, at the head of the list, or at the end of + * the list. A tail queue may be traversed in either direction. + * + * A circle queue is headed by a pair of pointers, one to the head of the + * list and the other to the tail of the list. The elements are doubly + * linked so that an arbitrary element can be removed without a need to + * traverse the list. New elements can be added to the list before or after + * an existing element, at the head of the list, or at the end of the list. + * A circle queue may be traversed in either direction, but has a more + * complex end of list detection. + * + * For details on the use of these macros, see the queue(3) manual page. + * + * + * SLIST LIST STAILQ TAILQ CIRCLEQ + * _HEAD + + + + + + * _HEAD_INITIALIZER + + + + + + * _ENTRY + + + + + + * _INIT + + + + + + * _EMPTY + + + + + + * _FIRST + + + + + + * _NEXT + + + + + + * _PREV - - - + + + * _LAST - - + + + + * _FOREACH + + + + + + * _FOREACH_REVERSE - - - + + + * _INSERT_HEAD + + + + + + * _INSERT_BEFORE - + - + + + * _INSERT_AFTER + + + + + + * _INSERT_TAIL - - + + + + * _REMOVE_HEAD + - + - - + * _REMOVE + + + + + + * + */ + +/* + * Singly-linked List declarations. + */ +#define SLIST_HEAD(name, type) \ +struct name { \ + struct type *slh_first; /* first element */ \ +} + +#define SLIST_HEAD_INITIALIZER(head) \ + { NULL } + +#define SLIST_ENTRY(type) \ +struct { \ + struct type *sle_next; /* next element */ \ +} + +/* + * Singly-linked List functions. + */ +#define SLIST_EMPTY(head) ((head)->slh_first == NULL) + +#define SLIST_FIRST(head) ((head)->slh_first) + +#define SLIST_FOREACH(var, head, field) \ + for ((var) = SLIST_FIRST((head)); \ + (var); \ + (var) = SLIST_NEXT((var), field)) + +#define SLIST_INIT(head) do { \ + SLIST_FIRST((head)) = NULL; \ +} while (0) + +#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ + SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ + SLIST_NEXT((slistelm), field) = (elm); \ +} while (0) + +#define SLIST_INSERT_HEAD(head, elm, field) do { \ + SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ + SLIST_FIRST((head)) = (elm); \ +} while (0) + +#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) + +#define SLIST_REMOVE(head, elm, type, field) do { \ + if (SLIST_FIRST((head)) == (elm)) { \ + SLIST_REMOVE_HEAD((head), field); \ + } \ + else { \ + struct type *curelm = SLIST_FIRST((head)); \ + while (SLIST_NEXT(curelm, field) != (elm)) \ + curelm = SLIST_NEXT(curelm, field); \ + SLIST_NEXT(curelm, field) = \ + SLIST_NEXT(SLIST_NEXT(curelm, field), field); \ + } \ +} while (0) + +#define SLIST_REMOVE_HEAD(head, field) do { \ + SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ +} while (0) + +/* + * Singly-linked Tail queue declarations. + */ +#define STAILQ_HEAD(name, type) \ +struct name { \ + struct type *stqh_first;/* first element */ \ + struct type **stqh_last;/* addr of last next element */ \ +} + +#define STAILQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).stqh_first } + +#define STAILQ_ENTRY(type) \ +struct { \ + struct type *stqe_next; /* next element */ \ +} + +/* + * Singly-linked Tail queue functions. + */ +#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) + +#define STAILQ_FIRST(head) ((head)->stqh_first) + +#define STAILQ_FOREACH(var, head, field) \ + for((var) = STAILQ_FIRST((head)); \ + (var); \ + (var) = STAILQ_NEXT((var), field)) + +#define STAILQ_INIT(head) do { \ + STAILQ_FIRST((head)) = NULL; \ + (head)->stqh_last = &STAILQ_FIRST((head)); \ +} while (0) + +#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ + if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ + STAILQ_NEXT((tqelm), field) = (elm); \ +} while (0) + +#define STAILQ_INSERT_HEAD(head, elm, field) do { \ + if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ + STAILQ_FIRST((head)) = (elm); \ +} while (0) + +#define STAILQ_INSERT_TAIL(head, elm, field) do { \ + STAILQ_NEXT((elm), field) = NULL; \ + *(head)->stqh_last = (elm); \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ +} while (0) + +#define STAILQ_LAST(head, type, field) \ + (STAILQ_EMPTY(head) ? \ + NULL : \ + ((struct type *) \ + ((char *)((head)->stqh_last) - offsetof(struct type, field)))) + +#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) + +#define STAILQ_REMOVE(head, elm, type, field) do { \ + if (STAILQ_FIRST((head)) == (elm)) { \ + STAILQ_REMOVE_HEAD(head, field); \ + } \ + else { \ + struct type *curelm = STAILQ_FIRST((head)); \ + while (STAILQ_NEXT(curelm, field) != (elm)) \ + curelm = STAILQ_NEXT(curelm, field); \ + if ((STAILQ_NEXT(curelm, field) = \ + STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\ + (head)->stqh_last = &STAILQ_NEXT((curelm), field);\ + } \ +} while (0) + +#define STAILQ_REMOVE_HEAD(head, field) do { \ + if ((STAILQ_FIRST((head)) = \ + STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ + (head)->stqh_last = &STAILQ_FIRST((head)); \ +} while (0) + +#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \ + if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \ + (head)->stqh_last = &STAILQ_FIRST((head)); \ +} while (0) + +#define STAILQ_REMOVE_AFTER(head, elm, field) do { \ + if ((STAILQ_NEXT(elm, field) = \ + STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ +} while (0) + +/* + * List declarations. + */ +#define LIST_HEAD(name, type) \ +struct name { \ + struct type *lh_first; /* first element */ \ +} + +#define LIST_HEAD_INITIALIZER(head) \ + { NULL } + +#define LIST_ENTRY(type) \ +struct { \ + struct type *le_next; /* next element */ \ + struct type **le_prev; /* address of previous next element */ \ +} + +/* + * List functions. + */ + +#define LIST_EMPTY(head) ((head)->lh_first == NULL) + +#define LIST_FIRST(head) ((head)->lh_first) + +#define LIST_FOREACH(var, head, field) \ + for ((var) = LIST_FIRST((head)); \ + (var); \ + (var) = LIST_NEXT((var), field)) + +#define LIST_INIT(head) do { \ + LIST_FIRST((head)) = NULL; \ +} while (0) + +#define LIST_INSERT_AFTER(listelm, elm, field) do { \ + if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ + LIST_NEXT((listelm), field)->field.le_prev = \ + &LIST_NEXT((elm), field); \ + LIST_NEXT((listelm), field) = (elm); \ + (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ +} while (0) + +#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ + (elm)->field.le_prev = (listelm)->field.le_prev; \ + LIST_NEXT((elm), field) = (listelm); \ + *(listelm)->field.le_prev = (elm); \ + (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ +} while (0) + +#define LIST_INSERT_HEAD(head, elm, field) do { \ + if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ + LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ + LIST_FIRST((head)) = (elm); \ + (elm)->field.le_prev = &LIST_FIRST((head)); \ +} while (0) + +#define LIST_NEXT(elm, field) ((elm)->field.le_next) + +#define LIST_REMOVE(elm, field) do { \ + if (LIST_NEXT((elm), field) != NULL) \ + LIST_NEXT((elm), field)->field.le_prev = \ + (elm)->field.le_prev; \ + *(elm)->field.le_prev = LIST_NEXT((elm), field); \ +} while (0) + +/* + * Tail queue declarations. + */ +#define TAILQ_HEAD(name, type) \ +struct name { \ + struct type *tqh_first; /* first element */ \ + struct type **tqh_last; /* addr of last next element */ \ +} + +#define TAILQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).tqh_first } + +#define TAILQ_ENTRY(type) \ +struct { \ + struct type *tqe_next; /* next element */ \ + struct type **tqe_prev; /* address of previous next element */ \ +} + +/* + * Tail queue functions. + */ +#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) + +#define TAILQ_FIRST(head) ((head)->tqh_first) + +#define TAILQ_FOREACH(var, head, field) \ + for ((var) = TAILQ_FIRST((head)); \ + (var); \ + (var) = TAILQ_NEXT((var), field)) + +#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ + for ((var) = TAILQ_LAST((head), headname); \ + (var); \ + (var) = TAILQ_PREV((var), headname, field)) + +#define TAILQ_INIT(head) do { \ + TAILQ_FIRST((head)) = NULL; \ + (head)->tqh_last = &TAILQ_FIRST((head)); \ +} while (0) + +#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ + if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ + TAILQ_NEXT((elm), field)->field.tqe_prev = \ + &TAILQ_NEXT((elm), field); \ + else \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); \ + TAILQ_NEXT((listelm), field) = (elm); \ + (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ +} while (0) + +#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ + TAILQ_NEXT((elm), field) = (listelm); \ + *(listelm)->field.tqe_prev = (elm); \ + (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ +} while (0) + +#define TAILQ_INSERT_HEAD(head, elm, field) do { \ + if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ + TAILQ_FIRST((head))->field.tqe_prev = \ + &TAILQ_NEXT((elm), field); \ + else \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); \ + TAILQ_FIRST((head)) = (elm); \ + (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ +} while (0) + +#define TAILQ_INSERT_TAIL(head, elm, field) do { \ + TAILQ_NEXT((elm), field) = NULL; \ + (elm)->field.tqe_prev = (head)->tqh_last; \ + *(head)->tqh_last = (elm); \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); \ +} while (0) + +#define TAILQ_LAST(head, headname) \ + (*(((struct headname *)((head)->tqh_last))->tqh_last)) + +#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) + +#define TAILQ_PREV(elm, headname, field) \ + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) + +#define TAILQ_REMOVE(head, elm, field) do { \ + if ((TAILQ_NEXT((elm), field)) != NULL) \ + TAILQ_NEXT((elm), field)->field.tqe_prev = \ + (elm)->field.tqe_prev; \ + else \ + (head)->tqh_last = (elm)->field.tqe_prev; \ + *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ +} while (0) + +/* + * Circular queue declarations. + */ +#define CIRCLEQ_HEAD(name, type) \ +struct name { \ + struct type *cqh_first; /* first element */ \ + struct type *cqh_last; /* last element */ \ +} + +#define CIRCLEQ_HEAD_INITIALIZER(head) \ + { (void *)&(head), (void *)&(head) } + +#define CIRCLEQ_ENTRY(type) \ +struct { \ + struct type *cqe_next; /* next element */ \ + struct type *cqe_prev; /* previous element */ \ +} + +/* + * Circular queue functions. + */ +#define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head)) + +#define CIRCLEQ_FIRST(head) ((head)->cqh_first) + +#define CIRCLEQ_FOREACH(var, head, field) \ + for ((var) = CIRCLEQ_FIRST((head)); \ + (var) != (void *)(head) || ((var) = NULL); \ + (var) = CIRCLEQ_NEXT((var), field)) + +#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \ + for ((var) = CIRCLEQ_LAST((head)); \ + (var) != (void *)(head) || ((var) = NULL); \ + (var) = CIRCLEQ_PREV((var), field)) + +#define CIRCLEQ_INIT(head) do { \ + CIRCLEQ_FIRST((head)) = (void *)(head); \ + CIRCLEQ_LAST((head)) = (void *)(head); \ +} while (0) + +#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ + CIRCLEQ_NEXT((elm), field) = CIRCLEQ_NEXT((listelm), field); \ + CIRCLEQ_PREV((elm), field) = (listelm); \ + if (CIRCLEQ_NEXT((listelm), field) == (void *)(head)) \ + CIRCLEQ_LAST((head)) = (elm); \ + else \ + CIRCLEQ_PREV(CIRCLEQ_NEXT((listelm), field), field) = (elm);\ + CIRCLEQ_NEXT((listelm), field) = (elm); \ +} while (0) + +#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ + CIRCLEQ_NEXT((elm), field) = (listelm); \ + CIRCLEQ_PREV((elm), field) = CIRCLEQ_PREV((listelm), field); \ + if (CIRCLEQ_PREV((listelm), field) == (void *)(head)) \ + CIRCLEQ_FIRST((head)) = (elm); \ + else \ + CIRCLEQ_NEXT(CIRCLEQ_PREV((listelm), field), field) = (elm);\ + CIRCLEQ_PREV((listelm), field) = (elm); \ +} while (0) + +#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \ + CIRCLEQ_NEXT((elm), field) = CIRCLEQ_FIRST((head)); \ + CIRCLEQ_PREV((elm), field) = (void *)(head); \ + if (CIRCLEQ_LAST((head)) == (void *)(head)) \ + CIRCLEQ_LAST((head)) = (elm); \ + else \ + CIRCLEQ_PREV(CIRCLEQ_FIRST((head)), field) = (elm); \ + CIRCLEQ_FIRST((head)) = (elm); \ +} while (0) + +#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \ + CIRCLEQ_NEXT((elm), field) = (void *)(head); \ + CIRCLEQ_PREV((elm), field) = CIRCLEQ_LAST((head)); \ + if (CIRCLEQ_FIRST((head)) == (void *)(head)) \ + CIRCLEQ_FIRST((head)) = (elm); \ + else \ + CIRCLEQ_NEXT(CIRCLEQ_LAST((head)), field) = (elm); \ + CIRCLEQ_LAST((head)) = (elm); \ +} while (0) + +#define CIRCLEQ_LAST(head) ((head)->cqh_last) + +#define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next) + +#define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev) + +#define CIRCLEQ_REMOVE(head, elm, field) do { \ + if (CIRCLEQ_NEXT((elm), field) == (void *)(head)) \ + CIRCLEQ_LAST((head)) = CIRCLEQ_PREV((elm), field); \ + else \ + CIRCLEQ_PREV(CIRCLEQ_NEXT((elm), field), field) = \ + CIRCLEQ_PREV((elm), field); \ + if (CIRCLEQ_PREV((elm), field) == (void *)(head)) \ + CIRCLEQ_FIRST((head)) = CIRCLEQ_NEXT((elm), field); \ + else \ + CIRCLEQ_NEXT(CIRCLEQ_PREV((elm), field), field) = \ + CIRCLEQ_NEXT((elm), field); \ +} while (0) + +#ifdef __cplusplus +} +#endif + +#endif /* !_QUEUE_H_ */ diff --git a/libraries/nffs/src/syscfg/syscfg.h b/libraries/nffs/src/syscfg/syscfg.h new file mode 100644 index 00000000..36f4ade7 --- /dev/null +++ b/libraries/nffs/src/syscfg/syscfg.h @@ -0,0 +1,52 @@ +/**************************************************************************/ +/*! + @file syscfg.h + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#ifndef SYSCFG_H_ +#define SYSCFG_H_ + +/** + * This macro exists to ensure code includes this header when needed. If code + * checks the existence of a setting directly via ifdef without including this + * header, the setting macro will silently evaluate to 0. In contrast, an + * attempt to use these macros without including this header will result in a + * compiler error. + */ +#define MYNEWT_VAL(x) MYNEWT_VAL_ ## x + +#define MYNEWT_VAL_FS_CLI (0) +#define MYNEWT_VAL_NFFS_FLASH_AREA 0 +#define MYNEWT_VAL_NFFS_DETECT_FAIL (NFFS_DETECT_FAIL_FORMAT) + +#endif /* SYSCFG_H_ */