stepper23  2.1.0.0
stepper23.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 STEPPER23_H
29 #define STEPPER23_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 STEPPER23_REG_INPUT_0 0x00
74 #define STEPPER23_REG_INPUT_1 0x01
75 #define STEPPER23_REG_OUTPUT_0 0x02
76 #define STEPPER23_REG_OUTPUT_1 0x03
77 #define STEPPER23_REG_POLARITY_0 0x04
78 #define STEPPER23_REG_POLARITY_1 0x05
79 #define STEPPER23_REG_CONFIG_0 0x06
80 #define STEPPER23_REG_CONFIG_1 0x07
81  // stepper23_reg
83 
98 #define STEPPER23_P0_DMODE0_PIN 0x01
99 #define STEPPER23_P0_DMODE1_PIN 0x02
100 #define STEPPER23_P0_DMODE2_PIN 0x04
101 #define STEPPER23_P0_DECAY1_PIN 0x08
102 #define STEPPER23_P0_DECAY2_PIN 0x10
103 #define STEPPER23_P0_TRQ0_PIN 0x20
104 #define STEPPER23_P0_TRQ1_PIN 0x40
105 #define STEPPER23_P0_SLEEP_X_PIN 0x80
106 #define STEPPER23_P1_LO1_PIN 0x01
107 #define STEPPER23_P1_LO2_PIN 0x02
108 #define STEPPER23_P1_MO_PIN 0x04
109 #define STEPPER23_P1_RST_PIN 0x08
110 
115 #define STEPPER23_P0_DEFAULT_CONFIG 0x00
116 #define STEPPER23_P1_DEFAULT_CONFIG 0xF7
117 
122 #define STEPPER23_DIR_CW 1
123 #define STEPPER23_DIR_CCW 0
124 
129 #define STEPPER23_PIN_LOW_LEVEL 0
130 #define STEPPER23_PIN_HIGH_LEVEL 1
131 
136 #define STEPPER23_MODE_FULL_STEP 0x00
137 #define STEPPER23_MODE_1_OVER_8 0x01
138 #define STEPPER23_MODE_HALF_STEP_TYPE_B 0x02
139 #define STEPPER23_MODE_1_OVER_32 0x03
140 #define STEPPER23_MODE_HALF_STEP_TYPE_A 0x04
141 #define STEPPER23_MODE_1_OVER_16 0x05
142 #define STEPPER23_MODE_QUARTER_STEP 0x06
143 #define STEPPER23_MODE_MASK 0x07
144 
149 #define STEPPER23_TORQUE_100_PCT 0x00
150 #define STEPPER23_TORQUE_75_PCT 0x01
151 #define STEPPER23_TORQUE_50_PCT 0x02
152 #define STEPPER23_TORQUE_25_PCT 0x03
153 #define STEPPER23_TORQUE_MASK 0x03
154 
159 #define STEPPER23_DECAY_MIXED 0x00
160 #define STEPPER23_DECAY_SLOW 0x01
161 #define STEPPER23_DECAY_FAST 0x02
162 #define STEPPER23_DECAY_ADMD 0x03
163 #define STEPPER23_DECAY_MASK 0x03
164 
169 #define STEPPER23_SPEED_VERY_SLOW 0
170 #define STEPPER23_SPEED_SLOW 1
171 #define STEPPER23_SPEED_MEDIUM 2
172 #define STEPPER23_SPEED_FAST 3
173 #define STEPPER23_SPEED_VERY_FAST 4
174 
180 #define STEPPER23_DEVICE_ADDRESS_A1A0_00 0x20
181 #define STEPPER23_DEVICE_ADDRESS_A1A0_01 0x21
182 #define STEPPER23_DEVICE_ADDRESS_A1A0_10 0x22
183 #define STEPPER23_DEVICE_ADDRESS_A1A0_11 0x23
184  // stepper23_set
186 
201 #define STEPPER23_MAP_MIKROBUS( cfg, mikrobus ) \
202  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
203  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
204  cfg.dir = MIKROBUS( mikrobus, MIKROBUS_AN ); \
205  cfg.en = MIKROBUS( mikrobus, MIKROBUS_RST ); \
206  cfg.clk = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
207  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
208  // stepper23_map // stepper23
211 
216 typedef struct
217 {
218  // Output pins
219  digital_out_t dir;
220  digital_out_t en;
221  digital_out_t clk;
223  // Input pins
224  digital_in_t int_pin;
226  // Modules
227  i2c_master_t i2c;
229  // I2C slave address
230  uint8_t slave_address;
232 } stepper23_t;
233 
238 typedef struct
239 {
240  pin_name_t scl;
241  pin_name_t sda;
243  pin_name_t dir;
244  pin_name_t en;
245  pin_name_t clk;
246  pin_name_t int_pin;
248  uint32_t i2c_speed;
249  uint8_t i2c_address;
252 
257 typedef enum
258 {
260  STEPPER23_ERROR = -1
261 
263 
280 
295 
309 
323 err_t stepper23_write_register ( stepper23_t *ctx, uint8_t reg, uint8_t data_in );
324 
337 err_t stepper23_read_register ( stepper23_t *ctx, uint8_t reg, uint8_t *data_out );
338 
350 err_t stepper23_get_step_mode ( stepper23_t *ctx, uint8_t *mode );
351 
363 err_t stepper23_set_step_mode ( stepper23_t *ctx, uint8_t mode );
364 
376 err_t stepper23_get_torque ( stepper23_t *ctx, uint8_t *torque );
377 
389 err_t stepper23_set_torque ( stepper23_t *ctx, uint8_t torque );
390 
402 err_t stepper23_get_decay ( stepper23_t *ctx, uint8_t *decay );
403 
415 err_t stepper23_set_decay ( stepper23_t *ctx, uint8_t decay );
416 
429 err_t stepper23_get_sleep_x_pin ( stepper23_t *ctx, uint8_t *state );
430 
443 err_t stepper23_set_sleep_x_pin ( stepper23_t *ctx, uint8_t state );
444 
457 err_t stepper23_get_lo1_pin ( stepper23_t *ctx, uint8_t *state );
458 
471 err_t stepper23_get_lo2_pin ( stepper23_t *ctx, uint8_t *state );
472 
485 err_t stepper23_get_mo_pin ( stepper23_t *ctx, uint8_t *state );
486 
499 err_t stepper23_get_rst_pin ( stepper23_t *ctx, uint8_t *state );
500 
513 err_t stepper23_set_rst_pin ( stepper23_t *ctx, uint8_t state );
514 
526 
542 void stepper23_drive_motor ( stepper23_t *ctx, uint32_t steps, uint8_t speed );
543 
553 
563 
574 void stepper23_set_direction ( stepper23_t *ctx, uint8_t dir );
575 
585 
595 
606 void stepper23_set_clk_pin ( stepper23_t *ctx, uint8_t state );
607 
608 #ifdef __cplusplus
609 }
610 #endif
611 #endif // STEPPER23_H
612  // stepper23
614 
615 // ------------------------------------------------------------------------ END
stepper23_cfg_setup
void stepper23_cfg_setup(stepper23_cfg_t *cfg)
Stepper 23 configuration object setup function.
stepper23_set_rst_pin
err_t stepper23_set_rst_pin(stepper23_t *ctx, uint8_t state)
Stepper 23 set RST pin function.
stepper23_set_direction
void stepper23_set_direction(stepper23_t *ctx, uint8_t dir)
Stepper 23 set direction function.
stepper23_cfg_t::scl
pin_name_t scl
Definition: stepper23.h:240
stepper23_cfg_t::sda
pin_name_t sda
Definition: stepper23.h:241
stepper23_default_cfg
err_t stepper23_default_cfg(stepper23_t *ctx)
Stepper 23 default configuration function.
stepper23_get_mo_pin
err_t stepper23_get_mo_pin(stepper23_t *ctx, uint8_t *state)
Stepper 23 get mo pin function.
stepper23_switch_direction
void stepper23_switch_direction(stepper23_t *ctx)
Stepper 23 switch direction function.
stepper23_read_register
err_t stepper23_read_register(stepper23_t *ctx, uint8_t reg, uint8_t *data_out)
Stepper 23 read register function.
stepper23_set_sleep_x_pin
err_t stepper23_set_sleep_x_pin(stepper23_t *ctx, uint8_t state)
Stepper 23 set sleep x pin function.
stepper23_t::clk
digital_out_t clk
Definition: stepper23.h:221
stepper23_set_decay
err_t stepper23_set_decay(stepper23_t *ctx, uint8_t decay)
Stepper 23 set decay function.
stepper23_t::int_pin
digital_in_t int_pin
Definition: stepper23.h:224
stepper23_cfg_t::clk
pin_name_t clk
Definition: stepper23.h:245
stepper23_drive_motor
void stepper23_drive_motor(stepper23_t *ctx, uint32_t steps, uint8_t speed)
Stepper 23 driver motor function.
STEPPER23_ERROR
@ STEPPER23_ERROR
Definition: stepper23.h:260
stepper23_get_int_pin
uint8_t stepper23_get_int_pin(stepper23_t *ctx)
Stepper 23 get int pin function.
stepper23_cfg_t::i2c_address
uint8_t i2c_address
Definition: stepper23.h:249
stepper23_get_torque
err_t stepper23_get_torque(stepper23_t *ctx, uint8_t *torque)
Stepper 23 get torque function.
stepper23_init
err_t stepper23_init(stepper23_t *ctx, stepper23_cfg_t *cfg)
Stepper 23 initialization function.
stepper23_get_lo1_pin
err_t stepper23_get_lo1_pin(stepper23_t *ctx, uint8_t *state)
Stepper 23 get lo1 pin function.
stepper23_enable_device
void stepper23_enable_device(stepper23_t *ctx)
Stepper 23 enable device function.
stepper23_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: stepper23.h:248
stepper23_set_clk_pin
void stepper23_set_clk_pin(stepper23_t *ctx, uint8_t state)
Stepper 23 set clk pin function.
stepper23_disable_device
void stepper23_disable_device(stepper23_t *ctx)
Stepper 23 disable device function.
stepper23_write_register
err_t stepper23_write_register(stepper23_t *ctx, uint8_t reg, uint8_t data_in)
Stepper 23 write register function.
stepper23_t::en
digital_out_t en
Definition: stepper23.h:220
stepper23_get_decay
err_t stepper23_get_decay(stepper23_t *ctx, uint8_t *decay)
Stepper 23 get decay function.
stepper23_cfg_t::en
pin_name_t en
Definition: stepper23.h:244
stepper23_t
Stepper 23 Click context object.
Definition: stepper23.h:217
stepper23_t::slave_address
uint8_t slave_address
Definition: stepper23.h:230
stepper23_get_sleep_x_pin
err_t stepper23_get_sleep_x_pin(stepper23_t *ctx, uint8_t *state)
Stepper 23 get sleep x pin function.
stepper23_reset_device
err_t stepper23_reset_device(stepper23_t *ctx)
Stepper 23 reset device function.
stepper23_return_value_t
stepper23_return_value_t
Stepper 23 Click return value data.
Definition: stepper23.h:258
STEPPER23_OK
@ STEPPER23_OK
Definition: stepper23.h:259
stepper23_get_step_mode
err_t stepper23_get_step_mode(stepper23_t *ctx, uint8_t *mode)
Stepper 23 get step mode function.
stepper23_t::dir
digital_out_t dir
Definition: stepper23.h:219
stepper23_get_lo2_pin
err_t stepper23_get_lo2_pin(stepper23_t *ctx, uint8_t *state)
Stepper 23 get lo2 pin function.
stepper23_get_rst_pin
err_t stepper23_get_rst_pin(stepper23_t *ctx, uint8_t *state)
Stepper 23 get RST pin function.
stepper23_cfg_t::dir
pin_name_t dir
Definition: stepper23.h:243
stepper23_set_torque
err_t stepper23_set_torque(stepper23_t *ctx, uint8_t torque)
Stepper 23 set torque function.
stepper23_t::i2c
i2c_master_t i2c
Definition: stepper23.h:227
stepper23_set_step_mode
err_t stepper23_set_step_mode(stepper23_t *ctx, uint8_t mode)
Stepper 23 set step mode function.
stepper23_cfg_t
Stepper 23 Click configuration object.
Definition: stepper23.h:239
stepper23_cfg_t::int_pin
pin_name_t int_pin
Definition: stepper23.h:246