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 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_spi_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
53 #define MAGNETO_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
55  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
56  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
57  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
58  cfg.out = MIKROBUS( mikrobus, MIKROBUS_INT )
59 
65 #define MAGNETO_RETVAL uint8_t
66 
67 #define MAGNETO_OK 0x00
68 #define MAGNETO_INIT_ERROR 0xFF
69 
75 #define MAGNETO_REG_CLEAR_ERROR_FLAG 0x0001
76 #define MAGNETO_REG_PROGRAMMING_CONTROL 0x0003
77 #define MAGNETO_REG_OTP_ZERO_POS_HIGH 0x0016
78 #define MAGNETO_REG_OTP_ZERO_POS_LOW 0x0017
79 #define MAGNETO_REG_DIAG_AGC 0x3FFD
80 #define MAGNETO_REG_MAGNITUDE 0x3FFF
81 #define MAGNETO_REG_ANGLE 0x3FFF
82 
88 #define MAGNETO_CMD_RESET_VALUE 0x0000
89 #define MAGNETO_CMD_SET_VALUE 0xFFFE
90 #define MAGNETO_CMD_MAGNITUDE_VALUE 0x1FFE
91 #define MAGNETO_CMD_PARITY_VALUE 0x000F
92 #define MAGNETO_CMD_READ_COMMAND_BIT 0x4000
93 #define MAGNETO_CMD_HIGH_BYTE 0xFF00
94 #define MAGNETO_CMD_LOW_BYTE 0x00FF
95 
101 #define MAGNETO_FLAG_OK 0
102 #define MAGNETO_FLAG_ERROR 1
103  // End group macro
106 
107 // --------------------------------------------------------------- PUBLIC TYPES
116 typedef struct
117 {
118  // Output pins
119 
120  digital_out_t out;
121  digital_out_t cs;
122 
123  // Modules
124 
125  spi_master_t spi;
126  pin_name_t chip_select;
127 
128 } magneto_t;
129 
133 typedef struct
134 {
135  // Communication gpio pins
136 
137  pin_name_t miso;
138  pin_name_t mosi;
139  pin_name_t sck;
140  pin_name_t cs;
141 
142  // Additional gpio pins
143 
144  pin_name_t out;
145 
146  // static variable
147 
148  uint32_t spi_speed;
149  uint8_t spi_mode;
150  spi_master_chip_select_polarity_t cs_polarity;
151 
152 } magneto_cfg_t;
153  // End types group
155 
156 // ------------------------------------------------------------------ CONSTANTS // End constants group
164 
165 // ------------------------------------------------------------------ VARIABLES // End variable group
173 
174 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
175 
180 #ifdef __cplusplus
181 extern "C"{
182 #endif
183 
192 void magneto_cfg_setup ( magneto_cfg_t *cfg );
193 
203 
216 (
217  magneto_t *ctx,
218  uint8_t *wr_buf,
219  uint16_t wr_len,
220  uint8_t *rd_buf,
221  uint16_t rd_len
222 );
223 
234 uint16_t magneto_read_data ( magneto_t *ctx, uint16_t address_command );
235 
246 void magneto_write_data ( magneto_t *ctx, uint16_t address_command, uint16_t write_data );
247 
257 uint16_t magneto_get_state ( magneto_t *ctx );
258 
275 uint8_t magneto_check_state ( magneto_t *ctx );
276 
287 float magneto_get_angle ( magneto_t *ctx );
288 
289 #ifdef __cplusplus
290 }
291 #endif
292 #endif // _MAGNETO_H_
293  // End public_function group
296 
297 // ------------------------------------------------------------------------- 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:144
magneto_t
Click ctx object definition.
Definition: magneto.h:116
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:149
magneto_cfg_t
Click configuration structure definition.
Definition: magneto.h:133
magneto_t::out
digital_out_t out
Definition: magneto.h:120
MAGNETO_RETVAL
#define MAGNETO_RETVAL
Definition: magneto.h:65
magneto_t::spi
spi_master_t spi
Definition: magneto.h:125
magneto_t::cs
digital_out_t cs
Definition: magneto.h:121
magneto_cfg_t::miso
pin_name_t miso
Definition: magneto.h:137
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:150
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:126
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:138
magneto_cfg_t::cs
pin_name_t cs
Definition: magneto.h:140
magneto_cfg_t::spi_speed
uint32_t spi_speed
Definition: magneto.h:148
magneto_cfg_t::sck
pin_name_t sck
Definition: magneto.h:139
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.