Advertisement
mpthompson

os_init.h and os_init.c for RTX on Nordic NRF51822

Jun 18th, 2014
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. os_tick.h
  2. ----------------------------------------------------------------------
  3. #ifndef __OS_TICK_H
  4. #define __OS_TICK_H
  5.  
  6. #include "global.h"
  7.  
  8. int os_tick_init (void);
  9. uint32_t os_tick_val(void);
  10. uint32_t os_tick_ovf(void);
  11. void os_tick_irqack (void);
  12. uint32_t os_tick_get(void);
  13.  
  14. #endif // __OS_TICK_H
  15. ----------------------------------------------------------------------
  16.  
  17.  
  18. os_tick.c
  19. ----------------------------------------------------------------------
  20. #include "global.h"
  21. #include "nrf.h"
  22. #include "nrf_gpio.h"
  23.  
  24. // System tick count.
  25. static volatile uint32_t os_tick_count = 0;
  26.  
  27. // Starting the internal LFCLK XTAL oscillator.
  28. static void lfclk_config(void)
  29. {
  30.   NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
  31.   NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
  32.   NRF_CLOCK->TASKS_LFCLKSTART = 1;
  33.   while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
  34.   NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
  35. }
  36.  
  37. // Initialize alternative hardware timer as RTX kernel timer
  38. // Return: IRQ number of the alternative hardware timer
  39. int os_tick_init (void)
  40. {
  41.   // Start the internal LFCLK XTAL oscillator.
  42.   lfclk_config();
  43.  
  44.   // Set prescaler to a TICK of ~1000Hz.
  45.   NRF_RTC1->PRESCALER = 32;
  46.  
  47.   // Enable TICK event and TICK interrupt:
  48.   NRF_RTC1->EVTENSET = RTC_EVTENSET_TICK_Msk;
  49.   NRF_RTC1->INTENSET = RTC_INTENSET_TICK_Msk;
  50.  
  51.   // Start the clock.
  52.   NRF_RTC1->TASKS_START = 1;
  53.  
  54.   // Return the IRQ number.
  55.   return RTC1_IRQn;
  56. }
  57.  
  58. // Get alternative hardware timer current value (0 .. OS_TRV)
  59. uint32_t os_tick_val(void)
  60. {
  61.   return 0;
  62. }
  63.  
  64. // Get alternative hardware timer overflow flag
  65. // Return: 1 - overflow, 0 - no overflow
  66. uint32_t os_tick_ovf(void)
  67. {
  68.   return 0;
  69. }
  70.  
  71. // Acknowledge alternative hardware timer interrupt
  72. void os_tick_irqack (void)
  73. {
  74.   // Is this a tick condition?
  75.   if ((NRF_RTC1->EVENTS_TICK != 0) && ((NRF_RTC1->INTENSET & RTC_INTENSET_TICK_Msk) != 0))
  76.   {
  77.     // Increment the system tick count.
  78.     ++os_tick_count;
  79.  
  80.     // Clear the interrupt?
  81.     NRF_RTC1->EVENTS_TICK = 0;
  82.   }
  83. }
  84.  
  85. // Return the current OS tick count.
  86. uint32_t os_tick_get(void)
  87. {
  88.   return os_tick_count;
  89. }
  90.  
  91. ---------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement