Contiki-NG
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 count PWM duty cycle (count number)
120  * \param timer General purpose timer to use [0-3]
121  * \param ab Select which timer to use (Timer A or B)
122  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
123  */
124 int8_t pwm_enable(uint32_t freq, uint8_t duty, uint32_t count, uint8_t timer,
125  uint8_t ab);
126 /*---------------------------------------------------------------------------*/
127 /** \brief Disables a previously PWM configured GPTn
128  * \param timer General purpose timer to disable [0-3]
129  * \param ab Select which timer to disable (Timer A or B)
130  * \param port Port number used as PWM to disable (set as input GPIO)
131  * \param pin Pin number used as PWM to disable (set as input GPIO)
132  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
133  *
134  * This function disables a specific timer (A or B) and reset related registers
135  * to default values. The user must explicitely pass the port/pin number of
136  * the pin to disable as PWM and to be configured as input GPIO.
137  * The module clock is not disabled with this function
138  */
139 int8_t pwm_disable(uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin);
140 /*---------------------------------------------------------------------------*/
141 /** \brief Once configured, starts the PWM
142  * \param timer General purpose timer to start [0-3]
143  * \param ab Select which timer to start (Timer A or B)
144  * \param port Port number to use as PWM
145  * \param pin Pin number to use as PWM
146  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
147  */
148 int8_t pwm_start(uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin);
149 /*---------------------------------------------------------------------------*/
150 /** \brief Halts the PWM in a given GPT/timer
151  * \param timer General purpose timer to stop [0-3]
152  * \param ab Select which timer to stop (Timer A or B)
153  * \param port Port of the gpio port mapped to the PWM to stop
154  * \param pin Pin of the gpio port mapped to the PWM to stop
155  * \param state State to leave the pin once stopped, on (1) or off (0)
156  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
157  */
158 int8_t pwm_stop(uint8_t timer, uint8_t ab, uint8_t port, uint8_t pin, uint8_t state);
159 /*---------------------------------------------------------------------------*/
160 /** \brief Sets the PWM duty cycle signal direction (high/low)
161  * \param timer General purpose timer [0-3]
162  * \param ab Select which timer to use (Timer A or B)
163  * \param dir Direction of the PWM signal, \c PWM_SIGNAL_INVERTED or
164  * \c PWM_SIGNAL_STRAIGHT
165  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
166  */
167 int8_t pwm_set_direction(uint8_t timer, uint8_t ab, uint8_t dir);
168 /*---------------------------------------------------------------------------*/
169 /** \brief Toggle the PWM signal direction (inverts the current duty cycle)
170  * \param timer General purpose timer to use [0-3]
171  * \param ab Select which timer to use (Timer A or B)
172  * \return \c PWM_SUCCESS if successful, else \c PWM_ERROR
173  */
174 int8_t pwm_toggle_direction(uint8_t timer, uint8_t ab);
175 /*---------------------------------------------------------------------------*/
176 /** @} */
177 #endif /* PWM_H_ */
178 /*---------------------------------------------------------------------------*/
179 /**
180  * @}
181  * @}
182  */
Header file for the cc2538 System Control driver.
Header file with register and macro declarations for the cc2538 GPIO module.
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
Header file with declarations for the I/O Control module.
A timer.
Definition: timer.h:82
int8_t pwm_toggle_direction(uint8_t timer, uint8_t ab)
Toggle the PWM signal direction (inverts the current duty cycle)
Definition: pwm.c:302
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:274
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
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:190
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