brushless27  2.1.0.0
brushless27.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 BRUSHLESS27_H
29 #define BRUSHLESS27_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_i2c_master.h"
52 
73 #define BRUSHLESS27_REG_INPUT_PORT 0x00
74 #define BRUSHLESS27_REG_OUTPUT_PORT 0x01
75 #define BRUSHLESS27_REG_POLARITY_INV 0x02
76 #define BRUSHLESS27_REG_CONFIG 0x03
77  // brushless27_reg
79 
94 #define BRUSHLESS27_TR_COM_STATE_COAST 0x00
95 #define BRUSHLESS27_TR_COM_STATE_1 0x01
96 #define BRUSHLESS27_TR_COM_STATE_2 0x02
97 #define BRUSHLESS27_TR_COM_STATE_3 0x03
98 #define BRUSHLESS27_TR_COM_STATE_4 0x04
99 #define BRUSHLESS27_TR_COM_STATE_5 0x05
100 #define BRUSHLESS27_TR_COM_STATE_6 0x06
101 #define BRUSHLESS27_TR_COM_STATE_BRAKE 0x07
102 
107 #define BRUSHLESS27_NONE_PIN 0x00
108 #define BRUSHLESS27_UH_PIN 0x01
109 #define BRUSHLESS27_VH_PIN 0x02
110 #define BRUSHLESS27_WH_PIN 0x04
111 #define BRUSHLESS27_VL_PIN 0x10
112 #define BRUSHLESS27_WL_PIN 0x20
113 #define BRUSHLESS27_UL_PIN 0x40
114 #define BRUSHLESS27_FLT_PIN 0x80
115 #define BRUSHLESS27_ALL_OUTPUT_PIN 0x77
116 #define BRUSHLESS27_ALL_PIN 0xF7
117 
122 #define BRUSHLESS27_MODE_ENX_INX 0x01
123 #define BRUSHLESS27_MODE_INXL_INXH 0x00
124 
129 #define BRUSHLESS27_DIR_CW 0x01
130 #define BRUSHLESS27_DIR_CCW 0x00
131 
136 #define BRUSHLESS27_PIN_STATE_LOW 0x00
137 #define BRUSHLESS27_PIN_STATE_HIGH 0x01
138 
143 #define BRUSHLESS27_FLT_PIN_INACTIVE 0x01
144 #define BRUSHLESS27_FLT_PIN_ACTIVE 0x00
145 
150 #define BRUSHLESS27_SPEED_MIN 0
151 #define BRUSHLESS27_SPEED_MAX 100
152 #define BRUSHLESS27_NUM_PIN_TOGGLE 8
153 #define BRUSHLESS27_ROUND_TO_NEAREAST_INT 0.5
154 
164 #define BRUSHLESS27_COM_SEQ_DURATION_FOR_SPEED_MAX 21.611
165 #define BRUSHLESS27_COM_SEQ_DURATION_SPEED_STEP ( 22.044 - BRUSHLESS27_COM_SEQ_DURATION_FOR_SPEED_MAX )
166 
172 #define BRUSHLESS27_DEVICE_ADDRESS_A1A0_00 0x70
173 #define BRUSHLESS27_DEVICE_ADDRESS_A1A0_01 0x71
174 #define BRUSHLESS27_DEVICE_ADDRESS_A1A0_10 0x72
175 #define BRUSHLESS27_DEVICE_ADDRESS_A1A0_11 0x73
176  // brushless27_set
178 
193 #define BRUSHLESS27_MAP_MIKROBUS( cfg, mikrobus ) \
194  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
195  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
196  cfg.sen = MIKROBUS( mikrobus, MIKROBUS_AN ); \
197  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
198  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
199  // brushless27_map // brushless27
202 
207 typedef struct
208 {
209  // Output pins
210  digital_out_t rst;
212  // Input pins
213  digital_in_t sen;
214  digital_in_t int_pin;
216  // Modules
217  i2c_master_t i2c;
219  // I2C slave address
220  uint8_t slave_address;
222 } brushless27_t;
223 
228 typedef struct
229 {
230  pin_name_t scl;
231  pin_name_t sda;
233  pin_name_t sen;
234  pin_name_t rst;
235  pin_name_t int_pin;
237  uint32_t i2c_speed;
238  uint8_t i2c_address;
241 
246 typedef enum
247 {
249  BRUSHLESS27_ERROR = -1
250 
252 
269 
284 
298 
313 err_t brushless27_generic_write ( brushless27_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
314 
329 err_t brushless27_generic_read ( brushless27_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
330 
341 
352 
362 
374 
388 err_t brushless27_write_reg ( brushless27_t *ctx, uint8_t reg, uint8_t data_in );
389 
402 err_t brushless27_read_reg ( brushless27_t *ctx, uint8_t reg, uint8_t *data_out );
403 
416 err_t brushless27_set_pins ( brushless27_t *ctx, uint8_t set_mask, uint8_t clr_mask );
417 
429 err_t brushless27_set_trap_com_state ( brushless27_t *ctx, uint8_t state );
430 
444 err_t brushless27_toggle_pin ( brushless27_t *ctx, uint8_t pin_mask, uint8_t speed );
445 
459 err_t brushless27_perform_com_seq ( brushless27_t *ctx, uint8_t dir, uint8_t speed );
460 
475 err_t brushless27_drive_motor ( brushless27_t *ctx, uint8_t dir, uint8_t speed, uint32_t time_ms );
476 
477 #ifdef __cplusplus
478 }
479 #endif
480 #endif // BRUSHLESS27_H
481  // brushless27
483 
484 // ------------------------------------------------------------------------ END
brushless27_init
err_t brushless27_init(brushless27_t *ctx, brushless27_cfg_t *cfg)
Brushless 27 initialization function.
brushless27_cfg_t::scl
pin_name_t scl
Definition: brushless27.h:230
brushless27_default_cfg
err_t brushless27_default_cfg(brushless27_t *ctx)
Brushless 27 default configuration function.
brushless27_get_flt_pin
err_t brushless27_get_flt_pin(brushless27_t *ctx)
Brushless 27 get Fault pin function.
brushless27_t::slave_address
uint8_t slave_address
Definition: brushless27.h:220
brushless27_t::sen
digital_in_t sen
Definition: brushless27.h:213
brushless27_t::int_pin
digital_in_t int_pin
Definition: brushless27.h:214
brushless27_cfg_t::i2c_address
uint8_t i2c_address
Definition: brushless27.h:238
brushless27_cfg_t::sen
pin_name_t sen
Definition: brushless27.h:233
brushless27_read_reg
err_t brushless27_read_reg(brushless27_t *ctx, uint8_t reg, uint8_t *data_out)
Brushless 27 read register function.
BRUSHLESS27_ERROR
@ BRUSHLESS27_ERROR
Definition: brushless27.h:249
brushless27_cfg_t
Brushless 27 Click configuration object.
Definition: brushless27.h:229
brushless27_get_sen_pin
uint8_t brushless27_get_sen_pin(brushless27_t *ctx)
Brushless 27 get Sense pin function.
brushless27_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: brushless27.h:237
brushless27_reset_port_exp
void brushless27_reset_port_exp(brushless27_t *ctx)
Brushless 27 reset port expander function.
brushless27_cfg_t::rst
pin_name_t rst
Definition: brushless27.h:234
brushless27_drive_motor
err_t brushless27_drive_motor(brushless27_t *ctx, uint8_t dir, uint8_t speed, uint32_t time_ms)
Brushless 27 drive motor function.
brushless27_perform_com_seq
err_t brushless27_perform_com_seq(brushless27_t *ctx, uint8_t dir, uint8_t speed)
Brushless 27 perform com sequence function.
brushless27_generic_read
err_t brushless27_generic_read(brushless27_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Brushless 27 I2C reading function.
BRUSHLESS27_OK
@ BRUSHLESS27_OK
Definition: brushless27.h:248
brushless27_set_pins
err_t brushless27_set_pins(brushless27_t *ctx, uint8_t set_mask, uint8_t clr_mask)
Brushless 27 set pins function.
brushless27_return_value_t
brushless27_return_value_t
Brushless 27 Click return value data.
Definition: brushless27.h:247
brushless27_generic_write
err_t brushless27_generic_write(brushless27_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Brushless 27 I2C writing function.
brushless27_t::i2c
i2c_master_t i2c
Definition: brushless27.h:217
brushless27_write_reg
err_t brushless27_write_reg(brushless27_t *ctx, uint8_t reg, uint8_t data_in)
Brushless 27 write register function.
brushless27_get_int_pin
uint8_t brushless27_get_int_pin(brushless27_t *ctx)
Brushless 27 get Interrupt pin function.
brushless27_t::rst
digital_out_t rst
Definition: brushless27.h:210
brushless27_cfg_t::sda
pin_name_t sda
Definition: brushless27.h:231
brushless27_cfg_t::int_pin
pin_name_t int_pin
Definition: brushless27.h:235
brushless27_cfg_setup
void brushless27_cfg_setup(brushless27_cfg_t *cfg)
Brushless 27 configuration object setup function.
brushless27_set_trap_com_state
err_t brushless27_set_trap_com_state(brushless27_t *ctx, uint8_t state)
Brushless 27 set trapezoidal com state function.
brushless27_t
Brushless 27 Click context object.
Definition: brushless27.h:208
brushless27_toggle_pin
err_t brushless27_toggle_pin(brushless27_t *ctx, uint8_t pin_mask, uint8_t speed)
Brushless 27 toggle pin function.