xref: /btstack/src/btstack_uart.h (revision b28dc8004dd8d4fb9020a6dcd2bc81f05d36a008)
1 /*
2  * Copyright (C) 2021 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /**
39  * @title UART
40  *
41  * Common types for UART transports
42  *
43  */
44 
45 #ifndef BTSTACK_UART_H
46 #define BTSTACK_UART_H
47 
48 #include <stdint.h>
49 #include "btstack_config.h"
50 
51 #define BTSTACK_UART_PARITY_OFF  0
52 #define BTSTACK_UART_PARITY_EVEN 1
53 #define BTSTACK_UART_PARITY_ODD  2
54 
55 typedef enum {
56     // UART active, sleep off
57     BTSTACK_UART_SLEEP_OFF = 0,
58     // used for eHCILL
59     BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
60     // used for H5 and for eHCILL without support for wake on CTS pulse
61     BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE,
62 
63 } btstack_uart_sleep_mode_t;
64 
65 
66 typedef enum {
67     BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE  = 1 << BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
68     BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE     = 1 << BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE
69 } btstack_uart_sleep_mode_mask_t;
70 
71 // parity is mainly used with h5 mode.
72 typedef struct {
73     uint32_t              baudrate;
74     int                   flowcontrol;
75     const char *          device_name;
76     int                   parity;
77 } btstack_uart_config_t;
78 
79 /* API_START */
80 
81 typedef struct {
82     /**
83      * init transport
84      * @param uart_config
85      */
86     int (*init)(const btstack_uart_config_t * uart_config);
87 
88     /**
89      * open transport connection
90      */
91     int (*open)(void);
92 
93     /**
94      * close transport connection
95      */
96     int (*close)(void);
97 
98     /**
99      * set callback for block received. NULL disables callback
100      */
101     void (*set_block_received)(void (*block_handler)(void));
102 
103     /**
104      * set callback for sent. NULL disables callback
105      */
106     void (*set_block_sent)(void (*block_handler)(void));
107 
108     /**
109      * set baudrate
110      */
111     int (*set_baudrate)(uint32_t baudrate);
112 
113     /**
114      * set parity
115      */
116     int  (*set_parity)(int parity);
117 
118     /**
119      * set flowcontrol
120      */
121     int  (*set_flowcontrol)(int flowcontrol);
122 
123     /**
124      * receive block
125      */
126     void (*receive_block)(uint8_t *buffer, uint16_t len);
127 
128     /**
129      * send block
130      */
131     void (*send_block)(const uint8_t *buffer, uint16_t length);
132 
133 
134     /** Support for different Sleep Modes in TI's H4 eHCILL and in H5 - can be set to NULL if not used */
135 
136     /**
137      * query supported wakeup mechanisms
138      * @return supported_sleep_modes mask
139      */
140      int (*get_supported_sleep_modes)(void);
141 
142     /**
143      * set UART sleep mode - allows to turn off UART and it's clocks to save energy
144      * Supported sleep modes:
145      * - off: UART active, RTS low if receive_block was called and block not read yet
146      * - RTS high, wake on CTS: RTS should be high. On CTS pulse, UART gets enabled again and RTS goes to low
147      * - RTS low, wake on RX: data on RX will trigger UART enable, bytes might get lost
148      */
149     void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode);
150 
151     /**
152      * set wakeup handler - needed to notify hci transport of wakeup requests by Bluetooth controller
153      * Called upon CTS pulse or RX data. See sleep modes.
154      */
155     void (*set_wakeup_handler)(void (*wakeup_handler)(void));
156 
157 
158     /** Support for HCI H5 Transport Mode - can be set to NULL for H4 */
159 
160     /**
161      * H5/SLIP only: set callback for frame received. NULL disables callback
162      */
163     void (*set_frame_received)(void (*frame_handler)(uint16_t frame_size));
164 
165     /**
166      * H5/SLIP only: set callback for frame sent. NULL disables callback
167      */
168     void (*set_frame_sent)(void (*block_handler)(void));
169 
170     /**
171      * H5/SLIP only: receive SLIP frame
172      */
173     void (*receive_frame)(uint8_t *buffer, uint16_t len);
174 
175     /**
176      * H5/SLIP only: send SLIP frame
177      */
178     void (*send_frame)(const uint8_t *buffer, uint16_t length);
179 
180 } btstack_uart_t;
181 
182 /* API_END */
183 
184 // common implementations
185 const btstack_uart_t * btstack_uart_posix_instance(void);
186 
187 #endif
188