servo  2.0.0.0
servo.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef SERVO_H
36 #define SERVO_H
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_i2c_master.h"
55 
56 // -------------------------------------------------------------- PUBLIC MACROS
66 #define SERVO_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
68  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
69  cfg.oe = MIKROBUS( mikrobus, MIKROBUS_CS )
70 
76 #define SERVO_RETVAL uint8_t
77 
78 #define SERVO_OK 0x00
79 #define SERVO_INIT_ERROR 0xFF
80 
86 #define SERVO_REG_MODE_1 0x00
87 #define SERVO_REG_MODE_2 0x01
88 #define SERVO_REG_SUB_ADDRESS_1 0x02
89 #define SERVO_REG_SUB_ADDRESS_2 0x03
90 #define SERVO_REG_SUB_ADDRESS_3 0x04
91 #define SERVO_REG_ALL_CALL_ADR 0x05
92 
93 #define SERVO_REG_MOTOR_1_ON_L 0x06
94 #define SERVO_REG_MOTOR_1_ON_H 0x07
95 #define SERVO_REG_MOTOR_1_OFF_L 0x08
96 #define SERVO_REG_MOTOR_1_OFF_H 0x09
97 #define SERVO_REG_MOTOR_2_ON_L 0x0A
98 #define SERVO_REG_MOTOR_2_ON_H 0x0B
99 #define SERVO_REG_MOTOR_2_OFF_L 0x0C
100 #define SERVO_REG_MOTOR_2_OFF_H 0x0D
101 #define SERVO_REG_MOTOR_3_ON_L 0x0E
102 #define SERVO_REG_MOTOR_3_ON_H 0x0F
103 #define SERVO_REG_MOTOR_3_OFF_L 0x10
104 #define SERVO_REG_MOTOR_3_OFF_H 0x11
105 #define SERVO_REG_MOTOR_4_ON_L 0x12
106 #define SERVO_REG_MOTOR_4_ON_H 0x13
107 #define SERVO_REG_MOTOR_4_OFF_L 0x14
108 #define SERVO_REG_MOTOR_4_OFF_H 0x15
109 #define SERVO_REG_MOTOR_5_ON_L 0x16
110 #define SERVO_REG_MOTOR_5_ON_H 0x17
111 #define SERVO_REG_MOTOR_5_OFF_L 0x18
112 #define SERVO_REG_MOTOR_5_OFF_H 0x19
113 #define SERVO_REG_MOTOR_6_ON_L 0x1A
114 #define SERVO_REG_MOTOR_6_ON_H 0x1B
115 #define SERVO_REG_MOTOR_6_OFF_L 0x1C
116 #define SERVO_REG_MOTOR_6_OFF_H 0x1D
117 #define SERVO_REG_MOTOR_7_ON_L 0x1E
118 #define SERVO_REG_MOTOR_7_ON_H 0x1F
119 #define SERVO_REG_MOTOR_7_OFF_L 0x20
120 #define SERVO_REG_MOTOR_7_OFF_H 0x21
121 #define SERVO_REG_MOTOR_8_ON_L 0x22
122 #define SERVO_REG_MOTOR_8_ON_H 0x23
123 #define SERVO_REG_MOTOR_8_OFF_L 0x24
124 #define SERVO_REG_MOTOR_8_OFF_H 0x25
125 #define SERVO_REG_MOTOR_9_ON_L 0x26
126 #define SERVO_REG_MOTOR_9_ON_H 0x27
127 #define SERVO_REG_MOTOR_9_OFF_L 0x28
128 #define SERVO_REG_MOTOR_9_OFF_H 0x29
129 #define SERVO_REG_MOTOR_10_ON_L 0x2A
130 #define SERVO_REG_MOTOR_10_ON_H 0x2B
131 #define SERVO_REG_MOTOR_10_OFF_L 0x2C
132 #define SERVO_REG_MOTOR_10_OFF_H 0x2D
133 #define SERVO_REG_MOTOR_11_ON_L 0x2E
134 #define SERVO_REG_MOTOR_11_ON_H 0x2F
135 #define SERVO_REG_MOTOR_11_OFF_L 0x30
136 #define SERVO_REG_MOTOR_11_OFF_H 0x31
137 #define SERVO_REG_MOTOR_12_ON_L 0x32
138 #define SERVO_REG_MOTOR_12_ON_H 0x33
139 #define SERVO_REG_MOTOR_12_OFF_L 0x34
140 #define SERVO_REG_MOTOR_12_OFF_H 0x35
141 #define SERVO_REG_MOTOR_13_ON_L 0x36
142 #define SERVO_REG_MOTOR_13_ON_H 0x37
143 #define SERVO_REG_MOTOR_13_OFF_L 0x38
144 #define SERVO_REG_MOTOR_13_OFF_H 0x39
145 #define SERVO_REG_MOTOR_14_ON_L 0x3A
146 #define SERVO_REG_MOTOR_14_ON_H 0x3B
147 #define SERVO_REG_MOTOR_14_OFF_L 0x3C
148 #define SERVO_REG_MOTOR_14_OFF_H 0x3D
149 #define SERVO_REG_MOTOR_15_ON_L 0x3E
150 #define SERVO_REG_MOTOR_15_ON_H 0x3F
151 #define SERVO_REG_MOTOR_15_OFF_L 0x40
152 #define SERVO_REG_MOTOR_15_OFF_H 0x41
153 #define SERVO_REG_MOTOR_16_ON_L 0x42
154 #define SERVO_REG_MOTOR_16_ON_H 0x43
155 #define SERVO_REG_MOTOR_16_OFF_L 0x44
156 #define SERVO_REG_MOTOR_16_OFF_H 0x45
157 
158 #define SERVO_REG_ALL_MOTOR_ON_L 0xFA
159 #define SERVO_REG_ALL_MOTOR_ON_H 0xFB
160 #define SERVO_REG_ALL_MOTOR_OFF_L 0xFC
161 #define SERVO_REG_ALL_MOTOR_OFF_H 0xFD
162 #define SERVO_REG_PRE_SCALE 0xFE
163 #define SERVO_REG_TEST_MODE 0xFF
164 
170 #define SERVO_MODE1_RESTART_ENABLE 0x01 << 7
171 #define SERVO_MODE1_RESTART_DISABLE 0x00 << 7
172 #define SERVO_MODE1_INTERNAL_CLOCK 0x00 << 6
173 #define SERVO_MODE1_EXTCLK_PIN_CLOCK 0x01 << 6
174 #define SERVO_MODE1_AUTO_INCREMENT_ENABLE 0x01 << 5
175 #define SERVO_MODE1_AUTO_INCREMENT_DISABLE 0x00 << 5
176 #define SERVO_MODE1_NORMAL_MODE 0x00 << 4
177 #define SERVO_MODE1_LOW_POWER_MODE 0x01 << 4
178 #define SERVO_MODE1_USE_SUBADR_1 0x01 << 3
179 #define SERVO_MODE1_NO_USE_SUBADR_1 0x00 << 3
180 #define SERVO_MODE1_USE_SUBADR_2 0x01 << 2
181 #define SERVO_MODE1_NO_USE_SUBADR_2 0x00 << 2
182 #define SERVO_MODE1_USE_SUBADR_3 0x01 << 1
183 #define SERVO_MODE1_NO_USE_SUBADR_3 0x00 << 1
184 #define SERVO_MODE1_USE_ALL_CALL_ADR 0x01
185 #define SERVO_MODE1_NO_USE_ALL_CALL_ADR 0x00
186 
192 #define SERVO_MODE2_OUT_LOGIC_NOT_INVERTED 0x00 << 4
193 #define SERVO_MODE2_OUT_LOGIC_INVERTED 0x01 << 4
194 #define SERVO_MODE2_OUT_CHANGE_ON_STOP_CMD 0x00 << 3
195 #define SERVO_MODE2_OUT_CHANGE_ON_ACK_CMD 0x01 << 3
196 #define SERVO_MODE2_OPEN_DRAIN_STRUCTURE 0x00 << 2
197 #define SERVO_MODE2_TOTEM_POLE_STRUCTURE 0x01 << 2
198 
204 #define SERVO_DEFAULT_LOW_RESOLUTION 0
205 #define SERVO_DEFAULT_HIGH_RESOLUTION 330
206 
207 #define SERVO_GENERAL_CALL_ADR 0x00
208 #define SERVO_SOFT_RESET 0x06
209 
210 #define SERVO_VREF_3300 3300
211 #define SERVO_VREF_5000 5000
212 
218 #define SERVO_MOTOR_1 0x06
219 #define SERVO_MOTOR_2 0x0A
220 #define SERVO_MOTOR_3 0x0E
221 #define SERVO_MOTOR_4 0x12
222 #define SERVO_MOTOR_5 0x16
223 #define SERVO_MOTOR_6 0x1A
224 #define SERVO_MOTOR_7 0x1E
225 #define SERVO_MOTOR_8 0x22
226 #define SERVO_MOTOR_9 0x26
227 #define SERVO_MOTOR_10 0x2A
228 #define SERVO_MOTOR_11 0x2E
229 #define SERVO_MOTOR_12 0x32
230 #define SERVO_MOTOR_13 0x36
231 #define SERVO_MOTOR_14 0x3A
232 #define SERVO_MOTOR_15 0x3E
233 #define SERVO_MOTOR_16 0x42
234 
235 #define SERVO_POSITIVE_CH0_NEGATIVE_CH1 0xA0
236 #define SERVO_POSITIVE_CH2_NEGATIVE_CH3 0xA1
237 #define SERVO_POSITIVE_CH4_NEGATIVE_CH5 0xA2
238 #define SERVO_POSITIVE_CH6_NEGATIVE_CH7 0xA3
239 #define SERVO_POSITIVE_CH8_NEGATIVE_CH9 0xA4
240 #define SERVO_POSITIVE_CH10_NEGATIVE_CH11 0xA5
241 #define SERVO_POSITIVE_CH12_NEGATIVE_CH13 0xA6
242 #define SERVO_POSITIVE_CH14_NEGATIVE_CH15 0xA7
243 #define SERVO_POSITIVE_CH1_NEGATIVE_CH0 0xA8
244 #define SERVO_POSITIVE_CH3_NEGATIVE_CH2 0xA9
245 #define SERVO_POSITIVE_CH5_NEGATIVE_CH4 0xAA
246 #define SERVO_POSITIVE_CH7_NEGATIVE_CH6 0xAB
247 #define SERVO_POSITIVE_CH9_NEGATIVE_CH8 0xAC
248 #define SERVO_POSITIVE_CH11_NEGATIVE_CH10 0xAD
249 #define SERVO_POSITIVE_CH13_NEGATIVE_CH12 0xAE
250 #define SERVO_POSITIVE_CH15_NEGATIVE_CH14 0xAF
251 
252 #define SERVO_POSITIVE_CH0 0xB0
253 #define SERVO_POSITIVE_CH2 0xB1
254 #define SERVO_POSITIVE_CH4 0xB2
255 #define SERVO_POSITIVE_CH6 0xB3
256 #define SERVO_POSITIVE_CH8 0xB4
257 #define SERVO_POSITIVE_CH10 0xB5
258 #define SERVO_POSITIVE_CH12 0xB6
259 #define SERVO_POSITIVE_CH14 0xB7
260 #define SERVO_POSITIVE_CH1 0xB8
261 #define SERVO_POSITIVE_CH3 0xB9
262 #define SERVO_POSITIVE_CH5 0xBA
263 #define SERVO_POSITIVE_CH7 0xBB
264 #define SERVO_POSITIVE_CH9 0xBC
265 #define SERVO_POSITIVE_CH11 0xBD
266 #define SERVO_POSITIVE_CH13 0xBE
267 #define SERVO_POSITIVE_CH15 0xBF
268  // End group macro
271 // --------------------------------------------------------------- PUBLIC TYPES
277 typedef struct
278 {
279  uint8_t min_position;
280  uint8_t max_position;
281  uint16_t low_resolution;
282  uint16_t high_resolution;
283 
285 
286 typedef struct
287 {
288  uint8_t x;
289  uint8_t in_min;
290  uint8_t in_max;
291  uint16_t out_min;
292  uint16_t out_max;
293 
294 } servo_map_t;
295 
299 typedef struct
300 {
301  // Output pins
302 
303  digital_out_t oe;
304 
305  // Modules
306 
307  i2c_master_t i2c;
308 
309  // ctx variable
310 
313 
314  uint8_t min_pos;
315  uint8_t max_pos;
316  uint16_t vref;
317  uint16_t low_res;
318  uint16_t high_res;
319 
321 
322 } servo_t;
323 
327 typedef struct
328 {
329  // Communication gpio pins
330 
331  pin_name_t scl;
332  pin_name_t sda;
333 
334  // Additional gpio pins
335 
336  pin_name_t oe;
337 
338  // static variable
339 
340  uint32_t i2c_speed;
343 } servo_cfg_t;
344  // End types group
346 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
347 
353 #ifdef __cplusplus
354 extern "C"{
355 #endif
356 
366 
376 
385 
396 void servo_generic_write_of_pca9685 ( servo_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
397 
408 void servo_generic_write_of_of_ltc2497 ( servo_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
409 
420 void servo_generic_read_of_pca9685 ( servo_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
431 void servo_generic_read_of_ltc2497 ( servo_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
432 
441 void servo_setting ( servo_t *ctx, servo_pos_and_res_t pos_and_res );
442 
451 void servo_set_vref ( servo_t *ctx, uint16_t v_ref );
452 
463 void servo_stop ( servo_t *ctx );
464 
472 void servo_start ( servo_t *ctx );
473 
481 void servo_soft_reset ( servo_t *ctx );
482 
490 void servo_sleep ( servo_t *ctx );
491 
517 void servo_set_mode ( servo_t *ctx, uint8_t mode, uint8_t w_data );
518 
528 void servo_set_position ( servo_t *ctx, uint8_t motor, uint8_t position );
529 
538 void servo_set_freq ( servo_t *ctx, uint16_t freq );
539 
548 uint32_t servo_get_channel ( servo_t *ctx, uint8_t channel );
549 
558 uint16_t setvo_get_current ( servo_t *ctx, uint8_t channel );
559 
560 #ifdef __cplusplus
561 }
562 #endif
563 #endif // _SERVO_H_
564  // End public_function group
567 
568 // ------------------------------------------------------------------------- END
servo_map_t
Definition: servo.h:287
servo_pos_and_res_t::min_position
uint8_t min_position
Definition: servo.h:279
servo_generic_write_of_pca9685
void servo_generic_write_of_pca9685(servo_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function of pca9685.
setvo_get_current
uint16_t setvo_get_current(servo_t *ctx, uint8_t channel)
Functions for reading current in mA.
SERVO_RETVAL
#define SERVO_RETVAL
Definition: servo.h:76
servo_generic_write_of_of_ltc2497
void servo_generic_write_of_of_ltc2497(servo_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function of ltc2497.
servo_map_t::in_max
uint8_t in_max
Definition: servo.h:290
servo_t::max_pos
uint8_t max_pos
Definition: servo.h:315
servo_t::pos_and_res
servo_pos_and_res_t pos_and_res
Definition: servo.h:320
servo_t
Click ctx object definition.
Definition: servo.h:300
servo_t::i2c
i2c_master_t i2c
Definition: servo.h:307
servo_generic_read_of_pca9685
void servo_generic_read_of_pca9685(servo_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function of pca9685.
servo_cfg_setup
void servo_cfg_setup(servo_cfg_t *cfg)
Config Object Initialization function.
servo_setting
void servo_setting(servo_t *ctx, servo_pos_and_res_t pos_and_res)
Setting function.
servo_generic_read_of_ltc2497
void servo_generic_read_of_ltc2497(servo_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function of ltc2497.
servo_soft_reset
void servo_soft_reset(servo_t *ctx)
Soft reset function.
servo_init
SERVO_RETVAL servo_init(servo_t *ctx, servo_cfg_t *cfg)
Initialization function.
servo_t::oe
digital_out_t oe
Definition: servo.h:303
servo_stop
void servo_stop(servo_t *ctx)
Stop function.
servo_t::slave_address_of_pca9685
uint8_t slave_address_of_pca9685
Definition: servo.h:311
servo_pos_and_res_t::max_position
uint8_t max_position
Definition: servo.h:280
servo_map_t::out_max
uint16_t out_max
Definition: servo.h:292
servo_default_cfg
void servo_default_cfg(servo_t *ctx)
Click Default Configuration function.
servo_pos_and_res_t
Definition: servo.h:278
servo_set_mode
void servo_set_mode(servo_t *ctx, uint8_t mode, uint8_t w_data)
Set mode function.
servo_start
void servo_start(servo_t *ctx)
Start function.
servo_cfg_t::oe
pin_name_t oe
Definition: servo.h:336
servo_pos_and_res_t::low_resolution
uint16_t low_resolution
Definition: servo.h:281
servo_set_freq
void servo_set_freq(servo_t *ctx, uint16_t freq)
Set frequency function.
servo_set_vref
void servo_set_vref(servo_t *ctx, uint16_t v_ref)
Set Vref function.
servo_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: servo.h:340
servo_t::low_res
uint16_t low_res
Definition: servo.h:317
servo_sleep
void servo_sleep(servo_t *ctx)
Sleep function.
servo_t::vref
uint16_t vref
Definition: servo.h:316
servo_pos_and_res_t::high_resolution
uint16_t high_resolution
Definition: servo.h:282
servo_t::high_res
uint16_t high_res
Definition: servo.h:318
servo_map_t::x
uint8_t x
Definition: servo.h:288
servo_t::min_pos
uint8_t min_pos
Definition: servo.h:314
servo_cfg_t::scl
pin_name_t scl
Definition: servo.h:331
servo_t::slave_address_of_ltc2497
uint8_t slave_address_of_ltc2497
Definition: servo.h:312
servo_cfg_t::i2c_address_of_pca9685
uint8_t i2c_address_of_pca9685
Definition: servo.h:341
servo_map_t::out_min
uint16_t out_min
Definition: servo.h:291
servo_cfg_t
Click configuration structure definition.
Definition: servo.h:328
servo_get_channel
uint32_t servo_get_channel(servo_t *ctx, uint8_t channel)
Functions for reading adc value of current.
servo_cfg_t::sda
pin_name_t sda
Definition: servo.h:332
servo_set_position
void servo_set_position(servo_t *ctx, uint8_t motor, uint8_t position)
Set position function.
servo_map_t::in_min
uint8_t in_min
Definition: servo.h:289
servo_cfg_t::i2c_address_of_ltc2497
uint8_t i2c_address_of_ltc2497
Definition: servo.h:342