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 * 2006-03-13 Bernard first version
9 * 2011-05-15 lgnq modified according bernard's implementaion.
10 */
11
12 #include <rtthread.h>
13
14 #include "serial.h"
15
16 /**
17 * @addtogroup FM4 MB9BF568R
18 */
19 /*@{*/
20
21 /* RT-Thread Device Interface */
22 /**
23 * This function initializes serial
24 */
rt_serial_init(rt_device_t dev)25 static rt_err_t rt_serial_init (rt_device_t dev)
26 {
27 struct serial_device* uart = (struct serial_device*) dev->user_data;
28
29 if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
30 {
31 if (dev->flag & RT_DEVICE_FLAG_INT_RX)
32 {
33 rt_memset(uart->int_rx->rx_buffer, 0,
34 sizeof(uart->int_rx->rx_buffer));
35 uart->int_rx->read_index = uart->int_rx->save_index = 0;
36 }
37
38 if (dev->flag & RT_DEVICE_FLAG_INT_TX)
39 {
40 rt_memset(uart->int_tx->tx_buffer, 0,
41 sizeof(uart->int_tx->tx_buffer));
42 uart->int_tx->write_index = uart->int_tx->save_index = 0;
43 }
44
45 dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
46 }
47
48 return RT_EOK;
49 }
50
51 /* save a char to serial buffer */
rt_serial_savechar(struct serial_device * uart,char ch)52 static void rt_serial_savechar(struct serial_device* uart, char ch)
53 {
54 rt_base_t level;
55
56 /* disable interrupt */
57 level = rt_hw_interrupt_disable();
58
59 uart->int_rx->rx_buffer[uart->int_rx->save_index] = ch;
60 uart->int_rx->save_index ++;
61 if (uart->int_rx->save_index >= UART_RX_BUFFER_SIZE)
62 uart->int_rx->save_index = 0;
63
64 /* if the next position is read index, discard this 'read char' */
65 if (uart->int_rx->save_index == uart->int_rx->read_index)
66 {
67 uart->int_rx->read_index ++;
68 if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
69 uart->int_rx->read_index = 0;
70 }
71
72 /* enable interrupt */
73 rt_hw_interrupt_enable(level);
74 }
75
rt_serial_open(rt_device_t dev,rt_uint16_t oflag)76 static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag)
77 {
78 struct serial_device* uart;
79
80 RT_ASSERT(dev != RT_NULL);
81 uart = (struct serial_device*) dev->user_data;
82
83 if (dev->flag & RT_DEVICE_FLAG_INT_RX)
84 {
85 /* enable interrupt */
86 UART_ENABLE_IRQ(uart->rx_irq);
87 }
88
89 return RT_EOK;
90 }
91
rt_serial_close(rt_device_t dev)92 static rt_err_t rt_serial_close(rt_device_t dev)
93 {
94 struct serial_device* uart;
95
96 RT_ASSERT(dev != RT_NULL);
97 uart = (struct serial_device*) dev->user_data;
98
99 if (dev->flag & RT_DEVICE_FLAG_INT_RX)
100 {
101 /* disable interrupt */
102 UART_DISABLE_IRQ(uart->rx_irq);
103 }
104
105 return RT_EOK;
106 }
107
rt_serial_read(rt_device_t dev,rt_off_t pos,void * buffer,rt_size_t size)108 static rt_ssize_t rt_serial_read (rt_device_t dev, rt_off_t pos, void* buffer,
109 rt_size_t size)
110 {
111 rt_uint8_t* ptr;
112 rt_err_t err_code;
113 struct serial_device* uart;
114
115 ptr = buffer;
116 err_code = RT_EOK;
117 uart = (struct serial_device*)dev->user_data;
118
119 if (dev->flag & RT_DEVICE_FLAG_INT_RX)
120 {
121 rt_base_t level;
122
123 /* interrupt mode Rx */
124 while (size)
125 {
126 if (uart->int_rx->read_index != uart->int_rx->save_index)
127 {
128 *ptr++ = uart->int_rx->rx_buffer[uart->int_rx->read_index];
129 size --;
130
131 /* disable interrupt */
132 level = rt_hw_interrupt_disable();
133
134 uart->int_rx->read_index ++;
135 if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
136 uart->int_rx->read_index = 0;
137
138 /* enable interrupt */
139 rt_hw_interrupt_enable(level);
140 }
141 else
142 {
143 /* set error code */
144 err_code = -RT_EEMPTY;
145 break;
146 }
147 }
148 }
149 else
150 {
151 /* polling mode */
152 while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
153 {
154 while (uart->uart_device->SSR & SSR_RDRF)
155 {
156 *ptr = uart->uart_device->RDR & 0xff;
157 ptr ++;
158 }
159 }
160 }
161
162 /* set error code */
163 rt_set_errno(err_code);
164 return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
165 }
166
rt_serial_write(rt_device_t dev,rt_off_t pos,const void * buffer,rt_size_t size)167 static rt_ssize_t rt_serial_write (rt_device_t dev, rt_off_t pos,
168 const void* buffer, rt_size_t size)
169 {
170 rt_uint8_t* ptr;
171 rt_err_t err_code;
172 struct serial_device* uart;
173
174 err_code = RT_EOK;
175 ptr = (rt_uint8_t*)buffer;
176 uart = (struct serial_device*)dev->user_data;
177
178 if (dev->flag & RT_DEVICE_FLAG_INT_TX)
179 {
180 /* interrupt mode Tx */
181 while (uart->int_tx->save_index != uart->int_tx->write_index)
182 {
183 /* save on tx buffer */
184 uart->int_tx->tx_buffer[uart->int_tx->save_index] = *ptr++;
185
186 -- size;
187
188 /* move to next position */
189 uart->int_tx->save_index ++;
190
191 /* wrap save index */
192 if (uart->int_tx->save_index >= UART_TX_BUFFER_SIZE)
193 uart->int_tx->save_index = 0;
194 }
195
196 /* set error code */
197 if (size > 0)
198 err_code = -RT_EFULL;
199 }
200 else
201 {
202 /* polling mode */
203 while (size)
204 {
205 /*
206 * to be polite with serial console add a line feed
207 * to the carriage return character
208 */
209 if (*ptr == '\n' && (dev->flag & RT_DEVICE_FLAG_STREAM))
210 {
211 while (!(uart->uart_device->SSR & SSR_TDRE));
212 uart->uart_device->TDR = '\r';
213 }
214
215 while (!(uart->uart_device->SSR & SSR_TDRE));
216 uart->uart_device->TDR = (*ptr & 0x1FF);
217
218 ++ptr;
219 --size;
220 }
221 }
222
223 /* set error code */
224 rt_set_errno(err_code);
225
226 return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
227 }
228
rt_serial_control(rt_device_t dev,int cmd,void * args)229 static rt_err_t rt_serial_control (rt_device_t dev, int cmd, void *args)
230 {
231 RT_ASSERT(dev != RT_NULL);
232
233 switch (cmd)
234 {
235 case RT_DEVICE_CTRL_SUSPEND:
236 /* suspend device */
237 dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
238 break;
239
240 case RT_DEVICE_CTRL_RESUME:
241 /* resume device */
242 dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
243 break;
244 }
245
246 return RT_EOK;
247 }
248
249 /*
250 * serial register
251 */
rt_hw_fujitsu_serial_register(rt_device_t device,const char * name,rt_uint32_t flag,struct serial_device * serial)252 rt_err_t rt_hw_fujitsu_serial_register(rt_device_t device, const char* name,
253 rt_uint32_t flag, struct serial_device *serial)
254 {
255 RT_ASSERT(device != RT_NULL);
256
257 device->type = RT_Device_Class_Char;
258 device->rx_indicate = RT_NULL;
259 device->tx_complete = RT_NULL;
260 device->init = rt_serial_init;
261 device->open = rt_serial_open;
262 device->close = rt_serial_close;
263 device->read = rt_serial_read;
264 device->write = rt_serial_write;
265 device->control = rt_serial_control;
266 device->user_data = serial;
267
268 /* register a character device */
269 return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
270 }
271
272 /* ISR for serial interrupt */
rt_hw_fujitsu_serial_isr(rt_device_t device)273 void rt_hw_fujitsu_serial_isr(rt_device_t device)
274 {
275 struct serial_device* uart = (struct serial_device*) device->user_data;
276
277 /* interrupt mode receive */
278 RT_ASSERT(device->flag & RT_DEVICE_FLAG_INT_RX);
279
280 /* save on rx buffer */
281 while (uart->uart_device->SSR & SSR_RDRF)
282 {
283 rt_serial_savechar(uart, uart->uart_device->RDR & 0xff);
284 }
285
286 /* invoke callback */
287 if (device->rx_indicate != RT_NULL)
288 {
289 rt_size_t rx_length;
290
291 /* get rx length */
292 rx_length = uart->int_rx->read_index > uart->int_rx->save_index ?
293 UART_RX_BUFFER_SIZE - uart->int_rx->read_index + uart->int_rx->save_index :
294 uart->int_rx->save_index - uart->int_rx->read_index;
295
296 device->rx_indicate(device, rx_length);
297 }
298 }
299
300 #ifdef RT_USING_UART0
301 /* UART0 device driver structure */
302 #define UART0 FM4_MFS0
303 struct serial_int_rx uart0_int_rx;
304 struct serial_device uart0 =
305 {
306 UART0,
307 MFS0_RX_IRQn,
308 MFS0_TX_IRQn,
309 &uart0_int_rx,
310 RT_NULL
311 };
312 struct rt_device uart0_device;
313
MFS0_RX_IRQHandler(void)314 void MFS0_RX_IRQHandler(void)
315 {
316 /* enter interrupt */
317 rt_interrupt_enter();
318 rt_hw_fujitsu_serial_isr(&uart0_device);
319 /* leave interrupt */
320 rt_interrupt_leave();
321 }
322
323
324 #endif /**< #ifdef RT_USING_UART0 */
325
326
rt_hw_serial_init(void)327 void rt_hw_serial_init(void)
328 {
329 uint32_t APB2_clock = (SystemCoreClock >> (APBC2_PSR_Val & 0x03));
330
331 #ifdef RT_USING_UART0
332 // Initialize ports for MFS0
333 FM4_GPIO->PFR2 = 0x06u; // P21>SIN0_0, P22>SOT0_0
334 FM4_GPIO->EPFR07 &= 0xFFFFFF0Ful;
335 FM4_GPIO->EPFR07 |= 0x00000040ul;
336
337 // Initialize MFS to UART asynchronous mode
338
339 uart0.uart_device->SMR = SMR_MD_UART | SMR_SOE;;
340 uart0.uart_device->BGR = (APB2_clock + (BPS/2))/BPS - 1; /* round */
341 uart0.uart_device->ESCR = ESCR_DATABITS_8;
342 uart0.uart_device->SCR = SCR_RXE | SCR_TXE | SCR_RIE;
343
344 /* register UART0 device */
345 rt_hw_fujitsu_serial_register(&uart0_device,
346 "uart0",
347 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
348 &uart0);
349
350
351 #endif /**< #ifdef RT_USING_UART0 */
352
353 }
354
355 /*@}*/
356