stepper5 2.1.0.0
stepper5.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 STEPPER5_H
29#define STEPPER5_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_uart.h"
52
73#define STEPPER5_REG_GCONF 0x00
74#define STEPPER5_REG_GSTAT 0x01
75#define STEPPER5_REG_IFCNT 0x02
76#define STEPPER5_REG_SLAVECONF 0x03
77#define STEPPER5_REG_OTP_PROG 0x04
78#define STEPPER5_REG_OTP_READ 0x05
79#define STEPPER5_REG_IOIN 0x06
80#define STEPPER5_REG_FACTORY_CONF 0x07
81#define STEPPER5_REG_IHOLD_IRUN 0x10
82#define STEPPER5_REG_TPOWERDOWN 0x11
83#define STEPPER5_REG_TSTEP 0x12
84#define STEPPER5_REG_TPWMTHRS 0x13
85#define STEPPER5_REG_VACTUAL 0x22
86#define STEPPER5_REG_MSCNT 0x6A
87#define STEPPER5_REG_MSCURACT 0x6B
88#define STEPPER5_REG_CHOPCONF 0x6C
89#define STEPPER5_REG_COOLCONF 0x6D
90#define STEPPER5_REG_DRV_STATUS 0x6F
91#define STEPPER5_REG_PWMCONF 0x70
92#define STEPPER5_REG_PWM_SCALE 0x71
93#define STEPPER5_REG_PWM_AUTO 0x72
94
95 // stepper5_reg
96
111#define STEPPER5_IOIN_VERSION_MASK 0xFF000000ul
112#define STEPPER5_IOIN_VERSION_DEFAULT 0x20000000ul
113
118#define STEPPER5_DIR_CW 0
119#define STEPPER5_DIR_CCW 1
120
125#define STEPPER5_PIN_STATE_LOW 0
126#define STEPPER5_PIN_STATE_HIGH 1
127
132#define STEPPER5_SPEED_VERY_SLOW 0
133#define STEPPER5_SPEED_SLOW 1
134#define STEPPER5_SPEED_MEDIUM 2
135#define STEPPER5_SPEED_FAST 3
136#define STEPPER5_SPEED_VERY_FAST 4
137
142#define STEPPER5_GCONF_I_SCALE_ANALOG_INT_5V 0x00000000ul
143#define STEPPER5_GCONF_I_SCALE_ANALOG_EXT_VREF 0x00000001ul
144#define STEPPER5_GCONF_INTERNAL_RSENSE_EXT 0x00000000ul
145#define STEPPER5_GCONF_INTERNAL_RSENSE_INT 0x00000002ul
146#define STEPPER5_GCONF_EN_SPREAD_CYCLE_SCHOP 0x00000000ul
147#define STEPPER5_GCONF_EN_SPREAD_CYCLE_SCYCLE 0x00000004ul
148#define STEPPER5_GCONF_SHAFT_INVERSE 0x00000008ul
149#define STEPPER5_GCONF_INDEX_OTPW_FIRST_MSTEP 0x00000000ul
150#define STEPPER5_GCONF_INDEX_OTPW_OVERTEMP 0x00000010ul
151#define STEPPER5_GCONF_INDEX_STEP_OTPW 0x00000000ul
152#define STEPPER5_GCONF_INDEX_STEP_INT 0x00000020ul
153#define STEPPER5_GCONF_PDN_DISABLE_SSC_REDUCT 0x00000000ul
154#define STEPPER5_GCONF_PDN_DISABLE_UART_EN 0x00000040ul
155#define STEPPER5_GCONF_MSTEP_REG_SELECT_MS_PINS 0x00000000ul
156#define STEPPER5_GCONF_MSTEP_REG_SELECT_MSTEP 0x00000080ul
157#define STEPPER5_GCONF_MULTISTEP_FILT_DIS 0x00000000ul
158#define STEPPER5_GCONF_MULTISTEP_FILT_EN 0x00000100ul
159#define STEPPER5_GCONF_TEST_MODE_NORMAL 0x00000000ul
160#define STEPPER5_GCONF_TEST_MODE_ENN 0x00000200ul
161
166#define STEPPER5_TOFF_MASK 0x0000000Ful
167#define STEPPER5_TOFF_SHIFT 0
168#define STEPPER5_TOFF_DRIVER_DISABLE 0
169#define STEPPER5_TOFF_MIN 1
170#define STEPPER5_TOFF_DEFAULT 4
171#define STEPPER5_TOFF_MAX 15
172
177#define STEPPER5_MRES_MASK 0x0F000000ul
178#define STEPPER5_MRES_SHIFT 24
179#define STEPPER5_MRES_256 0
180#define STEPPER5_MRES_128 1
181#define STEPPER5_MRES_64 2
182#define STEPPER5_MRES_32 3
183#define STEPPER5_MRES_16 4
184#define STEPPER5_MRES_8 5
185#define STEPPER5_MRES_4 6
186#define STEPPER5_MRES_2 7
187#define STEPPER5_MRES_FULLSTEP 8
188
193#define STEPPER5_IRUN_MASK 0x00000F00ul
194#define STEPPER5_IRUN_SHIFT 8
195#define STEPPER5_IRUN_MIN 0
196#define STEPPER5_IRUN_DEFAULT 16
197#define STEPPER5_IRUN_MAX 31
198
203#define STEPPER5_UART_SYNC_BYTE 0x05
204#define STEPPER5_UART_SLAVE_ADDRESS 0x00
205#define STEPPER5_UART_WRITE_BIT 0x80
206
212#define STEPPER5_TX_DRV_BUFFER_SIZE 100
213#define STEPPER5_RX_DRV_BUFFER_SIZE 300
214
215 // stepper5_set
216
231#define STEPPER5_MAP_MIKROBUS( cfg, mikrobus ) \
232 cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
233 cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
234 cfg.diag = MIKROBUS( mikrobus, MIKROBUS_AN ); \
235 cfg.dir = MIKROBUS( mikrobus, MIKROBUS_RST ); \
236 cfg.en = MIKROBUS( mikrobus, MIKROBUS_CS ); \
237 cfg.step = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
238 cfg.ind = MIKROBUS( mikrobus, MIKROBUS_INT );
239
240 // stepper5_map
241 // stepper5
242
247typedef struct
248{
249 // Output pins
250 digital_out_t dir;
251 digital_out_t en;
252 digital_out_t step;
254 // Input pins
255 digital_in_t diag;
256 digital_in_t ind;
258 // Modules
259 uart_t uart;
261 // Buffers
262 uint8_t uart_rx_buffer[ STEPPER5_RX_DRV_BUFFER_SIZE ];
263 uint8_t uart_tx_buffer[ STEPPER5_TX_DRV_BUFFER_SIZE ];
265} stepper5_t;
266
271typedef struct
272{
273 // Communication gpio pins
274 pin_name_t rx_pin;
275 pin_name_t tx_pin;
277 // Additional gpio pins
278 pin_name_t diag;
279 pin_name_t dir;
280 pin_name_t en;
281 pin_name_t step;
282 pin_name_t ind;
284 // Static variable
285 uint32_t baud_rate;
287 uart_data_bits_t data_bit;
288 uart_parity_t parity_bit;
289 uart_stop_bits_t stop_bit;
292
297typedef enum
298{
300 STEPPER5_ERROR = -1
301
303
320
335
349
361void stepper5_write_reg ( stepper5_t *ctx, uint8_t reg, uint32_t data_in );
362
375err_t stepper5_read_reg ( stepper5_t *ctx, uint8_t reg, uint32_t *data_out );
376
388
398
408
419void stepper5_set_direction ( stepper5_t *ctx, uint8_t dir );
420
430
441void stepper5_set_step_pin ( stepper5_t *ctx, uint8_t state );
442
452
462
474err_t stepper5_set_toff ( stepper5_t *ctx, uint8_t toff );
475
496err_t stepper5_set_step_res ( stepper5_t *ctx, uint8_t mres );
497
509err_t stepper5_set_run_current ( stepper5_t *ctx, uint8_t irun );
510
526void stepper5_drive_motor ( stepper5_t *ctx, uint32_t steps, uint8_t speed );
527
528#ifdef __cplusplus
529}
530#endif
531#endif // STEPPER5_H
532
533 // stepper5
534
535// ------------------------------------------------------------------------ END
#define STEPPER5_RX_DRV_BUFFER_SIZE
Definition stepper5.h:213
#define STEPPER5_TX_DRV_BUFFER_SIZE
Stepper 5 driver buffer size.
Definition stepper5.h:212
err_t stepper5_read_reg(stepper5_t *ctx, uint8_t reg, uint32_t *data_out)
Stepper 5 read reg function.
void stepper5_disable_device(stepper5_t *ctx)
Stepper 5 disable device function.
err_t stepper5_default_cfg(stepper5_t *ctx)
Stepper 5 default configuration function.
void stepper5_switch_direction(stepper5_t *ctx)
Stepper 5 switch direction function.
err_t stepper5_set_run_current(stepper5_t *ctx, uint8_t irun)
Stepper 5 set run current function.
err_t stepper5_set_toff(stepper5_t *ctx, uint8_t toff)
Stepper 5 set toff function.
void stepper5_cfg_setup(stepper5_cfg_t *cfg)
Stepper 5 configuration object setup function.
uint8_t stepper5_get_index_pin(stepper5_t *ctx)
Stepper 5 get index pin function.
uint8_t stepper5_get_diag_pin(stepper5_t *ctx)
Stepper 5 get diag pin function.
err_t stepper5_set_step_res(stepper5_t *ctx, uint8_t mres)
Stepper 5 set step res function.
void stepper5_set_direction(stepper5_t *ctx, uint8_t dir)
Stepper 5 set direction function.
void stepper5_set_step_pin(stepper5_t *ctx, uint8_t state)
Stepper 5 set step pin function.
void stepper5_drive_motor(stepper5_t *ctx, uint32_t steps, uint8_t speed)
Stepper 5 driver motor function.
void stepper5_enable_device(stepper5_t *ctx)
Stepper 5 enable device function.
err_t stepper5_init(stepper5_t *ctx, stepper5_cfg_t *cfg)
Stepper 5 initialization function.
void stepper5_write_reg(stepper5_t *ctx, uint8_t reg, uint32_t data_in)
Stepper 5 write reg function.
err_t stepper5_check_com(stepper5_t *ctx)
Stepper 5 check com function.
stepper5_return_value_t
Stepper 5 Click return value data.
Definition stepper5.h:298
@ STEPPER5_ERROR
Definition stepper5.h:300
@ STEPPER5_OK
Definition stepper5.h:299
Stepper 5 Click configuration object.
Definition stepper5.h:272
pin_name_t diag
Definition stepper5.h:278
uint32_t baud_rate
Definition stepper5.h:285
pin_name_t step
Definition stepper5.h:281
bool uart_blocking
Definition stepper5.h:286
uart_data_bits_t data_bit
Definition stepper5.h:287
pin_name_t tx_pin
Definition stepper5.h:275
pin_name_t rx_pin
Definition stepper5.h:274
uart_stop_bits_t stop_bit
Definition stepper5.h:289
pin_name_t dir
Definition stepper5.h:279
pin_name_t en
Definition stepper5.h:280
uart_parity_t parity_bit
Definition stepper5.h:288
pin_name_t ind
Definition stepper5.h:282
Stepper 5 Click context object.
Definition stepper5.h:248
uart_t uart
Definition stepper5.h:259
digital_out_t step
Definition stepper5.h:252
digital_out_t en
Definition stepper5.h:251
digital_in_t ind
Definition stepper5.h:256
digital_in_t diag
Definition stepper5.h:255
digital_out_t dir
Definition stepper5.h:250