rtc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /******************************************************************************
  2. * @brief Real-ETMe counter (RTC) driver source code.
  3. *
  4. ******************************************************************************/
  5. #include "common.h"
  6. #include "rtc.h"
  7. /******************************************************************************
  8. * Global variables
  9. ******************************************************************************/
  10. /******************************************************************************
  11. * Constants and macros
  12. ******************************************************************************/
  13. /******************************************************************************
  14. * Local types
  15. ******************************************************************************/
  16. /******************************************************************************
  17. * Local function prototypes
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * Local variables
  21. ******************************************************************************/
  22. /*!
  23. * @brief global variable to store RTC callbacks.
  24. *
  25. */
  26. RTC_CallbackType RTC_Callback[1] = {(RTC_CallbackType)NULL}; /*!< RTC initial callback */
  27. /******************************************************************************
  28. * Local functions
  29. ******************************************************************************/
  30. void RTC_Isr(void);
  31. /******************************************************************************
  32. * Global functions
  33. ******************************************************************************/
  34. /******************************************************************************
  35. * define RTC APIs
  36. *
  37. *//*! @addtogroup rtc_api_list
  38. * @{
  39. *******************************************************************************/
  40. /*****************************************************************************//*!
  41. *
  42. * @brief inital RTC module
  43. *
  44. * @param[in] pConfig point to configuration
  45. *
  46. * @return none
  47. *
  48. * @ Pass/ Fail criteria: none
  49. *****************************************************************************/
  50. void RTC_Init(RTC_ConfigType *pConfig)
  51. {
  52. uint16_t u16Clocksource, u16Prescler;
  53. uint16_t u16ModVal;
  54. u16Clocksource =0;
  55. u16Prescler =0;
  56. u16ModVal =0;
  57. SIM->SCGC |= SIM_SCGC_RTC_MASK;
  58. u16ModVal = pConfig->u16ModuloValue;
  59. RTC_SetModulo(u16ModVal);
  60. if (pConfig->bRTCOut)
  61. {
  62. RTC->SC= RTC_SC_RTCO_MASK;
  63. }
  64. if (pConfig->bInterruptEn)
  65. {
  66. NVIC_EnableIRQ(RTC_IRQn);
  67. RTC_EnableInt();
  68. }
  69. else
  70. {
  71. NVIC_DisableIRQ(RTC_IRQn);
  72. }
  73. if (pConfig->bFlag)
  74. {
  75. RTC_ClrFlags();
  76. }
  77. u16Clocksource = pConfig->bClockSource;
  78. u16Prescler = pConfig->bClockPresaler;
  79. RTC_SetClock(u16Clocksource,u16Prescler );
  80. }
  81. /*****************************************************************************//*!
  82. *
  83. * @brief set call back function for rtc module
  84. *
  85. * @param[in] pfnCallback point to call back function
  86. *
  87. * @return none
  88. *
  89. * @ Pass/ Fail criteria: none
  90. *****************************************************************************/
  91. void RTC_SetCallback(RTC_CallbackType pfnCallback)
  92. {
  93. RTC_Callback[0] = pfnCallback;
  94. }
  95. /*****************************************************************************//*!
  96. *
  97. * @brief de-initialize rtc module , reset rtc register
  98. *
  99. * @param none
  100. *
  101. * @return none
  102. *
  103. * @ Pass/ Fail criteria: none
  104. *****************************************************************************/
  105. void RTC_DeInit(void)
  106. {
  107. NVIC_DisableIRQ(RTC_IRQn);
  108. RTC->MOD = 0;
  109. while(RTC->MOD);
  110. if(RTC_GetFlags())
  111. {
  112. RTC_ClrFlags();
  113. }
  114. RTC->SC = 0;
  115. while(RTC->SC);
  116. SIM->SCGC &= ~SIM_SCGC_RTC_MASK;
  117. }
  118. /*! @} End of rtc_api_list */
  119. /*****************************************************************************//*!
  120. *
  121. * @brief RTC module interrupt service routine
  122. *
  123. * @param none
  124. *
  125. * @return none
  126. *
  127. * @ Pass/ Fail criteria: none
  128. *****************************************************************************/
  129. void RTC_Isr(void)
  130. {
  131. RTC_ClrFlags();
  132. if (RTC_Callback[0])
  133. {
  134. RTC_Callback[0]();
  135. }
  136. }