Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / serial.c
blobae6f34222818eda0e4fcb9497f790dd71567d0d3
1 /*
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)
8 * any later version.
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/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
26 #include "serial.h"
28 void serialPrint(serialPort_t *instance, const char *str)
30 uint8_t ch;
31 while ((ch = *(str++)) != 0) {
32 serialWrite(instance, ch);
36 uint32_t serialGetBaudRate(serialPort_t *instance)
38 return instance->baudRate;
41 void serialWrite(serialPort_t *instance, uint8_t ch)
43 instance->vTable->serialWrite(instance, ch);
47 void serialWriteBuf(serialPort_t *instance, const uint8_t *data, int count)
49 if (instance->vTable->writeBuf) {
50 instance->vTable->writeBuf(instance, data, count);
51 } else {
52 for (const uint8_t *p = data; count > 0; count--, p++) {
54 while (!serialTxBytesFree(instance)) {
57 serialWrite(instance, *p);
62 uint32_t serialRxBytesWaiting(const serialPort_t *instance)
64 return instance->vTable->serialTotalRxWaiting(instance);
67 uint32_t serialTxBytesFree(const serialPort_t *instance)
69 return instance->vTable->serialTotalTxFree(instance);
72 uint8_t serialRead(serialPort_t *instance)
74 return instance->vTable->serialRead(instance);
77 void serialSetBaudRate(serialPort_t *instance, uint32_t baudRate)
79 instance->vTable->serialSetBaudRate(instance, baudRate);
82 bool isSerialTransmitBufferEmpty(const serialPort_t *instance)
84 return instance->vTable->isSerialTransmitBufferEmpty(instance);
87 void serialSetMode(serialPort_t *instance, portMode_e mode)
89 instance->vTable->setMode(instance, mode);
92 void serialSetCtrlLineStateCb(serialPort_t *serialPort, void (*cb)(void *context, uint16_t ctrlLineState), void *context)
94 // If a callback routine for changes to control line state is supported by the underlying
95 // driver, then set the callback.
96 if (serialPort->vTable->setCtrlLineStateCb) {
97 serialPort->vTable->setCtrlLineStateCb(serialPort, cb, context);
101 void serialSetBaudRateCb(serialPort_t *serialPort, void (*cb)(serialPort_t *context, uint32_t baud), serialPort_t *context)
103 // If a callback routine for changes to baud rate is supported by the underlying
104 // driver, then set the callback.
105 if (serialPort->vTable->setBaudRateCb) {
106 serialPort->vTable->setBaudRateCb(serialPort, cb, context);
110 void serialWriteBufShim(void *instance, const uint8_t *data, int count)
112 serialWriteBuf((serialPort_t *)instance, data, count);
115 void serialBeginWrite(serialPort_t *instance)
117 if (instance->vTable->beginWrite)
118 instance->vTable->beginWrite(instance);
121 void serialEndWrite(serialPort_t *instance)
123 if (instance->vTable->endWrite)
124 instance->vTable->endWrite(instance);