magneto  2.0.0.0
magneto.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 MAGNETO_H
36 #define MAGNETO_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 // -------------------------------------------------------------- PUBLIC MACROS
67 #define MAGNETO_MAP_MIKROBUS( cfg, mikrobus ) \
68  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
69  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
70  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
71  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
72  cfg.out = MIKROBUS( mikrobus, MIKROBUS_INT )
73 
79 #define MAGNETO_RETVAL uint8_t
80 
81 #define MAGNETO_OK 0x00
82 #define MAGNETO_INIT_ERROR 0xFF
83 
89 #define MAGNETO_REG_CLEAR_ERROR_FLAG 0x0001
90 #define MAGNETO_REG_PROGRAMMING_CONTROL 0x0003
91 #define MAGNETO_REG_OTP_ZERO_POS_HIGH 0x0016
92 #define MAGNETO_REG_OTP_ZERO_POS_LOW 0x0017
93 #define MAGNETO_REG_DIAG_AGC 0x3FFD
94 #define MAGNETO_REG_MAGNITUDE 0x3FFF
95 #define MAGNETO_REG_ANGLE 0x3FFF
96 
102 #define MAGNETO_CMD_RESET_VALUE 0x0000
103 #define MAGNETO_CMD_SET_VALUE 0xFFFE
104 #define MAGNETO_CMD_MAGNITUDE_VALUE 0x1FFE
105 #define MAGNETO_CMD_PARITY_VALUE 0x000F
106 #define MAGNETO_CMD_READ_COMMAND_BIT 0x4000
107 #define MAGNETO_CMD_HIGH_BYTE 0xFF00
108 #define MAGNETO_CMD_LOW_BYTE 0x00FF
109 
115 #define MAGNETO_FLAG_OK 0
116 #define MAGNETO_FLAG_ERROR 1
117  // End group macro
120 
121 // --------------------------------------------------------------- PUBLIC TYPES
130 typedef struct
131 {
132  // Output pins
133 
134  digital_out_t out;
135  digital_out_t cs;
136 
137  // Modules
138 
139  spi_master_t spi;
140  pin_name_t chip_select;
141 
142 } magneto_t;
143 
147 typedef struct
148 {
149  // Communication gpio pins
150 
151  pin_name_t miso;
152  pin_name_t mosi;
153  pin_name_t sck;
154  pin_name_t cs;
155 
156  // Additional gpio pins
157 
158  pin_name_t out;
159 
160  // static variable
161 
162  uint32_t spi_speed;
163  uint8_t spi_mode;
164  spi_master_chip_select_polarity_t cs_polarity;
165 
166 } magneto_cfg_t;
167  // End types group
169 
170 // ------------------------------------------------------------------ CONSTANTS // End constants group
178 
179 // ------------------------------------------------------------------ VARIABLES // End variable group
187 
188 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
189 
194 #ifdef __cplusplus
195 extern "C"{
196 #endif
197 
207 
217 
230 (
231  magneto_t *ctx,
232  uint8_t *wr_buf,
233  uint16_t wr_len,
234  uint8_t *rd_buf,
235  uint16_t rd_len
236 );
237 
248 uint16_t magneto_read_data ( magneto_t *ctx, uint16_t address_command );
249 
260 void magneto_write_data ( magneto_t *ctx, uint16_t address_command, uint16_t write_data );
261 
271 uint16_t magneto_get_state ( magneto_t *ctx );
272 
290 
302 
303 #ifdef __cplusplus
304 }
305 #endif
306 #endif // _MAGNETO_H_
307  // End public_function group
310 
311 // ------------------------------------------------------------------------- END
magneto_init
MAGNETO_RETVAL magneto_init(magneto_t *ctx, magneto_cfg_t *cfg)
Initialization function.
magneto_cfg_t::out
pin_name_t out
Definition: magneto.h:158
magneto_t
Click ctx object definition.
Definition: magneto.h:131
magneto_get_angle
float magneto_get_angle(magneto_t *ctx)
Calculate angle function.
magneto_cfg_t::spi_mode
uint8_t spi_mode
Definition: magneto.h:163
magneto_cfg_t
Click configuration structure definition.
Definition: magneto.h:148
magneto_t::out
digital_out_t out
Definition: magneto.h:134
MAGNETO_RETVAL
#define MAGNETO_RETVAL
Definition: magneto.h:79
magneto_t::spi
spi_master_t spi
Definition: magneto.h:139
magneto_t::cs
digital_out_t cs
Definition: magneto.h:135
magneto_cfg_t::miso
pin_name_t miso
Definition: magneto.h:151
magneto_check_state
uint8_t magneto_check_state(magneto_t *ctx)
Check state function.
magneto_read_data
uint16_t magneto_read_data(magneto_t *ctx, uint16_t address_command)
16-bit command generic read function.
magneto_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: magneto.h:164
magneto_cfg_setup
void magneto_cfg_setup(magneto_cfg_t *cfg)
Config Object Initialization function.
magneto_t::chip_select
pin_name_t chip_select
Definition: magneto.h:140
magneto_get_state
uint16_t magneto_get_state(magneto_t *ctx)
Get state function.
magneto_cfg_t::mosi
pin_name_t mosi
Definition: magneto.h:152
magneto_cfg_t::cs
pin_name_t cs
Definition: magneto.h:154
magneto_cfg_t::spi_speed
uint32_t spi_speed
Definition: magneto.h:162
magneto_cfg_t::sck
pin_name_t sck
Definition: magneto.h:153
magneto_generic_transfer
void magneto_generic_transfer(magneto_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
magneto_write_data
void magneto_write_data(magneto_t *ctx, uint16_t address_command, uint16_t write_data)
16-bit command generic write function.