From c7685742be49bd4e6f9dc97723238c63dadaad88 Mon Sep 17 00:00:00 2001 From: David Saada Date: Tue, 14 Aug 2018 19:51:03 +0300 Subject: [PATCH] Add some logic related to initialization - Add an initialization flag on which read/program/erase actions depend. - Add an init reference count. --- DataFlashBlockDevice.cpp | 45 +++++++++++++++++++++++++++++++++++++++- DataFlashBlockDevice.h | 2 ++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/DataFlashBlockDevice.cpp b/DataFlashBlockDevice.cpp index 0d2309661a..3661b78d32 100644 --- a/DataFlashBlockDevice.cpp +++ b/DataFlashBlockDevice.cpp @@ -15,6 +15,7 @@ */ #include "DataFlashBlockDevice.h" +#include "mbed_critical.h" #include @@ -138,7 +139,11 @@ DataFlashBlockDevice::DataFlashBlockDevice(PinName mosi, : _spi(mosi, miso, sclk), _cs(cs, 1), _nwp(nwp), - _device_size(0) + _device_size(0), + _page_size(0), + _block_size(0), + _is_initialized(0), + _init_ref_count(0) { /* check that frequency is within range */ if (freq > DATAFLASH_LOW_FREQUENCY) { @@ -161,6 +166,16 @@ int DataFlashBlockDevice::init() { DEBUG_PRINTF("init\r\n"); + if (!_is_initialized) { + _init_ref_count = 0; + } + + uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1); + + if (val != 1) { + return BD_ERROR_OK; + } + int result = BD_ERROR_DEVICE_ERROR; /* read ID register to validate model and set dimensions */ @@ -262,6 +277,10 @@ int DataFlashBlockDevice::init() /* write protect device when idle */ _write_enable(false); + if (result == BD_ERROR_OK) { + _is_initialized = true; + } + return result; } @@ -269,6 +288,18 @@ int DataFlashBlockDevice::deinit() { DEBUG_PRINTF("deinit\r\n"); + if (!_is_initialized) { + _init_ref_count = 0; + return BD_ERROR_OK; + } + + uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1); + + if (val) { + return BD_ERROR_OK; + } + + _is_initialized = false; return BD_ERROR_OK; } @@ -276,6 +307,10 @@ int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size) { DEBUG_PRINTF("read: %p %" PRIX64 " %" PRIX64 "\r\n", buffer, addr, size); + if (!_is_initialized) { + return BD_ERROR_DEVICE_ERROR; + } + int result = BD_ERROR_DEVICE_ERROR; /* check parameters are valid and the read is within bounds */ @@ -317,6 +352,10 @@ int DataFlashBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t { DEBUG_PRINTF("program: %p %" PRIX64 " %" PRIX64 "\r\n", buffer, addr, size); + if (!_is_initialized) { + return BD_ERROR_DEVICE_ERROR; + } + int result = BD_ERROR_DEVICE_ERROR; /* check parameters are valid and the write is within bounds */ @@ -379,6 +418,10 @@ int DataFlashBlockDevice::erase(bd_addr_t addr, bd_size_t size) { DEBUG_PRINTF("erase: %" PRIX64 " %" PRIX64 "\r\n", addr, size); + if (!_is_initialized) { + return BD_ERROR_DEVICE_ERROR; + } + int result = BD_ERROR_DEVICE_ERROR; /* check parameters are valid and the erase is within bounds */ diff --git a/DataFlashBlockDevice.h b/DataFlashBlockDevice.h index e73b40222f..d3e0448383 100644 --- a/DataFlashBlockDevice.h +++ b/DataFlashBlockDevice.h @@ -163,6 +163,8 @@ private: uint32_t _device_size; uint16_t _page_size; uint16_t _block_size; + bool _is_initialized; + uint32_t _init_ref_count; // Internal functions uint16_t _get_register(uint8_t opcode);