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 #if defined __cplusplus 52 extern "C" { 53 #endif 54 55 #define BTSTACK_UART_PARITY_OFF 0 56 #define BTSTACK_UART_PARITY_EVEN 1 57 #define BTSTACK_UART_PARITY_ODD 2 58 59 typedef enum { 60 // UART active, sleep off 61 BTSTACK_UART_SLEEP_OFF = 0, 62 // used for eHCILL 63 BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE, 64 // used for H5 and for eHCILL without support for wake on CTS pulse 65 BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE, 66 67 } btstack_uart_sleep_mode_t; 68 69 70 typedef enum { 71 BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE = 1 << BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE, 72 BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE = 1 << BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE 73 } btstack_uart_sleep_mode_mask_t; 74 75 // parity is mainly used with h5 mode. 76 typedef struct { 77 uint32_t baudrate; 78 int flowcontrol; 79 const char * device_name; 80 int parity; 81 } btstack_uart_config_t; 82 83 /* API_START */ 84 85 typedef struct { 86 /** 87 * init transport 88 * @param uart_config 89 */ 90 int (*init)(const btstack_uart_config_t * uart_config); 91 92 /** 93 * open transport connection 94 */ 95 int (*open)(void); 96 97 /** 98 * close transport connection 99 */ 100 int (*close)(void); 101 102 /** 103 * set callback for block received. NULL disables callback 104 */ 105 void (*set_block_received)(void (*block_handler)(void)); 106 107 /** 108 * set callback for sent. NULL disables callback 109 */ 110 void (*set_block_sent)(void (*block_handler)(void)); 111 112 /** 113 * set baudrate 114 */ 115 int (*set_baudrate)(uint32_t baudrate); 116 117 /** 118 * set parity 119 */ 120 int (*set_parity)(int parity); 121 122 /** 123 * set flowcontrol 124 */ 125 int (*set_flowcontrol)(int flowcontrol); 126 127 /** 128 * receive block 129 */ 130 void (*receive_block)(uint8_t *buffer, uint16_t len); 131 132 /** 133 * send block 134 */ 135 void (*send_block)(const uint8_t *buffer, uint16_t length); 136 137 138 /** Support for different Sleep Modes in TI's H4 eHCILL and in H5 - can be set to NULL if not used */ 139 140 /** 141 * query supported wakeup mechanisms 142 * @return supported_sleep_modes mask 143 */ 144 int (*get_supported_sleep_modes)(void); 145 146 /** 147 * set UART sleep mode - allows to turn off UART and it's clocks to save energy 148 * Supported sleep modes: 149 * - off: UART active, RTS low if receive_block was called and block not read yet 150 * - RTS high, wake on CTS: RTS should be high. On CTS pulse, UART gets enabled again and RTS goes to low 151 * - RTS low, wake on RX: data on RX will trigger UART enable, bytes might get lost 152 */ 153 void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode); 154 155 /** 156 * set wakeup handler - needed to notify hci transport of wakeup requests by Bluetooth controller 157 * Called upon CTS pulse or RX data. See sleep modes. 158 */ 159 void (*set_wakeup_handler)(void (*wakeup_handler)(void)); 160 161 162 /** Support for HCI H5 Transport Mode - can be set to NULL for H4 */ 163 164 /** 165 * H5/SLIP only: set callback for frame received. NULL disables callback 166 */ 167 void (*set_frame_received)(void (*frame_handler)(uint16_t frame_size)); 168 169 /** 170 * H5/SLIP only: set callback for frame sent. NULL disables callback 171 */ 172 void (*set_frame_sent)(void (*block_handler)(void)); 173 174 /** 175 * H5/SLIP only: receive SLIP frame 176 */ 177 void (*receive_frame)(uint8_t *buffer, uint16_t len); 178 179 /** 180 * H5/SLIP only: send SLIP frame 181 */ 182 void (*send_frame)(const uint8_t *buffer, uint16_t length); 183 184 } btstack_uart_t; 185 186 /* API_END */ 187 188 // common implementations 189 const btstack_uart_t * btstack_uart_posix_instance(void); 190 191 #if defined __cplusplus 192 } 193 #endif 194 195 #endif 196