core_flash.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "bl_inc.h"
  22. void CACHE_CleanAll(CACHE_TypeDef *Cache)
  23. {
  24. while (Cache->CACHE_CS & CACHE_IS_BUSY);
  25. Cache->CACHE_REF = CACHE_REFRESH_ALLTAG;
  26. Cache->CACHE_REF |= CACHE_REFRESH;
  27. while ((Cache->CACHE_REF & CACHE_REFRESH));
  28. }
  29. /**
  30. * @brief Flash Erase Sector.
  31. * @param sectorAddress: The sector address to be erased
  32. * @retval FLASH Status: The returned value can be: QSPI_STATUS_ERROR, QSPI_STATUS_OK
  33. */
  34. uint8_t FLASH_EraseSector(uint32_t sectorAddress)
  35. {
  36. uint8_t ret;
  37. __disable_irq();
  38. //__disable_fault_irq();
  39. ret = ROM_QSPI_EraseSector(NULL, sectorAddress);
  40. //__enable_fault_irq();
  41. __enable_irq();
  42. return ret;
  43. }
  44. /**
  45. * @brief Flash Program Interface.
  46. * @param addr: specifies the address to be programmed.
  47. * @param size: specifies the size to be programmed.
  48. * @param buffer: pointer to the data to be programmed, need word aligned
  49. * @retval FLASH Status: The returned value can be: QSPI_STATUS_ERROR, QSPI_STATUS_OK
  50. */
  51. uint8_t FLASH_ProgramPage(uint32_t addr, uint32_t size, uint8_t *buffer)
  52. {
  53. uint8_t ret;
  54. QSPI_CommandTypeDef cmdType;
  55. cmdType.Instruction = QUAD_INPUT_PAGE_PROG_CMD;
  56. cmdType.BusMode = QSPI_BUSMODE_114;
  57. cmdType.CmdFormat = QSPI_CMDFORMAT_CMD8_ADDR24_PDAT;
  58. __disable_irq();
  59. //__disable_fault_irq();
  60. ret = ROM_QSPI_ProgramPage(&cmdType, DMA_Channel_1, addr, size, buffer);
  61. //__enable_fault_irq();
  62. __enable_irq();
  63. return ret;
  64. }
  65. int Flash_EraseSector(uint32_t address, uint8_t NeedCheck)
  66. {
  67. uint8_t buf[256];
  68. uint32_t i;
  69. uint8_t retry = 1;
  70. void *res;
  71. memset(buf, 0xff, 256);
  72. BL_ERASESECTOR_AGAIN:
  73. FLASH_EraseSector(address);
  74. CACHE_CleanAll(CACHE);
  75. if (!NeedCheck) return ERROR_NONE;
  76. for(i = 0; i < 4096; i+=256)
  77. {
  78. res = memcmp(address + i, buf, 256);
  79. if (res)
  80. {
  81. DBG_INFO("%x", res);
  82. if (retry)
  83. {
  84. retry = 0;
  85. goto BL_ERASESECTOR_AGAIN;
  86. }
  87. else
  88. {
  89. return -1;
  90. }
  91. }
  92. }
  93. return 0;
  94. }
  95. int Flash_ProgramData(uint32_t address, uint32_t *Data, uint32_t Len, uint8_t NeedCheck)
  96. {
  97. void *res;
  98. uint32_t size = (Len + (4 - 1)) & (~(4 - 1));
  99. if (size > 256)
  100. {
  101. size = 256;
  102. }
  103. FLASH_ProgramPage(address, size, Data);
  104. CACHE_CleanAll(CACHE);
  105. if (!NeedCheck) return ERROR_NONE;
  106. res = memcmp(address, Data, Len);
  107. if (res)
  108. {
  109. DBG_INFO("%x", res);
  110. FLASH_ProgramPage(address, size, Data);
  111. CACHE_CleanAll(CACHE);
  112. res = memcmp(address, Data, size);
  113. if (res)
  114. {
  115. DBG_INFO("%x", res);
  116. return -1;
  117. }
  118. }
  119. return 0;
  120. }