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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/*
* Copyright (c) 2018, Synopsys, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <drivers/serial.h>
#include "board.h"
#include "inc/arc/arc_timer.h"
#include "inc/arc/arc_exception.h"
#include "inc/embARC_error.h"
#include "mux.h"
#include "dw_uart_obj.h"
static void rt_hw_timer_isr(int vector, void *param)
{
arc_timer_int_clear(BOARD_OS_TIMER_ID);
rt_tick_increase();
}
static void emsk_mux_init(void)
{
MUX_REG *mux_regs;
mux_regs = (MUX_REG *)(PERIPHERAL_BASE|REL_REGBASE_PINMUX);
mux_init(mux_regs);
/**
* + Please refer to corresponding EMSK User Guide for detailed information
* -> Appendix: A Hardware Functional Description
* -> Pmods Configuration summary
* + Set up pin-multiplexer of all PMOD connections
* - PM1 J1: Upper row as UART 0, lower row as SPI Slave
* - PM2 J2: IIC 0 and run/halt signals
* - PM3 J3: GPIO Port A and Port C
* - PM4 J4: IIC 1 and Port D
* - PM5 J5: Upper row as SPI Master, lower row as Port A
* - PM6 J6: Upper row as SPI Master, lower row as Port A
*/
set_pmod_mux(mux_regs, PM1_UR_UART_0 | PM1_LR_SPI_S \
| PM2_I2C_HRI \
| PM3_GPIO_AC \
| PM4_I2C_GPIO_D \
| PM5_UR_SPI_M1 | PM5_LR_GPIO_A \
| PM6_UR_SPI_M0 | PM6_LR_GPIO_A );
/**
* PM1 upper row as UART
* UM4:RXD, UM3:TXD
* UM2:RTS_N, UM1:CTS_N
*/
set_uart_map(mux_regs, 0xe4);
}
static struct rt_serial_device _emsk_uart0; //abstracted serial for RTT
static struct rt_serial_device _emsk_uart1;
static rt_err_t _configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
DEV_UART_PTR uart;
unsigned int id;
int ret;
id = (unsigned int)(serial->parent.user_data);
uart = uart_get_dev(id);
ret = uart->uart_control(UART_CMD_SET_BAUD, (void *)(cfg->baud_rate));
if (ret != E_OK) {
return RT_ERROR;
}
return RT_EOK;
}
static rt_err_t _control(struct rt_serial_device *serial, int cmd, void *arg)
{
DEV_UART_PTR uart;
unsigned int id;
id = (unsigned int)(serial->parent.user_data);
uart = uart_get_dev(id);
switch (cmd) {
case RT_DEVICE_CTRL_CLR_INT:
uart->uart_control(UART_CMD_SET_RXINT, (void *)0);
break;
case RT_DEVICE_CTRL_SET_INT:
uart->uart_control(UART_CMD_SET_RXINT, (void *)1);
break;
case RT_DEVICE_CTRL_SUSPEND:
uart->uart_control(UART_CMD_DIS_DEV, (void *)0);
break;
case RT_DEVICE_CTRL_RESUME:
uart->uart_control(UART_CMD_ENA_DEV, (void *)0);
break;
default:
return RT_ERROR;
break;
}
return RT_EOK;
}
static int _putc(struct rt_serial_device *serial, char c)
{
DEV_UART_PTR uart;
unsigned int id;
int ret;
id = (unsigned int)(serial->parent.user_data);
uart = uart_get_dev(id);
ret = uart->uart_write(&c, 1);
if (ret < 0) {
return -1;
} else {
return 1;
}
}
static int _getc(struct rt_serial_device *serial)
{
DEV_UART_PTR uart;
unsigned int id;
unsigned int data;
int ret;
int rd_avail = 0;
id = (unsigned int)(serial->parent.user_data);
uart = uart_get_dev(id);
uart->uart_control(UART_CMD_GET_RXAVAIL, (void *)(&rd_avail));
if (rd_avail > 0) {
ret = uart->uart_read(&data, 1);
} else {
return -1;
}
if (ret < 0) {
return -1;
} else {
return data;
}
}
static void _emsk_uart0_isr(void *ptr)
{
rt_hw_serial_isr((struct rt_serial_device*)&_emsk_uart0, RT_SERIAL_EVENT_RX_IND);
}
static const struct rt_uart_ops _emsk_uart0_ops =
{
_configure,
_control,
_putc,
_getc,
};
static void _emsk_uart1_isr(void *ptr)
{
rt_hw_serial_isr((struct rt_serial_device*)&_emsk_uart1, RT_SERIAL_EVENT_RX_IND);
}
static const struct rt_uart_ops _emsk_uart1_ops =
{
_configure,
_control,
_putc,
_getc,
};
int rt_hw_uart_init(void)
{
DEV_UART_PTR uart;
struct serial_configure config;
int ret;
config.baud_rate = BAUD_RATE_115200;
config.bit_order = BIT_ORDER_LSB;
config.data_bits = DATA_BITS_8;
config.parity = PARITY_NONE;
config.stop_bits = STOP_BITS_1;
config.invert = NRZ_NORMAL;
config.bufsz = RT_SERIAL_RB_BUFSZ;
_emsk_uart0.ops = &_emsk_uart0_ops;
_emsk_uart0.config = config;
_emsk_uart1.ops = &_emsk_uart1_ops;
_emsk_uart1.config = config;
/* open UART1 for USB-UART interface */
uart = uart_get_dev(DW_UART_1_ID);
/* default format: 8bits, no parity, 1 stop bits */
ret = uart->uart_open(config.baud_rate);
if (ret != E_OPNED && ret != E_OK) {
return RT_ERROR;
}
/* enable rx int */
uart->uart_control(UART_CMD_SET_RXINT, (void *)1);
/* use customized int isr */
uart->uart_control(UART_CMD_SET_RXCB, _emsk_uart1_isr);
uart->uart_control(UART_CMD_SET_RXINT_BUF, NULL);
rt_hw_serial_register(&_emsk_uart1, "uart1",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
(void *)DW_UART_1_ID);
/* open UART0 in PMOD A*/
uart = uart_get_dev(DW_UART_0_ID);
/* default format: 8bits, no parity, 1 stop bits */
ret = uart->uart_open(config.baud_rate);
if (ret != E_OPNED && ret != E_OK) {
return RT_ERROR;
}
/* enable rx int */
uart->uart_control(UART_CMD_SET_RXINT, (void *)1);
/* use customized int isr */
uart->uart_control(UART_CMD_SET_RXCB, _emsk_uart0_isr);
uart->uart_control(UART_CMD_SET_RXINT_BUF, NULL);
rt_hw_serial_register(&_emsk_uart0, "uart0",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
(void *)DW_UART_0_ID);
return RT_EOK;
}
void rt_hw_console_output(const char *str)
{
while(*str != '\0')
{
if (*str == '\n') {
_putc(&_emsk_uart1,'\r');
}
_putc(&_emsk_uart1,*str);
str++;
}
}
int rt_hw_timer_init(void)
{
unsigned int cyc = BOARD_CPU_CLOCK / RT_TICK_PER_SECOND;
int_disable(BOARD_OS_TIMER_INTNO); /* disable os timer interrupt */
arc_timer_stop(BOARD_OS_TIMER_ID);
arc_timer_start(BOARD_OS_TIMER_ID, TIMER_CTRL_IE | TIMER_CTRL_NH, cyc);
int_handler_install(BOARD_OS_TIMER_INTNO, (INT_HANDLER)rt_hw_timer_isr);
int_enable(BOARD_OS_TIMER_INTNO);
return 0;
}
INIT_BOARD_EXPORT(rt_hw_timer_init);
void rt_hw_board_init()
{
emsk_mux_init();
rt_hw_uart_init();
rt_components_board_init();
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
}
|