dcmotor12  2.1.0.0
dcmotor12.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 DCMOTOR12_H
29 #define DCMOTOR12_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_spi_master.h"
48 #include "spi_specifics.h"
49 #include "drv_i2c_master.h"
50 #include "drv_analog_in.h"
51 
72 #define DCMOTOR12_REG_STATUS1 0x01
73 #define DCMOTOR12_REG_STATUS2 0x02
74 #define DCMOTOR12_REG_STATUS3 0x03
75 #define DCMOTOR12_REG_CONFIG1 0x04
76 #define DCMOTOR12_REG_CONFIG2 0x05
77 #define DCMOTOR12_REG_CONFIG3 0x06
78 #define DCMOTOR12_REG_CONFIG4 0x07
79 #define DCMOTOR12_REG_CONFIG5 0x08
80 #define DCMOTOR12_REG_CONFIG6 0x09
81 #define DCMOTOR12_REG_CONFIG7 0x0A
82 
87 #define DCMOTOR12_REG_INPUT 0x00
88 #define DCMOTOR12_REG_OUTPUT 0x01
89 #define DCMOTOR12_REG_POLARITY 0x02
90 #define DCMOTOR12_REG_CONFIG 0x03
91  // dcmotor12_reg
93 
108 #define DCMOTOR12_CONFIG12_OPEN_LOAD_NOP_100US 0x000000ul
109 #define DCMOTOR12_CONFIG12_OPEN_LOAD_NOP_50US 0x002000ul
110 #define DCMOTOR12_CONFIG12_OPEN_LOAD_NOP_500US 0x004000ul
111 #define DCMOTOR12_CONFIG12_OPEN_LOAD_NOP_MASK 0x006000ul
112 #define DCMOTOR12_CONFIG12_DUTY_OFF_LOW_SIDE 0x000000ul
113 #define DCMOTOR12_CONFIG12_DUTY_OFF_HIGH_SIDE 0x001000ul
114 #define DCMOTOR12_CONFIG12_DUTY_OFF_MASK 0x001000ul
115 #define DCMOTOR12_CONFIG12_VBAT_UVC_0US 0x000000ul
116 #define DCMOTOR12_CONFIG12_VBAT_UVC_100US 0x000800ul
117 #define DCMOTOR12_CONFIG12_VBAT_UVC_MASK 0x000800ul
118 #define DCMOTOR12_CONFIG12_CLIMIT_UP_6p5A 0x000000ul
119 #define DCMOTOR12_CONFIG12_CLIMIT_UP_4p6A 0x000400ul
120 #define DCMOTOR12_CONFIG12_CLIMIT_UP_MASK 0x000400ul
121 #define DCMOTOR12_CONFIG12_CLIMIT_LOW_0p25A 0x000000ul
122 #define DCMOTOR12_CONFIG12_CLIMIT_LOW_0p5A 0x000200ul
123 #define DCMOTOR12_CONFIG12_CLIMIT_LOW_MASK 0x000200ul
124 #define DCMOTOR12_CONFIG12_CLIMIT_LOW_0p25A 0x000000ul
125 #define DCMOTOR12_CONFIG12_CLIMIT_LOW_0p5A 0x000200ul
126 #define DCMOTOR12_CONFIG12_OPEN_LOAD_OP_200mA 0x000000ul
127 #define DCMOTOR12_CONFIG12_OPEN_LOAD_OP_100mA 0x000080ul
128 #define DCMOTOR12_CONFIG12_OPEN_LOAD_OP_300mA 0x000100ul
129 #define DCMOTOR12_CONFIG12_OPEN_LOAD_OP_MASK 0x000180ul
130 #define DCMOTOR12_CONFIG12_SR_MODE_NORMAL 0x000000ul
131 #define DCMOTOR12_CONFIG12_SR_MODE_SLOW1 0x000010ul
132 #define DCMOTOR12_CONFIG12_SR_MODE_SLOW2 0x000020ul
133 #define DCMOTOR12_CONFIG12_SR_MODE_SLOW3 0x000030ul
134 #define DCMOTOR12_CONFIG12_SR_MODE_SLOW4 0x000040ul
135 #define DCMOTOR12_CONFIG12_SR_MODE_FAST2 0x000050ul
136 #define DCMOTOR12_CONFIG12_SR_MODE_FAST1 0x000060ul
137 #define DCMOTOR12_CONFIG12_SR_MODE_MASK 0x000070ul
138 #define DCMOTOR12_CONFIG12_OC_LATCH 0x000000ul
139 #define DCMOTOR12_CONFIG12_OC_AUTO 0x000008ul
140 #define DCMOTOR12_CONFIG12_OC_MASK 0x000008ul
141 #define DCMOTOR12_CONFIG12_OC_LATCH 0x000000ul
142 #define DCMOTOR12_CONFIG12_OC_AUTO 0x000008ul
143 #define DCMOTOR12_CONFIG12_SPI_DISR_10mS 0x000000ul
144 #define DCMOTOR12_CONFIG12_SPI_DISR_100mS 0x000002ul
145 #define DCMOTOR12_CONFIG12_SPI_DISR_1S 0x000004ul
146 #define DCMOTOR12_CONFIG12_SPI_DISR_DISABLE 0x000006ul
147 #define DCMOTOR12_CONFIG12_SPI_DISR_MASK 0x000006ul
148 #define DCMOTOR12_CONFIG12_OPMODE_DISR_OFF 0x000000ul
149 #define DCMOTOR12_CONFIG12_OPMODE_DISR_CONTINUE 0x000001ul
150 #define DCMOTOR12_CONFIG12_OPMODE_DISR_MASK 0x000001ul
151 
156 #define DCMOTOR12_CONFIG56_MOTOR_OPMODE_MAX 3u
157 #define DCMOTOR12_CONFIG56_MOTOR_OPMODE_SHIFT 22ul
158 #define DCMOTOR12_CONFIG56_MOTOR_OPMODE_MASK 0xC00000ul
159 #define DCMOTOR12_CONFIG56_DRV_FREQ_MAX 2047u
160 #define DCMOTOR12_CONFIG56_DRV_FREQ_SHIFT 11ul
161 #define DCMOTOR12_CONFIG56_DRV_FREQ_MASK 0x3FF800ul
162 #define DCMOTOR12_CONFIG56_DUTY_PERIOD_MAX 2047u
163 #define DCMOTOR12_CONFIG56_DUTY_PERIOD_SHIFT 0ul
164 #define DCMOTOR12_CONFIG56_DUTY_PERIOD_MASK 0x0007FFul
165 
170 #define DCMOTOR12_DIAG1_PIN 0x01
171 #define DCMOTOR12_DIAG2_PIN 0x02
172 #define DCMOTOR12_EN1_PIN 0x04
173 #define DCMOTOR12_EN2_PIN 0x08
174 #define DCMOTOR12_CM_SEL_PIN 0x10
175 #define DCMOTOR12_SLP_PIN 0x20
176 
181 #define DCMOTOR12_DEFAULT_CONFIG 0xE3
182 
187 #define DCMOTOR12_MODE_OUTPUT_OFF 0x00
188 #define DCMOTOR12_MODE_FORWARD 0x01
189 #define DCMOTOR12_MODE_REVERSE 0x02
190 #define DCMOTOR12_MODE_BRAKE 0x03
191 
196 #define DCMOTOR12_PIN_LOW_LEVEL 0
197 #define DCMOTOR12_PIN_HIGH_LEVEL 1
198 
203 #define DCMOTOR12_VREF_3V3 3.3
204 #define DCMOTOR12_VREF_5V 5.0
205 
210 #define DCMOTOR12_NUM_CONVERSIONS 5000
211 #define DCMOTOR12_RSENSE 220.0f
212 #define DCMOTOR12_VOLTAGE_TO_CURRENT 0.00238167f
213 
219 #define DCMOTOR12_DEVICE_ADDRESS_A1A0_00 0x70
220 #define DCMOTOR12_DEVICE_ADDRESS_A1A0_01 0x71
221 #define DCMOTOR12_DEVICE_ADDRESS_A1A0_10 0x72
222 #define DCMOTOR12_DEVICE_ADDRESS_A1A0_11 0x73
223 
232 #define DCMOTOR12_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
233 #define DCMOTOR12_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
234  // dcmotor12_set
236 
251 #define DCMOTOR12_MAP_MIKROBUS( cfg, mikrobus ) \
252  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
253  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
254  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
255  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
256  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
257  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
258  cfg.cm = MIKROBUS( mikrobus, MIKROBUS_AN ); \
259  cfg.slp = MIKROBUS( mikrobus, MIKROBUS_RST ); \
260  cfg.clk = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
261  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
262  // dcmotor12_map // dcmotor12
265 
270 typedef struct
271 {
272  // Output pins
273  digital_out_t clk;
274  digital_out_t slp;
276  // Input pins
277  digital_in_t int_pin;
279  // Modules
280  spi_master_t spi;
281  i2c_master_t i2c;
282  analog_in_t adc;
284  uint8_t slave_address;
285  pin_name_t chip_select;
287 } dcmotor12_t;
288 
293 typedef struct
294 {
295  // Communication gpio pins
296  pin_name_t scl;
297  pin_name_t sda;
298  pin_name_t miso;
299  pin_name_t mosi;
300  pin_name_t sck;
301  pin_name_t cs;
302  pin_name_t cm;
304  // Additional gpio pins
305  pin_name_t clk;
306  pin_name_t slp;
307  pin_name_t int_pin;
309  // static variable
310  uint32_t spi_speed;
311  spi_master_mode_t spi_mode;
312  spi_master_chip_select_polarity_t cs_polarity;
314  uint32_t i2c_speed;
315  uint8_t i2c_address;
317  analog_in_resolution_t resolution;
318  float vref;
321 
326 typedef enum
327 {
329  DCMOTOR12_ERROR = -1
330 
332 
349 
364 
378 
392 err_t dcmotor12_write_register ( dcmotor12_t *ctx, uint8_t reg, uint32_t data_in );
393 
406 err_t dcmotor12_read_register ( dcmotor12_t *ctx, uint8_t reg, uint32_t *data_out );
407 
421 err_t dcmotor12_pca9538a_write_register ( dcmotor12_t *ctx, uint8_t reg, uint8_t data_in );
422 
435 err_t dcmotor12_pca9538a_read_register ( dcmotor12_t *ctx, uint8_t reg, uint8_t *data_out );
436 
446 
456 
466 
476 void dcmotor12_set_clk_pin ( dcmotor12_t *ctx, uint8_t state );
477 
490 err_t dcmotor12_set_en1_pin ( dcmotor12_t *ctx, uint8_t state );
491 
504 err_t dcmotor12_get_en1_pin ( dcmotor12_t *ctx, uint8_t *state );
505 
518 err_t dcmotor12_set_en2_pin ( dcmotor12_t *ctx, uint8_t state );
519 
532 err_t dcmotor12_get_en2_pin ( dcmotor12_t *ctx, uint8_t *state );
533 
546 err_t dcmotor12_set_cm_sel_pin ( dcmotor12_t *ctx, uint8_t state );
547 
560 err_t dcmotor12_get_cm_sel_pin ( dcmotor12_t *ctx, uint8_t *state );
561 
574 err_t dcmotor12_get_diag1_pin ( dcmotor12_t *ctx, uint8_t *state );
575 
588 err_t dcmotor12_get_diag2_pin ( dcmotor12_t *ctx, uint8_t *state );
589 
602 err_t dcmotor12_get_sleep_pin ( dcmotor12_t *ctx, uint8_t *state );
603 
615 err_t dcmotor12_read_an_pin_value ( dcmotor12_t *ctx, uint16_t *data_out );
616 
630 err_t dcmotor12_read_an_pin_voltage ( dcmotor12_t *ctx, float *data_out );
631 
643 err_t dcmotor12_get_motor_current ( dcmotor12_t *ctx, float *current );
644 
659 err_t dcmotor12_set_ch1_operation_mode ( dcmotor12_t *ctx, uint8_t mode );
660 
675 err_t dcmotor12_set_ch2_operation_mode ( dcmotor12_t *ctx, uint8_t mode );
676 
688 err_t dcmotor12_set_ch1_drive_frequency ( dcmotor12_t *ctx, uint16_t drv_freq );
689 
701 err_t dcmotor12_set_ch2_drive_frequency ( dcmotor12_t *ctx, uint16_t drv_freq );
702 
714 err_t dcmotor12_set_ch1_duty_period ( dcmotor12_t *ctx, uint16_t duty_period );
715 
727 err_t dcmotor12_set_ch2_duty_period ( dcmotor12_t *ctx, uint16_t duty_period );
728 
729 #ifdef __cplusplus
730 }
731 #endif
732 #endif // DCMOTOR12_H
733  // dcmotor12
735 
736 // ------------------------------------------------------------------------ END
dcmotor12_get_en1_pin
err_t dcmotor12_get_en1_pin(dcmotor12_t *ctx, uint8_t *state)
DC Motor 12 get en1 pin function.
dcmotor12_t
DC Motor 12 Click context object.
Definition: dcmotor12.h:271
dcmotor12_cfg_t
DC Motor 12 Click configuration object.
Definition: dcmotor12.h:294
dcmotor12_cfg_t::int_pin
pin_name_t int_pin
Definition: dcmotor12.h:307
dcmotor12_set_ch2_drive_frequency
err_t dcmotor12_set_ch2_drive_frequency(dcmotor12_t *ctx, uint16_t drv_freq)
DC Motor 12 set ch2 drive frequency function.
dcmotor12_set_en2_pin
err_t dcmotor12_set_en2_pin(dcmotor12_t *ctx, uint8_t state)
DC Motor 12 set en2 pin function.
dcmotor12_set_clk_pin
void dcmotor12_set_clk_pin(dcmotor12_t *ctx, uint8_t state)
DC Motor 12 set clk pin function.
dcmotor12_enter_sleep_mode
void dcmotor12_enter_sleep_mode(dcmotor12_t *ctx)
DC Motor 12 enter sleep mode function.
dcmotor12_read_register
err_t dcmotor12_read_register(dcmotor12_t *ctx, uint8_t reg, uint32_t *data_out)
DC Motor 12 read register function.
dcmotor12_t::slave_address
uint8_t slave_address
Definition: dcmotor12.h:284
dcmotor12_get_en2_pin
err_t dcmotor12_get_en2_pin(dcmotor12_t *ctx, uint8_t *state)
DC Motor 12 get en2 pin function.
dcmotor12_read_an_pin_value
err_t dcmotor12_read_an_pin_value(dcmotor12_t *ctx, uint16_t *data_out)
DC Motor 12 read AN pin value function.
dcmotor12_t::slp
digital_out_t slp
Definition: dcmotor12.h:274
spi_specifics.h
This file contains SPI specific macros, functions, etc.
dcmotor12_t::clk
digital_out_t clk
Definition: dcmotor12.h:273
dcmotor12_get_diag1_pin
err_t dcmotor12_get_diag1_pin(dcmotor12_t *ctx, uint8_t *state)
DC Motor 12 get diag1 pin function.
DCMOTOR12_ERROR
@ DCMOTOR12_ERROR
Definition: dcmotor12.h:329
dcmotor12_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: dcmotor12.h:312
dcmotor12_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: dcmotor12.h:314
dcmotor12_cfg_t::cs
pin_name_t cs
Definition: dcmotor12.h:301
dcmotor12_t::int_pin
digital_in_t int_pin
Definition: dcmotor12.h:277
dcmotor12_cfg_t::mosi
pin_name_t mosi
Definition: dcmotor12.h:299
dcmotor12_t::i2c
i2c_master_t i2c
Definition: dcmotor12.h:281
dcmotor12_write_register
err_t dcmotor12_write_register(dcmotor12_t *ctx, uint8_t reg, uint32_t data_in)
DC Motor 12 write register function.
dcmotor12_set_en1_pin
err_t dcmotor12_set_en1_pin(dcmotor12_t *ctx, uint8_t state)
DC Motor 12 set en1 pin function.
dcmotor12_t::adc
analog_in_t adc
Definition: dcmotor12.h:282
dcmotor12_cfg_t::miso
pin_name_t miso
Definition: dcmotor12.h:298
DCMOTOR12_OK
@ DCMOTOR12_OK
Definition: dcmotor12.h:328
dcmotor12_init
err_t dcmotor12_init(dcmotor12_t *ctx, dcmotor12_cfg_t *cfg)
DC Motor 12 initialization function.
dcmotor12_pca9538a_read_register
err_t dcmotor12_pca9538a_read_register(dcmotor12_t *ctx, uint8_t reg, uint8_t *data_out)
DC Motor 12 pca9538a read register function.
dcmotor12_wake_up_device
void dcmotor12_wake_up_device(dcmotor12_t *ctx)
DC Motor 12 wake up device function.
dcmotor12_get_motor_current
err_t dcmotor12_get_motor_current(dcmotor12_t *ctx, float *current)
DC Motor 12 get motor current function.
dcmotor12_set_ch2_duty_period
err_t dcmotor12_set_ch2_duty_period(dcmotor12_t *ctx, uint16_t duty_period)
DC Motor 12 set ch1 duty period function.
dcmotor12_cfg_t::clk
pin_name_t clk
Definition: dcmotor12.h:305
dcmotor12_cfg_setup
void dcmotor12_cfg_setup(dcmotor12_cfg_t *cfg)
DC Motor 12 configuration object setup function.
dcmotor12_get_cm_sel_pin
err_t dcmotor12_get_cm_sel_pin(dcmotor12_t *ctx, uint8_t *state)
DC Motor 12 get cm sel pin function.
dcmotor12_cfg_t::spi_speed
uint32_t spi_speed
Definition: dcmotor12.h:310
dcmotor12_t::chip_select
pin_name_t chip_select
Definition: dcmotor12.h:285
dcmotor12_cfg_t::sck
pin_name_t sck
Definition: dcmotor12.h:300
dcmotor12_cfg_t::i2c_address
uint8_t i2c_address
Definition: dcmotor12.h:315
dcmotor12_default_cfg
err_t dcmotor12_default_cfg(dcmotor12_t *ctx)
DC Motor 12 default configuration function.
dcmotor12_cfg_t::resolution
analog_in_resolution_t resolution
Definition: dcmotor12.h:317
dcmotor12_cfg_t::scl
pin_name_t scl
Definition: dcmotor12.h:296
dcmotor12_cfg_t::slp
pin_name_t slp
Definition: dcmotor12.h:306
dcmotor12_set_ch1_operation_mode
err_t dcmotor12_set_ch1_operation_mode(dcmotor12_t *ctx, uint8_t mode)
DC Motor 12 set ch1 operation mode function.
dcmotor12_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: dcmotor12.h:311
dcmotor12_set_ch2_operation_mode
err_t dcmotor12_set_ch2_operation_mode(dcmotor12_t *ctx, uint8_t mode)
DC Motor 12 set ch2 operation mode function.
dcmotor12_set_ch1_duty_period
err_t dcmotor12_set_ch1_duty_period(dcmotor12_t *ctx, uint16_t duty_period)
DC Motor 12 set ch1 duty period function.
dcmotor12_read_an_pin_voltage
err_t dcmotor12_read_an_pin_voltage(dcmotor12_t *ctx, float *data_out)
DC Motor 12 read AN pin voltage level function.
dcmotor12_get_int_pin
uint8_t dcmotor12_get_int_pin(dcmotor12_t *ctx)
DC Motor 12 get int pin function.
dcmotor12_return_value_t
dcmotor12_return_value_t
DC Motor 12 Click return value data.
Definition: dcmotor12.h:327
dcmotor12_set_ch1_drive_frequency
err_t dcmotor12_set_ch1_drive_frequency(dcmotor12_t *ctx, uint16_t drv_freq)
DC Motor 12 set ch1 drive frequency function.
dcmotor12_cfg_t::vref
float vref
Definition: dcmotor12.h:318
dcmotor12_pca9538a_write_register
err_t dcmotor12_pca9538a_write_register(dcmotor12_t *ctx, uint8_t reg, uint8_t data_in)
DC Motor 12 pca9538a write register function.
dcmotor12_set_cm_sel_pin
err_t dcmotor12_set_cm_sel_pin(dcmotor12_t *ctx, uint8_t state)
DC Motor 12 set cm sel pin function.
dcmotor12_cfg_t::cm
pin_name_t cm
Definition: dcmotor12.h:302
dcmotor12_t::spi
spi_master_t spi
Definition: dcmotor12.h:280
dcmotor12_cfg_t::sda
pin_name_t sda
Definition: dcmotor12.h:297
dcmotor12_get_sleep_pin
err_t dcmotor12_get_sleep_pin(dcmotor12_t *ctx, uint8_t *state)
DC Motor 12 get sleep pin function.
dcmotor12_get_diag2_pin
err_t dcmotor12_get_diag2_pin(dcmotor12_t *ctx, uint8_t *state)
DC Motor 12 get diag2 pin function.