arduino-esp32/cores/esp32/esp32-hal-dac.c
Jan Procházka 0d0d27fba8
Extend bus types and structure for Peripheral Manager (#8888)
* Extended bus types for peripheral manager

* add extra_type to peripheral manager

* Fix function call

* Fix check for no extra_type

* Add extra_type for SD and ETH CS pin

* added bus_num and bus_channel to pin structure

* update printPerimanInfo with new fields

* replaced setting bus to INIT with DetachPin call

* add defines for extra attributes

* use new required parameters in perimanSetPinBus

* function rename

* removed duplicate define

* Update print format

---------

Co-authored-by: Rodrigo Garcia <rodrigo.garcia@espressif.com>
2023-11-25 21:26:03 +02:00

81 lines
2.3 KiB
C

/*
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "esp32-hal-dac.h"
#if SOC_DAC_SUPPORTED
#include "esp32-hal.h"
#include "esp32-hal-periman.h"
#include "soc/dac_channel.h"
#include "driver/dac_oneshot.h"
static bool dacDetachBus(void * bus){
esp_err_t err = dac_oneshot_del_channel((dac_oneshot_handle_t)bus);
if(err != ESP_OK){
log_e("dac_oneshot_del_channel failed with error: %d", err);
return false;
}
return true;
}
bool __dacWrite(uint8_t pin, uint8_t value)
{
esp_err_t err = ESP_OK;
if(pin != DAC_CHAN0_GPIO_NUM && pin != DAC_CHAN1_GPIO_NUM){
log_e("pin %u is not a DAC pin", pin);
return false;//not dac pin
}
dac_oneshot_handle_t bus = (dac_oneshot_handle_t)perimanGetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT);
if(bus == NULL){
perimanSetBusDeinit(ESP32_BUS_TYPE_DAC_ONESHOT, dacDetachBus);
if(!perimanClearPinBus(pin)){
return false;
}
dac_channel_t channel = (pin == DAC_CHAN0_GPIO_NUM)?DAC_CHAN_0:DAC_CHAN_1;
dac_oneshot_config_t config = {
.chan_id = channel
};
err = dac_oneshot_new_channel(&config, &bus);
if(err != ESP_OK){
log_e("dac_oneshot_new_channel failed with error: %d", err);
return false;
}
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT, (void *)bus, -1, channel)){
dacDetachBus((void *)bus);
return false;
}
}
err = dac_oneshot_output_voltage(bus, value);
if(err != ESP_OK){
log_e("dac_oneshot_output_voltage failed with error: %d", err);
return false;
}
return true;
}
bool __dacDisable(uint8_t pin)
{
if(pin != DAC_CHAN0_GPIO_NUM && pin != DAC_CHAN1_GPIO_NUM){
log_e("pin %u is not a DAC pin", pin);
return false;//not dac pin
}
void * bus = perimanGetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT);
if(bus != NULL){
// will call dacDetachBus
return perimanClearPinBus(pin);
} else {
log_e("pin %u is not attached to DAC", pin);
}
return false;
}
extern bool dacWrite(uint8_t pin, uint8_t value) __attribute__ ((weak, alias("__dacWrite")));
extern bool dacDisable(uint8_t pin) __attribute__ ((weak, alias("__dacDisable")));
#endif