brushless21  2.0.0.0
brushless21.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 BRUSHLESS21_H
29 #define BRUSHLESS21_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 BRUSHLESS21_EEPROM_USER_0 0x06
71 #define BRUSHLESS21_EEPROM_USER_1 0x07
72 #define BRUSHLESS21_EEPROM_RATED_SPEED_DIR 0x08
73 #define BRUSHLESS21_EEPROM_ACCELERATION_AND_RES 0x09
74 #define BRUSHLESS21_EEPROM_CURRENT 0x0A
75 #define BRUSHLESS21_EEPROM_MODE 0x0B
76 #define BRUSHLESS21_EEPROM_PID_P_AND_INDUCTANCE 0x0C
77 #define BRUSHLESS21_EEPROM_PID_I_AND_DELAY 0x0D
78 #define BRUSHLESS21_EEPROM_LOCK_AND_SOFT_CTRL 0x0F
79 #define BRUSHLESS21_EEPROM_OCP_FCSPEED_ACDEC_BUF_FLT 0x10
80 #define BRUSHLESS21_EEPROM_SPEED_AND_FG 0x11
81 #define BRUSHLESS21_EEPROM_IPD_CURRENT_THR 0x12
82 #define BRUSHLESS21_EEPROM_RATED_VOLTAGE 0x14
83 #define BRUSHLESS21_EEPROM_SLIGHT_MOVE_LOW_DUTY_THR 0x15
84 #define BRUSHLESS21_EEPROM_CLOSED_LOOP_TIME 0x16
85 #define BRUSHLESS21_EEPROM_CONTROL 0xA1
86 #define BRUSHLESS21_EEPROM_ADDRESS 0xA2
87 #define BRUSHLESS21_EEPROM_DATA_IN 0xA3
88 
93 #define BRUSHLESS21_REG_RATED_SPEED_DIR 0x48
94 #define BRUSHLESS21_REG_ACCELERATION_AND_RES 0x49
95 #define BRUSHLESS21_REG_CURRENT 0x4A
96 #define BRUSHLESS21_REG_MODE 0x4B
97 #define BRUSHLESS21_REG_PID_P_AND_INDUCTANCE 0x4C
98 #define BRUSHLESS21_REG_PID_I_AND_DELAY 0x4D
99 #define BRUSHLESS21_REG_LOCK_AND_SOFT_CTRL 0x4F
100 #define BRUSHLESS21_REG_OCP_FCSPEED_ACDEC_BUF_FLT 0x50
101 #define BRUSHLESS21_REG_SPEED_AND_FG 0x51
102 #define BRUSHLESS21_REG_IPD_CURRENT_THR 0x52
103 #define BRUSHLESS21_REG_RATED_VOLTAGE 0x54
104 #define BRUSHLESS21_REG_SLIGHT_MOVE_LOW_DUTY_THR 0x55
105 #define BRUSHLESS21_REG_CLOSED_LOOP_TIME 0x56
106 #define BRUSHLESS21_REG_READBACK_MOTOR_SPEED 0x78
107 #define BRUSHLESS21_REG_READBACK_BUS_CURRENT 0x79
108 #define BRUSHLESS21_REG_READBACK_Q_AXIS_CURRENT 0x7A
109 #define BRUSHLESS21_REG_READBACK_VBB 0x7B
110 #define BRUSHLESS21_REG_READBACK_TEMPERATURE 0x7C
111 #define BRUSHLESS21_REG_READBACK_CONTROL_DEMAND 0x7D
112 #define BRUSHLESS21_REG_READBACK_CONTROL_COMMAND 0x7E
113  // brushless21_reg
115 
116 
131 #define BRUSHLESS21_DIR_REVERSE 0
132 #define BRUSHLESS21_DIR_FORWARD 1
133 
138 #define BRUSHLESS21_MOTOR_SPEED_HZ 0.530
139 #define BRUSHLESS21_VBB_MAX 20
140 #define BRUSHLESS21_VBB_RESOLUTION 0xFF
141 #define BRUSHLESS21_TEMPERATURE_OFFSET 73
142 #define BRUSHLESS21_I2C_DUTY_RESOLUTION 0x01FF
143 #define BRUSHLESS21_DUTY_CONTROLLED_BY_I2C_BIT 0x0200
144 
149 #define BRUSHLESS21_DEF_FREQ 5000
150 
156 #define BRUSHLESS21_SET_DEV_ADDR 0x55
157  // brushless21_cfg
159 
174 #define BRUSHLESS21_MAP_MIKROBUS( cfg, mikrobus ) \
175  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
176  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
177  cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
178  cfg.en = MIKROBUS( mikrobus, MIKROBUS_CS ); \
179  cfg.fg = MIKROBUS( mikrobus, MIKROBUS_INT );
180  // brushless21_map // brushless21
183 
188 typedef enum
189 {
194 
199 typedef struct
200 {
201  // Output pins
202  digital_out_t en;
204  // Input pins
205  digital_in_t fg;
207  // Modules
208  pwm_t pwm;
209  i2c_master_t i2c;
211  uint32_t pwm_freq;
212  uint8_t slave_address;
216 } brushless21_t;
217 
222 typedef struct
223 {
224  // Communication gpio pins
225  pin_name_t pwm;
226  pin_name_t scl;
227  pin_name_t sda;
229  // Additional gpio pins
230  pin_name_t en;
231  pin_name_t fg;
233  // Static variable
234  uint32_t dev_pwm_freq;
235  uint32_t i2c_speed;
236  uint8_t i2c_address;
241 
246 typedef enum
247 {
249  BRUSHLESS21_ERROR = -1
250 
252 
269 
285 
300 
314 
326 
338 
350 err_t brushless21_set_duty_cycle ( brushless21_t *ctx, float duty_cycle );
351 
364 err_t brushless21_write_register ( brushless21_t *ctx, uint8_t reg, uint16_t data_in );
365 
378 err_t brushless21_read_register ( brushless21_t *ctx, uint8_t reg, uint16_t *data_out );
379 
389 
399 
409 
421 err_t brushless21_get_temperature ( brushless21_t *ctx, int8_t *temperature );
422 
434 err_t brushless21_get_motor_speed ( brushless21_t *ctx, float *motor_speed_hz );
435 
447 err_t brushless21_get_vbb_voltage ( brushless21_t *ctx, float *vbb_voltage );
448 
461 err_t brushless21_set_direction ( brushless21_t *ctx, uint8_t direction );
462 
474 
475 #ifdef __cplusplus
476 }
477 #endif
478 #endif // BRUSHLESS21_H
479  // brushless21
481 
482 // ------------------------------------------------------------------------ END
brushless21_enable_device
void brushless21_enable_device(brushless21_t *ctx)
Brushless 21 enable device function.
brushless21_t::pwm
pwm_t pwm
Definition: brushless21.h:208
brushless21_t::i2c
i2c_master_t i2c
Definition: brushless21.h:209
brushless21_t::slave_address
uint8_t slave_address
Definition: brushless21.h:212
brushless21_cfg_t::pwm
pin_name_t pwm
Definition: brushless21.h:225
brushless21_cfg_t
Brushless 21 Click configuration object.
Definition: brushless21.h:223
brushless21_cfg_t::scl
pin_name_t scl
Definition: brushless21.h:226
brushless21_set_duty_cycle
err_t brushless21_set_duty_cycle(brushless21_t *ctx, float duty_cycle)
Brushless 21 set duty cycle function.
brushless21_cfg_t::dev_pwm_freq
uint32_t dev_pwm_freq
Definition: brushless21.h:234
brushless21_disable_device
void brushless21_disable_device(brushless21_t *ctx)
Brushless 21 disable device function.
brushless21_cfg_t::i2c_address
uint8_t i2c_address
Definition: brushless21.h:236
brushless21_read_register
err_t brushless21_read_register(brushless21_t *ctx, uint8_t reg, uint16_t *data_out)
Brushless 21 read register function.
brushless21_pwm_start
err_t brushless21_pwm_start(brushless21_t *ctx)
Brushless 21 start PWM module.
brushless21_cfg_t::drv_sel
brushless21_drv_t drv_sel
Definition: brushless21.h:238
brushless21_t::en
digital_out_t en
Definition: brushless21.h:202
brushless21_default_cfg
err_t brushless21_default_cfg(brushless21_t *ctx)
Brushless 21 default configuration function.
brushless21_get_temperature
err_t brushless21_get_temperature(brushless21_t *ctx, int8_t *temperature)
Brushless 21 get temperature function.
brushless21_write_register
err_t brushless21_write_register(brushless21_t *ctx, uint8_t reg, uint16_t data_in)
Brushless 21 write register function.
brushless21_t
Brushless 21 Click context object.
Definition: brushless21.h:200
brushless21_cfg_setup
void brushless21_cfg_setup(brushless21_cfg_t *cfg)
Brushless 21 configuration object setup function.
brushless21_set_direction
err_t brushless21_set_direction(brushless21_t *ctx, uint8_t direction)
Brushless 21 set direction function.
brushless21_drv_interface_selection
void brushless21_drv_interface_selection(brushless21_cfg_t *cfg, brushless21_drv_t drv_sel)
Brushless 21 driver interface setup function.
brushless21_get_motor_speed
err_t brushless21_get_motor_speed(brushless21_t *ctx, float *motor_speed_hz)
Brushless 21 get motor speed function.
brushless21_switch_direction
err_t brushless21_switch_direction(brushless21_t *ctx)
Brushless 21 switch direction function.
brushless21_cfg_t::fg
pin_name_t fg
Definition: brushless21.h:231
brushless21_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: brushless21.h:235
BRUSHLESS21_ERROR
@ BRUSHLESS21_ERROR
Definition: brushless21.h:249
brushless21_init
err_t brushless21_init(brushless21_t *ctx, brushless21_cfg_t *cfg)
Brushless 21 initialization function.
brushless21_cfg_t::sda
pin_name_t sda
Definition: brushless21.h:227
BRUSHLESS21_DRV_SEL_I2C
@ BRUSHLESS21_DRV_SEL_I2C
Definition: brushless21.h:191
brushless21_return_value_t
brushless21_return_value_t
Brushless 21 Click return value data.
Definition: brushless21.h:247
brushless21_t::pwm_freq
uint32_t pwm_freq
Definition: brushless21.h:211
brushless21_cfg_t::en
pin_name_t en
Definition: brushless21.h:230
brushless21_t::drv_sel
brushless21_drv_t drv_sel
Definition: brushless21.h:214
brushless21_get_vbb_voltage
err_t brushless21_get_vbb_voltage(brushless21_t *ctx, float *vbb_voltage)
Brushless 21 get vbb voltage function.
brushless21_pwm_stop
err_t brushless21_pwm_stop(brushless21_t *ctx)
Brushless 21 stop PWM module.
BRUSHLESS21_OK
@ BRUSHLESS21_OK
Definition: brushless21.h:248
brushless21_get_fg_pin
uint8_t brushless21_get_fg_pin(brushless21_t *ctx)
Brushless 21 get fg pin function.
BRUSHLESS21_DRV_SEL_GPIO
@ BRUSHLESS21_DRV_SEL_GPIO
Definition: brushless21.h:190
brushless21_t::fg
digital_in_t fg
Definition: brushless21.h:205
brushless21_drv_t
brushless21_drv_t
Brushless 21 Click driver selector.
Definition: brushless21.h:189