flash: add FlashIAP class

Flash IAP that provides erase/read/program to an internal API. It
invokes flash HAL functions.

FlashIAP checks for alignments for erase and program. HAL functions
do not, to avoid duplication per target implementation.
pull/3802/head
0xc0170 2017-02-18 11:07:02 -06:00 committed by Russ Butler
parent bae0f97bc9
commit 87001589c1
3 changed files with 306 additions and 0 deletions

167
drivers/FlashIAP.cpp Normal file
View File

@ -0,0 +1,167 @@
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include "FlashIAP.h"
#include "mbed_assert.h"
#ifdef DEVICE_FLASH
namespace mbed {
SingletonPtr<PlatformMutex> FlashIAP::_mutex;
static inline bool is_aligned(uint32_t number, uint32_t alignment)
{
if ((number % alignment) != 0) {
return false;
} else {
return true;
}
}
FlashIAP::FlashIAP()
{
}
FlashIAP::~FlashIAP()
{
}
int FlashIAP::init()
{
int ret = 0;
_mutex->lock();
if (flash_init(&_flash)) {
ret = -1;
}
_mutex->unlock();
return ret;
}
int FlashIAP::deinit()
{
int ret = 0;
_mutex->lock();
if (flash_free(&_flash)) {
ret = -1;
}
_mutex->unlock();
return ret;
}
int FlashIAP::read(void *buffer, uint32_t addr, uint32_t size)
{
_mutex->lock();
memcpy(buffer, (const void *)addr, size);
_mutex->unlock();
return 0;
}
int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size)
{
uint32_t page_size = get_page_size();
uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
// addr and size should be aligned to page size, and multiple of page size
// page program should not cross sector boundaries
if (!is_aligned(addr, page_size) ||
!is_aligned(size, page_size) ||
(size < page_size) ||
(((addr % current_sector_size) + size) > current_sector_size)) {
return -1;
}
int ret = 0;
_mutex->lock();
if (flash_program_page(&_flash, addr, (const uint8_t *)buffer, size)) {
ret = -1;
}
_mutex->unlock();
return ret;
}
bool FlashIAP::is_aligned_to_sector(uint32_t addr, uint32_t size)
{
uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
if (!is_aligned(size, current_sector_size) ||
!is_aligned(addr, current_sector_size)) {
return false;
} else {
return true;
}
}
int FlashIAP::erase(uint32_t addr, uint32_t size)
{
uint32_t current_sector_size = 0UL;
if (!is_aligned_to_sector(addr, size)) {
return -1;
}
int32_t ret = 0;
_mutex->lock();
while (size) {
ret = flash_erase_sector(&_flash, addr);
if (ret != 0) {
ret = -1;
break;
}
current_sector_size = flash_get_sector_size(&_flash, addr);
if (!is_aligned_to_sector(addr, size)) {
ret = -1;
break;
}
size -= current_sector_size;
addr += current_sector_size;
}
_mutex->unlock();
return ret;
}
uint32_t FlashIAP::get_page_size() const
{
return flash_get_page_size(&_flash);
}
uint32_t FlashIAP::get_sector_size(uint32_t addr) const
{
return flash_get_sector_size(&_flash, addr);
}
uint32_t FlashIAP::get_flash_start() const
{
return flash_get_start_address(&_flash);
}
uint32_t FlashIAP::get_flash_size() const
{
return flash_get_size(&_flash);
}
}
#endif

138
drivers/FlashIAP.h Normal file
View File

@ -0,0 +1,138 @@
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef MBED_FLASHIAP_H
#define MBED_FLASHIAP_H
#ifdef DEVICE_FLASH
#include "flash_api.h"
#include "platform/SingletonPtr.h"
#include "platform/PlatformMutex.h"
namespace mbed {
/** \addtogroup drivers */
/** @{*/
/** Flash IAP driver. It invokes flash HAL functions.
*
* Note Synchronization level: Thread safe
*/
class FlashIAP {
public:
FlashIAP();
~FlashIAP();
/** Initialize a flash IAP device
*
* Should be called once per lifetime of the object.
* @return 0 on success or a negative error code on failure
*/
int init();
/** Deinitialize a flash IAP device
*
* @return 0 on success or a negative error code on failure
*/
int deinit();
/** Read data from a flash device.
*
* This method invokes memcpy - reads number of bytes from the address
*
* @param buffer Buffer to write to
* @param addr Flash address to begin reading from
* @param size Size to read in bytes
* @return 0 on success, negative error code on failure
*/
int read(void *buffer, uint32_t addr, uint32_t size);
/** Program data to pages
*
* The sectors must have been erased prior to being programmed
*
* @param buffer Buffer of data to be written
* @param addr Address of a page to begin writing to, must be a multiple of program and sector sizes
* @param size Size to write in bytes, must be a multiple of program and sector sizes
* @return 0 on success, negative error code on failure
*/
int program(const void *buffer, uint32_t addr, uint32_t size);
/** Erase sectors
*
* The state of an erased sector is undefined until it has been programmed
*
* @param addr Address of a sector to begin erasing, must be a multiple of the sector size
* @param size Size to erase in bytes, must be a multiple of the sector size
* @return 0 on success, negative error code on failure
*/
int erase(uint32_t addr, uint32_t size);
/** Get the sector size at the defined address
*
* Sector size might differ at address ranges.
* An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
*
* @param addr Address of or inside the sector to query
* @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped
*/
uint32_t get_sector_size(uint32_t addr) const;
/** Get the flash start address
*
* @return Flash start address
*/
uint32_t get_flash_start() const;
/** Get the flash size
*
* @return Flash size
*/
uint32_t get_flash_size() const;
/** Get the program page size
*
* @return Size of a program page in bytes
*/
uint32_t get_page_size() const;
private:
/** Check if address and size are aligned to a sector
*
* @param addr Address of block to check for alignment
* @param size Size of block to check for alignment
* @return true if the block is sector aligned, false otherwise
*/
bool is_aligned_to_sector(uint32_t addr, uint32_t size);
flash_t _flash;
static SingletonPtr<PlatformMutex> _mutex;
};
} /* namespace mbed */
#endif /* DEVICE_FLASH */
#endif /* MBED_FLASHIAP_H */
/** @}*/

1
mbed.h
View File

@ -81,6 +81,7 @@
#include "drivers/Ethernet.h"
#include "drivers/CAN.h"
#include "drivers/RawSerial.h"
#include "drivers/FlashIAP.h"
// mbed Internal components
#include "drivers/Timer.h"