Skip to content

Commit 881929e

Browse files
Yossi LevyYossi Levy
authored andcommitted
Add 'components/storage/blockdevice/COMPONENT_FLASHIAP/' from commit '8f1ac821f1c411986b8533cbf4878ea9b3fe5efb'
git-subtree-dir: components/storage/blockdevice/COMPONENT_FLASHIAP git-subtree-mainline: 9a0844a git-subtree-split: 8f1ac82
2 parents 9a0844a + 8f1ac82 commit 881929e

File tree

11 files changed

+3366
-0
lines changed

11 files changed

+3366
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
util/*
2+
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifdef DEVICE_FLASH
18+
19+
#include "FlashIAPBlockDevice.h"
20+
#include "mbed_critical.h"
21+
22+
#include "mbed.h"
23+
24+
#include <inttypes.h>
25+
26+
#define FLASHIAP_READ_SIZE 1
27+
28+
// Debug available
29+
#ifndef FLASHIAP_DEBUG
30+
#define FLASHIAP_DEBUG 0
31+
#endif
32+
33+
#if FLASHIAP_DEBUG
34+
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
35+
#else
36+
#define DEBUG_PRINTF(...)
37+
#endif
38+
39+
FlashIAPBlockDevice::FlashIAPBlockDevice()
40+
: _flash(), _base(0), _size(0), _is_initialized(false), _init_ref_count(0)
41+
{
42+
DEBUG_PRINTF("FlashIAPBlockDevice: %" PRIX32 " %" PRIX32 "\r\n", address, size);
43+
}
44+
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+
56+
int FlashIAPBlockDevice::init()
57+
{
58+
DEBUG_PRINTF("init\r\n");
59+
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;
81+
}
82+
83+
int FlashIAPBlockDevice::deinit()
84+
{
85+
DEBUG_PRINTF("deinit\r\n");
86+
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+
100+
return _flash.deinit();
101+
}
102+
103+
int FlashIAPBlockDevice::read(void *buffer,
104+
bd_addr_t virtual_address,
105+
bd_size_t size)
106+
{
107+
DEBUG_PRINTF("read: %" PRIX64 " %" PRIX64 "\r\n", virtual_address, size);
108+
109+
/* Default to error return code; success must be set explicitly. */
110+
int result = BD_ERROR_DEVICE_ERROR;
111+
112+
/* Check that the address and size are properly aligned and fit. */
113+
if (_is_initialized && is_valid_read(virtual_address, size)) {
114+
115+
/* Convert virtual address to the physical address for the device. */
116+
bd_addr_t physical_address = _base + virtual_address;
117+
118+
/* Read data using the internal flash driver. */
119+
result = _flash.read(buffer, physical_address, size);
120+
121+
DEBUG_PRINTF("physical: %" PRIX64 "\r\n", physical_address);
122+
}
123+
124+
return result;
125+
}
126+
127+
int FlashIAPBlockDevice::program(const void *buffer,
128+
bd_addr_t virtual_address,
129+
bd_size_t size)
130+
{
131+
DEBUG_PRINTF("program: %" PRIX64 " %" PRIX64 "\r\n", virtual_address, size);
132+
133+
/* Default to error return code; success must be set explicitly. */
134+
int result = BD_ERROR_DEVICE_ERROR;
135+
136+
/* Check that the address and size are properly aligned and fit. */
137+
if (_is_initialized && is_valid_program(virtual_address, size)) {
138+
139+
/* Convert virtual address to the physical address for the device. */
140+
bd_addr_t physical_address = _base + virtual_address;
141+
142+
/* Write data using the internal flash driver. */
143+
result = _flash.program(buffer, physical_address, size);
144+
145+
DEBUG_PRINTF("physical: %" PRIX64 " %" PRIX64 "\r\n",
146+
physical_address,
147+
size);
148+
}
149+
150+
return result;
151+
}
152+
153+
int FlashIAPBlockDevice::erase(bd_addr_t virtual_address,
154+
bd_size_t size)
155+
{
156+
DEBUG_PRINTF("erase: %" PRIX64 " %" PRIX64 "\r\n", virtual_address, size);
157+
158+
/* Default to error return code; success must be set explicitly. */
159+
int result = BD_ERROR_DEVICE_ERROR;
160+
161+
/* Check that the address and size are properly aligned and fit. */
162+
if (_is_initialized && is_valid_erase(virtual_address, size)) {
163+
164+
/* Convert virtual address to the physical address for the device. */
165+
bd_addr_t physical_address = _base + virtual_address;
166+
167+
/* Erase sector */
168+
result = _flash.erase(physical_address, size);
169+
}
170+
171+
return result;
172+
}
173+
174+
bd_size_t FlashIAPBlockDevice::get_read_size() const
175+
{
176+
DEBUG_PRINTF("get_read_size: %d\r\n", FLASHIAP_READ_SIZE);
177+
178+
return FLASHIAP_READ_SIZE;
179+
}
180+
181+
bd_size_t FlashIAPBlockDevice::get_program_size() const
182+
{
183+
if (!_is_initialized) {
184+
return 0;
185+
}
186+
187+
uint32_t page_size = _flash.get_page_size();
188+
189+
DEBUG_PRINTF("get_program_size: %" PRIX32 "\r\n", page_size);
190+
191+
return page_size;
192+
}
193+
194+
bd_size_t FlashIAPBlockDevice::get_erase_size() const
195+
{
196+
if (!_is_initialized) {
197+
return 0;
198+
}
199+
200+
uint32_t erase_size = _flash.get_sector_size(_base);
201+
202+
DEBUG_PRINTF("get_erase_size: %" PRIX32 "\r\n", erase_size);
203+
204+
return erase_size;
205+
}
206+
207+
bd_size_t FlashIAPBlockDevice::get_erase_size(bd_addr_t addr) const
208+
{
209+
if (!_is_initialized) {
210+
return 0;
211+
}
212+
213+
uint32_t erase_size = _flash.get_sector_size(_base + addr);
214+
215+
DEBUG_PRINTF("get_erase_size: %" PRIX32 "\r\n", erase_size);
216+
217+
return erase_size;
218+
}
219+
220+
bd_size_t FlashIAPBlockDevice::size() const
221+
{
222+
DEBUG_PRINTF("size: %" PRIX64 "\r\n", _size);
223+
224+
return _size;
225+
}
226+
227+
#endif /* DEVICE_FLASH */
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MBED_FLASHIAP_BLOCK_DEVICE_H
18+
#define MBED_FLASHIAP_BLOCK_DEVICE_H
19+
20+
#ifdef DEVICE_FLASH
21+
22+
#include "BlockDevice.h"
23+
#include <mbed.h>
24+
25+
/** BlockDevice using the FlashIAP API
26+
*
27+
* @code
28+
* #include "mbed.h"
29+
* #include "FlashIAPBlockDevice.h"
30+
*
31+
*/
32+
class FlashIAPBlockDevice : public BlockDevice {
33+
public:
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();
41+
42+
/** Initialize a block device
43+
*
44+
* @return 0 on success or a negative error code on failure
45+
*/
46+
virtual int init();
47+
48+
/** Deinitialize a block device
49+
*
50+
* @return 0 on success or a negative error code on failure
51+
*/
52+
virtual int deinit();
53+
54+
/** Read blocks from a block device
55+
*
56+
* @param buffer Buffer to write blocks to
57+
* @param addr Address of block to begin reading from
58+
* @param size Size to read in bytes, must be a multiple of read block size
59+
* @return 0 on success, negative error code on failure
60+
*/
61+
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
62+
63+
/** Program blocks to a block device
64+
*
65+
* The blocks must have been erased prior to being programmed
66+
*
67+
* @param buffer Buffer of data to write to blocks
68+
* @param addr Address of block to begin writing to
69+
* @param size Size to write in bytes, must be a multiple of program block size
70+
* @return 0 on success, negative error code on failure
71+
*/
72+
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
73+
74+
/** Erase blocks on a block device
75+
*
76+
* The state of an erased block is undefined until it has been programmed
77+
*
78+
* @param addr Address of block to begin erasing
79+
* @param size Size to erase in bytes, must be a multiple of erase block size
80+
* @return 0 on success, negative error code on failure
81+
*/
82+
virtual int erase(bd_addr_t addr, bd_size_t size);
83+
84+
/** Get the size of a readable block
85+
*
86+
* @return Size of a readable block in bytes
87+
*/
88+
virtual bd_size_t get_read_size() const;
89+
90+
/** Get the size of a programable block
91+
*
92+
* @return Size of a programable block in bytes
93+
* @note Must be a multiple of the read size
94+
*/
95+
virtual bd_size_t get_program_size() const;
96+
97+
/** Get the size of a eraseable block
98+
*
99+
* @return Size of a eraseable block in bytes
100+
* @note Must be a multiple of the program size
101+
*/
102+
virtual bd_size_t get_erase_size() const;
103+
104+
/** Get the size of an erasable block given address
105+
*
106+
* @param addr Address within the erasable block
107+
* @return Size of an erasable block in bytes
108+
* @note Must be a multiple of the program size
109+
*/
110+
virtual bd_size_t get_erase_size(bd_addr_t addr) const;
111+
112+
/** Get the total size of the underlying device
113+
*
114+
* @return Size of the underlying device in bytes
115+
*/
116+
virtual bd_size_t size() const;
117+
118+
private:
119+
// Device configuration
120+
FlashIAP _flash;
121+
bd_addr_t _base;
122+
bd_size_t _size;
123+
bool _is_initialized;
124+
uint32_t _init_ref_count;
125+
};
126+
127+
#endif /* DEVICE_FLASH */
128+
#endif /* MBED_FLASHIAP_BLOCK_DEVICE_H */

0 commit comments

Comments
 (0)