magneto14  2.1.0.0
magneto14.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 MAGNETO14_H
29 #define MAGNETO14_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 MAGNETO14_REG_ZERO_0 0x00
71 #define MAGNETO14_REG_ZERO_1 0x01
72 #define MAGNETO14_REG_BCT 0x02
73 #define MAGNETO14_REG_ET_YX 0x03
74 #define MAGNETO14_REG_PPT_ILIP 0x04
75 #define MAGNETO14_REG_PPT 0x05
76 #define MAGNETO14_REG_MG_LT_HT_MG 0x06
77 #define MAGNETO14_REG_RD 0x09
78 #define MAGNETO14_REG_FW 0x0E
79 #define MAGNETO14_REG_HYS 0x10
80 #define MAGNETO14_REG_M_GH_GL 0x1B
81  // magneto14_reg
83 
93 #define MAGNETO14_CMD_READ_ANGLE 0x00
94 #define MAGNETO14_CMD_READ_REGISTER 0x02
95 #define MAGNETO14_CMD_WRITE_REGISTER 0x04
96 #define MAGNETO14_CMD_RESTORE_ALL_NVM 0x05
97 #define MAGNETO14_CMD_STORE_ALL_NVM 0x06
98 #define MAGNETO14_CMD_STORE_SINGLE_NVM 0x07
99  // magneto14_cmd
101 
116 #define MAGNETO14_CMD_BIT_MASK 0x07
117 #define MAGNETO14_REG_BIT_MASK 0x1F
118 
123 #define MAGNETO14_TRIMMING_X_DIS 0x00
124 #define MAGNETO14_TRIMMING_X_EN 0x01
125 #define MAGNETO14_TRIMMING_Y_DIS 0x00
126 #define MAGNETO14_TRIMMING_Y_EN 0x02
127 
132 #define MAGNETO14_MG_LT_LVL_0 0x00
133 #define MAGNETO14_MG_LT_LVL_1 0x20
134 #define MAGNETO14_MG_LT_LVL_2 0x40
135 #define MAGNETO14_MG_LT_LVL_3 0x60
136 #define MAGNETO14_MG_LT_LVL_4 0x80
137 #define MAGNETO14_MG_LT_LVL_5 0xA0
138 #define MAGNETO14_MG_LT_LVL_6 0xC0
139 #define MAGNETO14_MG_LT_LVL_7 0xE0
140 #define MAGNETO14_MG_HT_LVL_0 0x00
141 #define MAGNETO14_MG_HT_LVL_1 0x04
142 #define MAGNETO14_MG_HT_LVL_2 0x08
143 #define MAGNETO14_MG_HT_LVL_3 0x0C
144 #define MAGNETO14_MG_HT_LVL_4 0x10
145 #define MAGNETO14_MG_HT_LVL_5 0x14
146 #define MAGNETO14_MG_HT_LVL_6 0x18
147 #define MAGNETO14_MG_HT_LVL_7 0x1C
148 #define MAGNETO14_MG_BIT_MASK 0xFC
149 
154 #define MAGNETO14_MGH_ST_OK 0x00
155 #define MAGNETO14_FLD_ST_OK 0x00
156 #define MAGNETO14_FLD_ST_MGL 0x01
157 #define MAGNETO14_FLD_ST_MGH 0x02
158 #define MAGNETO14_FLD_ST_BIT_MASK 0x03
159 
168 #define MAGNETO14_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
169 #define MAGNETO14_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
170  // magneto14_set
172 
187 #define MAGNETO14_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.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
193  cfg.mgh = MIKROBUS( mikrobus, MIKROBUS_INT )
194  // magneto14_map // magneto14
197 
202 typedef struct
203 {
204  // Output pins
205  digital_out_t pwm;
207  // Input pins
208  digital_in_t mgh;
210  // Modules
211  spi_master_t spi;
213  pin_name_t chip_select;
215 } magneto14_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 pwm;
231  pin_name_t mgh;
233  // static variable
234  uint32_t spi_speed;
235  spi_master_mode_t spi_mode;
236  spi_master_chip_select_polarity_t cs_polarity;
239 
244 typedef enum
245 {
247  MAGNETO14_ERROR = -1
248 
250 
267 
282 
296 
311 err_t magneto14_write_reg ( magneto14_t *ctx, uint8_t cmd, uint8_t reg, uint8_t data_in );
312 
327 err_t magneto14_read_reg ( magneto14_t *ctx, uint8_t cmd, uint8_t reg, uint8_t *data_out );
328 
342 err_t magneto14_get_angle_raw_data ( magneto14_t *ctx, uint16_t *raw_data );
343 
357 err_t magneto14_get_angle ( magneto14_t *ctx, float *angle );
358 
373 err_t magneto14_set_mag_field_thd ( magneto14_t *ctx, uint8_t mglt, uint8_t mght );
374 
391 err_t magneto14_get_field_strength ( magneto14_t *ctx, uint8_t *field_strength );
392 
404 void magneto14_set_pwm_pin ( magneto14_t *ctx, uint8_t pwm_state );
405 
419 
420 #ifdef __cplusplus
421 }
422 #endif
423 #endif // MAGNETO14_H
424  // magneto14
426 
427 // ------------------------------------------------------------------------ END
magneto14_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: magneto14.h:235
magneto14_return_value_t
magneto14_return_value_t
Magneto 14 Click return value data.
Definition: magneto14.h:245
MAGNETO14_ERROR
@ MAGNETO14_ERROR
Definition: magneto14.h:247
magneto14_t::chip_select
pin_name_t chip_select
Definition: magneto14.h:213
magneto14_cfg_t::cs
pin_name_t cs
Definition: magneto14.h:227
magneto14_write_reg
err_t magneto14_write_reg(magneto14_t *ctx, uint8_t cmd, uint8_t reg, uint8_t data_in)
Magneto 14 data writing function.
spi_specifics.h
This file contains SPI specific macros, functions, etc.
magneto14_t
Magneto 14 Click context object.
Definition: magneto14.h:203
magneto14_cfg_setup
void magneto14_cfg_setup(magneto14_cfg_t *cfg)
Magneto 14 configuration object setup function.
magneto14_read_reg
err_t magneto14_read_reg(magneto14_t *ctx, uint8_t cmd, uint8_t reg, uint8_t *data_out)
Magneto 14 data reading function.
magneto14_cfg_t::mgh
pin_name_t mgh
Definition: magneto14.h:231
magneto14_cfg_t::sck
pin_name_t sck
Definition: magneto14.h:226
MAGNETO14_OK
@ MAGNETO14_OK
Definition: magneto14.h:246
magneto14_get_angle
err_t magneto14_get_angle(magneto14_t *ctx, float *angle)
Magneto 14 gets the angular position function.
magneto14_init
err_t magneto14_init(magneto14_t *ctx, magneto14_cfg_t *cfg)
Magneto 14 initialization function.
magneto14_get_angle_raw_data
err_t magneto14_get_angle_raw_data(magneto14_t *ctx, uint16_t *raw_data)
Magneto 14 gets the angle raw data function.
magneto14_cfg_t::mosi
pin_name_t mosi
Definition: magneto14.h:225
magneto14_set_pwm_pin
void magneto14_set_pwm_pin(magneto14_t *ctx, uint8_t pwm_state)
Magneto 14 set PWM pin function.
magneto14_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: magneto14.h:236
magneto14_default_cfg
err_t magneto14_default_cfg(magneto14_t *ctx)
Magneto 14 default configuration function.
magneto14_get_mgh
uint8_t magneto14_get_mgh(magneto14_t *ctx)
Magneto 14 gets the MGH function.
magneto14_cfg_t
Magneto 14 Click configuration object.
Definition: magneto14.h:222
magneto14_t::mgh
digital_in_t mgh
Definition: magneto14.h:208
magneto14_cfg_t::miso
pin_name_t miso
Definition: magneto14.h:224
magneto14_set_mag_field_thd
err_t magneto14_set_mag_field_thd(magneto14_t *ctx, uint8_t mglt, uint8_t mght)
Magneto 14 sets the magnetic field threshold function.
magneto14_cfg_t::spi_speed
uint32_t spi_speed
Definition: magneto14.h:234
magneto14_t::pwm
digital_out_t pwm
Definition: magneto14.h:205
magneto14_cfg_t::pwm
pin_name_t pwm
Definition: magneto14.h:230
magneto14_t::spi
spi_master_t spi
Definition: magneto14.h:211
magneto14_get_field_strength
err_t magneto14_get_field_strength(magneto14_t *ctx, uint8_t *field_strength)
Magneto 14 gets the magnetic field strength function.