2 ******************************************************************************
3 * @file stm32l0xx_hal_firewall.c
4 * @author MCD Application Team
6 * @date 06-February-2015
7 * @brief FIREWALL HAL module driver.
9 * This file provides firmware functions to manage the Firewall
10 * Peripheral initialization and enabling.
14 ===============================================================================
15 ##### How to use this driver #####
16 ===============================================================================
18 The FIREWALL HAL driver can be used as follows:
20 (#) Declare a FIREWALL_InitTypeDef initialization structure.
22 (#) Resort to HAL_FIREWALL_Config() API to initialize the Firewall
24 (#) Enable the FIREWALL in calling HAL_FIREWALL_EnableFirewall() API
26 (#) To ensure that any code executed outside the protected segment closes the
27 FIREWALL, the user must set the flag FIREWALL_PRE_ARM_SET in calling
28 __HAL_FIREWALL_PREARM_ENABLE() macro if called within a protected code segment
30 HAL_FIREWALL_EnablePreArmFlag() API if called outside of protected code segment
31 after HAL_FIREWALL_Config() call.
35 ******************************************************************************
38 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
40 * Redistribution and use in source and binary forms, with or without modification,
41 * are permitted provided that the following conditions are met:
42 * 1. Redistributions of source code must retain the above copyright notice,
43 * this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright notice,
45 * this list of conditions and the following disclaimer in the documentation
46 * and/or other materials provided with the distribution.
47 * 3. Neither the name of STMicroelectronics nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
52 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
58 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
59 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 ******************************************************************************
65 /* Includes ------------------------------------------------------------------*/
66 #include "stm32l0xx_hal.h"
68 /** @addtogroup STM32L0xx_HAL_Driver
72 /** @defgroup FIREWALL FIREWALL
73 * @brief HAL FIREWALL module driver
76 #ifdef HAL_FIREWALL_MODULE_ENABLED
78 /* Private typedef -----------------------------------------------------------*/
79 /* Private define ------------------------------------------------------------*/
80 /* Private macro -------------------------------------------------------------*/
81 /* Private variables ---------------------------------------------------------*/
82 /* Private function prototypes -----------------------------------------------*/
83 /* Private functions ---------------------------------------------------------*/
86 /** @defgroup FIREWALL_Exported_Functions FIREWALL Exported Functions
90 /** @defgroup FIREWALL_Exported_Functions_Group1 Initialization Functions
91 * @brief Initialization and Configuration Functions
94 ===============================================================================
95 ##### Initialization and Configuration functions #####
96 ===============================================================================
98 This subsection provides the functions allowing to initialize the Firewall.
99 Initialization is done by HAL_FIREWALL_Config():
101 (+) Enable the Firewall clock thru __HAL_RCC_FIREWALL_CLK_ENABLE() macro.
103 (+) Set the protected code segment address start and length.
105 (+) Set the protected non-volatile and/or volatile data segments
106 address starts and lengths if applicable.
108 (+) Set the volatile data segment execution and sharing status.
110 (+) Length must be set to 0 for an unprotected segment.
117 * @brief Initialize the Firewall according to the FIREWALL_InitTypeDef structure parameters.
118 * @param fw_init: Firewall initialization structure
119 * @note The API returns HAL_ERROR if the Firewall is already enabled.
122 HAL_StatusTypeDef
HAL_FIREWALL_Config(FIREWALL_InitTypeDef
* fw_init
)
124 /* Check the Firewall initialization structure allocation */
130 /* Enable Firewall clock */
131 __HAL_RCC_FIREWALL_CLK_ENABLE();
133 /* Make sure that Firewall is not enabled already */
134 if (__HAL_FIREWALL_IS_ENABLED() != RESET
)
139 /* Check Firewall configuration addresses and lengths when segment is protected */
141 if (fw_init
->CodeSegmentLength
!= 0)
143 assert_param(IS_FIREWALL_CODE_SEGMENT_ADDRESS(fw_init
->CodeSegmentStartAddress
));
144 assert_param(IS_FIREWALL_CODE_SEGMENT_LENGTH(fw_init
->CodeSegmentStartAddress
, fw_init
->CodeSegmentLength
));
146 /* Non volatile data segment */
147 if (fw_init
->NonVDataSegmentLength
!= 0)
149 assert_param(IS_FIREWALL_NONVOLATILEDATA_SEGMENT_ADDRESS(fw_init
->NonVDataSegmentStartAddress
));
150 assert_param(IS_FIREWALL_NONVOLATILEDATA_SEGMENT_LENGTH(fw_init
->NonVDataSegmentStartAddress
, fw_init
->NonVDataSegmentLength
));
152 /* Volatile data segment */
153 if (fw_init
->VDataSegmentLength
!= 0)
155 assert_param(IS_FIREWALL_VOLATILEDATA_SEGMENT_ADDRESS(fw_init
->VDataSegmentStartAddress
));
156 assert_param(IS_FIREWALL_VOLATILEDATA_SEGMENT_LENGTH(fw_init
->VDataSegmentStartAddress
, fw_init
->VDataSegmentLength
));
159 /* Check Firewall Configuration Register parameters */
160 assert_param(IS_FIREWALL_VOLATILEDATA_EXECUTE(fw_init
->VolatileDataExecution
));
161 assert_param(IS_FIREWALL_VOLATILEDATA_SHARE(fw_init
->VolatileDataShared
));
166 /* Protected code segment start address configuration */
167 WRITE_REG(FW
->CSSA
, (FW_CSSA_ADD
& fw_init
->CodeSegmentStartAddress
));
168 /* Protected code segment length configuration */
169 WRITE_REG(FW
->CSL
, (FW_CSL_LENG
& fw_init
->CodeSegmentLength
));
171 /* Protected non volatile data segment start address configuration */
172 WRITE_REG(FW
->NVDSSA
, (FW_NVDSSA_ADD
& fw_init
->NonVDataSegmentStartAddress
));
173 /* Protected non volatile data segment length configuration */
174 WRITE_REG(FW
->NVDSL
, (FW_NVDSL_LENG
& fw_init
->NonVDataSegmentLength
));
176 /* Protected volatile data segment start address configuration */
177 WRITE_REG(FW
->VDSSA
, (FW_VDSSA_ADD
& fw_init
->VDataSegmentStartAddress
));
178 /* Protected volatile data segment length configuration */
179 WRITE_REG(FW
->VDSL
, (FW_VDSL_LENG
& fw_init
->VDataSegmentLength
));
181 /* Set Firewall Configuration Register VDE and VDS bits
182 (volatile data execution and shared configuration) */
183 MODIFY_REG(FW
->CR
, FW_CR_VDS
|FW_CR_VDE
, fw_init
->VolatileDataExecution
|fw_init
->VolatileDataShared
);
189 * @brief Retrieve the Firewall configuration.
190 * @param fw_config: Firewall configuration, type is same as initialization structure
191 * @note This API can't be executed inside a code area protected by the Firewall
192 * when the Firewall is enabled
193 * @note If NVDSL register is different from 0, that is, if the non volatile data segment
194 * is defined, this API can't be executed when the Firewall is enabled.
195 * @note User should resort to __HAL_FIREWALL_GET_PREARM() macro to retrieve FPA bit status
198 void HAL_FIREWALL_GetConfig(FIREWALL_InitTypeDef
* fw_config
)
201 /* Enable Firewall clock, in case no Firewall configuration has been carried
202 out up to this point */
203 __HAL_RCC_FIREWALL_CLK_ENABLE();
205 /* Retrieve code segment protection setting */
206 fw_config
->CodeSegmentStartAddress
= (READ_REG(FW
->CSSA
) & FW_CSSA_ADD
);
207 fw_config
->CodeSegmentLength
= (READ_REG(FW
->CSL
) & FW_CSL_LENG
);
209 /* Retrieve non volatile data segment protection setting */
210 fw_config
->NonVDataSegmentStartAddress
= (READ_REG(FW
->NVDSSA
) & FW_NVDSSA_ADD
);
211 fw_config
->NonVDataSegmentLength
= (READ_REG(FW
->NVDSL
) & FW_NVDSL_LENG
);
213 /* Retrieve volatile data segment protection setting */
214 fw_config
->VDataSegmentStartAddress
= (READ_REG(FW
->VDSSA
) & FW_VDSSA_ADD
);
215 fw_config
->VDataSegmentLength
= (READ_REG(FW
->VDSL
) & FW_VDSL_LENG
);
217 /* Retrieve volatile data execution setting */
218 fw_config
->VolatileDataExecution
= (READ_REG(FW
->CR
) & FW_CR_VDE
);
220 /* Retrieve volatile data shared setting */
221 fw_config
->VolatileDataShared
= (READ_REG(FW
->CR
) & FW_CR_VDS
);
229 * @brief Enable FIREWALL.
230 * @note Firewall is enabled in clearing FWDIS bit of SYSCFG CFGR1 register.
231 * Once enabled, the Firewall cannot be disabled by software. Only a
232 * system reset can set again FWDIS bit.
235 void HAL_FIREWALL_EnableFirewall(void)
237 /* Clears FWDIS bit of SYSCFG CFGR1 register */
238 CLEAR_BIT(SYSCFG
->CFGR2
, SYSCFG_CFGR2_FWDISEN
);
243 * @brief Enable FIREWALL pre arm.
244 * @note When FPA bit is set, any code executed outside the protected segment
245 * will close the Firewall.
246 * @note This API provides the same service as __HAL_FIREWALL_PREARM_ENABLE() macro
247 * but can't be executed inside a code area protected by the Firewall.
248 * @note -- When the Firewall is disabled, user can resort to
249 * HAL_FIREWALL_EnablePreArmFlag() API any time.
250 * -- When the Firewall is enabled and NVDSL register is equal to 0 (that is,
251 * when the non volatile data segment is not defined),
252 * ** this API can be executed when the Firewall is closed
253 * ** when the Firewall is opened, user should resort to
254 * __HAL_FIREWALL_PREARM_ENABLE() macro instead
255 * -- When the Firewall is enabled and NVDSL register is different from 0
256 * (that is, when the non volatile data segment is defined)
257 * ** FW_CR register can be accessed only when the Firewall is opened:
258 * user should resort to __HAL_FIREWALL_PREARM_ENABLE() macro instead.
261 void HAL_FIREWALL_EnablePreArmFlag(void)
264 SET_BIT(FW
->CR
, FW_CR_FPA
);
269 * @brief Disable FIREWALL pre arm.
270 * @note When FPA bit is reset, any code executed outside the protected segment
271 * when the Firewall is opened will generate a system reset.
272 * @note This API provides the same service as __HAL_FIREWALL_PREARM_DISABLE() macro
273 * but can't be executed inside a code area protected by the Firewall.
274 * @note -- When the Firewall is disabled, user can resort to
275 * HAL_FIREWALL_EnablePreArmFlag() API any time.
276 * -- When the Firewall is enabled and NVDSL register is equal to 0 (that is,
277 * when the non volatile data segment is not defined),
278 * ** this API can be executed when the Firewall is closed
279 * ** when the Firewall is opened, user should resort to
280 * __HAL_FIREWALL_PREARM_DISABLE() macro instead
281 * -- When the Firewall is enabled and NVDSL register is different from 0
282 * (that is, when the non volatile data segment is defined)
283 * ** FW_CR register can be accessed only when the Firewall is opened:
284 * user should resort to __HAL_FIREWALL_PREARM_DISABLE() macro instead.
288 void HAL_FIREWALL_DisablePreArmFlag(void)
291 CLEAR_BIT(FW
->CR
, FW_CR_FPA
);
302 #endif /* HAL_FIREWALL_MODULE_ENABLED */
311 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/