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 "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_spi_master.h"
51 
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define MAGNETO4_MAP_MIKROBUS( cfg, mikrobus ) \
65  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
66  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
67  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
68  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
69  cfg.a = MIKROBUS( mikrobus, MIKROBUS_AN ); \
70  cfg.b = MIKROBUS( mikrobus, MIKROBUS_RST ); \
71  cfg.ind = MIKROBUS( mikrobus, MIKROBUS_INT )
72 
78 #define MAGNETO4_RETVAL uint8_t
79 
80 #define MAGNETO4_OK 0x00
81 #define MAGNETO4_INIT_ERROR 0xFF
82 
88 #define MAGNETO4_ENCODER_TO_ZERO_POSITION 0
89 
95 #define MAGNETO4_MAX_MAGNETIC_FIELD_VALUE 3000
96  // End group macro
99 // --------------------------------------------------------------- PUBLIC TYPES
108 typedef struct
109 {
110  digital_out_t cs;
111 
112  // Input pins
113 
114  digital_in_t a;
115  digital_in_t b;
116  digital_in_t pwm;
117  digital_in_t ind;
118 
119  // Modules
120 
121  spi_master_t spi;
122  pin_name_t chip_select;
123 
124  // ctx variable
125 
127  uint8_t a_edge;
128  uint8_t b_edge;
129  uint8_t state_a;
130  uint8_t state_b;
131 
132 } magneto4_t;
133 
137 typedef struct
138 {
139  // Communication gpio pins
140 
141  pin_name_t miso;
142  pin_name_t mosi;
143  pin_name_t sck;
144  pin_name_t cs;
145 
146  // Additional gpio pins
147 
148  pin_name_t a;
149  pin_name_t b;
150  pin_name_t pwm;
151  pin_name_t ind;
152 
153  // static variable
154 
155  uint32_t spi_speed;
156  uint8_t spi_mode;
157  spi_master_chip_select_polarity_t cs_polarity;
158 
160 
164 typedef struct
165 {
166  uint8_t dec_val;
167  uint8_t inc_val;
168  uint8_t lin_val;
169  uint8_t cof_val;
170  uint8_t ocf_val;
171 
172 } mag_status_t;
173  // End types group
175 
176 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
177 
182 #ifdef __cplusplus
183 extern "C"{
184 #endif
185 
195 
204 
215 
228 (
229  magneto4_t *ctx,
230  uint8_t *rd_buf,
231  uint16_t rd_len
232 );
233 
248 
263 
278 
289 
301 
313 
329 
340 
350 void magneto4_set_start_position ( magneto4_t *ctx, int32_t encoder_pos );
351 
352 #ifdef __cplusplus
353 }
354 #endif
355 #endif // _MAGNETO4_H_
356  // End public_function group
359 
360 // ------------------------------------------------------------------------- END
magneto4_start_measurement
uint32_t magneto4_start_measurement(magneto4_t *ctx)
Measurement function.
magneto4_t::b
digital_in_t b
Definition: magneto4.h:115
mag_status_t::dec_val
uint8_t dec_val
Definition: magneto4.h:166
magneto4_cfg_t::sck
pin_name_t sck
Definition: magneto4.h:143
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:150
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:128
mag_status_t
Click magnetic status structure definition.
Definition: magneto4.h:165
magneto4_cfg_t::cs
pin_name_t cs
Definition: magneto4.h:144
magneto4_t::pwm
digital_in_t pwm
Definition: magneto4.h:116
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:157
magneto4_t::encoder_position
int32_t encoder_position
Definition: magneto4.h:126
magneto4_t::cs
digital_out_t cs
Definition: magneto4.h:110
magneto4_t::spi
spi_master_t spi
Definition: magneto4.h:121
magneto4_get_encoder_position
void magneto4_get_encoder_position(magneto4_t *ctx)
Encoder position.
magneto4_t::a
digital_in_t a
Definition: magneto4.h:114
magneto4_t::a_edge
uint8_t a_edge
Definition: magneto4.h:127
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:142
mag_status_t::ocf_val
uint8_t ocf_val
Definition: magneto4.h:170
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:138
MAGNETO4_RETVAL
#define MAGNETO4_RETVAL
Definition: magneto4.h:78
magneto4_t::state_b
uint8_t state_b
Definition: magneto4.h:130
magneto4_cfg_t::ind
pin_name_t ind
Definition: magneto4.h:151
mag_status_t::inc_val
uint8_t inc_val
Definition: magneto4.h:167
magneto4_cfg_t::spi_speed
uint32_t spi_speed
Definition: magneto4.h:155
magneto4_t::ind
digital_in_t ind
Definition: magneto4.h:117
magneto4_cfg_t::spi_mode
uint8_t spi_mode
Definition: magneto4.h:156
magneto4_cfg_t::b
pin_name_t b
Definition: magneto4.h:149
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:168
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:148
mag_status_t::cof_val
uint8_t cof_val
Definition: magneto4.h:169
magneto4_t::chip_select
pin_name_t chip_select
Definition: magneto4.h:122
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:109
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:141
magneto4_t::state_a
uint8_t state_a
Definition: magneto4.h:129