forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32-hal-ledc.h
238 lines (212 loc) · 6.79 KB
/
esp32-hal-ledc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2015-2023 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _ESP32_HAL_LEDC_H_
#define _ESP32_HAL_LEDC_H_
#include "soc/soc_caps.h"
#if SOC_LEDC_SUPPORTED
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "hal/ledc_types.h"
typedef enum {
NOTE_C,
NOTE_Cs,
NOTE_D,
NOTE_Eb,
NOTE_E,
NOTE_F,
NOTE_Fs,
NOTE_G,
NOTE_Gs,
NOTE_A,
NOTE_Bb,
NOTE_B,
NOTE_MAX
} note_t;
typedef void (*voidFuncPtr)(void);
typedef void (*voidFuncPtrArg)(void *);
typedef struct {
uint8_t pin; // Pin assigned to channel
uint8_t channel; // Channel number
uint8_t channel_resolution; // Resolution of channel
voidFuncPtr fn;
void *arg;
#ifndef SOC_LEDC_SUPPORT_FADE_STOP
SemaphoreHandle_t lock; //xSemaphoreCreateBinary
#endif
} ledc_channel_handle_t;
/**
* @brief Get the LEDC clock source.
*
* @return LEDC clock source.
*/
ledc_clk_cfg_t ledcGetClockSource(void);
/**
* @brief Set the LEDC clock source.
*
* @param source LEDC clock source to set.
*
* @return true if LEDC clock source was successfully set, false otherwise.
*/
bool ledcSetClockSource(ledc_clk_cfg_t source);
/**
* @brief Attach a pin to the LEDC driver, with a given frequency and resolution.
* Channel is automatically assigned.
*
* @param pin GPIO pin
* @param freq frequency of PWM signal
* @param resolution resolution for LEDC pin
*
* @return true if configuration is successful and pin was successfully attached, false otherwise.
*/
bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution);
/**
* @brief Attach a pin to the LEDC driver, with a given frequency, resolution and channel.
*
* @param pin GPIO pin
* @param freq frequency of PWM signal
* @param resolution resolution for LEDC pin
* @param channel LEDC channel to attach to
*
* @return true if configuration is successful and pin was successfully attached, false otherwise.
*/
bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel);
/**
* @brief Set the duty cycle of a given pin.
*
* @param pin GPIO pin
* @param duty duty cycle to set
*
* @return true if duty cycle was successfully set, false otherwise.
*/
bool ledcWrite(uint8_t pin, uint32_t duty);
/**
* @brief Set the duty cycle of a given channel.
*
* @param channel LEDC channel
* @param duty duty cycle to set
*
* @return true if duty cycle was successfully set, false otherwise.
*/
bool ledcWriteChannel(uint8_t channel, uint32_t duty);
/**
* @brief Sets the duty to 50 % PWM tone on selected frequency.
*
* @param pin GPIO pin
* @param freq select frequency of pwm signal. If frequency is 0, duty will be set to 0.
*
* @return frequency if tone was successfully set.
* If ``0`` is returned, error occurs and LEDC pin was not configured.
*/
uint32_t ledcWriteTone(uint8_t pin, uint32_t freq);
/**
* @brief Sets the LEDC pin to specific note.
*
* @param pin GPIO pin
* @param note select note to be set (NOTE_C, NOTE_Cs, NOTE_D, NOTE_Eb, NOTE_E, NOTE_F, NOTE_Fs, NOTE_G, NOTE_Gs, NOTE_A, NOTE_Bb, NOTE_B).
* @param octave select octave for note.
*
* @return frequency if note was successfully set.
* If ``0`` is returned, error occurs and LEDC pin was not configured.
*/
uint32_t ledcWriteNote(uint8_t pin, note_t note, uint8_t octave);
/**
* @brief Read the duty cycle of a given LEDC pin.
*
* @param pin GPIO pin
*
* @return duty cycle of selected LEDC pin.
*/
uint32_t ledcRead(uint8_t pin);
/**
* @brief Read the frequency of a given LEDC pin.
*
* @param pin GPIO pin
*
* @return frequency of selected LEDC pin.
*/
uint32_t ledcReadFreq(uint8_t pin);
/**
* @brief Detach a pin from the LEDC driver.
*
* @param pin GPIO pin
*
* @return true if pin was successfully detached, false otherwise.
*/
bool ledcDetach(uint8_t pin);
/**
* @brief Change the frequency and resolution of a given LEDC pin.
*
* @param pin GPIO pin
* @param freq frequency of PWM signal
* @param resolution resolution for LEDC pin
*
* @return frequency configured for the LEDC channel.
* If ``0`` is returned, error occurs and LEDC pin was not configured.
*/
uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution);
/**
* @brief Sets inverting of the output signal for a given LEDC pin.
*
* @param pin GPIO pin
* @param out_invert select, if output should be inverted (true = inverting output).
*
* @return true if output inverting was successfully set, false otherwise.
*/
bool ledcOutputInvert(uint8_t pin, bool out_invert);
//Fade functions
/**
* @brief Setup and start a fade on a given LEDC pin.
*
* @param pin GPIO pin
* @param start_duty initial duty cycle of the fade
* @param target_duty target duty cycle of the fade
* @param max_fade_time_ms maximum fade time in milliseconds
*
* @return true if fade was successfully set and started, false otherwise.
*/
bool ledcFade(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms);
/**
* @brief Setup and start a fade on a given LEDC pin with a callback function.
*
* @param pin GPIO pin
* @param start_duty initial duty cycle of the fade
* @param target_duty target duty cycle of the fade
* @param max_fade_time_ms maximum fade time in milliseconds
* @param userFunc callback function to be called after fade is finished
*
* @return true if fade was successfully set and started, false otherwise.
*/
bool ledcFadeWithInterrupt(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms, void (*userFunc)(void));
/**
* @brief Setup and start a fade on a given LEDC pin with a callback function and argument.
*
* @param pin GPIO pin
* @param start_duty initial duty cycle of the fade
* @param target_duty target duty cycle of the fade
* @param max_fade_time_ms maximum fade time in milliseconds
* @param userFunc callback function to be called after fade is finished
* @param arg argument to be passed to the callback function
*
* @return true if fade was successfully set and started, false otherwise.
*/
bool ledcFadeWithInterruptArg(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms, void (*userFunc)(void *), void *arg);
#ifdef __cplusplus
}
#endif
#endif /* SOC_LEDC_SUPPORTED */
#endif /* _ESP32_HAL_LEDC_H_ */