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