stepper14  2.1.0.0
stepper14.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 STEPPER14_H
29 #define STEPPER14_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_i2c_master.h"
52 
73 #define STEPPER14_PCA9538A_REG_INPUT 0x00
74 #define STEPPER14_PCA9538A_REG_OUTPUT 0x01
75 #define STEPPER14_PCA9538A_REG_POLARITY 0x02
76 #define STEPPER14_PCA9538A_REG_CONFIG 0x03
77  // stepper14_reg
79 
94 #define STEPPER14_PIN_IN1 0x01
95 #define STEPPER14_PIN_IN2 0x02
96 #define STEPPER14_PIN_MODE 0x04
97 #define STEPPER14_PIN_IN3 0x08
98 #define STEPPER14_PIN_IN4 0x10
99 #define STEPPER14_PIN_TRQ 0x20
100 
105 #define STEPPER14_PCA9538A_DEFAULT_CONFIG 0xC0
106 
111 #define STEPPER14_DIR_CW 0
112 #define STEPPER14_DIR_CCW 1
113 
118 #define STEPPER14_PIN_STATE_LOW 0
119 #define STEPPER14_PIN_STATE_HIGH 1
120 
125 #define STEPPER14_INTERFACE_4_PIN 0
126 #define STEPPER14_INTERFACE_2_PIN 1
127 
132 #define STEPPER14_SPEED_VERY_SLOW 0
133 #define STEPPER14_SPEED_SLOW 1
134 #define STEPPER14_SPEED_MEDIUM 2
135 #define STEPPER14_SPEED_FAST 3
136 #define STEPPER14_SPEED_VERY_FAST 4
137 
142 #define STEPPER14_MODE_FULL_STEP 0
143 #define STEPPER14_MODE_HALF_STEP 1
144 
150 #define STEPPER14_DEVICE_ADDRESS_A1A0_00 0x70
151 #define STEPPER14_DEVICE_ADDRESS_A1A0_01 0x71
152 #define STEPPER14_DEVICE_ADDRESS_A1A0_10 0x72
153 #define STEPPER14_DEVICE_ADDRESS_A1A0_11 0x73
154  // stepper14_set
156 
171 #define STEPPER14_MAP_MIKROBUS( cfg, mikrobus ) \
172  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
173  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
174  cfg.en = MIKROBUS( mikrobus, MIKROBUS_CS ); \
175  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
176  cfg.flt = MIKROBUS( mikrobus, MIKROBUS_INT )
177  // stepper14_map // stepper14
180 
185 typedef struct
186 {
187  // Output pins
188  digital_out_t en;
189  digital_out_t rst;
191  // Input pins
192  digital_in_t flt;
194  // Modules
195  i2c_master_t i2c;
197  // I2C slave address
198  uint8_t slave_address;
200  uint8_t pin_mode;
201  uint8_t step_mode;
202  uint8_t direction;
204 } stepper14_t;
205 
210 typedef struct
211 {
212  pin_name_t scl;
213  pin_name_t sda;
215  pin_name_t en;
216  pin_name_t rst;
217  pin_name_t flt;
219  uint32_t i2c_speed;
220  uint8_t i2c_address;
223 
228 typedef enum
229 {
231  STEPPER14_ERROR = -1
232 
234 
251 
266 
280 
294 err_t stepper14_pca_write_reg ( stepper14_t *ctx, uint8_t reg, uint8_t data_in );
295 
308 err_t stepper14_pca_read_reg ( stepper14_t *ctx, uint8_t reg, uint8_t *data_out );
309 
320 void stepper14_set_rst_pin ( stepper14_t *ctx, uint8_t state );
321 
331 
341 
351 
361 
374 err_t stepper14_set_in1_pin ( stepper14_t *ctx, uint8_t state );
375 
388 err_t stepper14_set_in2_pin ( stepper14_t *ctx, uint8_t state );
389 
402 err_t stepper14_set_in3_pin ( stepper14_t *ctx, uint8_t state );
403 
416 err_t stepper14_set_in4_pin ( stepper14_t *ctx, uint8_t state );
417 
430 err_t stepper14_set_mode_pin ( stepper14_t *ctx, uint8_t state );
431 
444 err_t stepper14_set_trq_pin ( stepper14_t *ctx, uint8_t state );
445 
459 err_t stepper14_set_interface_mode ( stepper14_t *ctx, uint8_t mode );
460 
472 void stepper14_set_step_mode ( stepper14_t *ctx, uint8_t mode );
473 
484 void stepper14_set_direction ( stepper14_t *ctx, uint8_t dir );
485 
495 
513 err_t stepper14_drive_motor ( stepper14_t *ctx, uint32_t steps, uint8_t speed );
514 
515 #ifdef __cplusplus
516 }
517 #endif
518 #endif // STEPPER14_H
519  // stepper14
521 
522 // ------------------------------------------------------------------------ END
stepper14_get_fault_pin
uint8_t stepper14_get_fault_pin(stepper14_t *ctx)
Stepper 14 get FAULT pin function.
stepper14_switch_direction
void stepper14_switch_direction(stepper14_t *ctx)
Stepper 14 switch direction function.
STEPPER14_OK
@ STEPPER14_OK
Definition: stepper14.h:230
stepper14_t::direction
uint8_t direction
Definition: stepper14.h:202
stepper14_cfg_t::sda
pin_name_t sda
Definition: stepper14.h:213
stepper14_set_in3_pin
err_t stepper14_set_in3_pin(stepper14_t *ctx, uint8_t state)
Stepper 14 set IN3 pin function.
stepper14_t::slave_address
uint8_t slave_address
Definition: stepper14.h:198
stepper14_cfg_t::en
pin_name_t en
Definition: stepper14.h:215
stepper14_cfg_t::rst
pin_name_t rst
Definition: stepper14.h:216
stepper14_set_rst_pin
void stepper14_set_rst_pin(stepper14_t *ctx, uint8_t state)
Stepper 14 set rst pin function.
stepper14_t::en
digital_out_t en
Definition: stepper14.h:188
stepper14_set_in2_pin
err_t stepper14_set_in2_pin(stepper14_t *ctx, uint8_t state)
Stepper 14 set IN2 pin function.
stepper14_t
Stepper 14 Click context object.
Definition: stepper14.h:186
stepper14_t::flt
digital_in_t flt
Definition: stepper14.h:192
stepper14_disable_device
void stepper14_disable_device(stepper14_t *ctx)
Stepper 14 disable device function.
stepper14_reset_pca
void stepper14_reset_pca(stepper14_t *ctx)
Stepper 14 reset device function.
stepper14_return_value_t
stepper14_return_value_t
Stepper 14 Click return value data.
Definition: stepper14.h:229
stepper14_enable_device
void stepper14_enable_device(stepper14_t *ctx)
Stepper 14 enable device function.
stepper14_set_mode_pin
err_t stepper14_set_mode_pin(stepper14_t *ctx, uint8_t state)
Stepper 14 set MODE pin function.
stepper14_t::step_mode
uint8_t step_mode
Definition: stepper14.h:201
stepper14_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: stepper14.h:219
stepper14_t::pin_mode
uint8_t pin_mode
Definition: stepper14.h:200
stepper14_t::i2c
i2c_master_t i2c
Definition: stepper14.h:195
stepper14_set_in4_pin
err_t stepper14_set_in4_pin(stepper14_t *ctx, uint8_t state)
Stepper 14 set IN4 pin function.
stepper14_set_direction
void stepper14_set_direction(stepper14_t *ctx, uint8_t dir)
Stepper 14 set direction function.
stepper14_cfg_t::scl
pin_name_t scl
Definition: stepper14.h:212
STEPPER14_ERROR
@ STEPPER14_ERROR
Definition: stepper14.h:231
stepper14_set_trq_pin
err_t stepper14_set_trq_pin(stepper14_t *ctx, uint8_t state)
Stepper 14 set TRQ pin function.
stepper14_default_cfg
err_t stepper14_default_cfg(stepper14_t *ctx)
Stepper 14 default configuration function.
stepper14_pca_write_reg
err_t stepper14_pca_write_reg(stepper14_t *ctx, uint8_t reg, uint8_t data_in)
Stepper 14 pca write reg function.
stepper14_drive_motor
err_t stepper14_drive_motor(stepper14_t *ctx, uint32_t steps, uint8_t speed)
Stepper 14 driver motor function.
stepper14_init
err_t stepper14_init(stepper14_t *ctx, stepper14_cfg_t *cfg)
Stepper 14 initialization function.
stepper14_cfg_t
Stepper 14 Click configuration object.
Definition: stepper14.h:211
stepper14_set_step_mode
void stepper14_set_step_mode(stepper14_t *ctx, uint8_t mode)
Stepper 14 set step mode function.
stepper14_cfg_setup
void stepper14_cfg_setup(stepper14_cfg_t *cfg)
Stepper 14 configuration object setup function.
stepper14_pca_read_reg
err_t stepper14_pca_read_reg(stepper14_t *ctx, uint8_t reg, uint8_t *data_out)
Stepper 14 pca read reg function.
stepper14_cfg_t::flt
pin_name_t flt
Definition: stepper14.h:217
stepper14_set_in1_pin
err_t stepper14_set_in1_pin(stepper14_t *ctx, uint8_t state)
Stepper 14 set IN1 pin function.
stepper14_cfg_t::i2c_address
uint8_t i2c_address
Definition: stepper14.h:220
stepper14_t::rst
digital_out_t rst
Definition: stepper14.h:189
stepper14_set_interface_mode
err_t stepper14_set_interface_mode(stepper14_t *ctx, uint8_t mode)
Stepper 14 set interface mode function.