|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, Freescale Semiconductor, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without modification, |
| 6 | + * are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * o Redistributions of source code must retain the above copyright notice, this list |
| 9 | + * of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * o Redistributions in binary form must reproduce the above copyright notice, this |
| 12 | + * list of conditions and the following disclaimer in the documentation and/or |
| 13 | + * other materials provided with the distribution. |
| 14 | + * |
| 15 | + * o Neither the name of Freescale Semiconductor, Inc. nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from this |
| 17 | + * software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +#include "flash_api.h" |
| 32 | +#include "mbed_critical.h" |
| 33 | + |
| 34 | +#if DEVICE_FLASH |
| 35 | + |
| 36 | +#include "fsl_flash.h" |
| 37 | + |
| 38 | +int32_t flash_init(flash_t *obj) |
| 39 | +{ |
| 40 | + status_t result; |
| 41 | + |
| 42 | + /* Clean up Flash driver Structure*/ |
| 43 | + memset(&obj->flash_config, 0, sizeof(flash_config_t)); |
| 44 | + |
| 45 | + /* Setup flash driver structure for device and initialize variables. */ |
| 46 | + result = FLASH_Init(&obj->flash_config); |
| 47 | + if (kStatus_FLASH_Success != result) { |
| 48 | + return -1; |
| 49 | + } else { |
| 50 | + return 0; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +int32_t flash_free(flash_t *obj) |
| 55 | +{ |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +int32_t flash_erase_sector(flash_t *obj, uint32_t address) |
| 60 | +{ |
| 61 | + int status; |
| 62 | + |
| 63 | + /* We need to prevent flash accesses during erase operation */ |
| 64 | + core_util_critical_section_enter(); |
| 65 | + status = FLASH_Erase(&obj->flash_config, address, obj->flash_config.PFlashSectorSize, kFLASH_ApiEraseKey); |
| 66 | + |
| 67 | + if (status == kStatus_Success) { |
| 68 | + status = FLASH_VerifyErase(&obj->flash_config, address, obj->flash_config.PFlashSectorSize, kFLASH_MarginValueNormal); |
| 69 | + } |
| 70 | + core_util_critical_section_exit(); |
| 71 | + |
| 72 | + if (status == kStatus_Success) { |
| 73 | + return 0; |
| 74 | + } else { |
| 75 | + return -1; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size) |
| 80 | +{ |
| 81 | + int status; |
| 82 | + |
| 83 | + /* We need to prevent flash accesses during program operation */ |
| 84 | + core_util_critical_section_enter(); |
| 85 | + status = FLASH_Program(&obj->flash_config, address, (uint32_t *)data, size); |
| 86 | + |
| 87 | + if (status == kStatus_Success) { |
| 88 | + // Must use kFlashMargin_User, or kFlashMargin_Factory for verify program |
| 89 | + status = FLASH_VerifyProgram(&obj->flash_config, address, size, |
| 90 | + (uint32_t *)data, kFLASH_MarginValueUser, |
| 91 | + NULL, NULL); |
| 92 | + } |
| 93 | + core_util_critical_section_exit(); |
| 94 | + |
| 95 | + return status; |
| 96 | + |
| 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 = 0; |
| 103 | + uint32_t startaddr = 0; |
| 104 | + |
| 105 | + FLASH_GetProperty((flash_config_t *)&obj->flash_config, kFLASH_PropertyPflashBlockBaseAddr, &startaddr); |
| 106 | + FLASH_GetProperty((flash_config_t *)&obj->flash_config, kFLASH_PropertyPflashTotalSize, &devicesize); |
| 107 | + |
| 108 | + if ((address >= startaddr) && (address < (startaddr + devicesize))) { |
| 109 | + FLASH_GetProperty((flash_config_t *)&obj->flash_config, kFLASH_PropertyPflashSectorSize, §orsize); |
| 110 | + } |
| 111 | + |
| 112 | + return sectorsize; |
| 113 | +} |
| 114 | + |
| 115 | +uint32_t flash_get_page_size(const flash_t *obj) |
| 116 | +{ |
| 117 | + return FSL_FEATURE_FLASH_PFLASH_BLOCK_WRITE_UNIT_SIZE; |
| 118 | +} |
| 119 | + |
| 120 | +uint32_t flash_get_start_address(const flash_t *obj) |
| 121 | +{ |
| 122 | + uint32_t startaddr = 0; |
| 123 | + |
| 124 | + FLASH_GetProperty((flash_config_t *)&obj->flash_config, kFLASH_PropertyPflashBlockBaseAddr, &startaddr); |
| 125 | + |
| 126 | + return startaddr; |
| 127 | +} |
| 128 | + |
| 129 | +uint32_t flash_get_size(const flash_t *obj) |
| 130 | +{ |
| 131 | + uint32_t devicesize = 0; |
| 132 | + |
| 133 | + FLASH_GetProperty((flash_config_t *)&obj->flash_config, kFLASH_PropertyPflashTotalSize, &devicesize); |
| 134 | + |
| 135 | + return devicesize; |
| 136 | +} |
| 137 | + |
| 138 | +#endif |
0 commit comments