|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2018 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 | +#include "flash_api.h" |
| 17 | +#include "mbed_critical.h" |
| 18 | + |
| 19 | +#if DEVICE_FLASH |
| 20 | + |
| 21 | +#include "fsl_flashiap.h" |
| 22 | + |
| 23 | +int32_t flash_init(flash_t *obj) |
| 24 | +{ |
| 25 | + return 0; |
| 26 | +} |
| 27 | + |
| 28 | +int32_t flash_free(flash_t *obj) |
| 29 | +{ |
| 30 | + return 0; |
| 31 | +} |
| 32 | + |
| 33 | +int32_t flash_erase_sector(flash_t *obj, uint32_t address) |
| 34 | +{ |
| 35 | + uint32_t n; |
| 36 | + uint32_t status; |
| 37 | + int32_t ret = -1; |
| 38 | + |
| 39 | + /* We need to prevent flash accesses during erase operation */ |
| 40 | + core_util_critical_section_enter(); |
| 41 | + |
| 42 | + n = address / FSL_FEATURE_SYSCON_FLASH_SECTOR_SIZE_BYTES; // Get Sector Number |
| 43 | + |
| 44 | + status = FLASHIAP_PrepareSectorForWrite(n, n); |
| 45 | + if (status == kStatus_FLASHIAP_Success) { |
| 46 | + status = FLASHIAP_EraseSector(n, n, SystemCoreClock); |
| 47 | + if (status == kStatus_FLASHIAP_Success) { |
| 48 | + ret = 0; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + core_util_critical_section_exit(); |
| 53 | + |
| 54 | + return ret; |
| 55 | +} |
| 56 | + |
| 57 | +int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size) |
| 58 | +{ |
| 59 | + uint32_t n; |
| 60 | + uint32_t sector_number; |
| 61 | + |
| 62 | + uint32_t status; |
| 63 | + int32_t ret = -1; |
| 64 | + uint8_t buf[FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES]; |
| 65 | + |
| 66 | + if (address == 0) { // Check for Vector Table |
| 67 | + n = *((unsigned long *)(data + 0)) + |
| 68 | + *((unsigned long *)(data + 1)) + |
| 69 | + *((unsigned long *)(data + 2)) + |
| 70 | + *((unsigned long *)(data + 3)) + |
| 71 | + *((unsigned long *)(data + 4)) + |
| 72 | + *((unsigned long *)(data + 5)) + |
| 73 | + *((unsigned long *)(data + 6)); |
| 74 | + *((unsigned long *)(data + 7)) = 0 - n; // Signature at Reserved Vector |
| 75 | + } |
| 76 | + |
| 77 | + /* Copy into a local buffer to ensure address is word-aligned */ |
| 78 | + memcpy(&buf, data, FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES); |
| 79 | + |
| 80 | + /* We need to prevent flash accesses during program operation */ |
| 81 | + core_util_critical_section_enter(); |
| 82 | + |
| 83 | + sector_number = address / FSL_FEATURE_SYSCON_FLASH_SECTOR_SIZE_BYTES; // Get Sector Number |
| 84 | + |
| 85 | + status = FLASHIAP_PrepareSectorForWrite(sector_number, sector_number); |
| 86 | + if (status == kStatus_FLASHIAP_Success) { |
| 87 | + status = FLASHIAP_CopyRamToFlash(address, (uint32_t *)&buf, |
| 88 | + FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES, SystemCoreClock); |
| 89 | + if (status == kStatus_FLASHIAP_Success) { |
| 90 | + ret = 0; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + core_util_critical_section_exit(); |
| 95 | + |
| 96 | + return ret; |
| 97 | +} |
| 98 | + |
| 99 | +uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) |
| 100 | +{ |
| 101 | + uint32_t sectorsize = MBED_FLASH_INVALID_SIZE; |
| 102 | + uint32_t devicesize = FSL_FEATURE_SYSCON_FLASH_SIZE_BYTES; |
| 103 | + uint32_t startaddr = 0; |
| 104 | + |
| 105 | + if ((address >= startaddr) && (address < (startaddr + devicesize))) { |
| 106 | + sectorsize = FSL_FEATURE_SYSCON_FLASH_SECTOR_SIZE_BYTES; |
| 107 | + } |
| 108 | + |
| 109 | + return sectorsize; |
| 110 | +} |
| 111 | + |
| 112 | +uint32_t flash_get_page_size(const flash_t *obj) |
| 113 | +{ |
| 114 | + return FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES; |
| 115 | +} |
| 116 | + |
| 117 | +uint32_t flash_get_start_address(const flash_t *obj) |
| 118 | +{ |
| 119 | + uint32_t startaddr = 0; |
| 120 | + |
| 121 | + return startaddr; |
| 122 | +} |
| 123 | + |
| 124 | +uint32_t flash_get_size(const flash_t *obj) |
| 125 | +{ |
| 126 | + return FSL_FEATURE_SYSCON_FLASH_SIZE_BYTES; |
| 127 | +} |
| 128 | + |
| 129 | +#endif |
0 commit comments