xref: /btstack/src/btstack_uart.h (revision 9e104a3cf63fdbd212d138642b65a6147054c37b)
121c1535eSMatthias Ringwald /*
221c1535eSMatthias Ringwald  * Copyright (C) 2021 BlueKitchen GmbH
321c1535eSMatthias Ringwald  *
421c1535eSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
521c1535eSMatthias Ringwald  * modification, are permitted provided that the following conditions
621c1535eSMatthias Ringwald  * are met:
721c1535eSMatthias Ringwald  *
821c1535eSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
921c1535eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
1021c1535eSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
1121c1535eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
1221c1535eSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
1321c1535eSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
1421c1535eSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
1521c1535eSMatthias Ringwald  *    from this software without specific prior written permission.
1621c1535eSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
1721c1535eSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
1821c1535eSMatthias Ringwald  *    monetary gain.
1921c1535eSMatthias Ringwald  *
2021c1535eSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
2121c1535eSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2221c1535eSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2521c1535eSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2621c1535eSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2721c1535eSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2821c1535eSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2921c1535eSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3021c1535eSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3121c1535eSMatthias Ringwald  * SUCH DAMAGE.
3221c1535eSMatthias Ringwald  *
3321c1535eSMatthias Ringwald  * Please inquire about commercial licensing options at
3421c1535eSMatthias Ringwald  * [email protected]
3521c1535eSMatthias Ringwald  *
3621c1535eSMatthias Ringwald  */
3721c1535eSMatthias Ringwald 
38fe5a6c4eSMilanka Ringwald /**
39fe5a6c4eSMilanka Ringwald  * @title UART
4021c1535eSMatthias Ringwald  *
4121c1535eSMatthias Ringwald  * Common types for UART transports
4221c1535eSMatthias Ringwald  *
4321c1535eSMatthias Ringwald  */
4421c1535eSMatthias Ringwald 
4521c1535eSMatthias Ringwald #ifndef BTSTACK_UART_H
4621c1535eSMatthias Ringwald #define BTSTACK_UART_H
4721c1535eSMatthias Ringwald 
4821c1535eSMatthias Ringwald #include <stdint.h>
4921c1535eSMatthias Ringwald #include "btstack_config.h"
5021c1535eSMatthias Ringwald 
51*9e104a3cSpengyiqiang #if defined __cplusplus
52*9e104a3cSpengyiqiang extern "C" {
53*9e104a3cSpengyiqiang #endif
54*9e104a3cSpengyiqiang 
5521c1535eSMatthias Ringwald #define BTSTACK_UART_PARITY_OFF  0
5621c1535eSMatthias Ringwald #define BTSTACK_UART_PARITY_EVEN 1
5721c1535eSMatthias Ringwald #define BTSTACK_UART_PARITY_ODD  2
5821c1535eSMatthias Ringwald 
5921c1535eSMatthias Ringwald typedef enum {
6021c1535eSMatthias Ringwald     // UART active, sleep off
6121c1535eSMatthias Ringwald     BTSTACK_UART_SLEEP_OFF = 0,
6221c1535eSMatthias Ringwald     // used for eHCILL
6321c1535eSMatthias Ringwald     BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
6421c1535eSMatthias Ringwald     // used for H5 and for eHCILL without support for wake on CTS pulse
6521c1535eSMatthias Ringwald     BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE,
6621c1535eSMatthias Ringwald 
6721c1535eSMatthias Ringwald } btstack_uart_sleep_mode_t;
6821c1535eSMatthias Ringwald 
6921c1535eSMatthias Ringwald 
7021c1535eSMatthias Ringwald typedef enum {
7121c1535eSMatthias Ringwald     BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE  = 1 << BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
7221c1535eSMatthias Ringwald     BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE     = 1 << BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE
7321c1535eSMatthias Ringwald } btstack_uart_sleep_mode_mask_t;
7421c1535eSMatthias Ringwald 
7521c1535eSMatthias Ringwald // parity is mainly used with h5 mode.
7621c1535eSMatthias Ringwald typedef struct {
7721c1535eSMatthias Ringwald     uint32_t              baudrate;
7821c1535eSMatthias Ringwald     int                   flowcontrol;
7921c1535eSMatthias Ringwald     const char *          device_name;
8021c1535eSMatthias Ringwald     int                   parity;
8121c1535eSMatthias Ringwald } btstack_uart_config_t;
8221c1535eSMatthias Ringwald 
83fe5a6c4eSMilanka Ringwald /* API_START */
84fe5a6c4eSMilanka Ringwald 
8521c1535eSMatthias Ringwald typedef struct {
8621c1535eSMatthias Ringwald     /**
8721c1535eSMatthias Ringwald      * init transport
8821c1535eSMatthias Ringwald      * @param uart_config
8921c1535eSMatthias Ringwald      */
9021c1535eSMatthias Ringwald     int (*init)(const btstack_uart_config_t * uart_config);
9121c1535eSMatthias Ringwald 
9221c1535eSMatthias Ringwald     /**
9321c1535eSMatthias Ringwald      * open transport connection
9421c1535eSMatthias Ringwald      */
9521c1535eSMatthias Ringwald     int (*open)(void);
9621c1535eSMatthias Ringwald 
9721c1535eSMatthias Ringwald     /**
9821c1535eSMatthias Ringwald      * close transport connection
9921c1535eSMatthias Ringwald      */
10021c1535eSMatthias Ringwald     int (*close)(void);
10121c1535eSMatthias Ringwald 
10221c1535eSMatthias Ringwald     /**
10321c1535eSMatthias Ringwald      * set callback for block received. NULL disables callback
10421c1535eSMatthias Ringwald      */
10521c1535eSMatthias Ringwald     void (*set_block_received)(void (*block_handler)(void));
10621c1535eSMatthias Ringwald 
10721c1535eSMatthias Ringwald     /**
10821c1535eSMatthias Ringwald      * set callback for sent. NULL disables callback
10921c1535eSMatthias Ringwald      */
11021c1535eSMatthias Ringwald     void (*set_block_sent)(void (*block_handler)(void));
11121c1535eSMatthias Ringwald 
11221c1535eSMatthias Ringwald     /**
11321c1535eSMatthias Ringwald      * set baudrate
11421c1535eSMatthias Ringwald      */
11521c1535eSMatthias Ringwald     int (*set_baudrate)(uint32_t baudrate);
11621c1535eSMatthias Ringwald 
11721c1535eSMatthias Ringwald     /**
11821c1535eSMatthias Ringwald      * set parity
11921c1535eSMatthias Ringwald      */
12021c1535eSMatthias Ringwald     int  (*set_parity)(int parity);
12121c1535eSMatthias Ringwald 
12221c1535eSMatthias Ringwald     /**
12321c1535eSMatthias Ringwald      * set flowcontrol
12421c1535eSMatthias Ringwald      */
12521c1535eSMatthias Ringwald     int  (*set_flowcontrol)(int flowcontrol);
12621c1535eSMatthias Ringwald 
12721c1535eSMatthias Ringwald     /**
12821c1535eSMatthias Ringwald      * receive block
12921c1535eSMatthias Ringwald      */
13021c1535eSMatthias Ringwald     void (*receive_block)(uint8_t *buffer, uint16_t len);
13121c1535eSMatthias Ringwald 
13221c1535eSMatthias Ringwald     /**
13321c1535eSMatthias Ringwald      * send block
13421c1535eSMatthias Ringwald      */
13521c1535eSMatthias Ringwald     void (*send_block)(const uint8_t *buffer, uint16_t length);
13621c1535eSMatthias Ringwald 
13721c1535eSMatthias Ringwald 
13821c1535eSMatthias Ringwald     /** Support for different Sleep Modes in TI's H4 eHCILL and in H5 - can be set to NULL if not used */
13921c1535eSMatthias Ringwald 
14021c1535eSMatthias Ringwald     /**
14121c1535eSMatthias Ringwald      * query supported wakeup mechanisms
14221c1535eSMatthias Ringwald      * @return supported_sleep_modes mask
14321c1535eSMatthias Ringwald      */
14421c1535eSMatthias Ringwald      int (*get_supported_sleep_modes)(void);
14521c1535eSMatthias Ringwald 
14621c1535eSMatthias Ringwald     /**
14721c1535eSMatthias Ringwald      * set UART sleep mode - allows to turn off UART and it's clocks to save energy
14821c1535eSMatthias Ringwald      * Supported sleep modes:
14921c1535eSMatthias Ringwald      * - off: UART active, RTS low if receive_block was called and block not read yet
15021c1535eSMatthias Ringwald      * - RTS high, wake on CTS: RTS should be high. On CTS pulse, UART gets enabled again and RTS goes to low
15121c1535eSMatthias Ringwald      * - RTS low, wake on RX: data on RX will trigger UART enable, bytes might get lost
15221c1535eSMatthias Ringwald      */
15321c1535eSMatthias Ringwald     void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode);
15421c1535eSMatthias Ringwald 
15521c1535eSMatthias Ringwald     /**
15621c1535eSMatthias Ringwald      * set wakeup handler - needed to notify hci transport of wakeup requests by Bluetooth controller
15721c1535eSMatthias Ringwald      * Called upon CTS pulse or RX data. See sleep modes.
15821c1535eSMatthias Ringwald      */
15921c1535eSMatthias Ringwald     void (*set_wakeup_handler)(void (*wakeup_handler)(void));
16021c1535eSMatthias Ringwald 
16121c1535eSMatthias Ringwald 
16221c1535eSMatthias Ringwald     /** Support for HCI H5 Transport Mode - can be set to NULL for H4 */
16321c1535eSMatthias Ringwald 
16421c1535eSMatthias Ringwald     /**
16521c1535eSMatthias Ringwald      * H5/SLIP only: set callback for frame received. NULL disables callback
16621c1535eSMatthias Ringwald      */
16721c1535eSMatthias Ringwald     void (*set_frame_received)(void (*frame_handler)(uint16_t frame_size));
16821c1535eSMatthias Ringwald 
16921c1535eSMatthias Ringwald     /**
17021c1535eSMatthias Ringwald      * H5/SLIP only: set callback for frame sent. NULL disables callback
17121c1535eSMatthias Ringwald      */
17221c1535eSMatthias Ringwald     void (*set_frame_sent)(void (*block_handler)(void));
17321c1535eSMatthias Ringwald 
17421c1535eSMatthias Ringwald     /**
17521c1535eSMatthias Ringwald      * H5/SLIP only: receive SLIP frame
17621c1535eSMatthias Ringwald      */
17721c1535eSMatthias Ringwald     void (*receive_frame)(uint8_t *buffer, uint16_t len);
17821c1535eSMatthias Ringwald 
17921c1535eSMatthias Ringwald     /**
18021c1535eSMatthias Ringwald      * H5/SLIP only: send SLIP frame
18121c1535eSMatthias Ringwald      */
18221c1535eSMatthias Ringwald     void (*send_frame)(const uint8_t *buffer, uint16_t length);
18321c1535eSMatthias Ringwald 
18421c1535eSMatthias Ringwald } btstack_uart_t;
18521c1535eSMatthias Ringwald 
186fe5a6c4eSMilanka Ringwald /* API_END */
187fe5a6c4eSMilanka Ringwald 
188f2cb8f20SMatthias Ringwald // common implementations
189f2cb8f20SMatthias Ringwald const btstack_uart_t * btstack_uart_posix_instance(void);
19021c1535eSMatthias Ringwald 
191*9e104a3cSpengyiqiang #if defined __cplusplus
192*9e104a3cSpengyiqiang }
193*9e104a3cSpengyiqiang #endif
194*9e104a3cSpengyiqiang 
19521c1535eSMatthias Ringwald #endif
196