DownloadRaw

contents

  1. #include <driver/dac.h>
  2. #include <soc/rtc.h>
  3. #include <soc/sens_reg.h>
  4. //there is some overhead to enter the mutex, disable the tone generator, and safe the other bits of the register
  5. //this is taken out the inner loop of our code by doing this stuff just unce using prepare and unprepare
  6. //the functions are inline, so there is no overhead of a function call
  7. //the mutex can be ignored if no other task is messing around with the DAC registers. the compiler will optimize the mutex away then
  8. extern portMUX_TYPE rtc_spinlock;
  9. //static portMUX_TYPE rtc_spinlock = portMUX_INITIALIZER_UNLOCKED;
  10. //vPortCPUInitializeMutex(&rtc_spinlock);
  11. // Methods of initialization.
  12. // the timer function uses a spinlock that is embedded in the timer object, while portENTER_CRITICAL
  13. // needs you to provide your own spinlock. The effect is the same, however.
  14. int cleanRegDAC1;
  15. int cleanRegDAC2;
  16. void inline DACPrepare(const bool useMutex)
  17. {
  18. if(useMutex)
  19. portENTER_CRITICAL(&rtc_spinlock);
  20. CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_SW_TONE_EN);
  21. CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN1_M);
  22. CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN2_M);
  23. SET_PERI_REG_BITS(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_DAC, 255, RTC_IO_PDAC1_DAC_S);
  24. cleanRegDAC1 = (READ_PERI_REG(RTC_IO_PAD_DAC1_REG)&(~((RTC_IO_PDAC1_DAC)<<(RTC_IO_PDAC1_DAC_S))));
  25. cleanRegDAC2 = (READ_PERI_REG(RTC_IO_PAD_DAC2_REG)&(~((RTC_IO_PDAC2_DAC)<<(RTC_IO_PDAC2_DAC_S))));
  26. }
  27. void inline DAC1Write(const int value)
  28. {
  29. WRITE_PERI_REG(RTC_IO_PAD_DAC1_REG, cleanRegDAC1 | ((value & RTC_IO_PDAC1_DAC) << RTC_IO_PDAC1_DAC_S));
  30. }
  31. void inline DAC2Write(const int value)
  32. {
  33. WRITE_PERI_REG(RTC_IO_PAD_DAC2_REG, cleanRegDAC2 | ((value & RTC_IO_PDAC2_DAC) << RTC_IO_PDAC2_DAC_S));
  34. }
  35. void inline DACUnprepare(const bool useMutex)
  36. {
  37. if(useMutex)
  38. portEXIT_CRITICAL(&rtc_spinlock);
  39. }
  40. // Original DAC output code..
  41. /*
  42. #define SET_PERI_REG_BITS(reg,bit_map,value,shift) ({ \
  43. ASSERT_IF_DPORT_REG((reg), SET_PERI_REG_BITS); \
  44. (WRITE_PERI_REG((reg),(READ_PERI_REG(reg)&(~((bit_map)<<(shift))))|(((value) & bit_map)<<(shift)) )); \
  45. })
  46. //get field of register
  47. #define GET_PERI_REG_BITS2(reg, mask,shift) ({ \
  48. ASSERT_IF_DPORT_REG((reg), GET_PERI_REG_BITS2); \
  49. ((READ_PERI_REG(reg)>>(shift))&(mask)); \
  50. })
  51. esp_err_t dac_output_voltage(dac_channel_t channel, uint8_t dac_value)
  52. {
  53. RTC_MODULE_CHECK((channel >= DAC_CHANNEL_1) && (channel < DAC_CHANNEL_MAX), DAC_ERR_STR_CHANNEL_ERROR, ESP_ERR_INVALID_ARG);
  54. portENTER_CRITICAL(&rtc_spinlock);
  55. //Disable Tone
  56. CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_SW_TONE_EN);
  57. //Disable Channel Tone
  58. if (channel == DAC_CHANNEL_1) {
  59. CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN1_M);
  60. } else if (channel == DAC_CHANNEL_2) {
  61. CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN2_M);
  62. }
  63. //Set the Dac value
  64. if (channel == DAC_CHANNEL_1) {
  65. SET_PERI_REG_BITS(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_DAC, dac_value, RTC_IO_PDAC1_DAC_S); //dac_output
  66. } else if (channel == DAC_CHANNEL_2) {
  67. SET_PERI_REG_BITS(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_DAC, dac_value, RTC_IO_PDAC2_DAC_S); //dac_output
  68. }
  69. portEXIT_CRITICAL(&rtc_spinlock);
  70. return ESP_OK;
  71. }
  72. */