brushless7  2.0.0.0
brushless7.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 BRUSHLESS7_H
36 #define BRUSHLESS7_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 #include "drv_pwm.h"
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
63 #define BRUSHLESS7_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
65  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
66  cfg.cmo = MIKROBUS( mikrobus, MIKROBUS_AN ); \
67  cfg.dir = MIKROBUS( mikrobus, MIKROBUS_RST ); \
68  cfg.sby = MIKROBUS( mikrobus, MIKROBUS_CS ); \
69  cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
70  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
71 
77 #define BRUSHLESS7_RETVAL uint8_t
78 
79 #define BRUSHLESS7_OK 0x00
80 #define BRUSHLESS7_INIT_ERROR 0xFF
81 
87 #define BRUSHLESS7_SLAVE_ADDR_ID2_LOW_ID1_LOW 0x29
88 #define BRUSHLESS7_SLAVE_ADDR_ID2_LOW_ID1_HIGH 0x29
89 #define BRUSHLESS7_SLAVE_ADDR_ID2_HIGH_ID1_LOW 0x2D
90 #define BRUSHLESS7_SLAVE_ADDR_ID2_HIGH_ID1_HIGH 0x32
91 
97 #define BRUSHLESS7_REG_0 0
98 #define BRUSHLESS7_REG_1 1
99 #define BRUSHLESS7_REG_2 2
100 #define BRUSHLESS7_REG_3 3
101 #define BRUSHLESS7_REG_4 4
102 #define BRUSHLESS7_REG_5 5
103 #define BRUSHLESS7_REG_6 6
104 #define BRUSHLESS7_REG_7 7
105 #define BRUSHLESS7_REG_8 8
106 #define BRUSHLESS7_REG_9 9
107 #define BRUSHLESS7_REG_10 10
108 #define BRUSHLESS7_REG_11 11
109 #define BRUSHLESS7_REG_12 12
110 #define BRUSHLESS7_REG_13 13
111 #define BRUSHLESS7_REG_14 14
112 #define BRUSHLESS7_REG_15 15
113 #define BRUSHLESS7_REG_16 16
114 #define BRUSHLESS7_REG_17 17
115 #define BRUSHLESS7_REG_18 18
116 #define BRUSHLESS7_REG_19 19
117 #define BRUSHLESS7_REG_20 20
118 #define BRUSHLESS7_REG_21 21
119 #define BRUSHLESS7_REG_22 22
120 #define BRUSHLESS7_REG_23 23
121 #define BRUSHLESS7_REG_24 24
122 #define BRUSHLESS7_REG_25 25
123 #define BRUSHLESS7_REG_26 26
124 #define BRUSHLESS7_REG_27 27
125 #define BRUSHLESS7_REG_28 28
126 #define BRUSHLESS7_REG_29 29
127 #define BRUSHLESS7_REG_30 30
128 #define BRUSHLESS7_REG_86 86
129 #define BRUSHLESS7_REG_87 87
130 
136 #define BRUSHLESS7_DEV_ERROR 0xFF
137 #define BRUSHLESS7_DEV_OK 0x00
138 
144 #define BRUSHLESS7_MAX_SPEED_4096 0x00
145 #define BRUSHLESS7_MAX_SPEED_8192 0x02
146 #define BRUSHLESS7_MAX_SPEED_16384 0x04
147 #define BRUSHLESS7_MAX_SPEED_32768 0x06
148 
154 #define BRUSHLESS7_CTRL_TYPE_RPM 0xAA
155 #define BRUSHLESS7_CTRL_TYPE_DUTY 0xBB
156 #define BRUSHLESS7_CTRL_TYPE_STOP 0xCC
157 
163 #define BRUSHLESS7_DIR_CW 0xDD
164 #define BRUSHLESS7_DIR_CCW 0xEE
165 
171 #define BRUSHLESS7_ZERO 0
172 
178 #define BRUSHLESS7_START_DUTY_MAX 49.8
179 #define BRUSHLESS7_START_DUTY_AMP 512
180 
186 #define BRUSHLESS7_STOP_DUTY_MAX 49.6
187 #define BRUSHLESS7_STOP_DUTY_AMP 256
188 #define BRUSHLESS7_STOP_DUTY_MASK 0x80
189 
195 #define BRUSHLESS7_CHANGE_DUTY_MIN 0.4
196 #define BRUSHLESS7_CHANGE_DUTY_MAX 99.6
197 #define BRUSHLESS7_CHANGE_DUTY_AMP 256
198 
204 #define BRUSHLESS7_MAX_DUTY_MIN 50.2
205 #define BRUSHLESS7_MAX_DUTY_MAX 100.0
206 #define BRUSHLESS7_MAX_DUTY_AMP 512
207 #define BRUSHLESS7_MAX_DUTY_DEC 257
208 
214 #define BRUSHLESS7_START_RPM_MAX 4095
215 #define BRUSHLESS7_START_RPM_SHIFT 4
216 #define BRUSHLESS7_START_RPM_MASK 0x0F
217 
223 #define BRUSHLESS7_MAX_SPEED_RMP_MASK 0xF9
224 
230 #define BRUSHLESS7_PRECENTAGE_DIV 100
231 
237 #define BRUSHLESS7_DEF_FREQ 5000
238  // End group macro
241 // --------------------------------------------------------------- PUBLIC TYPES
250 typedef struct
251 {
252  // Output pins
253 
254  digital_out_t dir;
255  digital_out_t sby;
256 
257  // Input pins
258 
259  digital_in_t cmo;
260  digital_in_t int_pin;
261 
262  // Modules
263 
264  pwm_t pwm;
265  i2c_master_t i2c;
266 
267  // ctx variable
268 
269  uint32_t pwm_freq;
270  uint8_t slave_address;
271 
272 } brushless7_t;
273 
277 typedef struct
278 {
279  // Communication gpio pins
280 
281  pin_name_t scl;
282  pin_name_t sda;
283  pin_name_t pwm;
284 
285  // Additional gpio pins
286 
287  pin_name_t cmo;
288  pin_name_t dir;
289  pin_name_t sby;
290  pin_name_t int_pin;
291 
292  // static variable
293 
294  uint32_t dev_pwm_freq;
295  uint32_t i2c_speed;
296  uint8_t i2c_address;
297 
299  // End types group
301 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
302 
308 #ifdef __cplusplus
309 extern "C"{
310 #endif
311 
321 
331 
340 
351 void brushless7_generic_write ( brushless7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
352 
363 void brushless7_generic_read ( brushless7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
364 
371 void brushless7_set_sby_pin_state ( brushless7_t *ctx, uint8_t state );
372 
379 
388 
399 uint8_t brushless7_start_duty ( brushless7_t *ctx, float duty_ptc );
400 
411 uint8_t brushless7_stop_duty ( brushless7_t *ctx, float duty_ptc );
412 
423 uint8_t brushless7_change_duty ( brushless7_t *ctx, float duty_ptc );
424 
435 uint8_t brushless7_max_duty ( brushless7_t *ctx, float duty_ptc );
436 
447 uint8_t brushless7_start_rpm ( brushless7_t *ctx, uint16_t rpm_val );
448 
457 uint8_t brushless7_max_speed_rpm ( brushless7_t *ctx, uint8_t max_speed_rpm );
458 
467 uint8_t brushless7_rotating_direction ( brushless7_t *ctx, uint8_t direction_rot );
468 
477 uint8_t brushless7_control_mode_set ( brushless7_t *ctx, uint8_t ctrl_type );
478 
488 void brushless7_set_duty_cycle ( brushless7_t *ctx, float duty_cycle );
489 
498 
507 
508 #ifdef __cplusplus
509 }
510 #endif
511 #endif // _BRUSHLESS7_H_
512  // End public_function group
515 
516 // ------------------------------------------------------------------------- END
brushless7_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: brushless7.h:295
brushless7_default_cfg
void brushless7_default_cfg(brushless7_t *ctx)
Click Default Configuration function.
brushless7_max_duty
uint8_t brushless7_max_duty(brushless7_t *ctx, float duty_ptc)
Function for setting max duty of device.
brushless7_generic_read
void brushless7_generic_read(brushless7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
brushless7_cfg_t::dir
pin_name_t dir
Definition: brushless7.h:288
brushless7_t::pwm_freq
uint32_t pwm_freq
Definition: brushless7.h:269
brushless7_cfg_t::sby
pin_name_t sby
Definition: brushless7.h:289
brushless7_pwm_stop
void brushless7_pwm_stop(brushless7_t *ctx)
Stop PWM module.
brushless7_generic_write
void brushless7_generic_write(brushless7_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
brushless7_t::sby
digital_out_t sby
Definition: brushless7.h:255
brushless7_t::pwm
pwm_t pwm
Definition: brushless7.h:264
brushless7_cfg_t::scl
pin_name_t scl
Definition: brushless7.h:281
brushless7_pwm_start
void brushless7_pwm_start(brushless7_t *ctx)
Start PWM module.
brushless7_start_rpm
uint8_t brushless7_start_rpm(brushless7_t *ctx, uint16_t rpm_val)
Function for setting start rpm parameter of device.
brushless7_start_duty
uint8_t brushless7_start_duty(brushless7_t *ctx, float duty_ptc)
Function for setting start duty of device.
brushless7_cfg_t::cmo
pin_name_t cmo
Definition: brushless7.h:287
brushless7_t::dir
digital_out_t dir
Definition: brushless7.h:254
brushless7_set_duty_cycle
void brushless7_set_duty_cycle(brushless7_t *ctx, float duty_cycle)
Generic sets PWM duty cycle.
brushless7_t::i2c
i2c_master_t i2c
Definition: brushless7.h:265
brushless7_cfg_t::pwm
pin_name_t pwm
Definition: brushless7.h:283
brushless7_get_int_pin_state
uint8_t brushless7_get_int_pin_state(brushless7_t *ctx)
Generic function for getting INT pin state.
brushless7_max_speed_rpm
uint8_t brushless7_max_speed_rpm(brushless7_t *ctx, uint8_t max_speed_rpm)
Function for setting max rpm parameter of device.
brushless7_cfg_t::sda
pin_name_t sda
Definition: brushless7.h:282
brushless7_toggle_dir_pin_state
void brushless7_toggle_dir_pin_state(brushless7_t *ctx)
Generic function for toggling DIR pin state.
brushless7_cfg_setup
void brushless7_cfg_setup(brushless7_cfg_t *cfg)
Config Object Initialization function.
brushless7_init
BRUSHLESS7_RETVAL brushless7_init(brushless7_t *ctx, brushless7_cfg_t *cfg)
Initialization function.
brushless7_rotating_direction
uint8_t brushless7_rotating_direction(brushless7_t *ctx, uint8_t direction_rot)
Function for setting rotation direction.
brushless7_control_mode_set
uint8_t brushless7_control_mode_set(brushless7_t *ctx, uint8_t ctrl_type)
Function for setting type of device control.
brushless7_cfg_t::dev_pwm_freq
uint32_t dev_pwm_freq
Definition: brushless7.h:294
brushless7_t::int_pin
digital_in_t int_pin
Definition: brushless7.h:260
brushless7_set_sby_pin_state
void brushless7_set_sby_pin_state(brushless7_t *ctx, uint8_t state)
Generic function for setting SBY pin state.
brushless7_change_duty
uint8_t brushless7_change_duty(brushless7_t *ctx, float duty_ptc)
Function for changeing duty of device.
brushless7_t::slave_address
uint8_t slave_address
Definition: brushless7.h:270
brushless7_t::cmo
digital_in_t cmo
Definition: brushless7.h:259
BRUSHLESS7_RETVAL
#define BRUSHLESS7_RETVAL
Definition: brushless7.h:77
brushless7_cfg_t::int_pin
pin_name_t int_pin
Definition: brushless7.h:290
brushless7_cfg_t
Click configuration structure definition.
Definition: brushless7.h:278
brushless7_cfg_t::i2c_address
uint8_t i2c_address
Definition: brushless7.h:296
brushless7_stop_duty
uint8_t brushless7_stop_duty(brushless7_t *ctx, float duty_ptc)
Function for setting stop duty of device.
brushless7_t
Click ctx object definition.
Definition: brushless7.h:251