dcmotor22  2.0.0.0
dcmotor22.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 DCMOTOR22_H
29 #define DCMOTOR22_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_uart.h"
38 #include "drv_i2c_master.h"
39 
60 #define DCMOTOR22_REG_GCONF 0x00
61 #define DCMOTOR22_REG_GSTAT 0x01
62 #define DCMOTOR22_REG_IFCNT 0x02
63 #define DCMOTOR22_REG_SLAVECONF 0x03
64 #define DCMOTOR22_REG_IOIN 0x06
65 #define DCMOTOR22_REG_CURRENT_LIMIT 0x10
66 #define DCMOTOR22_REG_PWM_AB 0x22
67 #define DCMOTOR22_REG_CHOPCONF 0x6C
68 #define DCMOTOR22_REG_DRV_STATUS 0x6F
69 #define DCMOTOR22_REG_PWMCONF 0x70
70 
77 #define DCMOTOR22_SYNC_BYTE 0x05
78 #define DCMOTOR22_READ_WRITE_BIT 0x80
79 #define DCMOTOR22_RETRY_SEND_TIMEOUT 100
80 #define DCMOTOR22_READ_TIMEOUT 1000
81 
86 #define DCMOTOR22_GCONF_PWM_DIRECT ( 0x01ul )
87 #define DCMOTOR22_GCONF_EXTCAP_AVAILABLE ( 0x02ul )
88 #define DCMOTOR22_GCONF_PAR_MODE_DUAL_MOTOR ( 0x00ul )
89 #define DCMOTOR22_GCONF_PAR_MODE_SINGLE_MOTOR ( 0x04ul )
90 #define DCMOTOR22_GCONF_TEST_MODE ( 0x08ul )
91 
96 #define DCMOTOR22_GSTAT_RESET ( 0x01ul )
97 #define DCMOTOR22_GSTAT_DRV_ERR ( 0x02ul )
98 #define DCMOTOR22_GSTAT_UNDER_3V5 ( 0x04ul )
99 #define DCMOTOR22_GSTAT_CLEAR_ALL ( 0x07ul )
100 
105 #define DCMOTOR22_SLAVECONF_SENDDELAY_8BIT ( 0x0000ul )
106 #define DCMOTOR22_SLAVECONF_SENDDELAY_24BIT ( 0x0200ul )
107 #define DCMOTOR22_SLAVECONF_SENDDELAY_40BIT ( 0x0400ul )
108 #define DCMOTOR22_SLAVECONF_SENDDELAY_56BIT ( 0x0600ul )
109 #define DCMOTOR22_SLAVECONF_SENDDELAY_72BIT ( 0x0800ul )
110 #define DCMOTOR22_SLAVECONF_SENDDELAY_88BIT ( 0x0A00ul )
111 #define DCMOTOR22_SLAVECONF_SENDDELAY_104BIT ( 0x0C00ul )
112 #define DCMOTOR22_SLAVECONF_SENDDELAY_120BIT ( 0x0E00ul )
113 
118 #define DCMOTOR22_IOIN_EN ( 0x01ul )
119 #define DCMOTOR22_IOIN_NSTDBY ( 0x02ul )
120 #define DCMOTOR22_IOIN_AD0 ( 0x04ul )
121 #define DCMOTOR22_IOIN_AD1 ( 0x08ul )
122 #define DCMOTOR22_IOIN_DIAG ( 0x10ul )
123 #define DCMOTOR22_IOIN_UART_ON ( 0x20ul )
124 #define DCMOTOR22_IOIN_UART_IN ( 0x40ul )
125 #define DCMOTOR22_IOIN_MODE_IN ( 0x80ul )
126 #define DCMOTOR22_IOIN_A2 ( 0x0100ul )
127 #define DCMOTOR22_IOIN_A1 ( 0x0200ul )
128 #define DCMOTOR22_IOIN_COMP_A1A2 ( 0x0400ul )
129 #define DCMOTOR22_IOIN_COMP_B1B2 ( 0x0800ul )
130 #define DCMOTOR22_IOIN_VERSION ( 0x40000000ul )
131 #define DCMOTOR22_IOIN_VERSION_MASK ( 0xFF000000ul )
132 
137 #define DCMOTOR22_CURRENT_LIMIT_EN_FREEWHEELING ( 0x01ul )
138 #define DCMOTOR22_CURRENT_LIMIT_IRUN_1_32 ( 0x0000ul )
139 #define DCMOTOR22_CURRENT_LIMIT_IRUN_2_32 ( 0x0100ul )
140 #define DCMOTOR22_CURRENT_LIMIT_IRUN_3_32 ( 0x0200ul )
141 #define DCMOTOR22_CURRENT_LIMIT_IRUN_4_32 ( 0x0300ul )
142 #define DCMOTOR22_CURRENT_LIMIT_IRUN_5_32 ( 0x0400ul )
143 #define DCMOTOR22_CURRENT_LIMIT_IRUN_6_32 ( 0x0500ul )
144 #define DCMOTOR22_CURRENT_LIMIT_IRUN_7_32 ( 0x0600ul )
145 #define DCMOTOR22_CURRENT_LIMIT_IRUN_8_32 ( 0x0700ul )
146 #define DCMOTOR22_CURRENT_LIMIT_IRUN_9_32 ( 0x0800ul )
147 #define DCMOTOR22_CURRENT_LIMIT_IRUN_10_32 ( 0x0900ul )
148 #define DCMOTOR22_CURRENT_LIMIT_IRUN_11_32 ( 0x0A00ul )
149 #define DCMOTOR22_CURRENT_LIMIT_IRUN_12_32 ( 0x0B00ul )
150 #define DCMOTOR22_CURRENT_LIMIT_IRUN_13_32 ( 0x0C00ul )
151 #define DCMOTOR22_CURRENT_LIMIT_IRUN_14_32 ( 0x0D00ul )
152 #define DCMOTOR22_CURRENT_LIMIT_IRUN_15_32 ( 0x0E00ul )
153 #define DCMOTOR22_CURRENT_LIMIT_IRUN_16_32 ( 0x0F00ul )
154 #define DCMOTOR22_CURRENT_LIMIT_IRUN_17_32 ( 0x1000ul )
155 #define DCMOTOR22_CURRENT_LIMIT_IRUN_18_32 ( 0x1100ul )
156 #define DCMOTOR22_CURRENT_LIMIT_IRUN_19_32 ( 0x1200ul )
157 #define DCMOTOR22_CURRENT_LIMIT_IRUN_20_32 ( 0x1300ul )
158 #define DCMOTOR22_CURRENT_LIMIT_IRUN_21_32 ( 0x1400ul )
159 #define DCMOTOR22_CURRENT_LIMIT_IRUN_22_32 ( 0x1500ul )
160 #define DCMOTOR22_CURRENT_LIMIT_IRUN_23_32 ( 0x1600ul )
161 #define DCMOTOR22_CURRENT_LIMIT_IRUN_24_32 ( 0x1700ul )
162 #define DCMOTOR22_CURRENT_LIMIT_IRUN_25_32 ( 0x1800ul )
163 #define DCMOTOR22_CURRENT_LIMIT_IRUN_26_32 ( 0x1900ul )
164 #define DCMOTOR22_CURRENT_LIMIT_IRUN_27_32 ( 0x1A00ul )
165 #define DCMOTOR22_CURRENT_LIMIT_IRUN_28_32 ( 0x1B00ul )
166 #define DCMOTOR22_CURRENT_LIMIT_IRUN_29_32 ( 0x1C00ul )
167 #define DCMOTOR22_CURRENT_LIMIT_IRUN_30_32 ( 0x1D00ul )
168 #define DCMOTOR22_CURRENT_LIMIT_IRUN_31_32 ( 0x1E00ul )
169 #define DCMOTOR22_CURRENT_LIMIT_IRUN_32_32 ( 0x1F00ul )
170 
175 #define DCMOTOR22_MAX_PWM 255
176 #define DCMOTOR22_MIN_PWM ( -255 )
177 #define DCMOTOR22_PWM_RES 0x01FF
178 #define DCMOTOR22_PWM_DUTY_0 ( 0x00ul )
179 
184 #define DCMOTOR22_CHOPCONF_DISS2VS ( 0x80000000ul )
185 #define DCMOTOR22_CHOPCONF_DISS2G ( 0x40000000ul )
186 #define DCMOTOR22_CHOPCONF_TBL_0 ( 0x00000000ul )
187 #define DCMOTOR22_CHOPCONF_TBL_1 ( 0x00008000ul )
188 #define DCMOTOR22_CHOPCONF_TBL_2 ( 0x00010000ul )
189 #define DCMOTOR22_CHOPCONF_TBL_3 ( 0x00018000ul )
190 #define DCMOTOR22_CHOPCONF_ENABLEDRV ( 0x00000001ul )
191 
196 #define DCMOTOR22_PWMCONF_FREEWHEEL_NORMAL_OP ( 0x00000000ul )
197 #define DCMOTOR22_PWMCONF_FREEWHEEL_FREEWHEELING ( 0x00100000ul )
198 #define DCMOTOR22_PWMCONF_FREEWHEEL_LS_SHORTED ( 0x00200000ul )
199 #define DCMOTOR22_PWMCONF_FREEWHEEL_HS_SHORTED ( 0x00300000ul )
200 #define DCMOTOR22_PWMCONF_PWM_FREQ_2_1024 ( 0x00000000ul )
201 #define DCMOTOR22_PWMCONF_PWM_FREQ_2_683 ( 0x00010000ul )
202 #define DCMOTOR22_PWMCONF_PWM_FREQ_2_512 ( 0x00020000ul )
203 #define DCMOTOR22_PWMCONF_PWM_FREQ_2_410 ( 0x00030000ul )
204 
209 #define DCMOTOR22_MOTOR_A 0x00
210 #define DCMOTOR22_MOTOR_B 0x01
211 
216 #define DCMOTOR22_NUM_CONVERSIONS 100
217 #define DCMOTOR22_RSENSE 0.1
218 
223 #define DCMOTOR22_ADC_REG_SETUP 0x80
224 #define DCMOTOR22_ADC_SETUP_VREF_VDD_REF_NC_OFF 0x00
225 #define DCMOTOR22_ADC_SETUP_VREF_EXT_REF_IN_OFF 0x20
226 #define DCMOTOR22_ADC_SETUP_VREF_INT_REF_NC_OFF 0x40
227 #define DCMOTOR22_ADC_SETUP_VREF_INT_REF_NC_ON 0x50
228 #define DCMOTOR22_ADC_SETUP_VREF_INT_REF_OUT_OFF 0x60
229 #define DCMOTOR22_ADC_SETUP_VREF_INT_REF_OUT_ON 0x70
230 #define DCMOTOR22_ADC_SETUP_CLK_INT 0x00
231 #define DCMOTOR22_ADC_SETUP_CLK_EXT 0x08
232 #define DCMOTOR22_ADC_SETUP_UNIPOLAR 0x00
233 #define DCMOTOR22_ADC_SETUP_BIPOLAR 0x04
234 #define DCMOTOR22_ADC_SETUP_RST_RESET 0x00
235 #define DCMOTOR22_ADC_SETUP_RST_NO_ACTION 0x02
236 
241 #define DCMOTOR22_ADC_REG_CONFIG 0x00
242 #define DCMOTOR22_ADC_CONFIG_SCAN_AIN0_TO_CS0 0x00
243 #define DCMOTOR22_ADC_CONFIG_SCAN_CS0_8_TIMES 0x20
244 #define DCMOTOR22_ADC_CONFIG_SCAN_CS0 0x60
245 #define DCMOTOR22_ADC_CONFIG_CS0_AIN0 0x00
246 #define DCMOTOR22_ADC_CONFIG_CS0_AIN1 0x02
247 #define DCMOTOR22_ADC_CONFIG_DIFFERENTIAL 0x00
248 #define DCMOTOR22_ADC_CONFIG_SINGLE_ENDED 0x01
249 
254 #define DCMOTOR22_ADC_REG_BIT_MASK 0x80
255 #define DCMOTOR22_ADC_SETUP_VREF_BIT_MASK 0x70
256 #define DCMOTOR22_ADC_SETUP_CLK_BIT_MASK 0x08
257 #define DCMOTOR22_ADC_SETUP_UNI_BIP_BIT_MASK 0x04
258 #define DCMOTOR22_ADC_SETUP_RST_BIT_MASK 0x02
259 #define DCMOTOR22_ADC_CONFIG_SCAN_BIT_MASK 0x60
260 #define DCMOTOR22_ADC_CONFIG_CS0_BIT_MASK 0x02
261 #define DCMOTOR22_ADC_CONFIG_SGL_DIF_BIT_MASK 0x01
262 
267 #define DCMOTOR22_ADC_CHANNEL_0 0
268 #define DCMOTOR22_ADC_CHANNEL_1 1
269 
274 #define DCMOTOR22_ADC_RESOLUTION 0x0FFF
275 #define DCMOTOR22_ADC_VREF 2.048
276 
282 #define DCMOTOR22_ADC_DEVICE_ADDRESS 0x36
283 #define DCMOTOR22_UART_DEVICE_ADDRESS_0 0x00
284 #define DCMOTOR22_UART_DEVICE_ADDRESS_1 0x01
285 #define DCMOTOR22_UART_DEVICE_ADDRESS_2 0x02
286 #define DCMOTOR22_UART_DEVICE_ADDRESS_3 0x03
287 #define DCMOTOR22_UART_DEVICE_ADDRESS_MASK 0x03
288 
294 #define DRV_BUFFER_SIZE 200
295  // dcmotor22_cmd
297 
312 #define DCMOTOR22_MAP_MIKROBUS( cfg, mikrobus ) \
313  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
314  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
315  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
316  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
317  cfg.a0 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
318  cfg.a1 = MIKROBUS( mikrobus, MIKROBUS_CS ); \
319  cfg.en = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
320  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
321  // dcmotor22_map // dcmotor22
324 
329 typedef struct
330 {
331  // Output pins
332  digital_out_t a0;
333  digital_out_t a1;
334  digital_out_t en;
336  // Input pins
337  digital_in_t int_pin;
339  // Modules
340  i2c_master_t i2c;
341  uart_t uart;
346  char uart_rx_buffer[ DRV_BUFFER_SIZE ];
347  char uart_tx_buffer[ DRV_BUFFER_SIZE ];
349  uint8_t adc_setup_byte;
350  uint8_t adc_config_byte;
351  uint32_t pwm_ab;
353 } dcmotor22_t;
354 
359 typedef struct
360 {
361  // Communication gpio pins
362  pin_name_t rx_pin;
363  pin_name_t tx_pin;
364  pin_name_t scl;
365  pin_name_t sda;
367  // Additional gpio pins
368  pin_name_t a0;
369  pin_name_t a1;
370  pin_name_t en;
371  pin_name_t int_pin;
373  // Static variable
374  uint32_t baud_rate;
376  uart_data_bits_t data_bit;
377  uart_parity_t parity_bit;
378  uart_stop_bits_t stop_bit;
380  uint32_t i2c_speed;
381  uint8_t i2c_address;
384 
389 typedef enum
390 {
392  DCMOTOR22_ERROR = -1
393 
395 
412 
427 
441 
454 err_t dcmotor22_write_register ( dcmotor22_t *ctx, uint8_t reg, uint32_t data_in );
455 
468 err_t dcmotor22_read_register ( dcmotor22_t *ctx, uint8_t reg, uint32_t *data_out );
469 
481 err_t dcmotor22_adc_write_setup_byte ( dcmotor22_t *ctx, uint8_t setup_byte );
482 
494 err_t dcmotor22_adc_write_config_byte ( dcmotor22_t *ctx, uint8_t config_byte );
495 
507 err_t dcmotor22_adc_set_channel ( dcmotor22_t *ctx, uint8_t channel );
508 
521 err_t dcmotor22_adc_get_voltage ( dcmotor22_t *ctx, float *voltage );
522 
536 err_t dcmotor22_get_motor_current ( dcmotor22_t *ctx, uint8_t motor_sel, float *current );
537 
551 err_t dcmotor22_set_motor_pwm ( dcmotor22_t *ctx, uint8_t motor_sel, int16_t pwm_duty );
552 
562 
572 
582 
592 void dcmotor22_set_device_address ( dcmotor22_t *ctx, uint8_t addr );
593 
603 
604 #ifdef __cplusplus
605 }
606 #endif
607 #endif // DCMOTOR22_H
608  // dcmotor22
610 
611 // ------------------------------------------------------------------------ END
dcmotor22_return_value_t
dcmotor22_return_value_t
DC Motor 22 Click return value data.
Definition: dcmotor22.h:390
dcmotor22_t::adc_config_byte
uint8_t adc_config_byte
Definition: dcmotor22.h:350
dcmotor22_adc_write_setup_byte
err_t dcmotor22_adc_write_setup_byte(dcmotor22_t *ctx, uint8_t setup_byte)
DC Motor 22 ADC write setup byte function.
DCMOTOR22_OK
@ DCMOTOR22_OK
Definition: dcmotor22.h:391
dcmotor22_cfg_t::scl
pin_name_t scl
Definition: dcmotor22.h:364
dcmotor22_cfg_t::rx_pin
pin_name_t rx_pin
Definition: dcmotor22.h:362
dcmotor22_t::a1
digital_out_t a1
Definition: dcmotor22.h:333
DRV_BUFFER_SIZE
#define DRV_BUFFER_SIZE
DC Motor 22 driver buffer size.
Definition: dcmotor22.h:294
dcmotor22_default_cfg
err_t dcmotor22_default_cfg(dcmotor22_t *ctx)
DC Motor 22 default configuration function.
dcmotor22_t::i2c_slave_address
uint8_t i2c_slave_address
Definition: dcmotor22.h:344
dcmotor22_cfg_t::en
pin_name_t en
Definition: dcmotor22.h:370
dcmotor22_get_motor_current
err_t dcmotor22_get_motor_current(dcmotor22_t *ctx, uint8_t motor_sel, float *current)
DC Motor 22 get motor current function.
dcmotor22_cfg_setup
void dcmotor22_cfg_setup(dcmotor22_cfg_t *cfg)
DC Motor 22 configuration object setup function.
dcmotor22_t::a0
digital_out_t a0
Definition: dcmotor22.h:332
dcmotor22_t::int_pin
digital_in_t int_pin
Definition: dcmotor22.h:337
dcmotor22_cfg_t::sda
pin_name_t sda
Definition: dcmotor22.h:365
dcmotor22_disable_device
void dcmotor22_disable_device(dcmotor22_t *ctx)
DC Motor 22 disable device function.
dcmotor22_adc_set_channel
err_t dcmotor22_adc_set_channel(dcmotor22_t *ctx, uint8_t channel)
DC Motor 22 ADC set channel function.
dcmotor22_t::adc_setup_byte
uint8_t adc_setup_byte
Definition: dcmotor22.h:349
dcmotor22_cfg_t::a1
pin_name_t a1
Definition: dcmotor22.h:369
dcmotor22_t::i2c
i2c_master_t i2c
Definition: dcmotor22.h:340
dcmotor22_t
DC Motor 22 Click context object.
Definition: dcmotor22.h:330
dcmotor22_t::uart
uart_t uart
Definition: dcmotor22.h:341
dcmotor22_write_register
err_t dcmotor22_write_register(dcmotor22_t *ctx, uint8_t reg, uint32_t data_in)
DC Motor 22 write register function.
dcmotor22_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: dcmotor22.h:376
dcmotor22_adc_write_config_byte
err_t dcmotor22_adc_write_config_byte(dcmotor22_t *ctx, uint8_t config_byte)
DC Motor 22 ADC write config byte function.
dcmotor22_cfg_t::tx_pin
pin_name_t tx_pin
Definition: dcmotor22.h:363
dcmotor22_init
err_t dcmotor22_init(dcmotor22_t *ctx, dcmotor22_cfg_t *cfg)
DC Motor 22 initialization function.
dcmotor22_set_motor_pwm
err_t dcmotor22_set_motor_pwm(dcmotor22_t *ctx, uint8_t motor_sel, int16_t pwm_duty)
DC Motor 22 set motor pwm function.
dcmotor22_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: dcmotor22.h:380
dcmotor22_get_int_pin
uint8_t dcmotor22_get_int_pin(dcmotor22_t *ctx)
DC Motor 22 get int pin function.
dcmotor22_t::en
digital_out_t en
Definition: dcmotor22.h:334
dcmotor22_cfg_t::baud_rate
uint32_t baud_rate
Definition: dcmotor22.h:374
DCMOTOR22_ERROR
@ DCMOTOR22_ERROR
Definition: dcmotor22.h:392
dcmotor22_read_register
err_t dcmotor22_read_register(dcmotor22_t *ctx, uint8_t reg, uint32_t *data_out)
DC Motor 22 read register function.
dcmotor22_cfg_t::int_pin
pin_name_t int_pin
Definition: dcmotor22.h:371
dcmotor22_cfg_t::a0
pin_name_t a0
Definition: dcmotor22.h:368
dcmotor22_t::uart_device_address
uint8_t uart_device_address
Definition: dcmotor22.h:343
dcmotor22_adc_get_voltage
err_t dcmotor22_adc_get_voltage(dcmotor22_t *ctx, float *voltage)
DC Motor 22 ADC get voltage function.
dcmotor22_cfg_t::uart_blocking
bool uart_blocking
Definition: dcmotor22.h:375
dcmotor22_enable_device
void dcmotor22_enable_device(dcmotor22_t *ctx)
DC Motor 22 enable device function.
dcmotor22_reset_device
void dcmotor22_reset_device(dcmotor22_t *ctx)
DC Motor 22 reset device function.
dcmotor22_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: dcmotor22.h:378
dcmotor22_cfg_t
DC Motor 22 Click configuration object.
Definition: dcmotor22.h:360
dcmotor22_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: dcmotor22.h:377
dcmotor22_set_device_address
void dcmotor22_set_device_address(dcmotor22_t *ctx, uint8_t addr)
DC Motor 22 set device address function.
dcmotor22_cfg_t::i2c_address
uint8_t i2c_address
Definition: dcmotor22.h:381
dcmotor22_t::pwm_ab
uint32_t pwm_ab
Definition: dcmotor22.h:351