flash.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. ******************************************************************************
  3. * File Name : Flash.c
  4. * Description : This file provides code for the configuration
  5. * of the USART instances.
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "Flash.h"
  41. #include "uart_process.h"
  42. FLASH_EraseInitTypeDef CDLPropertiesEraseInitStruct;
  43. /* USER CODE BEGIN 0 */
  44. /* USER CODE END 0 */
  45. void FLASH_Init(void)
  46. {
  47. uint8_t reset_count = 0;
  48. /*CDL属性区擦除结构体初始化*/
  49. CDLPropertiesEraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  50. CDLPropertiesEraseInitStruct.PageAddress = CDLPROPERTIES_AREA_ADDRESS;
  51. CDLPropertiesEraseInitStruct.NbPages = (CDLPROPERTIES_AREA_END_ADDRESS - CDLPROPERTIES_AREA_ADDRESS+1)/FLASH_PAGE_SIZE;
  52. Transmit_UARTOrCAN = (uint8_t)FLASH_Read(CDLPROPERTIES_AREA_ADDRESS, 1);
  53. Uart_BootBaudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+4, 1);
  54. Uart_AppBaudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+8, 1);
  55. CAN_Baudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+12, 1);
  56. if(Transmit_UARTOrCAN == 0xFF)
  57. {
  58. Transmit_UARTOrCAN = 1;
  59. reset_count++;
  60. }
  61. if(Uart_BootBaudrate == 0xFFFFFFFF)
  62. {
  63. Uart_BootBaudrate = 19200;
  64. reset_count++;
  65. }
  66. if(Uart_AppBaudrate == 0xFFFFFFFF)
  67. {
  68. Uart_AppBaudrate = 9600;
  69. reset_count++;
  70. }
  71. if(CAN_Baudrate == 0xFFFFFFFF)
  72. {
  73. CAN_Baudrate = 250;
  74. reset_count++;
  75. }
  76. if(reset_count > 0)
  77. {
  78. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  79. }
  80. }
  81. uint32_t FLASH_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit)
  82. {
  83. uint32_t PageError;
  84. if (HAL_FLASHEx_Erase(pEraseInit, &PageError) != HAL_OK)
  85. {
  86. /* Error occurred while page erase */
  87. return (1);
  88. }
  89. return (0);
  90. }
  91. uint32_t FLASH_Read(uint32_t Address, uint16_t DataLength)
  92. {
  93. static uint32_t data32 = 0xFFFFF000;
  94. for (uint16_t i = 0; (i < DataLength) && (Address <= (FLASH_END_ADDRESS-4)); i++)
  95. {
  96. data32 = *(__IO uint32_t *)Address;
  97. Address = Address + 4;
  98. }
  99. return data32;
  100. }
  101. uint32_t FLASH_WriteCDLProperties(uint32_t Address)
  102. {
  103. uint32_t i = 0;
  104. uint32_t Data[4];
  105. uint16_t DataLength;
  106. /* Unlock the Flash to enable the flash control register access *************/
  107. HAL_FLASH_Unlock();
  108. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
  109. FLASH_FLASHEx_Erase(&CDLPropertiesEraseInitStruct);
  110. HAL_Delay(200);
  111. DataLength = 4;
  112. Data[0] = Transmit_UARTOrCAN;
  113. Data[1] = Uart_BootBaudrate;
  114. Data[2] = Uart_AppBaudrate;
  115. Data[3] = CAN_Baudrate;
  116. for (i = 0; (i < DataLength) && (Address <= (FLASH_END_ADDRESS-4)); i++)
  117. {
  118. /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  119. be done by word */
  120. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, *(uint32_t*)(Data+i)) == HAL_OK)
  121. {
  122. /* Check the written value */
  123. // if (*(uint32_t*)FlashAddress != *(uint16_t*)(Data+i))
  124. // {
  125. // /* Flash content doesn't match SRAM content */
  126. // return(1);
  127. // }
  128. /* Increment FLASH destination address */
  129. Address += 4;
  130. }
  131. else
  132. {
  133. /* Error occurred while writing data in Flash memory */
  134. return (1);
  135. }
  136. }
  137. /* Lock the Flash to disable the flash control register access (recommended
  138. to protect the FLASH memory against possible unwanted operation) *********/
  139. HAL_FLASH_Lock();
  140. return (0);
  141. }
  142. /* USER CODE END 1 */
  143. /**
  144. * @}
  145. */
  146. /**
  147. * @}
  148. */
  149. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/