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 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_pwm.h"
52 #include "drv_i2c_master.h"
53 
74 #define BRUSHLESS10_DAC_REG_DEVICE_CONFIG 0x01
75 #define BRUSHLESS10_DAC_REG_STATUS_TRIGGER 0x02
76 #define BRUSHLESS10_DAC_REG_BRDCAST 0x03
77 #define BRUSHLESS10_DAC_REG_DACA_DATA_LA 0x08
78 #define BRUSHLESS10_DAC_REG_DACB_DATA_MIN_SP 0x09
79 #define BRUSHLESS10_DAC_REG_DACC_DATA_SEL_LD 0x0A
80 #define BRUSHLESS10_DAC_REG_DACD_DATA_FPWM 0x0B
81 #define BRUSHLESS10_DAC_REG_DACE_DATA_SEL_LA 0x0C
82 #define BRUSHLESS10_DAC_REG_DACF_DATA_SEL_FG 0x0D
83 #define BRUSHLESS10_DAC_REG_DACG_DATA_SEL_SP 0x0E
84 #define BRUSHLESS10_DAC_REG_DACH_DATA_CW_CCW 0x0F
85  // brushless10_reg
87 
102 #define BRUSHLESS10_DAC_CHA_LA 0
103 #define BRUSHLESS10_DAC_CHB_MIN_SP 1
104 #define BRUSHLESS10_DAC_CHC_SEL_LD 2
105 #define BRUSHLESS10_DAC_CHD_FPWM 3
106 #define BRUSHLESS10_DAC_CHE_SEL_LA 4
107 #define BRUSHLESS10_DAC_CHF_SEL_FG 5
108 #define BRUSHLESS10_DAC_CHG_SEL_SP 6
109 #define BRUSHLESS10_DAC_CHH_CW_CCW 7
110 
115 #define BRUSHLESS10_LA0_STEP_0 88
116 #define BRUSHLESS10_LA0_STEP_1 234
117 #define BRUSHLESS10_LA0_STEP_2 332
118 #define BRUSHLESS10_LA0_STEP_3 430
119 #define BRUSHLESS10_LA0_STEP_4 527
120 #define BRUSHLESS10_LA0_STEP_5 625
121 #define BRUSHLESS10_LA0_STEP_6 723
122 #define BRUSHLESS10_LA0_STEP_7 820
123 #define BRUSHLESS10_LA0_STEP_8 918
124 #define BRUSHLESS10_LA0_STEP_9 1016
125 #define BRUSHLESS10_LA0_STEP_10 1113
126 #define BRUSHLESS10_LA0_STEP_11 1211
127 #define BRUSHLESS10_LA0_STEP_12 1309
128 #define BRUSHLESS10_LA0_STEP_13 1406
129 #define BRUSHLESS10_LA0_STEP_14 1504
130 #define BRUSHLESS10_LA0_STEP_15 1602
131 #define BRUSHLESS10_LA0_STEP_16 1699
132 #define BRUSHLESS10_LA0_STEP_17 1797
133 #define BRUSHLESS10_LA0_STEP_18 1894
134 #define BRUSHLESS10_LA0_STEP_19 1992
135 #define BRUSHLESS10_LA0_STEP_20 2090
136 #define BRUSHLESS10_LA0_STEP_21 2187
137 #define BRUSHLESS10_LA0_STEP_22 2285
138 #define BRUSHLESS10_LA0_STEP_23 2383
139 #define BRUSHLESS10_LA0_STEP_24 2480
140 #define BRUSHLESS10_LA0_STEP_25 2578
141 #define BRUSHLESS10_LA0_STEP_26 2676
142 #define BRUSHLESS10_LA0_STEP_27 2773
143 #define BRUSHLESS10_LA0_STEP_28 2871
144 #define BRUSHLESS10_LA0_STEP_29 2969
145 #define BRUSHLESS10_LA0_STEP_30 3066
146 #define BRUSHLESS10_LA0_STEP_31 4062
147 #define BRUSHLESS10_LA1_STEP_0 234
148 #define BRUSHLESS10_LA1_STEP_1 673
149 #define BRUSHLESS10_LA1_STEP_2 1064
150 #define BRUSHLESS10_LA1_STEP_3 1455
151 #define BRUSHLESS10_LA1_STEP_4 1845
152 #define BRUSHLESS10_LA1_STEP_5 2236
153 #define BRUSHLESS10_LA1_STEP_6 2627
154 #define BRUSHLESS10_LA1_STEP_7 3916
155 
160 #define BRUSHLESS10_MIN_SP_STEP_0 136
161 #define BRUSHLESS10_MIN_SP_STEP_1 478
162 #define BRUSHLESS10_MIN_SP_STEP_2 869
163 #define BRUSHLESS10_MIN_SP_STEP_3 1259
164 #define BRUSHLESS10_MIN_SP_STEP_4 1650
165 #define BRUSHLESS10_MIN_SP_STEP_5 2041
166 #define BRUSHLESS10_MIN_SP_STEP_6 2431
167 #define BRUSHLESS10_MIN_SP_STEP_7 2822
168 #define BRUSHLESS10_MIN_SP_STEP_8 3916
169 
174 #define BRUSHLESS10_FPWM_STEP_0 478
175 #define BRUSHLESS10_FPWM_STEP_1 1455
176 #define BRUSHLESS10_FPWM_STEP_2 2431
177 #define BRUSHLESS10_FPWM_STEP_3 3965
178 
183 #define BRUSHLESS10_SEL_LD_STEP_0 478
184 #define BRUSHLESS10_SEL_LD_STEP_1 1455
185 #define BRUSHLESS10_SEL_LD_STEP_2 2431
186 #define BRUSHLESS10_SEL_LD_STEP_3 3965
187 
192 #define BRUSHLESS10_SEL_FG_STEP_0 967
193 #define BRUSHLESS10_SEL_FG_STEP_1 3476
194 
199 #define BRUSHLESS10_SEL_SP_STEP_0 478
200 #define BRUSHLESS10_SEL_SP_STEP_1 1943
201 #define BRUSHLESS10_SEL_SP_STEP_2 3965
202 
207 #define BRUSHLESS10_SEL_LA_STEP_0 478
208 #define BRUSHLESS10_SEL_LA_STEP_1 1943
209 #define BRUSHLESS10_SEL_LA_STEP_2 3965
210 
215 #define BRUSHLESS10_CW_CCW_0 0
216 #define BRUSHLESS10_CW_CCW_1 5000
217 
222 #define BRUSHLESS10_DAC_VREF 5000
223 #define BRUSHLESS10_DAC_RESOLUTION 256
224 #define BRUSHLESS10_DAC_ROUND_TO_INT 0.5f
225 
230 #define BRUSHLESS10_DAC_CONFIG_PDN_ALL 0x0100
231 #define BRUSHLESS10_DAC_CONFIG_PDN_CHA 0x0001
232 #define BRUSHLESS10_DAC_CONFIG_PDN_CHB 0x0002
233 #define BRUSHLESS10_DAC_CONFIG_PDN_CHC 0x0004
234 #define BRUSHLESS10_DAC_CONFIG_PDN_CHD 0x0008
235 #define BRUSHLESS10_DAC_CONFIG_PDN_CHE 0x0010
236 #define BRUSHLESS10_DAC_CONFIG_PDN_CHF 0x0020
237 #define BRUSHLESS10_DAC_CONFIG_PDN_CHG 0x0040
238 #define BRUSHLESS10_DAC_CONFIG_PDN_CHH 0x0080
239 #define BRUSHLESS10_DAC_CONFIG_ENABLE_ALL_CH 0x0000
240 
245 #define BRUSHLESS10_DAC_DEVICE_ID 0x0500
246 #define BRUSHLESS10_DAC_DEVICE_ID_MASK 0x0FC0
247 #define BRUSHLESS10_DAC_SW_RESET 0x000A
248 
253 #define BRUSHLESS10_DEF_FREQ 50000
254 
260 #define BRUSHLESS10_DAC_DEVICE_ADDRESS_BROADCAST 0x47
261 #define BRUSHLESS10_DAC_DEVICE_ADDRESS_0 0x48
262 #define BRUSHLESS10_DAC_DEVICE_ADDRESS_1 0x49
263  // brushless10_set
265 
280 #define BRUSHLESS10_MAP_MIKROBUS( cfg, mikrobus ) \
281  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
282  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
283  cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
284  cfg.fg_out = MIKROBUS( mikrobus, MIKROBUS_AN ); \
285  cfg.brake = MIKROBUS( mikrobus, MIKROBUS_RST ); \
286  cfg.ld_out = MIKROBUS( mikrobus, MIKROBUS_INT );
287  // brushless10_map // brushless10
290 
295 typedef struct
296 {
297  // Output pins
298  digital_out_t brake;
300  // Input pins
301  digital_in_t fg_out;
302  digital_in_t ld_out;
304  // Modules
305  pwm_t pwm;
307  // ctx variable
308  uint32_t pwm_freq;
310  // Modules
311  i2c_master_t i2c;
313  // I2C slave address
314  uint8_t slave_address;
316 } brushless10_t;
317 
322 typedef struct
323 {
324  // Communication gpio pins
325  pin_name_t scl;
326  pin_name_t sda;
327  pin_name_t pwm;
329  // Additional gpio pins
330  pin_name_t fg_out;
331  pin_name_t brake;
332  pin_name_t ld_out;
334  // Static variable
335  uint32_t dev_pwm_freq;
336  uint32_t i2c_speed;
337  uint8_t i2c_address;
340 
345 typedef enum
346 {
348  BRUSHLESS10_ERROR = -1
349 
351 
368 
383 
397 
410 err_t brushless10_dac_write ( brushless10_t *ctx, uint8_t reg, uint16_t data_in );
411 
424 err_t brushless10_dac_read ( brushless10_t *ctx, uint8_t reg, uint16_t *data_out );
425 
437 err_t brushless10_set_duty_cycle ( brushless10_t *ctx, float duty_cycle );
438 
450 
462 
472 
482 
492 
502 
514 
526 
547 err_t brushless10_dac_set_data ( brushless10_t *ctx, uint8_t channel, uint8_t data_in );
548 
569 err_t brushless10_dac_set_vout ( brushless10_t *ctx, uint8_t channel, uint16_t vout_mv );
570 
571 #ifdef __cplusplus
572 }
573 #endif
574 #endif // BRUSHLESS10_H
575  // brushless10
577 
578 // ------------------------------------------------------------------------ END
brushless10_cfg_t::scl
pin_name_t scl
Definition: brushless10.h:325
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:323
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:298
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:302
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:326
BRUSHLESS10_ERROR
@ BRUSHLESS10_ERROR
Definition: brushless10.h:348
brushless10_cfg_t::brake
pin_name_t brake
Definition: brushless10.h:331
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:332
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:335
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:336
brushless10_t::pwm_freq
uint32_t pwm_freq
Definition: brushless10.h:308
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:311
brushless10_t::slave_address
uint8_t slave_address
Definition: brushless10.h:314
brushless10_t::pwm
pwm_t pwm
Definition: brushless10.h:305
brushless10_return_value_t
brushless10_return_value_t
Brushless 10 Click return value data.
Definition: brushless10.h:346
brushless10_cfg_t::i2c_address
uint8_t i2c_address
Definition: brushless10.h:337
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:330
brushless10_t
Brushless 10 Click context object.
Definition: brushless10.h:296
BRUSHLESS10_OK
@ BRUSHLESS10_OK
Definition: brushless10.h:347
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:327
brushless10_t::fg_out
digital_in_t fg_out
Definition: brushless10.h:301