4
4
#include " freertos/queue.h"
5
5
#include " freertos/semphr.h"
6
6
7
+ #if SOC_LEDC_SUPPORTED
7
8
static TaskHandle_t _tone_task = NULL ;
8
9
static QueueHandle_t _tone_queue = NULL ;
9
10
static int8_t _pin = -1 ;
11
+ static uint8_t _channel = 255 ;
10
12
11
13
typedef enum {
12
14
TONE_START,
@@ -20,6 +22,12 @@ typedef struct {
20
22
unsigned long duration;
21
23
} tone_msg_t ;
22
24
25
+ #ifdef SOC_LEDC_SUPPORT_HS_MODE
26
+ #define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM << 1 )
27
+ #else
28
+ #define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM)
29
+ #endif
30
+
23
31
static void tone_task (void *) {
24
32
tone_msg_t tone_msg;
25
33
while (1 ) {
@@ -29,7 +37,13 @@ static void tone_task(void *) {
29
37
log_d (" Task received from queue TONE_START: pin=%d, frequency=%u Hz, duration=%lu ms" , tone_msg.pin , tone_msg.frequency , tone_msg.duration );
30
38
31
39
if (_pin == -1 ) {
32
- if (ledcAttach (tone_msg.pin , tone_msg.frequency , 10 ) == 0 ) {
40
+ bool ret = true ;
41
+ if (_channel == 255 ) {
42
+ ret = ledcAttach (tone_msg.pin , tone_msg.frequency , 10 );
43
+ } else {
44
+ ret = ledcAttachChannel (tone_msg.pin , tone_msg.frequency , 10 , _channel);
45
+ }
46
+ if (!ret) {
33
47
log_e (" Tone start failed" );
34
48
break ;
35
49
}
@@ -73,7 +87,7 @@ static int tone_init() {
73
87
" toneTask" , // Name of the task
74
88
3500 , // Stack size in words
75
89
NULL , // Task input parameter
76
- 1 , // Priority of the task
90
+ 10 , // Priority of the task must be higher than Arduino task
77
91
&_tone_task // Task handle.
78
92
);
79
93
if (_tone_task == NULL ) {
@@ -126,3 +140,13 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
126
140
return ;
127
141
}
128
142
}
143
+
144
+ void setToneChannel (uint8_t channel) {
145
+ if (channel >= LEDC_CHANNELS) {
146
+ log_e (" Channel %u is not available (maximum %u)!" , channel, LEDC_CHANNELS);
147
+ return ;
148
+ }
149
+ _channel = channel;
150
+ }
151
+
152
+ #endif /* SOC_LEDC_SUPPORTED */
0 commit comments