Skip to content

Commit 520d1d4

Browse files
author
David Saada
committed
Add some logic related to initialization:
- Add an initialization flag on which read/program/erase actions depend. - Add an init reference count.
1 parent 5c49300 commit 520d1d4

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

SPIFBlockDevice.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
#include "SPIFBlockDevice.h"
18-
18+
#include "mbed_critical.h"
1919

2020
// Read/write/erase sizes
2121
#define SPIF_READ_SIZE 1
@@ -49,14 +49,24 @@ enum ops {
4949

5050
SPIFBlockDevice::SPIFBlockDevice(
5151
PinName mosi, PinName miso, PinName sclk, PinName cs, int freq)
52-
: _spi(mosi, miso, sclk), _cs(cs), _size(0)
52+
: _spi(mosi, miso, sclk), _cs(cs), _size(0), _is_initialized(false), _init_ref_count(0)
5353
{
5454
_cs = 1;
5555
_spi.frequency(freq);
5656
}
5757

5858
int SPIFBlockDevice::init()
5959
{
60+
if (!_is_initialized) {
61+
_init_ref_count = 0;
62+
}
63+
64+
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
65+
66+
if (val != 1) {
67+
return BD_ERROR_OK;
68+
}
69+
6070
// Check for vendor specific hacks, these should move into more general
6171
// handling when possible. RDID is not used to verify a device is attached.
6272
uint8_t id[3];
@@ -125,11 +135,23 @@ int SPIFBlockDevice::init()
125135
(table[4] << 0 ));
126136
_size = (density/8) + 1;
127137

138+
_is_initialized = true;
128139
return 0;
129140
}
130141

131142
int SPIFBlockDevice::deinit()
132143
{
144+
if (!_is_initialized) {
145+
_init_ref_count = 0;
146+
return 0;
147+
}
148+
149+
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
150+
151+
if (val) {
152+
return 0;
153+
}
154+
133155
// Latch write disable just to keep noise
134156
// from changing the device
135157
_cmdwrite(SPIF_WRDI, 0, 0, 0x0, NULL);
@@ -249,6 +271,10 @@ int SPIFBlockDevice::_wren()
249271

250272
int SPIFBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
251273
{
274+
if (!_is_initialized) {
275+
return BD_ERROR_DEVICE_ERROR;
276+
}
277+
252278
// Check the address and size fit onto the chip.
253279
MBED_ASSERT(is_valid_read(addr, size));
254280

@@ -261,6 +287,10 @@ int SPIFBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
261287
// Check the address and size fit onto the chip.
262288
MBED_ASSERT(is_valid_program(addr, size));
263289

290+
if (!_is_initialized) {
291+
return BD_ERROR_DEVICE_ERROR;
292+
}
293+
264294
while (size > 0) {
265295
int err = _wren();
266296
if (err) {
@@ -292,6 +322,10 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t size)
292322
// Check the address and size fit onto the chip.
293323
MBED_ASSERT(is_valid_erase(addr, size));
294324

325+
if (!_is_initialized) {
326+
return BD_ERROR_DEVICE_ERROR;
327+
}
328+
295329
while (size > 0) {
296330
int err = _wren();
297331
if (err) {
@@ -336,6 +370,10 @@ bd_size_t SPIFBlockDevice::get_erase_size(bd_addr_t addr) const
336370

337371
bd_size_t SPIFBlockDevice::size() const
338372
{
373+
if (!_is_initialized) {
374+
return BD_ERROR_DEVICE_ERROR;
375+
}
376+
339377
return _size;
340378
}
341379

SPIFBlockDevice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ class SPIFBlockDevice : public BlockDevice {
163163
// Device configuration discovered through sfdp
164164
bd_size_t _size;
165165

166+
bool _is_initialized;
167+
uint32_t _init_ref_count;
168+
166169
// Internal functions
167170
int _wren();
168171
int _sync();

0 commit comments

Comments
 (0)