]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_firewall.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal_firewall.c
1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_firewall.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    06-February-2015
7   * @brief   FIREWALL HAL module driver.
8   *
9   *          This file provides firmware functions to manage the Firewall
10   *          Peripheral initialization and enabling.
11   *
12   *
13   @verbatim
14  ===============================================================================
15                         ##### How to use this driver ##### 
16  ===============================================================================
17   [..]
18     The FIREWALL HAL driver can be used as follows:
19               
20     (#) Declare a FIREWALL_InitTypeDef initialization structure.
21   
22     (#) Resort to HAL_FIREWALL_Config() API to initialize the Firewall
23
24     (#) Enable the FIREWALL in calling HAL_FIREWALL_EnableFirewall() API
25     
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
29         or
30         HAL_FIREWALL_EnablePreArmFlag() API if called outside of protected code segment
31         after HAL_FIREWALL_Config() call.
32
33
34   @endverbatim
35   ******************************************************************************
36   * @attention
37   *
38   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
39   *
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.
50   *
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.
61   *
62   ******************************************************************************  
63   */
64
65 /* Includes ------------------------------------------------------------------*/
66 #include "stm32l0xx_hal.h"
67
68 /** @addtogroup STM32L0xx_HAL_Driver
69   * @{
70   */
71
72 /** @defgroup FIREWALL FIREWALL 
73   * @brief HAL FIREWALL module driver
74   * @{
75   */
76 #ifdef HAL_FIREWALL_MODULE_ENABLED
77     
78 /* Private typedef -----------------------------------------------------------*/
79 /* Private define ------------------------------------------------------------*/
80 /* Private macro -------------------------------------------------------------*/
81 /* Private variables ---------------------------------------------------------*/
82 /* Private function prototypes -----------------------------------------------*/
83 /* Private functions ---------------------------------------------------------*/
84
85
86 /** @defgroup FIREWALL_Exported_Functions FIREWALL Exported Functions
87   * @{
88   */
89
90 /** @defgroup FIREWALL_Exported_Functions_Group1 Initialization Functions 
91   * @brief    Initialization and Configuration Functions 
92   *
93 @verbatim    
94 ===============================================================================
95             ##### Initialization and Configuration functions #####
96  ===============================================================================
97     [..]
98     This subsection provides the functions allowing to initialize the Firewall.
99     Initialization is done by HAL_FIREWALL_Config(): 
100
101       (+) Enable the Firewall clock thru __HAL_RCC_FIREWALL_CLK_ENABLE() macro.
102            
103       (+) Set the protected code segment address start and length.
104           
105       (+) Set the protected non-volatile and/or volatile data segments 
106           address starts and lengths if applicable.          
107           
108       (+) Set the volatile data segment execution and sharing status.
109       
110       (+) Length must be set to 0 for an unprotected segment.      
111
112 @endverbatim
113   * @{
114   */
115
116 /**
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.     
120   * @retval HAL status
121   */
122 HAL_StatusTypeDef HAL_FIREWALL_Config(FIREWALL_InitTypeDef * fw_init)
123 {
124   /* Check the Firewall initialization structure allocation */
125   if(fw_init == NULL)
126   {
127     return HAL_ERROR;
128   }
129   
130   /* Enable Firewall clock */
131   __HAL_RCC_FIREWALL_CLK_ENABLE();
132
133   /* Make sure that Firewall is not enabled already */
134   if (__HAL_FIREWALL_IS_ENABLED() != RESET)
135   {
136     return HAL_ERROR;
137   }
138   
139   /* Check Firewall configuration addresses and lengths when segment is protected */
140   /* Code segment */
141   if (fw_init->CodeSegmentLength != 0)
142   {
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));  
145   }
146   /* Non volatile data segment */
147   if (fw_init->NonVDataSegmentLength != 0)
148   {
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));  
151   }
152   /* Volatile data segment */
153   if (fw_init->VDataSegmentLength != 0)
154   {
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));  
157   }
158   
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));
162   
163   
164    /* Configuration */
165   
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));
170   
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));
175   
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));  
180   
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);
184   
185   return HAL_OK;
186 }
187
188 /**
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            
196   * @retval None
197   */
198 void HAL_FIREWALL_GetConfig(FIREWALL_InitTypeDef * fw_config)
199 {
200
201   /* Enable Firewall clock, in case no Firewall configuration has been carried 
202      out up to this point */
203   __HAL_RCC_FIREWALL_CLK_ENABLE();
204
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);
208   
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);
212   
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);     
216   
217   /* Retrieve volatile data execution setting */
218   fw_config->VolatileDataExecution = (READ_REG(FW->CR) & FW_CR_VDE);
219   
220   /* Retrieve volatile data shared setting */
221   fw_config->VolatileDataShared = (READ_REG(FW->CR) & FW_CR_VDS);
222   
223   return;
224 }
225
226
227
228 /**
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.           
233   * @retval None
234   */
235 void HAL_FIREWALL_EnableFirewall(void)
236 {
237   /* Clears FWDIS bit of SYSCFG CFGR1 register */
238   CLEAR_BIT(SYSCFG->CFGR2, SYSCFG_CFGR2_FWDISEN);
239   
240 }
241
242 /**
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.               
259   * @retval None
260   */
261 void HAL_FIREWALL_EnablePreArmFlag(void)
262 {
263   /* Set FPA bit */
264   SET_BIT(FW->CR, FW_CR_FPA);
265 }
266
267
268 /**
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.               
285           
286   * @retval None
287   */
288 void HAL_FIREWALL_DisablePreArmFlag(void)
289 {
290   /* Clear FPA bit */
291   CLEAR_BIT(FW->CR, FW_CR_FPA);
292 }
293
294 /**
295   * @}
296   */
297
298 /**
299   * @}
300   */
301
302 #endif /* HAL_FIREWALL_MODULE_ENABLED */
303 /**
304   * @}
305   */
306
307 /**
308   * @}
309   */
310
311 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/