stepper20  2.1.0.0
stepper20.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 STEPPER20_H
29 #define STEPPER20_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_i2c_master.h"
48 
69 #define STEPPER20_REG_INPUT 0x00
70 #define STEPPER20_REG_OUTPUT 0x01
71 #define STEPPER20_REG_POLARITY 0x02
72 #define STEPPER20_REG_CONFIG 0x03
73  // stepper20_reg
75 
90 #define STEPPER20_DMODE0_PIN 0x01
91 #define STEPPER20_DMODE1_PIN 0x02
92 #define STEPPER20_DMODE2_PIN 0x04
93 #define STEPPER20_SLEEP_X_PIN 0x08
94 #define STEPPER20_LO_PIN 0x10
95 #define STEPPER20_MO_PIN 0x20
96 
101 #define STEPPER20_DEFAULT_CONFIG 0xF0
102 
107 #define STEPPER20_DIR_CCW 0
108 #define STEPPER20_DIR_CW 1
109 
114 #define STEPPER20_PIN_LOW_LEVEL 0
115 #define STEPPER20_PIN_HIGH_LEVEL 1
116 
121 #define STEPPER20_MODE_FULL_STEP 0x00
122 #define STEPPER20_MODE_HALF_STEP 0x01
123 #define STEPPER20_MODE_QUARTER_STEP 0x02
124 #define STEPPER20_MODE_1_OVER_8 0x03
125 #define STEPPER20_MODE_1_OVER_16 0x04
126 #define STEPPER20_MODE_1_OVER_32 0x05
127 #define STEPPER20_MODE_MASK 0x07
128 
133 #define STEPPER20_SPEED_VERY_SLOW 0
134 #define STEPPER20_SPEED_SLOW 1
135 #define STEPPER20_SPEED_MEDIUM 2
136 #define STEPPER20_SPEED_FAST 3
137 #define STEPPER20_SPEED_VERY_FAST 4
138 
144 #define STEPPER20_DEVICE_ADDRESS_A1A0_00 0x70
145 #define STEPPER20_DEVICE_ADDRESS_A1A0_01 0x71
146 #define STEPPER20_DEVICE_ADDRESS_A1A0_10 0x72
147 #define STEPPER20_DEVICE_ADDRESS_A1A0_11 0x73
148  // stepper20_set
150 
165 #define STEPPER20_MAP_MIKROBUS( cfg, mikrobus ) \
166  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
167  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
168  cfg.en = MIKROBUS( mikrobus, MIKROBUS_CS ); \
169  cfg.dir = MIKROBUS( mikrobus, MIKROBUS_AN ); \
170  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
171  cfg.clk = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
172  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
173  // stepper20_map // stepper20
176 
181 typedef struct
182 {
183  // Output pins
184  digital_out_t en;
185  digital_out_t dir;
186  digital_out_t rst;
187  digital_out_t clk;
189  // Input pins
190  digital_in_t int_pin;
192  // Modules
193  i2c_master_t i2c;
195  // I2C slave address
196  uint8_t slave_address;
198 } stepper20_t;
199 
204 typedef struct
205 {
206  pin_name_t scl;
207  pin_name_t sda;
209  pin_name_t en;
210  pin_name_t dir;
211  pin_name_t rst;
212  pin_name_t clk;
213  pin_name_t int_pin;
215  uint32_t i2c_speed;
216  uint8_t i2c_address;
219 
224 typedef enum
225 {
227  STEPPER20_ERROR = -1
228 
230 
247 
262 
276 
290 err_t stepper20_write_register ( stepper20_t *ctx, uint8_t reg, uint8_t data_in );
291 
304 err_t stepper20_read_register ( stepper20_t *ctx, uint8_t reg, uint8_t *data_out );
305 
317 err_t stepper20_get_step_mode ( stepper20_t *ctx, uint8_t *mode );
318 
330 err_t stepper20_set_step_mode ( stepper20_t *ctx, uint8_t mode );
331 
344 err_t stepper20_get_sleep_x_pin ( stepper20_t *ctx, uint8_t *state );
345 
358 err_t stepper20_set_sleep_x_pin ( stepper20_t *ctx, uint8_t state );
359 
372 err_t stepper20_get_lo_pin ( stepper20_t *ctx, uint8_t *state );
373 
386 err_t stepper20_get_mo_pin ( stepper20_t *ctx, uint8_t *state );
387 
403 void stepper20_drive_motor ( stepper20_t *ctx, uint32_t steps, uint8_t speed );
404 
414 
424 
435 void stepper20_set_direction ( stepper20_t *ctx, uint8_t dir );
436 
446 
456 
466 
477 void stepper20_set_clk_pin ( stepper20_t *ctx, uint8_t state );
478 
479 #ifdef __cplusplus
480 }
481 #endif
482 #endif // STEPPER20_H
483  // stepper20
485 
486 // ------------------------------------------------------------------------ END
stepper20_read_register
err_t stepper20_read_register(stepper20_t *ctx, uint8_t reg, uint8_t *data_out)
Stepper 20 read register function.
stepper20_set_clk_pin
void stepper20_set_clk_pin(stepper20_t *ctx, uint8_t state)
Stepper 20 set clk pin function.
stepper20_set_direction
void stepper20_set_direction(stepper20_t *ctx, uint8_t dir)
Stepper 20 set direction function.
stepper20_cfg_t::rst
pin_name_t rst
Definition: stepper20.h:211
stepper20_return_value_t
stepper20_return_value_t
Stepper 20 Click return value data.
Definition: stepper20.h:225
stepper20_t::rst
digital_out_t rst
Definition: stepper20.h:186
stepper20_cfg_t::sda
pin_name_t sda
Definition: stepper20.h:207
stepper20_cfg_t::int_pin
pin_name_t int_pin
Definition: stepper20.h:213
stepper20_default_cfg
err_t stepper20_default_cfg(stepper20_t *ctx)
Stepper 20 default configuration function.
stepper20_t::int_pin
digital_in_t int_pin
Definition: stepper20.h:190
stepper20_get_step_mode
err_t stepper20_get_step_mode(stepper20_t *ctx, uint8_t *mode)
Stepper 20 get step mode function.
STEPPER20_OK
@ STEPPER20_OK
Definition: stepper20.h:226
stepper20_cfg_t::clk
pin_name_t clk
Definition: stepper20.h:212
stepper20_cfg_t::en
pin_name_t en
Definition: stepper20.h:209
stepper20_enable_device
void stepper20_enable_device(stepper20_t *ctx)
Stepper 20 enable device function.
stepper20_cfg_setup
void stepper20_cfg_setup(stepper20_cfg_t *cfg)
Stepper 20 configuration object setup function.
stepper20_get_lo_pin
err_t stepper20_get_lo_pin(stepper20_t *ctx, uint8_t *state)
Stepper 20 get lo pin function.
stepper20_t::clk
digital_out_t clk
Definition: stepper20.h:187
stepper20_get_mo_pin
err_t stepper20_get_mo_pin(stepper20_t *ctx, uint8_t *state)
Stepper 20 get mo pin function.
stepper20_get_int_pin
uint8_t stepper20_get_int_pin(stepper20_t *ctx)
Stepper 20 get int pin function.
stepper20_set_sleep_x_pin
err_t stepper20_set_sleep_x_pin(stepper20_t *ctx, uint8_t state)
Stepper 20 set sleep x pin function.
stepper20_cfg_t::i2c_address
uint8_t i2c_address
Definition: stepper20.h:216
stepper20_t::en
digital_out_t en
Definition: stepper20.h:184
STEPPER20_ERROR
@ STEPPER20_ERROR
Definition: stepper20.h:227
stepper20_write_register
err_t stepper20_write_register(stepper20_t *ctx, uint8_t reg, uint8_t data_in)
Stepper 20 write register function.
stepper20_switch_direction
void stepper20_switch_direction(stepper20_t *ctx)
Stepper 20 switch direction function.
stepper20_init
err_t stepper20_init(stepper20_t *ctx, stepper20_cfg_t *cfg)
Stepper 20 initialization function.
stepper20_cfg_t::dir
pin_name_t dir
Definition: stepper20.h:210
stepper20_get_sleep_x_pin
err_t stepper20_get_sleep_x_pin(stepper20_t *ctx, uint8_t *state)
Stepper 20 get sleep x pin function.
stepper20_set_step_mode
err_t stepper20_set_step_mode(stepper20_t *ctx, uint8_t mode)
Stepper 20 set step mode function.
stepper20_t::i2c
i2c_master_t i2c
Definition: stepper20.h:193
stepper20_reset_device
void stepper20_reset_device(stepper20_t *ctx)
Stepper 20 reset device function.
stepper20_t::dir
digital_out_t dir
Definition: stepper20.h:185
stepper20_t::slave_address
uint8_t slave_address
Definition: stepper20.h:196
stepper20_t
Stepper 20 Click context object.
Definition: stepper20.h:182
stepper20_drive_motor
void stepper20_drive_motor(stepper20_t *ctx, uint32_t steps, uint8_t speed)
Stepper 20 driver motor function.
stepper20_disable_device
void stepper20_disable_device(stepper20_t *ctx)
Stepper 20 disable device function.
stepper20_cfg_t::scl
pin_name_t scl
Definition: stepper20.h:206
stepper20_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: stepper20.h:215
stepper20_cfg_t
Stepper 20 Click configuration object.
Definition: stepper20.h:205