xref: /btstack/src/btstack_uart_block.h (revision 18b8d0cbeeb8bf2cbacbbb18ec2f91488ef70093)
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 
56*18b8d0cbSMatthias Ringwald typedef enum {
57*18b8d0cbSMatthias Ringwald     // UART active, sleep off
58*18b8d0cbSMatthias Ringwald     BTSTACK_UART_SLEEP_OFF = 0,
59*18b8d0cbSMatthias Ringwald     // used for eHCILL
60*18b8d0cbSMatthias Ringwald     BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
61*18b8d0cbSMatthias Ringwald     // used for H5 and for eHCILL without support for wake on CTS pulse
62*18b8d0cbSMatthias Ringwald     BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE,
63*18b8d0cbSMatthias Ringwald 
64*18b8d0cbSMatthias Ringwald } btstack_uart_sleep_mode_t;
65*18b8d0cbSMatthias Ringwald 
66*18b8d0cbSMatthias Ringwald typedef enum {
67*18b8d0cbSMatthias Ringwald     BTSTACK_UART_SLEEP_MASK_HIGH_WAKE_ON_CTS_PULSE  = 1 << BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
68*18b8d0cbSMatthias Ringwald     BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE = 1 << BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE
69*18b8d0cbSMatthias Ringwald } btstack_uart_sleep_mode_mask_t;
70*18b8d0cbSMatthias 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 
118*18b8d0cbSMatthias Ringwald     // support for sleep modes in TI's H4 eHCILL and H5
119*18b8d0cbSMatthias Ringwald 
120*18b8d0cbSMatthias Ringwald     /**
121*18b8d0cbSMatthias Ringwald      * query supported wakeup mechanisms
122*18b8d0cbSMatthias Ringwald      * @return supported_sleep_modes mask
123*18b8d0cbSMatthias Ringwald      */
124*18b8d0cbSMatthias Ringwald      int (*get_supported_sleep_modes);
125*18b8d0cbSMatthias Ringwald 
126*18b8d0cbSMatthias Ringwald     /**
127*18b8d0cbSMatthias Ringwald      * set UART sleep mode - allows to turn off UART and it's clocks to save energy
128*18b8d0cbSMatthias Ringwald      * Supported sleep modes:
129*18b8d0cbSMatthias Ringwald      * - off: UART active, RTS low if receive_block was called and block not read yet
130*18b8d0cbSMatthias Ringwald      * - RTS high, wake on CTS: RTS should be high. On CTS pulse, UART gets enabled again and RTS goes to low
131*18b8d0cbSMatthias Ringwald      * - RTS low, wake on RX: data on RX will trigger UART enable, bytes might get lost
132*18b8d0cbSMatthias Ringwald      */
133*18b8d0cbSMatthias Ringwald     void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode);
134b52fe020SMatthias Ringwald 
135b52fe020SMatthias Ringwald } btstack_uart_block_t;
136b52fe020SMatthias Ringwald 
137b52fe020SMatthias Ringwald // common implementations
138b52fe020SMatthias Ringwald const btstack_uart_block_t * btstack_uart_block_posix_instance(void);
139084ad01cSMatthias Ringwald const btstack_uart_block_t * btstack_uart_block_embedded_instance(void);
140b52fe020SMatthias Ringwald 
141b52fe020SMatthias Ringwald #endif
142