@@ -164,6 +164,7 @@ DataFlashBlockDevice::DataFlashBlockDevice(PinName mosi,
164
164
165
165
int DataFlashBlockDevice::init ()
166
166
{
167
+ _mutex.lock ();
167
168
DEBUG_PRINTF (" init\r\n " );
168
169
169
170
if (!_is_initialized) {
@@ -173,6 +174,7 @@ int DataFlashBlockDevice::init()
173
174
uint32_t val = core_util_atomic_incr_u32 (&_init_ref_count, 1 );
174
175
175
176
if (val != 1 ) {
177
+ _mutex.unlock ();
176
178
return BD_ERROR_OK;
177
179
}
178
180
@@ -281,33 +283,40 @@ int DataFlashBlockDevice::init()
281
283
_is_initialized = true ;
282
284
}
283
285
286
+ _mutex.unlock ();
284
287
return result;
285
288
}
286
289
287
290
int DataFlashBlockDevice::deinit ()
288
291
{
292
+ _mutex.lock ();
289
293
DEBUG_PRINTF (" deinit\r\n " );
290
294
291
295
if (!_is_initialized) {
292
296
_init_ref_count = 0 ;
297
+ _mutex.unlock ();
293
298
return BD_ERROR_OK;
294
299
}
295
300
296
301
uint32_t val = core_util_atomic_decr_u32 (&_init_ref_count, 1 );
297
302
298
303
if (val) {
304
+ _mutex.unlock ();
299
305
return BD_ERROR_OK;
300
306
}
301
307
302
308
_is_initialized = false ;
309
+ _mutex.unlock ();
303
310
return BD_ERROR_OK;
304
311
}
305
312
306
313
int DataFlashBlockDevice::read (void *buffer, bd_addr_t addr, bd_size_t size)
307
314
{
315
+ _mutex.lock ();
308
316
DEBUG_PRINTF (" read: %p %" PRIX64 " %" PRIX64 " \r\n " , buffer, addr, size);
309
317
310
318
if (!_is_initialized) {
319
+ _mutex.unlock ();
311
320
return BD_ERROR_DEVICE_ERROR;
312
321
}
313
322
@@ -350,6 +359,7 @@ int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
350
359
351
360
int DataFlashBlockDevice::program (const void *buffer, bd_addr_t addr, bd_size_t size)
352
361
{
362
+ _mutex.lock ();
353
363
DEBUG_PRINTF (" program: %p %" PRIX64 " %" PRIX64 " \r\n " , buffer, addr, size);
354
364
355
365
if (!_is_initialized) {
@@ -416,6 +426,7 @@ int DataFlashBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t
416
426
417
427
int DataFlashBlockDevice::erase (bd_addr_t addr, bd_size_t size)
418
428
{
429
+ _mutex.lock ();
419
430
DEBUG_PRINTF (" erase: %" PRIX64 " %" PRIX64 " \r\n " , addr, size);
420
431
421
432
if (!_is_initialized) {
@@ -484,23 +495,29 @@ bd_size_t DataFlashBlockDevice::get_program_size() const
484
495
485
496
bd_size_t DataFlashBlockDevice::get_erase_size () const
486
497
{
498
+ _mutex.lock ();
487
499
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;
490
503
}
491
504
492
505
bd_size_t DataFlashBlockDevice::get_erase_size (bd_addr_t addr) const
493
506
{
507
+ _mutex.lock ();
494
508
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;
497
512
}
498
513
499
514
bd_size_t DataFlashBlockDevice::size () const
500
515
{
516
+ _mutex.lock ();
501
517
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;
504
521
}
505
522
506
523
/* *
@@ -512,6 +529,7 @@ bd_size_t DataFlashBlockDevice::size() const
512
529
*/
513
530
uint16_t DataFlashBlockDevice::_get_register (uint8_t opcode)
514
531
{
532
+ _mutex.lock ();
515
533
DEBUG_PRINTF (" _get_register: %" PRIX8 " \r\n " , opcode);
516
534
517
535
/* activate device */
@@ -527,6 +545,7 @@ uint16_t DataFlashBlockDevice::_get_register(uint8_t opcode)
527
545
/* deactivate device */
528
546
_cs = 1 ;
529
547
548
+ _mutex.unlock ();
530
549
return status;
531
550
}
532
551
0 commit comments