Skip to content

Commit 8f1ac82

Browse files
authored
Merge pull request #3 from ARMmbed/david_init_changes
Change driver initialization behavior:
2 parents d0d47dc + d1b5c5e commit 8f1ac82

File tree

4 files changed

+74
-57
lines changed

4 files changed

+74
-57
lines changed

FlashIAPBlockDevice.cpp

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifdef DEVICE_FLASH
1818

1919
#include "FlashIAPBlockDevice.h"
20+
#include "mbed_critical.h"
2021

2122
#include "mbed.h"
2223

@@ -35,23 +36,67 @@
3536
#define DEBUG_PRINTF(...)
3637
#endif
3738

38-
FlashIAPBlockDevice::FlashIAPBlockDevice(uint32_t address, uint32_t size)
39-
: _flash(), _base(address), _size(size)
39+
FlashIAPBlockDevice::FlashIAPBlockDevice()
40+
: _flash(), _base(0), _size(0), _is_initialized(false), _init_ref_count(0)
4041
{
4142
DEBUG_PRINTF("FlashIAPBlockDevice: %" PRIX32 " %" PRIX32 "\r\n", address, size);
4243
}
4344

45+
FlashIAPBlockDevice::FlashIAPBlockDevice(uint32_t address, uint32_t)
46+
: _flash(), _base(0), _size(0), _is_initialized(false), _init_ref_count(0)
47+
{
48+
49+
}
50+
51+
FlashIAPBlockDevice::~FlashIAPBlockDevice()
52+
{
53+
deinit();
54+
}
55+
4456
int FlashIAPBlockDevice::init()
4557
{
4658
DEBUG_PRINTF("init\r\n");
4759

48-
return _flash.init();
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+
70+
int ret = _flash.init();
71+
72+
if (ret) {
73+
return ret;
74+
}
75+
76+
_base = _flash.get_flash_start();
77+
_size = _flash.get_flash_size();
78+
79+
_is_initialized = true;
80+
return ret;
4981
}
5082

5183
int FlashIAPBlockDevice::deinit()
5284
{
5385
DEBUG_PRINTF("deinit\r\n");
5486

87+
if (!_is_initialized) {
88+
_init_ref_count = 0;
89+
return 0;
90+
}
91+
92+
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
93+
94+
if (val) {
95+
return 0;
96+
}
97+
98+
_is_initialized = false;
99+
55100
return _flash.deinit();
56101
}
57102

@@ -65,7 +110,7 @@ int FlashIAPBlockDevice::read(void *buffer,
65110
int result = BD_ERROR_DEVICE_ERROR;
66111

67112
/* Check that the address and size are properly aligned and fit. */
68-
if (is_valid_read(virtual_address, size)) {
113+
if (_is_initialized && is_valid_read(virtual_address, size)) {
69114

70115
/* Convert virtual address to the physical address for the device. */
71116
bd_addr_t physical_address = _base + virtual_address;
@@ -89,7 +134,7 @@ int FlashIAPBlockDevice::program(const void *buffer,
89134
int result = BD_ERROR_DEVICE_ERROR;
90135

91136
/* Check that the address and size are properly aligned and fit. */
92-
if (is_valid_program(virtual_address, size)) {
137+
if (_is_initialized && is_valid_program(virtual_address, size)) {
93138

94139
/* Convert virtual address to the physical address for the device. */
95140
bd_addr_t physical_address = _base + virtual_address;
@@ -114,7 +159,7 @@ int FlashIAPBlockDevice::erase(bd_addr_t virtual_address,
114159
int result = BD_ERROR_DEVICE_ERROR;
115160

116161
/* Check that the address and size are properly aligned and fit. */
117-
if (is_valid_erase(virtual_address, size)) {
162+
if (_is_initialized && is_valid_erase(virtual_address, size)) {
118163

119164
/* Convert virtual address to the physical address for the device. */
120165
bd_addr_t physical_address = _base + virtual_address;
@@ -135,6 +180,10 @@ bd_size_t FlashIAPBlockDevice::get_read_size() const
135180

136181
bd_size_t FlashIAPBlockDevice::get_program_size() const
137182
{
183+
if (!_is_initialized) {
184+
return 0;
185+
}
186+
138187
uint32_t page_size = _flash.get_page_size();
139188

140189
DEBUG_PRINTF("get_program_size: %" PRIX32 "\r\n", page_size);
@@ -144,6 +193,10 @@ bd_size_t FlashIAPBlockDevice::get_program_size() const
144193

145194
bd_size_t FlashIAPBlockDevice::get_erase_size() const
146195
{
196+
if (!_is_initialized) {
197+
return 0;
198+
}
199+
147200
uint32_t erase_size = _flash.get_sector_size(_base);
148201

149202
DEBUG_PRINTF("get_erase_size: %" PRIX32 "\r\n", erase_size);
@@ -153,6 +206,10 @@ bd_size_t FlashIAPBlockDevice::get_erase_size() const
153206

154207
bd_size_t FlashIAPBlockDevice::get_erase_size(bd_addr_t addr) const
155208
{
209+
if (!_is_initialized) {
210+
return 0;
211+
}
212+
156213
uint32_t erase_size = _flash.get_sector_size(_base + addr);
157214

158215
DEBUG_PRINTF("get_erase_size: %" PRIX32 "\r\n", erase_size);

FlashIAPBlockDevice.h

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@
2222
#include "BlockDevice.h"
2323
#include <mbed.h>
2424

25-
#ifndef MBED_CONF_FLASHIAP_BLOCK_DEVICE_BASE_ADDRESS
26-
#error flashiap-block-device.base-address not set
27-
#endif
28-
29-
#ifndef MBED_CONF_FLASHIAP_BLOCK_DEVICE_SIZE
30-
#error flashiap-block-device.size not set
31-
#endif
32-
3325
/** BlockDevice using the FlashIAP API
3426
*
3527
* @code
@@ -39,14 +31,13 @@
3931
*/
4032
class FlashIAPBlockDevice : public BlockDevice {
4133
public:
42-
/** Creates a FlashIAPBlockDevice, starting at the given address and
43-
* with a certain size.
44-
*
45-
* @param address Starting address for the BlockDevice
46-
* @param size Maximum size allocated to this BlockDevice
47-
*/
48-
FlashIAPBlockDevice(uint32_t address = MBED_CONF_FLASHIAP_BLOCK_DEVICE_BASE_ADDRESS,
49-
uint32_t size = MBED_CONF_FLASHIAP_BLOCK_DEVICE_SIZE);
34+
/** Creates a FlashIAPBlockDevice **/
35+
FlashIAPBlockDevice();
36+
37+
MBED_DEPRECATED("Please use default constructor instead")
38+
FlashIAPBlockDevice(uint32_t address, uint32_t size = 0);
39+
40+
virtual ~FlashIAPBlockDevice();
5041

5142
/** Initialize a block device
5243
*
@@ -129,6 +120,8 @@ class FlashIAPBlockDevice : public BlockDevice {
129120
FlashIAP _flash;
130121
bd_addr_t _base;
131122
bd_size_t _size;
123+
bool _is_initialized;
124+
uint32_t _init_ref_count;
132125
};
133126

134127
#endif /* DEVICE_FLASH */

README.md

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,13 @@ This driver is **EXPERIMENTAL** and improper usage could kill your board's flash
66
This driver should only be used on platforms where the FlashIAP implementation is using external flash or in conjunction with a filesystem with wear leveling, that can operate on a page size granularity.
77

88
Additional concerns:
9-
- The BlockDevice API assumes a uniform erase size so the underlying flash must also have uniform sectors.
109
- The FlashIAP may freeze code execution for a long period of time while writing to flash. Not even high-priority irqs will be allowed to run, which may interrupt background processes.
1110

1211

1312
## Configuration
14-
15-
The driver must be configured with the starting address for the internal flash area reserved for the block device and it's size in bytes. In the application's `mbed_app.json`, add the following lines:
16-
17-
```json
18-
{
19-
"target_overrides": {
20-
"K64F": {
21-
"flashiap-block-device.base-address": "<internal flash address where the block device starts>",
22-
"flashiap-block-device.size": "<number of bytes allocated to the internal flash block device>"
23-
}
24-
}
25-
}
26-
```
27-
13+
None.
2814

2915
## Tested on
3016

31-
* Realtek RTL8195AM
17+
* K82F
3218
* K64F

mbed_lib.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)