1 /*
2  * Copyright (c) 2006-2023, 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     DMA_Init(DMA0);
56     DMA_Init(DMA1);
57 
58     /* NVIC Configuration */
59 #define NVIC_VTOR_MASK              0x3FFFFF80
60 #ifdef  VECT_TAB_RAM
61     /* Set the Vector Table base location at 0x10000000 */
62     SCB->VTOR  = (0x10000000 & NVIC_VTOR_MASK);
63 #else  /* VECT_TAB_FLASH  */
64 
65 #ifdef PKG_USING_TFM
66     /* Set the Vector Table base location at 0x00020000 when RTT with TF-M*/
67     SCB->VTOR  = (0x00020000 & NVIC_VTOR_MASK);
68 #else
69     /* Set the Vector Table base location at 0x00000000 */
70     SCB->VTOR  = (0x00000000 & NVIC_VTOR_MASK);
71 #endif
72 #endif
73 
74 #ifndef PKG_USING_TFM
75     /* This init has finished in secure side of TF-M  */
76     BOARD_BootClockPLL150M();
77 #endif
78     //BOARD_BootClockFROHF96M();
79 
80     SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
81     /* set pend exception priority */
82     NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
83 
84     /*init uart device*/
85     rt_hw_uart_init();
86 
87 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
88     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
89 #endif
90 
91 #ifdef RT_USING_COMPONENTS_INIT
92     /* initialization board with RT-Thread Components */
93     rt_components_board_init();
94 #endif
95 
96 #ifdef RT_USING_HEAP
97     rt_kprintf("sram heap, begin: 0x%p, end: 0x%p\n", HEAP_BEGIN, HEAP_END);
98     rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
99 #endif
100 }
101 
102 /**
103  * This function will called when memory fault.
104  */
MemManage_Handler(void)105 void MemManage_Handler(void)
106 {
107     extern void HardFault_Handler(void);
108 
109     rt_kprintf("Memory Fault!\n");
110     HardFault_Handler();
111 }
112 
rt_hw_us_delay(rt_uint32_t us)113 void rt_hw_us_delay(rt_uint32_t us)
114 {
115     rt_uint32_t ticks;
116     rt_uint32_t told, tnow, tcnt = 0;
117     rt_uint32_t reload = SysTick->LOAD;
118 
119     ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
120     told = SysTick->VAL;
121     while (1)
122     {
123         tnow = SysTick->VAL;
124         if (tnow != told)
125         {
126             if (tnow < told)
127             {
128                 tcnt += told - tnow;
129             }
130             else
131             {
132                 tcnt += reload - tnow + told;
133             }
134             told = tnow;
135             if (tcnt >= ticks)
136             {
137                 break;
138             }
139         }
140     }
141 }
142