esp32-hal-dac.c
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. 14 15 #include "esp32-hal-dac.h" 16 #include "freertos/FreeRTOS.h" 17 #include "freertos/task.h" 18 #include "rom/ets_sys.h" 19 #include "esp_attr.h" 20 #include "esp_intr.h" 21 #include "soc/rtc_io_reg.h" 22 #include "soc/rtc_cntl_reg.h" 23 #include "soc/sens_reg.h" 24 #include "esp32-hal-gpio.h" 25 26 27 void IRAM_ATTR __dacWrite(uint8_t pin, uint8_t value) 28 { 29 if(pin < 25 || pin > 26){ 30 return;//not dac pin 31 } 32 pinMode(pin, ANALOG); 33 uint8_t channel = pin - 25; 34 35 36 //Disable Tone 37 CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_SW_TONE_EN); 38 39 if (channel) { 40 //Disable Channel Tone 41 CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN2_M); 42 //Set the Dac value 43 SET_PERI_REG_BITS(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_DAC, value, RTC_IO_PDAC2_DAC_S); //dac_output 44 //Channel output enable 45 SET_PERI_REG_MASK(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_XPD_DAC | RTC_IO_PDAC2_DAC_XPD_FORCE); 46 } else { 47 //Disable Channel Tone 48 CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN1_M); 49 //Set the Dac value 50 SET_PERI_REG_BITS(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_DAC, value, RTC_IO_PDAC1_DAC_S); //dac_output 51 //Channel output enable 52 SET_PERI_REG_MASK(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_XPD_DAC | RTC_IO_PDAC1_DAC_XPD_FORCE); 53 } 54 } 55 56 extern void dacWrite(uint8_t pin, uint8_t value) __attribute__ ((weak, alias("__dacWrite")));