silentstep 2.1.0.0
silentstep.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 SILENTSTEP_H
29#define SILENTSTEP_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_spi_master.h"
52#include "spi_specifics.h"
53
74#define SILENTSTEP_REG_GCONF 0x00
75#define SILENTSTEP_REG_GSTAT 0x01
76#define SILENTSTEP_REG_IFCNT 0x02
77#define SILENTSTEP_REG_SLAVECONF 0x03
78#define SILENTSTEP_REG_IOIN 0x04
79#define SILENTSTEP_REG_X_COMPARE 0x05
80#define SILENTSTEP_REG_IHOLD_IRUN 0x10
81#define SILENTSTEP_REG_TPOWERDOWN 0x11
82#define SILENTSTEP_REG_TSTEP 0x12
83#define SILENTSTEP_REG_TPWMTHRS 0x13
84#define SILENTSTEP_REG_TCOOLTHRS 0x14
85#define SILENTSTEP_REG_THIGH 0x15
86#define SILENTSTEP_REG_RAMP_MODE 0x20
87#define SILENTSTEP_REG_XACTUAL 0x21
88#define SILENTSTEP_REG_VACTUAL 0x22
89#define SILENTSTEP_REG_VSTART 0x23
90#define SILENTSTEP_REG_A1 0x24
91#define SILENTSTEP_REG_V1 0x25
92#define SILENTSTEP_REG_AMAX 0x26
93#define SILENTSTEP_REG_VMAX 0x27
94#define SILENTSTEP_REG_DMAX 0x28
95#define SILENTSTEP_REG_D1 0x2A
96#define SILENTSTEP_REG_VSTOP 0x2B
97#define SILENTSTEP_REG_TZEROWAIT 0x2C
98#define SILENTSTEP_REG_XTARGET 0x2D
99#define SILENTSTEP_REG_VDCMIN 0x33
100#define SILENTSTEP_REG_SW_MODE 0x34
101#define SILENTSTEP_REG_RAMP_STAT 0x35
102#define SILENTSTEP_REG_XLATCH 0x36
103#define SILENTSTEP_REG_ENCMODE 0x38
104#define SILENTSTEP_REG_X_ENC 0x39
105#define SILENTSTEP_REG_ENC_CONST 0x3A
106#define SILENTSTEP_REG_ENC_STATUS 0x3B
107#define SILENTSTEP_REG_ENC_LATCH 0x3C
108#define SILENTSTEP_REG_MSLUT_0 0x60
109#define SILENTSTEP_REG_MSLUT_1 0x61
110#define SILENTSTEP_REG_MSLUT_2 0x62
111#define SILENTSTEP_REG_MSLUT_3 0x63
112#define SILENTSTEP_REG_MSLUT_4 0x64
113#define SILENTSTEP_REG_MSLUT_5 0x65
114#define SILENTSTEP_REG_MSLUT_6 0x66
115#define SILENTSTEP_REG_MSLUT_7 0x67
116#define SILENTSTEP_REG_MSLUTSEL 0x68
117#define SILENTSTEP_REG_MSLUTSTART 0x69
118#define SILENTSTEP_REG_MSCNT 0x6A
119#define SILENTSTEP_REG_MSCURACT 0x6B
120#define SILENTSTEP_REG_CHOPCONF 0x6C
121#define SILENTSTEP_REG_COOLCONF 0x6D
122#define SILENTSTEP_REG_DCCTRL 0x6E
123#define SILENTSTEP_REG_DRV_STATUS 0x6F
124#define SILENTSTEP_REG_PWMCONF 0x70
125#define SILENTSTEP_REG_PWM_SCALE 0x71
126#define SILENTSTEP_REG_ENCM_CTRL 0x72
127#define SILENTSTEP_REG_LOST_STEPS 0x73
128
129 // silentstep_reg
130
145#define SILENTSTEP_IOIN_VERSION_MASK 0xFF000000ul
146#define SILENTSTEP_IOIN_VERSION_DEFAULT 0x11000000ul
147
152#define SILENTSTEP_DIR_CW 0
153#define SILENTSTEP_DIR_CCW 1
154
159#define SILENTSTEP_PIN_STATE_LOW 0
160#define SILENTSTEP_PIN_STATE_HIGH 1
161
166#define SILENTSTEP_SPEED_VERY_SLOW 0
167#define SILENTSTEP_SPEED_SLOW 1
168#define SILENTSTEP_SPEED_MEDIUM 2
169#define SILENTSTEP_SPEED_FAST 3
170#define SILENTSTEP_SPEED_VERY_FAST 4
171
176#define SILENTSTEP_TOFF_MASK 0x0000000Ful
177#define SILENTSTEP_TOFF_SHIFT 0
178#define SILENTSTEP_TOFF_DRIVER_DISABLE 0
179#define SILENTSTEP_TOFF_MIN 1
180#define SILENTSTEP_TOFF_DEFAULT 4
181#define SILENTSTEP_TOFF_MAX 15
182
187#define SILENTSTEP_MRES_MASK 0x0F000000ul
188#define SILENTSTEP_MRES_SHIFT 24
189#define SILENTSTEP_MRES_256 0
190#define SILENTSTEP_MRES_128 1
191#define SILENTSTEP_MRES_64 2
192#define SILENTSTEP_MRES_32 3
193#define SILENTSTEP_MRES_16 4
194#define SILENTSTEP_MRES_8 5
195#define SILENTSTEP_MRES_4 6
196#define SILENTSTEP_MRES_2 7
197#define SILENTSTEP_MRES_FULLSTEP 8
198
203#define SILENTSTEP_IRUN_MASK 0x00000F00ul
204#define SILENTSTEP_IRUN_SHIFT 8
205#define SILENTSTEP_IRUN_MIN 0
206#define SILENTSTEP_IRUN_DEFAULT 2
207#define SILENTSTEP_IRUN_MAX 31
208
217#define SILENTSTEP_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
218#define SILENTSTEP_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
219
220 // silentstep_set
221
236#define SILENTSTEP_MAP_MIKROBUS( cfg, mikrobus ) \
237 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
238 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
239 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
240 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
241 cfg.spm = MIKROBUS( mikrobus, MIKROBUS_AN ); \
242 cfg.sdm = MIKROBUS( mikrobus, MIKROBUS_RST ); \
243 cfg.step = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
244 cfg.dir = MIKROBUS( mikrobus, MIKROBUS_INT )
245
246 // silentstep_map
247 // silentstep
248
253typedef struct
254{
255 // Output pins
256 digital_out_t spm;
257 digital_out_t sdm;
258 digital_out_t step;
259 digital_out_t dir;
261 // Modules
262 spi_master_t spi;
264 pin_name_t chip_select;
267
272typedef struct
273{
274 // Communication gpio pins
275 pin_name_t miso;
276 pin_name_t mosi;
277 pin_name_t sck;
278 pin_name_t cs;
280 // Additional gpio pins
281 pin_name_t spm;
282 pin_name_t sdm;
283 pin_name_t step;
284 pin_name_t dir;
286 // static variable
287 uint32_t spi_speed;
288 spi_master_mode_t spi_mode;
289 spi_master_chip_select_polarity_t cs_polarity;
292
303
320
335
349
363err_t silentstep_write_reg ( silentstep_t *ctx, uint8_t reg, uint32_t data_in );
364
377err_t silentstep_read_reg ( silentstep_t *ctx, uint8_t reg, uint32_t *data_out );
378
390
401void silentstep_set_direction ( silentstep_t *ctx, uint8_t dir );
402
412
423void silentstep_set_spm_pin ( silentstep_t *ctx, uint8_t state );
424
435void silentstep_set_sdm_pin ( silentstep_t *ctx, uint8_t state );
436
447void silentstep_set_step_pin ( silentstep_t *ctx, uint8_t state );
448
460err_t silentstep_set_toff ( silentstep_t *ctx, uint8_t toff );
461
482err_t silentstep_set_step_res ( silentstep_t *ctx, uint8_t mres );
483
495err_t silentstep_set_run_current ( silentstep_t *ctx, uint8_t irun );
496
512void silentstep_drive_motor ( silentstep_t *ctx, uint32_t steps, uint8_t speed );
513
514#ifdef __cplusplus
515}
516#endif
517#endif // SILENTSTEP_H
518
519 // silentstep
520
521// ------------------------------------------------------------------------ END
err_t silentstep_set_step_res(silentstep_t *ctx, uint8_t mres)
Silent Step set step res function.
err_t silentstep_write_reg(silentstep_t *ctx, uint8_t reg, uint32_t data_in)
Silent Step write reg function.
void silentstep_set_step_pin(silentstep_t *ctx, uint8_t state)
Silent Step set step pin function.
void silentstep_set_sdm_pin(silentstep_t *ctx, uint8_t state)
Silent Step set sdm pin function.
void silentstep_set_spm_pin(silentstep_t *ctx, uint8_t state)
Silent Step set spm pin function.
void silentstep_drive_motor(silentstep_t *ctx, uint32_t steps, uint8_t speed)
Silent Step driver motor function.
err_t silentstep_set_toff(silentstep_t *ctx, uint8_t toff)
Silent Step set toff function.
err_t silentstep_default_cfg(silentstep_t *ctx)
Silent Step default configuration function.
err_t silentstep_check_com(silentstep_t *ctx)
Silent Step check com function.
void silentstep_cfg_setup(silentstep_cfg_t *cfg)
Silent Step configuration object setup function.
err_t silentstep_set_run_current(silentstep_t *ctx, uint8_t irun)
Silent Step set run current function.
void silentstep_set_direction(silentstep_t *ctx, uint8_t dir)
Silent Step set direction function.
err_t silentstep_init(silentstep_t *ctx, silentstep_cfg_t *cfg)
Silent Step initialization function.
err_t silentstep_read_reg(silentstep_t *ctx, uint8_t reg, uint32_t *data_out)
Silent Step read reg function.
void silentstep_switch_direction(silentstep_t *ctx)
Silent Step switch direction function.
silentstep_return_value_t
Silent Step Click return value data.
Definition silentstep.h:298
@ SILENTSTEP_OK
Definition silentstep.h:299
@ SILENTSTEP_ERROR
Definition silentstep.h:300
This file contains SPI specific macros, functions, etc.
Silent Step Click configuration object.
Definition silentstep.h:273
pin_name_t spm
Definition silentstep.h:281
pin_name_t step
Definition silentstep.h:283
spi_master_chip_select_polarity_t cs_polarity
Definition silentstep.h:289
pin_name_t sdm
Definition silentstep.h:282
pin_name_t sck
Definition silentstep.h:277
spi_master_mode_t spi_mode
Definition silentstep.h:288
pin_name_t mosi
Definition silentstep.h:276
uint32_t spi_speed
Definition silentstep.h:287
pin_name_t dir
Definition silentstep.h:284
pin_name_t miso
Definition silentstep.h:275
pin_name_t cs
Definition silentstep.h:278
Silent Step Click context object.
Definition silentstep.h:254
spi_master_t spi
Definition silentstep.h:262
digital_out_t sdm
Definition silentstep.h:257
digital_out_t spm
Definition silentstep.h:256
digital_out_t step
Definition silentstep.h:258
pin_name_t chip_select
Definition silentstep.h:264
digital_out_t dir
Definition silentstep.h:259