[DemoCode下载] M058S看门狗超时中断

[复制链接]
1705|10
 楼主| heisexingqisi 发表于 2017-1-31 09:46 | 显示全部楼层 |阅读模式
本帖最后由 heisexingqisi 于 2017-1-31 09:47 编辑
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 3 $
  5. * $Date: 15/02/06 10:23a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Implement periodic WDT time-out interrupt event.
  7. * @note
  8. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "M058S.h"

  12. #define PLL_CLOCK           50000000


  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* Global Interface Variables Declarations                                                                 */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. volatile uint8_t g_u8IsWDTTimeoutINT = 0;


  17. /**
  18. * [url=home.php?mod=space&uid=247401]@brief[/url]       IRQ Handler for WDT and WWDT Interrupt
  19. *
  20. * @param       None
  21. *
  22. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  23. *
  24. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The WDT_IRQHandler is default IRQ of WDT and WWDT, declared in startup_M058S.s.
  25. */
  26. void WDT_IRQHandler(void)
  27. {
  28.     if(WDT_GET_TIMEOUT_INT_FLAG() == 1)
  29.     {
  30.         /* Clear WDT time-out interrupt flag */
  31.         WDT_CLEAR_TIMEOUT_INT_FLAG();

  32.         g_u8IsWDTTimeoutINT = 1;
  33.     }
  34. }

  35. void SYS_Init(void)
  36. {
  37.     /*---------------------------------------------------------------------------------------------------------*/
  38.     /* Init System Clock                                                                                       */
  39.     /*---------------------------------------------------------------------------------------------------------*/
  40.     /* Enable HIRC 22.1184MHz clock */
  41.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  42.     /* Waiting for HIRC clock ready */
  43.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  44.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  45.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  46.     /* Enable external XTAL 12MHz and LIRC 10KHz clock */
  47.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk);

  48.     /* Waiting for external XTAL and LIRC clock ready */
  49.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_OSC10K_STB_Msk);

  50.     /* Set core clock as PLL_CLOCK from PLL */
  51.     CLK_SetCoreClock(PLL_CLOCK);

  52.     /* Enable peripheral clock */
  53.     CLK_EnableModuleClock(UART0_MODULE);
  54.     CLK_EnableModuleClock(WDT_MODULE);

  55.     /* Peripheral clock source */
  56.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
  57.     CLK_SetModuleClock(WDT_MODULE, CLK_CLKSEL1_WDT_S_LIRC, 0);

  58.     /*---------------------------------------------------------------------------------------------------------*/
  59.     /* Init I/O Multi-function                                                                                 */
  60.     /*---------------------------------------------------------------------------------------------------------*/
  61.     /* Set P3 multi-function pins for UART0 RXD, TXD */
  62.     SYS->P3_MFP = SYS_MFP_P30_RXD | SYS_MFP_P31_TXD;
  63. }

  64. void UART0_Init(void)
  65. {
  66.     /*---------------------------------------------------------------------------------------------------------*/
  67.     /* Init UART                                                                                               */
  68.     /*---------------------------------------------------------------------------------------------------------*/
  69.     /* Reset IP */
  70.     SYS_ResetModule(UART0_RST);

  71.     /* Configure UART0 and set UART0 Baudrate */
  72.     UART_Open(UART0, 115200);
  73. }

  74. /*---------------------------------------------------------------------------------------------------------*/
  75. /*  MAIN function                                                                                          */
  76. /*---------------------------------------------------------------------------------------------------------*/
  77. int main(void)
  78. {
  79.     uint32_t u32IntCnts = 0;

  80.     /* Unlock protected registers */
  81.     SYS_UnlockReg();

  82.     /* Init System, peripheral clock and multi-function I/O */
  83.     SYS_Init();

  84.     /* Init UART0 for printf */
  85.     UART0_Init();

  86.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  87.     printf("+---------------------------------------+\n");
  88.     printf("|    WDT Time-out Period Sample Code    |\n");
  89.     printf("+---------------------------------------+\n\n");

  90.     printf("# WDT Settings:\n");
  91.     printf("  Clock source is 10 kHz; Enable interrupt; Time-out interval is 2^14 * WDT clock.\n");
  92.     printf("# When WDT statr counting, system will generate a WDT time-out interrupt after 1.6384 ~ 1.7408 s.\n");
  93.     printf("  Measure P0.0 period time to check time-out interval.\n\n");

  94.     /* Use P0.0 to check time-out period time */
  95.     GPIO_SetMode(P0, 0, GPIO_PMD_OUTPUT);
  96.     P00 = 1;
  97.     P00 = 0;

  98.     /* Because of all bits can be written in WDT Control Register are write-protected;
  99.        To program it needs to disable register protection first. */
  100.     SYS_UnlockReg();

  101.     /* Select WDT time-out interval to 2^14 * WDT clock then start WDT counting */
  102.     g_u8IsWDTTimeoutINT = 0;
  103.     WDT_Open(WDT_TIMEOUT_2POW14, 0, FALSE, FALSE);

  104.     /* Enable WDT interrupt function */
  105.     WDT_EnableInt();

  106.     /* Enable WDT NVIC */
  107.     NVIC_EnableIRQ(WDT_IRQn);

  108.     while(1)
  109.     {
  110.         /* Check if WDT time-out interrupt occurred or not */
  111.         while(g_u8IsWDTTimeoutINT == 0);

  112.         g_u8IsWDTTimeoutINT = 0;
  113.         P00 ^= 1;

  114.         printf("WDT time-out interrupt occurred. INT counts: %d      \r", ++u32IntCnts);
  115.     }
  116. }

  117. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


 楼主| heisexingqisi 发表于 2017-1-31 09:47 | 显示全部楼层
当到时后,你不喂狗,就中断了。
 楼主| heisexingqisi 发表于 2017-1-31 09:48 | 显示全部楼层
void WDT_IRQHandler(void)
{
    if(WDT_GET_TIMEOUT_INT_FLAG() == 1)
    {
        /* Clear WDT time-out interrupt flag */
        WDT_CLEAR_TIMEOUT_INT_FLAG();

        g_u8IsWDTTimeoutINT = 1;
    }
}
其实这个中断处理函数才是用户的,其他那些是通用。。
 楼主| heisexingqisi 发表于 2017-1-31 09:48 | 显示全部楼层
进入中断后,调用这个中断处理函数,如果是超时中断了,就清除标志,然后在这个全局变量置位,让系统知道是哪儿中断了
mintspring 发表于 2017-2-7 13:55 | 显示全部楼层
单片机上的各种定时器都用的很好。
598330983 发表于 2017-2-7 17:32 | 显示全部楼层
照着代码抄几次,就熟悉了。
huangcunxiake 发表于 2017-2-7 22:54 | 显示全部楼层
用看门狗触发一个变量改变。
稳稳の幸福 发表于 2017-2-8 23:02 | 显示全部楼层
WDT_Open(WDT_TIMEOUT_2POW14, 0, FALSE, FALSE);
 楼主| heisexingqisi 发表于 2017-2-9 19:02 | 显示全部楼层
实际上就那么几个函数就实现了
 楼主| heisexingqisi 发表于 2017-2-9 19:07 | 显示全部楼层
  1. /**************************************************************************//**
  2. * @file     wdt.h
  3. * @version  V3.00
  4. * $Revision: 3 $
  5. * $Date: 15/04/08 5:58p $
  6. * @brief    WDT driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __WDT_H__
  12. #define __WDT_H__

  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif


  17. /** @addtogroup Standard_Driver Standard Driver
  18.   @{
  19. */

  20. /** @addtogroup WDT_Driver WDT Driver
  21.   @{
  22. */

  23. /** @addtogroup WDT_EXPORTED_CONSTANTS WDT Exported Constants
  24.   @{
  25. */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. /* WTCR Constants Definitions                                                                              */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. #define WDT_TIMEOUT_2POW4           (0UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^4 * WDT clocks */
  30. #define WDT_TIMEOUT_2POW6           (1UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^6 * WDT clocks */
  31. #define WDT_TIMEOUT_2POW8           (2UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^8 * WDT clocks */
  32. #define WDT_TIMEOUT_2POW10          (3UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^10 * WDT clocks */
  33. #define WDT_TIMEOUT_2POW12          (4UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^12 * WDT clocks */
  34. #define WDT_TIMEOUT_2POW14          (5UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^14 * WDT clocks */
  35. #define WDT_TIMEOUT_2POW16          (6UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^16 * WDT clocks */
  36. #define WDT_TIMEOUT_2POW18          (7UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^18 * WDT clocks */

  37. /*@}*/ /* end of group WDT_EXPORTED_CONSTANTS */


  38. /** @addtogroup WDT_EXPORTED_FUNCTIONS WDT Exported Functions
  39.   @{
  40. */

  41. /**
  42.   * @brief      Clear WDT Reset System Flag
  43.   *
  44.   * @param      None
  45.   *
  46.   * @return     None
  47.   *
  48.   * @details    This macro clears WDT time-out reset system flag.
  49.   */
  50. #define WDT_CLEAR_RESET_FLAG()          (WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTIF_Msk | WDT_WTCR_WTWKF_Msk)) | WDT_WTCR_WTRF_Msk)

  51. /**
  52.   * @brief      Clear WDT Time-out Interrupt Flag
  53.   *
  54.   * @param      None
  55.   *
  56.   * @return     None
  57.   *
  58.   * @details    This macro clears WDT time-out interrupt flag.
  59.   */
  60. #define WDT_CLEAR_TIMEOUT_INT_FLAG()    (WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTRF_Msk | WDT_WTCR_WTWKF_Msk)) | WDT_WTCR_WTIF_Msk)

  61. /**
  62.   * @brief      Clear WDT Wake-up Flag
  63.   *
  64.   * @param      None
  65.   *
  66.   * @return     None
  67.   *
  68.   * @details    This macro clears WDT time-out wake-up system flag.
  69.   */
  70. #define WDT_CLEAR_TIMEOUT_WAKEUP_FLAG() (WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTRF_Msk | WDT_WTCR_WTIF_Msk)) | WDT_WTCR_WTWKF_Msk)

  71. /**
  72.   * @brief      Get WDT Time-out Reset Flag
  73.   *
  74.   * @param      None
  75.   *
  76.   * @retval     0   WDT time-out reset system did not occur
  77.   * @retval     1   WDT time-out reset system occurred
  78.   *
  79.   * @details    This macro indicates system has been reset by WDT time-out reset or not.
  80.   */
  81. #define WDT_GET_RESET_FLAG()            ((WDT->WTCR & WDT_WTCR_WTRF_Msk)? 1 : 0)

  82. /**
  83.   * @brief      Get WDT Time-out Interrupt Flag
  84.   *
  85.   * @param      None
  86.   *
  87.   * @retval     0   WDT time-out interrupt did not occur
  88.   * @retval     1   WDT time-out interrupt occurred
  89.   *
  90.   * @details    This macro indicates WDT time-out interrupt occurred or not.
  91.   */
  92. #define WDT_GET_TIMEOUT_INT_FLAG()      ((WDT->WTCR & WDT_WTCR_WTIF_Msk)? 1 : 0)

  93. /**
  94.   * @brief      Get WDT Time-out Wake-up Flag
  95.   *
  96.   * @param      None
  97.   *
  98.   * @retval     0   WDT time-out interrupt does not cause CPU wake-up
  99.   * @retval     1   WDT time-out interrupt event cause CPU wake-up
  100.   *
  101.   * @details    This macro indicates WDT time-out interrupt event has waked up system or not.
  102.   */
  103. #define WDT_GET_TIMEOUT_WAKEUP_FLAG()   ((WDT->WTCR & WDT_WTCR_WTWKF_Msk)? 1 : 0)

  104. /**
  105.   * @brief      Reset WDT Counter
  106.   *
  107.   * @param      None
  108.   *
  109.   * @return     None
  110.   *
  111.   * @details    This macro is used to reset the internal 18-bit WDT up counter value.
  112.   * @note       If WDT is activated and enabled to reset system, user must reset WDT counter \n
  113.   *             before WDT time-out plus reset delay reached. Or WDT generate a reset signal.
  114.   */
  115. #define WDT_RESET_COUNTER()             (WDT->WTCR  = (WDT->WTCR & ~(WDT_WTCR_WTIF_Msk | WDT_WTCR_WTWKF_Msk | WDT_WTCR_WTRF_Msk)) | WDT_WTCR_WTR_Msk)

  116. /**
  117.   * @brief      Stop WDT Counting
  118.   *
  119.   * @param      None
  120.   *
  121.   * @return     None
  122.   *
  123.   * @details    This function will stop WDT counting and disable WDT module.
  124.   */
  125. static __INLINE void WDT_Close(void)
  126. {
  127.     WDT->WTCR = 0;
  128.     return;
  129. }

  130. /**
  131.   * @brief      Enable WDT Time-out Interrupt
  132.   *
  133.   * @param      None
  134.   *
  135.   * @return     None
  136.   *
  137.   * @details    This function will enable the WDT time-out interrupt function.
  138.   */
  139. static __INLINE void WDT_EnableInt(void)
  140. {
  141.     WDT->WTCR |= WDT_WTCR_WTIE_Msk;
  142.     return;
  143. }

  144. /**
  145.   * @brief      Disable WDT Time-out Interrupt
  146.   *
  147.   * @param      None
  148.   *
  149.   * @return     None
  150.   *
  151.   * @details    This function will disable the WDT time-out interrupt function.
  152.   */
  153. static __INLINE void WDT_DisableInt(void)
  154. {
  155.     // Do not touch write 1 clear bits
  156.     WDT->WTCR &= ~(WDT_WTCR_WTIE_Msk | WDT_WTCR_WTRF_Msk | WDT_WTCR_WTIF_Msk | WDT_WTCR_WTWKF_Msk);
  157.     return;
  158. }

  159. void WDT_Open(uint32_t u32TimeoutInterval, uint32_t u32ResetDelay, uint32_t u32EnableReset, uint32_t u32EnableWakeup);

  160. /*@}*/ /* end of group WDT_EXPORTED_FUNCTIONS */

  161. /*@}*/ /* end of group WDT_Driver */

  162. /*@}*/ /* end of group Standard_Driver */

  163. #ifdef __cplusplus
  164. }
  165. #endif

  166. #endif //__WDT_H__

  167. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


 楼主| heisexingqisi 发表于 2017-2-9 19:08 | 显示全部楼层
/**************************************************************************//**
* @file     wdt.c
* @version  V3.00
* $Revision: 4 $
* $Date: 15/04/08 5:58p $
* @brief    WDT driver source file
*
* @note
* Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include "M058S.h"


/** @addtogroup Standard_Driver Standard Driver
  @{
*/

/** @addtogroup WDT_Driver WDT Driver
  @{
*/

/** @addtogroup WDT_EXPORTED_FUNCTIONS WDT Exported Functions
  @{
*/

/**
  * @brief      Initialize WDT and start counting
  *
  * @param[in]  u32TimeoutInterval  Time-out interval period of WDT module. Valid values are:
  *                                 - \ref WDT_TIMEOUT_2POW4
  *                                 - \ref WDT_TIMEOUT_2POW6
  *                                 - \ref WDT_TIMEOUT_2POW8
  *                                 - \ref WDT_TIMEOUT_2POW10
  *                                 - \ref WDT_TIMEOUT_2POW12
  *                                 - \ref WDT_TIMEOUT_2POW14
  *                                 - \ref WDT_TIMEOUT_2POW16
  *                                 - \ref WDT_TIMEOUT_2POW18
  * @param[in]  u32ResetDelay       Reserved. Not support in M058S series.
  * @param[in]  u32EnableReset      Enable WDT time-out reset system function. Valid values are TRUE and FALSE.
  * @param[in]  u32EnableWakeup     Enable WDT time-out wake-up system function. Valid values are TRUE and FALSE.
  *
  * @return     None
  *
  * @details    This function makes WDT module start counting with different time-out interval, reset delay period and choose to \n
  *             enable or disable WDT time-out reset system or wake-up system.
  * @note       Please make sure that Register Write-Protection Function has been disabled before using this function.
  */
void WDT_Open(uint32_t u32TimeoutInterval,
              uint32_t u32ResetDelay,
              uint32_t u32EnableReset,
              uint32_t u32EnableWakeup)
{
    WDT->WTCR = u32TimeoutInterval | WDT_WTCR_WTE_Msk |
                (u32EnableReset << WDT_WTCR_WTRE_Pos) |
                (u32EnableWakeup << WDT_WTCR_WTWKE_Pos);
    return;
}

/*@}*/ /* end of group WDT_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group WDT_Driver */

/*@}*/ /* end of group Standard_Driver */

/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

157

主题

2770

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部