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 
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_spi_master.h"
52 #include "spi_specifics.h"
53 
74 #define MAGNETO13_REG_ZERO_0 0x00
75 #define MAGNETO13_REG_ZERO_1 0x01
76 #define MAGNETO13_REG_BCT 0x02
77 #define MAGNETO13_REG_ET_YX 0x03
78 #define MAGNETO13_REG_MG_LT_HT_MG 0x06
79 #define MAGNETO13_REG_IRQM_RAR_HYST 0x07
80 #define MAGNETO13_REG_THR 0x08
81 #define MAGNETO13_REG_RD 0x09
82 #define MAGNETO13_REG_REF 0x0A
83 #define MAGNETO13_REG_TRISTATE_SPI_PULL 0x0D
84 #define MAGNETO13_REG_FW 0x0E
85 #define MAGNETO13_REG_ERR_PAR_MEM_NVM 0x1A
86 #define MAGNETO13_REG_M_GH_GL 0x1B
87  // magneto13_reg
89 
99 #define MAGNETO13_CMD_READ_ANGLE 0x00
100 #define MAGNETO13_CMD_CLEAR_ERR_FLAGS 0x01
101 #define MAGNETO13_CMD_READ_REGISTER 0x02
102 #define MAGNETO13_CMD_WRITE_REGISTER 0x04
103 #define MAGNETO13_CMD_RESTORE_ALL_NVM 0x05
104 #define MAGNETO13_CMD_STORE_ALL_NVM 0x06
105 #define MAGNETO13_CMD_STORE_SINGLE_NVM 0x07
106  // magneto13_cmd
108 
123 #define MAGNETO13_CMD_BIT_MASK 0x07
124 #define MAGNETO13_REG_BIT_MASK 0x1F
125 
130 #define MAGNETO13_TRIMMING_X_DIS 0x00
131 #define MAGNETO13_TRIMMING_X_EN 0x01
132 #define MAGNETO13_TRIMMING_Y_DIS 0x00
133 #define MAGNETO13_TRIMMING_Y_EN 0x02
134 
139 #define MAGNETO13_MG_LT_LVL_0 0x00
140 #define MAGNETO13_MG_LT_LVL_1 0x40
141 #define MAGNETO13_MG_LT_LVL_2 0x80
142 #define MAGNETO13_MG_LT_LVL_3 0xC0
143 #define MAGNETO13_MG_HT_LVL_0 0x00
144 #define MAGNETO13_MG_HT_LVL_1 0x10
145 #define MAGNETO13_MG_HT_LVL_2 0x20
146 #define MAGNETO13_MG_HT_LVL_3 0x30
147 #define MAGNETO13_MG_BIT_MASK 0xF0
148 #define MAGNETO13_MG_FLD_THD_DIS 0x00
149 #define MAGNETO13_MG_FLD_THD_EN 0x01
150 
155 #define MAGNETO13_FLD_ST_OK 0x00
156 #define MAGNETO13_FLD_ST_MGL 0x01
157 #define MAGNETO13_FLD_ST_MGH 0x02
158 #define MAGNETO13_FLD_ST_BIT_MASK 0x03
159 
168 #define MAGNETO13_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
169 #define MAGNETO13_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
170  // magneto13_set
172 
187 #define MAGNETO13_MAP_MIKROBUS( cfg, mikrobus ) \
188  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
189  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
190  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
191  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
192  cfg.err = MIKROBUS( mikrobus, MIKROBUS_AN ); \
193  cfg.nvm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
194  cfg.irq = MIKROBUS( mikrobus, MIKROBUS_INT )
195  // magneto13_map // magneto13
198 
203 typedef struct
204 {
205  // Input pins
206  digital_in_t err;
207  digital_in_t nvm;
208  digital_in_t irq;
210  // Modules
211  spi_master_t spi;
213  pin_name_t chip_select;
215 } magneto13_t;
216 
221 typedef struct
222 {
223  // Communication gpio pins
224  pin_name_t miso;
225  pin_name_t mosi;
226  pin_name_t sck;
227  pin_name_t cs;
229  // Additional gpio pins
230  pin_name_t err;
231  pin_name_t nvm;
232  pin_name_t irq;
234  // static variable
235  uint32_t spi_speed;
236  spi_master_mode_t spi_mode;
237  spi_master_chip_select_polarity_t cs_polarity;
240 
245 typedef enum
246 {
248  MAGNETO13_ERROR = -1
249 
251 
268 
283 
297 
312 err_t magneto13_write_reg ( magneto13_t *ctx, uint8_t cmd, uint8_t reg, uint8_t data_in );
313 
328 err_t magneto13_read_reg ( magneto13_t *ctx, uint8_t cmd, uint8_t reg, uint8_t *data_out );
329 
343 err_t magneto13_get_angle_raw_data ( magneto13_t *ctx, uint16_t *raw_data );
344 
358 err_t magneto13_get_angle ( magneto13_t *ctx, float *angle );
359 
374 err_t magneto13_set_mag_field_thd ( magneto13_t *ctx, uint8_t mglt, uint8_t mght );
375 
389 
406 err_t magneto13_get_field_strength ( magneto13_t *ctx, uint8_t *field_strength );
407 
420 
433 
447 
448 
449 
450 #ifdef __cplusplus
451 }
452 #endif
453 #endif // MAGNETO13_H
454  // magneto13
456 
457 // ------------------------------------------------------------------------ END
magneto13_cfg_t::sck
pin_name_t sck
Definition: magneto13.h:226
magneto13_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: magneto13.h:237
magneto13_t
Magneto 13 Click context object.
Definition: magneto13.h:204
magneto13_cfg_t::spi_speed
uint32_t spi_speed
Definition: magneto13.h:235
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:236
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:213
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:208
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:231
magneto13_cfg_t::irq
pin_name_t irq
Definition: magneto13.h:232
MAGNETO13_ERROR
@ MAGNETO13_ERROR
Definition: magneto13.h:248
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:227
magneto13_cfg_t::miso
pin_name_t miso
Definition: magneto13.h:224
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:247
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:225
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:246
magneto13_cfg_t
Magneto 13 Click configuration object.
Definition: magneto13.h:222
magneto13_t::spi
spi_master_t spi
Definition: magneto13.h:211
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:206
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:230
magneto13_t::nvm
digital_in_t nvm
Definition: magneto13.h:207
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.