magneto4  2.0.0.0
magneto4.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright (c) 2019, MikroElektronika - www.mikroe.com
4  * All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * 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  // Input pins
101 
102  digital_in_t a;
103  digital_in_t b;
104  digital_in_t pwm;
105  digital_in_t ind;
106 
107  // Modules
108 
109  spi_master_t spi;
110 
111  // ctx variable
112 
114  uint8_t a_edge;
115  uint8_t b_edge;
116  uint8_t state_a;
117  uint8_t state_b;
118 
119 } magneto4_t;
120 
124 typedef struct
125 {
126  // Communication gpio pins
127 
128  pin_name_t miso;
129  pin_name_t mosi;
130  pin_name_t sck;
131  pin_name_t cs;
132 
133  // Additional gpio pins
134 
135  pin_name_t a;
136  pin_name_t b;
137  pin_name_t pwm;
138  pin_name_t ind;
139 
140  // static variable
141 
142  hal_spi_speed_t spi_speed;
143  hal_spi_mode_t spi_mode;
144 
146 
150 typedef struct
151 {
152  uint8_t dec_val;
153  uint8_t inc_val;
154  uint8_t lin_val;
155  uint8_t cof_val;
156  uint8_t ocf_val;
157 
158 } mag_status_t;
159  // End types group
161 
162 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
163 
168 #ifdef __cplusplus
169 extern "C"{
170 #endif
171 
180 void magneto4_cfg_setup ( magneto4_cfg_t *cfg );
181 
190 
200 void magneto4_default_cfg ( magneto4_t *ctx );
201 
210 void magneto4_generic_transfer ( magneto4_t *ctx, spi_master_transfer_data_t *block );
211 
225 uint8_t magneto4_get_out_a ( magneto4_t *ctx );
226 
240 uint8_t magneto4_get_out_b ( magneto4_t *ctx );
241 
255 uint8_t magneto4_get_index ( magneto4_t *ctx );
256 
266 uint32_t magneto4_start_measurement ( magneto4_t *ctx );
267 
278 uint16_t magneto4_get_magnetic_field ( magneto4_t *ctx );
279 
290 uint8_t magneto4_get_magnetic_status ( magneto4_t *ctx, mag_status_t *mag_status );
291 
306 uint8_t magneto4_get_encoder_direction ( magneto4_t *ctx, mag_status_t *mag_status );
307 
318 
328 void magneto4_set_start_position ( magneto4_t *ctx, int32_t encoder_pos );
329 
330 #ifdef __cplusplus
331 }
332 #endif
333 #endif // _MAGNETO4_H_
334  // End public_function group
337 
338 // ------------------------------------------------------------------------- END
magneto4_generic_transfer
void magneto4_generic_transfer(magneto4_t *ctx, spi_master_transfer_data_t *block)
Generic transfer function.
magneto4_start_measurement
uint32_t magneto4_start_measurement(magneto4_t *ctx)
Measurement function.
magneto4_t::b
digital_in_t b
Definition: magneto4.h:103
mag_status_t::dec_val
uint8_t dec_val
Definition: magneto4.h:152
magneto4_cfg_t::sck
pin_name_t sck
Definition: magneto4.h:130
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:137
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:115
mag_status_t
Click magnetic status structure definition.
Definition: magneto4.h:150
magneto4_cfg_t::cs
pin_name_t cs
Definition: magneto4.h:131
magneto4_t::pwm
digital_in_t pwm
Definition: magneto4.h:104
magneto4_get_magnetic_status
uint8_t magneto4_get_magnetic_status(magneto4_t *ctx, mag_status_t *mag_status)
Get Magnetic measurement status.
magneto4_t::encoder_position
int32_t encoder_position
Definition: magneto4.h:113
magneto4_t::spi
spi_master_t spi
Definition: magneto4.h:109
magneto4_get_encoder_position
void magneto4_get_encoder_position(magneto4_t *ctx)
Encoder position.
magneto4_t::a
digital_in_t a
Definition: magneto4.h:102
magneto4_t::a_edge
uint8_t a_edge
Definition: magneto4.h:114
magneto4_cfg_t::spi_speed
hal_spi_speed_t spi_speed
Definition: magneto4.h:142
magneto4_cfg_t::mosi
pin_name_t mosi
Definition: magneto4.h:129
mag_status_t::ocf_val
uint8_t ocf_val
Definition: magneto4.h:156
magneto4_cfg_t::spi_mode
hal_spi_mode_t spi_mode
Definition: magneto4.h:143
magneto4_get_encoder_direction
uint8_t magneto4_get_encoder_direction(magneto4_t *ctx, mag_status_t *mag_status)
Encoder direction.
magneto4_cfg_t
Click configuration structure definition.
Definition: magneto4.h:124
MAGNETO4_RETVAL
#define MAGNETO4_RETVAL
Definition: magneto4.h:68
magneto4_t::state_b
uint8_t state_b
Definition: magneto4.h:117
magneto4_cfg_t::ind
pin_name_t ind
Definition: magneto4.h:138
mag_status_t::inc_val
uint8_t inc_val
Definition: magneto4.h:153
magneto4_t::ind
digital_in_t ind
Definition: magneto4.h:105
magneto4_cfg_t::b
pin_name_t b
Definition: magneto4.h:136
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:154
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:135
mag_status_t::cof_val
uint8_t cof_val
Definition: magneto4.h:155
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:98
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:128
magneto4_t::state_a
uint8_t state_a
Definition: magneto4.h:116