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 
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 #include "drv_spi_master.h"
49 
50 
71 #define STEPPER15_REG_INPUT_PORT 0x00
72 #define STEPPER15_REG_OUTPUT_PORT 0x01
73 #define STEPPER15_REG_POLARITY_INVERSION 0x02
74 #define STEPPER15_REG_CONFIGURATION 0x03
75 
80 #define STEPPER15_REG_FAULT_STATUS 0x00
81 #define STEPPER15_REG_DIAG_STATUS_1 0x01
82 #define STEPPER15_REG_DIAG_STATUS_2 0x02
83 #define STEPPER15_REG_CTRL_1 0x03
84 #define STEPPER15_REG_CTRL_2 0x04
85 #define STEPPER15_REG_CTRL_3 0x05
86 #define STEPPER15_REG_CTRL_4 0x06
87 #define STEPPER15_REG_CTRL_5 0x07
88 #define STEPPER15_REG_CTRL_6 0x08
89 #define STEPPER15_REG_CTRL_7 0x09
90 #define STEPPER15_REG_CTRL_8 0x0A
91  // stepper15_reg
93 
108 #define STEPPER15_SET_DIRECTION_BIT_MASK 0xFB
109 #define STEPPER15_SET_DIRECTION_CCW 0x00
110 #define STEPPER15_SET_DIRECTION_CW 0x04
111 
116 #define STEPPER15_SET_WORK_MODE_BIT_MASK 0xFE
117 #define STEPPER15_SET_WORK_MODE_SLEEP 0x00
118 #define STEPPER15_SET_WORK_MODE_ENABLE_DEVICE 0x01
119 
124 #define STEPPER15_SET_OUT_MODE_BIT_MASK 0xFD
125 #define STEPPER15_SET_OUT_MODE_DEVICE_OUTPUTS_ENABLE 0x00
126 #define STEPPER15_SET_OUT_MODE_DEVICE_OUTPUTS_DISABLE 0x02
127 
132 #define STEPPER15_SET_CONFIG_DEFAULT 0xF8
133 
138 #define STEPPER15_FAULT_ST_OL_MASK 0x01
139 #define STEPPER15_FAULT_ST_OT_MASK 0x02
140 #define STEPPER15_FAULT_ST_STL_SLIP_MASK 0x04
141 #define STEPPER15_FAULT_ST_OCP_MASK 0x08
142 #define STEPPER15_FAULT_ST_CPUV_MASK 0x10
143 #define STEPPER15_FAULT_ST_UVLO_MASK 0x20
144 #define STEPPER15_FAULT_ST_SPI_ERROR_MASK 0x40
145 #define STEPPER15_FAULT_ST_FAULT_MASK 0x80
146 
151 #define STEPPER15_DIAG_1_OCP_HS1_A_MASK 0x01
152 #define STEPPER15_DIAG_1_OCP_LS1_A_MASK 0x02
153 #define STEPPER15_DIAG_1_OCP_HS2_A_MASK 0x04
154 #define STEPPER15_DIAG_1_OCP_LS2_A_MASK 0x08
155 #define STEPPER15_DIAG_1_OCP_HS1_B_MASK 0x10
156 #define STEPPER15_DIAG_1_OCP_LS1_B_MASK 0x20
157 #define STEPPER15_DIAG_1_OCP_HS2_B_MASK 0x40
158 #define STEPPER15_DIAG_1_OCP_LS2_B_MASK 0x80
159 
165 #define STEPPER15_DIAG_2_OL_A_MASK 0x01
166 #define STEPPER15_DIAG_2_OL_B_MASK 0x02
167 #define STEPPER15_DIAG_2_SLIP_MASK 0x04
168 #define STEPPER15_DIAG_2_STALL_MASK 0x08
169 #define STEPPER15_DIAG_2_STL_LRN_OK_MASK 0x10
170 #define STEPPER15_DIAG_2_OTS_MASK 0x20
171 #define STEPPER15_DIAG_2_OTW_MASK 0x40
172 #define STEPPER15_DIAG_2_UTW_MASK 0x80
173 
179 #define STEPPER15_CTRL1_SLEW_RATE_MASK 0x03
180 #define STEPPER15_CTRL1_EN_NSR_MASK 0x04
181 #define STEPPER15_CTRL1_RSVD1_MASK 0x08
182 #define STEPPER15_CTRL1_TRQ_DAC_MASK 0xF0
183 #define STEPPER15_SET_CTRL1_TRQ_100 0x00
184 #define STEPPER15_SET_CTRL1_TRQ_93_75 0x10
185 #define STEPPER15_SET_CTRL1_TRQ_87_5 0x20
186 #define STEPPER15_SET_CTRL1_TRQ_81_25 0x30
187 #define STEPPER15_SET_CTRL1_TRQ_75 0x40
188 #define STEPPER15_SET_CTRL1_TRQ_68_75 0x50
189 #define STEPPER15_SET_CTRL1_TRQ_62_5 0x60
190 #define STEPPER15_SET_CTRL1_TRQ_56_25 0x70
191 #define STEPPER15_SET_CTRL1_TRQ_50 0x80
192 #define STEPPER15_SET_CTRL1_TRQ_43_75 0x90
193 #define STEPPER15_SET_CTRL1_TRQ_37_5 0xA0
194 #define STEPPER15_SET_CTRL1_TRQ_31_25 0xB0
195 #define STEPPER15_SET_CTRL1_TRQ_25 0xC0
196 #define STEPPER15_SET_CTRL1_TRQ_18_75 0xD0
197 #define STEPPER15_SET_CTRL1_TRQ_12_5 0xE0
198 #define STEPPER15_SET_CTRL1_TRQ_6_25 0xF0
199 
200 
206 #define STEPPER15_CTRL2_DECAY_MASK 0x07
207 #define STEPPER15_CTRL2_TOFF_MASK 0x18
208 #define STEPPER15_CTRL2_RSVD2_MASK 0x60
209 #define STEPPER15_CTRL2_DIS_OUT_MASK 0x80
210 #define STEPPER15_SET_CTRL2_OUTPUT_ENABLE 0x00
211 #define STEPPER15_SET_CTRL2_OUTPUT_DISABLE 0x80
212 #define STEPPER15_SET_CTRL2_TOFF_7_us 0x00
213 #define STEPPER15_SET_CTRL2_TOFF_16_us 0x08
214 #define STEPPER15_SET_CTRL2_TOFF_24_us 0x10
215 #define STEPPER15_SET_CTRL2_TOFF_32_us 0x18
216 #define STEPPER15_SET_CTRL2_SMART_TUNE_RIPPLE_CTRL 0x07
217 
223 #define STEPPER15_CTRL3_MICROSTEP_MODE_MASK 0x0F
224 #define STEPPER15_CTRL3_SPI_STEP_MASK 0x10
225 #define STEPPER15_CTRL3_SPI_DIR_MASK 0x20
226 #define STEPPER15_CTRL3_STEP_MASK 0x40
227 #define STEPPER15_CTRL3_DIR_MASK 0x80
228 #define STEPPER15_SET_SPI_DIR_INPUT_PIN 0x00
229 #define STEPPER15_SET_SPI_DIR_SPI 0x80
230 #define STEPPER15_SET_SPI_STEP_INPUT_PIN 0x00
231 #define STEPPER15_SET_SPI_STEP_SPI 0x40
232 #define STEPPER15_SET_CTRL3_SET_MICROSTEP_MODE_FULL_STEP 0x00
233 
239 #define STEPPER15_CTRL4_TW_REP_MASK 0x01
240 #define STEPPER15_CTRL4_OTSD_MODE_MASK 0x02
241 #define STEPPER15_CTRL4_OCP_MODE_MASK 0x04
242 #define STEPPER15_CTRL4_EN_OL_MASK 0x08
243 #define STEPPER15_CTRL4_LOCK_MASK 0x70
244 #define STEPPER15_CTRL4_CLR_FLT_MASK 0x80
245 #define STEPPER15_SET_CTRL4_CLR_FLT 0x80
246 #define STEPPER15_SET_CTRL4_UNLOCK_ALL_REG 0x30
247 
253 #define STEPPER15_CTRL5_SLIP_REP_MASK 0x01
254 #define STEPPER15_CTRL5_EN_SLIP_MASK 0x02
255 #define STEPPER15_CTRL5_EN_STLFCHG_MASK 0x04
256 #define STEPPER15_CTRL5_STL_REP_MASK 0x08
257 #define STEPPER15_SET_CTRL5_REPORTED_ON_FAULT 0x08
258 
264 #define STEPPER15_CTRL6_STALL_TH_MASK 0xFF
265 
271 #define STEPPER15_CTRL7_STALL_TH_MASK 0xFF
272 
278 #define STEPPER15_CTRL8_REV_ID_MASK 0x0F
279 #define STEPPER15_CTRL8_STL_UPDATE_MODE_MASK 0x10
280 #define STEPPER15_CTRL8_UTW_REP_MASK 0x20
281 #define STEPPER15_CTRL8_RSVD3_MASK 0xC0
282 
288 #define STEPPER15_SET_DEV_ADDR 0x70
289  // stepper15_set
291 
301 #define STEPPER15_DIRECTION_COUNTERCLOCKWISE 0
302 #define STEPPER15_DIRECTION_CLOCKWISE 1
303  // direction
305 
315 #define STEPPER15_WORK_MODE_SLEEP 0
316 #define STEPPER15_WORK_MODE_ENABLE_DEVICE 1
317  // work_mode
319 
329 #define STEPPER15_OUTPUT_MODE_OUTPUTS_DISABLE 0
330 #define STEPPER15_OUTPUT_MODE_OUTPUTS_ENABLE 1
331  // output_mode
333 
343 #define STEPPER15_FAULT_CONDITION 0
344 #define STEPPER15_CORRECT_CONDITION 1
345  // fault_condition
347 
362 #define STEPPER15_MAP_MIKROBUS( cfg, mikrobus ) \
363  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
364  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
365  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
366  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
367  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
368  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
369  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
370  cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
371  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
372  // stepper15_map // stepper15
375 
380 typedef struct stepper15_s
381 {
382  digital_out_t rst;
383  digital_out_t pwm;
385  digital_in_t int_pin;
387  i2c_master_t i2c;
388  spi_master_t spi;
390  uint8_t slave_address;
391  pin_name_t chip_select;
394 
399 typedef struct
400 {
401  pin_name_t scl;
402  pin_name_t sda;
403  pin_name_t miso;
404  pin_name_t mosi;
405  pin_name_t sck;
406  pin_name_t cs;
408  pin_name_t rst;
409  pin_name_t pwm;
410  pin_name_t int_pin;
412  uint32_t i2c_speed;
413  uint8_t i2c_address;
415  uint32_t spi_speed;
416  spi_master_mode_t spi_mode;
417  spi_master_chip_select_polarity_t cs_polarity;
420 
425 typedef enum
426 {
428  STEPPER15_ERROR = -1
429 
431 
448 
464 
479 
495 err_t stepper15_i2c_write ( stepper15_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
496 
512 err_t stepper15_i2c_read ( stepper15_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
513 
529 err_t stepper15_spi_write ( stepper15_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
530 
545 err_t stepper15_spi_read ( stepper15_t *ctx, uint8_t reg, uint8_t *data_out );
546 
559 
573 
588 
604 err_t stepper15_make_one_step ( stepper15_t *ctx, uint8_t step_speed );
605 
623 err_t stepper15_set_direction ( stepper15_t *ctx, uint8_t direction );
624 
642 err_t stepper15_set_work_mode ( stepper15_t *ctx, uint8_t work_mode );
643 
661 err_t stepper15_set_output_mode ( stepper15_t *ctx, uint8_t output_mode );
662 
695 err_t stepper15_set_torque_dac ( stepper15_t *ctx, uint8_t trq_data );
696 
712 err_t stepper15_set_counts ( stepper15_t *ctx, uint8_t counts );
713 
728 
746 err_t stepper15_step_by_angle ( stepper15_t *ctx, uint8_t step_speed, float angle, uint16_t step_360 );
747 
748 #ifdef __cplusplus
749 }
750 #endif
751 #endif // STEPPER15_H
752  // stepper15
754 
755 // ------------------------------------------------------------------------ END
stepper15_s::pwm
digital_out_t pwm
Definition: stepper15.h:383
stepper15_cfg_t::scl
pin_name_t scl
Definition: stepper15.h:401
stepper15_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: stepper15.h:416
STEPPER15_ERROR
@ STEPPER15_ERROR
Definition: stepper15.h:428
stepper15_cfg_t::int_pin
pin_name_t int_pin
Definition: stepper15.h:410
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:426
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:382
stepper15_cfg_t::sck
pin_name_t sck
Definition: stepper15.h:405
stepper15_cfg_t::spi_speed
uint32_t spi_speed
Definition: stepper15.h:415
stepper15_t
struct stepper15_s stepper15_t
Stepper 15 Click context object.
stepper15_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: stepper15.h:412
stepper15_cfg_t::i2c_address
uint8_t i2c_address
Definition: stepper15.h:413
stepper15_s::i2c
i2c_master_t i2c
Definition: stepper15.h:387
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:404
stepper15_s::slave_address
uint8_t slave_address
Definition: stepper15.h:390
stepper15_cfg_t::pwm
pin_name_t pwm
Definition: stepper15.h:409
stepper15_s::chip_select
pin_name_t chip_select
Definition: stepper15.h:391
stepper15_cfg_t::rst
pin_name_t rst
Definition: stepper15.h:408
stepper15_cfg_t::miso
pin_name_t miso
Definition: stepper15.h:403
stepper15_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: stepper15.h:417
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:385
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:402
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:427
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:381
stepper15_cfg_t::cs
pin_name_t cs
Definition: stepper15.h:406
stepper15_cfg_t
Stepper 15 Click configuration object.
Definition: stepper15.h:400
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:388
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.