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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-11-16 bluebear233 first version
*/
#include <rtconfig.h>
#include <rtdevice.h>
#include <drv_uart.h>
#include "NuMicro.h"
/* Private Define ---------------------------------------------------------------*/
#define USEING_UART0 //Tx:PB13 Rx:PB12
/* Private Typedef --------------------------------------------------------------*/
struct usart
{
rt_serial_t dev;
UART_T *usart_base;
};
typedef struct usart* usart_t;
/* Private functions ------------------------------------------------------------*/
static rt_err_t usart_gpio_configure(struct rt_serial_device *serial);
static rt_err_t usart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
static rt_err_t usart_control(struct rt_serial_device *serial, int cmd, void *arg);
static int usart_send(struct rt_serial_device *serial, char c);
static int usart_receive(struct rt_serial_device *serial);
static void rt_hw_uart_register(usart_t uart, UART_T *uart_base,
char *name);
static void uart_isr(usart_t serial);
/* Private Variables ------------------------------------------------------------*/
static const struct rt_uart_ops m487_uart_ops =
{ usart_configure, usart_control, usart_send, usart_receive,
RT_NULL };
static const struct serial_configure m487_uart_default_config =
RT_SERIAL_CONFIG_DEFAULT;
#ifdef USEING_UART0
struct usart uart0;
#endif
/* Interrupt Handle Funtion ----------------------------------------------------*/
#ifdef USEING_UART0
/* 串口0 中断入口 */
void UART0_IRQHandler(void)
{
uart_isr(&uart0);
}
#endif
/**
* 中断处理函数
*/
static void uart_isr(usart_t serial)
{
// 获取串口基地址
UART_T *uart_base = ((usart_t)serial)->usart_base;
// 获取中断事件
uint32_t u32IntSts= uart_base->INTSTS;
// 接收中断
if(u32IntSts & (UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk))
{
rt_hw_serial_isr(&serial->dev, RT_SERIAL_EVENT_RX_IND);
}
}
/**
* 串口端口配置
*/
static rt_err_t usart_gpio_configure(struct rt_serial_device *serial)
{
// 获取串口基地址
UART_T *uart_base = ((usart_t)serial)->usart_base;
switch((uint32_t)uart_base)
{
case UART0_BASE:
SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk);
SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
break;
default:
rt_kprintf("unknow uart module\n");
RT_ASSERT(0);
}
return RT_EOK;
}
/**
* 串口配置
*/
static rt_err_t usart_configure(struct rt_serial_device *serial,
struct serial_configure *cfg)
{
// 获取串口基地址
UART_T *uart_base = ((usart_t)serial)->usart_base;
uint32_t uart_module = 0;
uint32_t uart_word_len = 0;
uint32_t uart_stop_bit = 0;
uint32_t uart_parity = 0;
IRQn_Type uart_irq_channel = 0;
switch((uint32_t)uart_base)
{
case UART0_BASE:
uart_module = UART0_MODULE;
uart_irq_channel = UART0_IRQn;
break;
default:
rt_kprintf("unknow uart module\n");
RT_ASSERT(0);
}
/* Enable IP clock */
CLK_EnableModuleClock(uart_module);
/* Select IP clock source */
CLK_SetModuleClock(uart_module, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));
/* check baudrate */
RT_ASSERT(cfg->baud_rate != 0);
/* check word len */
switch(cfg->data_bits)
{
case DATA_BITS_5:
uart_word_len = UART_WORD_LEN_5;
break;
case DATA_BITS_6:
uart_word_len = UART_WORD_LEN_6;
break;
case DATA_BITS_7:
uart_word_len = UART_WORD_LEN_7;
break;
case DATA_BITS_8:
uart_word_len = UART_WORD_LEN_8;
break;
default:
rt_kprintf("unsupose data len");
RT_ASSERT(0);
}
/* check stop bit */
switch(cfg->stop_bits)
{
case STOP_BITS_1:
uart_stop_bit = UART_STOP_BIT_1;
break;
case STOP_BITS_2:
uart_stop_bit = UART_STOP_BIT_2;
break;
default:
rt_kprintf("unsupose stop bit");
RT_ASSERT(0);
}
/* check stop bit */
switch(cfg->parity)
{
case PARITY_NONE:
uart_parity = UART_PARITY_NONE;
break;
case PARITY_ODD:
uart_parity = UART_PARITY_ODD;
break;
case PARITY_EVEN:
uart_parity = UART_PARITY_EVEN;
break;
default:
rt_kprintf("unsupose parity");
RT_ASSERT(0);
}
/* Open uart */
{
uint32_t u32UartClkSrcSel=0ul, u32UartClkDivNum=0ul;
uint32_t u32ClkTbl[4] = {__HXT, 0ul, __LXT, __HIRC};
uint32_t u32Baud_Div = 0ul;
if(uart_base == (UART_T*)UART0 )
{
/* Get UART clock source selection */
u32UartClkSrcSel = ((uint32_t)(CLK->CLKSEL1 & CLK_CLKSEL1_UART0SEL_Msk)) >> CLK_CLKSEL1_UART0SEL_Pos;
/* Get UART clock divider number */
u32UartClkDivNum = (CLK->CLKDIV0 & CLK_CLKDIV0_UART0DIV_Msk) >> CLK_CLKDIV0_UART0DIV_Pos;
}
else if(uart_base == (UART_T*)UART1 )
{
/* Get UART clock source selection */
u32UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART1SEL_Msk) >> CLK_CLKSEL1_UART1SEL_Pos;
/* Get UART clock divider number */
u32UartClkDivNum = (CLK->CLKDIV0 & CLK_CLKDIV0_UART1DIV_Msk) >> CLK_CLKDIV0_UART1DIV_Pos;
}
else if(uart_base == (UART_T*)UART2 )
{
/* Get UART clock source selection */
u32UartClkSrcSel = (CLK->CLKSEL3 & CLK_CLKSEL3_UART2SEL_Msk) >> CLK_CLKSEL3_UART2SEL_Pos;
/* Get UART clock divider number */
u32UartClkDivNum = (CLK->CLKDIV4 & CLK_CLKDIV4_UART2DIV_Msk) >> CLK_CLKDIV4_UART2DIV_Pos;
}
else if(uart_base == (UART_T*)UART3 )
{
/* Get UART clock source selection */
u32UartClkSrcSel = (CLK->CLKSEL3 & CLK_CLKSEL3_UART3SEL_Msk) >> CLK_CLKSEL3_UART3SEL_Pos;
/* Get UART clock divider number */
u32UartClkDivNum = (CLK->CLKDIV4 & CLK_CLKDIV4_UART3DIV_Msk) >> CLK_CLKDIV4_UART3DIV_Pos;
}
else if(uart_base == (UART_T*)UART4 )
{
/* Get UART clock source selection */
u32UartClkSrcSel = (CLK->CLKSEL3 & CLK_CLKSEL3_UART4SEL_Msk) >> CLK_CLKSEL3_UART4SEL_Pos;
/* Get UART clock divider number */
u32UartClkDivNum = (CLK->CLKDIV4 & CLK_CLKDIV4_UART4DIV_Msk) >> CLK_CLKDIV4_UART4DIV_Pos;
}
else if(uart_base == (UART_T*)UART5 )
{
/* Get UART clock source selection */
u32UartClkSrcSel = (CLK->CLKSEL3 & CLK_CLKSEL3_UART5SEL_Msk) >> CLK_CLKSEL3_UART5SEL_Pos;
/* Get UART clock divider number */
u32UartClkDivNum = (CLK->CLKDIV4 & CLK_CLKDIV4_UART5DIV_Msk) >> CLK_CLKDIV4_UART5DIV_Pos;
}
/* Select UART function */
uart_base->FUNCSEL = UART_FUNCSEL_UART;
/* Set UART line configuration */
uart_base->LINE = uart_word_len | uart_stop_bit | uart_parity;
/* Set UART Rx and RTS trigger level */
uart_base->FIFO &= ~(UART_FIFO_RFITL_Msk | UART_FIFO_RTSTRGLV_Msk);
/* Get PLL clock frequency if UART clock source selection is PLL */
if(u32UartClkSrcSel == 1ul)
{
u32ClkTbl[u32UartClkSrcSel] = CLK_GetPLLClockFreq();
}
/* Set UART baud rate */
if(cfg->baud_rate != 0ul)
{
u32Baud_Div = UART_BAUD_MODE2_DIVIDER((u32ClkTbl[u32UartClkSrcSel]) / (u32UartClkDivNum + 1ul), cfg->baud_rate);
if(u32Baud_Div > 0xFFFFul)
{
uart_base->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER((u32ClkTbl[u32UartClkSrcSel]) / (u32UartClkDivNum + 1ul), cfg->baud_rate));
}
else
{
uart_base->BAUD = (UART_BAUD_MODE2 | u32Baud_Div);
}
}
}
/* config nvic */
NVIC_EnableIRQ(uart_irq_channel);
/* config gpio */
usart_gpio_configure(serial);
return RT_EOK;
}
/**
* 串口中断控制
*/
static rt_err_t usart_control(struct rt_serial_device *serial,
int cmd, void *arg)
{
rt_err_t result = RT_EOK;
rt_uint32_t flag;
// 获取串口基地址
UART_T *uart_base = ((usart_t)serial)->usart_base;
switch ((uint32_t) arg)
{
case RT_DEVICE_FLAG_INT_RX:
flag = UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk | UART_INTEN_TOCNTEN_Msk;
switch (cmd)
{
case RT_DEVICE_CTRL_CLR_INT:
UART_DISABLE_INT(uart_base, flag);
break;
case RT_DEVICE_CTRL_SET_INT:
UART_ENABLE_INT(uart_base, flag);
break;
default:
RT_ASSERT(0);
}
break;
// TODO 完善DMA接口
default:
RT_ASSERT(0)
;
}
return result;
}
/**
* 串口发送函数
*/
static int usart_send(struct rt_serial_device *serial, char c)
{
// 获取串口基地址
UART_T *uart_base = ((usart_t)serial)->usart_base;
// 等待FIFO 发送
while(uart_base->FIFOSTS & UART_FIFOSTS_TXFULL_Msk);
// 发送字符
uart_base->DAT = c;
return 1;
}
/**
* 串口接收函数
*/
static int usart_receive(struct rt_serial_device *serial)
{
// 获取串口基地址
UART_T *uart_base = ((usart_t)serial)->usart_base;
// 如果FIFO 为空返回
if(uart_base->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk)
{
return -1;
}
return UART_READ(uart_base);
}
/**
* @brief 串口设备注册
* @param uart : UART设备结构体
* @param uart_base : UART外设基地址
* @param name : UART设备名
* @param tx_dma_channel : UART TX的DMA通道基地址(可选)
*/
static void rt_hw_uart_register(usart_t usart, UART_T * uart_base, char *name)
{
rt_uint32_t flag;
RT_ASSERT(usart != RT_NULL);
RT_ASSERT(uart_base != RT_NULL);
// 没有定义对应的硬件I2C
if (!(uart_base == UART0 || uart_base == UART1 || uart_base == UART2
|| uart_base == UART3 || uart_base == UART4 || uart_base == UART5))
{
RT_ASSERT(0);
}
usart->usart_base = uart_base;
usart->dev.ops = &m487_uart_ops;
usart->dev.config = m487_uart_default_config;
flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
rt_hw_serial_register(&usart->dev, name,
flag, RT_NULL);
}
/**
* 硬件串口注册
*/
int rt_hw_uart_init(void)
{
#ifdef USEING_UART0
rt_hw_uart_register(&uart0, UART0, "uart0");
#endif
return 0;
}
|