Subversion Repositories DashDisplay

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f1xx_hal_iwdg.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.4
  6.   * @date    29-April-2016
  7.   * @brief   IWDG HAL module driver.
  8.   *          This file provides firmware functions to manage the following
  9.   *          functionalities of the Independent Watchdog (IWDG) peripheral:
  10.   *           + Initialization and Configuration functions
  11.   *           + IO operation functions
  12.   *           + Peripheral State functions
  13.   @verbatim
  14. ================================================================================
  15.                     ##### IWDG specific features #####
  16. ================================================================================
  17.     [..]
  18.     (+) The IWDG can be started by either software or hardware (configurable
  19.         through option byte).
  20.     (+) The IWDG is clocked by its own dedicated Low-Speed clock (LSI) and
  21.         thus stays active even if the main clock fails.
  22.     (+) Once the IWDG is started, the LSI is forced ON and cannot be disabled
  23.         (LSI cannot be disabled too), and the counter starts counting down from
  24.         the reset value of 0xFFF. When it reaches the end of count value (0x000)
  25.         a system reset is generated.
  26.     (+) The IWDG counter should be refreshed at regular intervals, otherwise the
  27.         watchdog generates an MCU reset when the counter reaches 0.          
  28.     (+) The IWDG is implemented in the VDD voltage domain that is still functional
  29.         in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
  30.     (+) IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
  31.         reset occurs.
  32.  
  33.     (+) Min-max timeout value at 40KHz (LSI): 0.1us / 26.2s .
  34.         The IWDG timeout may vary due to LSI frequency dispersion. STM32F1xx
  35.         devices provide the capability to measure the LSI frequency (LSI clock
  36.         connected internally to TIM5 CH4 input capture). The measured value
  37.         can be used to have an IWDG timeout with an acceptable accuracy.
  38.         For more information, please refer to the STM32F1xx Reference manual.
  39.         Note: LSI Calibration is only available on: High density, XL-density and Connectivity line devices.
  40.  
  41.                      ##### How to use this driver #####
  42.   ==============================================================================
  43.     [..]
  44.     (+) Use IWDG using HAL_IWDG_Init() function to :
  45.         (++) Enable write access to IWDG_PR, IWDG_RLR.  
  46.         (++) Configure the IWDG prescaler, counter reload value.
  47.              This reload value will be loaded in the IWDG counter each time the counter
  48.              is reloaded, then the IWDG will start counting down from this value.
  49.     (+) Use IWDG using HAL_IWDG_Start() function to :
  50.         (++) Reload IWDG counter with value defined in the IWDG_RLR register.
  51.         (++) Start the IWDG, when the IWDG is used in software mode (no need
  52.              to enable the LSI, it will be enabled by hardware).
  53.     (+) Then the application program must refresh the IWDG counter at regular
  54.         intervals during normal operation to prevent an MCU reset, using
  55.         HAL_IWDG_Refresh() function.    
  56.    
  57.      *** IWDG HAL driver macros list ***
  58.      ====================================
  59.      [..]
  60.        Below the list of most used macros in IWDG HAL driver.
  61.        
  62.       (+) __HAL_IWDG_START: Enable the IWDG peripheral
  63.       (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in the reload register    
  64.       (+) __HAL_IWDG_GET_FLAG: Get the selected IWDG's flag status
  65.  
  66.   @endverbatim
  67.   ******************************************************************************
  68.   * @attention
  69.   *
  70.   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  71.   *
  72.   * Redistribution and use in source and binary forms, with or without modification,
  73.   * are permitted provided that the following conditions are met:
  74.   *   1. Redistributions of source code must retain the above copyright notice,
  75.   *      this list of conditions and the following disclaimer.
  76.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  77.   *      this list of conditions and the following disclaimer in the documentation
  78.   *      and/or other materials provided with the distribution.
  79.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  80.   *      may be used to endorse or promote products derived from this software
  81.   *      without specific prior written permission.
  82.   *
  83.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  84.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  85.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  86.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  87.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  88.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  89.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  90.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  91.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  92.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  93.   *
  94.   ******************************************************************************
  95.   */
  96.  
  97. /* Includes ------------------------------------------------------------------*/
  98. #include "stm32f1xx_hal.h"
  99.  
  100. /** @addtogroup STM32F1xx_HAL_Driver
  101.   * @{
  102.   */
  103.  
  104. /** @defgroup IWDG IWDG
  105.   * @brief IWDG HAL module driver.
  106.   * @{
  107.   */
  108.  
  109. #ifdef HAL_IWDG_MODULE_ENABLED
  110.  
  111. /* Private typedef -----------------------------------------------------------*/
  112. /* Private define ------------------------------------------------------------*/
  113.  
  114. /** @defgroup IWDG_Private_Constants IWDG Private Constants
  115.   * @{
  116.   */
  117.  
  118. #define IWDG_DEFAULT_TIMEOUT (uint32_t)1000
  119.  
  120. /**
  121.   * @}
  122.   */
  123.  
  124. /* Private macro -------------------------------------------------------------*/
  125. /* Private variables ---------------------------------------------------------*/
  126. /* Private function prototypes -----------------------------------------------*/
  127. /* Private functions ---------------------------------------------------------*/
  128.  
  129. /** @defgroup IWDG_Exported_Functions IWDG Exported Functions
  130.   * @{
  131.   */
  132.  
  133. /** @defgroup IWDG_Exported_Functions_Group1 Initialization and de-initialization functions
  134.  *  @brief    Initialization and Configuration functions.
  135.  *
  136. @verbatim    
  137.  ===============================================================================
  138.           ##### Initialization and de-initialization functions #####
  139.  ===============================================================================
  140.     [..]  This section provides functions allowing to:
  141.       (+) Initialize the IWDG according to the specified parameters
  142.           in the IWDG_InitTypeDef and create the associated handle
  143.       (+) Initialize the IWDG MSP
  144.       (+) DeInitialize IWDG MSP
  145.  
  146. @endverbatim
  147.   * @{
  148.   */
  149.  
  150. /**
  151.   * @brief  Initializes the IWDG according to the specified
  152.   *         parameters in the IWDG_InitTypeDef and creates the associated handle.
  153.   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  154.   *                the configuration information for the specified IWDG module.
  155.   * @retval HAL status
  156.   */
  157. HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
  158. {
  159.   /* Check the IWDG handle allocation */
  160.   if(hiwdg == NULL)
  161.   {
  162.     return HAL_ERROR;
  163.   }
  164.  
  165.   /* Check the parameters */
  166.   assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
  167.   assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
  168.   assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
  169.  
  170.   /* Check pending flag, if previous update not done, return error */
  171.   if((__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_PVU) != RESET)
  172.      &&(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET))
  173.   {
  174.     return HAL_ERROR;
  175.   }
  176.    
  177.   if(hiwdg->State == HAL_IWDG_STATE_RESET)
  178.   {  
  179.     /* Allocate lock resource and initialize it */
  180.     hiwdg->Lock = HAL_UNLOCKED;
  181.    
  182.     /* Init the low level hardware */
  183.     HAL_IWDG_MspInit(hiwdg);
  184.   }
  185.  
  186.   /* Change IWDG peripheral state */
  187.   hiwdg->State = HAL_IWDG_STATE_BUSY;  
  188.  
  189.   /* Enable write access to IWDG_PR and IWDG_RLR registers */  
  190.   IWDG_ENABLE_WRITE_ACCESS(hiwdg);
  191.  
  192.   /* Write to IWDG registers the IWDG_Prescaler & IWDG_Reload values to work with */
  193.   MODIFY_REG(hiwdg->Instance->PR, IWDG_PR_PR, hiwdg->Init.Prescaler);
  194.   MODIFY_REG(hiwdg->Instance->RLR, IWDG_RLR_RL, hiwdg->Init.Reload);
  195.  
  196.   /* Change IWDG peripheral state */
  197.   hiwdg->State = HAL_IWDG_STATE_READY;
  198.  
  199.   /* Return function status */
  200.   return HAL_OK;
  201. }
  202.  
  203. /**
  204.   * @brief  Initializes the IWDG MSP.
  205.   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  206.   *                the configuration information for the specified IWDG module.
  207.   * @retval None
  208.   */
  209. __weak void HAL_IWDG_MspInit(IWDG_HandleTypeDef *hiwdg)
  210. {
  211.   /* Prevent unused argument(s) compilation warning */
  212.   UNUSED(hiwdg);
  213.   /* NOTE : This function Should not be modified, when the callback is needed,
  214.             the HAL_IWDG_MspInit could be implemented in the user file
  215.    */
  216. }
  217.  
  218. /**
  219.   * @}
  220.   */
  221.  
  222. /** @defgroup IWDG_Exported_Functions_Group2 IO operation functions  
  223.  *  @brief   IO operation functions  
  224.  *
  225. @verbatim  
  226.  ===============================================================================
  227.                       ##### IO operation functions #####
  228.  ===============================================================================  
  229.     [..]  This section provides functions allowing to:
  230.       (+) Start the IWDG.
  231.       (+) Refresh the IWDG.
  232.  
  233. @endverbatim
  234.   * @{
  235.   */
  236.  
  237. /**
  238.   * @brief  Starts the IWDG.
  239.   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  240.   *                the configuration information for the specified IWDG module.
  241.   * @retval HAL status
  242.   */
  243. HAL_StatusTypeDef HAL_IWDG_Start(IWDG_HandleTypeDef *hiwdg)
  244. {
  245.   /* Process Locked */
  246.   __HAL_LOCK(hiwdg);
  247.  
  248.     /* Change IWDG peripheral state */  
  249.   hiwdg->State = HAL_IWDG_STATE_BUSY;
  250.  
  251.   /* Start the IWDG peripheral */
  252.   __HAL_IWDG_START(hiwdg);
  253.  
  254.     /* Reload IWDG counter with value defined in the RLR register */
  255.   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  256.  
  257.   /* Change IWDG peripheral state */    
  258.   hiwdg->State = HAL_IWDG_STATE_READY;
  259.  
  260.   /* Process Unlocked */
  261.   __HAL_UNLOCK(hiwdg);
  262.  
  263.   /* Return function status */
  264.   return HAL_OK;
  265. }
  266.  
  267. /**
  268.   * @brief  Refreshes the IWDG.
  269.   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  270.   *                the configuration information for the specified IWDG module.
  271.   * @retval HAL status
  272.   */
  273. HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
  274. {
  275.   uint32_t tickstart = 0;
  276.  
  277.   /* Process Locked */
  278.   __HAL_LOCK(hiwdg);
  279.  
  280.     /* Change IWDG peripheral state */
  281.   hiwdg->State = HAL_IWDG_STATE_BUSY;
  282.  
  283.   tickstart = HAL_GetTick();
  284.  
  285.   /* Wait until RVU flag is RESET */
  286.   while(__HAL_IWDG_GET_FLAG(hiwdg, IWDG_FLAG_RVU) != RESET)
  287.   {
  288.     if((HAL_GetTick() - tickstart ) > IWDG_DEFAULT_TIMEOUT)
  289.     {
  290.       /* Set IWDG state */
  291.       hiwdg->State = HAL_IWDG_STATE_TIMEOUT;
  292.  
  293.        /* Process unlocked */
  294.       __HAL_UNLOCK(hiwdg);
  295.  
  296.       return HAL_TIMEOUT;
  297.     }
  298.   }
  299.  
  300.   /* Reload IWDG counter with value defined in the reload register */
  301.   __HAL_IWDG_RELOAD_COUNTER(hiwdg);
  302.    
  303.   /* Change IWDG peripheral state */    
  304.   hiwdg->State = HAL_IWDG_STATE_READY;
  305.  
  306.   /* Process Unlocked */
  307.   __HAL_UNLOCK(hiwdg);
  308.  
  309.   /* Return function status */
  310.   return HAL_OK;
  311. }
  312.  
  313. /**
  314.   * @}
  315.   */
  316.  
  317. /** @defgroup IWDG_Exported_Functions_Group3 Peripheral State functions
  318.  *  @brief    Peripheral State functions.
  319.  *
  320. @verbatim  
  321.  ===============================================================================
  322.                       ##### Peripheral State functions #####
  323.  ===============================================================================  
  324.     [..]
  325.     This subsection permits to get in run-time the status of the peripheral
  326.     and the data flow.
  327.  
  328. @endverbatim
  329.   * @{
  330.   */
  331.  
  332. /**
  333.   * @brief  Returns the IWDG state.
  334.   * @param  hiwdg: pointer to a IWDG_HandleTypeDef structure that contains
  335.   *                the configuration information for the specified IWDG module.
  336.   * @retval HAL state
  337.   */
  338. HAL_IWDG_StateTypeDef HAL_IWDG_GetState(IWDG_HandleTypeDef *hiwdg)
  339. {
  340.   return hiwdg->State;
  341. }
  342.  
  343. /**
  344.   * @}
  345.   */
  346.  
  347. /**
  348.   * @}
  349.   */
  350.  
  351. #endif /* HAL_IWDG_MODULE_ENABLED */
  352. /**
  353.   * @}
  354.   */
  355.  
  356. /**
  357.   * @}
  358.   */
  359.  
  360. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  361.