123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- /**
- ******************************************************************************
- * File Name : Flash.c
- * Description : This file provides code for the configuration
- * of the USART instances.
- ******************************************************************************
- ** This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * COPYRIGHT(c) 2019 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "Flash.h"
- #include "uart_process.h"
- FLASH_EraseInitTypeDef CDLPropertiesEraseInitStruct;
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- void FLASH_Init(void)
- {
- uint8_t reset_count = 0;
-
- /*CDL属性区擦除结构体初始化*/
- CDLPropertiesEraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
- CDLPropertiesEraseInitStruct.PageAddress = CDLPROPERTIES_AREA_ADDRESS;
- CDLPropertiesEraseInitStruct.NbPages = (CDLPROPERTIES_AREA_END_ADDRESS - CDLPROPERTIES_AREA_ADDRESS+1)/FLASH_PAGE_SIZE;
-
- Transmit_UARTOrCAN = (uint8_t)FLASH_Read(CDLPROPERTIES_AREA_ADDRESS, 1);
- Uart_BootBaudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+4, 1);
- Uart_AppBaudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+8, 1);
- CAN_Baudrate = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+12, 1);
- HandingFlag_Enable = FLASH_Read(CDLPROPERTIES_AREA_ADDRESS+16, 1);
-
- if(Transmit_UARTOrCAN == 0xFF)
- {
- Transmit_UARTOrCAN = 1;
- reset_count++;
- }
- if(Uart_BootBaudrate == 0xFFFFFFFF)
- {
- Uart_BootBaudrate = 57600;
- reset_count++;
- }
- if(Uart_AppBaudrate == 0xFFFFFFFF)
- {
- Uart_AppBaudrate = 9600;
- reset_count++;
- }
- if(CAN_Baudrate == 0xFFFFFFFF)
- {
- CAN_Baudrate = 250;
- reset_count++;
- }
-
- if(reset_count > 0)
- {
- FLASH_WriteCDLProperties(CDLPROPERTIES_AREA_ADDRESS);
- }
- }
- uint32_t FLASH_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit)
- {
- uint32_t PageError;
- if (HAL_FLASHEx_Erase(pEraseInit, &PageError) != HAL_OK)
- {
- /* Error occurred while page erase */
- return (1);
- }
- return (0);
- }
- uint32_t FLASH_Read(uint32_t Address, uint16_t DataLength)
- {
- static uint32_t data32 = 0xFFFFF000;
-
- for (uint16_t i = 0; (i < DataLength) && (Address <= (FLASH_END_ADDRESS-4)); i++)
- {
- data32 = *(__IO uint32_t *)Address;
- Address = Address + 4;
- }
- return data32;
- }
- uint32_t FLASH_WriteCDLProperties(uint32_t Address)
- {
- uint32_t i = 0;
- uint32_t Data[5];
- uint16_t DataLength;
- /* Unlock the Flash to enable the flash control register access *************/
- HAL_FLASH_Unlock();
-
- __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
-
- FLASH_FLASHEx_Erase(&CDLPropertiesEraseInitStruct);
-
- HAL_Delay(200);
- DataLength = 5;
- Data[0] = Transmit_UARTOrCAN;
- Data[1] = Uart_BootBaudrate;
- Data[2] = Uart_AppBaudrate;
- Data[3] = CAN_Baudrate;
- Data[4] = HandingFlag_Enable;
- for (i = 0; (i < DataLength) && (Address <= (FLASH_END_ADDRESS-4)); i++)
- {
- /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
- be done by word */
- if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, *(uint32_t*)(Data+i)) == HAL_OK)
- {
- /* Check the written value */
- // if (*(uint32_t*)FlashAddress != *(uint16_t*)(Data+i))
- // {
- // /* Flash content doesn't match SRAM content */
- // return(1);
- // }
- /* Increment FLASH destination address */
- Address += 4;
- }
- else
- {
- /* Error occurred while writing data in Flash memory */
- return (1);
- }
- }
- /* Lock the Flash to disable the flash control register access (recommended
- to protect the FLASH memory against possible unwanted operation) *********/
- HAL_FLASH_Lock();
- return (0);
- }
- /* USER CODE END 1 */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|