/
Sangak
/
inav
Обзор
Документация
Войти
/
Sangak
/
inav
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/drivers/serial.h
129 строк
4 KB
JLP
Passthrough, USB Improvements
17 янв 2026, 17:15
17 янв 2026, 17:15
3bbc0ae
Код
Авторство
О чём код?
/* * This file is part of Cleanflight. * * Cleanflight is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Cleanflight is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>. */ #pragma once #include "drivers/io.h" typedef struct { ioTag_t rxPin; ioTag_t txPin; } serialPortPins_t; typedef enum portMode_t { MODE_RX = 1 << 0, MODE_TX = 1 << 1, MODE_RXTX = MODE_RX | MODE_TX } portMode_t; typedef enum portOptions_t { SERIAL_NOT_INVERTED = 0 << 0, SERIAL_INVERTED = 1 << 0, SERIAL_STOPBITS_1 = 0 << 1, SERIAL_STOPBITS_2 = 1 << 1, SERIAL_PARITY_NO = 0 << 2, SERIAL_PARITY_EVEN = 1 << 2, SERIAL_UNIDIR = 0 << 3, SERIAL_BIDIR = 1 << 3, /* * Note on SERIAL_BIDIR_PP * With SERIAL_BIDIR_PP, the very first start bit of back-to-back bytes * is lost and the first data byte will be lost by a framing error. * To ensure the first start bit to be sent, prepend a zero byte (0x00) * to actual data bytes. */ SERIAL_BIDIR_OD = 0 << 4, SERIAL_BIDIR_PP = 1 << 4, SERIAL_BIDIR_NOPULL = 1 << 5, // disable pulls in BIDIR RX mode SERIAL_BIDIR_UP = 0 << 5, // enable pullup in BIDIR mode SERIAL_LONGSTOP = 0 << 6, SERIAL_SHORTSTOP = 1 << 6, } portOptions_t; typedef void (*serialReceiveCallbackPtr)(uint16_t data, void *rxCallbackData); // used by serial drivers to return frames to app typedef struct serialPort_s { const struct serialPortVTable *vTable; uint8_t identifier; portMode_t mode; portOptions_t options; uint32_t baudRate; uint32_t rxBufferSize; uint32_t txBufferSize; volatile uint8_t *rxBuffer; volatile uint8_t *txBuffer; uint32_t rxBufferHead; uint32_t rxBufferTail; uint32_t txBufferHead; uint32_t txBufferTail; serialReceiveCallbackPtr rxCallback; void *rxCallbackData; } serialPort_t; struct serialPortVTable { void (*serialWrite)(serialPort_t *instance, uint8_t ch); uint32_t (*serialTotalRxWaiting)(const serialPort_t *instance); uint32_t (*serialTotalTxFree)(const serialPort_t *instance); uint8_t (*serialRead)(serialPort_t *instance); // Specified baud rate may not be allowed by an implementation, use serialGetBaudRate to determine actual baud rate in use. void (*serialSetBaudRate)(serialPort_t *instance, uint32_t baudRate); bool (*isSerialTransmitBufferEmpty)(const serialPort_t *instance); void (*setMode)(serialPort_t *instance, portMode_t mode); void (*setOptions)(serialPort_t *instance, portOptions_t options); void (*writeBuf)(serialPort_t *instance, const void *data, int count); bool (*isConnected)(const serialPort_t *instance); bool (*isIdle)(serialPort_t *instance); // Optional functions used to buffer large writes. void (*beginWrite)(serialPort_t *instance); void (*endWrite)(serialPort_t *instance); }; void serialWrite(serialPort_t *instance, uint8_t ch); uint32_t serialRxBytesWaiting(const serialPort_t *instance); uint32_t serialTxBytesFree(const serialPort_t *instance); void serialWriteBuf(serialPort_t *instance, const uint8_t *data, int count); uint8_t serialRead(serialPort_t *instance); uint32_t serialReadBuf(serialPort_t *instance, uint8_t *data, uint32_t maxLen); void serialSetBaudRate(serialPort_t *instance, uint32_t baudRate); void serialSetMode(serialPort_t *instance, portMode_t mode); void serialSetOptions(serialPort_t *instance, portOptions_t options); bool isSerialTransmitBufferEmpty(const serialPort_t *instance); void serialPrint(serialPort_t *instance, const char *str); uint32_t serialGetBaudRate(serialPort_t *instance); bool serialIsConnected(const serialPort_t *instance); bool serialIsIdle(serialPort_t *instance); // A shim that adapts the bufWriter API to the serialWriteBuf() API. void serialWriteBufShim(void *instance, const uint8_t *data, int count); void serialBeginWrite(serialPort_t *instance); void serialEndWrite(serialPort_t *instance);