xref: /btstack/src/btstack_uart_block.h (revision c3dafd4604b9c4bb7427d6d98556c681fa2f50a6)
1b52fe020SMatthias Ringwald /*
2b52fe020SMatthias Ringwald  * Copyright (C) 2016 BlueKitchen GmbH
3b52fe020SMatthias Ringwald  *
4b52fe020SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5b52fe020SMatthias Ringwald  * modification, are permitted provided that the following conditions
6b52fe020SMatthias Ringwald  * are met:
7b52fe020SMatthias Ringwald  *
8b52fe020SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9b52fe020SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10b52fe020SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11b52fe020SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12b52fe020SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13b52fe020SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14b52fe020SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15b52fe020SMatthias Ringwald  *    from this software without specific prior written permission.
16b52fe020SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17b52fe020SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18b52fe020SMatthias Ringwald  *    monetary gain.
19b52fe020SMatthias Ringwald  *
20b52fe020SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21b52fe020SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22b52fe020SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23b52fe020SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24b52fe020SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25b52fe020SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26b52fe020SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27b52fe020SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28b52fe020SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29b52fe020SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30b52fe020SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b52fe020SMatthias Ringwald  * SUCH DAMAGE.
32b52fe020SMatthias Ringwald  *
33b52fe020SMatthias Ringwald  * Please inquire about commercial licensing options at
34b52fe020SMatthias Ringwald  * [email protected]
35b52fe020SMatthias Ringwald  *
36b52fe020SMatthias Ringwald  */
37b52fe020SMatthias Ringwald 
38b52fe020SMatthias Ringwald /*
39b52fe020SMatthias Ringwald  *  btstack_uart_block.h
40b52fe020SMatthias Ringwald  *
41b52fe020SMatthias Ringwald  *  Common code to access serial port via asynchronous block read/write commands
42b52fe020SMatthias Ringwald  *
43b52fe020SMatthias Ringwald  */
44b52fe020SMatthias Ringwald 
45b52fe020SMatthias Ringwald #ifndef __BTSTACK_UART_BLOCK_H
46b52fe020SMatthias Ringwald #define __BTSTACK_UART_BLOCK_H
47b52fe020SMatthias Ringwald 
48b52fe020SMatthias Ringwald #include <stdint.h>
49084ad01cSMatthias Ringwald 
50084ad01cSMatthias Ringwald typedef struct {
51084ad01cSMatthias Ringwald     uint32_t   baudrate;
52084ad01cSMatthias Ringwald     int        flowcontrol;
53084ad01cSMatthias Ringwald     const char *device_name;
54084ad01cSMatthias Ringwald } btstack_uart_config_t;
55b52fe020SMatthias Ringwald 
5618b8d0cbSMatthias Ringwald typedef enum {
5718b8d0cbSMatthias Ringwald     // UART active, sleep off
5818b8d0cbSMatthias Ringwald     BTSTACK_UART_SLEEP_OFF = 0,
5918b8d0cbSMatthias Ringwald     // used for eHCILL
6018b8d0cbSMatthias Ringwald     BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
6118b8d0cbSMatthias Ringwald     // used for H5 and for eHCILL without support for wake on CTS pulse
6218b8d0cbSMatthias Ringwald     BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE,
6318b8d0cbSMatthias Ringwald 
6418b8d0cbSMatthias Ringwald } btstack_uart_sleep_mode_t;
6518b8d0cbSMatthias Ringwald 
6618b8d0cbSMatthias Ringwald typedef enum {
67307a4fe3SMatthias Ringwald     BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE  = 1 << BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
6818b8d0cbSMatthias Ringwald     BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE     = 1 << BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE
6918b8d0cbSMatthias Ringwald } btstack_uart_sleep_mode_mask_t;
7018b8d0cbSMatthias Ringwald 
71b52fe020SMatthias Ringwald typedef struct {
72b52fe020SMatthias Ringwald     /**
73b52fe020SMatthias Ringwald      * init transport
74084ad01cSMatthias Ringwald      * @param uart_config
75b52fe020SMatthias Ringwald      */
76084ad01cSMatthias Ringwald     int (*init)(const btstack_uart_config_t * uart_config);
77b52fe020SMatthias Ringwald 
78b52fe020SMatthias Ringwald     /**
79b52fe020SMatthias Ringwald      * open transport connection
80b52fe020SMatthias Ringwald      */
81b52fe020SMatthias Ringwald     int (*open)(void);
82b52fe020SMatthias Ringwald 
83b52fe020SMatthias Ringwald     /**
84b52fe020SMatthias Ringwald      * close transport connection
85b52fe020SMatthias Ringwald      */
86b52fe020SMatthias Ringwald     int (*close)(void);
87b52fe020SMatthias Ringwald 
88b52fe020SMatthias Ringwald     /**
89b52fe020SMatthias Ringwald      * set callback for block received
90b52fe020SMatthias Ringwald      */
91b52fe020SMatthias Ringwald     void (*set_block_received)(void (*block_handler)(void));
92b52fe020SMatthias Ringwald 
93b52fe020SMatthias Ringwald     /**
94b52fe020SMatthias Ringwald      * set callback for sent
95b52fe020SMatthias Ringwald      */
96b52fe020SMatthias Ringwald     void (*set_block_sent)(void (*block_handler)(void));
97b52fe020SMatthias Ringwald 
98b52fe020SMatthias Ringwald     /**
99b52fe020SMatthias Ringwald      * set baudrate
100b52fe020SMatthias Ringwald      */
101b52fe020SMatthias Ringwald     int (*set_baudrate)(uint32_t baudrate);
102b52fe020SMatthias Ringwald 
103b52fe020SMatthias Ringwald     /**
104b52fe020SMatthias Ringwald      * set parity
105b52fe020SMatthias Ringwald      */
106b52fe020SMatthias Ringwald     int  (*set_parity)(int parity);
107b52fe020SMatthias Ringwald 
108b52fe020SMatthias Ringwald     /**
109b52fe020SMatthias Ringwald      * receive block
110b52fe020SMatthias Ringwald      */
111b52fe020SMatthias Ringwald     void (*receive_block)(uint8_t *buffer, uint16_t len);
112b52fe020SMatthias Ringwald 
113b52fe020SMatthias Ringwald     /**
114b52fe020SMatthias Ringwald      * send block
115b52fe020SMatthias Ringwald      */
116b52fe020SMatthias Ringwald     void (*send_block)(const uint8_t *buffer, uint16_t length);
117b52fe020SMatthias Ringwald 
11818b8d0cbSMatthias Ringwald     // support for sleep modes in TI's H4 eHCILL and H5
11918b8d0cbSMatthias Ringwald 
12018b8d0cbSMatthias Ringwald     /**
12118b8d0cbSMatthias Ringwald      * query supported wakeup mechanisms
12218b8d0cbSMatthias Ringwald      * @return supported_sleep_modes mask
12318b8d0cbSMatthias Ringwald      */
124307a4fe3SMatthias Ringwald      int (*get_supported_sleep_modes)(void);
12518b8d0cbSMatthias Ringwald 
12618b8d0cbSMatthias Ringwald     /**
12718b8d0cbSMatthias Ringwald      * set UART sleep mode - allows to turn off UART and it's clocks to save energy
12818b8d0cbSMatthias Ringwald      * Supported sleep modes:
12918b8d0cbSMatthias Ringwald      * - off: UART active, RTS low if receive_block was called and block not read yet
13018b8d0cbSMatthias Ringwald      * - RTS high, wake on CTS: RTS should be high. On CTS pulse, UART gets enabled again and RTS goes to low
13118b8d0cbSMatthias Ringwald      * - RTS low, wake on RX: data on RX will trigger UART enable, bytes might get lost
13218b8d0cbSMatthias Ringwald      */
13318b8d0cbSMatthias Ringwald     void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode);
134b52fe020SMatthias Ringwald 
135*c3dafd46SMatthias Ringwald     /**
136*c3dafd46SMatthias Ringwald      * set wakeup handler - needed to notify hci transport of wakeup requests by Bluetooth controller
137*c3dafd46SMatthias Ringwald      * Called upon CTS pulse or RX data. See sleep modes.
138*c3dafd46SMatthias Ringwald      */
139*c3dafd46SMatthias Ringwald     void (*set_wakeup_handler)(void (*wakeup_handler)(void));
140*c3dafd46SMatthias Ringwald 
141b52fe020SMatthias Ringwald } btstack_uart_block_t;
142b52fe020SMatthias Ringwald 
143b52fe020SMatthias Ringwald // common implementations
144b52fe020SMatthias Ringwald const btstack_uart_block_t * btstack_uart_block_posix_instance(void);
1450ad2d5dbSMatthias Ringwald const btstack_uart_block_t * btstack_uart_block_windows_instance(void);
146084ad01cSMatthias Ringwald const btstack_uart_block_t * btstack_uart_block_embedded_instance(void);
147b52fe020SMatthias Ringwald 
148b52fe020SMatthias Ringwald #endif
149