bt  2.0.0.0
bt.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef BT_H
29 #define BT_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_uart.h"
48 
69 #define BT_MSG_TYPE_COMMAND 0x20
70 #define BT_MSG_TYPE_RESPONSE 0x20
71 #define BT_MSG_TYPE_EVENT 0xA0
72 
77 #define BT_MSG_CLASS_DFU 0x00
78 #define BT_MSG_CLASS_SYSTEM 0x01
79 #define BT_MSG_CLASS_GAP 0x02
80 #define BT_MSG_CLASS_LE_GAP 0x03
81 #define BT_MSG_CLASS_RFCOMM 0x04
82 #define BT_MSG_CLASS_IDENTITY 0x05
83 #define BT_MSG_CLASS_SDP 0x06
84 #define BT_MSG_CLASS_CONNECTION 0x07
85 #define BT_MSG_CLASS_LE_CONNECTION 0x08
86 #define BT_MSG_CLASS_GATT 0x09
87 #define BT_MSG_CLASS_GATT_SERVER 0x0A
88 #define BT_MSG_CLASS_ENDPOINT 0x0B
89 #define BT_MSG_CLASS_HARDWARE 0x0C
90 #define BT_MSG_CLASS_FLASH_PS 0x0D
91 #define BT_MSG_CLASS_TEST 0x0E
92 #define BT_MSG_CLASS_SM 0x0F
93 #define BT_MSG_CLASS_UTIL 0x11
94 #define BT_MSG_CLASS_HID 0x13
95 #define BT_MSG_CLASS_LE_SERIAL 0x14
96 
102 #define BT_MSG_ID_SYSTEM_RESET 0x01
103 #define BT_MSG_ID_SYSTEM_GET_BT_ADDRESS 0x03
104 #define BT_MSG_ID_SYSTEM_GET_CLASS_OF_DEV 0x05
105 #define BT_MSG_ID_SYSTEM_SET_CLASS_OF_DEV 0x06
106 #define BT_MSG_ID_SYSTEM_FACTORY_RESET 0x07
107 #define BT_MSG_ID_SYSTEM_SET_LOCAL_NAME 0x08
108 #define BT_MSG_ID_SYSTEM_GET_LOCAL_NAME 0x09
109 #define BT_MSG_ID_SM_SET_BONDABLE_MODE 0x00
110 #define BT_MSG_ID_SM_DELETE_BONDINGS 0x07
111 #define BT_MSG_ID_GAP_SET_MODE 0x03
112 #define BT_MSG_ID_HARDWARE_CONFIG_GPIO 0x01
113 #define BT_MSG_ID_HARDWARE_WRITE_GPIO 0x02
114 #define BT_MSG_ID_HARDWARE_READ_GPIO 0x03
115 #define BT_MSG_ID_ENDPOINT_SEND_DATA 0x00
116 #define BT_MSG_ID_RFCOMM_START_SERVER 0x01
117 #define BT_MSG_ID_RFCOMM_STOP_SERVER 0x03
118 #define BT_MSG_ID_EVT_ENDPOINT_DATA 0x01
119 #define BT_MSG_ID_EVT_CONNECTION_OPENED 0x00
120 #define BT_MSG_ID_EVT_CONNECTION_CLOSED 0x01
121 #define BT_MSG_ID_EVT_SM_BONDED 0x03
122 #define BT_MSG_ID_EVT_RFCOMM_OPENED 0x00
123 #define BT_MSG_ID_EVT_RFCOMM_MODEM_STATUS 0x01
124 
129 #define BT_SYSTEM_RESET_NORMAL 0x00
130 #define BT_SYSTEM_RESET_BOOT_TO_DFU 0x01
131 
136 #define BT_SM_SET_BONDABLE_NOT_ACCEPTED 0x00
137 #define BT_SM_SET_BONDABLE_ALLOWED 0x01
138 
143 #define BT_GAP_MODE_NOT_CONNECTABLE 0x00
144 #define BT_GAP_MODE_CONNECTABLE 0x01
145 #define BT_GAP_MODE_NOT_DISCOVERABLE 0x00
146 #define BT_GAP_MODE_DISCOVERABLE 0x01
147 #define BT_GAP_MODE_NOT_LIMITED 0x00
148 #define BT_GAP_MODE_LIMITED 0x01
149 
154 #define BT_RFCOMM_SERVER_DEF_SDP_ID 0x02
155 #define BT_RFCOMM_SERVER_DEF_STREAM_DEST 0x00
156 
161 #define BT_HARDWARE_CONFIG_PORT_A 0x00
162 #define BT_HARDWARE_CONFIG_PORT_B 0x01
163 #define BT_HARDWARE_CONFIG_GPIO_FLOAT 0x00
164 #define BT_HARDWARE_CONFIG_GPIO_PULLUP 0x01
165 #define BT_HARDWARE_CONFIG_GPIO_PULLDOWN 0x02
166 #define BT_HARDWARE_CONFIG_GPIO_INPUT 0x00
167 #define BT_HARDWARE_CONFIG_GPIO_OUTPUT 0x01
168 #define BT_HARDWARE_CONFIG_GPIO_FUNCTION 0x02
169 #define BT_HARDWARE_CONFIG_GPIO_ANALOG 0x03
170 #define BT_HARDWARE_BUTTON1_PIN_INDEX 0x08
171 #define BT_HARDWARE_BUTTON2_PIN_INDEX 0x09
172 #define BT_HARDWARE_BUTTON3_PIN_INDEX 0x0A
173 #define BT_HARDWARE_BUTTON4_PIN_INDEX 0x0C
174 #define BT_HARDWARE_BUTTON5_PIN_INDEX 0x0D
175 #define BT_HARDWARE_LED1_PIN_INDEX 0x0E
176 #define BT_HARDWARE_LED2_PIN_INDEX 0x0F
177 #define BT_HARDWARE_BUTTON1_PIN_MASK 0x0100
178 #define BT_HARDWARE_BUTTON2_PIN_MASK 0x0200
179 #define BT_HARDWARE_BUTTON3_PIN_MASK 0x0400
180 #define BT_HARDWARE_BUTTON4_PIN_MASK 0x1000
181 #define BT_HARDWARE_BUTTON5_PIN_MASK 0x2000
182 #define BT_HARDWARE_ALL_BUTTONS_PIN_MASK 0x3700
183 #define BT_HARDWARE_LED1_PIN_MASK 0x4000
184 #define BT_HARDWARE_LED2_PIN_MASK 0x8000
185 #define BT_HARDWARE_ALL_LEDS_PIN_MASK 0xC000
186 
191 #define BT_MAX_PAYLOAD_LEN 0x40
192 
198 #define DRV_BUFFER_SIZE 200
199  // bt_set
201 
216 #define BT_MAP_MIKROBUS( cfg, mikrobus ) \
217  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
218  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
219  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN ); \
220  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
221  cfg.cts = MIKROBUS( mikrobus, MIKROBUS_CS ); \
222  cfg.bt0 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
223  cfg.rts = MIKROBUS( mikrobus, MIKROBUS_INT );
224  // bt_map // bt
227 
232 typedef struct
233 {
234  digital_out_t an;
235  digital_out_t rst;
236  digital_out_t cts;
237  digital_out_t bt0;
239  digital_in_t rts;
241  // Modules
242  uart_t uart;
244  // Buffers
245  char uart_rx_buffer[ DRV_BUFFER_SIZE ];
246  char uart_tx_buffer[ DRV_BUFFER_SIZE ];
248 } bt_t;
249 
254 typedef struct
255 {
256  // Communication gpio pins
257  pin_name_t rx_pin;
258  pin_name_t tx_pin;
260  // Additional gpio pins
261  pin_name_t an;
262  pin_name_t rst;
263  pin_name_t cts;
264  pin_name_t bt0;
265  pin_name_t rts;
267  // Static variable
268  uint32_t baud_rate;
270  uart_data_bits_t data_bit;
271  uart_parity_t parity_bit;
272  uart_stop_bits_t stop_bit;
274 } bt_cfg_t;
275 
280 typedef enum
281 {
282  BT_OK = 0,
283  BT_ERROR = -1
284 
286 
291 typedef struct
292 {
293  uint8_t msg_type;
294  uint8_t payload_len;
295  uint8_t msg_class;
296  uint8_t msg_id;
297  uint8_t payload[ BT_MAX_PAYLOAD_LEN ];
298 
299 } bt_package_t;
300 
316 void bt_cfg_setup ( bt_cfg_t *cfg );
317 
332 err_t bt_init ( bt_t *ctx, bt_cfg_t *cfg );
333 
347 err_t bt_default_cfg ( bt_t *ctx );
348 
362 err_t bt_generic_write ( bt_t *ctx, char *data_buf, uint16_t len );
363 
377 err_t bt_generic_read ( bt_t *ctx, char *data_buf, uint16_t max_len );
378 
388 void bt_set_an_pin ( bt_t *ctx, uint8_t state );
389 
399 void bt_set_rst_pin ( bt_t *ctx, uint8_t state );
400 
410 void bt_set_cts_pin ( bt_t *ctx, uint8_t state );
411 
421 void bt_set_bt0_pin ( bt_t *ctx, uint8_t state );
422 
431 uint8_t bt_get_rts_pin ( bt_t *ctx );
432 
446 err_t bt_send_package ( bt_t *ctx, bt_package_t pkg );
447 
462 err_t bt_read_package ( bt_t *ctx, bt_package_t *pkg );
463 
476 err_t bt_set_local_name ( bt_t *ctx, char *name );
477 
491 err_t bt_set_bondable_mode ( bt_t *ctx, uint8_t mode );
492 
504 err_t bt_delete_bondings ( bt_t *ctx );
505 
518 err_t bt_set_class_of_device ( bt_t *ctx, uint32_t cod );
519 
537 err_t bt_set_gap_mode ( bt_t *ctx, uint8_t connectable, uint8_t discoverable, uint8_t limited );
538 
552 err_t bt_rfcomm_start_server ( bt_t *ctx, uint8_t sdp_id, uint8_t stream_dest );
553 
566 err_t bt_rfcomm_stop_server ( bt_t *ctx, uint8_t sdp_id );
567 
579 err_t bt_factory_reset ( bt_t *ctx );
580 
594 err_t bt_system_reset ( bt_t *ctx, uint8_t mode );
595 
617 err_t bt_hardware_config_gpio ( bt_t *ctx, uint8_t port, uint16_t pin_index, uint8_t mode, uint8_t pullup );
618 
634 err_t bt_hardware_read_gpio ( bt_t *ctx, uint8_t port, uint16_t mask, uint16_t *port_data );
635 
651 err_t bt_hardware_write_gpio ( bt_t *ctx, uint8_t port, uint16_t mask, uint16_t port_data );
652 
667 err_t bt_endpoint_send_data ( bt_t *ctx, uint8_t *endpoint, char *msg );
668 
669 #ifdef __cplusplus
670 }
671 #endif
672 #endif // BT_H
673  // bt
675 
676 // ------------------------------------------------------------------------ END
bt_t::uart
uart_t uart
Definition: bt.h:242
bt_init
err_t bt_init(bt_t *ctx, bt_cfg_t *cfg)
BT initialization function.
bt_set_cts_pin
void bt_set_cts_pin(bt_t *ctx, uint8_t state)
BT set CTS pin function.
bt_hardware_read_gpio
err_t bt_hardware_read_gpio(bt_t *ctx, uint8_t port, uint16_t mask, uint16_t *port_data)
BT hardware read gpio function.
bt_t::cts
digital_out_t cts
Definition: bt.h:236
bt_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: bt.h:272
bt_rfcomm_stop_server
err_t bt_rfcomm_stop_server(bt_t *ctx, uint8_t sdp_id)
BT rfcomm stop server function.
bt_factory_reset
err_t bt_factory_reset(bt_t *ctx)
BT factory reset function.
bt_package_t::msg_id
uint8_t msg_id
Definition: bt.h:296
bt_t::rts
digital_in_t rts
Definition: bt.h:239
bt_read_package
err_t bt_read_package(bt_t *ctx, bt_package_t *pkg)
BT read package function.
BT_ERROR
@ BT_ERROR
Definition: bt.h:283
bt_set_gap_mode
err_t bt_set_gap_mode(bt_t *ctx, uint8_t connectable, uint8_t discoverable, uint8_t limited)
BT set gap mode function.
bt_set_local_name
err_t bt_set_local_name(bt_t *ctx, char *name)
BT set local name function.
bt_set_an_pin
void bt_set_an_pin(bt_t *ctx, uint8_t state)
BT set AN pin function.
bt_delete_bondings
err_t bt_delete_bondings(bt_t *ctx)
BT delete bondings function.
bt_set_rst_pin
void bt_set_rst_pin(bt_t *ctx, uint8_t state)
BT set RST pin function.
bt_system_reset
err_t bt_system_reset(bt_t *ctx, uint8_t mode)
BT system reset function.
bt_cfg_t::rst
pin_name_t rst
Definition: bt.h:262
bt_t::rst
digital_out_t rst
Definition: bt.h:235
bt_cfg_t::cts
pin_name_t cts
Definition: bt.h:263
bt_cfg_t::rx_pin
pin_name_t rx_pin
Definition: bt.h:257
bt_t::bt0
digital_out_t bt0
Definition: bt.h:237
bt_generic_write
err_t bt_generic_write(bt_t *ctx, char *data_buf, uint16_t len)
BT data writing function.
bt_rfcomm_start_server
err_t bt_rfcomm_start_server(bt_t *ctx, uint8_t sdp_id, uint8_t stream_dest)
BT rfcomm start server function.
bt_cfg_t::rts
pin_name_t rts
Definition: bt.h:265
bt_set_bt0_pin
void bt_set_bt0_pin(bt_t *ctx, uint8_t state)
BT set BT0 pin function.
bt_hardware_config_gpio
err_t bt_hardware_config_gpio(bt_t *ctx, uint8_t port, uint16_t pin_index, uint8_t mode, uint8_t pullup)
BT hardware config gpio function.
bt_hardware_write_gpio
err_t bt_hardware_write_gpio(bt_t *ctx, uint8_t port, uint16_t mask, uint16_t port_data)
BT hardware write gpio function.
bt_cfg_t::baud_rate
uint32_t baud_rate
Definition: bt.h:268
bt_default_cfg
err_t bt_default_cfg(bt_t *ctx)
BT default configuration function.
bt_package_t
BT Click packet structure object.
Definition: bt.h:292
BT_OK
@ BT_OK
Definition: bt.h:282
bt_cfg_t::uart_blocking
bool uart_blocking
Definition: bt.h:269
bt_set_bondable_mode
err_t bt_set_bondable_mode(bt_t *ctx, uint8_t mode)
BT set bondable mode function.
DRV_BUFFER_SIZE
#define DRV_BUFFER_SIZE
BT driver buffer size.
Definition: bt.h:198
bt_send_package
err_t bt_send_package(bt_t *ctx, bt_package_t pkg)
BT send package function.
bt_get_rts_pin
uint8_t bt_get_rts_pin(bt_t *ctx)
BT get RTS pin function.
bt_return_value_t
bt_return_value_t
BT Click return value data.
Definition: bt.h:281
bt_cfg_t::an
pin_name_t an
Definition: bt.h:261
bt_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: bt.h:271
bt_package_t::msg_type
uint8_t msg_type
Definition: bt.h:293
bt_package_t::msg_class
uint8_t msg_class
Definition: bt.h:295
bt_cfg_t::bt0
pin_name_t bt0
Definition: bt.h:264
bt_cfg_setup
void bt_cfg_setup(bt_cfg_t *cfg)
BT configuration object setup function.
bt_package_t::payload_len
uint8_t payload_len
Definition: bt.h:294
bt_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: bt.h:270
bt_t
BT Click context object.
Definition: bt.h:233
bt_set_class_of_device
err_t bt_set_class_of_device(bt_t *ctx, uint32_t cod)
BT set class of device function.
BT_MAX_PAYLOAD_LEN
#define BT_MAX_PAYLOAD_LEN
BT payload setting.
Definition: bt.h:191
bt_cfg_t::tx_pin
pin_name_t tx_pin
Definition: bt.h:258
bt_cfg_t
BT Click configuration object.
Definition: bt.h:255
bt_generic_read
err_t bt_generic_read(bt_t *ctx, char *data_buf, uint16_t max_len)
BT data reading function.
bt_t::an
digital_out_t an
Definition: bt.h:234
bt_endpoint_send_data
err_t bt_endpoint_send_data(bt_t *ctx, uint8_t *endpoint, char *msg)
BT endpoint send data function.