Skip to content

Commit a430528

Browse files
committed
LEDC Driver Update
- Add double precision to LEDC frequency - Add method for writing frequencies (Tones) - Add method for writing notes (8 channels polyphony anyone?)
1 parent 8e94809 commit a430528

File tree

2 files changed

+107
-43
lines changed

2 files changed

+107
-43
lines changed

cores/esp32/esp32-hal-ledc.c

+98-42
Original file line numberDiff line numberDiff line change
@@ -50,36 +50,39 @@ xSemaphoreHandle _ledc_sys_lock;
5050
** ledc: 14 => Group: 1, Channel: 6, Timer: 3
5151
** ledc: 15 => Group: 1, Channel: 7, Timer: 3
5252
*/
53+
#define LEDC_CHAN(g,c) LEDC.channel_group[(g)].channel[(c)]
54+
#define LEDC_TIMER(g,t) LEDC.timer_group[(g)].timer[(t)]
5355

5456
//uint32_t frequency = (80MHz or 1MHz)/((div_num / 256.0)*(1 << bit_num));
55-
void ledcSetupTimer(uint8_t chan, uint32_t div_num, uint8_t bit_num, bool apb_clk)
57+
static void _ledcSetupTimer(uint8_t chan, uint32_t div_num, uint8_t bit_num, bool apb_clk)
5658
{
57-
ledc_dev_t * ledc_dev = (volatile ledc_dev_t *)(DR_REG_LEDC_BASE);
5859
uint8_t group=(chan/8), timer=((chan/2)%4);
5960
static bool tHasStarted = false;
6061
if(!tHasStarted) {
6162
tHasStarted = true;
6263
SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_LEDC_CLK_EN);
6364
CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_LEDC_RST);
64-
ledc_dev->conf.apb_clk_sel = 1;//LS use apb clock
65+
LEDC.conf.apb_clk_sel = 1;//LS use apb clock
6566
#if !CONFIG_DISABLE_HAL_LOCKS
6667
_ledc_sys_lock = xSemaphoreCreateMutex();
6768
#endif
6869
}
6970
LEDC_MUTEX_LOCK();
70-
ledc_dev->timer_group[group].timer[timer].conf.div_num = 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.
71-
ledc_dev->timer_group[group].timer[timer].conf.bit_num = 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.
72-
ledc_dev->timer_group[group].timer[timer].conf.tick_sel = apb_clk;//apb clock
71+
LEDC_TIMER(group, timer).conf.div_num = 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.
72+
LEDC_TIMER(group, timer).conf.bit_num = 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.
73+
LEDC_TIMER(group, timer).conf.tick_sel = apb_clk;//apb clock
7374
if(group) {
74-
ledc_dev->timer_group[group].timer[timer].conf.low_speed_update = 1;//This bit is only useful for low speed timer channels, reserved for high speed timers
75+
LEDC_TIMER(group, timer).conf.low_speed_update = 1;//This bit is only useful for low speed timer channels, reserved for high speed timers
7576
}
76-
ledc_dev->timer_group[group].timer[timer].conf.pause = 0;
77-
ledc_dev->timer_group[group].timer[timer].conf.rst = 1;//This bit is used to reset timer the counter will be 0 after reset.
78-
ledc_dev->timer_group[group].timer[timer].conf.rst = 0;
77+
LEDC_TIMER(group, timer).conf.pause = 0;
78+
LEDC_TIMER(group, timer).conf.rst = 1;//This bit is used to reset timer the counter will be 0 after reset.
79+
LEDC_TIMER(group, timer).conf.rst = 0;
7980
LEDC_MUTEX_UNLOCK();
8081
}
8182

82-
uint32_t ledcSetupTimerFreq(uint8_t chan, uint32_t freq, uint8_t bit_num)
83+
//max div_num 0x3FFFF (262143)
84+
//max bit_num 0x1F (31)
85+
static double _ledcSetupTimerFreq(uint8_t chan, double freq, uint8_t bit_num)
8386
{
8487
uint64_t clk_freq = APB_CLK_FREQ;
8588
clk_freq <<= 8;//div_num is 8 bit decimal
@@ -95,40 +98,60 @@ uint32_t ledcSetupTimerFreq(uint8_t chan, uint32_t freq, uint8_t bit_num)
9598
} else if(div_num < 256) {
9699
div_num = 256;//highest clock possible
97100
}
98-
ledcSetupTimer(chan, div_num, bit_num, apb_clk);
99-
return (clk_freq >> bit_num) / div_num;
101+
_ledcSetupTimer(chan, div_num, bit_num, apb_clk);
102+
//log_i("Fin: %f, Fclk: %uMhz, bits: %u, DIV: %u, Fout: %f",
103+
// freq, apb_clk?80:1, bit_num, div_num, (clk_freq >> bit_num) / (double)div_num);
104+
return (clk_freq >> bit_num) / (double)div_num;
100105
}
101106

102-
void ledcSetupChannel(uint8_t chan, uint8_t idle_level)
107+
static double _ledcTimerRead(uint8_t chan)
108+
{
109+
uint32_t div_num;
110+
uint8_t bit_num;
111+
bool apb_clk;
112+
uint8_t group=(chan/8), timer=((chan/2)%4);
113+
LEDC_MUTEX_LOCK();
114+
div_num = LEDC_TIMER(group, timer).conf.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.
115+
bit_num = LEDC_TIMER(group, timer).conf.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.
116+
apb_clk = LEDC_TIMER(group, timer).conf.tick_sel;//apb clock
117+
LEDC_MUTEX_UNLOCK();
118+
uint64_t clk_freq = 1000000;
119+
if(apb_clk) {
120+
clk_freq *= 80;
121+
}
122+
clk_freq <<= 8;//div_num is 8 bit decimal
123+
return (clk_freq >> bit_num) / (double)div_num;
124+
}
125+
126+
static void _ledcSetupChannel(uint8_t chan, uint8_t idle_level)
103127
{
104128
uint8_t group=(chan/8), channel=(chan%8), timer=((chan/2)%4);
105-
ledc_dev_t * ledc_dev = (volatile ledc_dev_t *)(DR_REG_LEDC_BASE);
106129
LEDC_MUTEX_LOCK();
107-
ledc_dev->channel_group[group].channel[channel].conf0.timer_sel = timer;//2 bit Selects the timer to attach 0-3
108-
ledc_dev->channel_group[group].channel[channel].conf0.idle_lv = idle_level;//1 bit This bit is used to control the output value when channel is off.
109-
ledc_dev->channel_group[group].channel[channel].hpoint.hpoint = 0;//20 bit The output value changes to high when timer selected by channel has reached hpoint
110-
ledc_dev->channel_group[group].channel[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
111-
ledc_dev->channel_group[group].channel[channel].conf1.duty_num = 1;//10 bit This register is used to control the number of increased or decreased times for channel
112-
ledc_dev->channel_group[group].channel[channel].conf1.duty_cycle = 1;//10 bit This register is used to increase or decrease the duty every duty_cycle cycles for channel
113-
ledc_dev->channel_group[group].channel[channel].conf1.duty_scale = 0;//10 bit This register controls the increase or decrease step scale for channel.
114-
ledc_dev->channel_group[group].channel[channel].duty.duty = 0;
115-
ledc_dev->channel_group[group].channel[channel].conf0.sig_out_en = 0;//This is the output enable control bit for channel
116-
ledc_dev->channel_group[group].channel[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.
130+
LEDC_CHAN(group, channel).conf0.timer_sel = timer;//2 bit Selects the timer to attach 0-3
131+
LEDC_CHAN(group, channel).conf0.idle_lv = idle_level;//1 bit This bit is used to control the output value when channel is off.
132+
LEDC_CHAN(group, channel).hpoint.hpoint = 0;//20 bit The output value changes to high when timer selected by channel has reached hpoint
133+
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
134+
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
135+
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
136+
LEDC_CHAN(group, channel).conf1.duty_scale = 0;//10 bit This register controls the increase or decrease step scale for channel.
137+
LEDC_CHAN(group, channel).duty.duty = 0;
138+
LEDC_CHAN(group, channel).conf0.sig_out_en = 0;//This is the output enable control bit for channel
139+
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.
117140
if(group) {
118-
ledc_dev->channel_group[group].channel[channel].conf0.val &= ~BIT(4);
141+
LEDC_CHAN(group, channel).conf0.val &= ~BIT(4);
119142
} else {
120-
ledc_dev->channel_group[group].channel[channel].conf0.clk_en = 0;
143+
LEDC_CHAN(group, channel).conf0.clk_en = 0;
121144
}
122145
LEDC_MUTEX_UNLOCK();
123146
}
124147

125-
uint32_t ledcSetup(uint8_t chan, uint32_t freq, uint8_t bit_num)
148+
double ledcSetup(uint8_t chan, double freq, uint8_t bit_num)
126149
{
127150
if(chan > 15) {
128151
return 0;
129152
}
130-
uint32_t res_freq = ledcSetupTimerFreq(chan, freq, bit_num);
131-
ledcSetupChannel(chan, LOW);
153+
double res_freq = _ledcSetupTimerFreq(chan, freq, bit_num);
154+
_ledcSetupChannel(chan, LOW);
132155
return res_freq;
133156
}
134157

@@ -138,24 +161,23 @@ void ledcWrite(uint8_t chan, uint32_t duty)
138161
return;
139162
}
140163
uint8_t group=(chan/8), channel=(chan%8);
141-
ledc_dev_t * ledc_dev = (volatile ledc_dev_t *)(DR_REG_LEDC_BASE);
142164
LEDC_MUTEX_LOCK();
143-
ledc_dev->channel_group[group].channel[channel].duty.duty = duty << 4;//25 bit (21.4)
165+
LEDC_CHAN(group, channel).duty.duty = duty << 4;//25 bit (21.4)
144166
if(duty) {
145-
ledc_dev->channel_group[group].channel[channel].conf0.sig_out_en = 1;//This is the output enable control bit for channel
146-
ledc_dev->channel_group[group].channel[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.
167+
LEDC_CHAN(group, channel).conf0.sig_out_en = 1;//This is the output enable control bit for channel
168+
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.
147169
if(group) {
148-
ledc_dev->channel_group[group].channel[channel].conf0.val |= BIT(4);
170+
LEDC_CHAN(group, channel).conf0.val |= BIT(4);
149171
} else {
150-
ledc_dev->channel_group[group].channel[channel].conf0.clk_en = 1;
172+
LEDC_CHAN(group, channel).conf0.clk_en = 1;
151173
}
152174
} else {
153-
ledc_dev->channel_group[group].channel[channel].conf0.sig_out_en = 0;//This is the output enable control bit for channel
154-
ledc_dev->channel_group[group].channel[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.
175+
LEDC_CHAN(group, channel).conf0.sig_out_en = 0;//This is the output enable control bit for channel
176+
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.
155177
if(group) {
156-
ledc_dev->channel_group[group].channel[channel].conf0.val &= ~BIT(4);
178+
LEDC_CHAN(group, channel).conf0.val &= ~BIT(4);
157179
} else {
158-
ledc_dev->channel_group[group].channel[channel].conf0.clk_en = 0;
180+
LEDC_CHAN(group, channel).conf0.clk_en = 0;
159181
}
160182
}
161183
LEDC_MUTEX_UNLOCK();
@@ -166,8 +188,42 @@ uint32_t ledcRead(uint8_t chan)
166188
if(chan > 15) {
167189
return 0;
168190
}
169-
ledc_dev_t * ledc_dev = (volatile ledc_dev_t *)(DR_REG_LEDC_BASE);
170-
return ledc_dev->channel_group[chan/8].channel[chan%8].duty.duty >> 4;
191+
return LEDC.channel_group[chan/8].channel[chan%8].duty.duty >> 4;
192+
}
193+
194+
double ledcReadFreq(uint8_t chan)
195+
{
196+
if(!ledcRead(chan)){
197+
return 0;
198+
}
199+
return _ledcTimerRead(chan);
200+
}
201+
202+
double ledcWriteTone(uint8_t chan, double freq)
203+
{
204+
if(chan > 15) {
205+
return 0;
206+
}
207+
if(!freq) {
208+
ledcWrite(chan, 0);
209+
return 0;
210+
}
211+
double res_freq = _ledcSetupTimerFreq(chan, freq, 10);
212+
ledcWrite(chan, 0x1FF);
213+
return res_freq;
214+
}
215+
216+
double ledcWriteNote(uint8_t chan, note_t note, uint8_t octave){
217+
const uint16_t noteFrequencyBase[12] = {
218+
// C C# D Eb E F F# G G# A Bb B
219+
4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902
220+
};
221+
222+
if(octave > 8 || note >= NOTE_MAX){
223+
return 0;
224+
}
225+
double noteFreq = (double)noteFrequencyBase[note] / (double)(1 << (8-octave));
226+
return ledcWriteTone(chan, noteFreq);
171227
}
172228

173229
void ledcAttachPin(uint8_t pin, uint8_t chan)

cores/esp32/esp32-hal-ledc.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ extern "C" {
2222
#include <stdint.h>
2323
#include <stdbool.h>
2424

25+
typedef enum {
26+
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
27+
} note_t;
28+
2529
//channel 0-15 resolution 1-16bits freq limits depend on resolution
26-
uint32_t ledcSetup(uint8_t channel, uint32_t freq, uint8_t resolution_bits);
30+
double ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits);
2731
void ledcWrite(uint8_t channel, uint32_t duty);
32+
double ledcWriteTone(uint8_t channel, double freq);
33+
double ledcWriteNote(uint8_t channel, note_t note, uint8_t octave);
2834
uint32_t ledcRead(uint8_t channel);
35+
double ledcReadFreq(uint8_t channel);
2936
void ledcAttachPin(uint8_t pin, uint8_t channel);
3037
void ledcDetachPin(uint8_t pin);
3138

39+
3240
#ifdef __cplusplus
3341
}
3442
#endif

0 commit comments

Comments
 (0)