You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.8 KiB

  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. #include <string.h>
  17. #include <stdbool.h>
  18. #include "legacy_flash_ops.h"
  19. uint8_t FlashBuf[MOCK_FLASH_SIZE] = {0};
  20. static bool flash_locked = true;
  21. FLASH_Status FLASH_ErasePage(uint32_t Page_Address) {
  22. if (flash_locked) return FLASH_ERROR_WRP;
  23. Page_Address -= (uintptr_t)FlashBuf;
  24. Page_Address -= (Page_Address % FEE_PAGE_SIZE);
  25. if (Page_Address >= MOCK_FLASH_SIZE) return FLASH_BAD_ADDRESS;
  26. memset(&FlashBuf[Page_Address], '\xff', FEE_PAGE_SIZE);
  27. return FLASH_COMPLETE;
  28. }
  29. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) {
  30. if (flash_locked) return FLASH_ERROR_WRP;
  31. Address -= (uintptr_t)FlashBuf;
  32. if (Address >= MOCK_FLASH_SIZE) return FLASH_BAD_ADDRESS;
  33. uint16_t oldData = *(uint16_t*)&FlashBuf[Address];
  34. if (oldData == 0xFFFF || Data == 0) {
  35. *(uint16_t*)&FlashBuf[Address] = Data;
  36. return FLASH_COMPLETE;
  37. } else {
  38. return FLASH_ERROR_PG;
  39. }
  40. }
  41. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) {
  42. return FLASH_COMPLETE;
  43. }
  44. void FLASH_Unlock(void) {
  45. flash_locked = false;
  46. }
  47. void FLASH_Lock(void) {
  48. flash_locked = true;
  49. }