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
32extern "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
78 // 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
156 // 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
180 // stepper21_map
181 // stepper21
182
187typedef 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
204
209typedef 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
234
251
266
280
294err_t stepper21_write_register ( stepper21_t *ctx, uint8_t reg, uint8_t data_in );
295
308err_t stepper21_read_register ( stepper21_t *ctx, uint8_t reg, uint8_t *data_out );
309
321err_t stepper21_get_step_mode ( stepper21_t *ctx, uint8_t *mode );
322
334err_t stepper21_set_step_mode ( stepper21_t *ctx, uint8_t mode );
335
348err_t stepper21_get_sleep_pin ( stepper21_t *ctx, uint8_t *state );
349
362err_t stepper21_set_sleep_pin ( stepper21_t *ctx, uint8_t state );
363
376err_t stepper21_get_fault_pin ( stepper21_t *ctx, uint8_t *state );
377
391err_t stepper21_get_decay_pin ( stepper21_t *ctx, uint8_t *state );
392
406err_t stepper21_set_decay_pin ( stepper21_t *ctx, uint8_t state );
407
420err_t stepper21_get_enable_pin ( stepper21_t *ctx, uint8_t *state );
421
434err_t stepper21_set_enable_pin ( stepper21_t *ctx, uint8_t state );
435
448err_t stepper21_get_home_pin ( stepper21_t *ctx, uint8_t *state );
449
465void stepper21_drive_motor ( stepper21_t *ctx, uint32_t steps, uint8_t speed );
466
478
490
501void stepper21_set_direction ( stepper21_t *ctx, uint8_t dir );
502
512
523void stepper21_set_rst_pin ( stepper21_t *ctx, uint8_t state );
524
534
544
555void stepper21_set_step_pin ( stepper21_t *ctx, uint8_t state );
556
557#ifdef __cplusplus
558}
559#endif
560#endif // STEPPER21_H
561
562 // stepper21
563
564// ------------------------------------------------------------------------ END
err_t stepper21_default_cfg(stepper21_t *ctx)
Stepper 21 default configuration function.
void stepper21_set_direction(stepper21_t *ctx, uint8_t dir)
Stepper 21 set direction function.
err_t stepper21_set_sleep_pin(stepper21_t *ctx, uint8_t state)
Stepper 21 set sleep pin function.
err_t stepper21_get_step_mode(stepper21_t *ctx, uint8_t *mode)
Stepper 21 get step mode function.
err_t stepper21_get_decay_pin(stepper21_t *ctx, uint8_t *state)
Stepper 21 get decay pin function.
err_t stepper21_get_sleep_pin(stepper21_t *ctx, uint8_t *state)
Stepper 21 get sleep pin function.
void stepper21_set_rst_pin(stepper21_t *ctx, uint8_t state)
Stepper 21 set rst pin function.
void stepper21_set_step_pin(stepper21_t *ctx, uint8_t state)
Stepper 21 set step pin function.
uint8_t stepper21_get_int_pin(stepper21_t *ctx)
Stepper 21 get int pin function.
err_t stepper21_set_decay_pin(stepper21_t *ctx, uint8_t state)
Stepper 21 set decay pin function.
void stepper21_switch_direction(stepper21_t *ctx)
Stepper 21 switch direction function.
err_t stepper21_set_enable_pin(stepper21_t *ctx, uint8_t state)
Stepper 21 set enable pin function.
void stepper21_cfg_setup(stepper21_cfg_t *cfg)
Stepper 21 configuration object setup function.
err_t stepper21_init(stepper21_t *ctx, stepper21_cfg_t *cfg)
Stepper 21 initialization function.
err_t stepper21_enable_device(stepper21_t *ctx)
Stepper 21 enable device function.
void stepper21_reset_device(stepper21_t *ctx)
Stepper 21 reset device function.
err_t stepper21_write_register(stepper21_t *ctx, uint8_t reg, uint8_t data_in)
Stepper 21 write register function.
err_t stepper21_get_home_pin(stepper21_t *ctx, uint8_t *state)
Stepper 21 get home pin function.
err_t stepper21_set_step_mode(stepper21_t *ctx, uint8_t mode)
Stepper 21 set step mode function.
err_t stepper21_get_fault_pin(stepper21_t *ctx, uint8_t *state)
Stepper 21 get fault pin function.
void stepper21_drive_motor(stepper21_t *ctx, uint32_t steps, uint8_t speed)
Stepper 21 driver motor function.
err_t stepper21_disable_device(stepper21_t *ctx)
Stepper 21 disable device function.
err_t stepper21_read_register(stepper21_t *ctx, uint8_t reg, uint8_t *data_out)
Stepper 21 read register function.
err_t stepper21_get_enable_pin(stepper21_t *ctx, uint8_t *state)
Stepper 21 get enable pin function.
stepper21_return_value_t
Stepper 21 Click return value data.
Definition stepper21.h:229
@ STEPPER21_OK
Definition stepper21.h:230
@ STEPPER21_ERROR
Definition stepper21.h:231
Stepper 21 Click configuration object.
Definition stepper21.h:210
pin_name_t step
Definition stepper21.h:216
uint32_t i2c_speed
Definition stepper21.h:219
pin_name_t dir
Definition stepper21.h:214
pin_name_t scl
Definition stepper21.h:211
pin_name_t int_pin
Definition stepper21.h:217
pin_name_t sda
Definition stepper21.h:212
pin_name_t rst
Definition stepper21.h:215
uint8_t i2c_address
Definition stepper21.h:220
Stepper 21 Click context object.
Definition stepper21.h:188
digital_in_t int_pin
Definition stepper21.h:195
digital_out_t step
Definition stepper21.h:192
i2c_master_t i2c
Definition stepper21.h:198
digital_out_t rst
Definition stepper21.h:191
uint8_t slave_address
Definition stepper21.h:201
digital_out_t dir
Definition stepper21.h:190