accel3  2.0.0.0
accel3.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef ACCEL3_H
36 #define ACCEL3_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 #include "drv_spi_master.h"
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
53 #define ACCEL3_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
55  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
56  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
57  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
58  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
59  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
60  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
61 
67 #define ACCEL3_MASTER_I2C 0
68 #define ACCEL3_MASTER_SPI 1
69 
75 #define ACCEL3_RETVAL uint8_t
76 
77 #define ACCEL3_OK 0x00
78 #define ACCEL3_INIT_ERROR 0xFF
79 
85 #define ACCEL3_I2C_ADDRESS 0x18
86 
92 #define ACCEL3_REG_WHO_AM_I 0x0F
93 #define ACCEL3_REG_HP_FILTER_RESET 0x25
94 #define ACCEL3_REG_REFERENCE 0x26
95 #define ACCEL3_REG_STATUS 0x27
96 
102 #define ACCEL3_CTRL_REG1 0x20
103 #define ACCEL3_CTRL_REG2 0x21
104 #define ACCEL3_CTRL_REG3 0x22
105 #define ACCEL3_CTRL_REG4 0x23
106 #define ACCEL3_CTRL_REG5 0x24
107 
113 #define ACCEL3_OUT_X_L 0x28
114 #define ACCEL3_OUT_X_H 0x29
115 #define ACCEL3_OUT_Y_L 0x2A
116 #define ACCEL3_OUT_Y_H 0x2B
117 #define ACCEL3_OUT_Z_L 0x2C
118 #define ACCEL3_OUT_Z_H 0x2D
119 
125 #define ACCEL3_ENABLE_AXIS 0x27
126 #define ACCEL3_FULL_SCALE 0x00
127 #define ACCEL3_DATA_MASK 0x80
128 #define ACCEL3_DEVICE_IDENTIFIER 0x32
129 #define ACCEL3_TRUE 0x00
130 #define ACCEL3_FALSE 0x01
131 #define ACCEL3_START_VALUE 0x00
132  // End group macro
135 
136 // --------------------------------------------------------------- PUBLIC TYPES
145 typedef uint8_t accel3_select_t;
146 
150 typedef void ( *accel3_master_io_t )( struct accel3_s*, uint8_t, uint8_t*, uint8_t );
151 
155 typedef struct accel3_s
156 {
157  // Output pins
158 
159  digital_out_t int_pin;
160  digital_out_t cs;
161 
162  // Modules
163 
164  i2c_master_t i2c;
165  spi_master_t spi;
166 
167  // ctx variable
168 
169  uint8_t slave_address;
170  pin_name_t chip_select;
174 
176 
180 typedef struct
181 {
182  // Communication gpio pins
183 
184  pin_name_t scl;
185  pin_name_t sda;
186  pin_name_t miso;
187  pin_name_t mosi;
188  pin_name_t sck;
189  pin_name_t cs;
190 
191  // Additional gpio pins
192 
193  pin_name_t int_pin;
194 
195  // static variable
196 
197  uint32_t i2c_speed;
198  uint8_t i2c_address;
199  uint32_t spi_speed;
200  uint8_t spi_mode;
201  spi_master_chip_select_polarity_t cs_polarity;
202 
204 
205 } accel3_cfg_t;
206  // End types group
208 
217 typedef struct
218 {
219  int16_t x;
220  int16_t y;
221  int16_t z;
222 
223 } accel3_data_t;
224  // Measurement data
226 
227 // ------------------------------------------------------------------ CONSTANTS // End constants group
235 
236 // ------------------------------------------------------------------ VARIABLES // End variable group
244 
245 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
251 #ifdef __cplusplus
252 extern "C"{
253 #endif
254 
264 
274 
283 
294 void accel3_generic_write ( accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
295 
306 void accel3_generic_read ( accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
307 
315 uint8_t accel3_check_id ( accel3_t *ctx );
316 
327 
328 #ifdef __cplusplus
329 }
330 #endif
331 #endif // _ACCEL3_H_
332  // End public_function group
335 
336 // ------------------------------------------------------------------------- END
accel3_check_id
uint8_t accel3_check_id(accel3_t *ctx)
Check Accel 3 ID function.
accel3_s::slave_address
uint8_t slave_address
Definition: accel3.h:169
accel3_s::chip_select
pin_name_t chip_select
Definition: accel3.h:170
accel3_cfg_t
Click configuration structure definition.
Definition: accel3.h:181
accel3_generic_write
void accel3_generic_write(accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
accel3_cfg_t::spi_mode
uint8_t spi_mode
Definition: accel3.h:200
accel3_cfg_t::miso
pin_name_t miso
Definition: accel3.h:186
accel3_generic_read
void accel3_generic_read(accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
accel3_read_data
void accel3_read_data(accel3_t *ctx, accel3_data_t *accel3_data)
Get accelerometer data function.
accel3_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accel3.h:201
accel3_cfg_t::mosi
pin_name_t mosi
Definition: accel3.h:187
accel3_cfg_t::sda
pin_name_t sda
Definition: accel3.h:185
accel3_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel3.h:198
accel3_data
accel3_data_t accel3_data
Definition: main.c:32
accel3_s::cs
digital_out_t cs
Definition: accel3.h:160
ACCEL3_RETVAL
#define ACCEL3_RETVAL
Definition: accel3.h:75
accel3_s::master_sel
accel3_select_t master_sel
Definition: accel3.h:173
accel3_data_t
Accel data object definition.
Definition: accel3.h:218
accel3_t
struct accel3_s accel3_t
Click ctx object definition.
accel3_s::write_f
accel3_master_io_t write_f
Definition: accel3.h:171
accel3_select_t
uint8_t accel3_select_t
Communication type.
Definition: accel3.h:145
accel3_master_io_t
void(* accel3_master_io_t)(struct accel3_s *, uint8_t, uint8_t *, uint8_t)
Master Input/Output type.
Definition: accel3.h:150
accel3_s::read_f
accel3_master_io_t read_f
Definition: accel3.h:172
accel3_data_t::y
int16_t y
Definition: accel3.h:220
accel3_data_t::z
int16_t z
Definition: accel3.h:221
accel3_cfg_t::sel
accel3_select_t sel
Definition: accel3.h:203
accel3_cfg_t::cs
pin_name_t cs
Definition: accel3.h:189
accel3_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel3.h:199
accel3_data_t::x
int16_t x
Definition: accel3.h:219
accel3_cfg_t::int_pin
pin_name_t int_pin
Definition: accel3.h:193
accel3_s::spi
spi_master_t spi
Definition: accel3.h:165
accel3_s::i2c
i2c_master_t i2c
Definition: accel3.h:164
accel3_default_cfg
void accel3_default_cfg(accel3_t *ctx, accel3_cfg_t *cfg)
Click Default Configuration function.
accel3_init
ACCEL3_RETVAL accel3_init(accel3_t *ctx, accel3_cfg_t *cfg)
Initialization function.
accel3_s::int_pin
digital_out_t int_pin
Definition: accel3.h:159
accel3_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel3.h:197
accel3_cfg_t::sck
pin_name_t sck
Definition: accel3.h:188
accel3_cfg_t::scl
pin_name_t scl
Definition: accel3.h:184
accel3_s
Click ctx object definition.
Definition: accel3.h:156
accel3_cfg_setup
void accel3_cfg_setup(accel3_cfg_t *cfg)
Config Object Initialization function.