A collection of Analog Devices drivers for the mbed platform
For additional information check out the mbed page of the Analog Devices wiki: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
libraries/libserialport/libserialport_fake.c@33:c3ec596a29c2, 2016-11-07 (annotated)
- Committer:
- Adrian Suciu
- Date:
- Mon Nov 07 16:27:12 2016 +0200
- Revision:
- 33:c3ec596a29c2
- Parent:
- 27:3c32495708f4
Added CN0391, CN0396 and CN0397 shields
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Paul Cercueil |
27:3c32495708f4 | 1 | /* |
Paul Cercueil |
27:3c32495708f4 | 2 | * Libserialport-fake |
Paul Cercueil |
27:3c32495708f4 | 3 | * |
Paul Cercueil |
27:3c32495708f4 | 4 | * This program is free software: you can redistribute it and/or modify |
Paul Cercueil |
27:3c32495708f4 | 5 | * it under the terms of the GNU Lesser General Public License as |
Paul Cercueil |
27:3c32495708f4 | 6 | * published by the Free Software Foundation, either version 3 of the |
Paul Cercueil |
27:3c32495708f4 | 7 | * License, or (at your option) any later version. |
Paul Cercueil |
27:3c32495708f4 | 8 | * |
Paul Cercueil |
27:3c32495708f4 | 9 | * This program is distributed in the hope that it will be useful, |
Paul Cercueil |
27:3c32495708f4 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Paul Cercueil |
27:3c32495708f4 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Paul Cercueil |
27:3c32495708f4 | 12 | * GNU General Public License for more details. |
Paul Cercueil |
27:3c32495708f4 | 13 | * |
Paul Cercueil |
27:3c32495708f4 | 14 | * You should have received a copy of the GNU Lesser General Public License |
Paul Cercueil |
27:3c32495708f4 | 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Paul Cercueil |
27:3c32495708f4 | 16 | */ |
Paul Cercueil |
27:3c32495708f4 | 17 | |
Paul Cercueil |
27:3c32495708f4 | 18 | #include <string.h> |
Paul Cercueil |
27:3c32495708f4 | 19 | |
Paul Cercueil |
27:3c32495708f4 | 20 | #include <mbed/serial_api.h> |
Paul Cercueil |
27:3c32495708f4 | 21 | |
Paul Cercueil |
27:3c32495708f4 | 22 | #include "libserialport.h" |
Paul Cercueil |
27:3c32495708f4 | 23 | |
Paul Cercueil |
27:3c32495708f4 | 24 | struct sp_port { |
Paul Cercueil |
27:3c32495708f4 | 25 | serial_t uart; |
Paul Cercueil |
27:3c32495708f4 | 26 | int data_bits, stop_bits; |
Paul Cercueil |
27:3c32495708f4 | 27 | SerialParity parity; |
Paul Cercueil |
27:3c32495708f4 | 28 | char *name, *description; |
Paul Cercueil |
27:3c32495708f4 | 29 | }; |
Paul Cercueil |
27:3c32495708f4 | 30 | |
Paul Cercueil |
27:3c32495708f4 | 31 | static struct sp_port sp_ports[5]; |
Paul Cercueil |
27:3c32495708f4 | 32 | |
Paul Cercueil |
27:3c32495708f4 | 33 | static void init_port(struct sp_port *port, PinName tx, PinName rx, |
Paul Cercueil |
27:3c32495708f4 | 34 | char *name, char *description) |
Paul Cercueil |
27:3c32495708f4 | 35 | { |
Paul Cercueil |
27:3c32495708f4 | 36 | serial_init(&port->uart, tx, rx); |
Paul Cercueil |
27:3c32495708f4 | 37 | serial_pinout_tx(tx); |
Paul Cercueil |
27:3c32495708f4 | 38 | |
Paul Cercueil |
27:3c32495708f4 | 39 | port->data_bits = 8; |
Paul Cercueil |
27:3c32495708f4 | 40 | port->stop_bits = 1; |
Paul Cercueil |
27:3c32495708f4 | 41 | port->parity = ParityNone; |
Paul Cercueil |
27:3c32495708f4 | 42 | port->name = name; |
Paul Cercueil |
27:3c32495708f4 | 43 | port->description = description; |
Paul Cercueil |
27:3c32495708f4 | 44 | } |
Paul Cercueil |
27:3c32495708f4 | 45 | |
Paul Cercueil |
27:3c32495708f4 | 46 | enum sp_return sp_get_port_by_name(const char *portname, |
Paul Cercueil |
27:3c32495708f4 | 47 | struct sp_port **port_ptr) |
Paul Cercueil |
27:3c32495708f4 | 48 | { |
Paul Cercueil |
27:3c32495708f4 | 49 | if (!strcmp(portname, "USB") || !strcmp(portname, "UART0")) { |
Paul Cercueil |
27:3c32495708f4 | 50 | init_port(&sp_ports[0], USBTX, USBRX, "UART0", "USB UART"); |
Paul Cercueil |
27:3c32495708f4 | 51 | *port_ptr = &sp_ports[0]; |
Paul Cercueil |
27:3c32495708f4 | 52 | return SP_OK; |
Paul Cercueil |
27:3c32495708f4 | 53 | } |
Paul Cercueil |
27:3c32495708f4 | 54 | |
Paul Cercueil |
27:3c32495708f4 | 55 | if (!strcmp(portname, "UART3")) { |
Paul Cercueil |
27:3c32495708f4 | 56 | init_port(&sp_ports[3], PTC17, PTC16, "UART3", "Arduino UART"); |
Paul Cercueil |
27:3c32495708f4 | 57 | *port_ptr = &sp_ports[3]; |
Paul Cercueil |
27:3c32495708f4 | 58 | return SP_OK; |
Paul Cercueil |
27:3c32495708f4 | 59 | } |
Paul Cercueil |
27:3c32495708f4 | 60 | |
Paul Cercueil |
27:3c32495708f4 | 61 | return SP_ERR_ARG; |
Paul Cercueil |
27:3c32495708f4 | 62 | } |
Paul Cercueil |
27:3c32495708f4 | 63 | |
Paul Cercueil |
27:3c32495708f4 | 64 | void sp_free_port(struct sp_port *port) |
Paul Cercueil |
27:3c32495708f4 | 65 | { |
Paul Cercueil |
27:3c32495708f4 | 66 | serial_free(&port->uart); |
Paul Cercueil |
27:3c32495708f4 | 67 | } |
Paul Cercueil |
27:3c32495708f4 | 68 | |
Paul Cercueil |
27:3c32495708f4 | 69 | enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) |
Paul Cercueil |
27:3c32495708f4 | 70 | { |
Paul Cercueil |
27:3c32495708f4 | 71 | if (flags != SP_MODE_READ_WRITE) |
Paul Cercueil |
27:3c32495708f4 | 72 | return SP_ERR_SUPP; |
Paul Cercueil |
27:3c32495708f4 | 73 | else |
Paul Cercueil |
27:3c32495708f4 | 74 | return SP_OK; |
Paul Cercueil |
27:3c32495708f4 | 75 | } |
Paul Cercueil |
27:3c32495708f4 | 76 | |
Paul Cercueil |
27:3c32495708f4 | 77 | enum sp_return sp_close(struct sp_port *port) |
Paul Cercueil |
27:3c32495708f4 | 78 | { |
Paul Cercueil |
27:3c32495708f4 | 79 | return SP_OK; |
Paul Cercueil |
27:3c32495708f4 | 80 | } |
Paul Cercueil |
27:3c32495708f4 | 81 | |
Paul Cercueil |
27:3c32495708f4 | 82 | enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate) |
Paul Cercueil |
27:3c32495708f4 | 83 | { |
Paul Cercueil |
27:3c32495708f4 | 84 | serial_baud(&port->uart, baudrate); |
Paul Cercueil |
27:3c32495708f4 | 85 | return SP_OK; |
Paul Cercueil |
27:3c32495708f4 | 86 | } |
Paul Cercueil |
27:3c32495708f4 | 87 | |
Paul Cercueil |
27:3c32495708f4 | 88 | enum sp_return sp_set_bits(struct sp_port *port, int bits) |
Paul Cercueil |
27:3c32495708f4 | 89 | { |
Paul Cercueil |
27:3c32495708f4 | 90 | port->data_bits = bits; |
Paul Cercueil |
27:3c32495708f4 | 91 | serial_format(&port->uart, bits, port->parity, port->stop_bits); |
Paul Cercueil |
27:3c32495708f4 | 92 | return SP_OK; |
Paul Cercueil |
27:3c32495708f4 | 93 | } |
Paul Cercueil |
27:3c32495708f4 | 94 | |
Paul Cercueil |
27:3c32495708f4 | 95 | enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits) |
Paul Cercueil |
27:3c32495708f4 | 96 | { |
Paul Cercueil |
27:3c32495708f4 | 97 | port->stop_bits = stopbits; |
Paul Cercueil |
27:3c32495708f4 | 98 | serial_format(&port->uart, port->data_bits, port->parity, stopbits); |
Paul Cercueil |
27:3c32495708f4 | 99 | return SP_OK; |
Paul Cercueil |
27:3c32495708f4 | 100 | } |
Paul Cercueil |
27:3c32495708f4 | 101 | |
Paul Cercueil |
27:3c32495708f4 | 102 | enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity) |
Paul Cercueil |
27:3c32495708f4 | 103 | { |
Paul Cercueil |
27:3c32495708f4 | 104 | switch (parity) { |
Paul Cercueil |
27:3c32495708f4 | 105 | case SP_PARITY_NONE: |
Paul Cercueil |
27:3c32495708f4 | 106 | port->parity = ParityNone; |
Paul Cercueil |
27:3c32495708f4 | 107 | break; |
Paul Cercueil |
27:3c32495708f4 | 108 | case SP_PARITY_ODD: |
Paul Cercueil |
27:3c32495708f4 | 109 | port->parity = ParityOdd; |
Paul Cercueil |
27:3c32495708f4 | 110 | break; |
Paul Cercueil |
27:3c32495708f4 | 111 | case SP_PARITY_EVEN: |
Paul Cercueil |
27:3c32495708f4 | 112 | port->parity = ParityEven; |
Paul Cercueil |
27:3c32495708f4 | 113 | break; |
Paul Cercueil |
27:3c32495708f4 | 114 | default: |
Paul Cercueil |
27:3c32495708f4 | 115 | return SP_ERR_SUPP; |
Paul Cercueil |
27:3c32495708f4 | 116 | } |
Paul Cercueil |
27:3c32495708f4 | 117 | |
Paul Cercueil |
27:3c32495708f4 | 118 | serial_format(&port->uart, port->data_bits, |
Paul Cercueil |
27:3c32495708f4 | 119 | port->parity, port->stop_bits); |
Paul Cercueil |
27:3c32495708f4 | 120 | return SP_OK; |
Paul Cercueil |
27:3c32495708f4 | 121 | } |
Paul Cercueil |
27:3c32495708f4 | 122 | |
Paul Cercueil |
27:3c32495708f4 | 123 | enum sp_return sp_set_flowcontrol( |
Paul Cercueil |
27:3c32495708f4 | 124 | struct sp_port *port, enum sp_flowcontrol flow) |
Paul Cercueil |
27:3c32495708f4 | 125 | { |
Paul Cercueil |
27:3c32495708f4 | 126 | if (flow != SP_FLOWCONTROL_NONE) |
Paul Cercueil |
27:3c32495708f4 | 127 | return SP_ERR_SUPP; |
Paul Cercueil |
27:3c32495708f4 | 128 | |
Paul Cercueil |
27:3c32495708f4 | 129 | #if 0 |
Paul Cercueil |
27:3c32495708f4 | 130 | /* This function is in serial_api.h, but it's not implemented anywhere */ |
Paul Cercueil |
27:3c32495708f4 | 131 | serial_set_flow_control(&port->uart, FlowControlNone, NC, NC); |
Paul Cercueil |
27:3c32495708f4 | 132 | #endif |
Paul Cercueil |
27:3c32495708f4 | 133 | return SP_OK; |
Paul Cercueil |
27:3c32495708f4 | 134 | } |
Paul Cercueil |
27:3c32495708f4 | 135 | |
Paul Cercueil |
27:3c32495708f4 | 136 | enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers) |
Paul Cercueil |
27:3c32495708f4 | 137 | { |
Paul Cercueil |
27:3c32495708f4 | 138 | if (buffers == SP_BUF_BOTH) |
Paul Cercueil |
27:3c32495708f4 | 139 | serial_clear(&port->uart); |
Paul Cercueil |
27:3c32495708f4 | 140 | return SP_OK; |
Paul Cercueil |
27:3c32495708f4 | 141 | } |
Paul Cercueil |
27:3c32495708f4 | 142 | |
Paul Cercueil |
27:3c32495708f4 | 143 | char *sp_get_port_name(const struct sp_port *port) |
Paul Cercueil |
27:3c32495708f4 | 144 | { |
Paul Cercueil |
27:3c32495708f4 | 145 | return port->name; |
Paul Cercueil |
27:3c32495708f4 | 146 | } |
Paul Cercueil |
27:3c32495708f4 | 147 | |
Paul Cercueil |
27:3c32495708f4 | 148 | char *sp_get_port_description(const struct sp_port *port) |
Paul Cercueil |
27:3c32495708f4 | 149 | { |
Paul Cercueil |
27:3c32495708f4 | 150 | return port->description; |
Paul Cercueil |
27:3c32495708f4 | 151 | } |
Paul Cercueil |
27:3c32495708f4 | 152 | |
Paul Cercueil |
27:3c32495708f4 | 153 | enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, |
Paul Cercueil |
27:3c32495708f4 | 154 | size_t count, unsigned int timeout_ms) |
Paul Cercueil |
27:3c32495708f4 | 155 | { |
Paul Cercueil |
27:3c32495708f4 | 156 | size_t i; |
Paul Cercueil |
27:3c32495708f4 | 157 | const unsigned char *ptr = buf; |
Paul Cercueil |
27:3c32495708f4 | 158 | |
Paul Cercueil |
27:3c32495708f4 | 159 | for (i = 0; i < count; i++) |
Paul Cercueil |
27:3c32495708f4 | 160 | serial_putc(&port->uart, (int) ptr[i]); |
Paul Cercueil |
27:3c32495708f4 | 161 | |
Paul Cercueil |
27:3c32495708f4 | 162 | return count; |
Paul Cercueil |
27:3c32495708f4 | 163 | } |
Paul Cercueil |
27:3c32495708f4 | 164 | |
Paul Cercueil |
27:3c32495708f4 | 165 | enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, |
Paul Cercueil |
27:3c32495708f4 | 166 | size_t count, unsigned int timeout_ms) |
Paul Cercueil |
27:3c32495708f4 | 167 | { |
Paul Cercueil |
27:3c32495708f4 | 168 | if (count == 0) |
Paul Cercueil |
27:3c32495708f4 | 169 | return SP_ERR_ARG; |
Paul Cercueil |
27:3c32495708f4 | 170 | |
Paul Cercueil |
27:3c32495708f4 | 171 | *(char *) buf = (char) serial_getc(&port->uart); |
Paul Cercueil |
27:3c32495708f4 | 172 | return 1; |
Paul Cercueil |
27:3c32495708f4 | 173 | } |
Paul Cercueil |
27:3c32495708f4 | 174 | |
Paul Cercueil |
27:3c32495708f4 | 175 | int sp_last_error_code(void) |
Paul Cercueil |
27:3c32495708f4 | 176 | { |
Paul Cercueil |
27:3c32495708f4 | 177 | return 42; |
Paul Cercueil |
27:3c32495708f4 | 178 | } |