xref: /btstack/src/btstack_uart.h (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
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
23*2fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka 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 
5121c1535eSMatthias Ringwald #define BTSTACK_UART_PARITY_OFF  0
5221c1535eSMatthias Ringwald #define BTSTACK_UART_PARITY_EVEN 1
5321c1535eSMatthias Ringwald #define BTSTACK_UART_PARITY_ODD  2
5421c1535eSMatthias Ringwald 
5521c1535eSMatthias Ringwald typedef enum {
5621c1535eSMatthias Ringwald     // UART active, sleep off
5721c1535eSMatthias Ringwald     BTSTACK_UART_SLEEP_OFF = 0,
5821c1535eSMatthias Ringwald     // used for eHCILL
5921c1535eSMatthias Ringwald     BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
6021c1535eSMatthias Ringwald     // used for H5 and for eHCILL without support for wake on CTS pulse
6121c1535eSMatthias Ringwald     BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE,
6221c1535eSMatthias Ringwald 
6321c1535eSMatthias Ringwald } btstack_uart_sleep_mode_t;
6421c1535eSMatthias Ringwald 
6521c1535eSMatthias Ringwald 
6621c1535eSMatthias Ringwald typedef enum {
6721c1535eSMatthias Ringwald     BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE  = 1 << BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
6821c1535eSMatthias Ringwald     BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE     = 1 << BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE
6921c1535eSMatthias Ringwald } btstack_uart_sleep_mode_mask_t;
7021c1535eSMatthias Ringwald 
7121c1535eSMatthias Ringwald // parity is mainly used with h5 mode.
7221c1535eSMatthias Ringwald typedef struct {
7321c1535eSMatthias Ringwald     uint32_t              baudrate;
7421c1535eSMatthias Ringwald     int                   flowcontrol;
7521c1535eSMatthias Ringwald     const char *          device_name;
7621c1535eSMatthias Ringwald     int                   parity;
7721c1535eSMatthias Ringwald } btstack_uart_config_t;
7821c1535eSMatthias Ringwald 
79fe5a6c4eSMilanka Ringwald /* API_START */
80fe5a6c4eSMilanka Ringwald 
8121c1535eSMatthias Ringwald typedef struct {
8221c1535eSMatthias Ringwald     /**
8321c1535eSMatthias Ringwald      * init transport
8421c1535eSMatthias Ringwald      * @param uart_config
8521c1535eSMatthias Ringwald      */
8621c1535eSMatthias Ringwald     int (*init)(const btstack_uart_config_t * uart_config);
8721c1535eSMatthias Ringwald 
8821c1535eSMatthias Ringwald     /**
8921c1535eSMatthias Ringwald      * open transport connection
9021c1535eSMatthias Ringwald      */
9121c1535eSMatthias Ringwald     int (*open)(void);
9221c1535eSMatthias Ringwald 
9321c1535eSMatthias Ringwald     /**
9421c1535eSMatthias Ringwald      * close transport connection
9521c1535eSMatthias Ringwald      */
9621c1535eSMatthias Ringwald     int (*close)(void);
9721c1535eSMatthias Ringwald 
9821c1535eSMatthias Ringwald     /**
9921c1535eSMatthias Ringwald      * set callback for block received. NULL disables callback
10021c1535eSMatthias Ringwald      */
10121c1535eSMatthias Ringwald     void (*set_block_received)(void (*block_handler)(void));
10221c1535eSMatthias Ringwald 
10321c1535eSMatthias Ringwald     /**
10421c1535eSMatthias Ringwald      * set callback for sent. NULL disables callback
10521c1535eSMatthias Ringwald      */
10621c1535eSMatthias Ringwald     void (*set_block_sent)(void (*block_handler)(void));
10721c1535eSMatthias Ringwald 
10821c1535eSMatthias Ringwald     /**
10921c1535eSMatthias Ringwald      * set baudrate
11021c1535eSMatthias Ringwald      */
11121c1535eSMatthias Ringwald     int (*set_baudrate)(uint32_t baudrate);
11221c1535eSMatthias Ringwald 
11321c1535eSMatthias Ringwald     /**
11421c1535eSMatthias Ringwald      * set parity
11521c1535eSMatthias Ringwald      */
11621c1535eSMatthias Ringwald     int  (*set_parity)(int parity);
11721c1535eSMatthias Ringwald 
11821c1535eSMatthias Ringwald     /**
11921c1535eSMatthias Ringwald      * set flowcontrol
12021c1535eSMatthias Ringwald      */
12121c1535eSMatthias Ringwald     int  (*set_flowcontrol)(int flowcontrol);
12221c1535eSMatthias Ringwald 
12321c1535eSMatthias Ringwald     /**
12421c1535eSMatthias Ringwald      * receive block
12521c1535eSMatthias Ringwald      */
12621c1535eSMatthias Ringwald     void (*receive_block)(uint8_t *buffer, uint16_t len);
12721c1535eSMatthias Ringwald 
12821c1535eSMatthias Ringwald     /**
12921c1535eSMatthias Ringwald      * send block
13021c1535eSMatthias Ringwald      */
13121c1535eSMatthias Ringwald     void (*send_block)(const uint8_t *buffer, uint16_t length);
13221c1535eSMatthias Ringwald 
13321c1535eSMatthias Ringwald 
13421c1535eSMatthias Ringwald     /** Support for different Sleep Modes in TI's H4 eHCILL and in H5 - can be set to NULL if not used */
13521c1535eSMatthias Ringwald 
13621c1535eSMatthias Ringwald     /**
13721c1535eSMatthias Ringwald      * query supported wakeup mechanisms
13821c1535eSMatthias Ringwald      * @return supported_sleep_modes mask
13921c1535eSMatthias Ringwald      */
14021c1535eSMatthias Ringwald      int (*get_supported_sleep_modes)(void);
14121c1535eSMatthias Ringwald 
14221c1535eSMatthias Ringwald     /**
14321c1535eSMatthias Ringwald      * set UART sleep mode - allows to turn off UART and it's clocks to save energy
14421c1535eSMatthias Ringwald      * Supported sleep modes:
14521c1535eSMatthias Ringwald      * - off: UART active, RTS low if receive_block was called and block not read yet
14621c1535eSMatthias Ringwald      * - RTS high, wake on CTS: RTS should be high. On CTS pulse, UART gets enabled again and RTS goes to low
14721c1535eSMatthias Ringwald      * - RTS low, wake on RX: data on RX will trigger UART enable, bytes might get lost
14821c1535eSMatthias Ringwald      */
14921c1535eSMatthias Ringwald     void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode);
15021c1535eSMatthias Ringwald 
15121c1535eSMatthias Ringwald     /**
15221c1535eSMatthias Ringwald      * set wakeup handler - needed to notify hci transport of wakeup requests by Bluetooth controller
15321c1535eSMatthias Ringwald      * Called upon CTS pulse or RX data. See sleep modes.
15421c1535eSMatthias Ringwald      */
15521c1535eSMatthias Ringwald     void (*set_wakeup_handler)(void (*wakeup_handler)(void));
15621c1535eSMatthias Ringwald 
15721c1535eSMatthias Ringwald 
15821c1535eSMatthias Ringwald     /** Support for HCI H5 Transport Mode - can be set to NULL for H4 */
15921c1535eSMatthias Ringwald 
16021c1535eSMatthias Ringwald     /**
16121c1535eSMatthias Ringwald      * H5/SLIP only: set callback for frame received. NULL disables callback
16221c1535eSMatthias Ringwald      */
16321c1535eSMatthias Ringwald     void (*set_frame_received)(void (*frame_handler)(uint16_t frame_size));
16421c1535eSMatthias Ringwald 
16521c1535eSMatthias Ringwald     /**
16621c1535eSMatthias Ringwald      * H5/SLIP only: set callback for frame sent. NULL disables callback
16721c1535eSMatthias Ringwald      */
16821c1535eSMatthias Ringwald     void (*set_frame_sent)(void (*block_handler)(void));
16921c1535eSMatthias Ringwald 
17021c1535eSMatthias Ringwald     /**
17121c1535eSMatthias Ringwald      * H5/SLIP only: receive SLIP frame
17221c1535eSMatthias Ringwald      */
17321c1535eSMatthias Ringwald     void (*receive_frame)(uint8_t *buffer, uint16_t len);
17421c1535eSMatthias Ringwald 
17521c1535eSMatthias Ringwald     /**
17621c1535eSMatthias Ringwald      * H5/SLIP only: send SLIP frame
17721c1535eSMatthias Ringwald      */
17821c1535eSMatthias Ringwald     void (*send_frame)(const uint8_t *buffer, uint16_t length);
17921c1535eSMatthias Ringwald 
18021c1535eSMatthias Ringwald } btstack_uart_t;
18121c1535eSMatthias Ringwald 
182fe5a6c4eSMilanka Ringwald /* API_END */
183fe5a6c4eSMilanka Ringwald 
184f2cb8f20SMatthias Ringwald // common implementations
185f2cb8f20SMatthias Ringwald const btstack_uart_t * btstack_uart_posix_instance(void);
18621c1535eSMatthias Ringwald 
18721c1535eSMatthias Ringwald #endif
188