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 
35 #include "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_spi_master.h"
38 
59 #define STEPPER16_REG_CR1 0x01
60 #define STEPPER16_REG_CR2 0x02
61 #define STEPPER16_REG_CR3 0x03
62 #define STEPPER16_REG_CR4 0x04
63 #define STEPPER16_REG_CR5 0x0B
64 #define STEPPER16_REG_CR6 0x0C
65 #define STEPPER16_REG_SR1 0x05
66 #define STEPPER16_REG_SR2 0x06
67 #define STEPPER16_REG_SR3 0x07
68 #define STEPPER16_REG_SR4 0x08
69 #define STEPPER16_REG_SR5 0x09
70 #define STEPPER16_REG_SR6 0x0A
71  // stepper16_reg
73 
88 #define STEPPER16_STEP_RES_FULL 0x7
89 #define STEPPER16_STEP_RES_HALF 0x3
90 #define STEPPER16_STEP_RES_QUARTER 0x2
91 #define STEPPER16_STEP_RES_1div8 0x1
92 #define STEPPER16_STEP_RES_1div16 0x0
93 
98 #define STEPPER16_SPEED_VERY_SLOW 1
99 #define STEPPER16_SPEED_SLOW 2
100 #define STEPPER16_SPEED_MEDIUM 3
101 #define STEPPER16_SPEED_FAST 4
102 #define STEPPER16_SPEED_VERY_FAST 5
103  // stepper16_set
105 
120 #define STEPPER16_MAP_MIKROBUS( cfg, mikrobus ) \
121  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
122  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
123  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
124  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
125  cfg.dir = MIKROBUS( mikrobus, MIKROBUS_RST ); \
126  cfg.nxt = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
127  cfg.err = MIKROBUS( mikrobus, MIKROBUS_INT )
128  // stepper16_map // stepper16
131 
136 typedef struct
137 {
138  // Output pins
139  digital_out_t dir;
140  digital_out_t nxt;
141  digital_out_t cs;
143  // Input pins
144  digital_in_t err;
146  // Modules
147  spi_master_t spi;
148  pin_name_t chip_select;
150  //status bits
151  uint8_t spierr;
152  uint8_t uv;
153  uint8_t eldef;
154  uint8_t tsd;
155  uint8_t tw;
157  uint16_t steps;
158  float resolution;
159  uint8_t step_resolution;
161 } stepper16_t;
162 
167 typedef struct
168 {
169  // Communication gpio pins
170  pin_name_t miso;
171  pin_name_t mosi;
172  pin_name_t sck;
173  pin_name_t cs;
175  // Additional gpio pins
176  pin_name_t dir;
177  pin_name_t nxt;
178  pin_name_t err;
180  // static variable
181  uint32_t spi_speed;
182  spi_master_mode_t spi_mode;
183  spi_master_chip_select_polarity_t cs_polarity;
186 
191 typedef enum
192 {
194  STEPPER16_ERROR = -1
195 
197 
214 
229 
244 
258 err_t stepper16_generic_write ( stepper16_t *ctx, uint8_t reg, uint16_t data_in );
259 
273 err_t stepper16_generic_read ( stepper16_t *ctx, uint8_t reg, uint16_t *data_out );
274 
283 void stepper16_set_next ( stepper16_t *ctx, uint8_t value );
284 
293 void stepper16_set_dir ( stepper16_t *ctx, uint8_t value );
294 
303 
314 
323 
337 void stepper16_set_step_resolution ( stepper16_t *ctx, uint8_t step_res );
338 
353 void stepper16_move_motor_angle ( stepper16_t *ctx, float degree, uint8_t speed );
354 
369 void stepper16_move_motor_step ( stepper16_t *ctx, uint16_t steps, uint8_t speed );
370 
380 
381 #ifdef __cplusplus
382 }
383 #endif
384 #endif // STEPPER16_H
385  // stepper16
387 
388 // ------------------------------------------------------------------------ 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:147
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:171
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:170
stepper16_t::dir
digital_out_t dir
Definition: stepper16.h:139
stepper16_t::err
digital_in_t err
Definition: stepper16.h:144
stepper16_cfg_t::dir
pin_name_t dir
Definition: stepper16.h:176
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:153
stepper16_t
Stepper16 Click context object.
Definition: stepper16.h:137
stepper16_t::uv
uint8_t uv
Definition: stepper16.h:152
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:159
stepper16_t::resolution
float resolution
Definition: stepper16.h:158
stepper16_cfg_t::nxt
pin_name_t nxt
Definition: stepper16.h:177
stepper16_cfg_t::cs
pin_name_t cs
Definition: stepper16.h:173
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:172
STEPPER16_ERROR
@ STEPPER16_ERROR
Definition: stepper16.h:194
stepper16_set_dir
void stepper16_set_dir(stepper16_t *ctx, uint8_t value)
Set direction.
stepper16_t::tsd
uint8_t tsd
Definition: stepper16.h:154
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:157
stepper16_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: stepper16.h:183
stepper16_return_value_t
stepper16_return_value_t
Stepper16 Click return value data.
Definition: stepper16.h:192
stepper16_t::cs
digital_out_t cs
Definition: stepper16.h:141
stepper16_default_cfg
err_t stepper16_default_cfg(stepper16_t *ctx)
Stepper16 default configuration function.
stepper16_t::tw
uint8_t tw
Definition: stepper16.h:155
stepper16_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: stepper16.h:182
stepper16_cfg_t::err
pin_name_t err
Definition: stepper16.h:178
stepper16_wake_up
void stepper16_wake_up(stepper16_t *ctx)
Transimt wake up signal.
stepper16_cfg_t
Stepper16 Click configuration object.
Definition: stepper16.h:168
stepper16_t::chip_select
pin_name_t chip_select
Definition: stepper16.h:148
stepper16_t::spierr
uint8_t spierr
Definition: stepper16.h:151
stepper16_cfg_setup
void stepper16_cfg_setup(stepper16_cfg_t *cfg)
Stepper16 configuration object setup function.
STEPPER16_OK
@ STEPPER16_OK
Definition: stepper16.h:193
stepper16_cfg_t::spi_speed
uint32_t spi_speed
Definition: stepper16.h:181
stepper16_t::nxt
digital_out_t nxt
Definition: stepper16.h:140