-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.c
executable file
·128 lines (105 loc) · 2.76 KB
/
board.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017-07-24 Tanek the first version
* 2018-11-12 Ernest Chen modify copyright
* 2020-03-06 CCHs add support for hifive devB
*/
#include <stdint.h>
#include <rthw.h>
#include <rtthread.h>
#include <riscv-ops.h>
#include <metal/machine.h>
#include <metal/uart.h>
#define CONSOLE_UART (&__metal_dt_serial_10013000.uart)
#define RTC_FREQ (32768UL)
#define TICK_COUNT (RTC_FREQ / RT_TICK_PER_SECOND)
#define MTIME (*((volatile uint64_t *)(METAL_RISCV_CLINT0_0_BASE_ADDRESS + METAL_RISCV_CLINT0_MTIME)))
#define MTIMECMP (*((volatile uint64_t *)(METAL_RISCV_CLINT0_0_BASE_ADDRESS + METAL_RISCV_CLINT0_MTIMECMP_BASE)))
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
#define RT_HEAP_SIZE 1024
static uint32_t rt_heap[RT_HEAP_SIZE]; // heap default size: 4K(1024 * 4)
RT_WEAK void *rt_heap_begin_get(void)
{
return rt_heap;
}
RT_WEAK void *rt_heap_end_get(void)
{
return rt_heap + RT_HEAP_SIZE;
}
#endif
/* fixed misaligned bug for qemu */
void *__wrap_memset(void *s, int c, size_t n)
{
return rt_memset(s, c, n);
}
/* system tick interrupt */
void handle_m_time_interrupt()
{
//clear_csr(mie, METAL_LOCAL_INTERRUPT_TMR);
MTIMECMP = MTIME + TICK_COUNT;
rt_tick_increase();
//set_csr(mie, METAL_LOCAL_INTERRUPT_TMR);
}
//systerm timer init
static void rt_hw_timer_init(void)
{
MTIMECMP = MTIME + TICK_COUNT;
/* enable timer interrupt*/
set_csr(mie, METAL_LOCAL_INTERRUPT_TMR);
}
#if defined(CONSOLE_UART)
void rt_hw_console_init()
{
metal_uart_init(CONSOLE_UART,115200);
}
void rt_hw_console_output(const char *str)
{
rt_size_t i, size;
char c;
size = rt_strlen(str);
for (i = 0; i < size; i++){
c = *(str+i);
if (c == '\n') {
metal_uart_putc(CONSOLE_UART, '\r');
}
metal_uart_putc(CONSOLE_UART, c);
}
}
char rt_hw_console_getchar(void)
{
int ch;
if(metal_uart_getc(CONSOLE_UART,&ch) ==0){
return ch;
}
return -1;
}
#else
void rt_hw_console_init()
{
#pragma message("No console defined");
}
#endif
/**
* This function will initial your board.
*/
void rt_hw_board_init()
{
//initialize console
rt_hw_console_init();
/* initialize hardware interrupt */
rt_hw_interrupt_init();
/* initialize timer0 */
rt_hw_timer_init();
/* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init();
#endif
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}