silentstep4 2.1.0.0
silentstep4.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 SILENTSTEP4_H
29#define SILENTSTEP4_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#include "drv_spi_master.h"
53#include "spi_specifics.h"
54
75#define SILENTSTEP4_REG_GCONF 0x00
76#define SILENTSTEP4_REG_GSTAT 0x01
77#define SILENTSTEP4_REG_IFCNT 0x02
78#define SILENTSTEP4_REG_NODECONF 0x03
79#define SILENTSTEP4_REG_IOIN 0x04
80#define SILENTSTEP4_REG_DRV_CONF 0x0A
81#define SILENTSTEP4_REG_GLOBAL_SCALER 0x0B
82#define SILENTSTEP4_REG_IHOLD_IRUN 0x10
83#define SILENTSTEP4_REG_TPOWERDOWN 0x11
84#define SILENTSTEP4_REG_TSTEP 0x12
85#define SILENTSTEP4_REG_TPWMTHRS 0x13
86#define SILENTSTEP4_REG_TCOOLTHRS 0x14
87#define SILENTSTEP4_REG_THIGH 0x15
88#define SILENTSTEP4_REG_DIRECT_MODE 0x2D
89#define SILENTSTEP4_REG_ENCMODE 0x38
90#define SILENTSTEP4_REG_X_ENC 0x39
91#define SILENTSTEP4_REG_ENC_CONST 0x3A
92#define SILENTSTEP4_REG_ENC_STATUS 0x3B
93#define SILENTSTEP4_REG_ENC_LATCH 0x3C
94#define SILENTSTEP4_REG_ADC_VSUPPLY_AIN 0x50
95#define SILENTSTEP4_REG_ADC_TEMP 0x51
96#define SILENTSTEP4_REG_OTW_OV_VTH 0x52
97#define SILENTSTEP4_REG_MSLUT_0 0x60
98#define SILENTSTEP4_REG_MSLUT_1 0x61
99#define SILENTSTEP4_REG_MSLUT_2 0x62
100#define SILENTSTEP4_REG_MSLUT_3 0x63
101#define SILENTSTEP4_REG_MSLUT_4 0x64
102#define SILENTSTEP4_REG_MSLUT_5 0x65
103#define SILENTSTEP4_REG_MSLUT_6 0x66
104#define SILENTSTEP4_REG_MSLUT_7 0x67
105#define SILENTSTEP4_REG_MSLUTSEL 0x68
106#define SILENTSTEP4_REG_MSLUTSTART 0x69
107#define SILENTSTEP4_REG_MSCNT 0x6A
108#define SILENTSTEP4_REG_MSCURACT 0x6B
109#define SILENTSTEP4_REG_CHOPCONF 0x6C
110#define SILENTSTEP4_REG_COOLCONF 0x6D
111#define SILENTSTEP4_REG_DRV_STATUS 0x6F
112#define SILENTSTEP4_REG_PWMCONF 0x70
113#define SILENTSTEP4_REG_PWM_SCALE 0x71
114#define SILENTSTEP4_REG_PWM_AUTO 0x72
115#define SILENTSTEP4_REG_SG4_THRS 0x74
116#define SILENTSTEP4_REG_SG4_RESULT 0x75
117#define SILENTSTEP4_REG_SG4_IND 0x76
118
123#define SILENTSTEP4_PCA9538A_REG_INPUT 0x00
124#define SILENTSTEP4_PCA9538A_REG_OUTPUT 0x01
125#define SILENTSTEP4_PCA9538A_REG_POLARITY 0x02
126#define SILENTSTEP4_PCA9538A_REG_CONFIG 0x03
127
142#define SILENTSTEP4_IOIN_VERSION_MASK 0xFF000000ul
143#define SILENTSTEP4_IOIN_VERSION_DEFAULT 0x40000000ul
144
149#define SILENTSTEP4_PIN_EN 0x01
150#define SILENTSTEP4_PIN_SLEEP 0x02
151#define SILENTSTEP4_PIN_DIAG0 0x04
152#define SILENTSTEP4_PIN_DIAG1 0x08
153#define SILENTSTEP4_PIN_OV 0x10
154
159#define SILENTSTEP4_PCA9538A_DEFAULT_CONFIG 0xFC
160
165#define SILENTSTEP4_DIR_CW 0
166#define SILENTSTEP4_DIR_CCW 1
167
172#define SILENTSTEP4_PIN_STATE_LOW 0
173#define SILENTSTEP4_PIN_STATE_HIGH 1
174
179#define SILENTSTEP4_SPEED_VERY_SLOW 0
180#define SILENTSTEP4_SPEED_SLOW 1
181#define SILENTSTEP4_SPEED_MEDIUM 2
182#define SILENTSTEP4_SPEED_FAST 3
183#define SILENTSTEP4_SPEED_VERY_FAST 4
184
189#define SILENTSTEP4_TOFF_MASK 0x0000000Ful
190#define SILENTSTEP4_TOFF_SHIFT 0
191#define SILENTSTEP4_TOFF_DRIVER_DISABLE 0
192#define SILENTSTEP4_TOFF_MIN 1
193#define SILENTSTEP4_TOFF_DEFAULT 4
194#define SILENTSTEP4_TOFF_MAX 15
195
200#define SILENTSTEP4_MRES_MASK 0x0F000000ul
201#define SILENTSTEP4_MRES_SHIFT 24
202#define SILENTSTEP4_MRES_256 0
203#define SILENTSTEP4_MRES_128 1
204#define SILENTSTEP4_MRES_64 2
205#define SILENTSTEP4_MRES_32 3
206#define SILENTSTEP4_MRES_16 4
207#define SILENTSTEP4_MRES_8 5
208#define SILENTSTEP4_MRES_4 6
209#define SILENTSTEP4_MRES_2 7
210#define SILENTSTEP4_MRES_FULLSTEP 8
211
216#define SILENTSTEP4_IRUN_MASK 0x00000F00ul
217#define SILENTSTEP4_IRUN_SHIFT 8
218#define SILENTSTEP4_IRUN_MIN 0
219#define SILENTSTEP4_IRUN_DEFAULT 16
220#define SILENTSTEP4_IRUN_MAX 31
221
227#define SILENTSTEP4_DEVICE_ADDRESS_A1A0_00 0x70
228#define SILENTSTEP4_DEVICE_ADDRESS_A1A0_01 0x71
229#define SILENTSTEP4_DEVICE_ADDRESS_A1A0_10 0x72
230#define SILENTSTEP4_DEVICE_ADDRESS_A1A0_11 0x73
231
240#define SILENTSTEP4_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
241#define SILENTSTEP4_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
242
243 // silentstep4_set
244
259#define SILENTSTEP4_MAP_MIKROBUS( cfg, mikrobus ) \
260 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
261 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
262 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
263 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
264 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
265 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
266 cfg.dir = MIKROBUS( mikrobus, MIKROBUS_AN ); \
267 cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
268 cfg.step = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
269 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
270
271 // silentstep4_map
272 // silentstep4
273
278typedef struct silentstep4_s
279{
280 digital_out_t dir;
281 digital_out_t rst;
282 digital_out_t step;
284 digital_in_t int_pin;
286 i2c_master_t i2c;
287 spi_master_t spi;
290 pin_name_t chip_select;
293
298typedef struct
299{
300 pin_name_t scl;
301 pin_name_t sda;
302 pin_name_t miso;
303 pin_name_t mosi;
304 pin_name_t sck;
305 pin_name_t cs;
306 pin_name_t dir;
307 pin_name_t rst;
308 pin_name_t step;
309 pin_name_t int_pin;
311 uint32_t i2c_speed;
312 uint8_t i2c_address;
314 uint32_t spi_speed;
315 spi_master_mode_t spi_mode;
316 spi_master_chip_select_polarity_t cs_polarity;
319
330
347
362
376
390err_t silentstep4_write_reg ( silentstep4_t *ctx, uint8_t reg, uint32_t data_in );
391
404err_t silentstep4_read_reg ( silentstep4_t *ctx, uint8_t reg, uint32_t *data_out );
405
419err_t silentstep4_pca_write_reg ( silentstep4_t *ctx, uint8_t reg, uint8_t data_in );
420
433err_t silentstep4_pca_read_reg ( silentstep4_t *ctx, uint8_t reg, uint8_t *data_out );
434
446
459err_t silentstep4_get_en_pin ( silentstep4_t *ctx, uint8_t *state );
460
473err_t silentstep4_set_en_pin ( silentstep4_t *ctx, uint8_t state );
474
487err_t silentstep4_get_sleep_pin ( silentstep4_t *ctx, uint8_t *state );
488
501err_t silentstep4_set_sleep_pin ( silentstep4_t *ctx, uint8_t state );
502
515err_t silentstep4_get_diag0_pin ( silentstep4_t *ctx, uint8_t *state );
516
529err_t silentstep4_get_diag1_pin ( silentstep4_t *ctx, uint8_t *state );
530
543err_t silentstep4_get_ov_pin ( silentstep4_t *ctx, uint8_t *state );
544
556
568
579void silentstep4_set_direction ( silentstep4_t *ctx, uint8_t dir );
580
590
600
611void silentstep4_set_rst_pin ( silentstep4_t *ctx, uint8_t state );
612
622
633void silentstep4_set_step_pin ( silentstep4_t *ctx, uint8_t state );
634
646err_t silentstep4_set_toff ( silentstep4_t *ctx, uint8_t toff );
647
668err_t silentstep4_set_step_res ( silentstep4_t *ctx, uint8_t mres );
669
681err_t silentstep4_set_run_current ( silentstep4_t *ctx, uint8_t irun );
682
698void silentstep4_drive_motor ( silentstep4_t *ctx, uint32_t steps, uint8_t speed );
699
700#ifdef __cplusplus
701}
702#endif
703#endif // SILENTSTEP4_H
704
705 // silentstep4
706
707// ------------------------------------------------------------------------ END
err_t silentstep4_pca_write_reg(silentstep4_t *ctx, uint8_t reg, uint8_t data_in)
Silent Step 4 pca write reg function.
err_t silentstep4_set_run_current(silentstep4_t *ctx, uint8_t irun)
Silent Step 4 set run current function.
void silentstep4_set_rst_pin(silentstep4_t *ctx, uint8_t state)
Silent Step 4 set rst pin function.
err_t silentstep4_read_reg(silentstep4_t *ctx, uint8_t reg, uint32_t *data_out)
Silent Step 4 read reg function.
void silentstep4_set_direction(silentstep4_t *ctx, uint8_t dir)
Silent Step 4 set direction function.
void silentstep4_reset_pca(silentstep4_t *ctx)
Silent Step 4 reset device function.
spi_master_t spi
Definition silentstep4.h:287
uint8_t silentstep4_get_int_pin(silentstep4_t *ctx)
Silent Step 4 get int pin function.
silentstep4_return_value_t
Silent Step 4 Click return value data.
Definition silentstep4.h:325
err_t silentstep4_set_sleep_pin(silentstep4_t *ctx, uint8_t state)
Silent Step 4 set sleep pin function.
void silentstep4_switch_direction(silentstep4_t *ctx)
Silent Step 4 switch direction function.
void silentstep4_cfg_setup(silentstep4_cfg_t *cfg)
Silent Step 4 configuration object setup function.
err_t silentstep4_write_reg(silentstep4_t *ctx, uint8_t reg, uint32_t data_in)
Silent Step 4 write reg function.
err_t silentstep4_enable_device(silentstep4_t *ctx)
Silent Step 4 enable device function.
struct silentstep4_s silentstep4_t
Silent Step 4 Click context object.
pin_name_t step
Definition silentstep4.h:308
err_t silentstep4_init(silentstep4_t *ctx, silentstep4_cfg_t *cfg)
Silent Step 4 initialization function.
uint32_t i2c_speed
Definition silentstep4.h:311
err_t silentstep4_get_diag0_pin(silentstep4_t *ctx, uint8_t *state)
Silent Step 4 get diag0 pin function.
void silentstep4_set_step_pin(silentstep4_t *ctx, uint8_t state)
Silent Step 4 set step pin function.
err_t silentstep4_default_cfg(silentstep4_t *ctx)
Silent Step 4 default configuration function.
err_t silentstep4_set_toff(silentstep4_t *ctx, uint8_t toff)
Silent Step 4 set toff function.
spi_master_chip_select_polarity_t cs_polarity
Definition silentstep4.h:316
pin_name_t sck
Definition silentstep4.h:304
err_t silentstep4_set_step_res(silentstep4_t *ctx, uint8_t mres)
Silent Step 4 set step res function.
spi_master_mode_t spi_mode
Definition silentstep4.h:315
err_t silentstep4_set_en_pin(silentstep4_t *ctx, uint8_t state)
Silent Step 4 set en pin function.
digital_in_t int_pin
Definition silentstep4.h:284
digital_out_t step
Definition silentstep4.h:282
i2c_master_t i2c
Definition silentstep4.h:286
err_t silentstep4_get_diag1_pin(silentstep4_t *ctx, uint8_t *state)
Silent Step 4 get diag1 pin function.
err_t silentstep4_pca_read_reg(silentstep4_t *ctx, uint8_t reg, uint8_t *data_out)
Silent Step 4 pca read reg function.
pin_name_t mosi
Definition silentstep4.h:303
uint32_t spi_speed
Definition silentstep4.h:314
digital_out_t rst
Definition silentstep4.h:281
void silentstep4_drive_motor(silentstep4_t *ctx, uint32_t steps, uint8_t speed)
Silent Step 4 driver motor function.
pin_name_t dir
Definition silentstep4.h:306
pin_name_t scl
Definition silentstep4.h:300
uint8_t slave_address
Definition silentstep4.h:289
err_t silentstep4_get_en_pin(silentstep4_t *ctx, uint8_t *state)
Silent Step 4 get en pin function.
pin_name_t int_pin
Definition silentstep4.h:309
err_t silentstep4_get_ov_pin(silentstep4_t *ctx, uint8_t *state)
Silent Step 4 get ov pin function.
pin_name_t miso
Definition silentstep4.h:302
pin_name_t sda
Definition silentstep4.h:301
err_t silentstep4_get_sleep_pin(silentstep4_t *ctx, uint8_t *state)
Silent Step 4 get sleep pin function.
pin_name_t chip_select
Definition silentstep4.h:290
err_t silentstep4_disable_device(silentstep4_t *ctx)
Silent Step 4 disable device function.
pin_name_t rst
Definition silentstep4.h:307
digital_out_t dir
Definition silentstep4.h:280
pin_name_t cs
Definition silentstep4.h:305
err_t silentstep4_check_com(silentstep4_t *ctx)
Silent Step 4 check com function.
uint8_t i2c_address
Definition silentstep4.h:312
@ SILENTSTEP4_ERROR
Definition silentstep4.h:327
@ SILENTSTEP4_OK
Definition silentstep4.h:326
This file contains SPI specific macros, functions, etc.
Silent Step 4 Click configuration object.
Definition silentstep4.h:299
Silent Step 4 Click context object.
Definition silentstep4.h:279