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 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_spi_master.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
54 #define MAGNETO4_MAP_MIKROBUS( cfg, mikrobus ) \
55  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
56  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
57  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
58  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
59  cfg.a = MIKROBUS( mikrobus, MIKROBUS_AN ); \
60  cfg.b = MIKROBUS( mikrobus, MIKROBUS_RST ); \
61  cfg.ind = MIKROBUS( mikrobus, MIKROBUS_INT )
62 
68 #define MAGNETO4_RETVAL uint8_t
69 
70 #define MAGNETO4_OK 0x00
71 #define MAGNETO4_INIT_ERROR 0xFF
72 
78 #define MAGNETO4_ENCODER_TO_ZERO_POSITION 0
79 
85 #define MAGNETO4_MAX_MAGNETIC_FIELD_VALUE 3000
86  // End group macro
89 // --------------------------------------------------------------- PUBLIC TYPES
98 typedef struct
99 {
100  digital_out_t cs;
101 
102  // Input pins
103 
104  digital_in_t a;
105  digital_in_t b;
106  digital_in_t pwm;
107  digital_in_t ind;
108 
109  // Modules
110 
111  spi_master_t spi;
112  pin_name_t chip_select;
113 
114  // ctx variable
115 
117  uint8_t a_edge;
118  uint8_t b_edge;
119  uint8_t state_a;
120  uint8_t state_b;
121 
122 } magneto4_t;
123 
127 typedef struct
128 {
129  // Communication gpio pins
130 
131  pin_name_t miso;
132  pin_name_t mosi;
133  pin_name_t sck;
134  pin_name_t cs;
135 
136  // Additional gpio pins
137 
138  pin_name_t a;
139  pin_name_t b;
140  pin_name_t pwm;
141  pin_name_t ind;
142 
143  // static variable
144 
145  uint32_t spi_speed;
146  uint8_t spi_mode;
147  spi_master_chip_select_polarity_t cs_polarity;
148 
150 
154 typedef struct
155 {
156  uint8_t dec_val;
157  uint8_t inc_val;
158  uint8_t lin_val;
159  uint8_t cof_val;
160  uint8_t ocf_val;
161 
162 } mag_status_t;
163  // End types group
165 
166 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
167 
172 #ifdef __cplusplus
173 extern "C"{
174 #endif
175 
184 void magneto4_cfg_setup ( magneto4_cfg_t *cfg );
185 
194 
204 void magneto4_default_cfg ( magneto4_t *ctx );
205 
218 (
219  magneto4_t *ctx,
220  uint8_t *rd_buf,
221  uint16_t rd_len
222 );
223 
237 uint8_t magneto4_get_out_a ( magneto4_t *ctx );
238 
252 uint8_t magneto4_get_out_b ( magneto4_t *ctx );
253 
267 uint8_t magneto4_get_index ( magneto4_t *ctx );
268 
278 uint32_t magneto4_start_measurement ( magneto4_t *ctx );
279 
290 uint16_t magneto4_get_magnetic_field ( magneto4_t *ctx );
291 
302 uint8_t magneto4_get_magnetic_status ( magneto4_t *ctx, mag_status_t *mag_status );
303 
318 uint8_t magneto4_get_encoder_direction ( magneto4_t *ctx, mag_status_t *mag_status );
319 
330 
340 void magneto4_set_start_position ( magneto4_t *ctx, int32_t encoder_pos );
341 
342 #ifdef __cplusplus
343 }
344 #endif
345 #endif // _MAGNETO4_H_
346  // End public_function group
349 
350 // ------------------------------------------------------------------------- END
pin_name_t miso
Definition: magneto4.h:131
pin_name_t a
Definition: magneto4.h:138
void magneto4_cfg_setup(magneto4_cfg_t *cfg)
Config Object Initialization function.
spi_master_chip_select_polarity_t cs_polarity
Definition: magneto4.h:147
uint32_t magneto4_start_measurement(magneto4_t *ctx)
Measurement function.
pin_name_t cs
Definition: magneto4.h:134
uint8_t dec_val
Definition: magneto4.h:156
int32_t encoder_position
Definition: magneto4.h:116
void magneto4_generic_transfer(magneto4_t *ctx, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
pin_name_t pwm
Definition: magneto4.h:140
pin_name_t sck
Definition: magneto4.h:133
digital_in_t pwm
Definition: magneto4.h:106
digital_in_t ind
Definition: magneto4.h:107
uint8_t inc_val
Definition: magneto4.h:157
Click magnetic status structure definition.
Definition: magneto4.h:154
Click configuration structure definition.
Definition: magneto4.h:127
uint8_t state_b
Definition: magneto4.h:120
uint8_t ocf_val
Definition: magneto4.h:160
void magneto4_set_start_position(magneto4_t *ctx, int32_t encoder_pos)
Set start encoder position.
digital_out_t cs
Definition: magneto4.h:100
uint8_t state_a
Definition: magneto4.h:119
uint8_t magneto4_get_index(magneto4_t *ctx)
Get IND ( interrupt ) pin status.
uint8_t lin_val
Definition: magneto4.h:158
uint8_t magneto4_get_out_b(magneto4_t *ctx)
Get B ( RST ) pin status.
Click ctx object definition.
Definition: magneto4.h:98
#define MAGNETO4_RETVAL
Definition: magneto4.h:68
uint8_t magneto4_get_out_a(magneto4_t *ctx)
Get A ( AN ) pin status.
pin_name_t mosi
Definition: magneto4.h:132
uint8_t magneto4_get_magnetic_status(magneto4_t *ctx, mag_status_t *mag_status)
Get Magnetic measurement status.
uint8_t b_edge
Definition: magneto4.h:118
pin_name_t chip_select
Definition: magneto4.h:112
uint32_t spi_speed
Definition: magneto4.h:145
MAGNETO4_RETVAL magneto4_init(magneto4_t *ctx, magneto4_cfg_t *cfg)
Initialization function.
uint8_t magneto4_get_encoder_direction(magneto4_t *ctx, mag_status_t *mag_status)
Encoder direction.
uint8_t spi_mode
Definition: magneto4.h:146
void magneto4_default_cfg(magneto4_t *ctx)
Click Default Configuration function.
uint8_t cof_val
Definition: magneto4.h:159
spi_master_t spi
Definition: magneto4.h:111
digital_in_t a
Definition: magneto4.h:104
uint16_t magneto4_get_magnetic_field(magneto4_t *ctx)
Measurement Field.
uint8_t a_edge
Definition: magneto4.h:117
digital_in_t b
Definition: magneto4.h:105
pin_name_t b
Definition: magneto4.h:139
pin_name_t ind
Definition: magneto4.h:141
void magneto4_get_encoder_position(magneto4_t *ctx)
Encoder position.