adac_core.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. #include <string.h>
  12. #include <freertos/FreeRTOS.h>
  13. #include <freertos/task.h>
  14. #include <driver/i2s.h>
  15. #include "driver/i2c.h"
  16. #include "esp_log.h"
  17. #include "adac.h"
  18. static const char TAG[] = "DAC core";
  19. static int i2c_port = -1;
  20. /****************************************************************************************
  21. * init
  22. */
  23. int adac_init(char *config, int i2c_port_num) {
  24. char *p;
  25. int i2c_addr = 0;
  26. // some crappy codecs require MCLK to work
  27. if ((p = strcasestr(config, "mck")) != NULL) {
  28. ESP_LOGI(TAG, "Configuring MCLK on GPIO0");
  29. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
  30. REG_WRITE(PIN_CTRL, 0xFFFFFFF0);
  31. }
  32. i2c_port = i2c_port_num;
  33. // configure i2c
  34. i2c_config_t i2c_config = {
  35. .mode = I2C_MODE_MASTER,
  36. .sda_io_num = -1,
  37. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  38. .scl_io_num = -1,
  39. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  40. .master.clk_speed = 250000,
  41. };
  42. if ((p = strcasestr(config, "i2c")) != NULL) i2c_addr = atoi(strchr(p, '=') + 1);
  43. if ((p = strcasestr(config, "sda")) != NULL) i2c_config.sda_io_num = atoi(strchr(p, '=') + 1);
  44. if ((p = strcasestr(config, "scl")) != NULL) i2c_config.scl_io_num = atoi(strchr(p, '=') + 1);
  45. if (i2c_config.sda_io_num == -1 || i2c_config.scl_io_num == -1) {
  46. ESP_LOGW(TAG, "DAC does not use i2c");
  47. return i2c_addr;
  48. }
  49. ESP_LOGI(TAG, "DAC uses I2C port:%d, sda:%d, scl:%d", i2c_port, i2c_config.sda_io_num, i2c_config.scl_io_num);
  50. // we have an I2C configured
  51. i2c_param_config(i2c_port, &i2c_config);
  52. i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  53. return i2c_addr;
  54. }
  55. /****************************************************************************************
  56. * close
  57. */
  58. void adac_deinit(void) {
  59. if (i2c_port != -1) i2c_driver_delete(i2c_port);
  60. }
  61. /****************************************************************************************
  62. *
  63. */
  64. esp_err_t adac_write_byte(int i2c_addr,uint8_t reg, uint8_t val) {
  65. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  66. i2c_master_start(cmd);
  67. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  68. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  69. i2c_master_write_byte(cmd, val, I2C_MASTER_NACK);
  70. i2c_master_stop(cmd);
  71. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  72. i2c_cmd_link_delete(cmd);
  73. if (ret != ESP_OK) {
  74. ESP_LOGW(TAG, "I2C write failed");
  75. }
  76. return ret;
  77. }
  78. /****************************************************************************************
  79. *
  80. */
  81. uint8_t adac_read_byte(int i2c_addr, uint8_t reg) {
  82. uint8_t data = 255;
  83. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  84. i2c_master_start(cmd);
  85. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  86. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  87. i2c_master_start(cmd);
  88. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_READ, I2C_MASTER_NACK);
  89. i2c_master_read_byte(cmd, &data, I2C_MASTER_NACK);
  90. i2c_master_stop(cmd);
  91. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  92. i2c_cmd_link_delete(cmd);
  93. if (ret != ESP_OK) {
  94. ESP_LOGW(TAG, "I2C read failed");
  95. }
  96. return data;
  97. }
  98. /****************************************************************************************
  99. *
  100. */
  101. uint16_t adac_read_word(int i2c_addr, uint8_t reg) {
  102. uint8_t data[2] = { 255, 255 };
  103. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  104. i2c_master_start(cmd);
  105. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  106. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  107. i2c_master_start(cmd);
  108. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_READ, I2C_MASTER_NACK);
  109. i2c_master_read(cmd, data, 2, I2C_MASTER_NACK);
  110. i2c_master_stop(cmd);
  111. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  112. i2c_cmd_link_delete(cmd);
  113. if (ret != ESP_OK) {
  114. ESP_LOGW(TAG, "I2C read failed");
  115. }
  116. return (data[0] << 8) | data[1];
  117. }
  118. /****************************************************************************************
  119. *
  120. */
  121. esp_err_t adac_write_word(int i2c_addr, uint8_t reg, uint16_t val)
  122. {
  123. uint8_t data[] = { i2c_addr << 1, reg,
  124. val >> 8, val & 0xff };
  125. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  126. i2c_master_start(cmd);
  127. i2c_master_write(cmd, data, 4, I2C_MASTER_NACK);
  128. i2c_master_stop(cmd);
  129. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  130. i2c_cmd_link_delete(cmd);
  131. if (ret != ESP_OK) {
  132. ESP_LOGW(TAG, "I2C write failed");
  133. }
  134. return ret;
  135. }