servo2 2.1.0.0
Loading...
Searching...
No Matches
servo2.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
27
28#ifndef SERVO2_H
29#define SERVO2_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_pwm.h"
52
58
63
68
78#define SERVO2_PWM_MAX 4095
79#define SERVO2_PWM_MIN 0
80#define SERVO2_PWM_ZERO_DEGREE_PULSE 3980
81#define SERVO2_PWM_180_DEGREE_PULSE 3468
82
87#define SERVO2_ANGLE_MIN 0
88#define SERVO2_ANGLE_MAX 180
89#define SERVO2_CHANNEL_0 0
90#define SERVO2_CHANNEL_1 1
91#define SERVO2_CHANNEL_2 2
92#define SERVO2_CHANNEL_3 3
93#define SERVO2_CHANNEL_4 4
94#define SERVO2_CHANNEL_5 5
95#define SERVO2_CHANNEL_6 6
96#define SERVO2_CHANNEL_7 7
97#define SERVO2_CHANNEL_8 8
98#define SERVO2_CHANNEL_9 9
99#define SERVO2_CHANNEL_10 10
100#define SERVO2_CHANNEL_11 11
101#define SERVO2_CHANNEL_12 12
102#define SERVO2_CHANNEL_13 13
103#define SERVO2_CHANNEL_14 14
104#define SERVO2_CHANNEL_15 15
105#define SERVO2_CHANNEL_ALL 16
106#define SERVO2_NUM_CHANNELS 16
107
112#define SERVO2_DEF_FREQ 256000ul
113#define SERVO2_DEF_DC 0.5
114 // servo2_cfg
116
121
126
131#define SERVO2_MAP_MIKROBUS( cfg, mikrobus ) \
132 cfg.sout = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
133 cfg.sin = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
134 cfg.sclk = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
135 cfg.xlat = MIKROBUS( mikrobus, MIKROBUS_CS ); \
136 cfg.blank = MIKROBUS( mikrobus, MIKROBUS_AN ); \
137 cfg.gsclk = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
138 cfg.xerr = MIKROBUS( mikrobus, MIKROBUS_INT )
139 // servo2_map // servo2
142
147typedef struct
148{
149 // Output pins
150 digital_out_t xlat;
151 digital_out_t blank;
152 digital_out_t sclk;
153 digital_out_t sin;
154
155 // Input pins
156 digital_in_t sout;
157 digital_in_t xerr;
158
159 // Modules
160 pwm_t pwm;
161
162 // ctx variable
163 uint32_t pwm_freq;
164
166
167} servo2_t;
168
173typedef struct
174{
175 // Communication gpio pins
176 pin_name_t xlat;
177 pin_name_t sclk;
178 pin_name_t sin;
179 pin_name_t sout;
180 pin_name_t gsclk;
181
182 // Additional gpio pins
183 pin_name_t blank;
184 pin_name_t xerr;
185
186 // Static variable
187 uint32_t dev_pwm_freq;
188
190
195typedef enum
196{
199
201
207
218
232err_t servo2_init ( servo2_t *ctx, servo2_cfg_t *cfg );
233
245err_t servo2_set_duty_cycle ( servo2_t *ctx, float duty_cycle );
246
258
270
281void servo2_write_data ( servo2_t *ctx, uint8_t *data_in, uint8_t len );
282
291void servo2_set_sclk_pin ( servo2_t *ctx, uint8_t state );
292
301void servo2_set_sin_pin ( servo2_t *ctx, uint8_t state );
302
311void servo2_set_xlat_pin ( servo2_t *ctx, uint8_t state );
312
321void servo2_set_blank_pin ( servo2_t *ctx, uint8_t state );
322
331
340
351err_t servo2_set_channel_pwm ( servo2_t *ctx, uint8_t ch_num, uint16_t pwm_out );
352
363err_t servo2_set_angle ( servo2_t *ctx, uint8_t ch_num, uint8_t angle );
364
373
374#ifdef __cplusplus
375}
376#endif
377#endif // SERVO2_H
378 // servo2
380
381// ------------------------------------------------------------------------ END
#define SERVO2_NUM_CHANNELS
Definition servo2.h:106
err_t servo2_init(servo2_t *ctx, servo2_cfg_t *cfg)
Servo 2 initialization function.
void servo2_update_output(servo2_t *ctx)
Servo 2 update output function.
err_t servo2_set_duty_cycle(servo2_t *ctx, float duty_cycle)
Servo 2 sets PWM duty cycle.
void servo2_set_xlat_pin(servo2_t *ctx, uint8_t state)
Servo 2 set XLAT pin function.
void servo2_set_blank_pin(servo2_t *ctx, uint8_t state)
Servo 2 set BLANK pin function.
err_t servo2_set_channel_pwm(servo2_t *ctx, uint8_t ch_num, uint16_t pwm_out)
Servo 2 set channel PWM function.
err_t servo2_pwm_stop(servo2_t *ctx)
Servo 2 stop PWM module.
err_t servo2_set_angle(servo2_t *ctx, uint8_t ch_num, uint8_t angle)
Servo 2 set angle function.
uint8_t servo2_get_xerr_pin(servo2_t *ctx)
Servo 2 get XERR pin state function.
err_t servo2_pwm_start(servo2_t *ctx)
Servo 2 start PWM module.
void servo2_write_data(servo2_t *ctx, uint8_t *data_in, uint8_t len)
Servo 2 write data function.
void servo2_cfg_setup(servo2_cfg_t *cfg)
Servo 2 configuration object setup function.
uint8_t servo2_get_sout_pin(servo2_t *ctx)
Servo 2 get SOUT pin state function.
void servo2_set_sin_pin(servo2_t *ctx, uint8_t state)
Servo 2 set SIN pin function.
void servo2_set_sclk_pin(servo2_t *ctx, uint8_t state)
Servo 2 set SCLK pin function.
servo2_return_value_t
Servo 2 Click return value data.
Definition servo2.h:196
@ SERVO2_OK
Definition servo2.h:197
@ SERVO2_ERROR
Definition servo2.h:198
Servo 2 Click configuration object.
Definition servo2.h:174
pin_name_t sclk
Definition servo2.h:177
pin_name_t sin
Definition servo2.h:178
pin_name_t xerr
Definition servo2.h:184
uint32_t dev_pwm_freq
Definition servo2.h:187
pin_name_t blank
Definition servo2.h:183
pin_name_t sout
Definition servo2.h:179
pin_name_t gsclk
Definition servo2.h:180
pin_name_t xlat
Definition servo2.h:176
Servo 2 Click context object.
Definition servo2.h:148
digital_out_t xlat
Definition servo2.h:150
digital_out_t sin
Definition servo2.h:153
digital_in_t xerr
Definition servo2.h:157
digital_out_t blank
Definition servo2.h:151
uint32_t pwm_freq
Definition servo2.h:163
digital_out_t sclk
Definition servo2.h:152
digital_in_t sout
Definition servo2.h:156
uint16_t pwm_out[SERVO2_NUM_CHANNELS]
Definition servo2.h:165
pwm_t pwm
Definition servo2.h:160