|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included |
| 12 | + * in all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 | + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
| 18 | + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + * |
| 22 | + * Except as contained in this notice, the name of Maxim Integrated |
| 23 | + * Products, Inc. shall not be used except as stated in the Maxim Integrated |
| 24 | + * Products, Inc. Branding Policy. |
| 25 | + * |
| 26 | + * The mere transfer of this software does not imply any licenses |
| 27 | + * of trade secrets, proprietary technology, copyrights, patents, |
| 28 | + * trademarks, maskwork rights, or any other form of intellectual |
| 29 | + * property whatsoever. Maxim Integrated Products, Inc. retains all |
| 30 | + * ownership rights. |
| 31 | + ******************************************************************************* |
| 32 | + */ |
| 33 | + |
| 34 | +#if DEVICE_FLASH |
| 35 | +#include "flash_api.h" |
| 36 | +#include "mbed_critical.h" |
| 37 | +#include "cmsis.h" |
| 38 | +#include "flc.h" |
| 39 | + |
| 40 | + |
| 41 | +/** |
| 42 | + * * \defgroup flash_hal Flash HAL API |
| 43 | + * * @{ |
| 44 | + * */ |
| 45 | + |
| 46 | +/** Initialize the flash peripheral and the flash_t object |
| 47 | + * * |
| 48 | + * * @param obj The flash object |
| 49 | + * * @return 0 for success, -1 for error |
| 50 | + * */ |
| 51 | +int32_t flash_init(flash_t *obj) |
| 52 | +{ |
| 53 | + return FLC_Init(); |
| 54 | +} |
| 55 | + |
| 56 | +/** Uninitialize the flash peripheral and the flash_t object |
| 57 | + * * |
| 58 | + * * @param obj The flash object |
| 59 | + * * @return 0 for success, -1 for error |
| 60 | + * */ |
| 61 | +int32_t flash_free(flash_t *obj) |
| 62 | +{ |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +/** Erase one sector starting at defined address |
| 67 | + * * |
| 68 | + * * The address should be at sector boundary. This function does not do any check for address alignments |
| 69 | + * * @param obj The flash object |
| 70 | + * * @param address The sector starting address |
| 71 | + * * @return 0 for success, -1 for error |
| 72 | + * */ |
| 73 | +int32_t flash_erase_sector(flash_t *obj, uint32_t address) |
| 74 | +{ |
| 75 | + if(FLC_PageErase(address, MXC_V_FLC_ERASE_CODE_PAGE_ERASE, MXC_V_FLC_FLSH_UNLOCK_KEY) != 0) |
| 76 | + { |
| 77 | + return -1; |
| 78 | + } else { |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +/** Program pages starting at defined address |
| 85 | + * * |
| 86 | + * * The pages should not cross multiple sectors. |
| 87 | + * * This function does not do any check for address alignments or if size is aligned to a page size. |
| 88 | + * * @param obj The flash object |
| 89 | + * * @param address The sector starting address |
| 90 | + * * @param data The data buffer to be programmed |
| 91 | + * * @param size The number of bytes to program |
| 92 | + * * @return 0 for success, -1 for error |
| 93 | + * */ |
| 94 | +int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size) |
| 95 | +{ |
| 96 | + int32_t status = E_BUSY; |
| 97 | + |
| 98 | + while ( status == E_BUSY ) |
| 99 | + { |
| 100 | + status = FLC_Write(address, data, size, MXC_V_FLC_FLSH_UNLOCK_KEY); |
| 101 | + } |
| 102 | + |
| 103 | + if (status != 0) |
| 104 | + { |
| 105 | + return -1; |
| 106 | + } else { |
| 107 | + return 0; |
| 108 | + } |
| 109 | + |
| 110 | +} |
| 111 | + |
| 112 | +/** Get sector size |
| 113 | + * * |
| 114 | + * * @param obj The flash object |
| 115 | + * * @param address The sector starting address |
| 116 | + * * @return The size of a sector |
| 117 | + * */ |
| 118 | +uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) |
| 119 | +{ |
| 120 | + /* 1 sector = 1 page */ |
| 121 | + if (address >= (MXC_FLASH_MEM_BASE + MXC_FLASH_FULL_MEM_SIZE)) { |
| 122 | + return MBED_FLASH_INVALID_SIZE; |
| 123 | + } else { |
| 124 | + return MXC_FLASH_PAGE_SIZE; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/** Get page size |
| 129 | + * * |
| 130 | + * * The page size defines the writable page size |
| 131 | + * * @param obj The flash object |
| 132 | + * * @return The size of a page |
| 133 | + * */ |
| 134 | +uint32_t flash_get_page_size(const flash_t *obj) |
| 135 | +{ |
| 136 | + return MXC_FLASH_PAGE_SIZE; |
| 137 | +} |
| 138 | + |
| 139 | +/** Get start address for the flash region |
| 140 | + * * |
| 141 | + * * @param obj The flash object |
| 142 | + * * @return The start address for the flash region |
| 143 | + * */ |
| 144 | +uint32_t flash_get_start_address(const flash_t *obj) |
| 145 | +{ |
| 146 | + return MXC_FLASH_MEM_BASE; |
| 147 | +} |
| 148 | + |
| 149 | +/** Get the flash region size |
| 150 | + * * |
| 151 | + * * @param obj The flash object |
| 152 | + * * @return The flash region size |
| 153 | + * */ |
| 154 | +uint32_t flash_get_size(const flash_t *obj) |
| 155 | +{ |
| 156 | + return MXC_FLASH_FULL_MEM_SIZE; |
| 157 | +} |
| 158 | + |
| 159 | +/** Get the flash erase value |
| 160 | + * * |
| 161 | + * * @param obj The flash object |
| 162 | + * * @return The flash erase value |
| 163 | + * */ |
| 164 | +uint8_t flash_get_erase_value(const flash_t *obj) |
| 165 | +{ |
| 166 | + (void)obj; |
| 167 | + |
| 168 | + return 0xFF; |
| 169 | +} |
| 170 | + |
| 171 | +#endif |
| 172 | + |
0 commit comments