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"
24 #include "drivers/io_types.h"
25 #include "drivers/resource.h"
32 MODE_RXTX
= MODE_RX
| MODE_TX
36 SERIAL_NOT_INVERTED
= 0 << 0,
37 SERIAL_INVERTED
= 1 << 0,
38 SERIAL_STOPBITS_1
= 0 << 1,
39 SERIAL_STOPBITS_2
= 1 << 1,
40 SERIAL_PARITY_NO
= 0 << 2,
41 SERIAL_PARITY_EVEN
= 1 << 2,
42 SERIAL_UNIDIR
= 0 << 3,
43 SERIAL_BIDIR
= 1 << 3,
46 * Note on SERIAL_BIDIR_PP
47 * With SERIAL_BIDIR_PP, the very first start bit of back-to-back bytes
48 * is lost and the first data byte will be lost by a framing error.
49 * To ensure the first start bit to be sent, prepend a zero byte (0x00)
50 * to actual data bytes.
52 SERIAL_BIDIR_OD
= 0 << 4,
53 SERIAL_BIDIR_PP
= 1 << 4,
54 SERIAL_BIDIR_NOPULL
= 1 << 5, // disable pulls in BIDIR RX mode
55 SERIAL_BIDIR_PP_PD
= 1 << 6, // PP mode, normall inverted, but with PullDowns, to fix SA after bidir issue fixed (#10220)
58 // Define known line control states which may be passed up by underlying serial driver callback
59 #define CTRL_LINE_STATE_DTR (1 << 0)
60 #define CTRL_LINE_STATE_RTS (1 << 1)
62 typedef void (*serialReceiveCallbackPtr
)(uint16_t data
, void *rxCallbackData
); // used by serial drivers to return frames to app
63 typedef void (*serialIdleCallbackPtr
)();
65 typedef struct serialPort_s
{
67 const struct serialPortVTable
*vTable
;
70 portOptions_e options
;
74 uint32_t rxBufferSize
;
75 uint32_t txBufferSize
;
76 volatile uint8_t *rxBuffer
;
77 volatile uint8_t *txBuffer
;
78 uint32_t rxBufferHead
;
79 uint32_t rxBufferTail
;
80 uint32_t txBufferHead
;
81 uint32_t txBufferTail
;
83 serialReceiveCallbackPtr rxCallback
;
86 serialIdleCallbackPtr idleCallback
;
91 #if defined(USE_SOFTSERIAL1) || defined(USE_SOFTSERIAL2)
92 # ifdef USE_SOFTSERIAL2
93 # define SERIAL_PORT_MAX_INDEX (RESOURCE_SOFT_OFFSET + 2)
95 # define SERIAL_PORT_MAX_INDEX (RESOURCE_SOFT_OFFSET + 1)
98 # define SERIAL_PORT_MAX_INDEX RESOURCE_SOFT_OFFSET
101 typedef struct serialPinConfig_s
{
102 ioTag_t ioTagTx
[SERIAL_PORT_MAX_INDEX
];
103 ioTag_t ioTagRx
[SERIAL_PORT_MAX_INDEX
];
104 ioTag_t ioTagInverter
[SERIAL_PORT_MAX_INDEX
];
107 PG_DECLARE(serialPinConfig_t
, serialPinConfig
);
109 struct serialPortVTable
{
110 void (*serialWrite
)(serialPort_t
*instance
, uint8_t ch
);
112 uint32_t (*serialTotalRxWaiting
)(const serialPort_t
*instance
);
113 uint32_t (*serialTotalTxFree
)(const serialPort_t
*instance
);
115 uint8_t (*serialRead
)(serialPort_t
*instance
);
117 // Specified baud rate may not be allowed by an implementation, use serialGetBaudRate to determine actual baud rate in use.
118 void (*serialSetBaudRate
)(serialPort_t
*instance
, uint32_t baudRate
);
120 bool (*isSerialTransmitBufferEmpty
)(const serialPort_t
*instance
);
122 void (*setMode
)(serialPort_t
*instance
, portMode_e mode
);
123 void (*setCtrlLineStateCb
)(serialPort_t
*instance
, void (*cb
)(void *instance
, uint16_t ctrlLineState
), void *context
);
124 void (*setBaudRateCb
)(serialPort_t
*instance
, void (*cb
)(serialPort_t
*context
, uint32_t baud
), serialPort_t
*context
);
126 void (*writeBuf
)(serialPort_t
*instance
, const void *data
, int count
);
127 // Optional functions used to buffer large writes.
128 void (*beginWrite
)(serialPort_t
*instance
);
129 void (*endWrite
)(serialPort_t
*instance
);
132 void serialWrite(serialPort_t
*instance
, uint8_t ch
);
133 uint32_t serialRxBytesWaiting(const serialPort_t
*instance
);
134 uint32_t serialTxBytesFree(const serialPort_t
*instance
);
135 void serialWriteBuf(serialPort_t
*instance
, const uint8_t *data
, int count
);
136 uint8_t serialRead(serialPort_t
*instance
);
137 void serialSetBaudRate(serialPort_t
*instance
, uint32_t baudRate
);
138 void serialSetMode(serialPort_t
*instance
, portMode_e mode
);
139 void serialSetCtrlLineStateCb(serialPort_t
*instance
, void (*cb
)(void *context
, uint16_t ctrlLineState
), void *context
);
140 void serialSetBaudRateCb(serialPort_t
*instance
, void (*cb
)(serialPort_t
*context
, uint32_t baud
), serialPort_t
*context
);
141 bool isSerialTransmitBufferEmpty(const serialPort_t
*instance
);
142 void serialPrint(serialPort_t
*instance
, const char *str
);
143 uint32_t serialGetBaudRate(serialPort_t
*instance
);
145 // A shim that adapts the bufWriter API to the serialWriteBuf() API.
146 void serialWriteBufShim(void *instance
, const uint8_t *data
, int count
);
147 void serialBeginWrite(serialPort_t
*instance
);
148 void serialEndWrite(serialPort_t
*instance
);