Guest User

LPC4357 RTC + Wwatchdog

a guest
Mar 13th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.25 KB | None | 0 0
  1. /*
  2.  * @brief RTC example
  3.  *
  4.  * @note
  5.  * Copyright(C) NXP Semiconductors, 2013
  6.  * All rights reserved.
  7.  *
  8.  * @par
  9.  * Software that is described herein is for illustrative purposes only
  10.  * which provides customers with programming information regarding the
  11.  * LPC products.  This software is supplied "AS IS" without any warranties of
  12.  * any kind, and NXP Semiconductors and its licensor disclaim any and
  13.  * all warranties, express or implied, including all implied warranties of
  14.  * merchantability, fitness for a particular purpose and non-infringement of
  15.  * intellectual property rights.  NXP Semiconductors assumes no responsibility
  16.  * or liability for the use of the software, conveys no license or rights under any
  17.  * patent, copyright, mask work right, or any other intellectual property rights in
  18.  * or to any products. NXP Semiconductors reserves the right to make changes
  19.  * in the software without notification. NXP Semiconductors also makes no
  20.  * representation or warranty that such application will be suitable for the
  21.  * specified use without further testing or modification.
  22.  *
  23.  * @par
  24.  * Permission to use, copy, modify, and distribute this software and its
  25.  * documentation is hereby granted, under NXP Semiconductors' and its
  26.  * licensor's relevant copyrights in the software, without fee, provided that it
  27.  * is used in conjunction with NXP Semiconductors microcontrollers.  This
  28.  * copyright, permission, and disclaimer notice must appear in all copies of
  29.  * this code.
  30.  */
  31.  
  32. #include "board.h"
  33.  
  34. /*****************************************************************************
  35.  * Private types/enumerations/variables
  36.  ****************************************************************************/
  37.  
  38. static volatile bool fIntervalReached;
  39. static volatile bool fAlarmTimeMatched;
  40. static volatile bool On0, On1;
  41.  
  42. /*****************************************************************************
  43.  * Public types/enumerations/variables
  44.  ****************************************************************************/
  45.  
  46. /*****************************************************************************
  47.  * Private functions
  48.  ****************************************************************************/
  49.  
  50. /* Gets and shows the current time and date */
  51. static void showTime(RTC_TIME_T *pTime)
  52. {
  53.     DEBUGOUT("Time: %.2d:%.2d:%.2d %.2d/%.2d/%.4d\r\n", pTime->time[RTC_TIMETYPE_HOUR],
  54.              pTime->time[RTC_TIMETYPE_MINUTE],
  55.              pTime->time[RTC_TIMETYPE_SECOND],
  56.              pTime->time[RTC_TIMETYPE_MONTH],
  57.              pTime->time[RTC_TIMETYPE_DAYOFMONTH],
  58.              pTime->time[RTC_TIMETYPE_YEAR]);
  59. }
  60.  
  61. /*****************************************************************************
  62.  * Public functions
  63.  ****************************************************************************/
  64.  
  65. /**
  66.  * @brief   RTC interrupt handler
  67.  * @return  Nothing
  68.  */
  69. void RTC_IRQHandler(void)
  70. {
  71.     uint32_t sec;
  72.  
  73.     /* Toggle heart beat LED for each second field change interrupt */
  74.     if (Chip_RTC_GetIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE)) {
  75.         /* Clear pending interrupt */
  76.         Chip_RTC_ClearIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE);
  77.         On0 = (bool) !On0;
  78.         Board_LED_Set(0, On0);
  79.     }
  80.  
  81.     /* display timestamp every 5 seconds in the background */
  82.     sec = Chip_RTC_GetTime(LPC_RTC, RTC_TIMETYPE_SECOND);
  83.     if (!(sec % 5)) {
  84.         fIntervalReached = true;    /* set flag for background */
  85.     }
  86.  
  87.     /* Check for alarm match */
  88.     if (Chip_RTC_GetIntPending(LPC_RTC, RTC_INT_ALARM)) {
  89.         /* Clear pending interrupt */
  90.         Chip_RTC_ClearIntPending(LPC_RTC, RTC_INT_ALARM);
  91.         fAlarmTimeMatched = true;   /* set alarm handler flag */
  92.     }
  93. }
  94.  
  95. // WATCHDOG CODE #########################
  96. static volatile int wdtFeedState;
  97. static volatile bool On = false, On1 = false;
  98. static uint8_t ch;
  99.  
  100. /* WDT interrupt handler */
  101. static void wdt_handle(void) {
  102.     uint32_t wdtStatus = Chip_WWDT_GetStatus(LPC_WWDT);
  103.  
  104.     On = (bool) !On;
  105.  
  106.     /* The chip will reset before this happens, but if the WDT doesn't
  107.        have WWDT_WDMOD_WDRESET enabled, this will hit once */
  108.     if (wdtStatus & WWDT_WDMOD_WDTOF) {
  109.         /* A watchdog feed didn't occur prior to window timeout */
  110.         Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDTOF);
  111.  
  112.         if (wdtFeedState == 2) {
  113.             Chip_WWDT_Start(LPC_WWDT);  /* Needs restart */
  114.         }
  115.     }
  116.  
  117.     /* Handle warning interrupt */
  118.     if (wdtStatus & WWDT_WDMOD_WDINT) {
  119.         /* A watchdog feed didn't occur prior to warning timeout */
  120.         Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDINT);
  121.  
  122.         if (wdtFeedState == 1) {
  123.             Chip_WWDT_Feed(LPC_WWDT);
  124.         }
  125.     }
  126. }
  127.  
  128. void WDT_IRQHandler(void)
  129. {
  130.     wdt_handle();
  131. }
  132.  
  133. /**
  134.  * @brief   SysTick Interrupt Handler
  135.  * @return  Nothing
  136.  * @note    Systick interrupt handler feeds WWDT
  137.  */
  138. void SysTick_Handler(void)
  139. {
  140.     if (wdtFeedState == 0) {
  141.         On = (bool) !On;
  142.         Chip_WWDT_Feed(LPC_WWDT);
  143.     }
  144. }
  145.  
  146.  
  147. //########################################
  148.  
  149. /**
  150.  * @brief   Main entry point
  151.  * @return  Nothing
  152.  */
  153. int main(void)
  154. {
  155.     RTC_TIME_T FullTime;
  156.  
  157.     SystemCoreClockUpdate();
  158.     Board_Init();
  159.  
  160.     fIntervalReached  = 0;
  161.     fAlarmTimeMatched = 0;
  162.     On0 = On1 = false;
  163.     Board_LED_Set(2, false);
  164.  
  165.     DEBUGSTR("The RTC operates on a 1 Hz clock.\r\n" \
  166.              "Register writes can take up to 2 cycles.\r\n" \
  167.              "It will take a few seconds to fully\r\n" \
  168.              "initialize it and start it running.\r\n\r\n");
  169.  
  170.     DEBUGSTR("We'll print a timestamp every 5 seconds.\r\n" \
  171.              "...and another when the alarm occurs.\r\n");
  172.  
  173.     Chip_RTC_Init(LPC_RTC);
  174.  
  175.     /* Set current time for RTC 2:00:00PM, 2012-10-05 */
  176.     FullTime.time[RTC_TIMETYPE_SECOND]  = 0;
  177.     FullTime.time[RTC_TIMETYPE_MINUTE]  = 0;
  178.     FullTime.time[RTC_TIMETYPE_HOUR]    = 14;
  179.     FullTime.time[RTC_TIMETYPE_DAYOFMONTH]  = 5;
  180.     FullTime.time[RTC_TIMETYPE_DAYOFWEEK]   = 5;
  181.     FullTime.time[RTC_TIMETYPE_DAYOFYEAR]   = 279;
  182.     FullTime.time[RTC_TIMETYPE_MONTH]   = 10;
  183.     FullTime.time[RTC_TIMETYPE_YEAR]    = 2012;
  184.  
  185.     Chip_RTC_SetFullTime(LPC_RTC, &FullTime);
  186.  
  187.     /* Set ALARM time for 17 seconds from time */
  188.     FullTime.time[RTC_TIMETYPE_SECOND]  = 17;
  189.     Chip_RTC_SetFullAlarmTime(LPC_RTC, &FullTime);
  190.  
  191.     /* Set the RTC to generate an interrupt on each second */
  192.     Chip_RTC_CntIncrIntConfig(LPC_RTC, RTC_AMR_CIIR_IMSEC, ENABLE);
  193.  
  194.     /* Enable matching for alarm for second, minute, hour fields only */
  195.     Chip_RTC_AlarmIntConfig(LPC_RTC, RTC_AMR_CIIR_IMSEC | RTC_AMR_CIIR_IMMIN | RTC_AMR_CIIR_IMHOUR, ENABLE);
  196.  
  197.     /* Clear interrupt pending */
  198.     Chip_RTC_ClearIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE | RTC_INT_ALARM);
  199.  
  200.     /* Enable RTC interrupt in NVIC */
  201.     NVIC_SetPriority(RTC_IRQn, 7);
  202.     NVIC_EnableIRQ((IRQn_Type) RTC_IRQn);
  203.  
  204.     /* Enable RTC (starts increase the tick counter and second counter register) */
  205.     Chip_RTC_Enable(LPC_RTC, ENABLE);
  206.  
  207.     // WATCHDOG CODE #########################
  208.     /* Watchdog will be fed on each watchdog interrupt */
  209.     wdtFeedState = 0;
  210.  
  211.     /* Initialize WWDT and event router */
  212.     Chip_WWDT_Init(LPC_WWDT);
  213.     Chip_EVRT_Init();
  214.  
  215.     /* Set watchdog feed time constant to 1s */
  216.     Chip_WWDT_SetTimeOut(LPC_WWDT, WDT_OSC / 4);
  217.  
  218.     /* Configure WWDT to reset on timeout */
  219.     Chip_WWDT_SetOption(LPC_WWDT, WWDT_WDMOD_WDRESET);
  220.  
  221.     /* Clear watchdog warning and timeout interrupts */
  222.     Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDTOF | WWDT_WDMOD_WDINT);
  223.  
  224.     /* Start watchdog */
  225.     Chip_WWDT_Start(LPC_WWDT);
  226.  
  227.     /* Setup Systick for a 10Hz tick rate. Systick clock is clocked at
  228.        CPU core clock speed */
  229.     SysTick_Config(Chip_Clock_GetRate(CLK_MX_MXCORE) / 20);
  230.  
  231.     /* Watchdog test options */
  232.     DEBUGOUT("Press '1' to enable watchdog feed on systick interrupt\n\r");
  233.     DEBUGOUT("Press '2' to enable watchdog feed on warning interrupt\n\r");
  234.     DEBUGOUT("Press '3' to disable watchdog feed (will reset device)\n\r");
  235.     DEBUGOUT("Press '4' to switch from WDT interrupt to event router handler\n\r");
  236.     // ###################################
  237.  
  238.     /* Loop forever */
  239.     while (1) {
  240.         if (fIntervalReached) { /* Every 5s */
  241.             fIntervalReached = 0;
  242.  
  243.             On1 = (bool) !On1;
  244.             Board_LED_Set(1, On1);
  245.  
  246.             /* read and display time */
  247.             Chip_RTC_GetFullTime(LPC_RTC, &FullTime);
  248.             showTime(&FullTime);
  249.         }
  250.  
  251.         if (fAlarmTimeMatched) {
  252.             fAlarmTimeMatched = false;
  253.  
  254.             /* announce event */
  255.             DEBUGSTR("ALARM triggered!\r\n");
  256.             Board_LED_Set(2, true);
  257.  
  258.             /* read and display time */
  259.             Chip_RTC_GetFullTime(LPC_RTC, &FullTime);
  260.             showTime(&FullTime);
  261.         }
  262.     }
  263. }
Add Comment
Please, Sign In to add comment