Skip to content

Commit f9cddfd

Browse files
committed
Update DAC driver to use the new ESP-IDF driver API
1 parent 5b43dee commit f9cddfd

File tree

4 files changed

+82
-63
lines changed

4 files changed

+82
-63
lines changed

cores/esp32/esp32-hal-dac.c

+61-33
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,77 @@
1-
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
1+
/*
2+
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
146

15-
#include "esp32-hal.h"
16-
#include "soc/soc_caps.h"
7+
#include "esp32-hal-dac.h"
178

18-
#ifndef SOC_DAC_SUPPORTED
19-
#define NODAC
20-
#else
9+
#if SOC_DAC_SUPPORTED
10+
#include "esp32-hal.h"
11+
#include "esp32-hal-periman.h"
2112
#include "soc/dac_channel.h"
22-
#include "driver/dac.h"
13+
#include "driver/dac_oneshot.h"
14+
15+
static bool dacDetachBus(void * bus){
16+
esp_err_t err = dac_oneshot_del_channel((dac_oneshot_handle_t)bus);
17+
if(err != ESP_OK){
18+
log_e("dac_oneshot_del_channel failed with error: %d", err);
19+
return false;
20+
}
21+
return true;
22+
}
2323

24-
void ARDUINO_ISR_ATTR __dacWrite(uint8_t pin, uint8_t value)
24+
bool __dacWrite(uint8_t pin, uint8_t value)
2525
{
26-
if(pin < DAC_CHANNEL_1_GPIO_NUM || pin > DAC_CHANNEL_2_GPIO_NUM){
27-
return;//not dac pin
26+
esp_err_t err = ESP_OK;
27+
if(pin != DAC_CHAN0_GPIO_NUM && pin != DAC_CHAN1_GPIO_NUM){
28+
log_e("pin %u is not a DAC pin", pin);
29+
return false;//not dac pin
2830
}
2931

30-
uint8_t channel = pin - DAC_CHANNEL_1_GPIO_NUM;
31-
dac_output_enable(channel);
32-
dac_output_voltage(channel, value);
32+
dac_oneshot_handle_t bus = (dac_oneshot_handle_t)perimanGetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT);
33+
if(bus == NULL){
34+
perimanSetBusDeinit(ESP32_BUS_TYPE_DAC_ONESHOT, dacDetachBus);
35+
dac_channel_t channel = (pin == DAC_CHAN0_GPIO_NUM)?DAC_CHAN_0:DAC_CHAN_1;
36+
dac_oneshot_config_t config = {
37+
.chan_id = channel
38+
};
39+
err = dac_oneshot_new_channel(&config, &bus);
40+
if(err != ESP_OK){
41+
log_e("dac_oneshot_new_channel failed with error: %d", err);
42+
return false;
43+
}
44+
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT, (void *)bus)){
45+
dacDetachBus((void *)bus);
46+
return false;
47+
}
48+
}
3349

50+
err = dac_oneshot_output_voltage(bus, value);
51+
if(err != ESP_OK){
52+
log_e("dac_oneshot_output_voltage failed with error: %d", err);
53+
return false;
54+
}
55+
return true;
3456
}
3557

36-
void ARDUINO_ISR_ATTR __dacDisable(uint8_t pin)
58+
bool __dacDisable(uint8_t pin)
3759
{
38-
if(pin < DAC_CHANNEL_1_GPIO_NUM || pin > DAC_CHANNEL_2_GPIO_NUM){
39-
return;//not dac pin
60+
if(pin != DAC_CHAN0_GPIO_NUM && pin != DAC_CHAN1_GPIO_NUM){
61+
log_e("pin %u is not a DAC pin", pin);
62+
return false;//not dac pin
4063
}
41-
42-
uint8_t channel = pin - DAC_CHANNEL_1_GPIO_NUM;
43-
dac_output_disable(channel);
64+
void * bus = perimanGetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT);
65+
if(bus != NULL){
66+
// will call dacDetachBus
67+
return perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL);
68+
} else {
69+
log_e("pin %u is not attached to DAC", pin);
70+
}
71+
return false;
4472
}
4573

46-
extern void dacWrite(uint8_t pin, uint8_t value) __attribute__ ((weak, alias("__dacWrite")));
47-
extern void dacDisable(uint8_t pin) __attribute__ ((weak, alias("__dacDisable")));
74+
extern bool dacWrite(uint8_t pin, uint8_t value) __attribute__ ((weak, alias("__dacWrite")));
75+
extern bool dacDisable(uint8_t pin) __attribute__ ((weak, alias("__dacDisable")));
4876

4977
#endif

cores/esp32/esp32-hal-dac.h

+12-23
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
11
/*
2-
Arduino.h - Main include file for the Arduino SDK
3-
Copyright (c) 2005-2013 Arduino Team. All right reserved.
4-
5-
This library is free software; you can redistribute it and/or
6-
modify it under the terms of the GNU Lesser General Public
7-
License as published by the Free Software Foundation; either
8-
version 2.1 of the License, or (at your option) any later version.
9-
10-
This library is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13-
Lesser General Public License for more details.
14-
15-
You should have received a copy of the GNU Lesser General Public
16-
License along with this library; if not, write to the Free Software
17-
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2+
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
185
*/
196

20-
#ifndef MAIN_ESP32_HAL_DAC_H_
21-
#define MAIN_ESP32_HAL_DAC_H_
7+
#pragma once
8+
9+
#include "soc/soc_caps.h"
10+
#if SOC_DAC_SUPPORTED
2211

2312
#ifdef __cplusplus
2413
extern "C" {
2514
#endif
2615

27-
#include "esp32-hal.h"
28-
#include "driver/gpio.h"
16+
#include <stdint.h>
17+
#include <stdbool.h>
2918

30-
void dacWrite(uint8_t pin, uint8_t value);
31-
void dacDisable(uint8_t pin);
19+
bool dacWrite(uint8_t pin, uint8_t value);
20+
bool dacDisable(uint8_t pin);
3221

3322
#ifdef __cplusplus
3423
}
3524
#endif
3625

37-
#endif /* MAIN_ESP32_HAL_DAC_H_ */
26+
#endif /* SOC_DAC_SUPPORTED */

cores/esp32/esp32-hal-sigmadelta.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include "esp32-hal.h"
8-
#include "esp32-hal-periman.h"
7+
#include "esp32-hal-sigmadelta.h"
98

109
#if SOC_SDM_SUPPORTED
10+
#include "esp32-hal.h"
11+
#include "esp32-hal-periman.h"
1112
#include "driver/sdm.h"
1213

1314
static bool sigmaDeltaDetachBus(void * bus){
@@ -82,7 +83,7 @@ bool sigmaDeltaDetach(uint8_t pin)
8283
// will call sigmaDeltaDetachBus
8384
return perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL);
8485
} else {
85-
log_e("pin %u is not attached to SigmaDelta");
86+
log_e("pin %u is not attached to SigmaDelta", pin);
8687
}
8788
return false;
8889
}

cores/esp32/esp32-hal-sigmadelta.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
#pragma once
88

9+
#include "soc/soc_caps.h"
10+
#if SOC_SDM_SUPPORTED
11+
912
#ifdef __cplusplus
1013
extern "C" {
1114
#endif
1215

13-
#include "soc/soc_caps.h"
14-
#if SOC_SDM_SUPPORTED
15-
1616
#include <stdint.h>
1717
#include <stdbool.h>
1818

@@ -21,7 +21,8 @@ bool sigmaDeltaAttach(uint8_t pin, uint32_t freq);
2121
bool sigmaDeltaWrite(uint8_t pin, uint8_t duty);
2222
bool sigmaDeltaDetach(uint8_t pin);
2323

24-
#endif
2524
#ifdef __cplusplus
2625
}
2726
#endif
27+
28+
#endif /* SOC_SDM_SUPPORTED */

0 commit comments

Comments
 (0)