stepper7 2.1.0.0
stepper7.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 STEPPER7_H
29#define STEPPER7_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 STEPPER7_REG_IODIR 0x00
75#define STEPPER7_REG_IPOL 0x01
76#define STEPPER7_REG_GPINTEN 0x02
77#define STEPPER7_REG_DEFVAL 0x03
78#define STEPPER7_REG_INTCON 0x04
79#define STEPPER7_REG_IOCON 0x05
80#define STEPPER7_REG_GPPU 0x06
81#define STEPPER7_REG_INTF 0x07
82#define STEPPER7_REG_INTCAP 0x08
83#define STEPPER7_REG_GPIO 0x09
84#define STEPPER7_REG_OLAT 0x0A
85
86 // stepper7_reg
87
102#define STEPPER7_PIN_PHASE2 0x80
103#define STEPPER7_PIN_PHASE1 0x40
104#define STEPPER7_PIN_I12 0x20
105#define STEPPER7_PIN_I02 0x10
106#define STEPPER7_PIN_I11 0x08
107#define STEPPER7_PIN_I01 0x04
108
113#define STEPPER7_IODIR_DEFAULT 0x03
114#define STEPPER7_IOCON_DEFAULT 0x38
115#define STEPPER7_GPIO_CLEAR 0x00
116
121#define STEPPER7_DIR_CW 0
122#define STEPPER7_DIR_CCW 1
123
128#define STEPPER7_PIN_LOW_LEVEL 0
129#define STEPPER7_PIN_HIGH_LEVEL 1
130
135#define STEPPER7_MODE_FULL_STEP 0
136#define STEPPER7_MODE_HALF_STEP 1
137#define STEPPER7_MODE_1_OVER_8_STEP 2
138
143#define STEPPER7_SPEED_VERY_SLOW 0
144#define STEPPER7_SPEED_SLOW 1
145#define STEPPER7_SPEED_MEDIUM 2
146#define STEPPER7_SPEED_FAST 3
147#define STEPPER7_SPEED_VERY_FAST 4
148
153#define STEPPER7_SPI_READ_OPCODE 0x41
154#define STEPPER7_SPI_WRITE_OPCODE 0x40
155
164#define STEPPER7_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
165#define STEPPER7_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
166
167 // stepper7_set
168
183#define STEPPER7_MAP_MIKROBUS( cfg, mikrobus ) \
184 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
185 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
186 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
187 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
188 cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST );
189
190 // stepper7_map
191 // stepper7
192
197typedef struct
198{
199 // Output pins
200 digital_out_t rst;
202 // Modules
203 spi_master_t spi;
205 pin_name_t chip_select;
207 uint8_t step_mode;
208 uint8_t direction;
210} stepper7_t;
211
216typedef struct
217{
218 // Communication gpio pins
219 pin_name_t miso;
220 pin_name_t mosi;
221 pin_name_t sck;
222 pin_name_t cs;
224 // Additional gpio pins
225 pin_name_t rst;
227 // static variable
228 uint32_t spi_speed;
229 spi_master_mode_t spi_mode;
230 spi_master_chip_select_polarity_t cs_polarity;
233
238typedef enum
239{
241 STEPPER7_ERROR = -1
242
244
261
276
290
303err_t stepper7_write_reg ( stepper7_t *ctx, uint8_t reg, uint8_t data_in );
304
317err_t stepper7_read_reg ( stepper7_t *ctx, uint8_t reg, uint8_t *data_out );
318
329void stepper7_set_rst_pin ( stepper7_t *ctx, uint8_t state );
330
340
353err_t stepper7_set_phase2_pin ( stepper7_t *ctx, uint8_t state );
354
367err_t stepper7_set_phase1_pin ( stepper7_t *ctx, uint8_t state );
368
381err_t stepper7_set_i12_pin ( stepper7_t *ctx, uint8_t state );
382
395err_t stepper7_set_i02_pin ( stepper7_t *ctx, uint8_t state );
396
409err_t stepper7_set_i11_pin ( stepper7_t *ctx, uint8_t state );
410
423err_t stepper7_set_i01_pin ( stepper7_t *ctx, uint8_t state );
424
436
448
461void stepper7_set_step_mode ( stepper7_t *ctx, uint8_t mode );
462
473void stepper7_set_direction ( stepper7_t *ctx, uint8_t dir );
474
484
502err_t stepper7_drive_motor ( stepper7_t *ctx, uint32_t steps, uint8_t speed );
503
504#ifdef __cplusplus
505}
506#endif
507#endif // STEPPER7_H
508
509 // stepper7
510
511// ------------------------------------------------------------------------ END
err_t stepper7_init(stepper7_t *ctx, stepper7_cfg_t *cfg)
Stepper 7 initialization function.
err_t stepper7_set_i12_pin(stepper7_t *ctx, uint8_t state)
Stepper 7 set I12 pin function.
err_t stepper7_disable_device(stepper7_t *ctx)
Stepper 7 disable device function.
err_t stepper7_read_reg(stepper7_t *ctx, uint8_t reg, uint8_t *data_out)
Stepper 7 read reg function.
err_t stepper7_drive_motor(stepper7_t *ctx, uint32_t steps, uint8_t speed)
Stepper 7 driver motor function.
void stepper7_cfg_setup(stepper7_cfg_t *cfg)
Stepper 7 configuration object setup function.
err_t stepper7_write_reg(stepper7_t *ctx, uint8_t reg, uint8_t data_in)
Stepper 7 write reg function.
err_t stepper7_set_i02_pin(stepper7_t *ctx, uint8_t state)
Stepper 7 set I02 pin function.
err_t stepper7_enable_device(stepper7_t *ctx)
Stepper 7 enable device function.
void stepper7_set_direction(stepper7_t *ctx, uint8_t dir)
Stepper 7 set direction function.
err_t stepper7_default_cfg(stepper7_t *ctx)
Stepper 7 default configuration function.
err_t stepper7_set_i01_pin(stepper7_t *ctx, uint8_t state)
Stepper 7 set I01 pin function.
err_t stepper7_set_phase1_pin(stepper7_t *ctx, uint8_t state)
Stepper 7 set PHASE1 pin function.
void stepper7_switch_direction(stepper7_t *ctx)
Stepper 7 switch direction function.
err_t stepper7_set_phase2_pin(stepper7_t *ctx, uint8_t state)
Stepper 7 set PHASE2 pin function.
void stepper7_set_step_mode(stepper7_t *ctx, uint8_t mode)
Stepper 7 set step mode function.
err_t stepper7_set_i11_pin(stepper7_t *ctx, uint8_t state)
Stepper 7 set I11 pin function.
void stepper7_reset_device(stepper7_t *ctx)
Stepper 7 reset device function.
void stepper7_set_rst_pin(stepper7_t *ctx, uint8_t state)
Stepper 7 set rst pin function.
This file contains SPI specific macros, functions, etc.
stepper7_return_value_t
Stepper 7 Click return value data.
Definition stepper7.h:239
@ STEPPER7_OK
Definition stepper7.h:240
@ STEPPER7_ERROR
Definition stepper7.h:241
Stepper 7 Click configuration object.
Definition stepper7.h:217
spi_master_chip_select_polarity_t cs_polarity
Definition stepper7.h:230
pin_name_t sck
Definition stepper7.h:221
spi_master_mode_t spi_mode
Definition stepper7.h:229
pin_name_t mosi
Definition stepper7.h:220
uint32_t spi_speed
Definition stepper7.h:228
pin_name_t miso
Definition stepper7.h:219
pin_name_t rst
Definition stepper7.h:225
pin_name_t cs
Definition stepper7.h:222
Stepper 7 Click context object.
Definition stepper7.h:198
spi_master_t spi
Definition stepper7.h:203
uint8_t step_mode
Definition stepper7.h:207
digital_out_t rst
Definition stepper7.h:200
uint8_t direction
Definition stepper7.h:208
pin_name_t chip_select
Definition stepper7.h:205