ser_posix.c

Go to the documentation of this file.
00001 
00039 #include "cfg/cfg_ser.h"
00040 
00041 #include <cfg/debug.h>
00042 #include <cfg/compiler.h>
00043 
00044 #include <drv/ser.h>
00045 #include <drv/ser_p.h>
00046 
00047 #include <struct/fifobuf.h>
00048 
00049 #include <sys/types.h>
00050 #include <sys/stat.h>
00051 
00052 #include <fcntl.h> /* open() */
00053 #include <unistd.h> /* read(), write() */
00054 
00055 
00056 /* From the high-level serial driver */
00057 extern struct Serial ser_handles[SER_CNT];
00058 
00059 /* TX and RX buffers */
00060 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
00061 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
00062 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
00063 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
00064 
00065 
00069 struct EmulSerial
00070 {
00071     struct SerialHardware hw;
00072     struct Serial *ser;
00073     int fd;
00074 };
00075 
00076 
00077 /*
00078  * Callbacks
00079  */
00080 static void uart_init(struct SerialHardware *_hw, struct Serial *ser)
00081 {
00082     struct EmulSerial *hw = (struct EmulSerial *)_hw;
00083 
00084     hw->ser = ser;
00085     hw->fd = open("/dev/ttyS0", O_RDWR);
00086 }
00087 
00088 static void uart_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
00089 {
00090     struct EmulSerial *hw = (struct EmulSerial *)_hw;
00091 
00092     close(hw->fd);
00093     hw->fd = -1;
00094 }
00095 
00096 static void uart_txStart(struct SerialHardware * _hw)
00097 {
00098     struct EmulSerial *hw = (struct EmulSerial *)_hw;
00099 
00100     while(!fifo_isempty(&hw->ser->txfifo))
00101     {
00102         char c = fifo_pop(&hw->ser->txfifo);
00103         write(hw->fd, &c, 1);
00104     }
00105 }
00106 
00107 static bool uart_txSending(UNUSED_ARG(struct SerialHardware *, _hw))
00108 {
00109     return false;
00110 }
00111 
00112 
00113 static void uart_setBaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
00114 {
00115     TRACEMSG("rate=%lu", rate);
00116     // TODO
00117 
00118 }
00119 
00120 static void uart_setParity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
00121 {
00122     TRACEMSG("parity=%d", parity);
00123     // TODO
00124 }
00125 
00126 // FIXME: move into compiler.h?  Ditch?
00127 #if COMPILER_C99
00128     #define C99INIT(name,val) .name = val
00129 #elif defined(__GNUC__)
00130     #define C99INIT(name,val) name: val
00131 #else
00132     #warning No designated initializers, double check your code
00133     #define C99INIT(name,val) (val)
00134 #endif
00135 
00136 /*
00137  * High-level interface data structures.
00138  */
00139 static const struct SerialHardwareVT uart_vtable =
00140 {
00141     C99INIT(init, uart_init),
00142     C99INIT(cleanup, uart_cleanup),
00143     C99INIT(setBaudrate, uart_setBaudrate),
00144     C99INIT(setParity, uart_setParity),
00145     C99INIT(txStart, uart_txStart),
00146     C99INIT(txSending, uart_txSending),
00147 };
00148 
00149 static struct EmulSerial UARTDescs[SER_CNT] =
00150 {
00151     {
00152         C99INIT(hw, ) {
00153             C99INIT(table, &uart_vtable),
00154             C99INIT(txbuffer, uart0_txbuffer),
00155             C99INIT(rxbuffer, uart0_rxbuffer),
00156             C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
00157             C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
00158         },
00159         C99INIT(ser, NULL),
00160         C99INIT(fd, -1),
00161     },
00162     {
00163         C99INIT(hw, ) {
00164             C99INIT(table, &uart_vtable),
00165             C99INIT(txbuffer, uart1_txbuffer),
00166             C99INIT(rxbuffer, uart1_rxbuffer),
00167             C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
00168             C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
00169         },
00170         C99INIT(ser, NULL),
00171         C99INIT(fd, -1),
00172     },
00173 };
00174 
00175 struct SerialHardware *ser_hw_getdesc(int unit)
00176 {
00177     ASSERT(unit < SER_CNT);
00178     return &UARTDescs[unit].hw;
00179 }