Skip to content

Commit d04c8e3

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 d04c8e3

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

SPIFBlockDevice.cpp

Lines changed: 41 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,15 +135,28 @@ 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);
136158

159+
_is_initialized = false;
137160
return 0;
138161
}
139162

@@ -249,6 +272,10 @@ int SPIFBlockDevice::_wren()
249272

250273
int SPIFBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
251274
{
275+
if (!_is_initialized) {
276+
return BD_ERROR_DEVICE_ERROR;
277+
}
278+
252279
// Check the address and size fit onto the chip.
253280
MBED_ASSERT(is_valid_read(addr, size));
254281

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

291+
if (!_is_initialized) {
292+
return BD_ERROR_DEVICE_ERROR;
293+
}
294+
264295
while (size > 0) {
265296
int err = _wren();
266297
if (err) {
@@ -292,6 +323,10 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t size)
292323
// Check the address and size fit onto the chip.
293324
MBED_ASSERT(is_valid_erase(addr, size));
294325

326+
if (!_is_initialized) {
327+
return BD_ERROR_DEVICE_ERROR;
328+
}
329+
295330
while (size > 0) {
296331
int err = _wren();
297332
if (err) {
@@ -336,6 +371,10 @@ bd_size_t SPIFBlockDevice::get_erase_size(bd_addr_t addr) const
336371

337372
bd_size_t SPIFBlockDevice::size() const
338373
{
374+
if (!_is_initialized) {
375+
return BD_ERROR_DEVICE_ERROR;
376+
}
377+
339378
return _size;
340379
}
341380

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)