silentstep3 2.1.0.0
silentstep3.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 SILENTSTEP3_H
29#define SILENTSTEP3_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 SILENTSTEP3_REG_DRVCTRL 0x00
75#define SILENTSTEP3_REG_CHOPCONF 0x04
76#define SILENTSTEP3_REG_SMARTEN 0x05
77#define SILENTSTEP3_REG_SGCSCONF 0x06
78#define SILENTSTEP3_REG_DRVCONF 0x07
79
80 // silentstep3_reg
81
96#define SILENTSTEP3_DRVCTRL_1_PHA_MASK 0x20000ul
97#define SILENTSTEP3_DRVCTRL_1_CA_MASK 0x1FE00ul
98#define SILENTSTEP3_DRVCTRL_1_PHB_MASK 0x00100ul
99#define SILENTSTEP3_DRVCTRL_1_CB_MASK 0x000FFul
100
105#define SILENTSTEP3_DRVCTRL_0_INTPOL_MASK 0x00200ul
106#define SILENTSTEP3_DRVCTRL_0_DEDGE_MASK 0x00100ul
107#define SILENTSTEP3_DRVCTRL_0_MRES_256 0x00000ul
108#define SILENTSTEP3_DRVCTRL_0_MRES_128 0x00001ul
109#define SILENTSTEP3_DRVCTRL_0_MRES_64 0x00002ul
110#define SILENTSTEP3_DRVCTRL_0_MRES_32 0x00003ul
111#define SILENTSTEP3_DRVCTRL_0_MRES_16 0x00004ul
112#define SILENTSTEP3_DRVCTRL_0_MRES_8 0x00005ul
113#define SILENTSTEP3_DRVCTRL_0_MRES_4 0x00006ul
114#define SILENTSTEP3_DRVCTRL_0_MRES_2 0x00007ul
115#define SILENTSTEP3_DRVCTRL_0_MRES_1 0x00008ul
116#define SILENTSTEP3_DRVCTRL_0_MRES_MASK 0x0000Ful
117
122#define SILENTSTEP3_CHOPCONF_TBL_16 0x00000ul
123#define SILENTSTEP3_CHOPCONF_TBL_24 0x08000ul
124#define SILENTSTEP3_CHOPCONF_TBL_32 0x10000ul
125#define SILENTSTEP3_CHOPCONF_TBL_54 0x18000ul
126#define SILENTSTEP3_CHOPCONF_TBL_MASK 0x18000ul
127#define SILENTSTEP3_CHOPCONF_CHM_STANDARD 0x00000ul
128#define SILENTSTEP3_CHOPCONF_CHM_FAST 0x04000ul
129#define SILENTSTEP3_CHOPCONF_CHM_MASK 0x04000ul
130#define SILENTSTEP3_CHOPCONF_RNDTF_DISABLE 0x00000ul
131#define SILENTSTEP3_CHOPCONF_RNDTF_ENABLE 0x02000ul
132#define SILENTSTEP3_CHOPCONF_RNDTF_MASK 0x02000ul
133#define SILENTSTEP3_CHOPCONF_HDEC_16 0x00000ul
134#define SILENTSTEP3_CHOPCONF_HDEC_32 0x00800ul
135#define SILENTSTEP3_CHOPCONF_HDEC_48 0x01000ul
136#define SILENTSTEP3_CHOPCONF_HDEC_64 0x01800ul
137#define SILENTSTEP3_CHOPCONF_HDEC_MASK 0x01800ul
138#define SILENTSTEP3_CHOPCONF_HEND_0 0x00180ul
139#define SILENTSTEP3_CHOPCONF_HEND_MASK 0x00780ul
140#define SILENTSTEP3_CHOPCONF_HSTRT_4 0x00030ul
141#define SILENTSTEP3_CHOPCONF_HSTRT_MASK 0x00070ul
142#define SILENTSTEP3_CHOPCONF_TOFF_4 0x00004ul
143#define SILENTSTEP3_CHOPCONF_TOFF_MASK 0x0000Ful
144
149#define SILENTSTEP3_SMARTEN_SEIMIN_HALF_CS 0x00000ul
150#define SILENTSTEP3_SMARTEN_SEIMIN_QUARTER_CS 0x08000ul
151#define SILENTSTEP3_SMARTEN_SEIMIN_MASK 0x08000ul
152#define SILENTSTEP3_SMARTEN_SEDN_32 0x00000ul
153#define SILENTSTEP3_SMARTEN_SEDN_8 0x02000ul
154#define SILENTSTEP3_SMARTEN_SEDN_2 0x04000ul
155#define SILENTSTEP3_SMARTEN_SEDN_1 0x06000ul
156#define SILENTSTEP3_SMARTEN_SEDN_MASK 0x06000ul
157#define SILENTSTEP3_SMARTEN_SEMAX_2 0x00200ul
158#define SILENTSTEP3_SMARTEN_SEMAX_MASK 0x00F00ul
159#define SILENTSTEP3_SMARTEN_SEUP_1 0x00000ul
160#define SILENTSTEP3_SMARTEN_SEUP_2 0x00020ul
161#define SILENTSTEP3_SMARTEN_SEUP_4 0x00040ul
162#define SILENTSTEP3_SMARTEN_SEUP_8 0x00060ul
163#define SILENTSTEP3_SMARTEN_SEUP_MASK 0x00060ul
164#define SILENTSTEP3_SMARTEN_SEMIN_2 0x00002ul
165#define SILENTSTEP3_SMARTEN_SEMIN_MASK 0x0000Ful
166
171#define SILENTSTEP3_SGCSCONF_SFILT_STANDARD 0x00000ul
172#define SILENTSTEP3_SGCSCONF_SFILT_FILTERED 0x10000ul
173#define SILENTSTEP3_SGCSCONF_SFILT_MASK 0x10000ul
174#define SILENTSTEP3_SGCSCONF_SGT_0 0x00000ul
175#define SILENTSTEP3_SGCSCONF_SGT_MASK 0x07F00ul
176#define SILENTSTEP3_SGCSCONF_CS_2_OF_32 0x00002ul
177#define SILENTSTEP3_SGCSCONF_CS_MASK 0x0001Ful
178
183#define SILENTSTEP3_DRVCONF_TST_MASK 0x10000ul
184#define SILENTSTEP3_DRVCONF_SLPH_MIN 0x00000ul
185#define SILENTSTEP3_DRVCONF_SLPH_MIN_TEMP 0x04000ul
186#define SILENTSTEP3_DRVCONF_SLPH_MED_TEMP 0x08000ul
187#define SILENTSTEP3_DRVCONF_SLPH_MAX 0x0C000ul
188#define SILENTSTEP3_DRVCONF_SLPH_MASK 0x0C000ul
189#define SILENTSTEP3_DRVCONF_SLPL_MIN 0x00000ul
190#define SILENTSTEP3_DRVCONF_SLPL_MIN_TEMP 0x01000ul
191#define SILENTSTEP3_DRVCONF_SLPL_MED_TEMP 0x02000ul
192#define SILENTSTEP3_DRVCONF_SLPL_MAX 0x03000ul
193#define SILENTSTEP3_DRVCONF_SLPL_MASK 0x03000ul
194#define SILENTSTEP3_DRVCONF_DISS2G_ENABLE 0x00000ul
195#define SILENTSTEP3_DRVCONF_DISS2G_DISABLE 0x00400ul
196#define SILENTSTEP3_DRVCONF_DISS2G_MASK 0x00400ul
197#define SILENTSTEP3_DRVCONF_TS2G_3_2US 0x00000ul
198#define SILENTSTEP3_DRVCONF_TS2G_1_6US 0x00100ul
199#define SILENTSTEP3_DRVCONF_TS2G_1_2US 0x00200ul
200#define SILENTSTEP3_DRVCONF_TS2G_0_8US 0x00300ul
201#define SILENTSTEP3_DRVCONF_TS2G_MASK 0x00300ul
202#define SILENTSTEP3_DRVCONF_SDOFF_0 0x00000ul
203#define SILENTSTEP3_DRVCONF_SDOFF_1 0x00080ul
204#define SILENTSTEP3_DRVCONF_SDOFF_MASK 0x00080ul
205#define SILENTSTEP3_DRVCONF_VSENSE_305MV 0x00000ul
206#define SILENTSTEP3_DRVCONF_VSENSE_165MV 0x00040ul
207#define SILENTSTEP3_DRVCONF_VSENSE_MASK 0x00040ul
208#define SILENTSTEP3_DRVCONF_RDSEL_MSTEP 0x00000ul
209#define SILENTSTEP3_DRVCONF_RDSEL_SG_LEVEL 0x00010ul
210#define SILENTSTEP3_DRVCONF_RDSEL_SG_CS_LEVEL 0x00020ul
211#define SILENTSTEP3_DRVCONF_RDSEL_MASK 0x00030ul
212
217#define SILENTSTEP3_PIN_STATE_LOW 0
218#define SILENTSTEP3_PIN_STATE_HIGH 1
219
224#define SILENTSTEP3_DIR_CCW 0
225#define SILENTSTEP3_DIR_CW 1
226
231#define SILENTSTEP3_MODE_FULL_STEP 8
232#define SILENTSTEP3_MODE_HALF_STEP 7
233#define SILENTSTEP3_MODE_QUARTER_STEP 6
234#define SILENTSTEP3_MODE_1_OVER_8_STEP 5
235#define SILENTSTEP3_MODE_1_OVER_16_STEP 4
236#define SILENTSTEP3_MODE_1_OVER_32_STEP 3
237#define SILENTSTEP3_MODE_1_OVER_64_STEP 2
238#define SILENTSTEP3_MODE_1_OVER_128_STEP 1
239#define SILENTSTEP3_MODE_1_OVER_256_STEP 0
240
245#define SILENTSTEP3_SPEED_VERY_SLOW 0
246#define SILENTSTEP3_SPEED_SLOW 1
247#define SILENTSTEP3_SPEED_MEDIUM 2
248#define SILENTSTEP3_SPEED_FAST 3
249#define SILENTSTEP3_SPEED_VERY_FAST 4
250
259#define SILENTSTEP3_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
260#define SILENTSTEP3_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
261
262 // silentstep3_set
263
278#define SILENTSTEP3_MAP_MIKROBUS( cfg, mikrobus ) \
279 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
280 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
281 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
282 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
283 cfg.en = MIKROBUS( mikrobus, MIKROBUS_AN ); \
284 cfg.sg = MIKROBUS( mikrobus, MIKROBUS_RST ); \
285 cfg.step = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
286 cfg.dir = MIKROBUS( mikrobus, MIKROBUS_INT )
287
288 // silentstep3_map
289 // silentstep3
290
295typedef struct
296{
297 // Output pins
298 digital_out_t en;
299 digital_out_t step;
300 digital_out_t dir;
302 // Input pins
303 digital_in_t sg;
305 // Modules
306 spi_master_t spi;
308 pin_name_t chip_select;
310 uint32_t drvctrl;
311 uint32_t chopconf;
312 uint32_t smarten;
313 uint32_t sgcsconf;
314 uint32_t drvconf;
317
322typedef struct
323{
324 // Communication gpio pins
325 pin_name_t miso;
326 pin_name_t mosi;
327 pin_name_t sck;
328 pin_name_t cs;
330 // Additional gpio pins
331 pin_name_t en;
332 pin_name_t sg;
333 pin_name_t step;
334 pin_name_t dir;
336 // static variable
337 uint32_t spi_speed;
338 spi_master_mode_t spi_mode;
339 spi_master_chip_select_polarity_t cs_polarity;
342
353
370
385
399
413err_t silentstep3_write_register ( silentstep3_t *ctx, uint8_t reg, uint32_t data_in, uint32_t *data_out );
414
435err_t silentstep3_set_step_mode ( silentstep3_t *ctx, uint8_t mode );
436
446
456
467void silentstep3_set_direction ( silentstep3_t *ctx, uint8_t dir );
468
478
489void silentstep3_set_step_pin ( silentstep3_t *ctx, uint8_t state );
490
500
516void silentstep3_drive_motor ( silentstep3_t *ctx, uint32_t steps, uint8_t speed );
517
518#ifdef __cplusplus
519}
520#endif
521#endif // SILENTSTEP3_H
522
523 // silentstep3
524
525// ------------------------------------------------------------------------ END
err_t silentstep3_write_register(silentstep3_t *ctx, uint8_t reg, uint32_t data_in, uint32_t *data_out)
Silent Step 3 write command function.
err_t silentstep3_default_cfg(silentstep3_t *ctx)
Silent Step 3 default configuration function.
void silentstep3_set_step_pin(silentstep3_t *ctx, uint8_t state)
Silent Step 3 set step pin function.
err_t silentstep3_set_step_mode(silentstep3_t *ctx, uint8_t mode)
Silent Step 3 set step mode function.
void silentstep3_set_direction(silentstep3_t *ctx, uint8_t dir)
Silent Step 3 set direction function.
err_t silentstep3_init(silentstep3_t *ctx, silentstep3_cfg_t *cfg)
Silent Step 3 initialization function.
uint8_t silentstep3_get_sg_pin(silentstep3_t *ctx)
Silent Step 3 get sg pin function.
void silentstep3_switch_direction(silentstep3_t *ctx)
Silent Step 3 switch direction function.
void silentstep3_disable_device(silentstep3_t *ctx)
Silent Step 3 disable device function.
void silentstep3_drive_motor(silentstep3_t *ctx, uint32_t steps, uint8_t speed)
Silent Step 3 driver motor function.
void silentstep3_enable_device(silentstep3_t *ctx)
Silent Step 3 enable device function.
void silentstep3_cfg_setup(silentstep3_cfg_t *cfg)
Silent Step 3 configuration object setup function.
silentstep3_return_value_t
Silent Step 3 Click return value data.
Definition silentstep3.h:348
@ SILENTSTEP3_OK
Definition silentstep3.h:349
@ SILENTSTEP3_ERROR
Definition silentstep3.h:350
This file contains SPI specific macros, functions, etc.
Silent Step 3 Click configuration object.
Definition silentstep3.h:323
pin_name_t sg
Definition silentstep3.h:332
pin_name_t step
Definition silentstep3.h:333
spi_master_chip_select_polarity_t cs_polarity
Definition silentstep3.h:339
pin_name_t sck
Definition silentstep3.h:327
spi_master_mode_t spi_mode
Definition silentstep3.h:338
pin_name_t mosi
Definition silentstep3.h:326
uint32_t spi_speed
Definition silentstep3.h:337
pin_name_t dir
Definition silentstep3.h:334
pin_name_t en
Definition silentstep3.h:331
pin_name_t miso
Definition silentstep3.h:325
pin_name_t cs
Definition silentstep3.h:328
Silent Step 3 Click context object.
Definition silentstep3.h:296
spi_master_t spi
Definition silentstep3.h:306
uint32_t drvctrl
Definition silentstep3.h:310
uint32_t drvconf
Definition silentstep3.h:314
uint32_t chopconf
Definition silentstep3.h:311
digital_out_t step
Definition silentstep3.h:299
uint32_t sgcsconf
Definition silentstep3.h:313
uint32_t smarten
Definition silentstep3.h:312
digital_out_t en
Definition silentstep3.h:298
pin_name_t chip_select
Definition silentstep3.h:308
digital_out_t dir
Definition silentstep3.h:300
digital_in_t sg
Definition silentstep3.h:303