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 
15 #include "board.h"
16 #include "drv_uart.h"
17 #include <nrfx_clock.h>
18 
19 /**
20  * This is the timer interrupt service routine.
21  *
22  */
SysTick_Handler(void)23 void SysTick_Handler(void)
24 {
25     /* enter interrupt */
26     rt_interrupt_enter();
27 
28     rt_tick_increase();
29 
30     /* leave interrupt */
31     rt_interrupt_leave();
32 }
33 
clk_event_handler(nrfx_clock_evt_type_t event)34 static void clk_event_handler(nrfx_clock_evt_type_t event){}
35 
SysTick_Configuration(void)36 void SysTick_Configuration(void)
37 {
38     nrfx_clock_init(clk_event_handler);
39     nrfx_clock_enable();
40     nrfx_clock_lfclk_start();
41     /* Set interrupt priority */
42     NVIC_SetPriority(SysTick_IRQn, 0xf);
43 
44     /* Configure SysTick to interrupt at the requested rate. */
45     nrf_systick_load_set(SystemCoreClock / RT_TICK_PER_SECOND);
46     nrf_systick_val_clear();
47     nrf_systick_csr_set(NRF_SYSTICK_CSR_CLKSOURCE_CPU | NRF_SYSTICK_CSR_TICKINT_ENABLE
48                         | NRF_SYSTICK_CSR_ENABLE);
49 
50 }
51 
52 /**
53  * The time delay function.
54  *
55  * @param microseconds.
56  */
rt_hw_us_delay(rt_uint32_t us)57 void rt_hw_us_delay(rt_uint32_t us)
58 {
59     rt_uint32_t ticks;
60     rt_uint32_t told, tnow, tcnt = 0;
61     rt_uint32_t reload = SysTick->LOAD;
62 
63     ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
64     told = SysTick->VAL;
65     while (1)
66     {
67         tnow = SysTick->VAL;
68         if (tnow != told)
69         {
70             if (tnow < told)
71             {
72                 tcnt += told - tnow;
73             }
74             else
75             {
76                 tcnt += reload - tnow + told;
77             }
78             told = tnow;
79             if (tcnt >= ticks)
80             {
81                 break;
82             }
83         }
84     }
85 }
86 
rt_hw_board_init(void)87 void rt_hw_board_init(void)
88 {
89     rt_hw_interrupt_enable(0);
90     // sd_power_dcdc_mode_set(NRF_POWER_DCDC_ENABLE);
91     /* Activate deep sleep mode */
92     SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
93 
94     SysTick_Configuration();
95 
96 #if defined(RT_USING_HEAP)
97     rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
98 #endif
99 
100 #ifdef RT_USING_SERIAL
101     rt_hw_uart_init();
102 #endif
103 
104 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
105     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
106 #endif
107 
108 #ifdef RT_USING_COMPONENTS_INIT
109     rt_components_board_init();
110 #endif
111 
112 #ifdef BSP_USING_SOFTDEVICE
113     extern uint32_t  Image$$RW_IRAM1$$Base;
114     uint32_t const *const m_ram_start  = &Image$$RW_IRAM1$$Base;
115     if ((uint32_t)m_ram_start == 0x20000000)
116     {
117         rt_kprintf("\r\n using softdevice the RAM couldn't be %p,please use the templete from package\r\n", m_ram_start);
118         while (1);
119     }
120     else
121     {
122         rt_kprintf("\r\n using softdevice the RAM at %p\r\n", m_ram_start);
123     }
124 #endif
125 
126 }
127 
128