Merge branch 'master' into abo_waypoint_tracking
[inav.git] / src / main / drivers / serial.c
blob165f9c316989780351745304076b59134b643d1d
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 #include <stdbool.h>
19 #include <stdint.h>
21 #include "platform.h"
23 #include "serial.h"
25 void serialPrint(serialPort_t *instance, const char *str)
27 uint8_t ch;
28 while ((ch = *(str++)) != 0) {
29 serialWrite(instance, ch);
33 uint32_t serialGetBaudRate(serialPort_t *instance)
35 return instance->baudRate;
38 void serialWrite(serialPort_t *instance, uint8_t ch)
40 instance->vTable->serialWrite(instance, ch);
44 void serialWriteBuf(serialPort_t *instance, const uint8_t *data, int count)
46 if (instance->vTable->writeBuf) {
47 instance->vTable->writeBuf(instance, data, count);
48 } else {
49 for (const uint8_t *p = data; count > 0; count--, p++) {
51 while (!serialTxBytesFree(instance)) {
54 serialWrite(instance, *p);
59 uint32_t serialRxBytesWaiting(const serialPort_t *instance)
61 return instance->vTable->serialTotalRxWaiting(instance);
64 uint32_t serialTxBytesFree(const serialPort_t *instance)
66 return instance->vTable->serialTotalTxFree(instance);
69 uint8_t serialRead(serialPort_t *instance)
71 return instance->vTable->serialRead(instance);
74 void serialSetBaudRate(serialPort_t *instance, uint32_t baudRate)
76 instance->vTable->serialSetBaudRate(instance, baudRate);
79 bool isSerialTransmitBufferEmpty(const serialPort_t *instance)
81 return instance->vTable->isSerialTransmitBufferEmpty(instance);
84 void serialSetMode(serialPort_t *instance, portMode_t mode)
86 instance->vTable->setMode(instance, mode);
89 void serialWriteBufShim(void *instance, const uint8_t *data, int count)
91 serialWriteBuf((serialPort_t *)instance, data, count);
94 void serialBeginWrite(serialPort_t *instance)
96 if (instance->vTable->beginWrite)
97 instance->vTable->beginWrite(instance);
100 void serialEndWrite(serialPort_t *instance)
102 if (instance->vTable->endWrite)
103 instance->vTable->endWrite(instance);
106 bool serialIsConnected(const serialPort_t *instance)
108 if (instance->vTable->isConnected)
109 instance->vTable->isConnected(instance);
111 // If API is not defined - assume connected
112 return true;
115 bool serialIsIdle(serialPort_t *instance)
117 if (instance->vTable->isIdle)
118 return instance->vTable->isIdle(instance);
119 else
120 return false;