Contiki-NG
servo.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016, Zolertia - http://www.zolertia.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30/*---------------------------------------------------------------------------*/
31/**
32 * \addtogroup zoul-servo
33 * @{
34 *
35 * \file
36 * Driver for a generic Servo driver
37 *
38 * \author
39 * Antonio Lignan <alinan@zolertia.com>
40 */
41/*---------------------------------------------------------------------------*/
42#include "contiki.h"
43#include "dev/pwm.h"
44#include "dev/gpio.h"
45#include "servo.h"
46/*---------------------------------------------------------------------------*/
47#define DEBUG 0
48#if DEBUG
49#define PRINTF(...) printf(__VA_ARGS__)
50#else
51#define PRINTF(...)
52#endif
53/*---------------------------------------------------------------------------*/
54int
55servo_position(uint16_t gptab, uint8_t port, uint8_t pin, uint16_t pos)
56{
57 uint8_t gpt_num;
58 uint8_t gpt_ab;
59 uint32_t count = 0;
60
61 if((gptab < SERVO_CHANNEL_1) || (gptab > SERVO_CHANNEL_7)) {
62 PRINTF("Servo: invalid servo channel\n");
63 return SERVO_ERROR;
64 }
65
66 /* CC2538 has 4 ports (A-D) and up to 8 pins (0-7) */
67 if((port > GPIO_D_NUM) || (pin > 7)) {
68 PRINTF("Servo: Invalid pin/port settings\n");
69 return SERVO_ERROR;
70 }
71
72 if(pos > SERVO_MAX_DEGREES) {
73 PRINTF("Servo: invalid position (max %u)\n", SERVO_MAX_DEGREES);
74 return SERVO_ERROR;
75 }
76
77 count = (SERVO_MAX_VAL - SERVO_MIN_VAL) * pos;
78 count /= SERVO_MAX_DEGREES;
79 count += SERVO_MIN_VAL;
80
81 gpt_num = (uint8_t)(gptab >> 8);
82 gpt_ab = (uint8_t)(gptab & 0x00FF);
83
84 PRINTF("Servo: F%uHz GPTNUM %u GPTAB %u --> %uÂș (%lu)\n", SERVO_DEFAULT_FREQ,
85 gpt_num, gpt_ab,
86 pos, count);
87 /* Use count as argument instead of percentage */
88 if(pwm_enable(SERVO_DEFAULT_FREQ, 0, count, gpt_num,gpt_ab) != PWM_SUCCESS) {
89 PRINTF("Servo: failed to configure the pwm channel\n");
90 return SERVO_ERROR;
91 }
92
93 /* Start the PWM as soon as possible, keep the pulses to lock the servo in the
94 * given position
95 */
96 if(pwm_start(gpt_num, gpt_ab, port, pin) != PWM_SUCCESS) {
97 PRINTF("Servo: failed to initialize the pwm channel\n");
98 return SERVO_ERROR;
99 }
100
101 return SERVO_SUCCESS;
102}
103/*---------------------------------------------------------------------------*/
104int
105servo_stop(uint16_t gptab, uint8_t port, uint8_t pin)
106{
107 uint8_t gpt_num;
108 uint8_t gpt_ab;
109
110 if((gptab < SERVO_CHANNEL_1) || (gptab > SERVO_CHANNEL_7)) {
111 PRINTF("Servo: invalid servo channel\n");
112 return SERVO_ERROR;
113 }
114
115 /* CC2538 has 4 ports (A-D) and up to 8 pins (0-7) */
116 if((port > GPIO_D_NUM) || (pin > 7)) {
117 PRINTF("Servo: Invalid pin/port settings\n");
118 return SERVO_ERROR;
119 }
120
121 gpt_num = (uint8_t)((gptab & 0xFF00) >> 8);
122 gpt_ab = (uint8_t)(gptab & 0x00FF);
123
124 if(pwm_disable(gpt_num, gpt_ab, port, pin) != PWM_SUCCESS) {
125 PRINTF("Servo: unable to disable the pwm channel\n");
126 return SERVO_ERROR;
127 }
128
129 return SERVO_SUCCESS;
130}
131/*---------------------------------------------------------------------------*/
132/** @} */
Header file with register and macro declarations for the cc2538 GPIO module.
#define GPIO_D_NUM
GPIO_D: 3.
Definition: gpio.h:67
int8_t pwm_start(uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin)
Once configured, starts the PWM.
Definition: pwm.c:235
int8_t pwm_disable(uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin)
Disables a previously PWM configured GPTn.
Definition: pwm.c:330
int8_t pwm_enable(uint32_t freq, uint8_t duty, uint32_t count, uint8_t timer, uint8_t ab)
Configures the general purpose timer in PWM mode.
Definition: pwm.c:93
static volatile uint64_t count
Num.
Definition: clock.c:50
#define SERVO_DEFAULT_FREQ
50 Hz
Definition: servo.h:61
int servo_position(uint16_t gptab, uint8_t port, uint8_t pin, uint16_t pos)
Configures and positions a servo in a given position (by degrees) The servo will lock its position as...
Definition: servo.c:55
int servo_stop(uint16_t gptab, uint8_t port, uint8_t pin)
Fully stop a servo and reconfigures back the pin/port as GPIO.
Definition: servo.c:105
#define SERVO_CHANNEL_1
GPT0-B.
Definition: servo.h:89
#define SERVO_CHANNEL_7
GPT3-B.
Definition: servo.h:95
Header file for the CC2538 PWM driver.
Header file for a Generic Servo driver.