dcmotor29  2.1.0.0
dcmotor29.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 DCMOTOR29_H
29 #define DCMOTOR29_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 
71 #define DCMOTOR29_REG_DEVICE_ID 0x00
72 #define DCMOTOR29_REG_FAULT_SUMMARY 0x01
73 #define DCMOTOR29_REG_STATUS1 0x02
74 #define DCMOTOR29_REG_STATUS2 0x03
75 #define DCMOTOR29_REG_COMMAND 0x08
76 #define DCMOTOR29_REG_SPI_IN 0x09
77 #define DCMOTOR29_REG_CONFIG1 0x0A
78 #define DCMOTOR29_REG_CONFIG2 0x0B
79 #define DCMOTOR29_REG_CONFIG3 0x0C
80 #define DCMOTOR29_REG_CONFIG4 0x0D
81 #define DCMOTOR29_REG_INPUT_PORT 0x00
82 #define DCMOTOR29_REG_OUTPUT_PORT 0x01
83 #define DCMOTOR29_REG_POLARITY_INV 0x02
84 #define DCMOTOR29_REG_CONFIG 0x03
85  // dcmotor29_reg
87 
102 #define DCMOTOR29_CLR_FLT_CMD 0x80
103 #define DCMOTOR29_SPI_IN_UNLOCK_CMD 0x10
104 #define DCMOTOR29_SPI_IN_LOCK_CMD 0x08
105 #define DCMOTOR29_REG_UNLOCK_CMD 0x01
106 #define DCMOTOR29_REG_LOCK_CMD 0x02
107 
113 #define DCMOTOR29_NONE_PIN 0x00
114 #define DCMOTOR29_IN2_PIN 0x01
115 #define DCMOTOR29_DOF_PIN 0x02
116 #define DCMOTOR29_FLT_PIN 0x04
117 #define DCMOTOR29_ALL_PIN 0x07
118 
124 #define DCMOTOR29_READ_REG_MASK 0x40
125 
131 #define DCMOTOR29_DRVOFF_ON 0x01
132 #define DCMOTOR29_DRVOFF_OFF 0x00
133 
139 #define DCMOTOR29_PIN_STATE_HIGH 0x01
140 #define DCMOTOR29_PIN_STATE_LOW 0x00
141 
147 #define DCMOTOR29_S_EN_DEVOFF_MASK 0x08
148 #define DCMOTOR29_S_EN_DEVOFF2_MASK 0x04
149 #define DCMOTOR29_S_EN_IN1_MASK 0x02
150 #define DCMOTOR29_S_EN_IN2_MASK 0x01
151 #define DCMOTOR29_S_EN_NONE 0x00
152 
158 #define DCMOTOR29_EN_OLA 0x80
159 #define DCMOTOR29_VMOV_SEL_DISABLED 0x60
160 #define DCMOTOR29_VMOV_SEL_18V 0x40
161 #define DCMOTOR29_VMOV_SEL_28V 0x20
162 #define DCMOTOR29_VMOV_SEL_35V 0x00
163 #define DCMOTOR29_SSC_DISABLE 0x10
164 #define DCMOTOR29_OCP_RETRY 0x08
165 #define DCMOTOR29_TSD_RETRY 0x04
166 #define DCMOTOR29_VMOV_RETRY 0x02
167 #define DCMOTOR29_OLA_RETRY 0x01
168 
174 #define DCMOTOR29_TOF_50_US 0xC0
175 #define DCMOTOR29_TOF_40_US 0x80
176 #define DCMOTOR29_TOF_30_US 0x40
177 #define DCMOTOR29_TOF_20_US 0x00
178 #define DCMOTOR29_S_SR_50_V_US 0x1C
179 #define DCMOTOR29_S_SR_38_V_US 0x18
180 #define DCMOTOR29_S_SR_26_V_US 0x14
181 #define DCMOTOR29_S_SR_20_V_US 0x10
182 #define DCMOTOR29_S_SR_14_V_US 0x0C
183 #define DCMOTOR29_S_SR_9_8V_US 0x08
184 #define DCMOTOR29_S_SR_5_V_US 0x04
185 #define DCMOTOR29_S_SR_1_5V_US 0x00
186 #define DCMOTOR29_S_MODE_PWM 0x03
187 #define DCMOTOR29_S_MODE_INDEPENDENT 0x01
188 #define DCMOTOR29_S_MODE_PH_EN 0x00
189 
195 #define DCMOTOR29_DRIVE_MOTOR_CW 0x00
196 #define DCMOTOR29_DRIVE_MOTOR_CCW 0x01
197 #define DCMOTOR29_DRIVE_MOTOR_BRAKE 0x02
198 #define DCMOTOR29_DRIVE_MOTOR_COASTING 0x03
199 
205 #define DCMOTOR29_DEVICE_ADDRESS_A1A0_00 0x70
206 #define DCMOTOR29_DEVICE_ADDRESS_A1A0_01 0x71
207 #define DCMOTOR29_DEVICE_ADDRESS_A1A0_10 0x72
208 #define DCMOTOR29_DEVICE_ADDRESS_A1A0_11 0x73
209 
218 #define DCMOTOR29_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
219 #define DCMOTOR29_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
220  // dcmotor29_set
222 
237 #define DCMOTOR29_MAP_MIKROBUS( cfg, mikrobus ) \
238  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
239  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
240  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
241  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
242  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
243  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
244  cfg.ip = MIKROBUS( mikrobus, MIKROBUS_AN ); \
245  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
246  cfg.in1 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
247  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
248  // dcmotor29_map // dcmotor29
251 
256 typedef struct
257 {
258  // Output pins
259  digital_out_t rst;
260  digital_out_t in1;
262  // Input pins
263  digital_in_t ip;
264  digital_in_t int_pin;
266  // Modules
267  spi_master_t spi;
268  i2c_master_t i2c;
270  // I2C slave address
271  uint8_t slave_address;
273  pin_name_t chip_select;
275 } dcmotor29_t;
276 
281 typedef struct
282 {
283  // Communication gpio pins
284  pin_name_t miso;
285  pin_name_t mosi;
286  pin_name_t sck;
287  pin_name_t cs;
288  pin_name_t scl;
289  pin_name_t sda;
291  // Additional gpio pins
292  pin_name_t ip;
293  pin_name_t rst;
294  pin_name_t in1;
295  pin_name_t int_pin;
297  // static variable
298  uint32_t spi_speed;
299  spi_master_mode_t spi_mode;
300  spi_master_chip_select_polarity_t cs_polarity;
301  uint32_t i2c_speed;
302  uint8_t i2c_address;
305 
310 typedef enum
311 {
313  DCMOTOR29_ERROR = -1
314 
316 
333 
348 
362 
377 err_t dcmotor29_generic_write ( dcmotor29_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
378 
393 err_t dcmotor29_generic_read ( dcmotor29_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
394 
404 void dcmotor29_set_in1_pin ( dcmotor29_t *ctx, uint8_t pin_state );
405 
415 void dcmotor29_set_rst_pin ( dcmotor29_t *ctx, uint8_t pin_state );
416 
426 
436 
451 err_t dcmotor29_register_write ( dcmotor29_t *ctx, uint8_t reg, uint8_t data_in );
452 
466 err_t dcmotor29_register_read ( dcmotor29_t *ctx, uint8_t reg, uint8_t *data_out );
467 
482 err_t dcmotor29_port_expander_write ( dcmotor29_t *ctx, uint8_t reg, uint8_t data_in );
483 
497 err_t dcmotor29_port_expander_read ( dcmotor29_t *ctx, uint8_t reg, uint8_t *data_out );
498 
511 err_t dcmotor29_set_pins ( dcmotor29_t *ctx, uint8_t set_mask, uint8_t clr_mask );
512 
524 err_t dcmotor29_drvoff_state ( dcmotor29_t *ctx, uint8_t drvoff_state );
525 
537 err_t dcmotor29_drive_motor ( dcmotor29_t *ctx, uint8_t state );
538 
539 #ifdef __cplusplus
540 }
541 #endif
542 #endif // DCMOTOR29_H
543  // dcmotor29
545 
546 // ------------------------------------------------------------------------ END
dcmotor29_set_rst_pin
void dcmotor29_set_rst_pin(dcmotor29_t *ctx, uint8_t pin_state)
DC Motor 29 set RST pin function.
dcmotor29_cfg_t::scl
pin_name_t scl
Definition: dcmotor29.h:288
dcmotor29_t::i2c
i2c_master_t i2c
Definition: dcmotor29.h:268
dcmotor29_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: dcmotor29.h:299
dcmotor29_port_expander_read
err_t dcmotor29_port_expander_read(dcmotor29_t *ctx, uint8_t reg, uint8_t *data_out)
DC Motor 29 port ecpander read register function.
dcmotor29_set_pins
err_t dcmotor29_set_pins(dcmotor29_t *ctx, uint8_t set_mask, uint8_t clr_mask)
DC Motor 29 set pins function.
dcmotor29_generic_read
err_t dcmotor29_generic_read(dcmotor29_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
DC Motor 29 data reading function.
dcmotor29_set_in1_pin
void dcmotor29_set_in1_pin(dcmotor29_t *ctx, uint8_t pin_state)
DC Motor 29 set IN1 pin function.
dcmotor29_cfg_t::cs
pin_name_t cs
Definition: dcmotor29.h:287
spi_specifics.h
This file contains SPI specific macros, functions, etc.
dcmotor29_init
err_t dcmotor29_init(dcmotor29_t *ctx, dcmotor29_cfg_t *cfg)
DC Motor 29 initialization function.
dcmotor29_default_cfg
err_t dcmotor29_default_cfg(dcmotor29_t *ctx)
DC Motor 29 default configuration function.
dcmotor29_cfg_t::rst
pin_name_t rst
Definition: dcmotor29.h:293
dcmotor29_t::chip_select
pin_name_t chip_select
Definition: dcmotor29.h:273
DCMOTOR29_OK
@ DCMOTOR29_OK
Definition: dcmotor29.h:312
dcmotor29_t::spi
spi_master_t spi
Definition: dcmotor29.h:267
dcmotor29_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: dcmotor29.h:300
dcmotor29_t::slave_address
uint8_t slave_address
Definition: dcmotor29.h:271
dcmotor29_register_read
err_t dcmotor29_register_read(dcmotor29_t *ctx, uint8_t reg, uint8_t *data_out)
DC Motor 29 data register reading function.
dcmotor29_drive_motor
err_t dcmotor29_drive_motor(dcmotor29_t *ctx, uint8_t state)
DC Motor 29 drive motor function.
dcmotor29_cfg_t::i2c_address
uint8_t i2c_address
Definition: dcmotor29.h:302
dcmotor29_t::in1
digital_out_t in1
Definition: dcmotor29.h:260
dcmotor29_port_expander_write
err_t dcmotor29_port_expander_write(dcmotor29_t *ctx, uint8_t reg, uint8_t data_in)
DC Motor 29 port ecpander write register function.
dcmotor29_generic_write
err_t dcmotor29_generic_write(dcmotor29_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
DC Motor 29 data writing function.
DCMOTOR29_ERROR
@ DCMOTOR29_ERROR
Definition: dcmotor29.h:313
dcmotor29_t
DC Motor 29 Click context object.
Definition: dcmotor29.h:257
dcmotor29_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: dcmotor29.h:301
dcmotor29_get_int_pin
uint8_t dcmotor29_get_int_pin(dcmotor29_t *ctx)
DC Motor 29 get INT pin function.
dcmotor29_get_ip_pin
uint8_t dcmotor29_get_ip_pin(dcmotor29_t *ctx)
DC Motor 29 get IP pin function.
dcmotor29_t::ip
digital_in_t ip
Definition: dcmotor29.h:263
dcmotor29_cfg_t::miso
pin_name_t miso
Definition: dcmotor29.h:284
dcmotor29_cfg_setup
void dcmotor29_cfg_setup(dcmotor29_cfg_t *cfg)
DC Motor 29 configuration object setup function.
dcmotor29_register_write
err_t dcmotor29_register_write(dcmotor29_t *ctx, uint8_t reg, uint8_t data_in)
DC Motor 29 data register writing function.
dcmotor29_t::rst
digital_out_t rst
Definition: dcmotor29.h:259
dcmotor29_return_value_t
dcmotor29_return_value_t
DC Motor 29 Click return value data.
Definition: dcmotor29.h:311
dcmotor29_cfg_t::in1
pin_name_t in1
Definition: dcmotor29.h:294
dcmotor29_cfg_t::int_pin
pin_name_t int_pin
Definition: dcmotor29.h:295
dcmotor29_cfg_t
DC Motor 29 Click configuration object.
Definition: dcmotor29.h:282
dcmotor29_cfg_t::ip
pin_name_t ip
Definition: dcmotor29.h:292
dcmotor29_cfg_t::mosi
pin_name_t mosi
Definition: dcmotor29.h:285
dcmotor29_cfg_t::spi_speed
uint32_t spi_speed
Definition: dcmotor29.h:298
dcmotor29_cfg_t::sck
pin_name_t sck
Definition: dcmotor29.h:286
dcmotor29_drvoff_state
err_t dcmotor29_drvoff_state(dcmotor29_t *ctx, uint8_t drvoff_state)
DC Motor 29 control drvoff function.
dcmotor29_cfg_t::sda
pin_name_t sda
Definition: dcmotor29.h:289
dcmotor29_t::int_pin
digital_in_t int_pin
Definition: dcmotor29.h:264