1 /*
2  * Copyright (c) 2006-2024 RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2022-07-1      Rbb666       first version
9  */
10 
11 #include "drv_common.h"
12 
13 #ifdef RT_USING_SERIAL
14     #include "drv_uart.h"
15 #endif
16 
17 #define DBG_TAG "drv_common"
18 #define DBG_LVL DBG_INFO
19 #include <rtdbg.h>
20 
21 #ifdef RT_USING_FINSH
22 #include <finsh.h>
reboot(uint8_t argc,char ** argv)23 static void reboot(uint8_t argc, char **argv)
24 {
25     rt_hw_cpu_reset();
26 }
27 MSH_CMD_EXPORT(reboot, Reboot System);
28 #endif /* RT_USING_FINSH */
29 
30 /**
31  * this is the timer interrupt service routine.
32  */
SysTick_Handler_CB(void)33 void SysTick_Handler_CB(void)
34 {
35     /* enter interrupt */
36     rt_interrupt_enter();
37 
38     rt_tick_increase();
39 
40     /* leave interrupt */
41     rt_interrupt_leave();
42 }
43 
44 /* systick configuration */
rt_hw_systick_init(void)45 void rt_hw_systick_init(void)
46 {
47     Cy_SysTick_Init(CY_SYSTICK_CLOCK_SOURCE_CLK_CPU, SystemCoreClock / RT_TICK_PER_SECOND);
48     Cy_SysTick_SetCallback(0, SysTick_Handler_CB);
49     Cy_SysTick_EnableInterrupt();
50 }
51 
52 /**
53  * @brief  this function is executed in case of error occurrence.
54  * @param  none
55  * @retval none
56  */
_Error_Handler(char * s,int num)57 void _Error_Handler(char *s, int num)
58 {
59     /* User can add his own implementation to report the HAL error return state */
60     LOG_E("Error_Handler at file:%s num:%d", s, num);
61 
62     while (1)
63     {
64     }
65 }
66 
67 /**
68  * this function will delay for some us.
69  *
70  * @param us the delay time of us
71  */
rt_hw_us_delay(rt_uint32_t us)72 void rt_hw_us_delay(rt_uint32_t us)
73 {
74     rt_uint32_t start, now, delta, reload, us_tick;
75     start = SysTick->VAL;
76     reload = SysTick->LOAD;
77     us_tick = SystemCoreClock / 1000000UL;
78 
79     do
80     {
81         now = SysTick->VAL;
82         delta = start > now ? start - now : reload + start - now;
83     }
84     while(delta < us_tick * us);
85 }
86 
87 /**
88  * this function will initial ifx board.
89  */
rt_hw_board_init()90 rt_weak void rt_hw_board_init()
91 {
92     cy_bsp_all_init();
93 
94     /* systick init */
95     rt_hw_systick_init();
96 
97     /* heap initialization */
98     #if defined(RT_USING_HEAP)
99     rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
100     #endif
101 
102     /* pin driver initialization is open by default */
103     #ifdef RT_USING_PIN
104     rt_hw_pin_init();
105     #endif
106 
107     /* usart driver initialization is open by default */
108     #ifdef RT_USING_SERIAL
109     rt_hw_uart_init();
110     #endif
111 
112     /* set the shell console output device */
113     #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
114     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
115     #endif
116 
117     /* board underlying hardware initialization */
118     #ifdef RT_USING_COMPONENTS_INIT
119     rt_components_board_init();
120     #endif
121 }
122