2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
23 #include "drivers/io.h"
29 MODE_RXTX
= MODE_RX
| MODE_TX
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
54 // Define known line control states which may be passed up by underlying serial driver callback
55 #define CTRL_LINE_STATE_DTR (1 << 0)
56 #define CTRL_LINE_STATE_RTS (1 << 1)
58 typedef void (*serialReceiveCallbackPtr
)(uint16_t data
, void *rxCallbackData
); // used by serial drivers to return frames to app
60 typedef struct serialPort_s
{
62 const struct serialPortVTable
*vTable
;
65 portOptions_e options
;
69 uint32_t rxBufferSize
;
70 uint32_t txBufferSize
;
71 volatile uint8_t *rxBuffer
;
72 volatile uint8_t *txBuffer
;
73 uint32_t rxBufferHead
;
74 uint32_t rxBufferTail
;
75 uint32_t txBufferHead
;
76 uint32_t txBufferTail
;
78 serialReceiveCallbackPtr rxCallback
;
84 #if defined(USE_SOFTSERIAL1) || defined(USE_SOFTSERIAL2)
85 # ifdef USE_SOFTSERIAL2
86 # define SERIAL_PORT_MAX_INDEX (RESOURCE_SOFT_OFFSET + 2)
88 # define SERIAL_PORT_MAX_INDEX (RESOURCE_SOFT_OFFSET + 1)
91 # define SERIAL_PORT_MAX_INDEX RESOURCE_SOFT_OFFSET
94 typedef struct serialPinConfig_s
{
95 ioTag_t ioTagTx
[SERIAL_PORT_MAX_INDEX
];
96 ioTag_t ioTagRx
[SERIAL_PORT_MAX_INDEX
];
97 ioTag_t ioTagInverter
[SERIAL_PORT_MAX_INDEX
];
100 PG_DECLARE(serialPinConfig_t
, serialPinConfig
);
102 struct serialPortVTable
{
103 void (*serialWrite
)(serialPort_t
*instance
, uint8_t ch
);
105 uint32_t (*serialTotalRxWaiting
)(const serialPort_t
*instance
);
106 uint32_t (*serialTotalTxFree
)(const serialPort_t
*instance
);
108 uint8_t (*serialRead
)(serialPort_t
*instance
);
110 // Specified baud rate may not be allowed by an implementation, use serialGetBaudRate to determine actual baud rate in use.
111 void (*serialSetBaudRate
)(serialPort_t
*instance
, uint32_t baudRate
);
113 bool (*isSerialTransmitBufferEmpty
)(const serialPort_t
*instance
);
115 void (*setMode
)(serialPort_t
*instance
, portMode_e mode
);
116 void (*setCtrlLineStateCb
)(serialPort_t
*instance
, void (*cb
)(void *instance
, uint16_t ctrlLineState
), void *context
);
117 void (*setBaudRateCb
)(serialPort_t
*instance
, void (*cb
)(serialPort_t
*context
, uint32_t baud
), serialPort_t
*context
);
119 void (*writeBuf
)(serialPort_t
*instance
, const void *data
, int count
);
120 // Optional functions used to buffer large writes.
121 void (*beginWrite
)(serialPort_t
*instance
);
122 void (*endWrite
)(serialPort_t
*instance
);
125 void serialWrite(serialPort_t
*instance
, uint8_t ch
);
126 uint32_t serialRxBytesWaiting(const serialPort_t
*instance
);
127 uint32_t serialTxBytesFree(const serialPort_t
*instance
);
128 void serialWriteBuf(serialPort_t
*instance
, const uint8_t *data
, int count
);
129 uint8_t serialRead(serialPort_t
*instance
);
130 void serialSetBaudRate(serialPort_t
*instance
, uint32_t baudRate
);
131 void serialSetMode(serialPort_t
*instance
, portMode_e mode
);
132 void serialSetCtrlLineStateCb(serialPort_t
*instance
, void (*cb
)(void *context
, uint16_t ctrlLineState
), void *context
);
133 void serialSetBaudRateCb(serialPort_t
*instance
, void (*cb
)(serialPort_t
*context
, uint32_t baud
), serialPort_t
*context
);
134 bool isSerialTransmitBufferEmpty(const serialPort_t
*instance
);
135 void serialPrint(serialPort_t
*instance
, const char *str
);
136 uint32_t serialGetBaudRate(serialPort_t
*instance
);
138 // A shim that adapts the bufWriter API to the serialWriteBuf() API.
139 void serialWriteBufShim(void *instance
, const uint8_t *data
, int count
);
140 void serialBeginWrite(serialPort_t
*instance
);
141 void serialEndWrite(serialPort_t
*instance
);