bletiny  2.0.0.0
bletiny.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 BLETINY_H
29 #define BLETINY_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 #include "drv_analog_in.h"
49 
70 #define BLETINY_CMD_ATR "ATr"
71 #define BLETINY_CMD_ATZ "ATZ"
72 #define BLETINY_CMD_GAPDISCONNECT "AT+GAPDISCONNECT"
73 #define BLETINY_CMD_ATI "ATI"
74 #define BLETINY_CMD_ATRI "ATrI"
75 #define BLETINY_CMD_BDADDR "AT+BDADDR"
76 #define BLETINY_CMD_RSSI "AT+RSSI"
77 #define BLETINY_CMD_CHECK_MEM "AT+MEM=0"
78 #define BLETINY_CMD_CLEAR_MEM "AT+MEM=0,"
79  // bletiny_reg
81 
96 #define BLETINY_RSP_OK "OK"
97 #define BLETINY_RSP_ERROR "ERROR"
98 #define BLETINY_CONNECTED "+CONNECTED"
99 #define BLETINY_DISCONNECTED "+DISCONNECTED"
100 
105 #define BLETINY_SPI_CFG_SPEED_2MHZ 0
106 #define BLETINY_SPI_CFG_SPEED_4MHZ 1
107 #define BLETINY_SPI_CFG_SPEED_8MHZ 2
108 #define BLETINY_SPI_CFG_MODE0 0
109 #define BLETINY_SPI_CFG_MODE1 1
110 #define BLETINY_SPI_CFG_MODE2 2
111 #define BLETINY_SPI_CFG_MODE3 3
112 
117 #define BLETINY_I2C_CFG_SPEED_100KHZ 100
118 #define BLETINY_I2C_CFG_SPEED_400KHZ 400
119 #define BLETINY_I2C_CFG_REG_LEN_1BYTE 8
120 #define BLETINY_I2C_CFG_REG_LEN_1BYTES 16
121 
127 #define DRV_BUFFER_SIZE 500
128  // bletiny_set
130 
145 #define BLETINY_MAP_MIKROBUS( cfg, mikrobus ) \
146  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
147  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
148  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST );
149  // bletiny_map // bletiny
152 
157 typedef struct
158 {
159  // Output pins
160  digital_out_t rst;
162  // Modules
163  uart_t uart;
165  // Buffers
166  char uart_rx_buffer[ DRV_BUFFER_SIZE ];
167  char uart_tx_buffer[ DRV_BUFFER_SIZE ];
169 } bletiny_t;
170 
175 typedef struct
176 {
177  // Communication gpio pins
178  pin_name_t rx_pin;
179  pin_name_t tx_pin;
181  // Additional gpio pins
182  pin_name_t rst;
184  // Static variable
185  uint32_t baud_rate;
187  uart_data_bits_t data_bit;
188  uart_parity_t parity_bit;
189  uart_stop_bits_t stop_bit;
191 } bletiny_cfg_t;
192 
197 typedef enum
198 {
204 
206 
223 
238 err_t bletiny_init ( bletiny_t *ctx, bletiny_cfg_t *cfg );
239 
254 
268 err_t bletiny_generic_write ( bletiny_t *ctx, char *data_buf, uint16_t len );
269 
283 err_t bletiny_generic_read ( bletiny_t *ctx, char *data_buf, uint16_t max_len );
284 
294 void bletiny_send_cmd ( bletiny_t *ctx, char *cmd );
295 
306 void bletiny_spi_config ( bletiny_t *ctx, uint8_t speed, uint8_t mode );
307 
321 err_t bletiny_spi_write ( bletiny_t *ctx, char *data_buf, uint8_t len );
322 
336 err_t bletiny_spi_read ( bletiny_t *ctx, char *data_buf, uint8_t len );
337 
354 err_t bletiny_spi_write_then_read ( bletiny_t *ctx, uint8_t *data_write, uint8_t write_len, char *data_read, uint8_t read_len );
355 
366 void bletiny_i2c_config ( bletiny_t *ctx, uint8_t speed, uint8_t reg_len );
367 
382 err_t bletiny_i2c_write ( bletiny_t *ctx, uint8_t slave_address, uint16_t reg, uint8_t reg_data );
383 
399 err_t bletiny_i2c_read ( bletiny_t *ctx, uint8_t slave_address, uint16_t reg, uint8_t *data_buf, uint8_t len );
400 
401 #ifdef __cplusplus
402 }
403 #endif
404 #endif // BLETINY_H
405  // bletiny
407 
408 // ------------------------------------------------------------------------ END
bletiny_cfg_t::tx_pin
pin_name_t tx_pin
Definition: bletiny.h:179
BLETINY_ERROR_UNKNOWN
@ BLETINY_ERROR_UNKNOWN
Definition: bletiny.h:203
bletiny_generic_read
err_t bletiny_generic_read(bletiny_t *ctx, char *data_buf, uint16_t max_len)
BLE TINY data reading function.
bletiny_generic_write
err_t bletiny_generic_write(bletiny_t *ctx, char *data_buf, uint16_t len)
BLE TINY data writing function.
bletiny_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: bletiny.h:188
bletiny_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: bletiny.h:189
bletiny_t::uart
uart_t uart
Definition: bletiny.h:163
bletiny_cfg_t::rst
pin_name_t rst
Definition: bletiny.h:182
bletiny_t::rst
digital_out_t rst
Definition: bletiny.h:160
bletiny_init
err_t bletiny_init(bletiny_t *ctx, bletiny_cfg_t *cfg)
BLE TINY initialization function.
bletiny_cfg_t::rx_pin
pin_name_t rx_pin
Definition: bletiny.h:178
bletiny_return_value_t
bletiny_return_value_t
BLE TINY Click return value data.
Definition: bletiny.h:198
bletiny_cfg_t
BLE TINY Click configuration object.
Definition: bletiny.h:176
bletiny_cfg_t::baud_rate
uint32_t baud_rate
Definition: bletiny.h:185
DRV_BUFFER_SIZE
#define DRV_BUFFER_SIZE
BLE TINY driver buffer size.
Definition: bletiny.h:127
bletiny_cfg_t::uart_blocking
bool uart_blocking
Definition: bletiny.h:186
bletiny_send_cmd
void bletiny_send_cmd(bletiny_t *ctx, char *cmd)
Send command function.
bletiny_default_cfg
err_t bletiny_default_cfg(bletiny_t *ctx)
BLE TINY default configuration function.
bletiny_i2c_read
err_t bletiny_i2c_read(bletiny_t *ctx, uint8_t slave_address, uint16_t reg, uint8_t *data_buf, uint8_t len)
Send command to read data from slave device via I2C communication.
bletiny_cfg_setup
void bletiny_cfg_setup(bletiny_cfg_t *cfg)
BLE TINY configuration object setup function.
BLETINY_ERROR_TIMEOUT
@ BLETINY_ERROR_TIMEOUT
Definition: bletiny.h:201
bletiny_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: bletiny.h:187
bletiny_t
BLE TINY Click context object.
Definition: bletiny.h:158
bletiny_spi_read
err_t bletiny_spi_read(bletiny_t *ctx, char *data_buf, uint8_t len)
Send command to read data from slave device via SPI communication.
BLETINY_OK
@ BLETINY_OK
Definition: bletiny.h:199
BLETINY_ERROR
@ BLETINY_ERROR
Definition: bletiny.h:200
bletiny_i2c_config
void bletiny_i2c_config(bletiny_t *ctx, uint8_t speed, uint8_t reg_len)
Configure Click board for I2C master communication.
bletiny_spi_config
void bletiny_spi_config(bletiny_t *ctx, uint8_t speed, uint8_t mode)
Configure Click board for SPI master communication.
BLETINY_ERROR_RETURN
@ BLETINY_ERROR_RETURN
Definition: bletiny.h:202
bletiny_spi_write
err_t bletiny_spi_write(bletiny_t *ctx, char *data_buf, uint8_t len)
Send command to write data to slave device via SPI communication.
bletiny_spi_write_then_read
err_t bletiny_spi_write_then_read(bletiny_t *ctx, uint8_t *data_write, uint8_t write_len, char *data_read, uint8_t read_len)
Send command to write data to and then read data from slave device via SPI communication.
bletiny_i2c_write
err_t bletiny_i2c_write(bletiny_t *ctx, uint8_t slave_address, uint16_t reg, uint8_t reg_data)
Send command to write data to reg address of slave device via I2C communication.