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
32extern "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_uart.h"
52#include "drv_i2c_master.h"
53
74#define DCMOTOR22_REG_GCONF 0x00
75#define DCMOTOR22_REG_GSTAT 0x01
76#define DCMOTOR22_REG_IFCNT 0x02
77#define DCMOTOR22_REG_SLAVECONF 0x03
78#define DCMOTOR22_REG_IOIN 0x06
79#define DCMOTOR22_REG_CURRENT_LIMIT 0x10
80#define DCMOTOR22_REG_PWM_AB 0x22
81#define DCMOTOR22_REG_CHOPCONF 0x6C
82#define DCMOTOR22_REG_DRV_STATUS 0x6F
83#define DCMOTOR22_REG_PWMCONF 0x70
84
91#define DCMOTOR22_SYNC_BYTE 0x05
92#define DCMOTOR22_READ_WRITE_BIT 0x80
93#define DCMOTOR22_RETRY_SEND_TIMEOUT 100
94#define DCMOTOR22_READ_TIMEOUT 1000
95
100#define DCMOTOR22_GCONF_PWM_DIRECT ( 0x01ul )
101#define DCMOTOR22_GCONF_EXTCAP_AVAILABLE ( 0x02ul )
102#define DCMOTOR22_GCONF_PAR_MODE_DUAL_MOTOR ( 0x00ul )
103#define DCMOTOR22_GCONF_PAR_MODE_SINGLE_MOTOR ( 0x04ul )
104#define DCMOTOR22_GCONF_TEST_MODE ( 0x08ul )
105
110#define DCMOTOR22_GSTAT_RESET ( 0x01ul )
111#define DCMOTOR22_GSTAT_DRV_ERR ( 0x02ul )
112#define DCMOTOR22_GSTAT_UNDER_3V5 ( 0x04ul )
113#define DCMOTOR22_GSTAT_CLEAR_ALL ( 0x07ul )
114
119#define DCMOTOR22_SLAVECONF_SENDDELAY_8BIT ( 0x0000ul )
120#define DCMOTOR22_SLAVECONF_SENDDELAY_24BIT ( 0x0200ul )
121#define DCMOTOR22_SLAVECONF_SENDDELAY_40BIT ( 0x0400ul )
122#define DCMOTOR22_SLAVECONF_SENDDELAY_56BIT ( 0x0600ul )
123#define DCMOTOR22_SLAVECONF_SENDDELAY_72BIT ( 0x0800ul )
124#define DCMOTOR22_SLAVECONF_SENDDELAY_88BIT ( 0x0A00ul )
125#define DCMOTOR22_SLAVECONF_SENDDELAY_104BIT ( 0x0C00ul )
126#define DCMOTOR22_SLAVECONF_SENDDELAY_120BIT ( 0x0E00ul )
127
132#define DCMOTOR22_IOIN_EN ( 0x01ul )
133#define DCMOTOR22_IOIN_NSTDBY ( 0x02ul )
134#define DCMOTOR22_IOIN_AD0 ( 0x04ul )
135#define DCMOTOR22_IOIN_AD1 ( 0x08ul )
136#define DCMOTOR22_IOIN_DIAG ( 0x10ul )
137#define DCMOTOR22_IOIN_UART_ON ( 0x20ul )
138#define DCMOTOR22_IOIN_UART_IN ( 0x40ul )
139#define DCMOTOR22_IOIN_MODE_IN ( 0x80ul )
140#define DCMOTOR22_IOIN_A2 ( 0x0100ul )
141#define DCMOTOR22_IOIN_A1 ( 0x0200ul )
142#define DCMOTOR22_IOIN_COMP_A1A2 ( 0x0400ul )
143#define DCMOTOR22_IOIN_COMP_B1B2 ( 0x0800ul )
144#define DCMOTOR22_IOIN_VERSION ( 0x40000000ul )
145#define DCMOTOR22_IOIN_VERSION_MASK ( 0xFF000000ul )
146
151#define DCMOTOR22_CURRENT_LIMIT_EN_FREEWHEELING ( 0x01ul )
152#define DCMOTOR22_CURRENT_LIMIT_IRUN_1_32 ( 0x0000ul )
153#define DCMOTOR22_CURRENT_LIMIT_IRUN_2_32 ( 0x0100ul )
154#define DCMOTOR22_CURRENT_LIMIT_IRUN_3_32 ( 0x0200ul )
155#define DCMOTOR22_CURRENT_LIMIT_IRUN_4_32 ( 0x0300ul )
156#define DCMOTOR22_CURRENT_LIMIT_IRUN_5_32 ( 0x0400ul )
157#define DCMOTOR22_CURRENT_LIMIT_IRUN_6_32 ( 0x0500ul )
158#define DCMOTOR22_CURRENT_LIMIT_IRUN_7_32 ( 0x0600ul )
159#define DCMOTOR22_CURRENT_LIMIT_IRUN_8_32 ( 0x0700ul )
160#define DCMOTOR22_CURRENT_LIMIT_IRUN_9_32 ( 0x0800ul )
161#define DCMOTOR22_CURRENT_LIMIT_IRUN_10_32 ( 0x0900ul )
162#define DCMOTOR22_CURRENT_LIMIT_IRUN_11_32 ( 0x0A00ul )
163#define DCMOTOR22_CURRENT_LIMIT_IRUN_12_32 ( 0x0B00ul )
164#define DCMOTOR22_CURRENT_LIMIT_IRUN_13_32 ( 0x0C00ul )
165#define DCMOTOR22_CURRENT_LIMIT_IRUN_14_32 ( 0x0D00ul )
166#define DCMOTOR22_CURRENT_LIMIT_IRUN_15_32 ( 0x0E00ul )
167#define DCMOTOR22_CURRENT_LIMIT_IRUN_16_32 ( 0x0F00ul )
168#define DCMOTOR22_CURRENT_LIMIT_IRUN_17_32 ( 0x1000ul )
169#define DCMOTOR22_CURRENT_LIMIT_IRUN_18_32 ( 0x1100ul )
170#define DCMOTOR22_CURRENT_LIMIT_IRUN_19_32 ( 0x1200ul )
171#define DCMOTOR22_CURRENT_LIMIT_IRUN_20_32 ( 0x1300ul )
172#define DCMOTOR22_CURRENT_LIMIT_IRUN_21_32 ( 0x1400ul )
173#define DCMOTOR22_CURRENT_LIMIT_IRUN_22_32 ( 0x1500ul )
174#define DCMOTOR22_CURRENT_LIMIT_IRUN_23_32 ( 0x1600ul )
175#define DCMOTOR22_CURRENT_LIMIT_IRUN_24_32 ( 0x1700ul )
176#define DCMOTOR22_CURRENT_LIMIT_IRUN_25_32 ( 0x1800ul )
177#define DCMOTOR22_CURRENT_LIMIT_IRUN_26_32 ( 0x1900ul )
178#define DCMOTOR22_CURRENT_LIMIT_IRUN_27_32 ( 0x1A00ul )
179#define DCMOTOR22_CURRENT_LIMIT_IRUN_28_32 ( 0x1B00ul )
180#define DCMOTOR22_CURRENT_LIMIT_IRUN_29_32 ( 0x1C00ul )
181#define DCMOTOR22_CURRENT_LIMIT_IRUN_30_32 ( 0x1D00ul )
182#define DCMOTOR22_CURRENT_LIMIT_IRUN_31_32 ( 0x1E00ul )
183#define DCMOTOR22_CURRENT_LIMIT_IRUN_32_32 ( 0x1F00ul )
184
189#define DCMOTOR22_MAX_PWM 255
190#define DCMOTOR22_MIN_PWM ( -255 )
191#define DCMOTOR22_PWM_RES 0x01FF
192#define DCMOTOR22_PWM_DUTY_0 ( 0x00ul )
193
198#define DCMOTOR22_CHOPCONF_DISS2VS ( 0x80000000ul )
199#define DCMOTOR22_CHOPCONF_DISS2G ( 0x40000000ul )
200#define DCMOTOR22_CHOPCONF_TBL_0 ( 0x00000000ul )
201#define DCMOTOR22_CHOPCONF_TBL_1 ( 0x00008000ul )
202#define DCMOTOR22_CHOPCONF_TBL_2 ( 0x00010000ul )
203#define DCMOTOR22_CHOPCONF_TBL_3 ( 0x00018000ul )
204#define DCMOTOR22_CHOPCONF_ENABLEDRV ( 0x00000001ul )
205
210#define DCMOTOR22_PWMCONF_FREEWHEEL_NORMAL_OP ( 0x00000000ul )
211#define DCMOTOR22_PWMCONF_FREEWHEEL_FREEWHEELING ( 0x00100000ul )
212#define DCMOTOR22_PWMCONF_FREEWHEEL_LS_SHORTED ( 0x00200000ul )
213#define DCMOTOR22_PWMCONF_FREEWHEEL_HS_SHORTED ( 0x00300000ul )
214#define DCMOTOR22_PWMCONF_PWM_FREQ_2_1024 ( 0x00000000ul )
215#define DCMOTOR22_PWMCONF_PWM_FREQ_2_683 ( 0x00010000ul )
216#define DCMOTOR22_PWMCONF_PWM_FREQ_2_512 ( 0x00020000ul )
217#define DCMOTOR22_PWMCONF_PWM_FREQ_2_410 ( 0x00030000ul )
218
223#define DCMOTOR22_MOTOR_A 0x00
224#define DCMOTOR22_MOTOR_B 0x01
225
230#define DCMOTOR22_NUM_CONVERSIONS 100
231#define DCMOTOR22_RSENSE 0.1
232
237#define DCMOTOR22_ADC_REG_SETUP 0x80
238#define DCMOTOR22_ADC_SETUP_VREF_VDD_REF_NC_OFF 0x00
239#define DCMOTOR22_ADC_SETUP_VREF_EXT_REF_IN_OFF 0x20
240#define DCMOTOR22_ADC_SETUP_VREF_INT_REF_NC_OFF 0x40
241#define DCMOTOR22_ADC_SETUP_VREF_INT_REF_NC_ON 0x50
242#define DCMOTOR22_ADC_SETUP_VREF_INT_REF_OUT_OFF 0x60
243#define DCMOTOR22_ADC_SETUP_VREF_INT_REF_OUT_ON 0x70
244#define DCMOTOR22_ADC_SETUP_CLK_INT 0x00
245#define DCMOTOR22_ADC_SETUP_CLK_EXT 0x08
246#define DCMOTOR22_ADC_SETUP_UNIPOLAR 0x00
247#define DCMOTOR22_ADC_SETUP_BIPOLAR 0x04
248#define DCMOTOR22_ADC_SETUP_RST_RESET 0x00
249#define DCMOTOR22_ADC_SETUP_RST_NO_ACTION 0x02
250
255#define DCMOTOR22_ADC_REG_CONFIG 0x00
256#define DCMOTOR22_ADC_CONFIG_SCAN_AIN0_TO_CS0 0x00
257#define DCMOTOR22_ADC_CONFIG_SCAN_CS0_8_TIMES 0x20
258#define DCMOTOR22_ADC_CONFIG_SCAN_CS0 0x60
259#define DCMOTOR22_ADC_CONFIG_CS0_AIN0 0x00
260#define DCMOTOR22_ADC_CONFIG_CS0_AIN1 0x02
261#define DCMOTOR22_ADC_CONFIG_DIFFERENTIAL 0x00
262#define DCMOTOR22_ADC_CONFIG_SINGLE_ENDED 0x01
263
268#define DCMOTOR22_ADC_REG_BIT_MASK 0x80
269#define DCMOTOR22_ADC_SETUP_VREF_BIT_MASK 0x70
270#define DCMOTOR22_ADC_SETUP_CLK_BIT_MASK 0x08
271#define DCMOTOR22_ADC_SETUP_UNI_BIP_BIT_MASK 0x04
272#define DCMOTOR22_ADC_SETUP_RST_BIT_MASK 0x02
273#define DCMOTOR22_ADC_CONFIG_SCAN_BIT_MASK 0x60
274#define DCMOTOR22_ADC_CONFIG_CS0_BIT_MASK 0x02
275#define DCMOTOR22_ADC_CONFIG_SGL_DIF_BIT_MASK 0x01
276
281#define DCMOTOR22_ADC_CHANNEL_0 0
282#define DCMOTOR22_ADC_CHANNEL_1 1
283
288#define DCMOTOR22_ADC_RESOLUTION 0x0FFF
289#define DCMOTOR22_ADC_VREF 2.048
290
296#define DCMOTOR22_ADC_DEVICE_ADDRESS 0x36
297#define DCMOTOR22_UART_DEVICE_ADDRESS_0 0x00
298#define DCMOTOR22_UART_DEVICE_ADDRESS_1 0x01
299#define DCMOTOR22_UART_DEVICE_ADDRESS_2 0x02
300#define DCMOTOR22_UART_DEVICE_ADDRESS_3 0x03
301#define DCMOTOR22_UART_DEVICE_ADDRESS_MASK 0x03
302
308#define DRV_BUFFER_SIZE 200
309
310 // dcmotor22_cmd
311
326#define DCMOTOR22_MAP_MIKROBUS( cfg, mikrobus ) \
327 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
328 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
329 cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
330 cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
331 cfg.a0 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
332 cfg.a1 = MIKROBUS( mikrobus, MIKROBUS_CS ); \
333 cfg.en = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
334 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
335
336 // dcmotor22_map
337 // dcmotor22
338
343typedef struct
344{
345 // Output pins
346 digital_out_t a0;
347 digital_out_t a1;
348 digital_out_t en;
350 // Input pins
351 digital_in_t int_pin;
353 // Modules
354 i2c_master_t i2c;
355 uart_t uart;
360 char uart_rx_buffer[ DRV_BUFFER_SIZE ];
361 char uart_tx_buffer[ DRV_BUFFER_SIZE ];
365 uint32_t pwm_ab;
368
373typedef struct
374{
375 // Communication gpio pins
376 pin_name_t rx_pin;
377 pin_name_t tx_pin;
378 pin_name_t scl;
379 pin_name_t sda;
381 // Additional gpio pins
382 pin_name_t a0;
383 pin_name_t a1;
384 pin_name_t en;
385 pin_name_t int_pin;
387 // Static variable
388 uint32_t baud_rate;
390 uart_data_bits_t data_bit;
391 uart_parity_t parity_bit;
392 uart_stop_bits_t stop_bit;
394 uint32_t i2c_speed;
395 uint8_t i2c_address;
398
409
426
441
455
468err_t dcmotor22_write_register ( dcmotor22_t *ctx, uint8_t reg, uint32_t data_in );
469
482err_t dcmotor22_read_register ( dcmotor22_t *ctx, uint8_t reg, uint32_t *data_out );
483
495err_t dcmotor22_adc_write_setup_byte ( dcmotor22_t *ctx, uint8_t setup_byte );
496
508err_t dcmotor22_adc_write_config_byte ( dcmotor22_t *ctx, uint8_t config_byte );
509
521err_t dcmotor22_adc_set_channel ( dcmotor22_t *ctx, uint8_t channel );
522
535err_t dcmotor22_adc_get_voltage ( dcmotor22_t *ctx, float *voltage );
536
550err_t dcmotor22_get_motor_current ( dcmotor22_t *ctx, uint8_t motor_sel, float *current );
551
565err_t dcmotor22_set_motor_pwm ( dcmotor22_t *ctx, uint8_t motor_sel, int16_t pwm_duty );
566
576
586
596
606void dcmotor22_set_device_address ( dcmotor22_t *ctx, uint8_t addr );
607
617
618#ifdef __cplusplus
619}
620#endif
621#endif // DCMOTOR22_H
622
623 // dcmotor22
624
625// ------------------------------------------------------------------------ END
dcmotor22_return_value_t
DC Motor 22 Click return value data.
Definition dcmotor22.h:404
@ DCMOTOR22_ERROR
Definition dcmotor22.h:406
@ DCMOTOR22_OK
Definition dcmotor22.h:405
#define DRV_BUFFER_SIZE
DC Motor 22 driver buffer size.
Definition dcmotor22.h:308
err_t dcmotor22_write_register(dcmotor22_t *ctx, uint8_t reg, uint32_t data_in)
DC Motor 22 write register function.
void dcmotor22_set_device_address(dcmotor22_t *ctx, uint8_t addr)
DC Motor 22 set device address function.
err_t dcmotor22_adc_write_config_byte(dcmotor22_t *ctx, uint8_t config_byte)
DC Motor 22 ADC write config byte function.
void dcmotor22_cfg_setup(dcmotor22_cfg_t *cfg)
DC Motor 22 configuration object setup function.
err_t dcmotor22_adc_set_channel(dcmotor22_t *ctx, uint8_t channel)
DC Motor 22 ADC set channel function.
void dcmotor22_reset_device(dcmotor22_t *ctx)
DC Motor 22 reset device function.
err_t dcmotor22_set_motor_pwm(dcmotor22_t *ctx, uint8_t motor_sel, int16_t pwm_duty)
DC Motor 22 set motor pwm function.
err_t dcmotor22_adc_write_setup_byte(dcmotor22_t *ctx, uint8_t setup_byte)
DC Motor 22 ADC write setup byte function.
err_t dcmotor22_default_cfg(dcmotor22_t *ctx)
DC Motor 22 default configuration function.
err_t dcmotor22_read_register(dcmotor22_t *ctx, uint8_t reg, uint32_t *data_out)
DC Motor 22 read register function.
err_t dcmotor22_get_motor_current(dcmotor22_t *ctx, uint8_t motor_sel, float *current)
DC Motor 22 get motor current function.
err_t dcmotor22_adc_get_voltage(dcmotor22_t *ctx, float *voltage)
DC Motor 22 ADC get voltage function.
err_t dcmotor22_init(dcmotor22_t *ctx, dcmotor22_cfg_t *cfg)
DC Motor 22 initialization function.
uint8_t dcmotor22_get_int_pin(dcmotor22_t *ctx)
DC Motor 22 get int pin function.
void dcmotor22_disable_device(dcmotor22_t *ctx)
DC Motor 22 disable device function.
void dcmotor22_enable_device(dcmotor22_t *ctx)
DC Motor 22 enable device function.
DC Motor 22 Click configuration object.
Definition dcmotor22.h:374
uint32_t baud_rate
Definition dcmotor22.h:388
pin_name_t a0
Definition dcmotor22.h:382
uint32_t i2c_speed
Definition dcmotor22.h:394
bool uart_blocking
Definition dcmotor22.h:389
uart_data_bits_t data_bit
Definition dcmotor22.h:390
pin_name_t tx_pin
Definition dcmotor22.h:377
pin_name_t rx_pin
Definition dcmotor22.h:376
uart_stop_bits_t stop_bit
Definition dcmotor22.h:392
pin_name_t scl
Definition dcmotor22.h:378
pin_name_t en
Definition dcmotor22.h:384
pin_name_t int_pin
Definition dcmotor22.h:385
pin_name_t a1
Definition dcmotor22.h:383
uart_parity_t parity_bit
Definition dcmotor22.h:391
pin_name_t sda
Definition dcmotor22.h:379
uint8_t i2c_address
Definition dcmotor22.h:395
DC Motor 22 Click context object.
Definition dcmotor22.h:344
uint8_t adc_config_byte
Definition dcmotor22.h:364
digital_out_t a0
Definition dcmotor22.h:346
uint8_t adc_setup_byte
Definition dcmotor22.h:363
digital_out_t a1
Definition dcmotor22.h:347
uint8_t uart_device_address
Definition dcmotor22.h:357
uint32_t pwm_ab
Definition dcmotor22.h:365
uint8_t i2c_slave_address
Definition dcmotor22.h:358
uart_t uart
Definition dcmotor22.h:355
digital_in_t int_pin
Definition dcmotor22.h:351
i2c_master_t i2c
Definition dcmotor22.h:354
digital_out_t en
Definition dcmotor22.h:348