1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Change Logs:
8  * Date           Author       Notes
9  * 2009-01-05     Bernard      first implementation
10  * 2010-02-04     Magicoe      ported to LPC17xx
11  * 2010-05-02     Aozima       update CMSIS to 130
12  * 2017-08-02     XiaoYang     porting to LPC54608 bsp
13  * 2019-08-05     Magicoe      porting to LPC55S69-EVK bsp
14  * 2020-01-01     Karl         Add PKG_USING_TFM support
15  */
16 
17 #include <rthw.h>
18 #include <rtthread.h>
19 
20 #include "board.h"
21 #include "clock_config.h"
22 #include "drv_uart.h"
23 
24 /**
25  * This is the timer interrupt service routine.
26  *
27  */
SysTick_Handler(void)28 void SysTick_Handler(void)
29 {
30     /* enter interrupt */
31     rt_interrupt_enter();
32 
33     rt_tick_increase();
34 
35     /* leave interrupt */
36     rt_interrupt_leave();
37 }
38 
39 /**
40  * This function will initial LPC55Sxx board.
41  */
rt_hw_board_init()42 void rt_hw_board_init()
43 {
44     /* Hardware Initialization */
45     BOARD_InitPins();
46 
47     CLOCK_EnableClock(kCLOCK_InputMux);
48 
49     CLOCK_EnableClock(kCLOCK_Gpio0);
50     CLOCK_EnableClock(kCLOCK_Gpio1);
51 
52     GPIO_PortInit(GPIO, 0);
53     GPIO_PortInit(GPIO, 1);
54 
55     /* NVIC Configuration */
56 #define NVIC_VTOR_MASK              0x3FFFFF80
57 #ifdef  VECT_TAB_RAM
58     /* Set the Vector Table base location at 0x10000000 */
59     SCB->VTOR  = (0x10000000 & NVIC_VTOR_MASK);
60 #else  /* VECT_TAB_FLASH  */
61 
62 #ifdef PKG_USING_TFM
63     /* Set the Vector Table base location at 0x00020000 when RTT with TF-M*/
64     SCB->VTOR  = (0x00020000 & NVIC_VTOR_MASK);
65 #else
66     /* Set the Vector Table base location at 0x00000000 */
67     SCB->VTOR  = (0x00000000 & NVIC_VTOR_MASK);
68 #endif
69 #endif
70 
71 #ifndef PKG_USING_TFM
72     /* This init has finished in secure side of TF-M  */
73     BOARD_BootClockFROHF96M();
74 #endif
75     //BOARD_BootClockFROHF96M();
76 
77     SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
78     /* set pend exception priority */
79     NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
80 
81     /*init uart device*/
82     rt_hw_uart_init();
83 
84 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
85     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
86 #endif
87 
88 #ifdef RT_USING_COMPONENTS_INIT
89     /* initialization board with RT-Thread Components */
90     rt_components_board_init();
91 #endif
92 
93 #ifdef RT_USING_HEAP
94     rt_kprintf("sram heap, begin: 0x%p, end: 0x%p\n", HEAP_BEGIN, HEAP_END);
95     rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
96 #endif
97 }
98 
99 /**
100  * This function will called when memory fault.
101  */
MemManage_Handler(void)102 void MemManage_Handler(void)
103 {
104     extern void HardFault_Handler(void);
105 
106     rt_kprintf("Memory Fault!\n");
107     HardFault_Handler();
108 }
109 
rt_hw_us_delay(rt_uint32_t us)110 void rt_hw_us_delay(rt_uint32_t us)
111 {
112     rt_uint32_t ticks;
113     rt_uint32_t told, tnow, tcnt = 0;
114     rt_uint32_t reload = SysTick->LOAD;
115 
116     ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
117     told = SysTick->VAL;
118     while (1)
119     {
120         tnow = SysTick->VAL;
121         if (tnow != told)
122         {
123             if (tnow < told)
124             {
125                 tcnt += told - tnow;
126             }
127             else
128             {
129                 tcnt += reload - tnow + told;
130             }
131             told = tnow;
132             if (tcnt >= ticks)
133             {
134                 break;
135             }
136         }
137     }
138 }
139