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 * 2013-05-18 Bernard The first version for LPC40xx
9 * 2014-12-16 RT_learning The first version for LPC5410x
10 * 2017-08-01 XiaoYang The first version for LPC546xx
11 */
12
13
14 #include <rthw.h>
15 #include <rtthread.h>
16 #include <rtdevice.h>
17
18 #include "fsl_usart.h"
19 #include "fsl_common.h"
20 #include "fsl_iocon.h"
21
22 struct lpc_uart
23 {
24 USART_Type *UART;
25 IRQn_Type UART_IRQn;
26 };
27
lpc_configure(struct rt_serial_device * serial,struct serial_configure * cfg)28 static rt_err_t lpc_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
29 {
30 struct lpc_uart *uart;
31 usart_config_t u0_config;
32
33 RT_ASSERT(serial != RT_NULL);
34 uart = (struct lpc_uart *)serial->parent.user_data;
35
36 /*
37 * config.baudRate_Bps = 115200U;
38 * config.parityMode = kUSART_ParityDisabled;
39 * config.stopBitCount = kUSART_OneStopBit;
40 * config.loopback = false;
41 * config.enableTx = false;
42 * config.enableRx = false;
43 */
44 USART_GetDefaultConfig(&u0_config);
45 u0_config.baudRate_Bps = cfg->baud_rate;
46 u0_config.enableTx = true;
47 u0_config.enableRx = true;
48
49 USART_Init(uart->UART, &u0_config, CLOCK_GetFreq(kCLOCK_Flexcomm0));
50
51 return RT_EOK;
52 }
53
lpc_control(struct rt_serial_device * serial,int cmd,void * arg)54 static rt_err_t lpc_control(struct rt_serial_device *serial, int cmd, void *arg)
55 {
56 struct lpc_uart *uart;
57
58 RT_ASSERT(serial != RT_NULL);
59 uart = (struct lpc_uart *)serial->parent.user_data;
60
61 switch (cmd)
62 {
63 case RT_DEVICE_CTRL_CLR_INT:
64 /* disable rx irq */
65 USART_DisableInterrupts(uart->UART, kUSART_RxLevelInterruptEnable);
66 break;
67 case RT_DEVICE_CTRL_SET_INT:
68 /* enable rx irq */
69 USART_EnableInterrupts(uart->UART, kUSART_RxLevelInterruptEnable);
70 break;
71 }
72
73 return RT_EOK;
74 }
75
lpc_putc(struct rt_serial_device * serial,char c)76 static int lpc_putc(struct rt_serial_device *serial, char c)
77 {
78 struct lpc_uart *uart;
79
80 uart = (struct lpc_uart *)serial->parent.user_data;
81
82 while (!(kUSART_TxFifoNotFullFlag & USART_GetStatusFlags(uart->UART)));
83
84 USART_WriteByte(uart->UART, c);
85
86 return 1;
87 }
88
lpc_getc(struct rt_serial_device * serial)89 static int lpc_getc(struct rt_serial_device *serial)
90 {
91 struct lpc_uart *uart;
92
93 uart = (struct lpc_uart *)serial->parent.user_data;
94 if (kUSART_RxFifoNotEmptyFlag & USART_GetStatusFlags(uart->UART))
95 {
96 return USART_ReadByte(uart->UART);
97 }
98 else
99 return -1;
100 }
101
102 static const struct rt_uart_ops lpc_uart_ops =
103 {
104 lpc_configure,
105 lpc_control,
106 lpc_putc,
107 lpc_getc,
108 };
109
110
111 #define IOCON_PIO_DIGITAL_EN 0x0100u /*!< Enables digital function */
112 #define IOCON_PIO_FUNC1 0x01u /*!< Selects pin function 1 */
113 #define IOCON_PIO_INPFILT_OFF 0x0200u /*!< Input filter disabled */
114 #define IOCON_PIO_INV_DI 0x00u /*!< Input function is not inverted */
115 #define IOCON_PIO_MODE_INACT 0x00u /*!< No addition pin function */
116 #define IOCON_PIO_OPENDRAIN_DI 0x00u /*!< Open drain is disabled */
117 #define IOCON_PIO_SLEW_STANDARD 0x00u /*!< Standard mode, output slew rate control is enabled */
118 #define PIN29_IDX 29u /*!< Pin number for pin 29 in a port 0 */
119 #define PIN30_IDX 30u /*!< Pin number for pin 30 in a port 0 */
120 #define PORT0_IDX 0u /*!< Port index */
121
122 /* UART0 device driver structure */
123 struct lpc_uart uart0 =
124 {
125 USART0,
126 FLEXCOMM0_IRQn,
127 };
128 struct rt_serial_device serial0;
129
FLEXCOMM0_IRQHandler(void)130 void FLEXCOMM0_IRQHandler(void)
131 {
132 /* enter interrupt */
133 rt_interrupt_enter();
134
135 rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND);
136
137 /* leave interrupt */
138 rt_interrupt_leave();
139 }
140
rt_hw_uart_init(void)141 void rt_hw_uart_init(void)
142 {
143 struct lpc_uart *uart;
144 struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
145
146 #ifdef BSP_USING_UART0
147
148 uart = &uart0;
149
150 serial0.ops = &lpc_uart_ops;
151 serial0.config = config;
152 serial0.parent.user_data = uart;
153
154 /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
155 CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0);
156 /* Enables the clock for the IOCON block. 0 = Disable; 1 = Enable.: 0x01u */
157 // CLOCK_EnableClock(kCLOCK_Iocon);
158 {
159 const uint32_t port0_pin29_config = (
160 IOCON_PIO_FUNC1 | /* Pin is configured as FC0_RXD_SDA_MOSI */
161 IOCON_PIO_MODE_INACT | /* No addition pin function */
162 IOCON_PIO_INV_DI | /* Input function is not inverted */
163 IOCON_PIO_DIGITAL_EN | /* Enables digital function */
164 IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
165 IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
166 IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
167 );
168 IOCON_PinMuxSet(IOCON, PORT0_IDX, PIN29_IDX, port0_pin29_config); /* PORT0 PIN29 (coords: B13) is configured as FC0_RXD_SDA_MOSI */
169 const uint32_t port0_pin30_config = (
170 IOCON_PIO_FUNC1 | /* Pin is configured as FC0_TXD_SCL_MISO */
171 IOCON_PIO_MODE_INACT | /* No addition pin function */
172 IOCON_PIO_INV_DI | /* Input function is not inverted */
173 IOCON_PIO_DIGITAL_EN | /* Enables digital function */
174 IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
175 IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
176 IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
177 );
178 IOCON_PinMuxSet(IOCON, PORT0_IDX, PIN30_IDX, port0_pin30_config); /* PORT0 PIN30 (coords: A2) is configured as FC0_TXD_SCL_MISO */
179 }
180
181 /* Enable RX interrupt. */
182 USART_EnableInterrupts(uart->UART, kUSART_RxLevelInterruptEnable);
183 EnableIRQ(uart->UART_IRQn);
184
185 /* register UART0 device */
186 rt_hw_serial_register(&serial0, "uart0",
187 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
188 uart);
189 #endif
190 }
191