|
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 | + */ |
14 | 6 |
|
15 |
| -#include "esp32-hal.h" |
16 |
| -#include "soc/soc_caps.h" |
| 7 | +#include "esp32-hal-dac.h" |
17 | 8 |
|
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" |
21 | 12 | #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 | +} |
23 | 23 |
|
24 |
| -void ARDUINO_ISR_ATTR __dacWrite(uint8_t pin, uint8_t value) |
| 24 | +bool __dacWrite(uint8_t pin, uint8_t value) |
25 | 25 | {
|
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 |
28 | 30 | }
|
29 | 31 |
|
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 | + } |
33 | 49 |
|
| 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; |
34 | 56 | }
|
35 | 57 |
|
36 |
| -void ARDUINO_ISR_ATTR __dacDisable(uint8_t pin) |
| 58 | +bool __dacDisable(uint8_t pin) |
37 | 59 | {
|
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 |
40 | 63 | }
|
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; |
44 | 72 | }
|
45 | 73 |
|
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"))); |
48 | 76 |
|
49 | 77 | #endif
|
0 commit comments