Merge pull request #11 from ARMmbed/david_fix_init

Add some logic related to initialization
pull/7774/head
Christopher Haster 2018-08-25 12:17:21 -05:00 committed by GitHub
commit baabb52097
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -15,6 +15,7 @@
*/ */
#include "DataFlashBlockDevice.h" #include "DataFlashBlockDevice.h"
#include "mbed_critical.h"
#include <inttypes.h> #include <inttypes.h>
@ -138,7 +139,11 @@ DataFlashBlockDevice::DataFlashBlockDevice(PinName mosi,
: _spi(mosi, miso, sclk), : _spi(mosi, miso, sclk),
_cs(cs, 1), _cs(cs, 1),
_nwp(nwp), _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 */ /* check that frequency is within range */
if (freq > DATAFLASH_LOW_FREQUENCY) { if (freq > DATAFLASH_LOW_FREQUENCY) {
@ -161,6 +166,16 @@ int DataFlashBlockDevice::init()
{ {
DEBUG_PRINTF("init\r\n"); 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; int result = BD_ERROR_DEVICE_ERROR;
/* read ID register to validate model and set dimensions */ /* read ID register to validate model and set dimensions */
@ -262,6 +277,10 @@ int DataFlashBlockDevice::init()
/* write protect device when idle */ /* write protect device when idle */
_write_enable(false); _write_enable(false);
if (result == BD_ERROR_OK) {
_is_initialized = true;
}
return result; return result;
} }
@ -269,6 +288,18 @@ int DataFlashBlockDevice::deinit()
{ {
DEBUG_PRINTF("deinit\r\n"); 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; 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); 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; int result = BD_ERROR_DEVICE_ERROR;
/* check parameters are valid and the read is within bounds */ /* 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); 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; int result = BD_ERROR_DEVICE_ERROR;
/* check parameters are valid and the write is within bounds */ /* 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); DEBUG_PRINTF("erase: %" PRIX64 " %" PRIX64 "\r\n", addr, size);
if (!_is_initialized) {
return BD_ERROR_DEVICE_ERROR;
}
int result = BD_ERROR_DEVICE_ERROR; int result = BD_ERROR_DEVICE_ERROR;
/* check parameters are valid and the erase is within bounds */ /* check parameters are valid and the erase is within bounds */

View File

@ -163,6 +163,8 @@ private:
uint32_t _device_size; uint32_t _device_size;
uint16_t _page_size; uint16_t _page_size;
uint16_t _block_size; uint16_t _block_size;
bool _is_initialized;
uint32_t _init_ref_count;
// Internal functions // Internal functions
uint16_t _get_register(uint8_t opcode); uint16_t _get_register(uint8_t opcode);