xref: /btstack/src/btstack_uart.h (revision cd5f23a3250874824c01a2b3326a9522fea3f99f)
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 MATTHIAS
24  * RINGWALD 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  *  btstack_uart.h
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 typedef struct {
80     /**
81      * init transport
82      * @param uart_config
83      */
84     int (*init)(const btstack_uart_config_t * uart_config);
85 
86     /**
87      * open transport connection
88      */
89     int (*open)(void);
90 
91     /**
92      * close transport connection
93      */
94     int (*close)(void);
95 
96     /**
97      * set callback for block received. NULL disables callback
98      */
99     void (*set_block_received)(void (*block_handler)(void));
100 
101     /**
102      * set callback for sent. NULL disables callback
103      */
104     void (*set_block_sent)(void (*block_handler)(void));
105 
106     /**
107      * set baudrate
108      */
109     int (*set_baudrate)(uint32_t baudrate);
110 
111     /**
112      * set parity
113      */
114     int  (*set_parity)(int parity);
115 
116     /**
117      * set flowcontrol
118      */
119     int  (*set_flowcontrol)(int flowcontrol);
120 
121     /**
122      * receive block
123      */
124     void (*receive_block)(uint8_t *buffer, uint16_t len);
125 
126     /**
127      * send block
128      */
129     void (*send_block)(const uint8_t *buffer, uint16_t length);
130 
131 
132     /** Support for different Sleep Modes in TI's H4 eHCILL and in H5 - can be set to NULL if not used */
133 
134     /**
135      * query supported wakeup mechanisms
136      * @return supported_sleep_modes mask
137      */
138      int (*get_supported_sleep_modes)(void);
139 
140     /**
141      * set UART sleep mode - allows to turn off UART and it's clocks to save energy
142      * Supported sleep modes:
143      * - off: UART active, RTS low if receive_block was called and block not read yet
144      * - RTS high, wake on CTS: RTS should be high. On CTS pulse, UART gets enabled again and RTS goes to low
145      * - RTS low, wake on RX: data on RX will trigger UART enable, bytes might get lost
146      */
147     void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode);
148 
149     /**
150      * set wakeup handler - needed to notify hci transport of wakeup requests by Bluetooth controller
151      * Called upon CTS pulse or RX data. See sleep modes.
152      */
153     void (*set_wakeup_handler)(void (*wakeup_handler)(void));
154 
155 
156     /** Support for HCI H5 Transport Mode - can be set to NULL for H4 */
157 
158     /**
159      * H5/SLIP only: set callback for frame received. NULL disables callback
160      */
161     void (*set_frame_received)(void (*frame_handler)(uint16_t frame_size));
162 
163     /**
164      * H5/SLIP only: set callback for frame sent. NULL disables callback
165      */
166     void (*set_frame_sent)(void (*block_handler)(void));
167 
168     /**
169      * H5/SLIP only: receive SLIP frame
170      */
171     void (*receive_frame)(uint8_t *buffer, uint16_t len);
172 
173     /**
174      * H5/SLIP only: send SLIP frame
175      */
176     void (*send_frame)(const uint8_t *buffer, uint16_t length);
177 
178 } btstack_uart_t;
179 
180 // common implementations
181 const btstack_uart_t * btstack_uart_posix_instance(void);
182 
183 #endif
184