board.c 2.2 KB

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