NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
serial.h
Go to the documentation of this file.
1 /*
2  * This file is part of Ninjaflight.
3  *
4  * Ninjaflight 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.
8  *
9  * Ninjaflight 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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Ninjaflight. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Generic serial port interface abstraction layer
20  */
21 
22 #pragma once
23 
24 typedef enum portMode_t {
25  MODE_RX = 1 << 0,
26  MODE_TX = 1 << 1,
28 } portMode_t;
29 
30 typedef enum portOptions_t {
32  SERIAL_INVERTED = 1 << 0,
35  SERIAL_PARITY_NO = 0 << 2,
37  SERIAL_UNIDIR = 0 << 3,
38  SERIAL_BIDIR = 1 << 3
40 
41 typedef void (*serialReceiveCallbackPtr)(uint16_t data); // used by serial drivers to return frames to app
42 
43 typedef struct serialPort_s {
44  const struct serial_port_ops *vTable;
45 
46  uint8_t identifier;
49 
50  uint32_t baudRate;
51 
52  uint32_t rxBufferSize;
53  uint32_t txBufferSize;
54  volatile uint8_t *rxBuffer;
55  volatile uint8_t *txBuffer;
56  uint32_t rxBufferHead;
57  uint32_t rxBufferTail;
58  uint32_t txBufferHead;
59  uint32_t txBufferTail;
60 
61  // FIXME rename member to rxCallback
63 } serialPort_t;
64 
66  void (*put)(serialPort_t *instance, uint8_t ch);
67 
68  uint8_t (*serialTotalRxWaiting)(serialPort_t *instance);
69  uint8_t (*serialTotalTxFree)(serialPort_t *instance);
70 
71  uint8_t (*serialRead)(serialPort_t *instance);
72 
73  // Specified baud rate may not be allowed by an implementation, use serialGetBaudRate to determine actual baud rate in use.
74  void (*serialSetBaudRate)(serialPort_t *instance, uint32_t baudRate);
75 
77 
78  void (*setMode)(serialPort_t *instance, portMode_t mode);
79 
80  void (*writeBuf)(serialPort_t *instance, void *data, int count);
81  // Optional functions used to buffer large writes.
82  void (*beginWrite)(serialPort_t *instance);
83  void (*endWrite)(serialPort_t *instance);
84 };
85 
86 void serialWrite(serialPort_t *instance, uint8_t ch);
87 uint8_t serialRxBytesWaiting(serialPort_t *instance);
88 uint8_t serialTxBytesFree(serialPort_t *instance);
89 void serialWriteBuf(serialPort_t *instance, uint8_t *data, int count);
90 uint8_t serialRead(serialPort_t *instance);
91 void serialSetBaudRate(serialPort_t *instance, uint32_t baudRate);
92 void serialSetMode(serialPort_t *instance, portMode_t mode);
94 void serialPrint(serialPort_t *instance, const char *str);
95 uint32_t serialGetBaudRate(serialPort_t *instance);
96 
97 // A shim that adapts the bufWriter API to the serialWriteBuf() API.
98 void serialWriteBufShim(void *instance, uint8_t *data, int count);
99 void serialBeginWrite(serialPort_t *instance);
100 void serialEndWrite(serialPort_t *instance);
void(* put)(serialPort_t *instance, uint8_t ch)
Definition: serial.h:66
void serialWrite(serialPort_t *instance, uint8_t ch)
Definition: serial.c:38
bool(* isSerialTransmitBufferEmpty)(serialPort_t *instance)
Definition: serial.h:76
uint8_t serialRead(serialPort_t *instance)
Definition: serial.c:68
void serialBeginWrite(serialPort_t *instance)
Definition: serial.c:93
uint32_t txBufferSize
Definition: serial.h:53
void serialPrint(serialPort_t *instance, const char *str)
Definition: serial.c:25
uint32_t rxBufferTail
Definition: serial.h:57
Definition: serial.h:35
Definition: serial.h:32
Definition: serial.h:31
uint8_t serialTxBytesFree(serialPort_t *instance)
Definition: serial.c:63
uint8_t mode
Definition: gimbal.h:49
void(* beginWrite)(serialPort_t *instance)
Definition: serial.h:82
serialReceiveCallbackPtr callback
Definition: serial.h:62
const struct serial_port_ops * vTable
Definition: serial.h:44
uint8_t identifier
Definition: serial.h:46
uint32_t txBufferTail
Definition: serial.h:59
uint32_t txBufferHead
Definition: serial.h:58
portMode_t
Definition: serial.h:24
volatile uint8_t * rxBuffer
Definition: serial.h:54
Definition: serial.h:37
portOptions_t
Definition: serial.h:30
Definition: serial.h:38
Definition: serial.h:25
void serialSetBaudRate(serialPort_t *instance, uint32_t baudRate)
Definition: serial.c:73
bool isSerialTransmitBufferEmpty(serialPort_t *instance)
Definition: serial.c:78
uint8_t serialRxBytesWaiting(serialPort_t *instance)
Definition: serial.c:58
void serialWriteBufShim(void *instance, uint8_t *data, int count)
Definition: serial.c:88
Definition: serial.h:34
Definition: serial.h:65
void(* endWrite)(serialPort_t *instance)
Definition: serial.h:83
void(* serialReceiveCallbackPtr)(uint16_t data)
Definition: serial.h:41
void serialEndWrite(serialPort_t *instance)
Definition: serial.c:99
Definition: serial.h:27
uint32_t serialGetBaudRate(serialPort_t *instance)
Definition: serial.c:33
uint8_t(* serialTotalRxWaiting)(serialPort_t *instance)
Definition: serial.h:68
uint8_t(* serialRead)(serialPort_t *instance)
Definition: serial.h:71
Definition: serial.h:36
struct serialPort_s serialPort_t
portOptions_t options
Definition: serial.h:48
void serialWriteBuf(serialPort_t *instance, uint8_t *data, int count)
Definition: serial.c:43
void(* setMode)(serialPort_t *instance, portMode_t mode)
Definition: serial.h:78
Definition: serial.h:33
Definition: serial.h:26
uint8_t(* serialTotalTxFree)(serialPort_t *instance)
Definition: serial.h:69
portMode_t mode
Definition: serial.h:47
uint32_t baudRate
Definition: serial.h:50
uint32_t rxBufferSize
Definition: serial.h:52
uint32_t rxBufferHead
Definition: serial.h:56
void(* writeBuf)(serialPort_t *instance, void *data, int count)
Definition: serial.h:80
volatile uint8_t * txBuffer
Definition: serial.h:55
uint16_t data
Definition: config.c:91
void(* serialSetBaudRate)(serialPort_t *instance, uint32_t baudRate)
Definition: serial.h:74
void serialSetMode(serialPort_t *instance, portMode_t mode)
Definition: serial.c:83
Definition: serial.h:43