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 
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 STEPPER21_REG_INPUT 0x00
74 #define STEPPER21_REG_OUTPUT 0x01
75 #define STEPPER21_REG_POLARITY 0x02
76 #define STEPPER21_REG_CONFIG 0x03
77  // stepper21_reg
79 
94 #define STEPPER21_PIN_SLEEP 0x01
95 #define STEPPER21_PIN_FAULT 0x02
96 #define STEPPER21_PIN_DECAY 0x04
97 #define STEPPER21_PIN_ENABLE 0x08
98 #define STEPPER21_PIN_HOME 0x10
99 #define STEPPER21_PIN_MODE0 0x20
100 #define STEPPER21_PIN_MODE1 0x40
101 #define STEPPER21_PIN_MODE2 0x80
102 
107 #define STEPPER21_DEFAULT_CONFIG 0x16
108 
113 #define STEPPER21_DIR_CCW 0
114 #define STEPPER21_DIR_CW 1
115 
120 #define STEPPER21_PIN_STATE_LOW 0
121 #define STEPPER21_PIN_STATE_HIGH 1
122 #define STEPPER21_PIN_STATE_OPEN 2
123 
128 #define STEPPER21_MODE_FULL_STEP 0x00
129 #define STEPPER21_MODE_HALF_STEP 0x01
130 #define STEPPER21_MODE_QUARTER_STEP 0x02
131 #define STEPPER21_MODE_1_OVER_8 0x03
132 #define STEPPER21_MODE_1_OVER_16 0x04
133 #define STEPPER21_MODE_1_OVER_32 0x05
134 #define STEPPER21_MODE_MASK 0x07
135 
140 #define STEPPER21_SPEED_VERY_SLOW 0
141 #define STEPPER21_SPEED_SLOW 1
142 #define STEPPER21_SPEED_MEDIUM 2
143 #define STEPPER21_SPEED_FAST 3
144 #define STEPPER21_SPEED_VERY_FAST 4
145 
151 #define STEPPER21_DEVICE_ADDRESS_A1A0_00 0x70
152 #define STEPPER21_DEVICE_ADDRESS_A1A0_01 0x71
153 #define STEPPER21_DEVICE_ADDRESS_A1A0_10 0x72
154 #define STEPPER21_DEVICE_ADDRESS_A1A0_11 0x73
155  // stepper21_set
157 
172 #define STEPPER21_MAP_MIKROBUS( cfg, mikrobus ) \
173  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
174  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
175  cfg.dir = MIKROBUS( mikrobus, MIKROBUS_AN ); \
176  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
177  cfg.step = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
178  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
179  // stepper21_map // stepper21
182 
187 typedef struct
188 {
189  // Output pins
190  digital_out_t dir;
191  digital_out_t rst;
192  digital_out_t step;
194  // Input pins
195  digital_in_t int_pin;
197  // Modules
198  i2c_master_t i2c;
200  // I2C slave address
201  uint8_t slave_address;
203 } stepper21_t;
204 
209 typedef struct
210 {
211  pin_name_t scl;
212  pin_name_t sda;
214  pin_name_t dir;
215  pin_name_t rst;
216  pin_name_t step;
217  pin_name_t int_pin;
219  uint32_t i2c_speed;
220  uint8_t i2c_address;
223 
228 typedef enum
229 {
231  STEPPER21_ERROR = -1
232 
234 
251 
266 
280 
294 err_t stepper21_write_register ( stepper21_t *ctx, uint8_t reg, uint8_t data_in );
295 
308 err_t stepper21_read_register ( stepper21_t *ctx, uint8_t reg, uint8_t *data_out );
309 
321 err_t stepper21_get_step_mode ( stepper21_t *ctx, uint8_t *mode );
322 
334 err_t stepper21_set_step_mode ( stepper21_t *ctx, uint8_t mode );
335 
348 err_t stepper21_get_sleep_pin ( stepper21_t *ctx, uint8_t *state );
349 
362 err_t stepper21_set_sleep_pin ( stepper21_t *ctx, uint8_t state );
363 
376 err_t stepper21_get_fault_pin ( stepper21_t *ctx, uint8_t *state );
377 
391 err_t stepper21_get_decay_pin ( stepper21_t *ctx, uint8_t *state );
392 
406 err_t stepper21_set_decay_pin ( stepper21_t *ctx, uint8_t state );
407 
420 err_t stepper21_get_enable_pin ( stepper21_t *ctx, uint8_t *state );
421 
434 err_t stepper21_set_enable_pin ( stepper21_t *ctx, uint8_t state );
435 
448 err_t stepper21_get_home_pin ( stepper21_t *ctx, uint8_t *state );
449 
465 void stepper21_drive_motor ( stepper21_t *ctx, uint32_t steps, uint8_t speed );
466 
478 
490 
501 void stepper21_set_direction ( stepper21_t *ctx, uint8_t dir );
502 
512 
523 void stepper21_set_rst_pin ( stepper21_t *ctx, uint8_t state );
524 
534 
544 
555 void stepper21_set_step_pin ( stepper21_t *ctx, uint8_t state );
556 
557 #ifdef __cplusplus
558 }
559 #endif
560 #endif // STEPPER21_H
561  // stepper21
563 
564 // ------------------------------------------------------------------------ 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:216
stepper21_cfg_t::int_pin
pin_name_t int_pin
Definition: stepper21.h:217
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:229
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:198
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:201
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:192
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:190
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:215
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:231
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:220
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:214
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:195
stepper21_t::rst
digital_out_t rst
Definition: stepper21.h:191
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:219
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:212
stepper21_disable_device
err_t stepper21_disable_device(stepper21_t *ctx)
Stepper 21 disable device function.
STEPPER21_OK
@ STEPPER21_OK
Definition: stepper21.h:230
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:210
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:188
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:211