diff options
Diffstat (limited to 'platforms/test/flash_stm32_mock.c')
| -rw-r--r-- | platforms/test/flash_stm32_mock.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/platforms/test/flash_stm32_mock.c b/platforms/test/flash_stm32_mock.c new file mode 100644 index 000000000..222a004bc --- /dev/null +++ b/platforms/test/flash_stm32_mock.c | |||
| @@ -0,0 +1,49 @@ | |||
| 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 "flash_stm32.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) { return FLASH_COMPLETE; } | ||
| 48 | void FLASH_Unlock(void) { flash_locked = false; } | ||
| 49 | void FLASH_Lock(void) { flash_locked = true; } | ||
