angle4  2.0.0.0
angle4.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  */
32 // ----------------------------------------------------------------------------
33 
34 #ifndef ANGLE4_H
35 #define ANGLE4_H
36 
41 #ifdef PREINIT_SUPPORTED
42 #include "preinit.h"
43 #endif
44 
45 #ifdef MikroCCoreVersion
46  #if MikroCCoreVersion >= 1
47  #include "delays.h"
48  #endif
49 #endif
50 
51 #include "drv_digital_out.h"
52 #include "drv_digital_in.h"
53 #include "drv_spi_master.h"
54 
55 // -------------------------------------------------------------- PUBLIC MACROS
66 #define ANGLE4_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
68  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
69  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
70  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
71 
77 #define ANGLE4_RETVAL uint8_t
78 
79 #define ANGLE4_OK 0x00
80 #define ANGLE4_INIT_ERROR 0xFF
81 
87 #define ANGLE4_REG_CUSTOMER_CFG_0 0x04
88 #define ANGLE4_REG_CUSTOMER_CFG_1 0x05
89 #define ANGLE4_REG_CUSTOMER_CFG_2 0x06
90 #define ANGLE4_REG_OPERATING_VOLTAGE 0x0A
91 #define ANGLE4_REG_UNLOCK_REGISTER 0x10
92 #define ANGLE4_REG_CUSTOMER_RESERVED_OTP 0x11
93 #define ANGLE4_REG_ST_ZERO_RESET_OTP 0x12
94 #define ANGLE4_REG_CUSTOMER_CONFIG_OTP 0x13
95 #define ANGLE4_REG_PROGRAM_OPERATING_VOLTAGE 0x14
96 #define ANGLE4_REG_CONFIG_OF_FULL_CALIBRATION_OTP 0x1B
97 
103 #define ANGLE4_CCFG0_SELECT_UVW_MODE 0x80
104 #define ANGLE4_CCFG0_SELECT_PWM_MODE 0x00
105 #define ANGLE4_CCFG0_PWM_PERIOD_8193us 0x60
106 #define ANGLE4_CCFG0_PWM_PERIOD_4097us 0x40
107 #define ANGLE4_CCFG0_PWM_PERIOD_2049us 0x20
108 #define ANGLE4_CCFG0_PWM_PERIOD_1025us 0x00
109 #define ANGLE4_CCFG0_ABI_360edeg 0x18
110 #define ANGLE4_CCFG0_ABI_270edeg 0x10
111 #define ANGLE4_CCFG0_ABI_180edeg 0x08
112 #define ANGLE4_CCFG0_ABI_90edeg 0x00
113 #define ANGLE4_CCFG0_UVW_POLE_PAIRS_8 0x07
114 #define ANGLE4_CCFG0_UVW_POLE_PAIRS_7 0x06
115 #define ANGLE4_CCFG0_UVW_POLE_PAIRS_6 0x05
116 #define ANGLE4_CCFG0_UVW_POLE_PAIRS_5 0x04
117 #define ANGLE4_CCFG0_UVW_POLE_PAIRS_4 0x03
118 #define ANGLE4_CCFG0_UVW_POLE_PAIRS_3 0x02
119 #define ANGLE4_CCFG0_UVW_POLE_PAIRS_2 0x01
120 #define ANGLE4_CCFG0_UVW_POLE_PAIRS_1 0x00
121 
127 #define ANGLE4_CCFG1_ABI_CPR_4096 0x70
128 #define ANGLE4_CCFG1_ABI_CPR_2048 0x60
129 #define ANGLE4_CCFG1_ABI_CPR_1024 0x50
130 #define ANGLE4_CCFG1_ABI_CPR_512 0x40
131 #define ANGLE4_CCFG1_ABI_CPR_256 0x30
132 #define ANGLE4_CCFG1_ABI_CPR_128 0x20
133 #define ANGLE4_CCFG1_ABI_CPR_64 0x10
134 #define ANGLE4_CCFG1_ABI_CPR_32 0x00
135 #define ANGLE4_CCFG1_HYSTERESIS_1_4mdeg 0x09
136 #define ANGLE4_CCFG1_HYSTERESIS_0_7mdeg 0x08
137 #define ANGLE4_CCFG1_HYSTERESIS_0_35mdeg 0x07
138 #define ANGLE4_CCFG1_HYSTERESIS_0_17mdeg 0x06
139 #define ANGLE4_CCFG1_HYSTERESIS_0_08mdeg 0x05
140 #define ANGLE4_CCFG1_HYSTERESIS_0_04mdeg 0x04
141 #define ANGLE4_CCFG1_HYSTERESIS_0_02mdeg 0x03
142 #define ANGLE4_CCFG1_HYSTERESIS_0_01mdeg 0x02
143 #define ANGLE4_CCFG1_HYSTERESIS_0_005mdeg 0x01
144 #define ANGLE4_CCFG1_NO_HYSTERESIS 0x00
145 
151 #define ANGLE4_CCFG2_DIR_COUNTER_CLOCKWISE_ROTATION 0x80
152 #define ANGLE4_CCFG2_DIR_CLOCKWISE_ROTATION 0x00
153 #define ANGLE4_CCFG2_ZERO_LATENCY_MODE_ON 0x40
154 #define ANGLE4_CCFG2_ZERO_LATENCY_MODE_OFF 0x00
155 #define ANGLE4_CCFG2_ABS_RESOLUTION_10bit 0x30
156 #define ANGLE4_CCFG2_ABS_RESOLUTION_12bit 0x20
157 #define ANGLE4_CCFG2_ABS_RESOLUTION_14bit 0x10
158 #define ANGLE4_CCFG2_ABS_RESOLUTION_16bit 0x00
159 #define ANGLE4_CCFG2_SETTINGS_CPR_4096 0x06
160 #define ANGLE4_CCFG2_SETTINGS_CPR_2048 0x06
161 #define ANGLE4_CCFG2_SETTINGS_CPR_1024 0x04
162 #define ANGLE4_CCFG2_SETTINGS_CPR_512 0x04
163 #define ANGLE4_CCFG2_SETTINGS_CPR_256 0x03
164 #define ANGLE4_CCFG2_SETTINGS_CPR_128 0x02
165 #define ANGLE4_CCFG2_SETTINGS_CPR_64 0x02
166 #define ANGLE4_CCFG2_SETTINGS_CPR_32 0x02
167 #define ANGLE4_CCFG2_CPR_ABS_RESOLUTION 0x04
168 
174 #define ANGLE4_OPERATING_VOLTAGE_3300mV 0x00
175 #define ANGLE4_OPERATING_VOLTAGE_5000mV 0x02
176 
182 #define ANGLE4_DATA_RESOLUTION_14bit 0x10
183 #define ANGLE4_DATA_RESOLUTION_16bit 0x00
184 #define ANGLE4_DATA_RESOLUTION_12bit 0x30
185 #define ANGLE4_DATA_RESOLUTION_10bit 0x30
186  // End group macro
189 // --------------------------------------------------------------- PUBLIC TYPES
198 typedef struct
199 {
200  digital_out_t cs;
201 
202  // Modules
203 
204  spi_master_t spi;
205  pin_name_t chip_select;
206 
207  uint8_t resolution;
208  uint16_t new_angle;
209 
210 } angle4_t;
211 
215 typedef struct
216 {
217  // Communication gpio pins
218 
219  pin_name_t miso;
220  pin_name_t mosi;
221  pin_name_t sck;
222  pin_name_t cs;
223 
224  // static variable
225 
226  uint32_t spi_speed;
227  spi_master_mode_t spi_mode;
228  spi_master_chip_select_polarity_t cs_polarity;
229 
230  uint8_t cfg_resolution;
231  uint16_t cfg_new_angle;
232 
233 } angle4_cfg_t;
234  // End types group
236 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
237 
242 #ifdef __cplusplus
243 extern "C"{
244 #endif
245 
255 
265 
275 void angle4_default_cfg ( angle4_t *ctx, uint8_t dir, uint8_t data_resolution );
276 
288 void angle4_generic_transfer ( angle4_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
298 void angle4_write_byte ( angle4_t *ctx, uint8_t addr, uint8_t w_data );
299 
309 uint8_t angle4_read_byte ( angle4_t *ctx, uint8_t addr );
310 
319 
339 uint8_t angle4_get_new_angle ( angle4_t *ctx, uint16_t *data_out );
340 
341 #ifdef __cplusplus
342 }
343 #endif
344 #endif // _ANGLE4_H_
345  // End public_function group
348 
349 // ------------------------------------------------------------------------- END
angle4_t::chip_select
pin_name_t chip_select
Definition: angle4.h:205
angle4_t::spi
spi_master_t spi
Definition: angle4.h:204
angle4_read_byte
uint8_t angle4_read_byte(angle4_t *ctx, uint8_t addr)
One byte read function.
angle4_cfg_t::miso
pin_name_t miso
Definition: angle4.h:219
angle4_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: angle4.h:227
angle4_cfg_t::sck
pin_name_t sck
Definition: angle4.h:221
angle4_t::cs
digital_out_t cs
Definition: angle4.h:200
angle4_t::resolution
uint8_t resolution
Definition: angle4.h:207
angle4_default_cfg
void angle4_default_cfg(angle4_t *ctx, uint8_t dir, uint8_t data_resolution)
Click Default Configuration function.
angle4_t
Click ctx object definition.
Definition: angle4.h:199
angle4_cfg_t
Click configuration structure definition.
Definition: angle4.h:216
angle4_cfg_t::cfg_resolution
uint8_t cfg_resolution
Definition: angle4.h:230
angle4_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: angle4.h:228
angle4_cfg_t::cs
pin_name_t cs
Definition: angle4.h:222
angle4_cfg_t::cfg_new_angle
uint16_t cfg_new_angle
Definition: angle4.h:231
angle4_generic_transfer
void angle4_generic_transfer(angle4_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
ANGLE4_RETVAL
#define ANGLE4_RETVAL
Definition: angle4.h:77
angle4_cfg_t::mosi
pin_name_t mosi
Definition: angle4.h:220
angle4_t::new_angle
uint16_t new_angle
Definition: angle4.h:208
angle4_init
ANGLE4_RETVAL angle4_init(angle4_t *ctx, angle4_cfg_t *cfg)
Initialization function.
angle4_write_byte
void angle4_write_byte(angle4_t *ctx, uint8_t addr, uint8_t w_data)
One byte write function.
angle4_start_mesuremenet
void angle4_start_mesuremenet(angle4_t *ctx)
Start meaasurement function.
angle4_get_new_angle
uint8_t angle4_get_new_angle(angle4_t *ctx, uint16_t *data_out)
Functions for reading Angle.
angle4_cfg_setup
void angle4_cfg_setup(angle4_cfg_t *cfg)
Config Object Initialization function.
angle4_cfg_t::spi_speed
uint32_t spi_speed
Definition: angle4.h:226