stepper21  2.1.0.0
stepper21.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 STEPPER21_H
29 #define STEPPER21_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_i2c_master.h"
38 
59 #define STEPPER21_REG_INPUT 0x00
60 #define STEPPER21_REG_OUTPUT 0x01
61 #define STEPPER21_REG_POLARITY 0x02
62 #define STEPPER21_REG_CONFIG 0x03
63  // stepper21_reg
65 
80 #define STEPPER21_PIN_SLEEP 0x01
81 #define STEPPER21_PIN_FAULT 0x02
82 #define STEPPER21_PIN_DECAY 0x04
83 #define STEPPER21_PIN_ENABLE 0x08
84 #define STEPPER21_PIN_HOME 0x10
85 #define STEPPER21_PIN_MODE0 0x20
86 #define STEPPER21_PIN_MODE1 0x40
87 #define STEPPER21_PIN_MODE2 0x80
88 
93 #define STEPPER21_DEFAULT_CONFIG 0x16
94 
99 #define STEPPER21_DIR_CCW 0
100 #define STEPPER21_DIR_CW 1
101 
106 #define STEPPER21_PIN_STATE_LOW 0
107 #define STEPPER21_PIN_STATE_HIGH 1
108 #define STEPPER21_PIN_STATE_OPEN 2
109 
114 #define STEPPER21_MODE_FULL_STEP 0x00
115 #define STEPPER21_MODE_HALF_STEP 0x01
116 #define STEPPER21_MODE_QUARTER_STEP 0x02
117 #define STEPPER21_MODE_1_OVER_8 0x03
118 #define STEPPER21_MODE_1_OVER_16 0x04
119 #define STEPPER21_MODE_1_OVER_32 0x05
120 #define STEPPER21_MODE_MASK 0x07
121 
126 #define STEPPER21_SPEED_VERY_SLOW 0
127 #define STEPPER21_SPEED_SLOW 1
128 #define STEPPER21_SPEED_MEDIUM 2
129 #define STEPPER21_SPEED_FAST 3
130 #define STEPPER21_SPEED_VERY_FAST 4
131 
137 #define STEPPER21_DEVICE_ADDRESS_A1A0_00 0x70
138 #define STEPPER21_DEVICE_ADDRESS_A1A0_01 0x71
139 #define STEPPER21_DEVICE_ADDRESS_A1A0_10 0x72
140 #define STEPPER21_DEVICE_ADDRESS_A1A0_11 0x73
141  // stepper21_set
143 
158 #define STEPPER21_MAP_MIKROBUS( cfg, mikrobus ) \
159  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
160  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
161  cfg.dir = MIKROBUS( mikrobus, MIKROBUS_AN ); \
162  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
163  cfg.step = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
164  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
165  // stepper21_map // stepper21
168 
173 typedef struct
174 {
175  // Output pins
176  digital_out_t dir;
177  digital_out_t rst;
178  digital_out_t step;
180  // Input pins
181  digital_in_t int_pin;
183  // Modules
184  i2c_master_t i2c;
186  // I2C slave address
187  uint8_t slave_address;
189 } stepper21_t;
190 
195 typedef struct
196 {
197  pin_name_t scl;
198  pin_name_t sda;
200  pin_name_t dir;
201  pin_name_t rst;
202  pin_name_t step;
203  pin_name_t int_pin;
205  uint32_t i2c_speed;
206  uint8_t i2c_address;
209 
214 typedef enum
215 {
217  STEPPER21_ERROR = -1
218 
220 
237 
252 
266 
280 err_t stepper21_write_register ( stepper21_t *ctx, uint8_t reg, uint8_t data_in );
281 
294 err_t stepper21_read_register ( stepper21_t *ctx, uint8_t reg, uint8_t *data_out );
295 
307 err_t stepper21_get_step_mode ( stepper21_t *ctx, uint8_t *mode );
308 
320 err_t stepper21_set_step_mode ( stepper21_t *ctx, uint8_t mode );
321 
334 err_t stepper21_get_sleep_pin ( stepper21_t *ctx, uint8_t *state );
335 
348 err_t stepper21_set_sleep_pin ( stepper21_t *ctx, uint8_t state );
349 
362 err_t stepper21_get_fault_pin ( stepper21_t *ctx, uint8_t *state );
363 
377 err_t stepper21_get_decay_pin ( stepper21_t *ctx, uint8_t *state );
378 
392 err_t stepper21_set_decay_pin ( stepper21_t *ctx, uint8_t state );
393 
406 err_t stepper21_get_enable_pin ( stepper21_t *ctx, uint8_t *state );
407 
420 err_t stepper21_set_enable_pin ( stepper21_t *ctx, uint8_t state );
421 
434 err_t stepper21_get_home_pin ( stepper21_t *ctx, uint8_t *state );
435 
451 void stepper21_drive_motor ( stepper21_t *ctx, uint32_t steps, uint8_t speed );
452 
464 
476 
487 void stepper21_set_direction ( stepper21_t *ctx, uint8_t dir );
488 
498 
509 void stepper21_set_rst_pin ( stepper21_t *ctx, uint8_t state );
510 
520 
530 
541 void stepper21_set_step_pin ( stepper21_t *ctx, uint8_t state );
542 
543 #ifdef __cplusplus
544 }
545 #endif
546 #endif // STEPPER21_H
547  // stepper21
549 
550 // ------------------------------------------------------------------------ END
stepper21_init
err_t stepper21_init(stepper21_t *ctx, stepper21_cfg_t *cfg)
Stepper 21 initialization function.
stepper21_cfg_t::step
pin_name_t step
Definition: stepper21.h:202
stepper21_cfg_t::int_pin
pin_name_t int_pin
Definition: stepper21.h:203
stepper21_set_enable_pin
err_t stepper21_set_enable_pin(stepper21_t *ctx, uint8_t state)
Stepper 21 set enable pin function.
stepper21_return_value_t
stepper21_return_value_t
Stepper 21 Click return value data.
Definition: stepper21.h:215
stepper21_get_home_pin
err_t stepper21_get_home_pin(stepper21_t *ctx, uint8_t *state)
Stepper 21 get home pin function.
stepper21_t::i2c
i2c_master_t i2c
Definition: stepper21.h:184
stepper21_set_rst_pin
void stepper21_set_rst_pin(stepper21_t *ctx, uint8_t state)
Stepper 21 set rst pin function.
stepper21_t::slave_address
uint8_t slave_address
Definition: stepper21.h:187
stepper21_get_fault_pin
err_t stepper21_get_fault_pin(stepper21_t *ctx, uint8_t *state)
Stepper 21 get fault pin function.
stepper21_t::step
digital_out_t step
Definition: stepper21.h:178
stepper21_get_int_pin
uint8_t stepper21_get_int_pin(stepper21_t *ctx)
Stepper 21 get int pin function.
stepper21_t::dir
digital_out_t dir
Definition: stepper21.h:176
stepper21_get_step_mode
err_t stepper21_get_step_mode(stepper21_t *ctx, uint8_t *mode)
Stepper 21 get step mode function.
stepper21_cfg_t::rst
pin_name_t rst
Definition: stepper21.h:201
stepper21_set_direction
void stepper21_set_direction(stepper21_t *ctx, uint8_t dir)
Stepper 21 set direction function.
stepper21_switch_direction
void stepper21_switch_direction(stepper21_t *ctx)
Stepper 21 switch direction function.
STEPPER21_ERROR
@ STEPPER21_ERROR
Definition: stepper21.h:217
stepper21_set_step_mode
err_t stepper21_set_step_mode(stepper21_t *ctx, uint8_t mode)
Stepper 21 set step mode function.
stepper21_drive_motor
void stepper21_drive_motor(stepper21_t *ctx, uint32_t steps, uint8_t speed)
Stepper 21 driver motor function.
stepper21_set_decay_pin
err_t stepper21_set_decay_pin(stepper21_t *ctx, uint8_t state)
Stepper 21 set decay pin function.
stepper21_get_decay_pin
err_t stepper21_get_decay_pin(stepper21_t *ctx, uint8_t *state)
Stepper 21 get decay pin function.
stepper21_cfg_t::i2c_address
uint8_t i2c_address
Definition: stepper21.h:206
stepper21_set_step_pin
void stepper21_set_step_pin(stepper21_t *ctx, uint8_t state)
Stepper 21 set step pin function.
stepper21_cfg_t::dir
pin_name_t dir
Definition: stepper21.h:200
stepper21_default_cfg
err_t stepper21_default_cfg(stepper21_t *ctx)
Stepper 21 default configuration function.
stepper21_read_register
err_t stepper21_read_register(stepper21_t *ctx, uint8_t reg, uint8_t *data_out)
Stepper 21 read register function.
stepper21_write_register
err_t stepper21_write_register(stepper21_t *ctx, uint8_t reg, uint8_t data_in)
Stepper 21 write register function.
stepper21_t::int_pin
digital_in_t int_pin
Definition: stepper21.h:181
stepper21_t::rst
digital_out_t rst
Definition: stepper21.h:177
stepper21_get_sleep_pin
err_t stepper21_get_sleep_pin(stepper21_t *ctx, uint8_t *state)
Stepper 21 get sleep pin function.
stepper21_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: stepper21.h:205
stepper21_get_enable_pin
err_t stepper21_get_enable_pin(stepper21_t *ctx, uint8_t *state)
Stepper 21 get enable pin function.
stepper21_cfg_t::sda
pin_name_t sda
Definition: stepper21.h:198
stepper21_disable_device
err_t stepper21_disable_device(stepper21_t *ctx)
Stepper 21 disable device function.
STEPPER21_OK
@ STEPPER21_OK
Definition: stepper21.h:216
stepper21_reset_device
void stepper21_reset_device(stepper21_t *ctx)
Stepper 21 reset device function.
stepper21_set_sleep_pin
err_t stepper21_set_sleep_pin(stepper21_t *ctx, uint8_t state)
Stepper 21 set sleep pin function.
stepper21_cfg_t
Stepper 21 Click configuration object.
Definition: stepper21.h:196
stepper21_cfg_setup
void stepper21_cfg_setup(stepper21_cfg_t *cfg)
Stepper 21 configuration object setup function.
stepper21_t
Stepper 21 Click context object.
Definition: stepper21.h:174
stepper21_enable_device
err_t stepper21_enable_device(stepper21_t *ctx)
Stepper 21 enable device function.
stepper21_cfg_t::scl
pin_name_t scl
Definition: stepper21.h:197