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
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_uart.h"
48 
69 #define STEPPER5_REG_GCONF 0x00
70 #define STEPPER5_REG_GSTAT 0x01
71 #define STEPPER5_REG_IFCNT 0x02
72 #define STEPPER5_REG_SLAVECONF 0x03
73 #define STEPPER5_REG_OTP_PROG 0x04
74 #define STEPPER5_REG_OTP_READ 0x05
75 #define STEPPER5_REG_IOIN 0x06
76 #define STEPPER5_REG_FACTORY_CONF 0x07
77 #define STEPPER5_REG_IHOLD_IRUN 0x10
78 #define STEPPER5_REG_TPOWERDOWN 0x11
79 #define STEPPER5_REG_TSTEP 0x12
80 #define STEPPER5_REG_TPWMTHRS 0x13
81 #define STEPPER5_REG_VACTUAL 0x22
82 #define STEPPER5_REG_MSCNT 0x6A
83 #define STEPPER5_REG_MSCURACT 0x6B
84 #define STEPPER5_REG_CHOPCONF 0x6C
85 #define STEPPER5_REG_COOLCONF 0x6D
86 #define STEPPER5_REG_DRV_STATUS 0x6F
87 #define STEPPER5_REG_PWMCONF 0x70
88 #define STEPPER5_REG_PWM_SCALE 0x71
89 #define STEPPER5_REG_PWM_AUTO 0x72
90  // stepper5_reg
92 
107 #define STEPPER5_IOIN_VERSION_MASK 0xFF000000ul
108 #define STEPPER5_IOIN_VERSION_DEFAULT 0x20000000ul
109 
114 #define STEPPER5_DIR_CW 0
115 #define STEPPER5_DIR_CCW 1
116 
121 #define STEPPER5_PIN_STATE_LOW 0
122 #define STEPPER5_PIN_STATE_HIGH 1
123 
128 #define STEPPER5_SPEED_VERY_SLOW 0
129 #define STEPPER5_SPEED_SLOW 1
130 #define STEPPER5_SPEED_MEDIUM 2
131 #define STEPPER5_SPEED_FAST 3
132 #define STEPPER5_SPEED_VERY_FAST 4
133 
138 #define STEPPER5_GCONF_I_SCALE_ANALOG_INT_5V 0x00000000ul
139 #define STEPPER5_GCONF_I_SCALE_ANALOG_EXT_VREF 0x00000001ul
140 #define STEPPER5_GCONF_INTERNAL_RSENSE_EXT 0x00000000ul
141 #define STEPPER5_GCONF_INTERNAL_RSENSE_INT 0x00000002ul
142 #define STEPPER5_GCONF_EN_SPREAD_CYCLE_SCHOP 0x00000000ul
143 #define STEPPER5_GCONF_EN_SPREAD_CYCLE_SCYCLE 0x00000004ul
144 #define STEPPER5_GCONF_SHAFT_INVERSE 0x00000008ul
145 #define STEPPER5_GCONF_INDEX_OTPW_FIRST_MSTEP 0x00000000ul
146 #define STEPPER5_GCONF_INDEX_OTPW_OVERTEMP 0x00000010ul
147 #define STEPPER5_GCONF_INDEX_STEP_OTPW 0x00000000ul
148 #define STEPPER5_GCONF_INDEX_STEP_INT 0x00000020ul
149 #define STEPPER5_GCONF_PDN_DISABLE_SSC_REDUCT 0x00000000ul
150 #define STEPPER5_GCONF_PDN_DISABLE_UART_EN 0x00000040ul
151 #define STEPPER5_GCONF_MSTEP_REG_SELECT_MS_PINS 0x00000000ul
152 #define STEPPER5_GCONF_MSTEP_REG_SELECT_MSTEP 0x00000080ul
153 #define STEPPER5_GCONF_MULTISTEP_FILT_DIS 0x00000000ul
154 #define STEPPER5_GCONF_MULTISTEP_FILT_EN 0x00000100ul
155 #define STEPPER5_GCONF_TEST_MODE_NORMAL 0x00000000ul
156 #define STEPPER5_GCONF_TEST_MODE_ENN 0x00000200ul
157 
162 #define STEPPER5_TOFF_MASK 0x0000000Ful
163 #define STEPPER5_TOFF_SHIFT 0
164 #define STEPPER5_TOFF_DRIVER_DISABLE 0
165 #define STEPPER5_TOFF_MIN 1
166 #define STEPPER5_TOFF_DEFAULT 4
167 #define STEPPER5_TOFF_MAX 15
168 
173 #define STEPPER5_MRES_MASK 0x0F000000ul
174 #define STEPPER5_MRES_SHIFT 24
175 #define STEPPER5_MRES_256 0
176 #define STEPPER5_MRES_128 1
177 #define STEPPER5_MRES_64 2
178 #define STEPPER5_MRES_32 3
179 #define STEPPER5_MRES_16 4
180 #define STEPPER5_MRES_8 5
181 #define STEPPER5_MRES_4 6
182 #define STEPPER5_MRES_2 7
183 #define STEPPER5_MRES_FULLSTEP 8
184 
189 #define STEPPER5_IRUN_MASK 0x00000F00ul
190 #define STEPPER5_IRUN_SHIFT 8
191 #define STEPPER5_IRUN_MIN 0
192 #define STEPPER5_IRUN_DEFAULT 16
193 #define STEPPER5_IRUN_MAX 31
194 
199 #define STEPPER5_UART_SYNC_BYTE 0x05
200 #define STEPPER5_UART_SLAVE_ADDRESS 0x00
201 #define STEPPER5_UART_WRITE_BIT 0x80
202 
208 #define STEPPER5_TX_DRV_BUFFER_SIZE 100
209 #define STEPPER5_RX_DRV_BUFFER_SIZE 300
210  // stepper5_set
212 
227 #define STEPPER5_MAP_MIKROBUS( cfg, mikrobus ) \
228  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
229  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
230  cfg.diag = MIKROBUS( mikrobus, MIKROBUS_AN ); \
231  cfg.dir = MIKROBUS( mikrobus, MIKROBUS_RST ); \
232  cfg.en = MIKROBUS( mikrobus, MIKROBUS_CS ); \
233  cfg.step = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
234  cfg.ind = MIKROBUS( mikrobus, MIKROBUS_INT );
235  // stepper5_map // stepper5
238 
243 typedef struct
244 {
245  // Output pins
246  digital_out_t dir;
247  digital_out_t en;
248  digital_out_t step;
250  // Input pins
251  digital_in_t diag;
252  digital_in_t ind;
254  // Modules
255  uart_t uart;
257  // Buffers
258  uint8_t uart_rx_buffer[ STEPPER5_RX_DRV_BUFFER_SIZE ];
259  uint8_t uart_tx_buffer[ STEPPER5_TX_DRV_BUFFER_SIZE ];
261 } stepper5_t;
262 
267 typedef struct
268 {
269  // Communication gpio pins
270  pin_name_t rx_pin;
271  pin_name_t tx_pin;
273  // Additional gpio pins
274  pin_name_t diag;
275  pin_name_t dir;
276  pin_name_t en;
277  pin_name_t step;
278  pin_name_t ind;
280  // Static variable
281  uint32_t baud_rate;
283  uart_data_bits_t data_bit;
284  uart_parity_t parity_bit;
285  uart_stop_bits_t stop_bit;
288 
293 typedef enum
294 {
296  STEPPER5_ERROR = -1
297 
299 
316 
331 
345 
357 void stepper5_write_reg ( stepper5_t *ctx, uint8_t reg, uint32_t data_in );
358 
371 err_t stepper5_read_reg ( stepper5_t *ctx, uint8_t reg, uint32_t *data_out );
372 
384 
394 
404 
415 void stepper5_set_direction ( stepper5_t *ctx, uint8_t dir );
416 
426 
437 void stepper5_set_step_pin ( stepper5_t *ctx, uint8_t state );
438 
448 
458 
470 err_t stepper5_set_toff ( stepper5_t *ctx, uint8_t toff );
471 
492 err_t stepper5_set_step_res ( stepper5_t *ctx, uint8_t mres );
493 
505 err_t stepper5_set_run_current ( stepper5_t *ctx, uint8_t irun );
506 
522 void stepper5_drive_motor ( stepper5_t *ctx, uint32_t steps, uint8_t speed );
523 
524 #ifdef __cplusplus
525 }
526 #endif
527 #endif // STEPPER5_H
528  // stepper5
530 
531 // ------------------------------------------------------------------------ END
stepper5_t::diag
digital_in_t diag
Definition: stepper5.h:251
stepper5_set_step_res
err_t stepper5_set_step_res(stepper5_t *ctx, uint8_t mres)
Stepper 5 set step res function.
stepper5_cfg_t::en
pin_name_t en
Definition: stepper5.h:276
stepper5_cfg_setup
void stepper5_cfg_setup(stepper5_cfg_t *cfg)
Stepper 5 configuration object setup function.
stepper5_t::uart
uart_t uart
Definition: stepper5.h:255
STEPPER5_ERROR
@ STEPPER5_ERROR
Definition: stepper5.h:296
stepper5_switch_direction
void stepper5_switch_direction(stepper5_t *ctx)
Stepper 5 switch direction function.
stepper5_set_run_current
err_t stepper5_set_run_current(stepper5_t *ctx, uint8_t irun)
Stepper 5 set run current function.
stepper5_write_reg
void stepper5_write_reg(stepper5_t *ctx, uint8_t reg, uint32_t data_in)
Stepper 5 write reg function.
stepper5_cfg_t
Stepper 5 Click configuration object.
Definition: stepper5.h:268
stepper5_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: stepper5.h:285
STEPPER5_RX_DRV_BUFFER_SIZE
#define STEPPER5_RX_DRV_BUFFER_SIZE
Definition: stepper5.h:209
stepper5_set_step_pin
void stepper5_set_step_pin(stepper5_t *ctx, uint8_t state)
Stepper 5 set step pin function.
stepper5_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: stepper5.h:284
stepper5_set_direction
void stepper5_set_direction(stepper5_t *ctx, uint8_t dir)
Stepper 5 set direction function.
stepper5_cfg_t::ind
pin_name_t ind
Definition: stepper5.h:278
stepper5_cfg_t::uart_blocking
bool uart_blocking
Definition: stepper5.h:282
stepper5_t::ind
digital_in_t ind
Definition: stepper5.h:252
stepper5_cfg_t::dir
pin_name_t dir
Definition: stepper5.h:275
stepper5_t
Stepper 5 Click context object.
Definition: stepper5.h:244
stepper5_t::dir
digital_out_t dir
Definition: stepper5.h:246
stepper5_cfg_t::rx_pin
pin_name_t rx_pin
Definition: stepper5.h:270
stepper5_check_com
err_t stepper5_check_com(stepper5_t *ctx)
Stepper 5 check com function.
stepper5_init
err_t stepper5_init(stepper5_t *ctx, stepper5_cfg_t *cfg)
Stepper 5 initialization function.
stepper5_cfg_t::tx_pin
pin_name_t tx_pin
Definition: stepper5.h:271
stepper5_drive_motor
void stepper5_drive_motor(stepper5_t *ctx, uint32_t steps, uint8_t speed)
Stepper 5 driver motor function.
stepper5_default_cfg
err_t stepper5_default_cfg(stepper5_t *ctx)
Stepper 5 default configuration function.
stepper5_set_toff
err_t stepper5_set_toff(stepper5_t *ctx, uint8_t toff)
Stepper 5 set toff function.
stepper5_cfg_t::baud_rate
uint32_t baud_rate
Definition: stepper5.h:281
stepper5_return_value_t
stepper5_return_value_t
Stepper 5 Click return value data.
Definition: stepper5.h:294
stepper5_disable_device
void stepper5_disable_device(stepper5_t *ctx)
Stepper 5 disable device function.
stepper5_t::step
digital_out_t step
Definition: stepper5.h:248
STEPPER5_OK
@ STEPPER5_OK
Definition: stepper5.h:295
stepper5_cfg_t::step
pin_name_t step
Definition: stepper5.h:277
stepper5_t::en
digital_out_t en
Definition: stepper5.h:247
stepper5_get_diag_pin
uint8_t stepper5_get_diag_pin(stepper5_t *ctx)
Stepper 5 get diag pin function.
stepper5_read_reg
err_t stepper5_read_reg(stepper5_t *ctx, uint8_t reg, uint32_t *data_out)
Stepper 5 read reg function.
stepper5_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: stepper5.h:283
stepper5_cfg_t::diag
pin_name_t diag
Definition: stepper5.h:274
stepper5_get_index_pin
uint8_t stepper5_get_index_pin(stepper5_t *ctx)
Stepper 5 get index pin function.
stepper5_enable_device
void stepper5_enable_device(stepper5_t *ctx)
Stepper 5 enable device function.
STEPPER5_TX_DRV_BUFFER_SIZE
#define STEPPER5_TX_DRV_BUFFER_SIZE
Stepper 5 driver buffer size.
Definition: stepper5.h:208