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
|
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-02-06 yandld The first version for MCX
* 2024-11-11 hywing add more UART channels
*/
#include <rtdevice.h>
#include "drv_uart.h"
#include "fsl_lpuart.h"
#ifdef RT_USING_SERIAL
#define DBG_TAG "drv.uart"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#if (defined(CPU_MCXA346VLH) || defined(CPU_MCXA346VLL) || defined(CPU_MCXA346VLQ) || defined(CPU_MCXA346VPN) || \
defined(CPU_MCXA366VLH) || defined(CPU_MCXA366VLL) || defined(CPU_MCXA366VLQ) || defined(CPU_MCXA366VPN))
#define MCXA_UART_USE_FRO_LF_DIV
#endif
struct mcx_uart
{
struct rt_serial_device *serial;
LPUART_Type *uart_base;
IRQn_Type irqn;
clock_name_t clock_src;
clock_attach_id_t clock_attach_id;
clock_ip_name_t clock_ip_name;
clock_div_name_t clock_div_name;
char *device_name;
};
static void uart_isr(struct rt_serial_device *serial);
#if defined(BSP_USING_UART0)
struct rt_serial_device serial0;
void LPUART0_IRQHandler(void)
{
uart_isr(&serial0);
}
#endif
#if defined(BSP_USING_UART1)
struct rt_serial_device serial1;
void LPUART1_IRQHandler(void)
{
uart_isr(&serial1);
}
#endif
#if defined(BSP_USING_UART2)
struct rt_serial_device serial2;
void LPUART2_IRQHandler(void)
{
uart_isr(&serial2);
}
#endif
static const struct mcx_uart uarts[] =
{
#ifdef BSP_USING_UART0
{
&serial0,
LPUART0,
LPUART0_IRQn,
kCLOCK_Fro12M,
#if defined(MCXA_UART_USE_FRO_LF_DIV)
kFRO_LF_DIV_to_LPUART0,
#else
kFRO12M_to_LPUART0,
#endif
kCLOCK_GateLPUART0,
kCLOCK_DivLPUART0,
"uart0",
},
#endif
#ifdef BSP_USING_UART1
{
&serial1,
LPUART1,
LPUART1_IRQn,
kCLOCK_Fro12M,
#if defined(MCXA_UART_USE_FRO_LF_DIV)
kFRO_LF_DIV_to_LPUART1,
#else
kFRO12M_to_LPUART1,
#endif
kCLOCK_GateLPUART1,
kCLOCK_DivLPUART1,
"uart1",
},
#endif
#ifdef BSP_USING_UART2
{
&serial2,
LPUART2,
LPUART2_IRQn,
kCLOCK_Fro12M,
#if defined(MCXA_UART_USE_FRO_LF_DIV)
kFRO_LF_DIV_to_LPUART2,
#else
kFRO12M_to_LPUART2,
#endif
kCLOCK_GateLPUART2,
kCLOCK_DivLPUART2,
"uart2",
},
#endif
};
static rt_err_t mcx_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
struct mcx_uart *uart;
lpuart_config_t config;
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
uart = (struct mcx_uart *)serial->parent.user_data;
CLOCK_SetClockDiv(uart->clock_div_name, 1u);
CLOCK_AttachClk(uart->clock_attach_id);
CLOCK_EnableClock(uart->clock_ip_name);
LPUART_GetDefaultConfig(&config);
config.baudRate_Bps = cfg->baud_rate;
switch (cfg->data_bits)
{
case DATA_BITS_7:
config.dataBitsCount = kLPUART_SevenDataBits;
break;
default:
config.dataBitsCount = kLPUART_EightDataBits;
break;
}
config.enableTx = true;
config.enableRx = true;
LPUART_Init(uart->uart_base, &config, CLOCK_GetFreq(uart->clock_src));
return RT_EOK;
}
static rt_err_t mcx_control(struct rt_serial_device *serial, int cmd, void *arg)
{
struct mcx_uart *uart = (struct mcx_uart *)serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
switch (cmd)
{
case RT_DEVICE_CTRL_CLR_INT:
/* disable rx irq */
LPUART_DisableInterrupts(uart->uart_base, kLPUART_RxDataRegFullInterruptEnable);
DisableIRQ(uart->irqn);
break;
case RT_DEVICE_CTRL_SET_INT:
/* enable rx irq */
LPUART_EnableInterrupts(uart->uart_base, kLPUART_RxDataRegFullInterruptEnable);
EnableIRQ(uart->irqn);
break;
}
return RT_EOK;
}
static int mcx_putc(struct rt_serial_device *serial, char ch)
{
struct mcx_uart *uart = (struct mcx_uart *)serial->parent.user_data;
while(!(kLPUART_TxDataRegEmptyFlag & LPUART_GetStatusFlags(uart->uart_base)));
LPUART_WriteByte(uart->uart_base, ch);
return 1;
}
static int mcx_getc(struct rt_serial_device *serial)
{
struct mcx_uart *uart = (struct mcx_uart *)serial->parent.user_data;
if (kLPUART_RxDataRegFullFlag & LPUART_GetStatusFlags(uart->uart_base))
{
return LPUART_ReadByte(uart->uart_base);
}
else
{
return -1;
}
}
/**
* Uart common interrupt process. This need add to uart ISR.
*
* @param serial serial device
*/
static void uart_isr(struct rt_serial_device *serial)
{
struct mcx_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = (struct mcx_uart *) serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
/* enter interrupt */
rt_interrupt_enter();
/* UART in mode Receiver -------------------------------------------------*/
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
/* leave interrupt */
rt_interrupt_leave();
}
static const struct rt_uart_ops mcx_uart_ops =
{
mcx_configure,
mcx_control,
mcx_putc,
mcx_getc,
};
int rt_hw_uart_init(void)
{
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
int i;
for (i = 0; i < sizeof(uarts) / sizeof(uarts[0]); i++)
{
uarts[i].serial->ops = &mcx_uart_ops;
uarts[i].serial->config = config;
/* register UART device */
rt_hw_serial_register(uarts[i].serial, uarts[i].device_name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, (void *)&uarts[i]);
}
return 0;
}
INIT_BOARD_EXPORT(rt_hw_uart_init);
#endif /*BSP_USING_SERIAL */
|