You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

295 lines
11 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_firewall.c
  4. * @author MCD Application Team
  5. * @brief FIREWALL HAL module driver.
  6. * This file provides firmware functions to manage the Firewall
  7. * Peripheral initialization and enabling.
  8. *
  9. *
  10. @verbatim
  11. ===============================================================================
  12. ##### How to use this driver #####
  13. ===============================================================================
  14. [..]
  15. The FIREWALL HAL driver can be used as follows:
  16. (#) Declare a FIREWALL_InitTypeDef initialization structure.
  17. (#) Resort to HAL_FIREWALL_Config() API to initialize the Firewall
  18. (#) Enable the FIREWALL in calling HAL_FIREWALL_EnableFirewall() API
  19. (#) To ensure that any code executed outside the protected segment closes the
  20. FIREWALL, the user must set the flag FIREWALL_PRE_ARM_SET in calling
  21. __HAL_FIREWALL_PREARM_ENABLE() macro if called within a protected code segment
  22. or
  23. HAL_FIREWALL_EnablePreArmFlag() API if called outside of protected code segment
  24. after HAL_FIREWALL_Config() call.
  25. @endverbatim
  26. ******************************************************************************
  27. * @attention
  28. *
  29. * <h2><center>&copy; Copyright(c) 2016 STMicroelectronics.
  30. * All rights reserved.</center></h2>
  31. *
  32. * This software component is licensed by ST under BSD 3-Clause license,
  33. * the "License"; You may not use this file except in compliance with the
  34. * License. You may obtain a copy of the License at:
  35. * opensource.org/licenses/BSD-3-Clause
  36. *
  37. ******************************************************************************
  38. */
  39. #if !defined (STM32L010xB) && !defined (STM32L010x8) && !defined (STM32L010x6) && !defined (STM32L010x4) && !defined (STM32L011xx) && !defined (STM32L021xx) && !defined (STM32L031xx) && !defined (STM32L041xx)
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "stm32l0xx_hal.h"
  42. /** @addtogroup STM32L0xx_HAL_Driver
  43. * @{
  44. */
  45. #ifdef HAL_FIREWALL_MODULE_ENABLED
  46. /** @addtogroup FIREWALL
  47. * @brief HAL FIREWALL module driver
  48. * @{
  49. */
  50. /** @addtogroup FIREWALL_Exported_Functions
  51. * @{
  52. */
  53. /** @addtogroup FIREWALL_Exported_Functions_Group1
  54. * @brief Initialization and Configuration Functions
  55. *
  56. @verbatim
  57. ===============================================================================
  58. ##### Initialization and Configuration functions #####
  59. ===============================================================================
  60. [..]
  61. This subsection provides the functions allowing to initialize the Firewall.
  62. Initialization is done by HAL_FIREWALL_Config():
  63. (+) Enable the Firewall clock thru __HAL_RCC_FIREWALL_CLK_ENABLE() macro.
  64. (+) Set the protected code segment address start and length.
  65. (+) Set the protected non-volatile and/or volatile data segments
  66. address starts and lengths if applicable.
  67. (+) Set the volatile data segment execution and sharing status.
  68. (+) Length must be set to 0 for an unprotected segment.
  69. @endverbatim
  70. * @{
  71. */
  72. /**
  73. * @brief Initialize the Firewall according to the FIREWALL_InitTypeDef structure parameters.
  74. * @param fw_init: Firewall initialization structure
  75. * @note The API returns HAL_ERROR if the Firewall is already enabled.
  76. * @retval HAL status
  77. */
  78. HAL_StatusTypeDef HAL_FIREWALL_Config(FIREWALL_InitTypeDef * fw_init)
  79. {
  80. /* Check the Firewall initialization structure allocation */
  81. if(fw_init == NULL)
  82. {
  83. return HAL_ERROR;
  84. }
  85. /* Enable Firewall clock */
  86. __HAL_RCC_FIREWALL_CLK_ENABLE();
  87. /* Make sure that Firewall is not enabled already */
  88. if (__HAL_FIREWALL_IS_ENABLED() != RESET)
  89. {
  90. return HAL_ERROR;
  91. }
  92. /* Check Firewall configuration addresses and lengths when segment is protected */
  93. /* Code segment */
  94. if (fw_init->CodeSegmentLength != 0U)
  95. {
  96. assert_param(IS_FIREWALL_CODE_SEGMENT_ADDRESS(fw_init->CodeSegmentStartAddress));
  97. assert_param(IS_FIREWALL_CODE_SEGMENT_LENGTH(fw_init->CodeSegmentStartAddress, fw_init->CodeSegmentLength));
  98. /* Make sure that NonVDataSegmentLength is properly set to prevent code segment access */
  99. if (fw_init->NonVDataSegmentLength < 0x100)
  100. {
  101. return HAL_ERROR;
  102. }
  103. }
  104. /* Non volatile data segment */
  105. if (fw_init->NonVDataSegmentLength != 0U)
  106. {
  107. assert_param(IS_FIREWALL_NONVOLATILEDATA_SEGMENT_ADDRESS(fw_init->NonVDataSegmentStartAddress));
  108. assert_param(IS_FIREWALL_NONVOLATILEDATA_SEGMENT_LENGTH(fw_init->NonVDataSegmentStartAddress, fw_init->NonVDataSegmentLength));
  109. }
  110. /* Volatile data segment */
  111. if (fw_init->VDataSegmentLength != 0U)
  112. {
  113. assert_param(IS_FIREWALL_VOLATILEDATA_SEGMENT_ADDRESS(fw_init->VDataSegmentStartAddress));
  114. assert_param(IS_FIREWALL_VOLATILEDATA_SEGMENT_LENGTH(fw_init->VDataSegmentStartAddress, fw_init->VDataSegmentLength));
  115. }
  116. /* Check Firewall Configuration Register parameters */
  117. assert_param(IS_FIREWALL_VOLATILEDATA_EXECUTE(fw_init->VolatileDataExecution));
  118. assert_param(IS_FIREWALL_VOLATILEDATA_SHARE(fw_init->VolatileDataShared));
  119. /* Configuration */
  120. /* Protected code segment start address configuration */
  121. WRITE_REG(FIREWALL->CSSA, (FW_CSSA_ADD & fw_init->CodeSegmentStartAddress));
  122. /* Protected code segment length configuration */
  123. WRITE_REG(FIREWALL->CSL, (FW_CSL_LENG & fw_init->CodeSegmentLength));
  124. /* Protected non volatile data segment start address configuration */
  125. WRITE_REG(FIREWALL->NVDSSA, (FW_NVDSSA_ADD & fw_init->NonVDataSegmentStartAddress));
  126. /* Protected non volatile data segment length configuration */
  127. WRITE_REG(FIREWALL->NVDSL, (FW_NVDSL_LENG & fw_init->NonVDataSegmentLength));
  128. /* Protected volatile data segment start address configuration */
  129. WRITE_REG(FIREWALL->VDSSA, (FW_VDSSA_ADD & fw_init->VDataSegmentStartAddress));
  130. /* Protected volatile data segment length configuration */
  131. WRITE_REG(FIREWALL->VDSL, (FW_VDSL_LENG & fw_init->VDataSegmentLength));
  132. /* Set Firewall Configuration Register VDE and VDS bits
  133. (volatile data execution and shared configuration) */
  134. MODIFY_REG(FIREWALL->CR, FW_CR_VDS|FW_CR_VDE, fw_init->VolatileDataExecution|fw_init->VolatileDataShared);
  135. return HAL_OK;
  136. }
  137. /**
  138. * @brief Retrieve the Firewall configuration.
  139. * @param fw_config: Firewall configuration, type is same as initialization structure
  140. * @note This API can't be executed inside a code area protected by the Firewall
  141. * when the Firewall is enabled
  142. * @note If NVDSL register is different from 0, that is, if the non volatile data segment
  143. * is defined, this API can't be executed when the Firewall is enabled.
  144. * @note User should resort to __HAL_FIREWALL_GET_PREARM() macro to retrieve FPA bit status
  145. * @retval None
  146. */
  147. void HAL_FIREWALL_GetConfig(FIREWALL_InitTypeDef * fw_config)
  148. {
  149. /* Enable Firewall clock, in case no Firewall configuration has been carried
  150. out up to this point */
  151. __HAL_RCC_FIREWALL_CLK_ENABLE();
  152. /* Retrieve code segment protection setting */
  153. fw_config->CodeSegmentStartAddress = (READ_REG(FIREWALL->CSSA) & FW_CSSA_ADD);
  154. fw_config->CodeSegmentLength = (READ_REG(FIREWALL->CSL) & FW_CSL_LENG);
  155. /* Retrieve non volatile data segment protection setting */
  156. fw_config->NonVDataSegmentStartAddress = (READ_REG(FIREWALL->NVDSSA) & FW_NVDSSA_ADD);
  157. fw_config->NonVDataSegmentLength = (READ_REG(FIREWALL->NVDSL) & FW_NVDSL_LENG);
  158. /* Retrieve volatile data segment protection setting */
  159. fw_config->VDataSegmentStartAddress = (READ_REG(FIREWALL->VDSSA) & FW_VDSSA_ADD);
  160. fw_config->VDataSegmentLength = (READ_REG(FIREWALL->VDSL) & FW_VDSL_LENG);
  161. /* Retrieve volatile data execution setting */
  162. fw_config->VolatileDataExecution = (READ_REG(FIREWALL->CR) & FW_CR_VDE);
  163. /* Retrieve volatile data shared setting */
  164. fw_config->VolatileDataShared = (READ_REG(FIREWALL->CR) & FW_CR_VDS);
  165. return;
  166. }
  167. /**
  168. * @brief Enable FIREWALL.
  169. * @note Firewall is enabled in clearing FWDIS bit of SYSCFG CFGR1 register.
  170. * Once enabled, the Firewall cannot be disabled by software. Only a
  171. * system reset can set again FWDIS bit.
  172. * @retval None
  173. */
  174. void HAL_FIREWALL_EnableFirewall(void)
  175. {
  176. /* Clears FWDIS bit of SYSCFG CFGR1 register */
  177. CLEAR_BIT(SYSCFG->CFGR2, SYSCFG_CFGR2_FWDISEN);
  178. }
  179. /**
  180. * @brief Enable FIREWALL pre arm.
  181. * @note When FPA bit is set, any code executed outside the protected segment
  182. * will close the Firewall.
  183. * @note This API provides the same service as __HAL_FIREWALL_PREARM_ENABLE() macro
  184. * but can't be executed inside a code area protected by the Firewall.
  185. * @note When the Firewall is disabled, user can resort to HAL_FIREWALL_EnablePreArmFlag() API any time.
  186. * @note When the Firewall is enabled and NVDSL register is equal to 0 (that is,
  187. * when the non volatile data segment is not defined),
  188. * ** this API can be executed when the Firewall is closed
  189. * ** when the Firewall is opened, user should resort to
  190. * __HAL_FIREWALL_PREARM_ENABLE() macro instead
  191. * @note When the Firewall is enabled and NVDSL register is different from 0
  192. * (that is, when the non volatile data segment is defined)
  193. * ** FW_CR register can be accessed only when the Firewall is opened:
  194. * user should resort to __HAL_FIREWALL_PREARM_ENABLE() macro instead.
  195. * @retval None
  196. */
  197. void HAL_FIREWALL_EnablePreArmFlag(void)
  198. {
  199. /* Set FPA bit */
  200. SET_BIT(FIREWALL->CR, FW_CR_FPA);
  201. }
  202. /**
  203. * @brief Disable FIREWALL pre arm.
  204. * @note When FPA bit is reset, any code executed outside the protected segment
  205. * when the Firewall is opened will generate a system reset.
  206. * @note This API provides the same service as __HAL_FIREWALL_PREARM_DISABLE() macro
  207. * but can't be executed inside a code area protected by the Firewall.
  208. * @note When the Firewall is disabled, user can resort to HAL_FIREWALL_EnablePreArmFlag() API any time.
  209. * @note When the Firewall is enabled and NVDSL register is equal to 0 (that is,
  210. * when the non volatile data segment is not defined),
  211. * ** this API can be executed when the Firewall is closed
  212. * ** when the Firewall is opened, user should resort to
  213. * __HAL_FIREWALL_PREARM_DISABLE() macro instead
  214. * @note When the Firewall is enabled and NVDSL register is different from 0
  215. * (that is, when the non volatile data segment is defined)
  216. * ** FW_CR register can be accessed only when the Firewall is opened:
  217. * user should resort to __HAL_FIREWALL_PREARM_DISABLE() macro instead.
  218. * @retval None
  219. */
  220. void HAL_FIREWALL_DisablePreArmFlag(void)
  221. {
  222. /* Clear FPA bit */
  223. CLEAR_BIT(FIREWALL->CR, FW_CR_FPA);
  224. }
  225. /**
  226. * @}
  227. */
  228. /**
  229. * @}
  230. */
  231. /**
  232. * @}
  233. */
  234. #endif /* HAL_FIREWALL_MODULE_ENABLED */
  235. /**
  236. * @}
  237. */
  238. #endif /* #if !defined (STM32L010xB) && !defined (STM32L010x8) && !defined (STM32L010x6) && !defined (STM32L010x4) && !defined (STM32L011xx) && !defined (STM32L021xx) && !defined (STM32L031xx) && !defined (STM32L041xx) */
  239. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/