44#include " freertos/queue.h"
55#include " freertos/semphr.h"
66
7+ #if SOC_LEDC_SUPPORTED
78static TaskHandle_t _tone_task = NULL ;
89static QueueHandle_t _tone_queue = NULL ;
910static int8_t _pin = -1 ;
11+ static uint8_t _channel = 255 ;
1012
1113typedef enum {
1214 TONE_START,
@@ -20,6 +22,12 @@ typedef struct {
2022 unsigned long duration;
2123} tone_msg_t ;
2224
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+
2331static void tone_task (void *) {
2432 tone_msg_t tone_msg;
2533 while (1 ) {
@@ -29,7 +37,13 @@ static void tone_task(void *) {
2937 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 );
3038
3139 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) {
3347 log_e (" Tone start failed" );
3448 break ;
3549 }
@@ -73,7 +87,7 @@ static int tone_init() {
7387 " toneTask" , // Name of the task
7488 3500 , // Stack size in words
7589 NULL , // Task input parameter
76- 1 , // Priority of the task
90+ 10 , // Priority of the task must be higher than Arduino task
7791 &_tone_task // Task handle.
7892 );
7993 if (_tone_task == NULL ) {
@@ -126,3 +140,13 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
126140 return ;
127141 }
128142}
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