source: trunk/firmware/SES/Drivers/EEPROM/flash_interface.c@ 8

Last change on this file since 8 was 1, checked in by f.jahn, 3 years ago
File size: 7.5 KB
Line 
1/**
2 ******************************************************************************
3 * @file EEPROM_Emul/Porting/STM32G0/flash_interface.c
4 * @author MCD Application Team
5 * @brief This file provides all the EEPROM emulation flash interface functions.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics International N.V.
10 * All rights reserved.</center></h2>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted, provided that the following conditions are met:
14 *
15 * 1. Redistribution 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 other
21 * contributors to this software may be used to endorse or promote products
22 * derived from this software without specific written permission.
23 * 4. This software, including modifications and/or derivative works of this
24 * software, must execute solely and exclusively on microcontroller or
25 * microprocessor devices manufactured by or for STMicroelectronics.
26 * 5. Redistribution and use of this software other than as permitted under
27 * this license is void and will automatically terminate your rights under
28 * this license.
29 *
30 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
31 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
33 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
34 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
35 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
36 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
38 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
41 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 ******************************************************************************
44 */
45
46/* Includes ------------------------------------------------------------------*/
47#include "eeprom_emul.h"
48#include "flash_interface.h"
49
50/** @addtogroup EEPROM_Emulation
51 * @{
52 */
53
54/* Private typedef -----------------------------------------------------------*/
55/* Private constants ---------------------------------------------------------*/
56/* Private macro -------------------------------------------------------------*/
57/* Private variables ---------------------------------------------------------*/
58/* Private function prototypes -----------------------------------------------*/
59/* Exported functions --------------------------------------------------------*/
60/* Private functions ---------------------------------------------------------*/
61/** @addtogroup EEPROM_Private_Functions
62 * @{
63 */
64
65/**
66 * @brief Write a double word at the given address in Flash
67 * @param Address Where to write
68 * @param Data What to write
69 * @retval EE_Status
70 * - EE_OK: on success
71 * - EE_WRITE_ERROR: if an error occurs
72 */
73HAL_StatusTypeDef FI_WriteDoubleWord(uint32_t Address, uint64_t Data)
74{
75 return HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, Data);
76}
77
78/**
79 * @brief Erase a page in polling mode
80 * @param Page Page number
81 * @param NbPages Number of pages to erase
82 * @retval EE_Status
83 * - EE_OK: on success
84 * - EE error code: if an error occurs
85 */
86EE_Status FI_PageErase(uint32_t Page, uint16_t NbPages)
87{
88 EE_Status status = EE_OK;
89 FLASH_EraseInitTypeDef s_eraseinit;
90 uint32_t page_error = 0U;
91
92 s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES;
93 s_eraseinit.NbPages = NbPages;
94 s_eraseinit.Page = Page;
95
96 /* Erase the Page: Set Page status to ERASED status */
97 if (HAL_FLASHEx_Erase(&s_eraseinit, &page_error) != HAL_OK)
98 {
99 status = EE_ERASE_ERROR;
100 }
101 return status;
102}
103
104/**
105 * @brief Erase a page with interrupt enabled
106 * @param Page Page number
107 * @param NbPages Number of pages to erase
108 * @retval EE_Status
109 * - EE_OK: on success
110 * - EE error code: if an error occurs
111 */
112EE_Status FI_PageErase_IT(uint32_t Page, uint16_t NbPages)
113{
114 EE_Status status = EE_OK;
115 FLASH_EraseInitTypeDef s_eraseinit;
116
117 s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES;
118 s_eraseinit.NbPages = NbPages;
119 s_eraseinit.Page = Page;
120
121 /* Erase the Page: Set Page status to ERASED status */
122 if (HAL_FLASHEx_Erase_IT(&s_eraseinit) != HAL_OK)
123 {
124 status = EE_ERASE_ERROR;
125 }
126 return status;
127}
128
129/**
130 * @brief Delete corrupted Flash address, can be called from NMI. No Timeout.
131 * @param Address Address of the FLASH Memory to delete
132 * @retval EE_Status
133 * - EE_OK: on success
134 * - EE error code: if an error occurs
135 */
136EE_Status FI_DeleteCorruptedFlashAddress(uint32_t Address)
137{
138 EE_Status status = EE_OK;
139
140 /* Set FLASH Programmation bit */
141 SET_BIT(FLASH->CR, FLASH_CR_PG);
142
143 /* Program double word of value 0 */
144 *(__IO uint32_t*)(Address) = (uint32_t)0U;
145 *(__IO uint32_t*)(Address+4U) = (uint32_t)0U;
146
147 /* Wait programmation completion */
148 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
149 {
150 }
151
152 /* Check if error occured */
153 if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) ||
154 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)) ||
155 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR)))
156 {
157 status = EE_DELETE_ERROR;
158 }
159
160 /* Check FLASH End of Operation flag */
161 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
162 {
163 /* Clear FLASH End of Operation pending bit */
164 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
165 }
166
167 /* Clear FLASH Programmation bit */
168 CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
169
170 /* Clear FLASH ECCD bit */
171 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ECCD);
172
173 return status;
174}
175
176/**
177 * @brief Check if the configuration is 128-bits bank or 2*64-bits bank
178 * @param None
179 * @retval EE_Status
180 * - EE_OK: on success
181 * - EE error code: if an error occurs
182 */
183EE_Status FI_CheckBankConfig(void)
184{
185#if defined (FLASH_OPTR_DBANK)
186 FLASH_OBProgramInitTypeDef sOBCfg;
187 EE_Status status;
188
189 /* Request the Option Byte configuration :
190 - User and RDP level are always returned
191 - WRP and PCROP are not requested */
192 sOBCfg.WRPArea = 0xFF;
193 sOBCfg.PCROPConfig = 0xFF;
194 HAL_FLASHEx_OBGetConfig(&sOBCfg);
195
196 /* Check the value of the DBANK user option byte */
197 if ((sOBCfg.USERConfig & OB_DBANK_64_BITS) != 0)
198 {
199 status = EE_OK;
200 }
201 else
202 {
203 status = EE_INVALID_BANK_CFG;
204 }
205
206 return status;
207#else
208 /* No feature 128-bits single bank, so always 64-bits dual bank */
209 return EE_OK;
210#endif
211}
212
213
214/**
215 * @}
216 */
217
218/**
219 * @}
220 */
221
222/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.