board.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-29 supperthomas first version
  9. *
  10. */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include <nrfx_systick.h>
  14. #include "board.h"
  15. #include "drv_uart.h"
  16. #include <nrfx_clock.h>
  17. /**
  18. * This is the timer interrupt service routine.
  19. *
  20. */
  21. void SysTick_Handler(void)
  22. {
  23. /* enter interrupt */
  24. rt_interrupt_enter();
  25. rt_tick_increase();
  26. /* leave interrupt */
  27. rt_interrupt_leave();
  28. }
  29. static void clk_event_handler(nrfx_clock_evt_type_t event){}
  30. void SysTick_Configuration(void)
  31. {
  32. nrfx_clock_init(clk_event_handler);
  33. nrfx_clock_enable();
  34. nrfx_clock_lfclk_start();
  35. /* Set interrupt priority */
  36. NVIC_SetPriority(SysTick_IRQn, 0xf);
  37. /* Configure SysTick to interrupt at the requested rate. */
  38. nrf_systick_load_set(SystemCoreClock / RT_TICK_PER_SECOND);
  39. nrf_systick_val_clear();
  40. nrf_systick_csr_set(NRF_SYSTICK_CSR_CLKSOURCE_CPU | NRF_SYSTICK_CSR_TICKINT_ENABLE
  41. | NRF_SYSTICK_CSR_ENABLE);
  42. }
  43. /**
  44. * The time delay function.
  45. *
  46. * @param microseconds.
  47. */
  48. void rt_hw_us_delay(rt_uint32_t us)
  49. {
  50. rt_uint32_t ticks;
  51. rt_uint32_t told, tnow, tcnt = 0;
  52. rt_uint32_t reload = SysTick->LOAD;
  53. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  54. told = SysTick->VAL;
  55. while (1)
  56. {
  57. tnow = SysTick->VAL;
  58. if (tnow != told)
  59. {
  60. if (tnow < told)
  61. {
  62. tcnt += told - tnow;
  63. }
  64. else
  65. {
  66. tcnt += reload - tnow + told;
  67. }
  68. told = tnow;
  69. if (tcnt >= ticks)
  70. {
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. void rt_hw_board_init(void)
  77. {
  78. rt_hw_interrupt_enable(0);
  79. // sd_power_dcdc_mode_set(NRF_POWER_DCDC_ENABLE);
  80. /* Activate deep sleep mode */
  81. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  82. SysTick_Configuration();
  83. #if defined(RT_USING_HEAP)
  84. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  85. #endif
  86. #ifdef RT_USING_SERIAL
  87. rt_hw_uart_init();
  88. #endif
  89. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  90. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  91. #endif
  92. #ifdef RT_USING_COMPONENTS_INIT
  93. rt_components_board_init();
  94. #endif
  95. #ifdef BSP_USING_SOFTDEVICE
  96. extern uint32_t Image$$RW_IRAM1$$Base;
  97. uint32_t const *const m_ram_start = &Image$$RW_IRAM1$$Base;
  98. if ((uint32_t)m_ram_start == 0x20000000)
  99. {
  100. rt_kprintf("\r\n using softdevice the RAM couldn't be %p,please use the templete from package\r\n", m_ram_start);
  101. while (1);
  102. }
  103. else
  104. {
  105. rt_kprintf("\r\n using softdevice the RAM at %p\r\n", m_ram_start);
  106. }
  107. #endif
  108. }