1 /* USER CODE BEGIN Header */
2 /**
3   ******************************************************************************
4   * @file    flash_driver.c
5   * @author  MCD Application Team
6   * @brief   The Flash Driver module is the interface layer between Flash
7   *          management modules and HAL Flash drivers
8   ******************************************************************************
9   * @attention
10   *
11   * Copyright (c) 2024 STMicroelectronics.
12   * All rights reserved.
13   *
14   * This software is licensed under terms that can be found in the LICENSE file
15   * in the root directory of this software component.
16   * If no LICENSE file comes with this software, it is provided AS-IS.
17   *
18   ******************************************************************************
19   */
20 /* USER CODE END Header */
21 
22 /* Includes ------------------------------------------------------------------*/
23 #include "flash_driver.h"
24 #include "utilities_conf.h"
25 
26 /* Global variables ----------------------------------------------------------*/
27 /* Private typedef -----------------------------------------------------------*/
28 /* Private defines -----------------------------------------------------------*/
29 
30 #define FD_CTRL_NO_BIT_SET   (0UL) /* value used to reset the Flash Control status */
31 
32 /* Private macros ------------------------------------------------------------*/
33 /* Private variables ---------------------------------------------------------*/
34 
35 /**
36  * @brief variable used to represent the Flash Control status
37  */
38 static volatile FD_Flash_ctrl_bm_t FD_Flash_Control_status = FD_CTRL_NO_BIT_SET;
39 
40 /* Private function prototypes -----------------------------------------------*/
41 /* Functions Definition ------------------------------------------------------*/
42 
43 /**
44   * @brief  Update Flash Control status
45   * @param  Flags_bm: Bit mask identifying the caller (1 bit per user)
46   * @param  Status:   Action requested (enable or disable flash access)
47   * @retval None
48   */
FD_SetStatus(FD_Flash_ctrl_bm_t Flags_bm,FD_FLASH_Status_t Status)49 void FD_SetStatus(FD_Flash_ctrl_bm_t Flags_bm, FD_FLASH_Status_t Status)
50 {
51   UTILS_ENTER_CRITICAL_SECTION();
52 
53   switch (Status)
54   {
55     case LL_FLASH_DISABLE:
56     {
57       FD_Flash_Control_status |= (1u << Flags_bm);
58       break;
59     }
60     case LL_FLASH_ENABLE:
61     {
62       FD_Flash_Control_status &= ~(1u << Flags_bm);
63       break;
64     }
65     default :
66     {
67       break;
68     }
69   }
70 
71   UTILS_EXIT_CRITICAL_SECTION();
72 }
73 
74 /**
75   * @brief  Write a block of 128 bits (4 32-bit words) in Flash
76   * @param  Dest: Address where to write in Flash (128-bit aligned)
77   * @param  Payload: Address of data to be written in Flash (32-bit aligned)
78   * @retval FD_FlashOp_Status_t: Success or failure of Flash write operation
79   */
FD_WriteData(uint32_t Dest,uint32_t Payload)80 FD_FlashOp_Status_t FD_WriteData(uint32_t Dest, uint32_t Payload)
81 {
82   FD_FlashOp_Status_t status = FD_FLASHOP_FAILURE;
83 
84   /* Check if RFTS OR Application allow flash access */
85   if ((FD_Flash_Control_status & (1u << FD_FLASHACCESS_RFTS)) &&
86       (FD_Flash_Control_status & (1u << FD_FLASHACCESS_RFTS_BYPASS)))
87   { /* Access not allowed */
88     return status;
89   }
90 
91   /* Wait for system to allow flash access */
92   while (FD_Flash_Control_status & (1u << FD_FLASHACCESS_SYSTEM));
93 
94   if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, Dest, Payload) == HAL_OK)
95   {
96     status = FD_FLASHOP_SUCCESS;
97   }
98   return status;
99 }
100 
101 /**
102   * @brief  Erase one sector of Flash
103   * @param  Sect: Identifier of the sector to erase
104   * @retval FD_FlashOp_Status_t: Success or failure of Flash erase operation
105   */
FD_EraseSectors(uint32_t Sect)106 FD_FlashOp_Status_t FD_EraseSectors(uint32_t Sect)
107 {
108   FD_FlashOp_Status_t status = FD_FLASHOP_FAILURE;
109   uint32_t page_error;
110   FLASH_EraseInitTypeDef p_erase_init;
111 
112 #ifndef FLASH_DBANK_SUPPORT
113   if (FLASH_PAGE_NB < Sect)
114 #else
115   if ((FLASH_PAGE_NB * 2u) < Sect)
116 #endif
117   {
118     return status;
119   }
120 
121   /* Check if LL allows flash access */
122   if ((FD_Flash_Control_status & (1u << FD_FLASHACCESS_RFTS)) &&
123       (FD_Flash_Control_status & (1u << FD_FLASHACCESS_RFTS_BYPASS)))
124   { /* Access not allowed */
125     return status;
126   }
127 
128   /* Wait for system to allow flash access */
129   while (FD_Flash_Control_status & (1u << FD_FLASHACCESS_SYSTEM));
130 
131   p_erase_init.TypeErase = FLASH_TYPEERASE_PAGES;
132   p_erase_init.Page = (Sect & (FLASH_PAGE_NB - 1u));
133   p_erase_init.NbPages = 1;
134 
135 #if defined(FLASH_DBANK_SUPPORT)
136   /* Verify which Bank is impacted */
137   if ((FLASH_PAGE_NB <= Sect) ^ (OB_SWAP_BANK_ENABLE == READ_BIT (FLASH->OPTR, FLASH_OPTR_SWAP_BANK_Msk)))
138   {
139     p_erase_init.Banks = FLASH_BANK_2;
140   }
141   else
142   {
143     p_erase_init.Banks = FLASH_BANK_1;
144   }
145 #endif
146 
147   if (HAL_FLASHEx_Erase(&p_erase_init, &page_error) == HAL_OK)
148   {
149     status = FD_FLASHOP_SUCCESS;
150   }
151 
152   return status;
153 }
154