Skip to content

Commit abca2ca

Browse files
author
Marcus Chang
committed
FlashIAP: Add explicit read function to flash_api.h
On some platforms, the in-application memory is not memory mapped and therefore cannot be accessed using memcpy. The flash_read function added to flash_api.h (with a weak implementation using memcpy in mbed_flash_api.c) can be used for reading data from areas that are not memory mapped.
1 parent 1af8aef commit abca2ca

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

drivers/FlashIAP.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int FlashIAP::init()
5555
int ret = 0;
5656
_mutex->lock();
5757
if (flash_init(&_flash)) {
58-
ret = -1;
58+
ret = -1;
5959
}
6060
_mutex->unlock();
6161
return ret;
@@ -66,7 +66,7 @@ int FlashIAP::deinit()
6666
int ret = 0;
6767
_mutex->lock();
6868
if (flash_free(&_flash)) {
69-
ret = -1;
69+
ret = -1;
7070
}
7171
_mutex->unlock();
7272
return ret;
@@ -75,10 +75,11 @@ int FlashIAP::deinit()
7575

7676
int FlashIAP::read(void *buffer, uint32_t addr, uint32_t size)
7777
{
78+
int32_t ret = -1;
7879
_mutex->lock();
79-
memcpy(buffer, (const void *)addr, size);
80+
ret = flash_read(&_flash, addr, (uint8_t *) buffer, size);
8081
_mutex->unlock();
81-
return 0;
82+
return ret;
8283
}
8384

8485
int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size)

hal/flash_api.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ extern "C" {
4242
*/
4343

4444
/** Initialize the flash peripheral and the flash_t object
45-
*
45+
*
4646
* @param obj The flash object
4747
* @return 0 for success, -1 for error
4848
*/
4949
int32_t flash_init(flash_t *obj);
5050

5151
/** Uninitialize the flash peripheral and the flash_t object
52-
*
52+
*
5353
* @param obj The flash object
5454
* @return 0 for success, -1 for error
5555
*/
@@ -64,9 +64,20 @@ int32_t flash_free(flash_t *obj);
6464
*/
6565
int32_t flash_erase_sector(flash_t *obj, uint32_t address);
6666

67+
/** Read data starting at defined address
68+
*
69+
* This function has a WEAK implementation using memcpy for backwards compatibility.
70+
* @param obj The flash object
71+
* @param address Address to begin reading from
72+
* @param data The buffer to read data into
73+
* @param size The number of bytes to read
74+
* @return 0 for success, -1 for error
75+
*/
76+
int32_t flash_read(flash_t *obj, uint32_t address, uint8_t *data, uint32_t size);
77+
6778
/** Program one page starting at defined address
68-
*
69-
* The page should be at page boundary, should not cross multiple sectors.
79+
*
80+
* The page should be at page boundary, should not cross multiple sectors.
7081
* This function does not do any check for address alignments or if size is aligned to a page size.
7182
* @param obj The flash object
7283
* @param address The sector starting address
@@ -77,30 +88,30 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address);
7788
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size);
7889

7990
/** Get sector size
80-
*
91+
*
8192
* @param obj The flash object
8293
* @param address The sector starting address
8394
* @return The size of a sector
8495
*/
8596
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address);
8697

8798
/** Get page size
88-
*
99+
*
89100
* @param obj The flash object
90101
* @param address The page starting address
91102
* @return The size of a page
92103
*/
93104
uint32_t flash_get_page_size(const flash_t *obj);
94105

95106
/** Get start address for the flash region
96-
*
107+
*
97108
* @param obj The flash object
98109
* @return The start address for the flash region
99110
*/
100111
uint32_t flash_get_start_address(const flash_t *obj);
101112

102113
/** Get the flash region size
103-
*
114+
*
104115
* @param obj The flash object
105116
* @return The flash region size
106117
*/

hal/mbed_flash_api.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 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+
#include "hal/flash_api.h"
18+
19+
#if DEVICE_FLASH
20+
21+
#include "platform/mbed_toolchain.h"
22+
#include <string.h>
23+
24+
MBED_WEAK int32_t flash_read(flash_t *obj, uint32_t address, uint8_t *data, uint32_t size)
25+
{
26+
memcpy(data, (const void *)address, size);
27+
return 0;
28+
}
29+
30+
#endif

0 commit comments

Comments
 (0)