stepper15  2.0.0.0
stepper15.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 STEPPER15_H
29 #define STEPPER15_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 #include "drv_spi_master.h"
53 
54 
75 #define STEPPER15_REG_INPUT_PORT 0x00
76 #define STEPPER15_REG_OUTPUT_PORT 0x01
77 #define STEPPER15_REG_POLARITY_INVERSION 0x02
78 #define STEPPER15_REG_CONFIGURATION 0x03
79 
84 #define STEPPER15_REG_FAULT_STATUS 0x00
85 #define STEPPER15_REG_DIAG_STATUS_1 0x01
86 #define STEPPER15_REG_DIAG_STATUS_2 0x02
87 #define STEPPER15_REG_CTRL_1 0x03
88 #define STEPPER15_REG_CTRL_2 0x04
89 #define STEPPER15_REG_CTRL_3 0x05
90 #define STEPPER15_REG_CTRL_4 0x06
91 #define STEPPER15_REG_CTRL_5 0x07
92 #define STEPPER15_REG_CTRL_6 0x08
93 #define STEPPER15_REG_CTRL_7 0x09
94 #define STEPPER15_REG_CTRL_8 0x0A
95  // stepper15_reg
97 
112 #define STEPPER15_SET_DIRECTION_BIT_MASK 0xFB
113 #define STEPPER15_SET_DIRECTION_CCW 0x00
114 #define STEPPER15_SET_DIRECTION_CW 0x04
115 
120 #define STEPPER15_SET_WORK_MODE_BIT_MASK 0xFE
121 #define STEPPER15_SET_WORK_MODE_SLEEP 0x00
122 #define STEPPER15_SET_WORK_MODE_ENABLE_DEVICE 0x01
123 
128 #define STEPPER15_SET_OUT_MODE_BIT_MASK 0xFD
129 #define STEPPER15_SET_OUT_MODE_DEVICE_OUTPUTS_ENABLE 0x00
130 #define STEPPER15_SET_OUT_MODE_DEVICE_OUTPUTS_DISABLE 0x02
131 
136 #define STEPPER15_SET_CONFIG_DEFAULT 0xF8
137 
142 #define STEPPER15_FAULT_ST_OL_MASK 0x01
143 #define STEPPER15_FAULT_ST_OT_MASK 0x02
144 #define STEPPER15_FAULT_ST_STL_SLIP_MASK 0x04
145 #define STEPPER15_FAULT_ST_OCP_MASK 0x08
146 #define STEPPER15_FAULT_ST_CPUV_MASK 0x10
147 #define STEPPER15_FAULT_ST_UVLO_MASK 0x20
148 #define STEPPER15_FAULT_ST_SPI_ERROR_MASK 0x40
149 #define STEPPER15_FAULT_ST_FAULT_MASK 0x80
150 
155 #define STEPPER15_DIAG_1_OCP_HS1_A_MASK 0x01
156 #define STEPPER15_DIAG_1_OCP_LS1_A_MASK 0x02
157 #define STEPPER15_DIAG_1_OCP_HS2_A_MASK 0x04
158 #define STEPPER15_DIAG_1_OCP_LS2_A_MASK 0x08
159 #define STEPPER15_DIAG_1_OCP_HS1_B_MASK 0x10
160 #define STEPPER15_DIAG_1_OCP_LS1_B_MASK 0x20
161 #define STEPPER15_DIAG_1_OCP_HS2_B_MASK 0x40
162 #define STEPPER15_DIAG_1_OCP_LS2_B_MASK 0x80
163 
169 #define STEPPER15_DIAG_2_OL_A_MASK 0x01
170 #define STEPPER15_DIAG_2_OL_B_MASK 0x02
171 #define STEPPER15_DIAG_2_SLIP_MASK 0x04
172 #define STEPPER15_DIAG_2_STALL_MASK 0x08
173 #define STEPPER15_DIAG_2_STL_LRN_OK_MASK 0x10
174 #define STEPPER15_DIAG_2_OTS_MASK 0x20
175 #define STEPPER15_DIAG_2_OTW_MASK 0x40
176 #define STEPPER15_DIAG_2_UTW_MASK 0x80
177 
183 #define STEPPER15_CTRL1_SLEW_RATE_MASK 0x03
184 #define STEPPER15_CTRL1_EN_NSR_MASK 0x04
185 #define STEPPER15_CTRL1_RSVD1_MASK 0x08
186 #define STEPPER15_CTRL1_TRQ_DAC_MASK 0xF0
187 #define STEPPER15_SET_CTRL1_TRQ_100 0x00
188 #define STEPPER15_SET_CTRL1_TRQ_93_75 0x10
189 #define STEPPER15_SET_CTRL1_TRQ_87_5 0x20
190 #define STEPPER15_SET_CTRL1_TRQ_81_25 0x30
191 #define STEPPER15_SET_CTRL1_TRQ_75 0x40
192 #define STEPPER15_SET_CTRL1_TRQ_68_75 0x50
193 #define STEPPER15_SET_CTRL1_TRQ_62_5 0x60
194 #define STEPPER15_SET_CTRL1_TRQ_56_25 0x70
195 #define STEPPER15_SET_CTRL1_TRQ_50 0x80
196 #define STEPPER15_SET_CTRL1_TRQ_43_75 0x90
197 #define STEPPER15_SET_CTRL1_TRQ_37_5 0xA0
198 #define STEPPER15_SET_CTRL1_TRQ_31_25 0xB0
199 #define STEPPER15_SET_CTRL1_TRQ_25 0xC0
200 #define STEPPER15_SET_CTRL1_TRQ_18_75 0xD0
201 #define STEPPER15_SET_CTRL1_TRQ_12_5 0xE0
202 #define STEPPER15_SET_CTRL1_TRQ_6_25 0xF0
203 
204 
210 #define STEPPER15_CTRL2_DECAY_MASK 0x07
211 #define STEPPER15_CTRL2_TOFF_MASK 0x18
212 #define STEPPER15_CTRL2_RSVD2_MASK 0x60
213 #define STEPPER15_CTRL2_DIS_OUT_MASK 0x80
214 #define STEPPER15_SET_CTRL2_OUTPUT_ENABLE 0x00
215 #define STEPPER15_SET_CTRL2_OUTPUT_DISABLE 0x80
216 #define STEPPER15_SET_CTRL2_TOFF_7_us 0x00
217 #define STEPPER15_SET_CTRL2_TOFF_16_us 0x08
218 #define STEPPER15_SET_CTRL2_TOFF_24_us 0x10
219 #define STEPPER15_SET_CTRL2_TOFF_32_us 0x18
220 #define STEPPER15_SET_CTRL2_SMART_TUNE_RIPPLE_CTRL 0x07
221 
227 #define STEPPER15_CTRL3_MICROSTEP_MODE_MASK 0x0F
228 #define STEPPER15_CTRL3_SPI_STEP_MASK 0x10
229 #define STEPPER15_CTRL3_SPI_DIR_MASK 0x20
230 #define STEPPER15_CTRL3_STEP_MASK 0x40
231 #define STEPPER15_CTRL3_DIR_MASK 0x80
232 #define STEPPER15_SET_SPI_DIR_INPUT_PIN 0x00
233 #define STEPPER15_SET_SPI_DIR_SPI 0x80
234 #define STEPPER15_SET_SPI_STEP_INPUT_PIN 0x00
235 #define STEPPER15_SET_SPI_STEP_SPI 0x40
236 #define STEPPER15_SET_CTRL3_SET_MICROSTEP_MODE_FULL_STEP 0x00
237 
243 #define STEPPER15_CTRL4_TW_REP_MASK 0x01
244 #define STEPPER15_CTRL4_OTSD_MODE_MASK 0x02
245 #define STEPPER15_CTRL4_OCP_MODE_MASK 0x04
246 #define STEPPER15_CTRL4_EN_OL_MASK 0x08
247 #define STEPPER15_CTRL4_LOCK_MASK 0x70
248 #define STEPPER15_CTRL4_CLR_FLT_MASK 0x80
249 #define STEPPER15_SET_CTRL4_CLR_FLT 0x80
250 #define STEPPER15_SET_CTRL4_UNLOCK_ALL_REG 0x30
251 
257 #define STEPPER15_CTRL5_SLIP_REP_MASK 0x01
258 #define STEPPER15_CTRL5_EN_SLIP_MASK 0x02
259 #define STEPPER15_CTRL5_EN_STLFCHG_MASK 0x04
260 #define STEPPER15_CTRL5_STL_REP_MASK 0x08
261 #define STEPPER15_SET_CTRL5_REPORTED_ON_FAULT 0x08
262 
268 #define STEPPER15_CTRL6_STALL_TH_MASK 0xFF
269 
275 #define STEPPER15_CTRL7_STALL_TH_MASK 0xFF
276 
282 #define STEPPER15_CTRL8_REV_ID_MASK 0x0F
283 #define STEPPER15_CTRL8_STL_UPDATE_MODE_MASK 0x10
284 #define STEPPER15_CTRL8_UTW_REP_MASK 0x20
285 #define STEPPER15_CTRL8_RSVD3_MASK 0xC0
286 
292 #define STEPPER15_SET_DEV_ADDR 0x70
293  // stepper15_set
295 
305 #define STEPPER15_DIRECTION_COUNTERCLOCKWISE 0
306 #define STEPPER15_DIRECTION_CLOCKWISE 1
307  // direction
309 
319 #define STEPPER15_WORK_MODE_SLEEP 0
320 #define STEPPER15_WORK_MODE_ENABLE_DEVICE 1
321  // work_mode
323 
333 #define STEPPER15_OUTPUT_MODE_OUTPUTS_DISABLE 0
334 #define STEPPER15_OUTPUT_MODE_OUTPUTS_ENABLE 1
335  // output_mode
337 
347 #define STEPPER15_FAULT_CONDITION 0
348 #define STEPPER15_CORRECT_CONDITION 1
349  // fault_condition
351 
366 #define STEPPER15_MAP_MIKROBUS( cfg, mikrobus ) \
367  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
368  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
369  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
370  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
371  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
372  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
373  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
374  cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
375  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
376  // stepper15_map // stepper15
379 
384 typedef struct stepper15_s
385 {
386  digital_out_t rst;
387  digital_out_t pwm;
389  digital_in_t int_pin;
391  i2c_master_t i2c;
392  spi_master_t spi;
394  uint8_t slave_address;
395  pin_name_t chip_select;
398 
403 typedef struct
404 {
405  pin_name_t scl;
406  pin_name_t sda;
407  pin_name_t miso;
408  pin_name_t mosi;
409  pin_name_t sck;
410  pin_name_t cs;
412  pin_name_t rst;
413  pin_name_t pwm;
414  pin_name_t int_pin;
416  uint32_t i2c_speed;
417  uint8_t i2c_address;
419  uint32_t spi_speed;
420  spi_master_mode_t spi_mode;
421  spi_master_chip_select_polarity_t cs_polarity;
424 
429 typedef enum
430 {
432  STEPPER15_ERROR = -1
433 
435 
452 
468 
483 
499 err_t stepper15_i2c_write ( stepper15_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
500 
516 err_t stepper15_i2c_read ( stepper15_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
517 
533 err_t stepper15_spi_write ( stepper15_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
534 
549 err_t stepper15_spi_read ( stepper15_t *ctx, uint8_t reg, uint8_t *data_out );
550 
563 
577 
592 
608 err_t stepper15_make_one_step ( stepper15_t *ctx, uint8_t step_speed );
609 
627 err_t stepper15_set_direction ( stepper15_t *ctx, uint8_t direction );
628 
646 err_t stepper15_set_work_mode ( stepper15_t *ctx, uint8_t work_mode );
647 
665 err_t stepper15_set_output_mode ( stepper15_t *ctx, uint8_t output_mode );
666 
699 err_t stepper15_set_torque_dac ( stepper15_t *ctx, uint8_t trq_data );
700 
716 err_t stepper15_set_counts ( stepper15_t *ctx, uint8_t counts );
717 
732 
750 err_t stepper15_step_by_angle ( stepper15_t *ctx, uint8_t step_speed, float angle, uint16_t step_360 );
751 
752 #ifdef __cplusplus
753 }
754 #endif
755 #endif // STEPPER15_H
756  // stepper15
758 
759 // ------------------------------------------------------------------------ END
stepper15_s::pwm
digital_out_t pwm
Definition: stepper15.h:387
stepper15_cfg_t::scl
pin_name_t scl
Definition: stepper15.h:405
stepper15_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: stepper15.h:420
STEPPER15_ERROR
@ STEPPER15_ERROR
Definition: stepper15.h:432
stepper15_cfg_t::int_pin
pin_name_t int_pin
Definition: stepper15.h:414
stepper15_i2c_read
err_t stepper15_i2c_read(stepper15_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Stepper 15 I2C reading function.
stepper15_return_value_t
stepper15_return_value_t
Stepper 15 Click return value data.
Definition: stepper15.h:430
stepper15_default_cfg
err_t stepper15_default_cfg(stepper15_t *ctx)
Stepper 15 default configuration function.
stepper15_spi_read
err_t stepper15_spi_read(stepper15_t *ctx, uint8_t reg, uint8_t *data_out)
Stepper 15 SPI reading function.
stepper15_i2c_write
err_t stepper15_i2c_write(stepper15_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Stepper 15 I2C writing function.
stepper15_s::rst
digital_out_t rst
Definition: stepper15.h:386
stepper15_cfg_t::sck
pin_name_t sck
Definition: stepper15.h:409
stepper15_cfg_t::spi_speed
uint32_t spi_speed
Definition: stepper15.h:419
stepper15_t
struct stepper15_s stepper15_t
Stepper 15 Click context object.
stepper15_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: stepper15.h:416
stepper15_cfg_t::i2c_address
uint8_t i2c_address
Definition: stepper15.h:417
stepper15_s::i2c
i2c_master_t i2c
Definition: stepper15.h:391
stepper15_get_interrupt
uint8_t stepper15_get_interrupt(stepper15_t *ctx)
Stepper 15 get interrupt function.
stepper15_make_one_step
err_t stepper15_make_one_step(stepper15_t *ctx, uint8_t step_speed)
Stepper 15 make one step function.
stepper15_cfg_t::mosi
pin_name_t mosi
Definition: stepper15.h:408
stepper15_s::slave_address
uint8_t slave_address
Definition: stepper15.h:394
stepper15_cfg_t::pwm
pin_name_t pwm
Definition: stepper15.h:413
stepper15_s::chip_select
pin_name_t chip_select
Definition: stepper15.h:395
stepper15_cfg_t::rst
pin_name_t rst
Definition: stepper15.h:412
stepper15_cfg_t::miso
pin_name_t miso
Definition: stepper15.h:407
stepper15_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: stepper15.h:421
stepper15_init
err_t stepper15_init(stepper15_t *ctx, stepper15_cfg_t *cfg)
Stepper 15 initialization function.
stepper15_reset
void stepper15_reset(stepper15_t *ctx)
Stepper 15 reset function.
stepper15_s::int_pin
digital_in_t int_pin
Definition: stepper15.h:389
stepper15_set_work_mode
err_t stepper15_set_work_mode(stepper15_t *ctx, uint8_t work_mode)
Stepper 15 set work mode function.
stepper15_cfg_t::sda
pin_name_t sda
Definition: stepper15.h:406
stepper15_set_torque_dac
err_t stepper15_set_torque_dac(stepper15_t *ctx, uint8_t trq_data)
Stepper 15 set torque DAC function.
stepper15_spi_write
err_t stepper15_spi_write(stepper15_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Stepper 15 SPI writing function.
STEPPER15_OK
@ STEPPER15_OK
Definition: stepper15.h:431
stepper15_set_counts
err_t stepper15_set_counts(stepper15_t *ctx, uint8_t counts)
Stepper 15 set counts function.
stepper15_s
Stepper 15 Click context object.
Definition: stepper15.h:385
stepper15_cfg_t::cs
pin_name_t cs
Definition: stepper15.h:410
stepper15_cfg_t
Stepper 15 Click configuration object.
Definition: stepper15.h:404
stepper15_step_by_angle
err_t stepper15_step_by_angle(stepper15_t *ctx, uint8_t step_speed, float angle, uint16_t step_360)
Stepper 15 step by angle function.
stepper15_s::spi
spi_master_t spi
Definition: stepper15.h:392
stepper15_cfg_setup
void stepper15_cfg_setup(stepper15_cfg_t *cfg)
Stepper 15 configuration object setup function.
stepper15_set_direction
err_t stepper15_set_direction(stepper15_t *ctx, uint8_t direction)
Stepper 15 set direction function.
stepper15_get_fault_condition
uint8_t stepper15_get_fault_condition(stepper15_t *ctx)
Stepper 15 get fault condition function.
stepper15_set_output_mode
err_t stepper15_set_output_mode(stepper15_t *ctx, uint8_t output_mode)
Stepper 15 set output mode function.
stepper15_motor_stop
void stepper15_motor_stop(stepper15_t *ctx)
Stepper 15 motor stop function.