magneto4  2.0.0.0
magneto4.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 MAGNETO4_H
36 #define MAGNETO4_H
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_spi_master.h"
55 
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
68 #define MAGNETO4_MAP_MIKROBUS( cfg, mikrobus ) \
69  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
70  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
71  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
72  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
73  cfg.a = MIKROBUS( mikrobus, MIKROBUS_AN ); \
74  cfg.b = MIKROBUS( mikrobus, MIKROBUS_RST ); \
75  cfg.ind = MIKROBUS( mikrobus, MIKROBUS_INT )
76 
82 #define MAGNETO4_RETVAL uint8_t
83 
84 #define MAGNETO4_OK 0x00
85 #define MAGNETO4_INIT_ERROR 0xFF
86 
92 #define MAGNETO4_ENCODER_TO_ZERO_POSITION 0
93 
99 #define MAGNETO4_MAX_MAGNETIC_FIELD_VALUE 3000
100  // End group macro
103 // --------------------------------------------------------------- PUBLIC TYPES
112 typedef struct
113 {
114  digital_out_t cs;
115 
116  // Input pins
117 
118  digital_in_t a;
119  digital_in_t b;
120  digital_in_t pwm;
121  digital_in_t ind;
122 
123  // Modules
124 
125  spi_master_t spi;
126  pin_name_t chip_select;
127 
128  // ctx variable
129 
131  uint8_t a_edge;
132  uint8_t b_edge;
133  uint8_t state_a;
134  uint8_t state_b;
135 
136 } magneto4_t;
137 
141 typedef struct
142 {
143  // Communication gpio pins
144 
145  pin_name_t miso;
146  pin_name_t mosi;
147  pin_name_t sck;
148  pin_name_t cs;
149 
150  // Additional gpio pins
151 
152  pin_name_t a;
153  pin_name_t b;
154  pin_name_t pwm;
155  pin_name_t ind;
156 
157  // static variable
158 
159  uint32_t spi_speed;
160  uint8_t spi_mode;
161  spi_master_chip_select_polarity_t cs_polarity;
162 
164 
168 typedef struct
169 {
170  uint8_t dec_val;
171  uint8_t inc_val;
172  uint8_t lin_val;
173  uint8_t cof_val;
174  uint8_t ocf_val;
175 
176 } mag_status_t;
177  // End types group
179 
180 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
181 
186 #ifdef __cplusplus
187 extern "C"{
188 #endif
189 
199 
208 
219 
232 (
233  magneto4_t *ctx,
234  uint8_t *rd_buf,
235  uint16_t rd_len
236 );
237 
252 
267 
282 
293 
305 
317 
333 
344 
354 void magneto4_set_start_position ( magneto4_t *ctx, int32_t encoder_pos );
355 
356 #ifdef __cplusplus
357 }
358 #endif
359 #endif // _MAGNETO4_H_
360  // End public_function group
363 
364 // ------------------------------------------------------------------------- END
magneto4_start_measurement
uint32_t magneto4_start_measurement(magneto4_t *ctx)
Measurement function.
magneto4_t::b
digital_in_t b
Definition: magneto4.h:119
mag_status_t::dec_val
uint8_t dec_val
Definition: magneto4.h:170
magneto4_cfg_t::sck
pin_name_t sck
Definition: magneto4.h:147
magneto4_cfg_setup
void magneto4_cfg_setup(magneto4_cfg_t *cfg)
Config Object Initialization function.
magneto4_cfg_t::pwm
pin_name_t pwm
Definition: magneto4.h:154
magneto4_get_out_a
uint8_t magneto4_get_out_a(magneto4_t *ctx)
Get A ( AN ) pin status.
magneto4_t::b_edge
uint8_t b_edge
Definition: magneto4.h:132
mag_status_t
Click magnetic status structure definition.
Definition: magneto4.h:169
magneto4_cfg_t::cs
pin_name_t cs
Definition: magneto4.h:148
magneto4_t::pwm
digital_in_t pwm
Definition: magneto4.h:120
magneto4_get_magnetic_status
uint8_t magneto4_get_magnetic_status(magneto4_t *ctx, mag_status_t *mag_status)
Get Magnetic measurement status.
magneto4_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: magneto4.h:161
magneto4_t::encoder_position
int32_t encoder_position
Definition: magneto4.h:130
magneto4_t::cs
digital_out_t cs
Definition: magneto4.h:114
magneto4_t::spi
spi_master_t spi
Definition: magneto4.h:125
magneto4_get_encoder_position
void magneto4_get_encoder_position(magneto4_t *ctx)
Encoder position.
magneto4_t::a
digital_in_t a
Definition: magneto4.h:118
magneto4_t::a_edge
uint8_t a_edge
Definition: magneto4.h:131
magneto4_generic_transfer
void magneto4_generic_transfer(magneto4_t *ctx, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
magneto4_cfg_t::mosi
pin_name_t mosi
Definition: magneto4.h:146
mag_status_t::ocf_val
uint8_t ocf_val
Definition: magneto4.h:174
magneto4_get_encoder_direction
uint8_t magneto4_get_encoder_direction(magneto4_t *ctx)
Encoder direction.
magneto4_cfg_t
Click configuration structure definition.
Definition: magneto4.h:142
MAGNETO4_RETVAL
#define MAGNETO4_RETVAL
Definition: magneto4.h:82
magneto4_t::state_b
uint8_t state_b
Definition: magneto4.h:134
magneto4_cfg_t::ind
pin_name_t ind
Definition: magneto4.h:155
mag_status_t::inc_val
uint8_t inc_val
Definition: magneto4.h:171
magneto4_cfg_t::spi_speed
uint32_t spi_speed
Definition: magneto4.h:159
magneto4_t::ind
digital_in_t ind
Definition: magneto4.h:121
magneto4_cfg_t::spi_mode
uint8_t spi_mode
Definition: magneto4.h:160
magneto4_cfg_t::b
pin_name_t b
Definition: magneto4.h:153
magneto4_default_cfg
void magneto4_default_cfg(magneto4_t *ctx)
Click Default Configuration function.
mag_status_t::lin_val
uint8_t lin_val
Definition: magneto4.h:172
magneto4_set_start_position
void magneto4_set_start_position(magneto4_t *ctx, int32_t encoder_pos)
Set start encoder position.
magneto4_get_magnetic_field
uint16_t magneto4_get_magnetic_field(magneto4_t *ctx)
Measurement Field.
magneto4_get_out_b
uint8_t magneto4_get_out_b(magneto4_t *ctx)
Get B ( RST ) pin status.
magneto4_cfg_t::a
pin_name_t a
Definition: magneto4.h:152
mag_status_t::cof_val
uint8_t cof_val
Definition: magneto4.h:173
magneto4_t::chip_select
pin_name_t chip_select
Definition: magneto4.h:126
magneto4_get_index
uint8_t magneto4_get_index(magneto4_t *ctx)
Get IND ( interrupt ) pin status.
magneto4_t
Click ctx object definition.
Definition: magneto4.h:113
magneto4_init
MAGNETO4_RETVAL magneto4_init(magneto4_t *ctx, magneto4_cfg_t *cfg)
Initialization function.
magneto4_cfg_t::miso
pin_name_t miso
Definition: magneto4.h:145
magneto4_t::state_a
uint8_t state_a
Definition: magneto4.h:133