Merge remote-tracking branch 'upstream/master' into abo_RTH_sanity_fix
[inav.git] / src / main / drivers / serial.h
blobf9a5b263fa18838d502f7662af1219c203958dd9
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #pragma once
19 #include "drivers/io.h"
21 typedef struct {
22 ioTag_t rxPin;
23 ioTag_t txPin;
24 } serialPortPins_t;
26 typedef enum portMode_t {
27 MODE_RX = 1 << 0,
28 MODE_TX = 1 << 1,
29 MODE_RXTX = MODE_RX | MODE_TX
30 } portMode_t;
32 typedef enum portOptions_t {
33 SERIAL_NOT_INVERTED = 0 << 0,
34 SERIAL_INVERTED = 1 << 0,
35 SERIAL_STOPBITS_1 = 0 << 1,
36 SERIAL_STOPBITS_2 = 1 << 1,
37 SERIAL_PARITY_NO = 0 << 2,
38 SERIAL_PARITY_EVEN = 1 << 2,
39 SERIAL_UNIDIR = 0 << 3,
40 SERIAL_BIDIR = 1 << 3,
43 * Note on SERIAL_BIDIR_PP
44 * With SERIAL_BIDIR_PP, the very first start bit of back-to-back bytes
45 * is lost and the first data byte will be lost by a framing error.
46 * To ensure the first start bit to be sent, prepend a zero byte (0x00)
47 * to actual data bytes.
49 SERIAL_BIDIR_OD = 0 << 4,
50 SERIAL_BIDIR_PP = 1 << 4,
51 SERIAL_BIDIR_NOPULL = 1 << 5, // disable pulls in BIDIR RX mode
52 SERIAL_BIDIR_UP = 0 << 5, // enable pullup in BIDIR mode
53 } portOptions_t;
55 typedef void (*serialReceiveCallbackPtr)(uint16_t data, void *rxCallbackData); // used by serial drivers to return frames to app
57 typedef struct serialPort_s {
59 const struct serialPortVTable *vTable;
61 uint8_t identifier;
62 portMode_t mode;
63 portOptions_t options;
65 uint32_t baudRate;
67 uint32_t rxBufferSize;
68 uint32_t txBufferSize;
69 volatile uint8_t *rxBuffer;
70 volatile uint8_t *txBuffer;
71 uint32_t rxBufferHead;
72 uint32_t rxBufferTail;
73 uint32_t txBufferHead;
74 uint32_t txBufferTail;
76 serialReceiveCallbackPtr rxCallback;
77 void *rxCallbackData;
78 } serialPort_t;
80 struct serialPortVTable {
81 void (*serialWrite)(serialPort_t *instance, uint8_t ch);
83 uint32_t (*serialTotalRxWaiting)(const serialPort_t *instance);
84 uint32_t (*serialTotalTxFree)(const serialPort_t *instance);
86 uint8_t (*serialRead)(serialPort_t *instance);
88 // Specified baud rate may not be allowed by an implementation, use serialGetBaudRate to determine actual baud rate in use.
89 void (*serialSetBaudRate)(serialPort_t *instance, uint32_t baudRate);
91 bool (*isSerialTransmitBufferEmpty)(const serialPort_t *instance);
93 void (*setMode)(serialPort_t *instance, portMode_t mode);
95 void (*writeBuf)(serialPort_t *instance, const void *data, int count);
97 bool (*isConnected)(const serialPort_t *instance);
99 bool (*isIdle)(serialPort_t *instance);
101 // Optional functions used to buffer large writes.
102 void (*beginWrite)(serialPort_t *instance);
103 void (*endWrite)(serialPort_t *instance);
106 void serialWrite(serialPort_t *instance, uint8_t ch);
107 uint32_t serialRxBytesWaiting(const serialPort_t *instance);
108 uint32_t serialTxBytesFree(const serialPort_t *instance);
109 void serialWriteBuf(serialPort_t *instance, const uint8_t *data, int count);
110 uint8_t serialRead(serialPort_t *instance);
111 void serialSetBaudRate(serialPort_t *instance, uint32_t baudRate);
112 void serialSetMode(serialPort_t *instance, portMode_t mode);
113 bool isSerialTransmitBufferEmpty(const serialPort_t *instance);
114 void serialPrint(serialPort_t *instance, const char *str);
115 uint32_t serialGetBaudRate(serialPort_t *instance);
116 bool serialIsConnected(const serialPort_t *instance);
117 bool serialIsIdle(serialPort_t *instance);
119 // A shim that adapts the bufWriter API to the serialWriteBuf() API.
120 void serialWriteBufShim(void *instance, const uint8_t *data, int count);
121 void serialBeginWrite(serialPort_t *instance);
122 void serialEndWrite(serialPort_t *instance);