mbed-os/targets/TARGET_TOSHIBA/TARGET_TMPM46B/flash_api.c

175 lines
4.9 KiB
C
Raw Normal View History

2018-01-22 12:16:19 +00:00
/* mbed Microcontroller Library
* (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "flash_api.h"
#include "mbed_critical.h"
#include "tmpm46b_fc.h"
2019-07-29 12:11:32 +00:00
#define PROGRAM_WIRTE_MAX 16U // Page program could be written 16 bytes/4 words once
#define SECTOR_SIZE 0x8000 // (512 * 8) sectors
#define PAGE_SIZE 16U // Page program size is 16 bytes
2018-01-22 12:16:19 +00:00
2019-07-29 12:11:32 +00:00
#if defined ( __ICCARM__ ) // IAR Compiler
2018-01-22 12:16:19 +00:00
#define FLASH_API_ROM ((uint32_t *)__section_begin("FLASH_CODE_ROM"))
#define SIZE_FLASH_API ((uint32_t)__section_size("FLASH_CODE_ROM"))
#define FLASH_API_RAM ((uint32_t *)__section_begin("FLASH_CODE_RAM"))
#pragma section = "FLASH_CODE_RAM"
#pragma section = "FLASH_CODE_ROM"
#endif
2019-07-29 12:11:32 +00:00
#if defined ( __ICCARM__ ) // IAR Compiler
static void Copy_Routine(uint32_t *dest, uint32_t *source, uint32_t size)
2018-01-22 12:16:19 +00:00
{
uint32_t *dest_addr, *source_addr, tmpsize;
uint32_t i, tmps, tmpd, mask;
2019-07-29 12:11:32 +00:00
dest_addr = dest;
2018-01-22 12:16:19 +00:00
source_addr = source;
tmpsize = size >> 2U;
2019-07-29 12:11:32 +00:00
for (i = 0U; i < tmpsize; i++) { // 32bits copy
2018-01-22 12:16:19 +00:00
*dest_addr = *source_addr;
dest_addr++;
source_addr++;
}
2019-07-29 12:11:32 +00:00
if (size & 0x00000003U) { // if the last data size is not 0(maybe 1,2 or 3), copy the last data
2018-01-22 12:16:19 +00:00
mask = 0xFFFFFF00U;
i = size & 0x00000003U;
tmps = *source_addr;
tmpd = *dest_addr;
while (i - 1U) {
mask = mask << 8U;
i--;
}
tmps = tmps & (~mask);
tmpd = tmpd & (mask);
2019-07-29 12:11:32 +00:00
*dest_addr = tmps + tmpd; // 32bits copy, but only change the bytes need to be changed
2018-01-22 12:16:19 +00:00
} else {
2019-07-29 12:11:32 +00:00
// Do nothing
2018-01-22 12:16:19 +00:00
}
}
#endif
int32_t flash_init(flash_t *obj)
{
TSB_FC->WCLKCR = 0x00000004U;
TSB_FC->PROGCR = 0x00000001U;
TSB_FC->ERASECR = 0x00000007U;
TSB_FC->AREASEL = 0x00000000U;
2019-07-29 12:11:32 +00:00
#if defined ( __ICCARM__ ) // IAR Compiler
2018-01-22 12:16:19 +00:00
Copy_Routine(FLASH_API_RAM, FLASH_API_ROM, SIZE_FLASH_API);
#endif
return 0;
}
int32_t flash_free(flash_t *obj)
{
return 0;
}
2019-07-29 12:11:32 +00:00
#if defined ( __ICCARM__ ) // IAR Compiler
2018-01-22 12:16:19 +00:00
#pragma location = "FLASH_ROM"
#endif
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
{
int status = FAIL;
2019-07-29 12:11:32 +00:00
// We need to prevent flash accesses during erase operation
2018-01-22 12:16:19 +00:00
core_util_critical_section_enter();
if (FC_SUCCESS == FC_EraseBlock(address)) {
status = SUCCESS;
} else {
2019-07-29 12:11:32 +00:00
// Do nothing
2018-01-22 12:16:19 +00:00
}
core_util_critical_section_exit();
return status;
}
2019-07-29 12:11:32 +00:00
#if defined ( __ICCARM__ ) // IAR Compiler
2018-01-22 12:16:19 +00:00
#pragma location = "FLASH_ROM"
#endif
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
{
int status = SUCCESS;
2019-07-29 12:11:32 +00:00
// We need to prevent flash accesses during program operation
2018-01-22 12:16:19 +00:00
core_util_critical_section_enter();
while (size > PROGRAM_WIRTE_MAX) {
2019-07-29 12:11:32 +00:00
if (FC_SUCCESS == FC_WritePage((uint32_t)address, (uint32_t *)data)) { // write one page every time
// Do nothing
2018-01-22 12:16:19 +00:00
} else {
status = FAIL;
2018-01-22 12:16:19 +00:00
break;
}
size = size - PROGRAM_WIRTE_MAX;
address = address + PROGRAM_WIRTE_MAX;
data = data + PROGRAM_WIRTE_MAX;
}
2019-07-29 12:11:32 +00:00
if (FC_SUCCESS == FC_WritePage((uint32_t)address, (uint32_t *)data)) { // write the last data, no more than one page
// Do nothing
2018-01-22 12:16:19 +00:00
} else {
status = FAIL;
2018-01-22 12:16:19 +00:00
}
core_util_critical_section_exit();
return status;
}
2019-07-29 12:11:32 +00:00
#if defined ( __ICCARM__ ) // IAR Compiler
2018-01-22 12:16:19 +00:00
#pragma location = "FLASH_ROM"
#endif
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
{
if (address >= FLASH_CHIP_SIZE)
return MBED_FLASH_INVALID_SIZE;
return SECTOR_SIZE;
}
2019-07-29 12:11:32 +00:00
#if defined ( __ICCARM__ ) // IAR Compiler
2018-01-22 12:16:19 +00:00
#pragma location = "FLASH_ROM"
#endif
uint32_t flash_get_page_size(const flash_t *obj)
{
return PAGE_SIZE;
2018-01-22 12:16:19 +00:00
}
2019-07-29 12:11:32 +00:00
#if defined ( __ICCARM__ ) // IAR Compiler
2018-01-22 12:16:19 +00:00
#pragma location = "FLASH_ROM"
#endif
uint32_t flash_get_start_address(const flash_t *obj)
{
return FLASH_START_ADDR;
}
2019-07-29 12:11:32 +00:00
#if defined ( __ICCARM__ ) // IAR Compiler
2018-01-22 12:16:19 +00:00
#pragma location = "FLASH_ROM"
#endif
uint32_t flash_get_size(const flash_t *obj)
{
return FLASH_CHIP_SIZE;
}
2019-07-29 12:11:32 +00:00
#if defined ( __ICCARM__ ) // IAR Compiler
#pragma location = "FLASH_ROM"
#endif
uint8_t flash_get_erase_value(const flash_t *obj)
{
(void)obj;
return 0xFF;
}