pal_os_event.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * \copyright
  3. * Copyright(c) 2018, Infineon Technologies AG
  4. * All rights reserved.
  5. *
  6. * This software is provided with terms and conditions as specified in OPTIGA(TM) Trust X Evaluation Kit License Agreement.
  7. * \endcopyright
  8. *
  9. * \author Infineon AG
  10. *
  11. * \file
  12. *
  13. * \brief This file implements the platform abstraction layer APIs for os event/scheduler.
  14. *
  15. * \addtogroup grPAL
  16. * @{
  17. */
  18. /**********************************************************************************************************************
  19. * HEADER FILES
  20. *********************************************************************************************************************/
  21. #include "stdlib.h"
  22. #include "stdio.h"
  23. #include "pal_os_event.h"
  24. #include "nrf_rtc.h"
  25. #include "nrf_drv_rtc.h"
  26. /**********************************************************************************************************************
  27. * MACROS
  28. *********************************************************************************************************************/
  29. /*********************************************************************************************************************
  30. * LOCAL DATA
  31. *********************************************************************************************************************/
  32. /// @cond hidden
  33. /// Callback function when timer elapses
  34. static volatile register_callback callback_registered = NULL;
  35. /// Pointer to store upper layer callback context (For example: Ifx i2c context)
  36. static void * callback_ctx;
  37. const nrf_drv_rtc_t rtc2 = NRF_DRV_RTC_INSTANCE(2);
  38. static nrf_drv_rtc_config_t m_rtc2_config = NRF_DRV_RTC_DEFAULT_CONFIG;
  39. // Tick count for pal_os_timer
  40. volatile uint32_t g_tick_count = 0;
  41. /**
  42. * Timer callback handler.
  43. *
  44. * This get called from the TIMER elapse event.<br>
  45. * Once the timer expires, the registered callback funtion gets called from the timer event handler, if
  46. * the call back is not NULL.<br>
  47. *
  48. *\param[in] args Callback argument
  49. *
  50. */
  51. static void ifx_rtc_handler(nrf_drv_rtc_int_type_t int_type)
  52. {
  53. volatile register_callback callback;
  54. if (int_type == NRF_DRV_RTC_INT_TICK)
  55. {
  56. g_tick_count++;
  57. }
  58. if (int_type == NRF_DRV_RTC_INT_COMPARE0)
  59. {
  60. nrf_drv_rtc_cc_disable(&rtc2, 0);
  61. if (callback_registered != NULL)
  62. {
  63. callback = callback_registered;
  64. callback_registered = NULL;
  65. callback(callback_ctx);
  66. }
  67. }
  68. }
  69. /// @endcond
  70. void pal_os_event_init()
  71. {
  72. // Initialize the RTC2 driver instance
  73. APP_ERROR_CHECK(nrf_drv_rtc_init(&rtc2, &m_rtc2_config, ifx_rtc_handler));
  74. // Set the prescaler to approximately get 1 ms intervals
  75. m_rtc2_config.prescaler = 31;
  76. // Enable tick event and interrupt
  77. nrf_drv_rtc_tick_enable(&rtc2, true);
  78. // Power on RTC instance
  79. nrf_drv_rtc_enable(&rtc2);
  80. }
  81. /**
  82. * Platform specific event call back registration function to trigger once when timer expires.
  83. * <br>
  84. *
  85. * <b>API Details:</b>
  86. * This function registers the callback function supplied by the caller.<br>
  87. * It triggers a timer with the supplied time interval in microseconds.<br>
  88. * Once the timer expires, the registered callback function gets called.<br>
  89. *
  90. * \param[in] callback Callback function pointer
  91. * \param[in] callback_args Callback arguments
  92. * \param[in] time_us time in micro seconds to trigger the call back
  93. *
  94. */
  95. void pal_os_event_register_callback_oneshot(register_callback callback,
  96. void* callback_args,
  97. uint32_t time_us)
  98. {
  99. callback_registered = callback;
  100. callback_ctx = callback_args;
  101. // Clear the counter
  102. nrf_drv_rtc_counter_clear(&rtc2);
  103. // Set the compare register to trigger approximately at time_us
  104. APP_ERROR_CHECK(nrf_drv_rtc_cc_set(&rtc2, 0, (time_us / 1024) + 1 , true));
  105. }
  106. /**
  107. * @}
  108. */