magneto13  2.1.0.0
magneto13.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 MAGNETO13_H
29 #define MAGNETO13_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_spi_master.h"
48 #include "spi_specifics.h"
49 
70 #define MAGNETO13_REG_ZERO_0 0x00
71 #define MAGNETO13_REG_ZERO_1 0x01
72 #define MAGNETO13_REG_BCT 0x02
73 #define MAGNETO13_REG_ET_YX 0x03
74 #define MAGNETO13_REG_MG_LT_HT_MG 0x06
75 #define MAGNETO13_REG_IRQM_RAR_HYST 0x07
76 #define MAGNETO13_REG_THR 0x08
77 #define MAGNETO13_REG_RD 0x09
78 #define MAGNETO13_REG_REF 0x0A
79 #define MAGNETO13_REG_TRISTATE_SPI_PULL 0x0D
80 #define MAGNETO13_REG_FW 0x0E
81 #define MAGNETO13_REG_ERR_PAR_MEM_NVM 0x1A
82 #define MAGNETO13_REG_M_GH_GL 0x1B
83  // magneto13_reg
85 
95 #define MAGNETO13_CMD_READ_ANGLE 0x00
96 #define MAGNETO13_CMD_CLEAR_ERR_FLAGS 0x01
97 #define MAGNETO13_CMD_READ_REGISTER 0x02
98 #define MAGNETO13_CMD_WRITE_REGISTER 0x04
99 #define MAGNETO13_CMD_RESTORE_ALL_NVM 0x05
100 #define MAGNETO13_CMD_STORE_ALL_NVM 0x06
101 #define MAGNETO13_CMD_STORE_SINGLE_NVM 0x07
102  // magneto13_cmd
104 
119 #define MAGNETO13_CMD_BIT_MASK 0x07
120 #define MAGNETO13_REG_BIT_MASK 0x1F
121 
126 #define MAGNETO13_TRIMMING_X_DIS 0x00
127 #define MAGNETO13_TRIMMING_X_EN 0x01
128 #define MAGNETO13_TRIMMING_Y_DIS 0x00
129 #define MAGNETO13_TRIMMING_Y_EN 0x02
130 
135 #define MAGNETO13_MG_LT_LVL_0 0x00
136 #define MAGNETO13_MG_LT_LVL_1 0x40
137 #define MAGNETO13_MG_LT_LVL_2 0x80
138 #define MAGNETO13_MG_LT_LVL_3 0xC0
139 #define MAGNETO13_MG_HT_LVL_0 0x00
140 #define MAGNETO13_MG_HT_LVL_1 0x10
141 #define MAGNETO13_MG_HT_LVL_2 0x20
142 #define MAGNETO13_MG_HT_LVL_3 0x30
143 #define MAGNETO13_MG_BIT_MASK 0xF0
144 #define MAGNETO13_MG_FLD_THD_DIS 0x00
145 #define MAGNETO13_MG_FLD_THD_EN 0x01
146 
151 #define MAGNETO13_FLD_ST_OK 0x00
152 #define MAGNETO13_FLD_ST_MGL 0x01
153 #define MAGNETO13_FLD_ST_MGH 0x02
154 #define MAGNETO13_FLD_ST_BIT_MASK 0x03
155 
164 #define MAGNETO13_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
165 #define MAGNETO13_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
166  // magneto13_set
168 
183 #define MAGNETO13_MAP_MIKROBUS( cfg, mikrobus ) \
184  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
185  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
186  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
187  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
188  cfg.err = MIKROBUS( mikrobus, MIKROBUS_AN ); \
189  cfg.nvm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
190  cfg.irq = MIKROBUS( mikrobus, MIKROBUS_INT )
191  // magneto13_map // magneto13
194 
199 typedef struct
200 {
201  // Input pins
202  digital_in_t err;
203  digital_in_t nvm;
204  digital_in_t irq;
206  // Modules
207  spi_master_t spi;
209  pin_name_t chip_select;
211 } magneto13_t;
212 
217 typedef struct
218 {
219  // Communication gpio pins
220  pin_name_t miso;
221  pin_name_t mosi;
222  pin_name_t sck;
223  pin_name_t cs;
225  // Additional gpio pins
226  pin_name_t err;
227  pin_name_t nvm;
228  pin_name_t irq;
230  // static variable
231  uint32_t spi_speed;
232  spi_master_mode_t spi_mode;
233  spi_master_chip_select_polarity_t cs_polarity;
236 
241 typedef enum
242 {
244  MAGNETO13_ERROR = -1
245 
247 
264 
279 
293 
308 err_t magneto13_write_reg ( magneto13_t *ctx, uint8_t cmd, uint8_t reg, uint8_t data_in );
309 
324 err_t magneto13_read_reg ( magneto13_t *ctx, uint8_t cmd, uint8_t reg, uint8_t *data_out );
325 
339 err_t magneto13_get_angle_raw_data ( magneto13_t *ctx, uint16_t *raw_data );
340 
354 err_t magneto13_get_angle ( magneto13_t *ctx, float *angle );
355 
370 err_t magneto13_set_mag_field_thd ( magneto13_t *ctx, uint8_t mglt, uint8_t mght );
371 
385 
402 err_t magneto13_get_field_strength ( magneto13_t *ctx, uint8_t *field_strength );
403 
416 
429 
443 
444 
445 
446 #ifdef __cplusplus
447 }
448 #endif
449 #endif // MAGNETO13_H
450  // magneto13
452 
453 // ------------------------------------------------------------------------ END
magneto13_cfg_t::sck
pin_name_t sck
Definition: magneto13.h:222
magneto13_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: magneto13.h:233
magneto13_t
Magneto 13 Click context object.
Definition: magneto13.h:200
magneto13_cfg_t::spi_speed
uint32_t spi_speed
Definition: magneto13.h:231
spi_specifics.h
This file contains SPI specific macros, functions, etc.
magneto13_write_reg
err_t magneto13_write_reg(magneto13_t *ctx, uint8_t cmd, uint8_t reg, uint8_t data_in)
Magneto 13 data writing function.
magneto13_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: magneto13.h:232
magneto13_get_interrupt
uint8_t magneto13_get_interrupt(magneto13_t *ctx)
Magneto 13 gets the interrupt on the angle change state function.
magneto13_t::chip_select
pin_name_t chip_select
Definition: magneto13.h:209
magneto13_enable_fld_thd_det
err_t magneto13_enable_fld_thd_det(magneto13_t *ctx)
Magneto 13 enables the magnetic field threshold detection function.
magneto13_t::irq
digital_in_t irq
Definition: magneto13.h:204
magneto13_init
err_t magneto13_init(magneto13_t *ctx, magneto13_cfg_t *cfg)
Magneto 13 initialization function.
magneto13_cfg_t::nvm
pin_name_t nvm
Definition: magneto13.h:227
magneto13_cfg_t::irq
pin_name_t irq
Definition: magneto13.h:228
MAGNETO13_ERROR
@ MAGNETO13_ERROR
Definition: magneto13.h:244
magneto13_set_mag_field_thd
err_t magneto13_set_mag_field_thd(magneto13_t *ctx, uint8_t mglt, uint8_t mght)
Magneto 13 sets the magnetic field threshold function.
magneto13_cfg_t::cs
pin_name_t cs
Definition: magneto13.h:223
magneto13_cfg_t::miso
pin_name_t miso
Definition: magneto13.h:220
magneto13_get_field_strength
err_t magneto13_get_field_strength(magneto13_t *ctx, uint8_t *field_strength)
Magneto 13 gets the magnetic field strength function.
MAGNETO13_OK
@ MAGNETO13_OK
Definition: magneto13.h:243
magneto13_read_reg
err_t magneto13_read_reg(magneto13_t *ctx, uint8_t cmd, uint8_t reg, uint8_t *data_out)
Magneto 13 data reading function.
magneto13_get_nvm_access_state
uint8_t magneto13_get_nvm_access_state(magneto13_t *ctx)
Magneto 13 checks the access to the NVM function.
magneto13_get_angle
err_t magneto13_get_angle(magneto13_t *ctx, float *angle)
Magneto 13 gets the angular position function.
magneto13_cfg_t::mosi
pin_name_t mosi
Definition: magneto13.h:221
magneto13_cfg_setup
void magneto13_cfg_setup(magneto13_cfg_t *cfg)
Magneto 13 configuration object setup function.
magneto13_return_value_t
magneto13_return_value_t
Magneto 13 Click return value data.
Definition: magneto13.h:242
magneto13_cfg_t
Magneto 13 Click configuration object.
Definition: magneto13.h:218
magneto13_t::spi
spi_master_t spi
Definition: magneto13.h:207
magneto13_default_cfg
err_t magneto13_default_cfg(magneto13_t *ctx)
Magneto 13 default configuration function.
magneto13_t::err
digital_in_t err
Definition: magneto13.h:202
magneto13_get_error_flag
uint8_t magneto13_get_error_flag(magneto13_t *ctx)
Magneto 13 gets the error flag function.
magneto13_cfg_t::err
pin_name_t err
Definition: magneto13.h:226
magneto13_t::nvm
digital_in_t nvm
Definition: magneto13.h:203
magneto13_get_angle_raw_data
err_t magneto13_get_angle_raw_data(magneto13_t *ctx, uint16_t *raw_data)
Magneto 13 gets the angle raw data function.