flash.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. HandingFlag_Enable = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+16, 1);
  57. if(Transmit_UARTOrCAN == 0xFF)
  58. {
  59. Transmit_UARTOrCAN = 1;
  60. reset_count++;
  61. }
  62. if(Uart_BootBaudrate == 0xFFFFFFFF)
  63. {
  64. Uart_BootBaudrate = 57600;
  65. reset_count++;
  66. }
  67. if(Uart_AppBaudrate == 0xFFFFFFFF)
  68. {
  69. Uart_AppBaudrate = 9600;
  70. reset_count++;
  71. }
  72. if(CAN_Baudrate == 0xFFFFFFFF)
  73. {
  74. CAN_Baudrate = 250;
  75. reset_count++;
  76. }
  77. if(reset_count > 0)
  78. {
  79. FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
  80. }
  81. }
  82. uint32_t FLASH_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit)
  83. {
  84. uint32_t PageError;
  85. if (HAL_FLASHEx_Erase(pEraseInit, &PageError) != HAL_OK)
  86. {
  87. /* Error occurred while page erase */
  88. return (1);
  89. }
  90. return (0);
  91. }
  92. uint32_t FLASH_Read(uint32_t Address, uint16_t DataLength)
  93. {
  94. static uint32_t data32 = 0xFFFFF000;
  95. for (uint16_t i = 0; (i < DataLength) && (Address <= (FLASH_END_ADDRESS-4)); i++)
  96. {
  97. data32 = *(__IO uint32_t *)Address;
  98. Address = Address + 4;
  99. }
  100. return data32;
  101. }
  102. uint32_t FLASH_WriteCDLProperties(uint32_t Address)
  103. {
  104. uint32_t i = 0;
  105. uint32_t Data[5];
  106. uint16_t DataLength;
  107. /* Unlock the Flash to enable the flash control register access *************/
  108. HAL_FLASH_Unlock();
  109. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
  110. FLASH_FLASHEx_Erase(&CDLPropertiesEraseInitStruct);
  111. HAL_Delay(200);
  112. DataLength = 5;
  113. Data[0] = Transmit_UARTOrCAN;
  114. Data[1] = Uart_BootBaudrate;
  115. Data[2] = Uart_AppBaudrate;
  116. Data[3] = CAN_Baudrate;
  117. Data[4] = HandingFlag_Enable;
  118. for (i = 0; (i < DataLength) && (Address <= (FLASH_END_ADDRESS-4)); i++)
  119. {
  120. /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  121. be done by word */
  122. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, *(uint32_t*)(Data+i)) == HAL_OK)
  123. {
  124. /* Check the written value */
  125. // if (*(uint32_t*)FlashAddress != *(uint16_t*)(Data+i))
  126. // {
  127. // /* Flash content doesn't match SRAM content */
  128. // return(1);
  129. // }
  130. /* Increment FLASH destination address */
  131. Address += 4;
  132. }
  133. else
  134. {
  135. /* Error occurred while writing data in Flash memory */
  136. return (1);
  137. }
  138. }
  139. /* Lock the Flash to disable the flash control register access (recommended
  140. to protect the FLASH memory against possible unwanted operation) *********/
  141. HAL_FLASH_Lock();
  142. return (0);
  143. }
  144. /* USER CODE END 1 */
  145. /**
  146. * @}
  147. */
  148. /**
  149. * @}
  150. */
  151. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/