flash.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* stm32f4.c
  2. *
  3. * Copyright (C) 2020 Danielinux
  4. *
  5. */
  6. #include <stdint.h>
  7. #include "system.h"
  8. /* STM32F4 FLASH Geometry */
  9. #define FLASH_SECTOR_0 0x0000000 /* 16 Kb */
  10. #define FLASH_SECTOR_1 0x0004000 /* 16 Kb */
  11. #define FLASH_SECTOR_2 0x0008000 /* 16 Kb */
  12. #define FLASH_SECTOR_3 0x000C000 /* 16 Kb */
  13. #define FLASH_SECTOR_4 0x0010000 /* 64 Kb */
  14. #define FLASH_SECTOR_5 0x0020000 /* 128 Kb */
  15. #define FLASH_SECTOR_6 0x0040000 /* 128 Kb */
  16. #define FLASH_SECTOR_7 0x0060000 /* 128 Kb */
  17. #define FLASH_SECTOR_8 0x0080000 /* 128 Kb */
  18. #define FLASH_SECTOR_9 0x00A0000 /* 128 Kb */
  19. #define FLASH_SECTOR_10 0x00C0000 /* 128 Kb */
  20. #define FLASH_SECTOR_11 0x00E0000 /* 128 Kb */
  21. #define FLASH_TOP 0x0100000
  22. #define FLASH_SECTORS 12
  23. const uint32_t flash_sector[FLASH_SECTORS + 1] = {
  24. FLASH_SECTOR_0,
  25. FLASH_SECTOR_1,
  26. FLASH_SECTOR_2,
  27. FLASH_SECTOR_3,
  28. FLASH_SECTOR_4,
  29. FLASH_SECTOR_5,
  30. FLASH_SECTOR_6,
  31. FLASH_SECTOR_7,
  32. FLASH_SECTOR_8,
  33. FLASH_SECTOR_9,
  34. FLASH_SECTOR_10,
  35. FLASH_SECTOR_11,
  36. FLASH_TOP
  37. };
  38. static void flash_wait_complete(void)
  39. {
  40. while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY)
  41. ;
  42. }
  43. static void flash_erase_sector(uint32_t sec)
  44. {
  45. uint32_t reg = FLASH_CR & (~(FLASH_CR_SNB_MASK << FLASH_CR_SNB_SHIFT));
  46. FLASH_CR = reg | (sec & FLASH_CR_SNB_MASK) << FLASH_CR_SNB_SHIFT;
  47. FLASH_CR |= FLASH_CR_SER;
  48. FLASH_CR |= FLASH_CR_STRT;
  49. flash_wait_complete();
  50. FLASH_CR &= ~FLASH_CR_SER;
  51. FLASH_CR &= ~(FLASH_CR_SNB_MASK << FLASH_CR_SNB_SHIFT);
  52. }
  53. static void clear_errors(void)
  54. {
  55. FLASH_SR |= ( FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR | FLASH_SR_WRPERR | FLASH_SR_OPERR | FLASH_SR_EOP );
  56. }
  57. int flash_write(uint32_t address, const uint8_t *data, int len)
  58. {
  59. int i;
  60. uint32_t val;
  61. flash_wait_complete();
  62. clear_errors();
  63. /* Set 8-bit write */
  64. FLASH_CR &= (~(0x03 << 8));
  65. for (i = 0; i < len; i++) {
  66. FLASH_CR |= FLASH_CR_PG;
  67. *((uint8_t *)(address + i)) = data[i];
  68. flash_wait_complete();
  69. FLASH_CR &= ~FLASH_CR_PG;
  70. }
  71. return 0;
  72. }
  73. void flash_unlock(void)
  74. {
  75. FLASH_CR |= FLASH_CR_LOCK;
  76. FLASH_KEYR = FLASH_KEY1;
  77. FLASH_KEYR = FLASH_KEY2;
  78. }
  79. void flash_lock(void)
  80. {
  81. FLASH_CR |= FLASH_CR_LOCK;
  82. }
  83. int flash_erase(uint32_t address, int len)
  84. {
  85. int start = -1, end = -1;
  86. uint32_t end_address;
  87. int i;
  88. if (len == 0)
  89. return -1;
  90. end_address = address + len - 1;
  91. if (address < flash_sector[0] || end_address > FLASH_TOP)
  92. return -1;
  93. for (i = 0; i < FLASH_SECTORS; i++)
  94. {
  95. if ((address >= flash_sector[i]) && (address < flash_sector[i + 1])) {
  96. start = i;
  97. }
  98. if ((end_address >= flash_sector[i]) && (end_address < flash_sector[i + 1])) {
  99. end = i;
  100. }
  101. if (start > 0 && end > 0)
  102. break;
  103. }
  104. if (start < 0 || end < 0)
  105. return -1;
  106. for (i = start; i <= end; i++)
  107. flash_erase_sector(i);
  108. return 0;
  109. }