xref: /btstack/src/btstack_uart_block.h (revision df25739fc3ea5a0a90f0f5925e6461d653697d2e)
1 /*
2  * Copyright (C) 2016 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_block.h
40  *
41  *  Common code to access serial port via asynchronous block read/write commands
42  *
43  */
44 
45 #ifndef __BTSTACK_UART_BLOCK_H
46 #define __BTSTACK_UART_BLOCK_H
47 
48 #include <stdint.h>
49 
50 typedef struct {
51     uint32_t   baudrate;
52     int        flowcontrol;
53     const char *device_name;
54 } btstack_uart_config_t;
55 
56 typedef enum {
57     // UART active, sleep off
58     BTSTACK_UART_SLEEP_OFF = 0,
59     // used for eHCILL
60     BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
61     // used for H5 and for eHCILL without support for wake on CTS pulse
62     BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE,
63 
64 } btstack_uart_sleep_mode_t;
65 
66 typedef enum {
67     BTSTACK_UART_SLEEP_MASK_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 typedef struct {
72     /**
73      * init transport
74      * @param uart_config
75      */
76     int (*init)(const btstack_uart_config_t * uart_config);
77 
78     /**
79      * open transport connection
80      */
81     int (*open)(void);
82 
83     /**
84      * close transport connection
85      */
86     int (*close)(void);
87 
88     /**
89      * set callback for block received
90      */
91     void (*set_block_received)(void (*block_handler)(void));
92 
93     /**
94      * set callback for sent
95      */
96     void (*set_block_sent)(void (*block_handler)(void));
97 
98     /**
99      * set baudrate
100      */
101     int (*set_baudrate)(uint32_t baudrate);
102 
103     /**
104      * set parity
105      */
106     int  (*set_parity)(int parity);
107 
108     /**
109      * receive block
110      */
111     void (*receive_block)(uint8_t *buffer, uint16_t len);
112 
113     /**
114      * send block
115      */
116     void (*send_block)(const uint8_t *buffer, uint16_t length);
117 
118     // support for sleep modes in TI's H4 eHCILL and H5
119 
120     /**
121      * query supported wakeup mechanisms
122      * @return supported_sleep_modes mask
123      */
124      int (*get_supported_sleep_modes);
125 
126     /**
127      * set UART sleep mode - allows to turn off UART and it's clocks to save energy
128      * Supported sleep modes:
129      * - off: UART active, RTS low if receive_block was called and block not read yet
130      * - RTS high, wake on CTS: RTS should be high. On CTS pulse, UART gets enabled again and RTS goes to low
131      * - RTS low, wake on RX: data on RX will trigger UART enable, bytes might get lost
132      */
133     void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode);
134 
135 } btstack_uart_block_t;
136 
137 // common implementations
138 const btstack_uart_block_t * btstack_uart_block_posix_instance(void);
139 const btstack_uart_block_t * btstack_uart_block_embedded_instance(void);
140 
141 #endif
142