tfmini 2.0.0.0
tfmini.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 TFMINI_H
29#define TFMINI_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_i2c_master.h"
52#include "drv_uart.h"
53
74#define TFMINI_CMD_HEADER 0x5A
75#define TFMINI_CMD_ID_FW_VERSION 0x01
76#define TFMINI_CMD_ID_SYSTEM_RESET 0x02
77#define TFMINI_CMD_ID_FRAME_RATE 0x03
78#define TFMINI_CMD_ID_TRIGGER_DETECTION 0x04
79#define TFMINI_CMD_ID_OUTPUT_FORMAT 0x05
80#define TFMINI_CMD_ID_BAUD_RATE 0x06
81#define TFMINI_CMD_ID_ENABLE_OUTPUT 0x07
82#define TFMINI_CMD_ID_COM_SELECTION 0x0A
83#define TFMINI_CMD_ID_SLAVE_ADDRESS 0x0B
84#define TFMINI_CMD_ID_DATA_FRAME 0x00
85#define TFMINI_CMD_ID_IO_MODE_ENABLE 0x3B
86#define TFMINI_CMD_ID_RESTORE_FACTORY 0x10
87#define TFMINI_CMD_ID_SAVE_SETTINGS 0x11
88
93#define TFMINI_HEADER 0x59
94#define TFMINI_FRAME_SIZE 0x07
95#define TFMINI_MAX_BYTES_BEFORE_HEADER 100
96#define TFMINI_LOW_SIGNAL_STRENGTH -1
97#define TFMINI_SIGNAL_STRENGTH_SATURATION -2
98#define TFMINI_AMBIENT_LIGHT_SATURATION -4
99#define TFMINI_NO_HEADER_FOUND -5
100#define TFMINI_WRONG_CHECKSUM -6
101
107#define DRV_BUFFER_SIZE 500
108
114#define TFMINI_SET_DEV_ADDR 0x10
115
116 // tfmini_cmd
117
132#define TFMINI_MAP_MIKROBUS( cfg, mikrobus ) \
133 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
134 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
135 cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
136 cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX );
137
138 // tfmini_map
139 // tfmini
140
151
156typedef struct
157{
158 uint8_t cmd_id;
159 uint8_t payload[ TFMINI_FRAME_SIZE ];
160 uint8_t payload_len;
163
168typedef struct
169{
170 // Modules
171 i2c_master_t i2c;
172 uart_t uart;
174 // Buffers
175 char uart_rx_buffer[ DRV_BUFFER_SIZE ];
176 char uart_tx_buffer[ DRV_BUFFER_SIZE ];
181} tfmini_t;
182
187typedef struct
188{
189 // Communication gpio pins
190 pin_name_t scl;
191 pin_name_t sda;
192 pin_name_t rx_pin;
193 pin_name_t tx_pin;
195 // Static variable
196 uint32_t baud_rate;
198 uart_data_bits_t data_bit;
199 uart_parity_t parity_bit;
200 uart_stop_bits_t stop_bit;
201 uint32_t i2c_speed;
202 uint8_t i2c_address;
207
212typedef enum
213{
215 TFMINI_ERROR = -1
216
218
235
251
266err_t tfmini_init ( tfmini_t *ctx, tfmini_cfg_t *cfg );
267
282
297
314
327err_t tfmini_get_firmware_version ( tfmini_t *ctx, uint32_t *fw_version );
328
341
354err_t tfmini_set_frame_rate ( tfmini_t *ctx, uint16_t frame_rate );
355
373
386
399
418err_t tfmini_get_measurement ( tfmini_t *ctx, int16_t *distance, int16_t *strength, float *temperature );
419
420#ifdef __cplusplus
421}
422#endif
423#endif // TFMINI_H
424
425 // tfmini
426
427// ------------------------------------------------------------------------ END
#define DRV_BUFFER_SIZE
TFmini driver buffer size.
Definition tfmini.h:107
#define TFMINI_FRAME_SIZE
Definition tfmini.h:94
err_t tfmini_init(tfmini_t *ctx, tfmini_cfg_t *cfg)
TFmini initialization function.
err_t tfmini_change_interface(tfmini_t *ctx, tfmini_drv_t drv_sel)
TFmini change interface function.
err_t tfmini_default_cfg(tfmini_t *ctx)
TFmini default configuration function.
err_t tfmini_send_frame(tfmini_t *ctx, tfmini_frame_t frame)
TFmini send frame function.
err_t tfmini_factory_default(tfmini_t *ctx)
TFmini factory default function.
err_t tfmini_get_response(tfmini_t *ctx, tfmini_frame_t *frame)
TFmini get response function.
err_t tfmini_get_firmware_version(tfmini_t *ctx, uint32_t *fw_version)
TFmini get firmware version function.
err_t tfmini_set_frame_rate(tfmini_t *ctx, uint16_t frame_rate)
TFmini set frame rate function.
err_t tfmini_get_measurement(tfmini_t *ctx, int16_t *distance, int16_t *strength, float *temperature)
TFmini get measurement function.
void tfmini_drv_interface_selection(tfmini_cfg_t *cfg, tfmini_drv_t drv_sel)
TFmini driver interface setup function.
err_t tfmini_system_reset(tfmini_t *ctx)
TFmini system reset function.
void tfmini_cfg_setup(tfmini_cfg_t *cfg)
TFmini configuration object setup function.
err_t tfmini_save_settings(tfmini_t *ctx)
TFmini save settings function.
TFmini Click configuration object.
Definition tfmini.h:188
uint32_t baud_rate
Definition tfmini.h:196
uint32_t i2c_speed
Definition tfmini.h:201
bool uart_blocking
Definition tfmini.h:197
uart_data_bits_t data_bit
Definition tfmini.h:198
pin_name_t tx_pin
Definition tfmini.h:193
tfmini_drv_t drv_sel
Definition tfmini.h:204
pin_name_t rx_pin
Definition tfmini.h:192
uart_stop_bits_t stop_bit
Definition tfmini.h:200
pin_name_t scl
Definition tfmini.h:190
uart_parity_t parity_bit
Definition tfmini.h:199
pin_name_t sda
Definition tfmini.h:191
uint8_t i2c_address
Definition tfmini.h:202
TFmini Click command frame definition structure.
Definition tfmini.h:157
uint8_t cmd_id
Definition tfmini.h:158
uint8_t payload_len
Definition tfmini.h:160
TFmini Click context object.
Definition tfmini.h:169
uart_t uart
Definition tfmini.h:172
i2c_master_t i2c
Definition tfmini.h:171
tfmini_drv_t drv_sel
Definition tfmini.h:179
uint8_t slave_address
Definition tfmini.h:178
tfmini_drv_t
TFmini Click driver selector.
Definition tfmini.h:146
@ TFMINI_DRV_SEL_I2C
Definition tfmini.h:148
@ TFMINI_DRV_SEL_UART
Definition tfmini.h:147
tfmini_return_value_t
TFmini Click return value data.
Definition tfmini.h:213
@ TFMINI_ERROR
Definition tfmini.h:215
@ TFMINI_OK
Definition tfmini.h:214