fpec_f7.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * fpec_f7.c
  3. *
  4. * STM32F73x Flash Memory Program/Erase Controller (FPEC).
  5. *
  6. * Written & released by Keir Fraser <keir.xen@gmail.com>
  7. *
  8. * This is free and unencumbered software released into the public domain.
  9. * See the file COPYING for more details, or visit <http://unlicense.org>.
  10. */
  11. static void fpec_wait_and_clear(void)
  12. {
  13. cpu_sync();
  14. while (flash->sr & FLASH_SR_BSY)
  15. continue;
  16. flash->cr = 0;
  17. }
  18. void fpec_init(void)
  19. {
  20. /* Unlock the FPEC. */
  21. if (flash->cr & FLASH_CR_LOCK) {
  22. flash->keyr = 0x45670123;
  23. flash->keyr = 0xcdef89ab;
  24. }
  25. fpec_wait_and_clear();
  26. }
  27. void fpec_page_erase(uint32_t flash_address)
  28. {
  29. int sector = (flash_address - 0x08000000) >> 14;
  30. fpec_wait_and_clear();
  31. flash->cr = FLASH_CR_PSIZE(2) | FLASH_CR_SER | FLASH_CR_SNB(sector);
  32. flash->cr |= FLASH_CR_STRT;
  33. fpec_wait_and_clear();
  34. }
  35. void fpec_write(const void *data, unsigned int size, uint32_t flash_address)
  36. {
  37. uint16_t *_f = (uint16_t *)flash_address;
  38. const uint16_t *_d = data;
  39. fpec_wait_and_clear();
  40. for (; size != 0; size -= 2) {
  41. flash->cr = FLASH_CR_PSIZE(1) | FLASH_CR_PG;
  42. *_f++ = *_d++;
  43. fpec_wait_and_clear();
  44. }
  45. }
  46. /*
  47. * Local variables:
  48. * mode: C
  49. * c-file-style: "Linux"
  50. * c-basic-offset: 4
  51. * tab-width: 4
  52. * indent-tabs-mode: nil
  53. * End:
  54. */