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 "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 #include "drv_spi_master.h"
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
63 #define ACCEL3_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
65  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
66  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
67  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
68  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
69  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
70  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
71 
77 #define ACCEL3_MASTER_I2C 0
78 #define ACCEL3_MASTER_SPI 1
79 
85 #define ACCEL3_RETVAL uint8_t
86 
87 #define ACCEL3_OK 0x00
88 #define ACCEL3_INIT_ERROR 0xFF
89 
95 #define ACCEL3_I2C_ADDRESS 0x18
96 
102 #define ACCEL3_REG_WHO_AM_I 0x0F
103 #define ACCEL3_REG_HP_FILTER_RESET 0x25
104 #define ACCEL3_REG_REFERENCE 0x26
105 #define ACCEL3_REG_STATUS 0x27
106 
112 #define ACCEL3_CTRL_REG1 0x20
113 #define ACCEL3_CTRL_REG2 0x21
114 #define ACCEL3_CTRL_REG3 0x22
115 #define ACCEL3_CTRL_REG4 0x23
116 #define ACCEL3_CTRL_REG5 0x24
117 
123 #define ACCEL3_OUT_X_L 0x28
124 #define ACCEL3_OUT_X_H 0x29
125 #define ACCEL3_OUT_Y_L 0x2A
126 #define ACCEL3_OUT_Y_H 0x2B
127 #define ACCEL3_OUT_Z_L 0x2C
128 #define ACCEL3_OUT_Z_H 0x2D
129 
135 #define ACCEL3_ENABLE_AXIS 0x27
136 #define ACCEL3_FULL_SCALE 0x00
137 #define ACCEL3_DATA_MASK 0x80
138 #define ACCEL3_DEVICE_IDENTIFIER 0x32
139 #define ACCEL3_TRUE 0x00
140 #define ACCEL3_FALSE 0x01
141 #define ACCEL3_START_VALUE 0x00
142  // End group macro
145 
146 // --------------------------------------------------------------- PUBLIC TYPES
155 typedef uint8_t accel3_select_t;
156 
160 typedef void ( *accel3_master_io_t )( struct accel3_s*, uint8_t, uint8_t*, uint8_t );
161 
165 typedef struct accel3_s
166 {
167  // Output pins
168 
169  digital_out_t int_pin;
170  digital_out_t cs;
171 
172  // Modules
173 
174  i2c_master_t i2c;
175  spi_master_t spi;
176 
177  // ctx variable
178 
179  uint8_t slave_address;
180  pin_name_t chip_select;
184 
186 
190 typedef struct
191 {
192  // Communication gpio pins
193 
194  pin_name_t scl;
195  pin_name_t sda;
196  pin_name_t miso;
197  pin_name_t mosi;
198  pin_name_t sck;
199  pin_name_t cs;
200 
201  // Additional gpio pins
202 
203  pin_name_t int_pin;
204 
205  // static variable
206 
207  uint32_t i2c_speed;
208  uint8_t i2c_address;
209  uint32_t spi_speed;
210  uint8_t spi_mode;
211  spi_master_chip_select_polarity_t cs_polarity;
212 
214 
215 } accel3_cfg_t;
216  // End types group
218 
227 typedef struct
228 {
229  int16_t x;
230  int16_t y;
231  int16_t z;
232 
233 } accel3_data_t;
234  // Measurement data
236 
237 // ------------------------------------------------------------------ CONSTANTS // End constants group
245 
246 // ------------------------------------------------------------------ VARIABLES // End variable group
254 
255 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
261 #ifdef __cplusplus
262 extern "C"{
263 #endif
264 
274 
284 
293 
304 void accel3_generic_write ( accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
305 
316 void accel3_generic_read ( accel3_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
317 
325 uint8_t accel3_check_id ( accel3_t *ctx );
326 
337 
338 #ifdef __cplusplus
339 }
340 #endif
341 #endif // _ACCEL3_H_
342  // End public_function group
345 
346 // ------------------------------------------------------------------------- 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:179
accel3_s::chip_select
pin_name_t chip_select
Definition: accel3.h:180
accel3_cfg_t
Click configuration structure definition.
Definition: accel3.h:191
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:210
accel3_cfg_t::miso
pin_name_t miso
Definition: accel3.h:196
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:211
accel3_cfg_t::mosi
pin_name_t mosi
Definition: accel3.h:197
accel3_cfg_t::sda
pin_name_t sda
Definition: accel3.h:195
accel3_cfg_t::i2c_address
uint8_t i2c_address
Definition: accel3.h:208
accel3_data
accel3_data_t accel3_data
Definition: main.c:32
accel3_s::cs
digital_out_t cs
Definition: accel3.h:170
ACCEL3_RETVAL
#define ACCEL3_RETVAL
Definition: accel3.h:85
accel3_s::master_sel
accel3_select_t master_sel
Definition: accel3.h:183
accel3_data_t
Accel data object definition.
Definition: accel3.h:228
accel3_t
struct accel3_s accel3_t
Click ctx object definition.
accel3_s::write_f
accel3_master_io_t write_f
Definition: accel3.h:181
accel3_select_t
uint8_t accel3_select_t
Communication type.
Definition: accel3.h:155
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:160
accel3_s::read_f
accel3_master_io_t read_f
Definition: accel3.h:182
accel3_data_t::y
int16_t y
Definition: accel3.h:230
accel3_data_t::z
int16_t z
Definition: accel3.h:231
accel3_cfg_t::sel
accel3_select_t sel
Definition: accel3.h:213
accel3_cfg_t::cs
pin_name_t cs
Definition: accel3.h:199
accel3_cfg_t::spi_speed
uint32_t spi_speed
Definition: accel3.h:209
accel3_data_t::x
int16_t x
Definition: accel3.h:229
accel3_cfg_t::int_pin
pin_name_t int_pin
Definition: accel3.h:203
accel3_s::spi
spi_master_t spi
Definition: accel3.h:175
accel3_s::i2c
i2c_master_t i2c
Definition: accel3.h:174
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:169
accel3_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: accel3.h:207
accel3_cfg_t::sck
pin_name_t sck
Definition: accel3.h:198
accel3_cfg_t::scl
pin_name_t scl
Definition: accel3.h:194
accel3_s
Click ctx object definition.
Definition: accel3.h:166
accel3_cfg_setup
void accel3_cfg_setup(accel3_cfg_t *cfg)
Config Object Initialization function.