legacy_flash_ops_mock.c (1820B)
1 /* Copyright 2021 by Don Kjer 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 #include <string.h> 18 #include <stdbool.h> 19 #include "legacy_flash_ops.h" 20 21 uint8_t FlashBuf[MOCK_FLASH_SIZE] = {0}; 22 23 static bool flash_locked = true; 24 25 FLASH_Status FLASH_ErasePage(uint32_t Page_Address) { 26 if (flash_locked) return FLASH_ERROR_WRP; 27 Page_Address -= (uintptr_t)FlashBuf; 28 Page_Address -= (Page_Address % FEE_PAGE_SIZE); 29 if (Page_Address >= MOCK_FLASH_SIZE) return FLASH_BAD_ADDRESS; 30 memset(&FlashBuf[Page_Address], '\xff', FEE_PAGE_SIZE); 31 return FLASH_COMPLETE; 32 } 33 34 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) { 35 if (flash_locked) return FLASH_ERROR_WRP; 36 Address -= (uintptr_t)FlashBuf; 37 if (Address >= MOCK_FLASH_SIZE) return FLASH_BAD_ADDRESS; 38 uint16_t oldData = *(uint16_t *)&FlashBuf[Address]; 39 if (oldData == 0xFFFF || Data == 0) { 40 *(uint16_t *)&FlashBuf[Address] = Data; 41 return FLASH_COMPLETE; 42 } else { 43 return FLASH_ERROR_PG; 44 } 45 } 46 47 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) { 48 return FLASH_COMPLETE; 49 } 50 void FLASH_Unlock(void) { 51 flash_locked = false; 52 } 53 void FLASH_Lock(void) { 54 flash_locked = true; 55 }