Update preset.
[bertos.git] / bertos / drv / ser.c
index c8ca15be17b1bf20a2ae355fc6c951fd5bde5840..c9c3b0f7611e13bcb97632eb3c40bdc04ac9bb7f 100644 (file)
@@ -28,7 +28,6 @@
  *
  * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
  * Copyright 2000 Bernie Innocenti <bernie@codewiz.org>
- *
  * -->
  *
  * \brief Buffered serial I/O driver
  *  \li \c CONFIG_SER_TXTIMEOUT - Enable software serial transmission timeouts
  *
  *
- * \version $Id$
  * \author Bernie Innocenti <bernie@codewiz.org>
  */
 
 #include "ser.h"
 #include "wdt.h"
+#include "timer.h"
 #include "ser_p.h"
 
 #include "cfg/cfg_ser.h"
-#include "cfg/cfg_kern.h"
+#include "cfg/cfg_proc.h"
 #include <cfg/debug.h>
 
 #include <mware/formatwr.h>
 
-#include <string.h> /* memset */
+#include <cpu/power.h> /* cpu_relax() */
+
+#include <string.h> /* memset() */
 
 /*
  * Sanity check for config parameters required by this module.
  */
-#if !defined(CONFIG_KERNEL) || ((CONFIG_KERNEL != 0) && CONFIG_KERNEL != 1)
-       #error CONFIG_KERNEL must be set to either 0 or 1 in config.h
+#if !defined(CONFIG_KERN) || ((CONFIG_KERN != 0) && CONFIG_KERN != 1)
+       #error CONFIG_KERN must be set to either 0 or 1 in cfg_kern.h
 #endif
 #if !defined(CONFIG_SER_RXTIMEOUT)
-       #error CONFIG_SER_TXTIMEOUT missing in config.h
+       #error CONFIG_SER_TXTIMEOUT missing in cfg_ser.h
 #endif
 #if !defined(CONFIG_SER_RXTIMEOUT)
-       #error CONFIG_SER_RXTIMEOUT missing in config.h
+       #error CONFIG_SER_RXTIMEOUT missing in cfg_ser.h
 #endif
 #if !defined(CONFIG_SER_DEFBAUDRATE)
-       #error CONFIG_SER_DEFBAUDRATE missing in config.h
-#endif
-
-#if CONFIG_KERNEL
-       #include <kern/proc.h>
-#endif
-
-#if CONFIG_SER_TXTIMEOUT != -1 || CONFIG_SER_RXTIMEOUT != -1
-       #include <drv/timer.h>
+       #error CONFIG_SER_DEFBAUDRATE missing in cfg_ser.h
 #endif
 
 
@@ -113,11 +106,8 @@ static int ser_putchar(int c, struct Serial *port)
                /* Wait while buffer is full... */
                do
                {
-                       wdt_reset();
-#if CONFIG_KERNEL && CONFIG_KERN_SCHED
-                       /* Give up timeslice to other processes. */
-                       proc_switch();
-#endif
+                       cpu_relax();
+
 #if CONFIG_SER_TXTIMEOUT != -1
                        if (timer_clock() - start_time >= port->txtimeout)
                        {
@@ -158,14 +148,12 @@ static int ser_getchar(struct Serial *port)
 
                ticks_t start_time = timer_clock();
 #endif
+
                /* Wait while buffer is empty */
                do
                {
-                       wdt_reset();
-#if CONFIG_KERNEL && CONFIG_KERN_SCHED
-                       /* Give up timeslice to other processes. */
-                       proc_switch();
-#endif
+                       cpu_relax();
+
 #if CONFIG_SER_RXTIMEOUT != -1
                        if (timer_clock() - start_time >= port->rxtimeout)
                        {
@@ -210,7 +198,7 @@ int ser_getchar_nowait(struct Serial *fd)
  */
 static size_t ser_read(struct KFile *fd, void *_buf, size_t size)
 {
-       Serial *fds = SERIALKFILE(fd);
+       Serial *fds = SERIAL_CAST(fd);
 
        size_t i = 0;
        char *buf = (char *)_buf;
@@ -235,7 +223,7 @@ static size_t ser_read(struct KFile *fd, void *_buf, size_t size)
  */
 static size_t ser_write(struct KFile *fd, const void *_buf, size_t size)
 {
-       Serial *fds = SERIALKFILE(fd);
+       Serial *fds = SERIAL_CAST(fd);
        const char *buf = (const char *)_buf;
        size_t i = 0;
 
@@ -252,45 +240,33 @@ static size_t ser_write(struct KFile *fd, const void *_buf, size_t size)
 #if CONFIG_SER_RXTIMEOUT != -1 || CONFIG_SER_TXTIMEOUT != -1
 void ser_settimeouts(struct Serial *fd, mtime_t rxtimeout, mtime_t txtimeout)
 {
-       fd->rxtimeout = ms_to_ticks(rxtimeout);
-       fd->txtimeout = ms_to_ticks(txtimeout);
+       #if CONFIG_SER_RXTIMEOUT != -1
+               fd->rxtimeout = ms_to_ticks(rxtimeout);
+       #else
+               (void)rxtimeout;
+       #endif
+
+       #if CONFIG_SER_TXTIMEOUT != -1
+               fd->txtimeout = ms_to_ticks(txtimeout);
+       #else
+               (void)txtimeout;
+       #endif
 }
 #endif /* CONFIG_SER_RXTIMEOUT || CONFIG_SER_TXTIMEOUT */
 
-#if CONFIG_SER_RXTIMEOUT != -1
+
 /**
- * Discard input to resynchronize with remote end.
- *
- * Discard incoming data until the port stops receiving
- * characters for at least \a delay milliseconds.
- *
- * \note Serial errors are reset before and after executing the purge.
+ * Set the baudrate for the serial port
  */
-void ser_resync(struct Serial *fd, mtime_t delay)
-{
-       mtime_t old_rxtimeout = ticks_to_ms(fd->rxtimeout);
-
-       ser_settimeouts(fd, delay, ticks_to_ms(fd->txtimeout));
-       do
-       {
-               ser_setstatus(fd, 0);
-               ser_getchar(fd);
-       }
-       while (!(ser_getstatus(fd) & SERRF_RXTIMEOUT));
-
-       /* Restore port to an usable status */
-       ser_setstatus(fd, 0);
-       ser_settimeouts(fd, old_rxtimeout, ticks_to_ms(fd->txtimeout));
-}
-#endif /* CONFIG_SER_RXTIMEOUT */
-
-
 void ser_setbaudrate(struct Serial *fd, unsigned long rate)
 {
        fd->hw->table->setBaudrate(fd->hw, rate);
 }
 
 
+/**
+ * Set the parity for the \a fd serial port
+ */
 void ser_setparity(struct Serial *fd, int parity)
 {
        fd->hw->table->setParity(fd->hw, parity);
@@ -298,13 +274,13 @@ void ser_setparity(struct Serial *fd, int parity)
 
 static int ser_error(struct KFile *fd)
 {
-       Serial *fds = SERIALKFILE(fd);
+       Serial *fds = SERIAL_CAST(fd);
        return ser_getstatus(fds);
 }
 
 static void ser_clearerr(struct KFile *fd)
 {
-       Serial *fds = SERIALKFILE(fd);
+       Serial *fds = SERIAL_CAST(fd);
        ser_setstatus(fds, 0);
 }
 
@@ -346,7 +322,7 @@ void ser_purgeTx(struct Serial *fd)
  */
 static int ser_flush(struct KFile *fd)
 {
-       Serial *fds = SERIALKFILE(fd);
+       Serial *fds = SERIAL_CAST(fd);
 
        /*
         * Wait until the FIFO becomes empty, and then until the byte currently in
@@ -354,13 +330,7 @@ static int ser_flush(struct KFile *fd)
         */
        while (!fifo_isempty(&fds->txfifo)
               || fds->hw->table->txSending(fds->hw))
-       {
-               #if CONFIG_KERNEL && CONFIG_KERN_SCHED
-                       /* Give up timeslice to other processes. */
-                       proc_switch();
-               #endif
-                       wdt_reset();
-       }
+               cpu_relax();
        return 0;
 }
 
@@ -411,7 +381,7 @@ static struct Serial *ser_open(struct Serial *fd, unsigned int unit)
  */
 static int ser_close(struct KFile *fd)
 {
-       Serial *fds = SERIALKFILE(fd);
+       Serial *fds = SERIAL_CAST(fd);
        Serial *port = fds;
 
        ASSERT(port->is_open);
@@ -436,7 +406,7 @@ static int ser_close(struct KFile *fd)
  */
 static struct KFile *ser_reopen(struct KFile *fd)
 {
-       Serial *fds = SERIALKFILE(fd);
+       Serial *fds = SERIAL_CAST(fd);
 
        ser_close(fd);
        ser_open(fds, fds->unit);
@@ -445,6 +415,8 @@ static struct KFile *ser_reopen(struct KFile *fd)
 
 /**
  * Init serial driver for \a unit.
+ *
+ * Use values SER_UARTn as values for \a unit.
  */
 void ser_init(struct Serial *fds, unsigned int unit)
 {
@@ -469,7 +441,7 @@ void ser_init(struct Serial *fds, unsigned int unit)
  */
 static size_t spimaster_read(struct KFile *fd, void *_buf, size_t size)
 {
-       Serial *fd_spi = SERIALKFILE(fd);
+       Serial *fd_spi = SERIAL_CAST(fd);
 
        ser_flush(&fd_spi->fd);
        ser_purgeRx(fd_spi);
@@ -500,7 +472,7 @@ static size_t spimaster_read(struct KFile *fd, void *_buf, size_t size)
  */
 static size_t spimaster_write(struct KFile *fd, const void *buf, size_t size)
 {
-       Serial *fd_spi = SERIALKFILE(fd);
+       Serial *fd_spi = SERIAL_CAST(fd);
 
        ser_purgeRx(fd_spi);
 
@@ -511,6 +483,8 @@ static size_t spimaster_write(struct KFile *fd, const void *buf, size_t size)
 /**
  * Init SPI serial driver \a unit in master mode.
  *
+ * Use SER_SPIn for \a unit parameter.
+ *
  * This interface implements the SPI master protocol over a serial SPI
  * driver. This is needed because normal serial driver send/receive data
  * at the same time. SPI slaves like memories and other peripherals