Add handy functions for handling non recurrent timeouts.
[bertos.git] / bertos / drv / ser.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief Buffered serial I/O driver
34  *
35  * The serial rx interrupt buffers incoming data in a software FIFO
36  * to decouple the higher level protocols from the line speed.
37  * Outgoing data is buffered as well for better performance.
38  * This driver is not optimized for best performance, but it
39  * has proved to be fast enough to handle transfer rates up to
40  * 38400bps on a 16MHz 80196.
41  *
42  * MODULE CONFIGURATION
43  *
44  *  \li \c CONFIG_SER_HWHANDSHAKE - set to 1 to enable RTS/CTS handshake.
45  *         Support is incomplete/untested.
46  *  \li \c CONFIG_SER_TXTIMEOUT - Enable software serial transmission timeouts
47  *
48  *
49  * \author Bernie Innocenti <bernie@codewiz.org>
50  */
51
52 #include "ser.h"
53 #include "wdt.h"
54 #include "timer.h"
55 #include "ser_p.h"
56
57 #include "cfg/cfg_ser.h"
58 #include "cfg/cfg_proc.h"
59 #include <cfg/debug.h>
60
61 #include <mware/formatwr.h>
62
63 #include <cpu/power.h> /* cpu_relax() */
64
65 #include <string.h> /* memset() */
66
67 /*
68  * Sanity check for config parameters required by this module.
69  */
70 #if !defined(CONFIG_KERN) || ((CONFIG_KERN != 0) && CONFIG_KERN != 1)
71         #error CONFIG_KERN must be set to either 0 or 1 in cfg_kern.h
72 #endif
73 #if !defined(CONFIG_SER_RXTIMEOUT)
74         #error CONFIG_SER_TXTIMEOUT missing in cfg_ser.h
75 #endif
76 #if !defined(CONFIG_SER_RXTIMEOUT)
77         #error CONFIG_SER_RXTIMEOUT missing in cfg_ser.h
78 #endif
79 #if !defined(CONFIG_SER_DEFBAUDRATE)
80         #error CONFIG_SER_DEFBAUDRATE missing in cfg_ser.h
81 #endif
82
83
84 struct Serial *ser_handles[SER_CNT];
85
86 /**
87  * Insert \a c in tx FIFO buffer.
88  * \note This function will switch out the calling process
89  * if the tx buffer is full. If the buffer is full
90  * and \a port->txtimeout is 0 return EOF immediatly.
91  *
92  * \return EOF on error or timeout, \a c otherwise.
93  */
94 static int ser_putchar(int c, struct Serial *port)
95 {
96         if (fifo_isfull_locked(&port->txfifo))
97         {
98 #if CONFIG_SER_TXTIMEOUT != -1
99                 /* If timeout == 0 we don't want to wait */
100                 if (port->txtimeout == 0)
101                         return EOF;
102
103                 ticks_t start_time = timer_clock();
104 #endif
105
106                 /* Wait while buffer is full... */
107                 do
108                 {
109                         cpu_relax();
110
111 #if CONFIG_SER_TXTIMEOUT != -1
112                         if (timer_clock() - start_time >= port->txtimeout)
113                         {
114                                 ATOMIC(port->status |= SERRF_TXTIMEOUT);
115                                 return EOF;
116                         }
117 #endif /* CONFIG_SER_TXTIMEOUT */
118                 }
119                 while (fifo_isfull_locked(&port->txfifo));
120         }
121
122         fifo_push_locked(&port->txfifo, (unsigned char)c);
123
124         /* (re)trigger tx interrupt */
125         port->hw->table->txStart(port->hw);
126
127         /* Avoid returning signed extended char */
128         return (int)((unsigned char)c);
129 }
130
131
132 /**
133  * Fetch a character from the rx FIFO buffer.
134  * \note This function will switch out the calling process
135  * if the rx buffer is empty. If the buffer is empty
136  * and \a port->rxtimeout is 0 return EOF immediatly.
137  *
138  * \return EOF on error or timeout, \a c otherwise.
139  */
140 static int ser_getchar(struct Serial *port)
141 {
142         if (fifo_isempty_locked(&port->rxfifo))
143         {
144 #if CONFIG_SER_RXTIMEOUT != -1
145                 /* If timeout == 0 we don't want to wait for chars */
146                 if (port->rxtimeout == 0)
147                         return EOF;
148
149                 ticks_t start_time = timer_clock();
150 #endif
151
152                 /* Wait while buffer is empty */
153                 do
154                 {
155                         cpu_relax();
156
157 #if CONFIG_SER_RXTIMEOUT != -1
158                         if (timer_clock() - start_time >= port->rxtimeout)
159                         {
160                                 ATOMIC(port->status |= SERRF_RXTIMEOUT);
161                                 return EOF;
162                         }
163 #endif /* CONFIG_SER_RXTIMEOUT */
164                 }
165                 while (fifo_isempty_locked(&port->rxfifo) && (ser_getstatus(port) & SERRF_RX) == 0);
166         }
167
168         /*
169          * Get a byte from the FIFO (avoiding sign-extension),
170          * re-enable RTS, then return result.
171          */
172         if (ser_getstatus(port) & SERRF_RX)
173                 return EOF;
174         return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
175 }
176
177 /**
178  * Fetch a character from the rx FIFO buffer.
179  * If the buffer is empty, ser_getchar_nowait() returns
180  * EOF immediatly.
181  * \note Deprecated, use ser_getchar with rx_timeout set to 0.
182  */
183 int ser_getchar_nowait(struct Serial *fd)
184 {
185         if (fifo_isempty_locked(&fd->rxfifo))
186                 return EOF;
187
188         /* NOTE: the double cast prevents unwanted sign extension */
189         return (int)(unsigned char)fifo_pop_locked(&fd->rxfifo);
190 }
191
192
193
194 /**
195  * Read at most \a size bytes from \a port and put them in \a buf
196  *
197  * \return number of bytes actually read.
198  */
199 static size_t ser_read(struct KFile *fd, void *_buf, size_t size)
200 {
201         Serial *fds = SERIAL_CAST(fd);
202
203         size_t i = 0;
204         char *buf = (char *)_buf;
205         int c;
206
207         while (i < size)
208         {
209                 if ((c = ser_getchar(fds)) == EOF)
210                         break;
211                 buf[i++] = c;
212         }
213
214         return i;
215 }
216
217 /**
218  * \brief Write a buffer to serial.
219  *
220  * \return 0 if OK, EOF in case of error.
221  *
222  * \todo Optimize with fifo_pushblock()
223  */
224 static size_t ser_write(struct KFile *fd, const void *_buf, size_t size)
225 {
226         Serial *fds = SERIAL_CAST(fd);
227         const char *buf = (const char *)_buf;
228         size_t i = 0;
229
230         while (size--)
231         {
232                 if (ser_putchar(*buf++, fds) == EOF)
233                         break;
234                 i++;
235         }
236         return i;
237 }
238
239
240 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
241 void ser_settimeouts(struct Serial *fd, mtime_t rxtimeout, mtime_t txtimeout)
242 {
243         #if CONFIG_SER_RXTIMEOUT != -1
244                 fd->rxtimeout = ms_to_ticks(rxtimeout);
245         #else
246                 (void)rxtimeout;
247         #endif
248
249         #if CONFIG_SER_TXTIMEOUT != -1
250                 fd->txtimeout = ms_to_ticks(txtimeout);
251         #else
252                 (void)txtimeout;
253         #endif
254 }
255 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
256
257
258 /**
259  * Set the baudrate for the serial port
260  */
261 void ser_setbaudrate(struct Serial *fd, unsigned long rate)
262 {
263         fd->hw->table->setBaudrate(fd->hw, rate);
264 }
265
266
267 /**
268  * Set the parity for the \a fd serial port
269  */
270 void ser_setparity(struct Serial *fd, int parity)
271 {
272         fd->hw->table->setParity(fd->hw, parity);
273 }
274
275 static int ser_error(struct KFile *fd)
276 {
277         Serial *fds = SERIAL_CAST(fd);
278         return ser_getstatus(fds);
279 }
280
281 static void ser_clearerr(struct KFile *fd)
282 {
283         Serial *fds = SERIAL_CAST(fd);
284         ser_setstatus(fds, 0);
285 }
286
287
288
289 /**
290  * Flush both the RX and TX buffers.
291  */
292 void ser_purge(struct Serial *fd)
293 {
294         ser_purgeRx(fd);
295         ser_purgeTx(fd);
296 }
297
298 /**
299  * Flush RX buffer.
300  */
301 void ser_purgeRx(struct Serial *fd)
302 {
303         fifo_flush_locked(&fd->rxfifo);
304 }
305
306 /**
307  * Flush TX buffer.
308  */
309 void ser_purgeTx(struct Serial *fd)
310 {
311         fifo_flush_locked(&fd->txfifo);
312 }
313
314
315 /**
316  * Wait until all pending output is completely
317  * transmitted to the other end.
318  *
319  * \note The current implementation only checks the
320  *       software transmission queue. Any hardware
321  *       FIFOs are ignored.
322  */
323 static int ser_flush(struct KFile *fd)
324 {
325         Serial *fds = SERIAL_CAST(fd);
326
327         /*
328          * Wait until the FIFO becomes empty, and then until the byte currently in
329          * the hardware register gets shifted out.
330          */
331         while (!fifo_isempty(&fds->txfifo)
332                || fds->hw->table->txSending(fds->hw))
333                 cpu_relax();
334         return 0;
335 }
336
337
338 /**
339  * Initialize a serial port.
340  *
341  * \param fd KFile Serial struct interface.
342  * \param unit  Serial unit to open. Possible values are architecture dependant.
343  */
344 static struct Serial *ser_open(struct Serial *fd, unsigned int unit)
345 {
346         ASSERT(unit < countof(ser_handles));
347
348         ser_handles[unit] = fd;
349         ASSERT(!fd->is_open);
350         DB(fd->is_open = true);
351
352         fd->unit = unit;
353
354         fd->hw = ser_hw_getdesc(unit);
355
356         /* Initialize circular buffers */
357         ASSERT(fd->hw->txbuffer);
358         ASSERT(fd->hw->rxbuffer);
359         fifo_init(&fd->txfifo, fd->hw->txbuffer, fd->hw->txbuffer_size);
360         fifo_init(&fd->rxfifo, fd->hw->rxbuffer, fd->hw->rxbuffer_size);
361
362         fd->hw->table->init(fd->hw, fd);
363
364         /* Set default values */
365 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
366         ser_settimeouts(fd, CONFIG_SER_RXTIMEOUT, CONFIG_SER_TXTIMEOUT);
367 #endif
368 #if CONFIG_SER_DEFBAUDRATE
369         ser_setbaudrate(fd, CONFIG_SER_DEFBAUDRATE);
370 #endif
371
372         /* Clear error flags */
373         ser_setstatus(fd, 0);
374
375         return fd;
376 }
377
378
379 /**
380  * Clean up serial port, disabling the associated hardware.
381  */
382 static int ser_close(struct KFile *fd)
383 {
384         Serial *fds = SERIAL_CAST(fd);
385         Serial *port = fds;
386
387         ASSERT(port->is_open);
388         DB(port->is_open = false);
389
390         // Wait until we finish sending everything
391         ser_flush(fd);
392
393         port->hw->table->cleanup(port->hw);
394         DB(port->hw = NULL);
395
396         /*
397          * We purge the FIFO buffer only after the low-level cleanup, so that
398          * we are sure that there are no more interrupts.
399          */
400         ser_purge(fds);
401         return 0;
402 }
403
404 /**
405  * Reopen serial port.
406  */
407 static struct KFile *ser_reopen(struct KFile *fd)
408 {
409         Serial *fds = SERIAL_CAST(fd);
410
411         ser_close(fd);
412         ser_open(fds, fds->unit);
413         return (KFile *)fds;
414 }
415
416 /**
417  * Init serial driver for \a unit.
418  *
419  * Use values SER_UARTn as values for \a unit.
420  */
421 void ser_init(struct Serial *fds, unsigned int unit)
422 {
423         memset(fds, 0, sizeof(*fds));
424
425         DB(fds->fd._type = KFT_SERIAL);
426         fds->fd.reopen = ser_reopen;
427         fds->fd.close = ser_close;
428         fds->fd.read = ser_read;
429         fds->fd.write = ser_write;
430         fds->fd.flush = ser_flush;
431         fds->fd.error = ser_error;
432         fds->fd.clearerr = ser_clearerr;
433         ser_open(fds, unit);
434 }
435
436
437 /**
438  * Read data from SPI bus.
439  * Since we are master, we have to trigger slave by sending
440  * fake chars on the bus.
441  */
442 static size_t spimaster_read(struct KFile *fd, void *_buf, size_t size)
443 {
444         Serial *fd_spi = SERIAL_CAST(fd);
445
446         ser_flush(&fd_spi->fd);
447         ser_purgeRx(fd_spi);
448
449         size_t total_rd = 0;
450         uint8_t *buf = (uint8_t *)_buf;
451         int c;
452
453         while (size--)
454         {
455                 /*
456                  * Send and receive chars 1 by 1, otherwise the rxfifo
457                  * will overrun.
458                  */
459                 ser_putchar(0, fd_spi);
460
461                 if ((c = ser_getchar(fd_spi)) == EOF)
462                         break;
463
464                 *buf++ = c;
465                 total_rd++;
466         }
467         return total_rd;
468 }
469
470 /**
471  * Write data to SPI bus.
472  */
473 static size_t spimaster_write(struct KFile *fd, const void *buf, size_t size)
474 {
475         Serial *fd_spi = SERIAL_CAST(fd);
476
477         ser_purgeRx(fd_spi);
478
479         return ser_write(&fd_spi->fd, buf, size);
480 }
481
482
483 /**
484  * Init SPI serial driver \a unit in master mode.
485  *
486  * Use SER_SPIn for \a unit parameter.
487  *
488  * This interface implements the SPI master protocol over a serial SPI
489  * driver. This is needed because normal serial driver send/receive data
490  * at the same time. SPI slaves like memories and other peripherals
491  * first receive and *then* send response back instead.
492  * To achieve this, when we are master and we are *sending*,
493  * we have to discard all incoming data. Then, when we want to
494  * receive, we must write fake data to SPI to trigger slave devices.
495  */
496 void spimaster_init(Serial *fds, unsigned int unit)
497 {
498         ser_init(fds, unit);
499         fds->fd.read = spimaster_read;
500         fds->fd.write = spimaster_write;
501 }
502
503