ser_posix.c

Go to the documentation of this file.
00001 
00039 /*#*
00040  *#* $Log$
00041  *#* Revision 1.2  2006/07/19 12:56:26  bernie
00042  *#* Convert to new Doxygen style.
00043  *#*
00044  *#* Revision 1.1  2006/02/17 22:28:00  bernie
00045  *#* Rename ser_emul.c to ser_posix.c.
00046  *#*
00047  *#* Revision 1.4  2006/02/17 22:23:06  bernie
00048  *#* Update POSIX serial emulator.
00049  *#*
00050  *#* Revision 1.3  2005/11/04 16:20:02  bernie
00051  *#* Fix reference to README.devlib in header.
00052  *#*
00053  *#* Revision 1.2  2005/04/11 19:10:27  bernie
00054  *#* Include top-level headers from cfg/ subdir.
00055  *#*
00056  *#* Revision 1.1  2004/12/31 17:40:00  bernie
00057  *#* Add a simple serial emulation driver.
00058  *#*
00059  *#*/
00060 
00061 #include "ser.h"
00062 #include "ser_p.h"
00063 
00064 #include <cfg/debug.h>
00065 #include <cfg/compiler.h>
00066 #include <mware/fifobuf.h>
00067 
00068 #include <appconfig.h>
00069 
00070 #include <sys/types.h>
00071 #include <sys/stat.h>
00072 #include <fcntl.h> /* open() */
00073 #include <unistd.h> /* read(), write() */
00074 
00075 
00076 /* From the high-level serial driver */
00077 extern struct Serial ser_handles[SER_CNT];
00078 
00079 /* TX and RX buffers */
00080 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
00081 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
00082 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
00083 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
00084 
00085 
00089 struct EmulSerial
00090 {
00091     struct SerialHardware hw;
00092     struct Serial *ser;
00093     int fd;
00094 };
00095 
00096 
00097 /*
00098  * Callbacks
00099  */
00100 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
00101 {
00102     struct EmulSerial *hw = (struct EmulSerial *)_hw;
00103 
00104     hw->ser = ser;
00105     hw->fd = open("/dev/ttyS0", O_RDWR);
00106 }
00107 
00108 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
00109 {
00110     struct EmulSerial *hw = (struct EmulSerial *)_hw;
00111 
00112     close(hw->fd);
00113     hw->fd = -1;
00114 }
00115 
00116 static void uart_txStart(struct SerialHardware * _hw)
00117 {
00118     struct EmulSerial *hw = (struct EmulSerial *)_hw;
00119 
00120     while(!fifo_isempty(&hw->ser->txfifo))
00121     {
00122         char c = fifo_pop(&hw->ser->txfifo);
00123         write(hw->fd, &c, 1);
00124     }
00125 }
00126 
00127 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
00128 {
00129     return false;
00130 }
00131 
00132 
00133 static void uart_setBaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
00134 {
00135     TRACEMSG("rate=%lu", rate);
00136     // TODO
00137 
00138 }
00139 
00140 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
00141 {
00142     TRACEMSG("parity=%d", parity);
00143     // TODO
00144 }
00145 
00146 // FIXME: move into compiler.h?  Ditch?
00147 #if COMPILER_C99
00148     #define C99INIT(name,val) .name = val
00149 #elif defined(__GNUC__)
00150     #define C99INIT(name,val) name: val
00151 #else
00152     #warning No designated initializers, double check your code
00153     #define C99INIT(name,val) (val)
00154 #endif
00155 
00156 /*
00157  * High-level interface data structures.
00158  */
00159 static const struct SerialHardwareVT uart_vtable =
00160 {
00161     C99INIT(init, uart_init),
00162     C99INIT(cleanup, uart_cleanup),
00163     C99INIT(setBaudrate, uart_setBaudrate),
00164     C99INIT(setParity, uart_setParity),
00165     C99INIT(txStart, uart_txStart),
00166     C99INIT(txSending, uart_txSending),
00167 };
00168 
00169 static struct EmulSerial UARTDescs[SER_CNT] =
00170 {
00171     {
00172         C99INIT(hw, ) {
00173             C99INIT(table, &uart_vtable),
00174             C99INIT(txbuffer, uart0_txbuffer),
00175             C99INIT(rxbuffer, uart0_rxbuffer),
00176             C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
00177             C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
00178         },
00179         C99INIT(ser, NULL),
00180         C99INIT(fd, -1),
00181     },
00182     {
00183         C99INIT(hw, ) {
00184             C99INIT(table, &uart_vtable),
00185             C99INIT(txbuffer, uart1_txbuffer),
00186             C99INIT(rxbuffer, uart1_rxbuffer),
00187             C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
00188             C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
00189         },
00190         C99INIT(ser, NULL),
00191         C99INIT(fd, -1),
00192     },
00193 };
00194 
00195 struct SerialHardware *ser_hw_getdesc(int unit)
00196 {
00197     ASSERT(unit < SER_CNT);
00198     return &UARTDescs[unit].hw;
00199 }