Contiki 3.x
pwm.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, 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  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*---------------------------------------------------------------------------*/
32 /**
33  * \addtogroup cc2538
34  * @{
35  *
36  * \defgroup cc2538-pwm-driver CC2538 PWM driver
37  *
38  * Driver for the CC2538 PWM on GPTIMER
39  *
40  * The driver uses the timers A and B of the general purpose timers to create
41  * a PWM signal, allowing to set a duty cycle value from 1-100%. This
42  * implementation relies on having a peripheral clock of 16MHz, but it can be
43  * easily changed (see PWM_FREQ_MIN and PWM_FREQ_MAX values). The reason it is
44  * fixed to these frequencies is to have a consistent duty cycle
45  * implementation.
46  *
47  * Depending on the specific needs these limits can be changed to meet a given
48  * duty cycle and lower frequencies by using the prescaler (GPTIMER_TnPR).
49  *
50  * Running a PWM timer prevents the LPM driver from dropping to PM1+.
51  *
52  * @{
53  *
54  * \file
55  * Header file for the CC2538 PWM driver
56  *
57  * \author
58  * Javier Sanchez <jsanchez@zolertia.com>
59  * Antonio Lignan <alinan@zolertia.com>
60  */
61 /*---------------------------------------------------------------------------*/
62 #ifndef PWM_H_
63 #define PWM_H_
64 /*---------------------------------------------------------------------------*/
65 #include "contiki.h"
66 #include "dev/ioc.h"
67 #include "dev/gpio.h"
68 #include "dev/sys-ctrl.h"
69 /*---------------------------------------------------------------------------*/
70 /** \name PWM return values
71  * @{
72  */
73 #define PWM_SUCCESS 0
74 #define PWM_ERROR (-1)
75 /** @} */
76 /*---------------------------------------------------------------------------*/
77 /** \name PWM recommended values respect to peripheral clock frequency
78  * @{
79  */
80 /* Roughly 244 Hz with a 16-MHz system clock, no prescaler */
81 #define PWM_SYS_16MHZ_NO_PRES_MIN 0xFFFF
82 #define PWM_SYS_16MHZ_NO_PRES_MIN_FREQ 244
83 /* Roughly 1 Hz with a 16-MHz system clock, to keep frequency parameter in Hz */
84 #define PWM_SYS_16MHZ_PRES_MIN 0x00F42400
85 #define PWM_SYS_16MHZ_PRES_MIN_FREQ 1
86 /* Yields 160 KHz at 16 MHz and allows down to 1% (integer) duty cycles */
87 #define PWM_SYS_16MHZ_NO_PRES_MAX 100
88 #define PWM_SYS_16MHZ_NO_PRES_MAX_FREQ 160000
89 /** @} */
90 /*---------------------------------------------------------------------------*/
91 /** \name PWM driver definitions and configuration values
92  * @{
93  */
94 #define PWM_TIMER_A 0
95 #define PWM_TIMER_B 1
96 #define PWM_TIMER_0 0
97 #define PWM_TIMER_1 1
98 #define PWM_TIMER_2 2
99 #define PWM_TIMER_3 3
100 #define PWM_TIMER_MIN PWM_TIMER_0
101 #define PWM_TIMER_MAX PWM_TIMER_3
102 #define PWM_SIGNAL_STRAIGHT 1
103 #define PWM_SIGNAL_INVERTED 0
104 #define PWM_OFF_WHEN_STOP 0
105 #define PWM_ON_WHEN_STOP 1
106 #define PWM_GPTIMER_CFG_SPLIT_MODE 0x04
107 #define PWM_DUTY_MAX 100
108 #define PWM_DUTY_MIN 0
109 #define PWM_FREQ_MIN PWM_SYS_16MHZ_PRES_MIN_FREQ
110 #define PWM_FREQ_MAX PWM_SYS_16MHZ_NO_PRES_MAX_FREQ
111 /** @} */
112 /*---------------------------------------------------------------------------*/
113 /** \name PWM functions
114  * @{
115  */
116 /** \brief Configures the general purpose timer in PWM mode
117  * \param freq PWM frequency (in Hz)
118  * \param duty PWM duty cycle (percentage in integers)
119  * \param timer General purpose timer to use [0-3]
120  * \param ab Select which timer to use (Timer A or B)
121  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
122  */
123 int8_t pwm_enable(uint32_t freq, uint8_t duty, uint8_t timer, uint8_t ab);
124 /*---------------------------------------------------------------------------*/
125 /** \brief Disables a previously PWM configured GPTn
126  * \param timer General purpose timer to disable [0-3]
127  * \param ab Select which timer to disable (Timer A or B)
128  * \param port Port number used as PWM to disable (set as input GPIO)
129  * \param pin Pin number used as PWM to disable (set as input GPIO)
130  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
131  *
132  * This function disables a specific timer (A or B) and reset related registers
133  * to default values. The user must explicitely pass the port/pin number of
134  * the pin to disable as PWM and to be configured as input GPIO.
135  * The module clock is not disabled with this function
136  */
137 int8_t pwm_disable(uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin);
138 /*---------------------------------------------------------------------------*/
139 /** \brief Once configured, starts the PWM
140  * \param timer General purpose timer to start [0-3]
141  * \param ab Select which timer to start (Timer A or B)
142  * \param port Port number to use as PWM
143  * \param pin Pin number to use as PWM
144  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
145  */
146 int8_t pwm_start(uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin);
147 /*---------------------------------------------------------------------------*/
148 /** \brief Halts the PWM in a given GPT/timer
149  * \param timer General purpose timer to stop [0-3]
150  * \param ab Select which timer to stop (Timer A or B)
151  * \param port Port of the gpio port mapped to the PWM to stop
152  * \param pin Pin of the gpio port mapped to the PWM to stop
153  * \param state State to leave the pin once stopped, on (1) or off (0)
154  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
155  */
156 int8_t pwm_stop(uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin, uint8_t state);
157 /*---------------------------------------------------------------------------*/
158 /** \brief Sets the PWM duty cycle signal direction (high/low)
159  * \param timer General purpose timer [0-3]
160  * \param ab Select which timer to use (Timer A or B)
161  * \param dir Direction of the PWM signal, \c PWM_SIGNAL_INVERTED or
162  * \c PWM_SIGNAL_STRAIGHT
163  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
164  */
165 int8_t pwm_set_direction(uint8_t timer, uint8_t ab, uint8_t dir);
166 /*---------------------------------------------------------------------------*/
167 /** \brief Toggle the PWM signal direction (inverts the current duty cycle)
168  * \param timer General purpose timer to use [0-3]
169  * \param ab Select which timer to use (Timer A or B)
170  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
171  */
172 int8_t pwm_toggle_direction(uint8_t timer, uint8_t ab);
173 /*---------------------------------------------------------------------------*/
174 /** @} */
175 #endif /* PWM_H_ */
176 /*---------------------------------------------------------------------------*/
177 /**
178  * @}
179  * @}
180  */
int8_t pwm_start(uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin)
Once configured, starts the PWM.
Definition: pwm.c:227
A timer.
Definition: timer.h:86
int8_t pwm_stop(uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin, uint8_t state)
Halts the PWM in a given GPT/timer.
Definition: pwm.c:182
int8_t pwm_set_direction(uint8_t timer, uint8_t ab, uint8_t dir)
Sets the PWM duty cycle signal direction (high/low)
Definition: pwm.c:266
Header file with declarations for the I/O Control module.
Header file for the cc2538 System Control driver.
int8_t pwm_enable(uint32_t freq, uint8_t duty, uint8_t timer, uint8_t ab)
Configures the general purpose timer in PWM mode.
Definition: pwm.c:93
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:322
int8_t pwm_toggle_direction(uint8_t timer, uint8_t ab)
Toggle the PWM signal direction (inverts the current duty cycle)
Definition: pwm.c:294