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  * 2009-01-05     Bernard      first implementation
9  * 2014-07-18     ArdaFu       Port to TM4C129X
10  */
11 
12 #include <rthw.h>
13 #include <rtthread.h>
14 #include "board.h"
15 
16 
17 #include "driverlib/interrupt.h"
18 #include "driverlib/sysctl.h"
19 #include "driverlib/systick.h"
20 #include "driverlib/fpu.h"
21 #include "driverlib/rom_map.h"
22 
23 #define SYS_CLOCK_DEFAULT 120000000
24 uint32_t SystemCoreClock;
25 #define FAULT_NMI               2           // NMI fault
26 #define FAULT_HARD              3           // Hard fault
27 #define FAULT_MPU               4           // MPU fault
28 #define FAULT_BUS               5           // Bus fault
29 #define FAULT_USAGE             6           // Usage fault
30 #define FAULT_SVCALL            11          // SVCall
31 #define FAULT_DEBUG             12          // Debug monitor
32 #define FAULT_PENDSV            14          // PendSV
33 #define FAULT_SYSTICK           15          // System Tick
34 
35 /**
36  * This is the timer interrupt service routine.
37  *
38  */
SysTick_Handler(void)39 void SysTick_Handler(void)
40 {
41     /* enter interrupt */
42     rt_interrupt_enter();
43 
44     rt_tick_increase();
45 
46     /* leave interrupt */
47     rt_interrupt_leave();
48 }
49 
50 extern void PendSV_Handler(void);
51 extern void HardFault_Handler(void);
52 
53 /**
54  * This function will initial TM4C129X board.
55  */
rt_hw_board_init()56 void rt_hw_board_init()
57 {
58     //init low level drivers. e.g. cpu uart etc.
59     rt_components_board_init();
60     //init HEAP.
61     #ifdef RT_USING_HEAP
62         rt_system_heap_init(HEAP_BEGIN, HEAP_END);
63     #endif
64     //redirect RTT stdio to CONSOLE device
65     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
66 }
67 
rt_hw_cpu_init(void)68 int rt_hw_cpu_init(void)
69 {
70     MAP_IntMasterDisable();
71     IntRegister(FAULT_HARD, HardFault_Handler);
72     IntRegister(FAULT_PENDSV, PendSV_Handler);
73     IntRegister(FAULT_SYSTICK, SysTick_Handler);
74 
75     // Enable lazy stacking for interrupt handlers.  This allows floating-point
76     // instructions to be used within interrupt handlers, but at the expense of
77     // extra stack usage.
78     MAP_FPULazyStackingEnable();
79 
80     // Set the clocking to run directly from the external crystal/oscillator.
81     // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
82     // crystal on your board.
83     SystemCoreClock = MAP_SysCtlClockFreqSet(
84                 (SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480),
85                 SYS_CLOCK_DEFAULT);
86 
87     MAP_SysTickDisable();
88     MAP_SysTickPeriodSet(SystemCoreClock/ RT_TICK_PER_SECOND - 1);
89     MAP_SysTickIntEnable();
90     MAP_SysTickEnable();
91 
92     return 0;
93 }
94 // rt_hw_cpu_init should be run before any other INIT_BOARD_EXPORT
95 // We use INIT_EXPORT here and set the sequence index to "0.xxxx"
96 INIT_EXPORT(rt_hw_cpu_init, "0.post");
97