- Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathesp32-hal-ledc.c
270 lines (252 loc) · 10.8 KB
/
esp32-hal-ledc.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2015-2016 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.
#include"esp32-hal.h"
#include"freertos/FreeRTOS.h"
#include"freertos/task.h"
#include"freertos/semphr.h"
#include"rom/ets_sys.h"
#include"esp32-hal-matrix.h"
#include"soc/dport_reg.h"
#include"soc/ledc_reg.h"
#include"soc/ledc_struct.h"
#ifCONFIG_DISABLE_HAL_LOCKS
#defineLEDC_MUTEX_LOCK()
#defineLEDC_MUTEX_UNLOCK()
#else
#defineLEDC_MUTEX_LOCK() do {} while (xSemaphoreTake(_ledc_sys_lock, portMAX_DELAY) != pdPASS)
#defineLEDC_MUTEX_UNLOCK() xSemaphoreGive(_ledc_sys_lock)
xSemaphoreHandle_ledc_sys_lock;
#endif
/*
* LEDC Chan to Group/Channel/Timer Mapping
** ledc: 0 => Group: 0, Channel: 0, Timer: 0
** ledc: 1 => Group: 0, Channel: 1, Timer: 0
** ledc: 2 => Group: 0, Channel: 2, Timer: 1
** ledc: 3 => Group: 0, Channel: 3, Timer: 1
** ledc: 4 => Group: 0, Channel: 4, Timer: 2
** ledc: 5 => Group: 0, Channel: 5, Timer: 2
** ledc: 6 => Group: 0, Channel: 6, Timer: 3
** ledc: 7 => Group: 0, Channel: 7, Timer: 3
** ledc: 8 => Group: 1, Channel: 0, Timer: 0
** ledc: 9 => Group: 1, Channel: 1, Timer: 0
** ledc: 10 => Group: 1, Channel: 2, Timer: 1
** ledc: 11 => Group: 1, Channel: 3, Timer: 1
** ledc: 12 => Group: 1, Channel: 4, Timer: 2
** ledc: 13 => Group: 1, Channel: 5, Timer: 2
** ledc: 14 => Group: 1, Channel: 6, Timer: 3
** ledc: 15 => Group: 1, Channel: 7, Timer: 3
*/
#defineLEDC_CHAN(g,c) LEDC.channel_group[(g)].channel[(c)]
#defineLEDC_TIMER(g,t) LEDC.timer_group[(g)].timer[(t)]
staticvoid_on_apb_change(void*arg, apb_change_ev_tev_type, uint32_told_apb, uint32_tnew_apb){
if(ev_type==APB_AFTER_CHANGE&&old_apb!=new_apb){
uint32_tiarg= (uint32_t)arg;
uint8_tchan=iarg;
uint8_tgroup=(chan/8), timer=((chan/2)%4);
old_apb /= 1000000;
new_apb /= 1000000;
if(LEDC_TIMER(group, timer).conf.tick_sel){
LEDC_MUTEX_LOCK();
uint32_told_div=LEDC_TIMER(group, timer).conf.clock_divider;
uint32_tdiv_num= (new_apb*old_div) / old_apb;
if(div_num>LEDC_DIV_NUM_HSTIMER0_V){
new_apb=REF_CLK_FREQ / 1000000;
div_num= (new_apb*old_div) / old_apb;
if(div_num>LEDC_DIV_NUM_HSTIMER0_V) {
div_num=LEDC_DIV_NUM_HSTIMER0_V;//lowest clock possible
}
LEDC_TIMER(group, timer).conf.tick_sel=0;
} elseif(div_num<256) {
div_num=256;//highest clock possible
}
LEDC_TIMER(group, timer).conf.clock_divider=div_num;
LEDC_MUTEX_UNLOCK();
}
}
}
//uint32_t frequency = (80MHz or 1MHz)/((div_num / 256.0)*(1 << bit_num));
staticvoid_ledcSetupTimer(uint8_tchan, uint32_tdiv_num, uint8_tbit_num, boolapb_clk)
{
uint8_tgroup=(chan/8), timer=((chan/2)%4);
staticbooltHasStarted= false;
if(!tHasStarted) {
tHasStarted= true;
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_LEDC_CLK_EN);
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_LEDC_RST);
LEDC.conf.apb_clk_sel=1;//LS use apb clock
#if !CONFIG_DISABLE_HAL_LOCKS
_ledc_sys_lock=xSemaphoreCreateMutex();
#endif
}
LEDC_MUTEX_LOCK();
LEDC_TIMER(group, timer).conf.clock_divider=div_num;//18 bit (10.8) This register is used to configure parameter for divider in timer the least significant eight bits represent the decimal part.
LEDC_TIMER(group, timer).conf.duty_resolution=bit_num;//5 bit This register controls the range of the counter in timer. the counter range is [0 2**bit_num] the max bit width for counter is 20.
LEDC_TIMER(group, timer).conf.tick_sel=apb_clk;//apb clock
if(group) {
LEDC_TIMER(group, timer).conf.low_speed_update=1;//This bit is only useful for low speed timer channels, reserved for high speed timers
}
LEDC_TIMER(group, timer).conf.pause=0;
LEDC_TIMER(group, timer).conf.rst=1;//This bit is used to reset timer the counter will be 0 after reset.
LEDC_TIMER(group, timer).conf.rst=0;
LEDC_MUTEX_UNLOCK();
uint32_tiarg=chan;
addApbChangeCallback((void*)iarg, _on_apb_change);
}
//max div_num 0x3FFFF (262143)
//max bit_num 0x1F (31)
staticdouble_ledcSetupTimerFreq(uint8_tchan, doublefreq, uint8_tbit_num)
{
uint64_tclk_freq=getApbFrequency();
clk_freq <<= 8;//div_num is 8 bit decimal
uint32_tdiv_num= (clk_freq >> bit_num) / freq;
boolapb_clk= true;
if(div_num>LEDC_DIV_NUM_HSTIMER0_V) {
clk_freq /= 80;
div_num= (clk_freq >> bit_num) / freq;
if(div_num>LEDC_DIV_NUM_HSTIMER0_V) {
div_num=LEDC_DIV_NUM_HSTIMER0_V;//lowest clock possible
}
apb_clk= false;
} elseif(div_num<256) {
div_num=256;//highest clock possible
}
_ledcSetupTimer(chan, div_num, bit_num, apb_clk);
//log_i("Fin: %f, Fclk: %uMhz, bits: %u, DIV: %u, Fout: %f",
// freq, apb_clk?80:1, bit_num, div_num, (clk_freq >> bit_num) / (double)div_num);
return (clk_freq >> bit_num) / (double)div_num;
}
staticdouble_ledcTimerRead(uint8_tchan)
{
uint32_tdiv_num;
uint8_tbit_num;
boolapb_clk;
uint8_tgroup=(chan/8), timer=((chan/2)%4);
LEDC_MUTEX_LOCK();
div_num=LEDC_TIMER(group, timer).conf.clock_divider;//18 bit (10.8) This register is used to configure parameter for divider in timer the least significant eight bits represent the decimal part.
bit_num=LEDC_TIMER(group, timer).conf.duty_resolution;//5 bit This register controls the range of the counter in timer. the counter range is [0 2**bit_num] the max bit width for counter is 20.
apb_clk=LEDC_TIMER(group, timer).conf.tick_sel;//apb clock
LEDC_MUTEX_UNLOCK();
uint64_tclk_freq=1000000;
if(apb_clk) {
clk_freq=getApbFrequency();
}
clk_freq <<= 8;//div_num is 8 bit decimal
return (clk_freq >> bit_num) / (double)div_num;
}
staticvoid_ledcSetupChannel(uint8_tchan, uint8_tidle_level)
{
uint8_tgroup=(chan/8), channel=(chan%8), timer=((chan/2)%4);
LEDC_MUTEX_LOCK();
LEDC_CHAN(group, channel).conf0.timer_sel=timer;//2 bit Selects the timer to attach 0-3
LEDC_CHAN(group, channel).conf0.idle_lv=idle_level;//1 bit This bit is used to control the output value when channel is off.
LEDC_CHAN(group, channel).hpoint.hpoint=0;//20 bit The output value changes to high when timer selected by channel has reached hpoint
LEDC_CHAN(group, channel).conf1.duty_inc=1;//1 bit This register is used to increase the duty of output signal or decrease the duty of output signal for high speed channel
LEDC_CHAN(group, channel).conf1.duty_num=1;//10 bit This register is used to control the number of increased or decreased times for channel
LEDC_CHAN(group, channel).conf1.duty_cycle=1;//10 bit This register is used to increase or decrease the duty every duty_cycle cycles for channel
LEDC_CHAN(group, channel).conf1.duty_scale=0;//10 bit This register controls the increase or decrease step scale for channel.
LEDC_CHAN(group, channel).duty.duty=0;
LEDC_CHAN(group, channel).conf0.sig_out_en=0;//This is the output enable control bit for channel
LEDC_CHAN(group, channel).conf1.duty_start=0;//When duty_num duty_cycle and duty_scale has been configured. these register won't take effect until set duty_start. this bit is automatically cleared by hardware.
if(group) {
LEDC_CHAN(group, channel).conf0.val &= ~BIT(4);
} else {
LEDC_CHAN(group, channel).conf0.clk_en=0;
}
LEDC_MUTEX_UNLOCK();
}
doubleledcSetup(uint8_tchan, doublefreq, uint8_tbit_num)
{
if(chan>15) {
return0;
}
doubleres_freq=_ledcSetupTimerFreq(chan, freq, bit_num);
_ledcSetupChannel(chan, LOW);
returnres_freq;
}
voidledcWrite(uint8_tchan, uint32_tduty)
{
if(chan>15) {
return;
}
uint8_tgroup=(chan/8), channel=(chan%8);
LEDC_MUTEX_LOCK();
LEDC_CHAN(group, channel).duty.duty=duty << 4;//25 bit (21.4)
if(duty) {
LEDC_CHAN(group, channel).conf0.sig_out_en=1;//This is the output enable control bit for channel
LEDC_CHAN(group, channel).conf1.duty_start=1;//When duty_num duty_cycle and duty_scale has been configured. these register won't take effect until set duty_start. this bit is automatically cleared by hardware.
if(group) {
LEDC_CHAN(group, channel).conf0.val |= BIT(4);
} else {
LEDC_CHAN(group, channel).conf0.clk_en=1;
}
} else {
LEDC_CHAN(group, channel).conf0.sig_out_en=0;//This is the output enable control bit for channel
LEDC_CHAN(group, channel).conf1.duty_start=0;//When duty_num duty_cycle and duty_scale has been configured. these register won't take effect until set duty_start. this bit is automatically cleared by hardware.
if(group) {
LEDC_CHAN(group, channel).conf0.val &= ~BIT(4);
} else {
LEDC_CHAN(group, channel).conf0.clk_en=0;
}
}
LEDC_MUTEX_UNLOCK();
}
uint32_tledcRead(uint8_tchan)
{
if(chan>15) {
return0;
}
returnLEDC.channel_group[chan/8].channel[chan%8].duty.duty >> 4;
}
doubleledcReadFreq(uint8_tchan)
{
if(!ledcRead(chan)){
return0;
}
return_ledcTimerRead(chan);
}
doubleledcWriteTone(uint8_tchan, doublefreq)
{
if(chan>15) {
return0;
}
if(!freq) {
ledcWrite(chan, 0);
return0;
}
doubleres_freq=_ledcSetupTimerFreq(chan, freq, 10);
ledcWrite(chan, 0x1FF);
returnres_freq;
}
doubleledcWriteNote(uint8_tchan, note_tnote, uint8_toctave){
constuint16_tnoteFrequencyBase[12] = {
// C C# D Eb E F F# G G# A Bb B
4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902
};
if(octave>8||note >= NOTE_MAX){
return0;
}
doublenoteFreq= (double)noteFrequencyBase[note] / (double)(1 << (8-octave));
returnledcWriteTone(chan, noteFreq);
}
voidledcAttachPin(uint8_tpin, uint8_tchan)
{
if(chan>15) {
return;
}
pinMode(pin, OUTPUT);
pinMatrixOutAttach(pin, ((chan/8)?LEDC_LS_SIG_OUT0_IDX:LEDC_HS_SIG_OUT0_IDX) + (chan%8), false, false);
}
voidledcDetachPin(uint8_tpin)
{
pinMatrixOutDetach(pin, false, false);
}