flash_if.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. ******************************************************************************
  3. * @file flash_if.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 31-October-2011
  7. * @brief This file provides high level routines to manage internal Flash
  8. * programming (erase and write).
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  20. ******************************************************************************
  21. */
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "flash_if.h"
  24. #include "httpserver.h"
  25. #include "common_config.h"
  26. #include "wdg.h"
  27. #include "tinystdio.h"
  28. /**
  29. * @brief Чтение данных из flash
  30. * @param address
  31. * @retval data
  32. */
  33. uint32_t flash_read(uint32_t address)
  34. {
  35. return (*(__IO uint32_t*) address);
  36. }
  37. /**
  38. * @brief Unlocks Flash for write access
  39. * @param None
  40. * @retval None
  41. */
  42. void FLASH_If_Init(void)
  43. {
  44. FLASH_Unlock();
  45. }
  46. /**
  47. * @brief This function does an erase of all user flash area
  48. * @param StartSector: start of user flash area
  49. * @retval 0: user flash area successfully erased
  50. * 1: error occurred
  51. */
  52. int8_t FLASH_If_Erase(uint32_t StartSector)
  53. {
  54. uint32_t FlashAddress;
  55. FlashAddress = StartSector;
  56. /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  57. be done by word */
  58. if (FlashAddress <= (uint32_t) USER_FLASH_LAST_PAGE_ADDRESS)
  59. {
  60. FLASH_Unlock();
  61. GPIOE->ODR ^= WDT_PIN;
  62. FLASH_EraseSector(FLASH_Sector_5, VoltageRange_3); /* 128 Kbyte */
  63. GPIOE->ODR ^= WDT_PIN;
  64. FLASH_EraseSector(FLASH_Sector_6, VoltageRange_3); /* 128 Kbyte */
  65. GPIOE->ODR ^= WDT_PIN;
  66. FLASH_EraseSector(FLASH_Sector_7, VoltageRange_3); /* 128 Kbyte */
  67. GPIOE->ODR ^= WDT_PIN;
  68. /* Check if we use more than 512K flash */
  69. if (USER_FLASH_END_ADDRESS > 0x08000000 + 0x7FFFF) {
  70. FLASH_EraseSector(FLASH_Sector_8, VoltageRange_3); /* 128 Kbyte */
  71. GPIOE->ODR ^= WDT_PIN;
  72. FLASH_EraseSector(FLASH_Sector_9, VoltageRange_3); /* 128 Kbyte */
  73. GPIOE->ODR ^= WDT_PIN;
  74. }
  75. FLASH_Lock();
  76. }
  77. else
  78. {
  79. return (1);
  80. }
  81. return (0);
  82. }
  83. /**
  84. * @brief This function writes a data buffer in flash (data are 32-bit aligned).
  85. * @note After writing data buffer, the flash content is checked.
  86. * @param FlashAddress: start address for writing data buffer
  87. * @param Data: pointer on data buffer
  88. * @param DataLength: length of data buffer (unit is 32-bit word)
  89. * @retval 0: Data successfully written to Flash memory
  90. * 1: Error occurred while writing data in Flash memory
  91. * 2: Written Data in flash memory is different from expected one
  92. */
  93. uint32_t FLASH_If_Write(__IO uint32_t* FlashAddress, uint32_t* Data ,uint16_t DataLength)
  94. {
  95. uint32_t i = 0;
  96. uint32_t delta;
  97. /*
  98. delta = 0x0805FFFF - *FlashAddress; // ReturnFlashWriteAddress();
  99. printf("%i", delta);
  100. if ((delta < 1460) && (DataLength != 1))
  101. DataLength = delta / 4;
  102. */
  103. for (i = 0; (i < DataLength) && (*FlashAddress <= (USER_FLASH_END_ADDRESS)); i++)
  104. {
  105. /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  106. be done by word */
  107. if (FLASH_ProgramWord(*FlashAddress, *(uint32_t*)(Data+i)) == FLASH_COMPLETE)
  108. {
  109. /* Check the written value */
  110. if (*(uint32_t*)*FlashAddress != *(uint32_t*)(Data+i))
  111. {
  112. /* Flash content doesn't match SRAM content */
  113. return(2);
  114. }
  115. /* Increment FLASH destination address */
  116. *FlashAddress += 4;
  117. }
  118. else
  119. {
  120. /* Error occurred while writing data in Flash memory */
  121. return (1);
  122. }
  123. }
  124. return (0);
  125. }
  126. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/