flash_if.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. ******************************************************************************
  3. * @file IAP_Main/Src/flash_if.c
  4. * @author MCD Application Team
  5. * @version V1.6.0
  6. * @date 12-May-2017
  7. * @brief This file provides all the memory related operation functions.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Includes ------------------------------------------------------------------*/
  38. #include "flash_if.h"
  39. #include "hwsetup.h"
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* Private define ------------------------------------------------------------*/
  42. /* Private macro -------------------------------------------------------------*/
  43. /* Private variables ---------------------------------------------------------*/
  44. /* Private function prototypes -----------------------------------------------*/
  45. /* Private functions ---------------------------------------------------------*/
  46. volatile DL_FLASHCTL_COMMAND_STATUS gCmdStatus;
  47. volatile uint8_t gErrorType=0;
  48. uint32_t adderr=0;
  49. // inline void BSL_CI_disableCache(void);
  50. // inline void BSL_CI_enableCache(void);
  51. /**
  52. * @brief Unlocks Flash for write access
  53. * @param None
  54. * @retval None
  55. */
  56. /**
  57. * @brief This function does an erase of all user flash area
  58. * @param start: start of user flash area
  59. * @retval FLASHIF_OK : user flash area successfully erased
  60. * FLASHIF_ERASEKO : error occurred
  61. */
  62. uint32_t FLASH_If_Erase(uint32_t start)
  63. {
  64. uint32_t NbrOfSectors = 0;
  65. // flash_unlock();
  66. __disable_irq();
  67. for(NbrOfSectors = 0; start + NbrOfSectors * USER_FLASH_SECTOR_SIZE < (USER_FLASH_BANK1_END_ADDRESS + 1);NbrOfSectors++)
  68. {
  69. DL_FlashCTL_unprotectSector(
  70. FLASHCTL, start, DL_FLASHCTL_REGION_SELECT_MAIN);
  71. // flash_sector_erase(start + NbrOfSectors * USER_FLASH_SECTOR_SIZE);
  72. gCmdStatus= DL_FlashCTL_eraseMemoryFromRAM( FLASHCTL, (start + NbrOfSectors * USER_FLASH_SECTOR_SIZE), DL_FLASHCTL_COMMAND_SIZE_SECTOR);
  73. adderr=(start + NbrOfSectors * USER_FLASH_SECTOR_SIZE);
  74. if (gCmdStatus != DL_FLASHCTL_COMMAND_STATUS_PASSED)
  75. {
  76. /* If command did not pass, set error flag. */
  77. gErrorType = 15;
  78. }
  79. }
  80. // flash_lock();
  81. __enable_irq();
  82. return FLASHIF_OK;
  83. }
  84. /* Public functions ---------------------------------------------------------*/
  85. /**
  86. * @brief This function writes a data buffer in flash (data are 32-bit aligned).
  87. * @note After writing data buffer, the flash content is checked.
  88. * @param destination: start address for target location
  89. * @param p_source: pointer on buffer with data to write
  90. * @param length: length of data buffer (unit is 32-bit word)
  91. * @retval uint32_t 0: Data successfully written to Flash memory
  92. * 1: Error occurred while writing data in Flash memory
  93. * 2: Written Data in flash memory is different from expected one
  94. */
  95. uint32_t *p_source_test;
  96. uint32_t gDataArray64[] = {0, 0};
  97. uint32_t gDataArraytest[] = {0, 0};
  98. uint32_t FLASH_If_Write(uint32_t destination, uint32_t *p_source, uint32_t length)
  99. {
  100. uint32_t i = 0;
  101. __disable_irq();
  102. // flash_unlock();
  103. // DL_FlashCTL_unprotectSector(
  104. // FLASHCTL, destination, DL_FLASHCTL_REGION_SELECT_MAIN);
  105. p_source_test=p_source;
  106. for (i = 0; (i < length) && (destination <= (USER_FLASH_BANK1_END_ADDRESS-8)); i++)
  107. {
  108. /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  109. be done by word */
  110. gDataArray64[0]= *(uint32_t*)(p_source+2*i) ;
  111. gDataArray64[1]= *(uint32_t*)(p_source+2*i+1) ;
  112. DL_FlashCTL_unprotectSector(
  113. FLASHCTL, destination, DL_FLASHCTL_REGION_SELECT_MAIN);
  114. gCmdStatus = DL_FlashCTL_programMemoryFromRAM64WithECCGenerated(
  115. FLASHCTL, destination , &gDataArray64[0]);
  116. // if (gCmdStatus == DL_FLASHCTL_COMMAND_STATUS_FAILED)
  117. if (gCmdStatus != DL_FLASHCTL_COMMAND_STATUS_PASSED)
  118. {
  119. /* If command did not pass, set error flag. */
  120. gErrorType = 12;
  121. }
  122. if(gErrorType !=11 ) //TODO
  123. // DL_FLASHCTL_COMMAND_STATUS_PASSED = DL_FlashCTL_programMemoryFromRAM16WithECCGenerated(
  124. // FLASHCTL, (MAIN_BASE_ADDRESS + 8), &gData16);
  125. {
  126. /* Check the written value */
  127. // gDataArraytest[0]=*(uint32_t*)destination;
  128. // gDataArraytest[1]=*(uint32_t*)(destination+1);
  129. // if ((*(uint32_t*)destination != *(uint32_t*)(p_source+i))
  130. // &&(*(uint32_t*)(destination+4) != *(uint32_t*)(p_source+i+1)))
  131. // {
  132. // /* Flash content doesn't match SRAM content */
  133. // return(FLASHIF_WRITINGCTRL_ERROR);
  134. // }
  135. /* Increment FLASH destination address */
  136. destination += 8;
  137. }
  138. else
  139. {
  140. gDataArraytest[0]=11;
  141. gDataArraytest[1]=22;
  142. /* Error occurred while writing data in Flash memory */
  143. return (FLASHIF_WRITING_ERROR);
  144. }
  145. }
  146. __enable_irq();
  147. // flash_lock();
  148. return (FLASHIF_OK);
  149. }
  150. //addr_end =1024整数倍-1
  151. uint8_t CMD_USER_API_Flash_Range_Erase(uint32_t addrStart,uint32_t addr_end)
  152. {
  153. uint8_t ret = 0;
  154. uint8_t erase_status = (uint8_t) 1;
  155. uint32_t main_flash_start_address = addrStart;
  156. uint32_t main_flash_end_address =addr_end;;
  157. uint32_t data_pointer;
  158. data_pointer = addrStart;
  159. /* Check if BSL is unlocked */
  160. /* If the address range is valid, then erase the memory range */
  161. if ((addrStart >= main_flash_start_address) &&
  162. (addr_end <= main_flash_end_address) && (addrStart <= addr_end))
  163. {
  164. // BSL_CI_disableCache();
  165. while (data_pointer <= addr_end) {
  166. DL_FlashCTL_unprotectSector(
  167. FLASHCTL, data_pointer, DL_FLASHCTL_REGION_SELECT_MAIN);
  168. DL_FlashCTL_eraseMemoryFromRAM(
  169. FLASHCTL, data_pointer, DL_FLASHCTL_COMMAND_SIZE_SECTOR);
  170. erase_status = erase_status &
  171. (uint8_t) DL_FlashCTL_waitForCmdDone(FLASHCTL);
  172. data_pointer = data_pointer + MAIN_SECTOR_SIZE;
  173. }
  174. DL_FlashCTL_unprotectSector(
  175. FLASHCTL, addr_end, DL_FLASHCTL_REGION_SELECT_MAIN);
  176. DL_FlashCTL_eraseMemoryFromRAM(
  177. FLASHCTL, addr_end, DL_FLASHCTL_COMMAND_SIZE_SECTOR);
  178. erase_status =
  179. erase_status & (uint8_t) DL_FlashCTL_waitForCmdDone(FLASHCTL);
  180. // BSL_CI_enableCache();
  181. /* Check if all flash sector erase got passed */
  182. if (erase_status == (uint8_t) 0)
  183. {
  184. ret = 2;
  185. }
  186. }
  187. else {
  188. ret = 3;
  189. }
  190. return ret;
  191. }
  192. //inline void BSL_CI_disableCache(void)
  193. //{
  194. // DL_CORE_configInstruction(CPUSS_CTL_ICACHE_DISABLE,
  195. // CPUSS_CTL_PREFETCH_DISABLE, CPUSS_CTL_LITEN_DISABLE);
  196. //}
  197. //
  198. //inline void BSL_CI_enableCache(void)
  199. //{
  200. // DL_CORE_configInstruction(CPUSS_CTL_ICACHE_ENABLE,
  201. // CPUSS_CTL_PREFETCH_ENABLE, CPUSS_CTL_LITEN_ENABLE);
  202. //}
  203. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/