Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

192 lines
5.6 KiB

  1. /**
  2. * \file hw_timer.c
  3. *
  4. * \brief Wrapper used by sw_timer utility using ASF timer api's
  5. *
  6. * Copyright (C) 2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. * Modified for use with Atmel START hpl_rtc functions
  43. */
  44. /**************************************** INCLUDES*****************************/
  45. #include <stdint.h>
  46. #include <stdbool.h>
  47. #include <utils_assert.h>
  48. #include <utils.h>
  49. #include <hal_atomic.h>
  50. #include <hpl_irq.h>
  51. #include "board-config.h"
  52. #include "gpio.h"
  53. #include "hw_timer.h"
  54. /**************************************** MACROS*****************************/
  55. //#define USE_HWTMR_DEBUG
  56. #define COMPARE_COUNT_MAX_VALUE ( uint32_t )( -1 )
  57. /**************************************** GLOBALS*****************************/
  58. HwTimerCallback_t HwTimerAlarmCallback = NULL;
  59. HwTimerCallback_t HwTimerOverflowCallback = NULL;
  60. #if defined( USE_HWTMR_DEBUG )
  61. Gpio_t DbgHwTmrPin;
  62. #endif
  63. /************************************** IMPLEMENTATION************************/
  64. /**
  65. * \brief Initializes the hw timer module
  66. */
  67. void HwTimerInit(void)
  68. {
  69. #if defined( USE_HWTMR_DEBUG )
  70. GpioInit( &DbgHwTmrPin, HWTMR_DBG_PIN_0, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
  71. #endif
  72. hri_mclk_set_APBAMASK_RTC_bit(MCLK);
  73. hri_rtcmode0_write_CTRLA_reg(RTC, RTC_MODE0_CTRLA_SWRST);
  74. hri_rtcmode0_wait_for_sync(RTC, RTC_MODE0_SYNCBUSY_SWRST);
  75. hri_rtcmode0_write_CTRLA_reg(RTC, RTC_MODE0_CTRLA_PRESCALER(0) |
  76. RTC_MODE0_CTRLA_COUNTSYNC);
  77. hri_rtcmode0_write_EVCTRL_reg(RTC, RTC_MODE0_EVCTRL_CMPEO0);
  78. hri_rtcmode0_write_COMP_reg(RTC, 0, ( uint32_t )COMPARE_COUNT_MAX_VALUE);
  79. hri_rtcmode0_set_INTEN_CMP0_bit(RTC);
  80. NVIC_EnableIRQ(RTC_IRQn);
  81. hri_rtcmode0_write_COUNT_reg(RTC, 0);
  82. hri_rtcmode0_wait_for_sync(RTC, RTC_MODE0_SYNCBUSY_COUNT);
  83. hri_rtcmode0_set_CTRLA_ENABLE_bit(RTC);
  84. }
  85. /**
  86. * \brief This function is used to set the callback when the hw timer
  87. * expires.
  88. * \param callback Callback to be registered
  89. */
  90. void HwTimerAlarmSetCallback(HwTimerCallback_t callback)
  91. {
  92. HwTimerAlarmCallback = callback;
  93. }
  94. /**
  95. * \brief This function is used to set the callback when the hw timer
  96. * overflows.
  97. * \param callback Callback to be registered
  98. */
  99. void HwTimerOverflowSetCallback(HwTimerCallback_t callback)
  100. {
  101. HwTimerOverflowCallback = callback;
  102. }
  103. /**
  104. * \brief Loads the timeout in terms of ticks into the hardware
  105. * \ticks Time value in terms of timer ticks
  106. */
  107. bool HwTimerLoadAbsoluteTicks(uint32_t ticks)
  108. {
  109. #if defined( USE_HWTMR_DEBUG )
  110. GpioWrite( &DbgHwTmrPin, 1 );
  111. #endif
  112. RTC_CRITICAL_SECTION_ENTER();
  113. hri_rtcmode0_write_COMP_reg(RTC, 0, ticks);
  114. hri_rtcmode0_wait_for_sync(RTC, RTC_MODE0_SYNCBUSY_MASK);
  115. uint32_t current = hri_rtcmode0_read_COUNT_reg(RTC);
  116. RTC_CRITICAL_SECTION_LEAVE();
  117. if((ticks - current - 1) >= (COMPARE_COUNT_MAX_VALUE >> 1)) {
  118. // if difference is more than half of max assume timer has passed
  119. return false;
  120. }
  121. if((ticks - current) < 10) {
  122. // if too close the matching interrupt does not trigger, so handle same as passed
  123. return false;
  124. }
  125. return true;
  126. }
  127. /**
  128. * \brief Gets the absolute time value
  129. * \retval Absolute time in ticks
  130. */
  131. uint32_t HwTimerGetTime(void)
  132. {
  133. hri_rtcmode0_wait_for_sync(RTC, RTC_MODE0_SYNCBUSY_COUNT);
  134. return hri_rtcmode0_read_COUNT_reg(RTC);
  135. }
  136. /**
  137. * \brief Disables the hw timer module
  138. */
  139. void HwTimerDisable(void)
  140. {
  141. }
  142. /**
  143. * \brief Rtc interrupt handler
  144. */
  145. void RTC_Handler(void)
  146. {
  147. /* Read and mask interrupt flag register */
  148. uint16_t flag = hri_rtcmode0_read_INTFLAG_reg(RTC);
  149. if (flag & RTC_MODE0_INTFLAG_CMP0) {
  150. #if defined( USE_HWTMR_DEBUG )
  151. GpioWrite( &DbgHwTmrPin, 0 );
  152. #endif
  153. hri_rtcmode0_clear_interrupt_CMP0_bit(RTC);
  154. if (HwTimerAlarmCallback != NULL) {
  155. HwTimerAlarmCallback();
  156. }
  157. /* Clear interrupt flag */
  158. }
  159. else if ( flag & RTC_MODE0_INTFLAG_OVF) {
  160. hri_rtcmode0_clear_interrupt_OVF_bit(RTC);
  161. if (HwTimerOverflowCallback != NULL) {
  162. HwTimerOverflowCallback();
  163. }
  164. }
  165. }
  166. /* eof hw_timer.c */