Skip to content

Commit 7614055

Browse files
gekyJaakko Korhonen
authored andcommitted
Added mutex to DataFlash for thread safety
1 parent c3e0e3e commit 7614055

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ DataFlashBlockDevice::DataFlashBlockDevice(PinName mosi,
164164

165165
int DataFlashBlockDevice::init()
166166
{
167+
_mutex.lock();
167168
DEBUG_PRINTF("init\r\n");
168169

169170
if (!_is_initialized) {
@@ -173,6 +174,7 @@ int DataFlashBlockDevice::init()
173174
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
174175

175176
if (val != 1) {
177+
_mutex.unlock();
176178
return BD_ERROR_OK;
177179
}
178180

@@ -281,33 +283,40 @@ int DataFlashBlockDevice::init()
281283
_is_initialized = true;
282284
}
283285

286+
_mutex.unlock();
284287
return result;
285288
}
286289

287290
int DataFlashBlockDevice::deinit()
288291
{
292+
_mutex.lock();
289293
DEBUG_PRINTF("deinit\r\n");
290294

291295
if (!_is_initialized) {
292296
_init_ref_count = 0;
297+
_mutex.unlock();
293298
return BD_ERROR_OK;
294299
}
295300

296301
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
297302

298303
if (val) {
304+
_mutex.unlock();
299305
return BD_ERROR_OK;
300306
}
301307

302308
_is_initialized = false;
309+
_mutex.unlock();
303310
return BD_ERROR_OK;
304311
}
305312

306313
int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
307314
{
315+
_mutex.lock();
308316
DEBUG_PRINTF("read: %p %" PRIX64 " %" PRIX64 "\r\n", buffer, addr, size);
309317

310318
if (!_is_initialized) {
319+
_mutex.unlock();
311320
return BD_ERROR_DEVICE_ERROR;
312321
}
313322

@@ -350,6 +359,7 @@ int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
350359

351360
int DataFlashBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
352361
{
362+
_mutex.lock();
353363
DEBUG_PRINTF("program: %p %" PRIX64 " %" PRIX64 "\r\n", buffer, addr, size);
354364

355365
if (!_is_initialized) {
@@ -416,6 +426,7 @@ int DataFlashBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t
416426

417427
int DataFlashBlockDevice::erase(bd_addr_t addr, bd_size_t size)
418428
{
429+
_mutex.lock();
419430
DEBUG_PRINTF("erase: %" PRIX64 " %" PRIX64 "\r\n", addr, size);
420431

421432
if (!_is_initialized) {
@@ -484,23 +495,29 @@ bd_size_t DataFlashBlockDevice::get_program_size() const
484495

485496
bd_size_t DataFlashBlockDevice::get_erase_size() const
486497
{
498+
_mutex.lock();
487499
DEBUG_PRINTF("erase size: %" PRIX16 "\r\n", _block_size);
488-
489-
return _block_size;
500+
bd_size_t block_size = _block_size;
501+
_mutex.unlock();
502+
return block_size;
490503
}
491504

492505
bd_size_t DataFlashBlockDevice::get_erase_size(bd_addr_t addr) const
493506
{
507+
_mutex.lock();
494508
DEBUG_PRINTF("erase size: %" PRIX16 "\r\n", _block_size);
495-
496-
return _block_size;
509+
bd_size_t block_size = _block_size;
510+
_mutex.unlock();
511+
return block_size;
497512
}
498513

499514
bd_size_t DataFlashBlockDevice::size() const
500515
{
516+
_mutex.lock();
501517
DEBUG_PRINTF("device size: %" PRIX32 "\r\n", _device_size);
502-
503-
return _device_size;
518+
bd_size_t device_size = _device_size;
519+
_mutex.unlock();
520+
return device_size;
504521
}
505522

506523
/**
@@ -512,6 +529,7 @@ bd_size_t DataFlashBlockDevice::size() const
512529
*/
513530
uint16_t DataFlashBlockDevice::_get_register(uint8_t opcode)
514531
{
532+
_mutex.lock();
515533
DEBUG_PRINTF("_get_register: %" PRIX8 "\r\n", opcode);
516534

517535
/* activate device */
@@ -527,6 +545,7 @@ uint16_t DataFlashBlockDevice::_get_register(uint8_t opcode)
527545
/* deactivate device */
528546
_cs = 1;
529547

548+
_mutex.unlock();
530549
return status;
531550
}
532551

components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ class DataFlashBlockDevice : public BlockDevice {
173173
int _sync(void);
174174
int _write_page(const uint8_t *buffer, uint32_t addr, uint32_t offset, uint32_t size);
175175
uint32_t _translate_address(bd_addr_t addr);
176+
177+
// Mutex for thread safety
178+
mutable PlatformMutex _mutex;
176179
};
177180

178181

0 commit comments

Comments
 (0)