stepper16  2.0.0.0
stepper16.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 STEPPER16_H
29 #define STEPPER16_H
30 
31 #ifdef __cplusplus
32 extern "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 
73 #define STEPPER16_REG_CR1 0x01
74 #define STEPPER16_REG_CR2 0x02
75 #define STEPPER16_REG_CR3 0x03
76 #define STEPPER16_REG_CR4 0x04
77 #define STEPPER16_REG_CR5 0x0B
78 #define STEPPER16_REG_CR6 0x0C
79 #define STEPPER16_REG_SR1 0x05
80 #define STEPPER16_REG_SR2 0x06
81 #define STEPPER16_REG_SR3 0x07
82 #define STEPPER16_REG_SR4 0x08
83 #define STEPPER16_REG_SR5 0x09
84 #define STEPPER16_REG_SR6 0x0A
85  // stepper16_reg
87 
102 #define STEPPER16_STEP_RES_FULL 0x7
103 #define STEPPER16_STEP_RES_HALF 0x3
104 #define STEPPER16_STEP_RES_QUARTER 0x2
105 #define STEPPER16_STEP_RES_1div8 0x1
106 #define STEPPER16_STEP_RES_1div16 0x0
107 
112 #define STEPPER16_SPEED_VERY_SLOW 1
113 #define STEPPER16_SPEED_SLOW 2
114 #define STEPPER16_SPEED_MEDIUM 3
115 #define STEPPER16_SPEED_FAST 4
116 #define STEPPER16_SPEED_VERY_FAST 5
117  // stepper16_set
119 
134 #define STEPPER16_MAP_MIKROBUS( cfg, mikrobus ) \
135  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
136  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
137  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
138  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
139  cfg.dir = MIKROBUS( mikrobus, MIKROBUS_RST ); \
140  cfg.nxt = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
141  cfg.err = MIKROBUS( mikrobus, MIKROBUS_INT )
142  // stepper16_map // stepper16
145 
150 typedef struct
151 {
152  // Output pins
153  digital_out_t dir;
154  digital_out_t nxt;
155  digital_out_t cs;
157  // Input pins
158  digital_in_t err;
160  // Modules
161  spi_master_t spi;
162  pin_name_t chip_select;
164  //status bits
165  uint8_t spierr;
166  uint8_t uv;
167  uint8_t eldef;
168  uint8_t tsd;
169  uint8_t tw;
171  uint16_t steps;
172  float resolution;
173  uint8_t step_resolution;
175 } stepper16_t;
176 
181 typedef struct
182 {
183  // Communication gpio pins
184  pin_name_t miso;
185  pin_name_t mosi;
186  pin_name_t sck;
187  pin_name_t cs;
189  // Additional gpio pins
190  pin_name_t dir;
191  pin_name_t nxt;
192  pin_name_t err;
194  // static variable
195  uint32_t spi_speed;
196  spi_master_mode_t spi_mode;
197  spi_master_chip_select_polarity_t cs_polarity;
200 
205 typedef enum
206 {
208  STEPPER16_ERROR = -1
209 
211 
228 
243 
258 
272 err_t stepper16_generic_write ( stepper16_t *ctx, uint8_t reg, uint16_t data_in );
273 
287 err_t stepper16_generic_read ( stepper16_t *ctx, uint8_t reg, uint16_t *data_out );
288 
297 void stepper16_set_next ( stepper16_t *ctx, uint8_t value );
298 
307 void stepper16_set_dir ( stepper16_t *ctx, uint8_t value );
308 
317 
328 
337 
351 void stepper16_set_step_resolution ( stepper16_t *ctx, uint8_t step_res );
352 
367 void stepper16_move_motor_angle ( stepper16_t *ctx, float degree, uint8_t speed );
368 
383 void stepper16_move_motor_step ( stepper16_t *ctx, uint16_t steps, uint8_t speed );
384 
394 
395 #ifdef __cplusplus
396 }
397 #endif
398 #endif // STEPPER16_H
399  // stepper16
401 
402 // ------------------------------------------------------------------------ END
stepper16_generic_write
err_t stepper16_generic_write(stepper16_t *ctx, uint8_t reg, uint16_t data_in)
Stepper16 data writing function.
stepper16_t::spi
spi_master_t spi
Definition: stepper16.h:161
stepper16_get_error_flag
uint8_t stepper16_get_error_flag(stepper16_t *ctx)
Get error.
stepper16_init
err_t stepper16_init(stepper16_t *ctx, stepper16_cfg_t *cfg)
Stepper16 initialization function.
stepper16_cfg_t::mosi
pin_name_t mosi
Definition: stepper16.h:185
stepper16_set_next
void stepper16_set_next(stepper16_t *ctx, uint8_t value)
Set next step state.
stepper16_cfg_t::miso
pin_name_t miso
Definition: stepper16.h:184
stepper16_t::dir
digital_out_t dir
Definition: stepper16.h:153
stepper16_t::err
digital_in_t err
Definition: stepper16.h:158
stepper16_cfg_t::dir
pin_name_t dir
Definition: stepper16.h:190
stepper16_move_motor_step
void stepper16_move_motor_step(stepper16_t *ctx, uint16_t steps, uint8_t speed)
Move motor in step value.
stepper16_t::eldef
uint8_t eldef
Definition: stepper16.h:167
stepper16_t
Stepper16 Click context object.
Definition: stepper16.h:151
stepper16_t::uv
uint8_t uv
Definition: stepper16.h:166
stepper16_set_step_resolution
void stepper16_set_step_resolution(stepper16_t *ctx, uint8_t step_res)
Set step resolution.
stepper16_t::step_resolution
uint8_t step_resolution
Definition: stepper16.h:173
stepper16_t::resolution
float resolution
Definition: stepper16.h:172
stepper16_cfg_t::nxt
pin_name_t nxt
Definition: stepper16.h:191
stepper16_cfg_t::cs
pin_name_t cs
Definition: stepper16.h:187
stepper16_move_motor_angle
void stepper16_move_motor_angle(stepper16_t *ctx, float degree, uint8_t speed)
Move motor in angle value.
stepper16_cfg_t::sck
pin_name_t sck
Definition: stepper16.h:186
STEPPER16_ERROR
@ STEPPER16_ERROR
Definition: stepper16.h:208
stepper16_set_dir
void stepper16_set_dir(stepper16_t *ctx, uint8_t value)
Set direction.
stepper16_t::tsd
uint8_t tsd
Definition: stepper16.h:168
stepper16_hard_reset
void stepper16_hard_reset(stepper16_t *ctx)
Resets device.
stepper16_calculate_resolution
void stepper16_calculate_resolution(stepper16_t *ctx)
Calculate step-degree resolution.
stepper16_generic_read
err_t stepper16_generic_read(stepper16_t *ctx, uint8_t reg, uint16_t *data_out)
Stepper16 data reading function.
stepper16_t::steps
uint16_t steps
Definition: stepper16.h:171
stepper16_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: stepper16.h:197
stepper16_return_value_t
stepper16_return_value_t
Stepper16 Click return value data.
Definition: stepper16.h:206
stepper16_t::cs
digital_out_t cs
Definition: stepper16.h:155
stepper16_default_cfg
err_t stepper16_default_cfg(stepper16_t *ctx)
Stepper16 default configuration function.
stepper16_t::tw
uint8_t tw
Definition: stepper16.h:169
stepper16_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: stepper16.h:196
stepper16_cfg_t::err
pin_name_t err
Definition: stepper16.h:192
stepper16_wake_up
void stepper16_wake_up(stepper16_t *ctx)
Transimt wake up signal.
stepper16_cfg_t
Stepper16 Click configuration object.
Definition: stepper16.h:182
stepper16_t::chip_select
pin_name_t chip_select
Definition: stepper16.h:162
stepper16_t::spierr
uint8_t spierr
Definition: stepper16.h:165
stepper16_cfg_setup
void stepper16_cfg_setup(stepper16_cfg_t *cfg)
Stepper16 configuration object setup function.
STEPPER16_OK
@ STEPPER16_OK
Definition: stepper16.h:207
stepper16_cfg_t::spi_speed
uint32_t spi_speed
Definition: stepper16.h:195
stepper16_t::nxt
digital_out_t nxt
Definition: stepper16.h:154