EVE 1.0
pwm.h
Go to the documentation of this file.
1 #ifndef DRIVER_PWM_H
2 #define DRIVER_PWM_H
3 /**********************************************************************/
4 /*
5  * Copyright (c) 2013-2017, Jetro AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the EVE platform.
31  */
32 
33 /**
34  * \file
35  * @brief Driver for PWM in nRF52.
36  *
37  * @author KLO, Jetro AS
38  */ /******************************************************************/
39 
40 #include <dev/gpio.h>
41 #include <hal/nrf_pwm.h>
42 
43 /**
44  * \defgroup pwm nRF52 PWM driver
45  * \ingroup nrf52
46  * \{
47  *
48  * This PWM driver can be used for PWM0, PWM1 and PWM2.
49  * For each PWM instance, state variables must be initilalized in RAM,
50  * and configuration data must be initilalized in FLASH.
51  *
52  * The state variable is of type struct pwm_state_t, and is used to hold
53  * a copy of used PWM values. The variable should not be initialized.
54  *
55  * The configuration data is of type struct pwm_t, and is used to set:
56  * - Which PWM block to be used
57  * - Clock prescaler
58  * - A pointer to the PWM state
59  * - Frequency: The frequency in Hz for the generated PWM
60  * - Mode: Edge- or center-aligned pulse generation
61  * - Range: The range for the PWM (counts from 0 to Range)
62  Example:
63  100: Result in % resolution (0 to 100)
64  1023: 10 bits resolution (0 to 1023)
65  * - Which channels to be used and which channels with inverted outputs
66  * (struct pwm_channel_config_t)
67  * - Location: Selects location for the PWM output pins (_TIMER_ROUTE_LOCATION_LOC0 -- 6)
68  *
69  * The timer is counting from 0 to TOP value.
70  * TOP value = (16 MHz clock / (2^Prescaler) / Frequency).
71  * Care must be taken to obtain enough resolution. Set Prescaler as low as possible.
72  *
73  */
74 
75 /***********************************************************************
76  * Global defines
77 ***********************************************************************/
78 /**
79  * Number of PWM channels available
80  */
81 #define PWM_NUM_CHANNELS 4
82 
83 /**
84  * pwm_state_t structure holds clock lock state and copy of used PWM values.
85  * The structure will be set by the PwmSet() function.
86  *
87  */
89 {
90  uint16_t ChannelValue[PWM_NUM_CHANNELS];
91  uint16_t TopValue;
92  uint8_t Shutdown;
93  uint8_t SeqActive;
94 };
95 
96 /**
97  * pwm_channel_config_t structure is used for configuration of PWM channels
98  * without dead time insertion.
99  * The structure must be initialized as a part of the PWM configuration.
100  *
101  */
103 {
104  uint8_t Pin : GPIO_PIN_ID_WIDTH; ///< GPIO pin location
105  uint8_t Enable : 1; ///< true if this channel is allowed for PWM
106  uint8_t Invert : 1; ///< Invert output
107 };
108 
109 /**
110  * pwm_t structure is used for configuration of PWM.
111  * The structure must be initialized before the PWM driver may be used.
112  *
113  */
114 struct pwm_t
115 {
116  NRF_PWM_Type *Dev;
117  uint32_t IrqPriority; ///< HW IRQ priority (see enum EVE_IRQ_PRIORITIES)
118  struct pwm_state_t *State;
119  nrf_pwm_clk_t BaseClock; ///< Base PWM clock
120  uint32_t DefaultFrequency; ///< TOP_Value = BaseClock / Frequency - 1
121  uint16_t Range; ///< PWM counts from 0 to Range
122  struct pwm_channel_config_t ChannelConfig[PWM_NUM_CHANNELS]; ///< Channel config
123  nrf_pwm_mode_t Mode; ///< Edge- or center-aligned mode
124 };
125 
126  /**
127  * @name Functions called from application programs
128  * @{
129  */
130 
131 /**********************************************************************/
132 /**
133  * @brief Name: PwmInit\n
134  * Initialize PWM.
135  *
136  * @param Pwm Configuration structure for PWM to be used.
137  ***********************************************************************/
138 void PwmInit(const struct pwm_t *Pwm);
139 
140 /**********************************************************************/
141 /**
142  * @brief Name: PwmSet\n
143  * Sets PWM duty cycle for the selected channel.
144  *
145  * @param Pwm Configuration structure for PWM to be used.
146  * @param Channel 0-2
147  * @param Value PWM active time (0 to Range set in struct pwm_t)
148  ***********************************************************************/
149 extern void PwmSet(const struct pwm_t *Pwm, uint8_t Channel, uint16_t Value);
150 
151 /**********************************************************************/
152 /**
153  * @brief Name: PwmFrequencySet\n
154  * Sets the PWM frequency.
155  * This frequency will be used for all channels on the selected timer.
156  *
157  * @param Pwm Configuration structure for PWM to be used.
158  * @param Frequency The frequency in Hz for the generated PWM
159  ***********************************************************************/
160 extern void PwmFrequencySet(const struct pwm_t *Pwm, uint32_t Frequency);
161 
162 /**********************************************************************/
163 /**
164  * @brief Name: PwmInterruptHandler\n
165  * PWM interrupt handler
166  * Called from board.c at hardware interrupt context.
167  *
168  * @param Pwm PWM instance
169  ***********************************************************************/
170 extern void PwmInterruptHandler(const struct pwm_t *Pwm);
171 
172 /** @} */
173 /** \} */
174 
175 #endif // DRIVER_PWM_H
#define PWM_NUM_CHANNELS
Definition: pwm.h:81
uint16_t Range
PWM counts from 0 to Range.
Definition: pwm.h:121
Definition: pwm.h:114
Driver for port initialisazion and IO functions for nRF52 uC.
void PwmInterruptHandler(const struct pwm_t *Pwm)
Name: PwmInterruptHandler PWM interrupt handler Called from board.c at hardware interrupt context...
uint32_t IrqPriority
HW IRQ priority (see enum EVE_IRQ_PRIORITIES)
Definition: pwm.h:117
void PwmFrequencySet(const struct pwm_t *Pwm, uint32_t Frequency)
Name: PwmFrequencySet Sets the PWM frequency. This frequency will be used for all channels on the sel...
nrf_pwm_clk_t BaseClock
Base PWM clock.
Definition: pwm.h:119
void PwmInit(const struct pwm_t *Pwm)
Name: PwmInit Initialize PWM.
void PwmSet(const struct pwm_t *Pwm, uint8_t Channel, uint16_t Value)
Name: PwmSet Sets PWM duty cycle for the selected channel.
nrf_pwm_mode_t Mode
Edge- or center-aligned mode.
Definition: pwm.h:123
uint32_t DefaultFrequency
TOP_Value = BaseClock / Frequency - 1.
Definition: pwm.h:120