Analog Devices / mbed-drivers
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers libserialport_fake.c Source File

libserialport_fake.c

00001 /*
00002  * Libserialport-fake
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, either version 3 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <string.h>
00019 
00020 #include <mbed/serial_api.h>
00021 
00022 #include "libserialport.h"
00023 
00024 struct sp_port {
00025     serial_t uart;
00026     int data_bits, stop_bits;
00027     SerialParity parity;
00028     char *name, *description;
00029 };
00030 
00031 static struct sp_port sp_ports[5];
00032 
00033 static void init_port(struct sp_port *port, PinName tx, PinName rx,
00034         char *name, char *description)
00035 {
00036     serial_init(&port->uart, tx, rx);
00037     serial_pinout_tx(tx);
00038 
00039     port->data_bits = 8;
00040     port->stop_bits = 1;
00041     port->parity = ParityNone;
00042     port->name = name;
00043     port->description = description;
00044 }
00045 
00046 enum sp_return sp_get_port_by_name(const char *portname,
00047         struct sp_port **port_ptr)
00048 {
00049     if (!strcmp(portname, "USB") || !strcmp(portname, "UART0")) {
00050         init_port(&sp_ports[0], USBTX, USBRX, "UART0", "USB UART");
00051         *port_ptr = &sp_ports[0];
00052         return SP_OK;
00053     }
00054 
00055     if (!strcmp(portname, "UART3")) {
00056         init_port(&sp_ports[3], PTC17, PTC16, "UART3", "Arduino UART");
00057         *port_ptr = &sp_ports[3];
00058         return SP_OK;
00059     }
00060 
00061     return SP_ERR_ARG;
00062 }
00063 
00064 void sp_free_port(struct sp_port *port)
00065 {
00066     serial_free(&port->uart);
00067 }
00068 
00069 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
00070 {
00071     if (flags != SP_MODE_READ_WRITE)
00072         return SP_ERR_SUPP;
00073     else
00074         return SP_OK;
00075 }
00076 
00077 enum sp_return sp_close(struct sp_port *port)
00078 {
00079     return SP_OK;
00080 }
00081 
00082 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate)
00083 {
00084     serial_baud(&port->uart, baudrate);
00085     return SP_OK;
00086 }
00087 
00088 enum sp_return sp_set_bits(struct sp_port *port, int bits)
00089 {
00090     port->data_bits = bits;
00091     serial_format(&port->uart, bits, port->parity, port->stop_bits);
00092     return SP_OK;
00093 }
00094 
00095 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits)
00096 {
00097     port->stop_bits = stopbits;
00098     serial_format(&port->uart, port->data_bits, port->parity, stopbits);
00099     return SP_OK;
00100 }
00101 
00102 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity)
00103 {
00104     switch (parity) {
00105     case SP_PARITY_NONE:
00106         port->parity = ParityNone;
00107         break;
00108     case SP_PARITY_ODD:
00109         port->parity = ParityOdd;
00110         break;
00111     case SP_PARITY_EVEN:
00112         port->parity = ParityEven;
00113         break;
00114     default:
00115         return SP_ERR_SUPP;
00116     }
00117 
00118     serial_format(&port->uart, port->data_bits,
00119             port->parity, port->stop_bits);
00120     return SP_OK;
00121 }
00122 
00123 enum sp_return sp_set_flowcontrol(
00124         struct sp_port *port, enum sp_flowcontrol flow)
00125 {
00126     if (flow != SP_FLOWCONTROL_NONE)
00127         return SP_ERR_SUPP;
00128 
00129 #if 0
00130     /* This function is in serial_api.h, but it's not implemented anywhere */
00131     serial_set_flow_control(&port->uart, FlowControlNone, NC, NC);
00132 #endif
00133     return SP_OK;
00134 }
00135 
00136 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
00137 {
00138     if (buffers == SP_BUF_BOTH)
00139         serial_clear(&port->uart);
00140     return SP_OK;
00141 }
00142 
00143 char *sp_get_port_name(const struct sp_port *port)
00144 {
00145     return port->name;
00146 }
00147 
00148 char *sp_get_port_description(const struct sp_port *port)
00149 {
00150     return port->description;
00151 }
00152 
00153 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
00154         size_t count, unsigned int timeout_ms)
00155 {
00156     size_t i;
00157     const unsigned char *ptr = buf;
00158 
00159     for (i = 0; i < count; i++)
00160         serial_putc(&port->uart, (int) ptr[i]);
00161 
00162     return count;
00163 }
00164 
00165 enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf,
00166         size_t count, unsigned int timeout_ms)
00167 {
00168     if (count == 0)
00169         return SP_ERR_ARG;
00170 
00171     *(char *) buf = (char) serial_getc(&port->uart);
00172     return 1;
00173 }
00174 
00175 int sp_last_error_code(void)
00176 {
00177     return 42;
00178 }