dcmotor26  2.1.0.0
dcmotor26.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 DCMOTOR26_H
29 #define DCMOTOR26_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_spi_master.h"
52 #include "spi_specifics.h"
53 #include "drv_i2c_master.h"
54 #include "drv_analog_in.h"
55 
76 #define DCMOTOR26_REG_STATUS1 0x01
77 #define DCMOTOR26_REG_STATUS2 0x02
78 #define DCMOTOR26_REG_STATUS3 0x03
79 #define DCMOTOR26_REG_CONFIG1 0x04
80 #define DCMOTOR26_REG_CONFIG2 0x05
81 #define DCMOTOR26_REG_CONFIG3 0x06
82 #define DCMOTOR26_REG_CONFIG4 0x07
83 #define DCMOTOR26_REG_CONFIG5 0x08
84 #define DCMOTOR26_REG_CONFIG6 0x09
85 #define DCMOTOR26_REG_CONFIG7 0x0A
86 
91 #define DCMOTOR26_REG_INPUT 0x00
92 #define DCMOTOR26_REG_OUTPUT 0x01
93 #define DCMOTOR26_REG_POLARITY 0x02
94 #define DCMOTOR26_REG_CONFIG 0x03
95  // dcmotor26_reg
97 
112 #define DCMOTOR26_CONFIG12_OPEN_LOAD_NOP_100US 0x000000ul
113 #define DCMOTOR26_CONFIG12_OPEN_LOAD_NOP_50US 0x002000ul
114 #define DCMOTOR26_CONFIG12_OPEN_LOAD_NOP_500US 0x004000ul
115 #define DCMOTOR26_CONFIG12_OPEN_LOAD_NOP_MASK 0x006000ul
116 #define DCMOTOR26_CONFIG12_DUTY_OFF_LOW_SIDE 0x000000ul
117 #define DCMOTOR26_CONFIG12_DUTY_OFF_HIGH_SIDE 0x001000ul
118 #define DCMOTOR26_CONFIG12_DUTY_OFF_MASK 0x001000ul
119 #define DCMOTOR26_CONFIG12_VBAT_UVC_0US 0x000000ul
120 #define DCMOTOR26_CONFIG12_VBAT_UVC_100US 0x000800ul
121 #define DCMOTOR26_CONFIG12_VBAT_UVC_MASK 0x000800ul
122 #define DCMOTOR26_CONFIG12_CLIMIT_UP_6p5A 0x000000ul
123 #define DCMOTOR26_CONFIG12_CLIMIT_UP_4p6A 0x000400ul
124 #define DCMOTOR26_CONFIG12_CLIMIT_UP_MASK 0x000400ul
125 #define DCMOTOR26_CONFIG12_CLIMIT_LOW_0p25A 0x000000ul
126 #define DCMOTOR26_CONFIG12_CLIMIT_LOW_0p5A 0x000200ul
127 #define DCMOTOR26_CONFIG12_CLIMIT_LOW_MASK 0x000200ul
128 #define DCMOTOR26_CONFIG12_CLIMIT_LOW_0p25A 0x000000ul
129 #define DCMOTOR26_CONFIG12_CLIMIT_LOW_0p5A 0x000200ul
130 #define DCMOTOR26_CONFIG12_OPEN_LOAD_OP_200mA 0x000000ul
131 #define DCMOTOR26_CONFIG12_OPEN_LOAD_OP_100mA 0x000080ul
132 #define DCMOTOR26_CONFIG12_OPEN_LOAD_OP_300mA 0x000100ul
133 #define DCMOTOR26_CONFIG12_OPEN_LOAD_OP_MASK 0x000180ul
134 #define DCMOTOR26_CONFIG12_SR_MODE_NORMAL 0x000000ul
135 #define DCMOTOR26_CONFIG12_SR_MODE_SLOW1 0x000010ul
136 #define DCMOTOR26_CONFIG12_SR_MODE_SLOW2 0x000020ul
137 #define DCMOTOR26_CONFIG12_SR_MODE_SLOW3 0x000030ul
138 #define DCMOTOR26_CONFIG12_SR_MODE_SLOW4 0x000040ul
139 #define DCMOTOR26_CONFIG12_SR_MODE_FAST2 0x000050ul
140 #define DCMOTOR26_CONFIG12_SR_MODE_FAST1 0x000060ul
141 #define DCMOTOR26_CONFIG12_SR_MODE_MASK 0x000070ul
142 #define DCMOTOR26_CONFIG12_OC_LATCH 0x000000ul
143 #define DCMOTOR26_CONFIG12_OC_AUTO 0x000008ul
144 #define DCMOTOR26_CONFIG12_OC_MASK 0x000008ul
145 #define DCMOTOR26_CONFIG12_OC_LATCH 0x000000ul
146 #define DCMOTOR26_CONFIG12_OC_AUTO 0x000008ul
147 #define DCMOTOR26_CONFIG12_SPI_DISR_10mS 0x000000ul
148 #define DCMOTOR26_CONFIG12_SPI_DISR_100mS 0x000002ul
149 #define DCMOTOR26_CONFIG12_SPI_DISR_1S 0x000004ul
150 #define DCMOTOR26_CONFIG12_SPI_DISR_DISABLE 0x000006ul
151 #define DCMOTOR26_CONFIG12_SPI_DISR_MASK 0x000006ul
152 #define DCMOTOR26_CONFIG12_OPMODE_DISR_OFF 0x000000ul
153 #define DCMOTOR26_CONFIG12_OPMODE_DISR_CONTINUE 0x000001ul
154 #define DCMOTOR26_CONFIG12_OPMODE_DISR_MASK 0x000001ul
155 
160 #define DCMOTOR26_CONFIG56_MOTOR_OPMODE_MAX 3u
161 #define DCMOTOR26_CONFIG56_MOTOR_OPMODE_SHIFT 22ul
162 #define DCMOTOR26_CONFIG56_MOTOR_OPMODE_MASK 0xC00000ul
163 #define DCMOTOR26_CONFIG56_DRV_FREQ_MAX 2047u
164 #define DCMOTOR26_CONFIG56_DRV_FREQ_SHIFT 11ul
165 #define DCMOTOR26_CONFIG56_DRV_FREQ_MASK 0x3FF800ul
166 #define DCMOTOR26_CONFIG56_DUTY_PERIOD_MAX 2047u
167 #define DCMOTOR26_CONFIG56_DUTY_PERIOD_SHIFT 0ul
168 #define DCMOTOR26_CONFIG56_DUTY_PERIOD_MASK 0x0007FFul
169 
174 #define DCMOTOR26_DIAG1_PIN 0x01
175 #define DCMOTOR26_DIAG2_PIN 0x02
176 #define DCMOTOR26_EN1_PIN 0x04
177 #define DCMOTOR26_EN2_PIN 0x08
178 #define DCMOTOR26_CM_SEL_PIN 0x10
179 #define DCMOTOR26_SLP_PIN 0x20
180 
185 #define DCMOTOR26_DEFAULT_CONFIG 0xE3
186 
191 #define DCMOTOR26_MODE_OUTPUT_OFF 0x00
192 #define DCMOTOR26_MODE_FORWARD 0x01
193 #define DCMOTOR26_MODE_REVERSE 0x02
194 #define DCMOTOR26_MODE_BRAKE 0x03
195 
200 #define DCMOTOR26_PIN_LOW_LEVEL 0
201 #define DCMOTOR26_PIN_HIGH_LEVEL 1
202 
207 #define DCMOTOR26_VREF_3V3 3.3
208 #define DCMOTOR26_VREF_5V 5.0
209 
214 #define DCMOTOR26_NUM_CONVERSIONS 5000
215 #define DCMOTOR26_RSENSE 220.0f
216 #define DCMOTOR26_VOLTAGE_TO_CURRENT 0.00238167f
217 
223 #define DCMOTOR26_DEVICE_ADDRESS_A1A0_00 0x70
224 #define DCMOTOR26_DEVICE_ADDRESS_A1A0_01 0x71
225 #define DCMOTOR26_DEVICE_ADDRESS_A1A0_10 0x72
226 #define DCMOTOR26_DEVICE_ADDRESS_A1A0_11 0x73
227 
236 #define DCMOTOR26_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
237 #define DCMOTOR26_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
238  // dcmotor26_set
240 
255 #define DCMOTOR26_MAP_MIKROBUS( cfg, mikrobus ) \
256  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
257  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
258  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
259  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
260  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
261  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
262  cfg.cm = MIKROBUS( mikrobus, MIKROBUS_AN ); \
263  cfg.slp = MIKROBUS( mikrobus, MIKROBUS_RST ); \
264  cfg.clk = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
265  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
266  // dcmotor26_map // dcmotor26
269 
274 typedef struct
275 {
276  // Output pins
277  digital_out_t clk;
278  digital_out_t slp;
280  // Input pins
281  digital_in_t int_pin;
283  // Modules
284  spi_master_t spi;
285  i2c_master_t i2c;
286  analog_in_t adc;
288  uint8_t slave_address;
289  pin_name_t chip_select;
291 } dcmotor26_t;
292 
297 typedef struct
298 {
299  // Communication gpio pins
300  pin_name_t scl;
301  pin_name_t sda;
302  pin_name_t miso;
303  pin_name_t mosi;
304  pin_name_t sck;
305  pin_name_t cs;
306  pin_name_t cm;
308  // Additional gpio pins
309  pin_name_t clk;
310  pin_name_t slp;
311  pin_name_t int_pin;
313  // static variable
314  uint32_t spi_speed;
315  spi_master_mode_t spi_mode;
316  spi_master_chip_select_polarity_t cs_polarity;
318  uint32_t i2c_speed;
319  uint8_t i2c_address;
321  analog_in_resolution_t resolution;
322  float vref;
325 
330 typedef enum
331 {
333  DCMOTOR26_ERROR = -1
334 
336 
353 
368 
382 
396 err_t dcmotor26_write_register ( dcmotor26_t *ctx, uint8_t reg, uint32_t data_in );
397 
410 err_t dcmotor26_read_register ( dcmotor26_t *ctx, uint8_t reg, uint32_t *data_out );
411 
425 err_t dcmotor26_pca9538a_write_register ( dcmotor26_t *ctx, uint8_t reg, uint8_t data_in );
426 
439 err_t dcmotor26_pca9538a_read_register ( dcmotor26_t *ctx, uint8_t reg, uint8_t *data_out );
440 
450 
460 
470 
480 void dcmotor26_set_clk_pin ( dcmotor26_t *ctx, uint8_t state );
481 
494 err_t dcmotor26_set_en1_pin ( dcmotor26_t *ctx, uint8_t state );
495 
508 err_t dcmotor26_get_en1_pin ( dcmotor26_t *ctx, uint8_t *state );
509 
522 err_t dcmotor26_set_en2_pin ( dcmotor26_t *ctx, uint8_t state );
523 
536 err_t dcmotor26_get_en2_pin ( dcmotor26_t *ctx, uint8_t *state );
537 
550 err_t dcmotor26_set_cm_sel_pin ( dcmotor26_t *ctx, uint8_t state );
551 
564 err_t dcmotor26_get_cm_sel_pin ( dcmotor26_t *ctx, uint8_t *state );
565 
578 err_t dcmotor26_get_diag1_pin ( dcmotor26_t *ctx, uint8_t *state );
579 
592 err_t dcmotor26_get_diag2_pin ( dcmotor26_t *ctx, uint8_t *state );
593 
606 err_t dcmotor26_get_sleep_pin ( dcmotor26_t *ctx, uint8_t *state );
607 
619 err_t dcmotor26_read_an_pin_value ( dcmotor26_t *ctx, uint16_t *data_out );
620 
634 err_t dcmotor26_read_an_pin_voltage ( dcmotor26_t *ctx, float *data_out );
635 
647 err_t dcmotor26_get_motor_current ( dcmotor26_t *ctx, float *current );
648 
663 err_t dcmotor26_set_ch1_operation_mode ( dcmotor26_t *ctx, uint8_t mode );
664 
679 err_t dcmotor26_set_ch2_operation_mode ( dcmotor26_t *ctx, uint8_t mode );
680 
692 err_t dcmotor26_set_ch1_drive_frequency ( dcmotor26_t *ctx, uint16_t drv_freq );
693 
705 err_t dcmotor26_set_ch2_drive_frequency ( dcmotor26_t *ctx, uint16_t drv_freq );
706 
718 err_t dcmotor26_set_ch1_duty_period ( dcmotor26_t *ctx, uint16_t duty_period );
719 
731 err_t dcmotor26_set_ch2_duty_period ( dcmotor26_t *ctx, uint16_t duty_period );
732 
733 #ifdef __cplusplus
734 }
735 #endif
736 #endif // DCMOTOR26_H
737  // dcmotor26
739 
740 // ------------------------------------------------------------------------ END
dcmotor26_get_int_pin
uint8_t dcmotor26_get_int_pin(dcmotor26_t *ctx)
DC Motor 26 get int pin function.
dcmotor26_set_ch1_drive_frequency
err_t dcmotor26_set_ch1_drive_frequency(dcmotor26_t *ctx, uint16_t drv_freq)
DC Motor 26 set ch1 drive frequency function.
dcmotor26_read_register
err_t dcmotor26_read_register(dcmotor26_t *ctx, uint8_t reg, uint32_t *data_out)
DC Motor 26 read register function.
dcmotor26_get_cm_sel_pin
err_t dcmotor26_get_cm_sel_pin(dcmotor26_t *ctx, uint8_t *state)
DC Motor 26 get cm sel pin function.
dcmotor26_enter_sleep_mode
void dcmotor26_enter_sleep_mode(dcmotor26_t *ctx)
DC Motor 26 enter sleep mode function.
dcmotor26_set_ch2_operation_mode
err_t dcmotor26_set_ch2_operation_mode(dcmotor26_t *ctx, uint8_t mode)
DC Motor 26 set ch2 operation mode function.
dcmotor26_get_motor_current
err_t dcmotor26_get_motor_current(dcmotor26_t *ctx, float *current)
DC Motor 26 get motor current function.
dcmotor26_pca9538a_write_register
err_t dcmotor26_pca9538a_write_register(dcmotor26_t *ctx, uint8_t reg, uint8_t data_in)
DC Motor 26 pca9538a write register function.
dcmotor26_wake_up_device
void dcmotor26_wake_up_device(dcmotor26_t *ctx)
DC Motor 26 wake up device function.
spi_specifics.h
This file contains SPI specific macros, functions, etc.
dcmotor26_read_an_pin_value
err_t dcmotor26_read_an_pin_value(dcmotor26_t *ctx, uint16_t *data_out)
DC Motor 26 read AN pin value function.
dcmotor26_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: dcmotor26.h:318
dcmotor26_set_ch2_drive_frequency
err_t dcmotor26_set_ch2_drive_frequency(dcmotor26_t *ctx, uint16_t drv_freq)
DC Motor 26 set ch2 drive frequency function.
dcmotor26_pca9538a_read_register
err_t dcmotor26_pca9538a_read_register(dcmotor26_t *ctx, uint8_t reg, uint8_t *data_out)
DC Motor 26 pca9538a read register function.
dcmotor26_t::adc
analog_in_t adc
Definition: dcmotor26.h:286
dcmotor26_cfg_t::sck
pin_name_t sck
Definition: dcmotor26.h:304
dcmotor26_t::slp
digital_out_t slp
Definition: dcmotor26.h:278
dcmotor26_cfg_t::int_pin
pin_name_t int_pin
Definition: dcmotor26.h:311
dcmotor26_set_cm_sel_pin
err_t dcmotor26_set_cm_sel_pin(dcmotor26_t *ctx, uint8_t state)
DC Motor 26 set cm sel pin function.
dcmotor26_get_diag1_pin
err_t dcmotor26_get_diag1_pin(dcmotor26_t *ctx, uint8_t *state)
DC Motor 26 get diag1 pin function.
dcmotor26_write_register
err_t dcmotor26_write_register(dcmotor26_t *ctx, uint8_t reg, uint32_t data_in)
DC Motor 26 write register function.
dcmotor26_cfg_setup
void dcmotor26_cfg_setup(dcmotor26_cfg_t *cfg)
DC Motor 26 configuration object setup function.
dcmotor26_read_an_pin_voltage
err_t dcmotor26_read_an_pin_voltage(dcmotor26_t *ctx, float *data_out)
DC Motor 26 read AN pin voltage level function.
dcmotor26_t::int_pin
digital_in_t int_pin
Definition: dcmotor26.h:281
dcmotor26_cfg_t::clk
pin_name_t clk
Definition: dcmotor26.h:309
dcmotor26_cfg_t::scl
pin_name_t scl
Definition: dcmotor26.h:300
dcmotor26_t
DC Motor 26 Click context object.
Definition: dcmotor26.h:275
dcmotor26_set_clk_pin
void dcmotor26_set_clk_pin(dcmotor26_t *ctx, uint8_t state)
DC Motor 26 set clk pin function.
dcmotor26_get_en1_pin
err_t dcmotor26_get_en1_pin(dcmotor26_t *ctx, uint8_t *state)
DC Motor 26 get en1 pin function.
dcmotor26_t::i2c
i2c_master_t i2c
Definition: dcmotor26.h:285
DCMOTOR26_OK
@ DCMOTOR26_OK
Definition: dcmotor26.h:332
dcmotor26_get_en2_pin
err_t dcmotor26_get_en2_pin(dcmotor26_t *ctx, uint8_t *state)
DC Motor 26 get en2 pin function.
dcmotor26_set_en1_pin
err_t dcmotor26_set_en1_pin(dcmotor26_t *ctx, uint8_t state)
DC Motor 26 set en1 pin function.
dcmotor26_cfg_t::cm
pin_name_t cm
Definition: dcmotor26.h:306
DCMOTOR26_ERROR
@ DCMOTOR26_ERROR
Definition: dcmotor26.h:333
dcmotor26_cfg_t::slp
pin_name_t slp
Definition: dcmotor26.h:310
dcmotor26_cfg_t::spi_speed
uint32_t spi_speed
Definition: dcmotor26.h:314
dcmotor26_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: dcmotor26.h:315
dcmotor26_cfg_t::sda
pin_name_t sda
Definition: dcmotor26.h:301
dcmotor26_set_ch1_operation_mode
err_t dcmotor26_set_ch1_operation_mode(dcmotor26_t *ctx, uint8_t mode)
DC Motor 26 set ch1 operation mode function.
dcmotor26_cfg_t::resolution
analog_in_resolution_t resolution
Definition: dcmotor26.h:321
dcmotor26_t::spi
spi_master_t spi
Definition: dcmotor26.h:284
dcmotor26_cfg_t::vref
float vref
Definition: dcmotor26.h:322
dcmotor26_set_en2_pin
err_t dcmotor26_set_en2_pin(dcmotor26_t *ctx, uint8_t state)
DC Motor 26 set en2 pin function.
dcmotor26_cfg_t::i2c_address
uint8_t i2c_address
Definition: dcmotor26.h:319
dcmotor26_set_ch2_duty_period
err_t dcmotor26_set_ch2_duty_period(dcmotor26_t *ctx, uint16_t duty_period)
DC Motor 26 set ch1 duty period function.
dcmotor26_t::chip_select
pin_name_t chip_select
Definition: dcmotor26.h:289
dcmotor26_cfg_t::miso
pin_name_t miso
Definition: dcmotor26.h:302
dcmotor26_cfg_t::mosi
pin_name_t mosi
Definition: dcmotor26.h:303
dcmotor26_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: dcmotor26.h:316
dcmotor26_return_value_t
dcmotor26_return_value_t
DC Motor 26 Click return value data.
Definition: dcmotor26.h:331
dcmotor26_get_sleep_pin
err_t dcmotor26_get_sleep_pin(dcmotor26_t *ctx, uint8_t *state)
DC Motor 26 get sleep pin function.
dcmotor26_cfg_t
DC Motor 26 Click configuration object.
Definition: dcmotor26.h:298
dcmotor26_init
err_t dcmotor26_init(dcmotor26_t *ctx, dcmotor26_cfg_t *cfg)
DC Motor 26 initialization function.
dcmotor26_t::slave_address
uint8_t slave_address
Definition: dcmotor26.h:288
dcmotor26_set_ch1_duty_period
err_t dcmotor26_set_ch1_duty_period(dcmotor26_t *ctx, uint16_t duty_period)
DC Motor 26 set ch1 duty period function.
dcmotor26_cfg_t::cs
pin_name_t cs
Definition: dcmotor26.h:305
dcmotor26_default_cfg
err_t dcmotor26_default_cfg(dcmotor26_t *ctx)
DC Motor 26 default configuration function.
dcmotor26_get_diag2_pin
err_t dcmotor26_get_diag2_pin(dcmotor26_t *ctx, uint8_t *state)
DC Motor 26 get diag2 pin function.
dcmotor26_t::clk
digital_out_t clk
Definition: dcmotor26.h:277