board.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <nrf51.h>
  14. #include <nrf51_bitfields.h>
  15. #include "board.h"
  16. #include "uart.h"
  17. /**
  18. * @addtogroup NRF51822
  19. */
  20. /*@{*/
  21. #define LFCLK_FREQUENCY (32768UL) /**< LFCLK frequency in Hertz, constant. */
  22. #define RTC_FREQUENCY (800UL) /**< Required RTC working clock RTC_FREQUENCY Hertz. Changable. */
  23. #define COUNTER_PRESCALER ((LFCLK_FREQUENCY / RTC_FREQUENCY) - 1) /* f = LFCLK/(prescaler + 1) */
  24. /** @brief Function starting the internal LFCLK XTAL oscillator.
  25. */
  26. void lfclk_config(void)
  27. {
  28. NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
  29. NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
  30. NRF_CLOCK->TASKS_LFCLKSTART = 1;
  31. while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
  32. {
  33. //Do nothing.
  34. }
  35. NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
  36. }
  37. /** @brief Function for configuring the RTC with TICK to 100Hz.
  38. */
  39. void rtc_config(void)
  40. {
  41. NVIC_EnableIRQ(RTC0_IRQn); // Enable Interrupt for the RTC in the core.
  42. NRF_RTC0->PRESCALER = COUNTER_PRESCALER; // Set prescaler to a TICK of RTC_FREQUENCY.
  43. // Enable TICK event and TICK interrupt:
  44. NRF_RTC0->EVTENSET = RTC_EVTENSET_TICK_Msk;
  45. NRF_RTC0->INTENSET = RTC_INTENSET_TICK_Msk;
  46. }
  47. /** @brief: Function for handling the RTC0 interrupts.
  48. * Triggered on TICK and COMPARE0 match.
  49. */
  50. void RTC0_IRQHandler(void)
  51. {
  52. /* enter interrupt */
  53. rt_interrupt_enter();
  54. if ((NRF_RTC0->EVENTS_TICK != 0) &&
  55. ((NRF_RTC0->INTENSET & RTC_INTENSET_TICK_Msk) != 0))
  56. {
  57. NRF_RTC0->EVENTS_TICK = 0;
  58. rt_tick_increase(); //This function will notify kernel there is one tick passed
  59. }
  60. /* leave interrupt */
  61. rt_interrupt_leave();
  62. }
  63. /**
  64. * This function will initial NRF51822 board.
  65. */
  66. void rt_hw_board_init()
  67. {
  68. lfclk_config();
  69. rtc_config();
  70. NRF_RTC0->TASKS_START = 1;
  71. /* Initial usart deriver, and set console device */
  72. rt_hw_uart_init();
  73. #ifdef RT_USING_CONSOLE
  74. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  75. #endif
  76. }
  77. /*@}*/