brushless10  2.1.0.0
brushless10.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef BRUSHLESS10_H
29 #define BRUSHLESS10_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_pwm.h"
48 #include "drv_i2c_master.h"
49 
70 #define BRUSHLESS10_DAC_REG_DEVICE_CONFIG 0x01
71 #define BRUSHLESS10_DAC_REG_STATUS_TRIGGER 0x02
72 #define BRUSHLESS10_DAC_REG_BRDCAST 0x03
73 #define BRUSHLESS10_DAC_REG_DACA_DATA_LA 0x08
74 #define BRUSHLESS10_DAC_REG_DACB_DATA_MIN_SP 0x09
75 #define BRUSHLESS10_DAC_REG_DACC_DATA_SEL_LD 0x0A
76 #define BRUSHLESS10_DAC_REG_DACD_DATA_FPWM 0x0B
77 #define BRUSHLESS10_DAC_REG_DACE_DATA_SEL_LA 0x0C
78 #define BRUSHLESS10_DAC_REG_DACF_DATA_SEL_FG 0x0D
79 #define BRUSHLESS10_DAC_REG_DACG_DATA_SEL_SP 0x0E
80 #define BRUSHLESS10_DAC_REG_DACH_DATA_CW_CCW 0x0F
81  // brushless10_reg
83 
98 #define BRUSHLESS10_DAC_CHA_LA 0
99 #define BRUSHLESS10_DAC_CHB_MIN_SP 1
100 #define BRUSHLESS10_DAC_CHC_SEL_LD 2
101 #define BRUSHLESS10_DAC_CHD_FPWM 3
102 #define BRUSHLESS10_DAC_CHE_SEL_LA 4
103 #define BRUSHLESS10_DAC_CHF_SEL_FG 5
104 #define BRUSHLESS10_DAC_CHG_SEL_SP 6
105 #define BRUSHLESS10_DAC_CHH_CW_CCW 7
106 
111 #define BRUSHLESS10_LA0_STEP_0 88
112 #define BRUSHLESS10_LA0_STEP_1 234
113 #define BRUSHLESS10_LA0_STEP_2 332
114 #define BRUSHLESS10_LA0_STEP_3 430
115 #define BRUSHLESS10_LA0_STEP_4 527
116 #define BRUSHLESS10_LA0_STEP_5 625
117 #define BRUSHLESS10_LA0_STEP_6 723
118 #define BRUSHLESS10_LA0_STEP_7 820
119 #define BRUSHLESS10_LA0_STEP_8 918
120 #define BRUSHLESS10_LA0_STEP_9 1016
121 #define BRUSHLESS10_LA0_STEP_10 1113
122 #define BRUSHLESS10_LA0_STEP_11 1211
123 #define BRUSHLESS10_LA0_STEP_12 1309
124 #define BRUSHLESS10_LA0_STEP_13 1406
125 #define BRUSHLESS10_LA0_STEP_14 1504
126 #define BRUSHLESS10_LA0_STEP_15 1602
127 #define BRUSHLESS10_LA0_STEP_16 1699
128 #define BRUSHLESS10_LA0_STEP_17 1797
129 #define BRUSHLESS10_LA0_STEP_18 1894
130 #define BRUSHLESS10_LA0_STEP_19 1992
131 #define BRUSHLESS10_LA0_STEP_20 2090
132 #define BRUSHLESS10_LA0_STEP_21 2187
133 #define BRUSHLESS10_LA0_STEP_22 2285
134 #define BRUSHLESS10_LA0_STEP_23 2383
135 #define BRUSHLESS10_LA0_STEP_24 2480
136 #define BRUSHLESS10_LA0_STEP_25 2578
137 #define BRUSHLESS10_LA0_STEP_26 2676
138 #define BRUSHLESS10_LA0_STEP_27 2773
139 #define BRUSHLESS10_LA0_STEP_28 2871
140 #define BRUSHLESS10_LA0_STEP_29 2969
141 #define BRUSHLESS10_LA0_STEP_30 3066
142 #define BRUSHLESS10_LA0_STEP_31 4062
143 #define BRUSHLESS10_LA1_STEP_0 234
144 #define BRUSHLESS10_LA1_STEP_1 673
145 #define BRUSHLESS10_LA1_STEP_2 1064
146 #define BRUSHLESS10_LA1_STEP_3 1455
147 #define BRUSHLESS10_LA1_STEP_4 1845
148 #define BRUSHLESS10_LA1_STEP_5 2236
149 #define BRUSHLESS10_LA1_STEP_6 2627
150 #define BRUSHLESS10_LA1_STEP_7 3916
151 
156 #define BRUSHLESS10_MIN_SP_STEP_0 136
157 #define BRUSHLESS10_MIN_SP_STEP_1 478
158 #define BRUSHLESS10_MIN_SP_STEP_2 869
159 #define BRUSHLESS10_MIN_SP_STEP_3 1259
160 #define BRUSHLESS10_MIN_SP_STEP_4 1650
161 #define BRUSHLESS10_MIN_SP_STEP_5 2041
162 #define BRUSHLESS10_MIN_SP_STEP_6 2431
163 #define BRUSHLESS10_MIN_SP_STEP_7 2822
164 #define BRUSHLESS10_MIN_SP_STEP_8 3916
165 
170 #define BRUSHLESS10_FPWM_STEP_0 478
171 #define BRUSHLESS10_FPWM_STEP_1 1455
172 #define BRUSHLESS10_FPWM_STEP_2 2431
173 #define BRUSHLESS10_FPWM_STEP_3 3965
174 
179 #define BRUSHLESS10_SEL_LD_STEP_0 478
180 #define BRUSHLESS10_SEL_LD_STEP_1 1455
181 #define BRUSHLESS10_SEL_LD_STEP_2 2431
182 #define BRUSHLESS10_SEL_LD_STEP_3 3965
183 
188 #define BRUSHLESS10_SEL_FG_STEP_0 967
189 #define BRUSHLESS10_SEL_FG_STEP_1 3476
190 
195 #define BRUSHLESS10_SEL_SP_STEP_0 478
196 #define BRUSHLESS10_SEL_SP_STEP_1 1943
197 #define BRUSHLESS10_SEL_SP_STEP_2 3965
198 
203 #define BRUSHLESS10_SEL_LA_STEP_0 478
204 #define BRUSHLESS10_SEL_LA_STEP_1 1943
205 #define BRUSHLESS10_SEL_LA_STEP_2 3965
206 
211 #define BRUSHLESS10_CW_CCW_0 0
212 #define BRUSHLESS10_CW_CCW_1 5000
213 
218 #define BRUSHLESS10_DAC_VREF 5000
219 #define BRUSHLESS10_DAC_RESOLUTION 256
220 #define BRUSHLESS10_DAC_ROUND_TO_INT 0.5f
221 
226 #define BRUSHLESS10_DAC_CONFIG_PDN_ALL 0x0100
227 #define BRUSHLESS10_DAC_CONFIG_PDN_CHA 0x0001
228 #define BRUSHLESS10_DAC_CONFIG_PDN_CHB 0x0002
229 #define BRUSHLESS10_DAC_CONFIG_PDN_CHC 0x0004
230 #define BRUSHLESS10_DAC_CONFIG_PDN_CHD 0x0008
231 #define BRUSHLESS10_DAC_CONFIG_PDN_CHE 0x0010
232 #define BRUSHLESS10_DAC_CONFIG_PDN_CHF 0x0020
233 #define BRUSHLESS10_DAC_CONFIG_PDN_CHG 0x0040
234 #define BRUSHLESS10_DAC_CONFIG_PDN_CHH 0x0080
235 #define BRUSHLESS10_DAC_CONFIG_ENABLE_ALL_CH 0x0000
236 
241 #define BRUSHLESS10_DAC_DEVICE_ID 0x0500
242 #define BRUSHLESS10_DAC_DEVICE_ID_MASK 0x0FC0
243 #define BRUSHLESS10_DAC_SW_RESET 0x000A
244 
249 #define BRUSHLESS10_DEF_FREQ 50000
250 
256 #define BRUSHLESS10_DAC_DEVICE_ADDRESS_BROADCAST 0x47
257 #define BRUSHLESS10_DAC_DEVICE_ADDRESS_0 0x48
258 #define BRUSHLESS10_DAC_DEVICE_ADDRESS_1 0x49
259  // brushless10_set
261 
276 #define BRUSHLESS10_MAP_MIKROBUS( cfg, mikrobus ) \
277  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
278  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
279  cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
280  cfg.fg_out = MIKROBUS( mikrobus, MIKROBUS_AN ); \
281  cfg.brake = MIKROBUS( mikrobus, MIKROBUS_RST ); \
282  cfg.ld_out = MIKROBUS( mikrobus, MIKROBUS_INT );
283  // brushless10_map // brushless10
286 
291 typedef struct
292 {
293  // Output pins
294  digital_out_t brake;
296  // Input pins
297  digital_in_t fg_out;
298  digital_in_t ld_out;
300  // Modules
301  pwm_t pwm;
303  // ctx variable
304  uint32_t pwm_freq;
306  // Modules
307  i2c_master_t i2c;
309  // I2C slave address
310  uint8_t slave_address;
312 } brushless10_t;
313 
318 typedef struct
319 {
320  // Communication gpio pins
321  pin_name_t scl;
322  pin_name_t sda;
323  pin_name_t pwm;
325  // Additional gpio pins
326  pin_name_t fg_out;
327  pin_name_t brake;
328  pin_name_t ld_out;
330  // Static variable
331  uint32_t dev_pwm_freq;
332  uint32_t i2c_speed;
333  uint8_t i2c_address;
336 
341 typedef enum
342 {
344  BRUSHLESS10_ERROR = -1
345 
347 
364 
379 
393 
406 err_t brushless10_dac_write ( brushless10_t *ctx, uint8_t reg, uint16_t data_in );
407 
420 err_t brushless10_dac_read ( brushless10_t *ctx, uint8_t reg, uint16_t *data_out );
421 
433 err_t brushless10_set_duty_cycle ( brushless10_t *ctx, float duty_cycle );
434 
446 
458 
468 
478 
488 
498 
510 
522 
543 err_t brushless10_dac_set_data ( brushless10_t *ctx, uint8_t channel, uint8_t data_in );
544 
565 err_t brushless10_dac_set_vout ( brushless10_t *ctx, uint8_t channel, uint16_t vout_mv );
566 
567 #ifdef __cplusplus
568 }
569 #endif
570 #endif // BRUSHLESS10_H
571  // brushless10
573 
574 // ------------------------------------------------------------------------ END
brushless10_cfg_t::scl
pin_name_t scl
Definition: brushless10.h:321
brushless10_default_cfg
err_t brushless10_default_cfg(brushless10_t *ctx)
Brushless 10 default configuration function.
brushless10_cfg_t
Brushless 10 Click configuration object.
Definition: brushless10.h:319
brushless10_get_fg_out_pin
uint8_t brushless10_get_fg_out_pin(brushless10_t *ctx)
Brushless 10 get fg out pin function.
brushless10_t::brake
digital_out_t brake
Definition: brushless10.h:294
brushless10_dac_write
err_t brushless10_dac_write(brushless10_t *ctx, uint8_t reg, uint16_t data_in)
Brushless 10 DAC write function.
brushless10_t::ld_out
digital_in_t ld_out
Definition: brushless10.h:298
brushless10_get_ld_out_pin
uint8_t brushless10_get_ld_out_pin(brushless10_t *ctx)
Brushless 10 get ld out pin function.
brushless10_cfg_t::sda
pin_name_t sda
Definition: brushless10.h:322
BRUSHLESS10_ERROR
@ BRUSHLESS10_ERROR
Definition: brushless10.h:344
brushless10_cfg_t::brake
pin_name_t brake
Definition: brushless10.h:327
brushless10_dac_set_data
err_t brushless10_dac_set_data(brushless10_t *ctx, uint8_t channel, uint8_t data_in)
Brushless 10 DAC set data function.
brushless10_cfg_t::ld_out
pin_name_t ld_out
Definition: brushless10.h:328
brushless10_pwm_stop
err_t brushless10_pwm_stop(brushless10_t *ctx)
Brushless 10 stop PWM module.
brushless10_cfg_setup
void brushless10_cfg_setup(brushless10_cfg_t *cfg)
Brushless 10 configuration object setup function.
brushless10_cfg_t::dev_pwm_freq
uint32_t dev_pwm_freq
Definition: brushless10.h:331
brushless10_set_duty_cycle
err_t brushless10_set_duty_cycle(brushless10_t *ctx, float duty_cycle)
Brushless 10 sets PWM duty cycle.
brushless10_release_brake
void brushless10_release_brake(brushless10_t *ctx)
Brushless 10 release brake function.
brushless10_pull_brake
void brushless10_pull_brake(brushless10_t *ctx)
Brushless 10 pull brake function.
brushless10_dac_read
err_t brushless10_dac_read(brushless10_t *ctx, uint8_t reg, uint16_t *data_out)
Brushless 10 DAC read function.
brushless10_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: brushless10.h:332
brushless10_t::pwm_freq
uint32_t pwm_freq
Definition: brushless10.h:304
brushless10_pwm_start
err_t brushless10_pwm_start(brushless10_t *ctx)
Brushless 10 start PWM module.
brushless10_dac_sw_reset
err_t brushless10_dac_sw_reset(brushless10_t *ctx)
Brushless 10 DAC sw reset function.
brushless10_t::i2c
i2c_master_t i2c
Definition: brushless10.h:307
brushless10_t::slave_address
uint8_t slave_address
Definition: brushless10.h:310
brushless10_t::pwm
pwm_t pwm
Definition: brushless10.h:301
brushless10_return_value_t
brushless10_return_value_t
Brushless 10 Click return value data.
Definition: brushless10.h:342
brushless10_cfg_t::i2c_address
uint8_t i2c_address
Definition: brushless10.h:333
brushless10_dac_check_com
err_t brushless10_dac_check_com(brushless10_t *ctx)
Brushless 10 DAC check com function.
brushless10_dac_set_vout
err_t brushless10_dac_set_vout(brushless10_t *ctx, uint8_t channel, uint16_t vout_mv)
Brushless 10 DAC set vout function.
brushless10_cfg_t::fg_out
pin_name_t fg_out
Definition: brushless10.h:326
brushless10_t
Brushless 10 Click context object.
Definition: brushless10.h:292
BRUSHLESS10_OK
@ BRUSHLESS10_OK
Definition: brushless10.h:343
brushless10_init
err_t brushless10_init(brushless10_t *ctx, brushless10_cfg_t *cfg)
Brushless 10 initialization function.
brushless10_cfg_t::pwm
pin_name_t pwm
Definition: brushless10.h:323
brushless10_t::fg_out
digital_in_t fg_out
Definition: brushless10.h:297