adac_core.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #define PARSE_PARAM(S,P,C,V) do { \
  19. char *__p; \
  20. if ((__p = strcasestr(S, P)) && (__p = strchr(__p, C))) V = atoi(__p+1); \
  21. } while (0)
  22. static const char TAG[] = "DAC core";
  23. static int i2c_port = -1;
  24. /****************************************************************************************
  25. * init
  26. */
  27. int adac_init(char *config, int i2c_port_num) {
  28. char *p;
  29. int i2c_addr = 0;
  30. // some crappy codecs require MCLK to work
  31. if ((p = strcasestr(config, "mck")) != NULL) {
  32. ESP_LOGI(TAG, "Configuring MCLK on GPIO0");
  33. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
  34. REG_WRITE(PIN_CTRL, 0xFFFFFFF0);
  35. }
  36. i2c_port = i2c_port_num;
  37. // configure i2c
  38. i2c_config_t i2c_config = {
  39. .mode = I2C_MODE_MASTER,
  40. .sda_io_num = -1,
  41. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  42. .scl_io_num = -1,
  43. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  44. .master.clk_speed = 250000,
  45. };
  46. PARSE_PARAM(config, "i2c", '=', i2c_addr);
  47. PARSE_PARAM(config, "sda", '=', i2c_config.sda_io_num);
  48. PARSE_PARAM(config, "scl", '=', i2c_config.scl_io_num);
  49. if (i2c_config.sda_io_num == -1 || i2c_config.scl_io_num == -1) {
  50. ESP_LOGW(TAG, "DAC does not use i2c");
  51. return i2c_addr;
  52. }
  53. ESP_LOGI(TAG, "DAC uses I2C port:%d, sda:%d, scl:%d", i2c_port, i2c_config.sda_io_num, i2c_config.scl_io_num);
  54. // we have an I2C configured
  55. i2c_param_config(i2c_port, &i2c_config);
  56. i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  57. return i2c_addr;
  58. }
  59. /****************************************************************************************
  60. * close
  61. */
  62. void adac_deinit(void) {
  63. if (i2c_port != -1) i2c_driver_delete(i2c_port);
  64. }
  65. /****************************************************************************************
  66. *
  67. */
  68. esp_err_t adac_write_byte(int i2c_addr,uint8_t reg, uint8_t val) {
  69. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  70. i2c_master_start(cmd);
  71. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  72. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  73. i2c_master_write_byte(cmd, val, I2C_MASTER_NACK);
  74. i2c_master_stop(cmd);
  75. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  76. i2c_cmd_link_delete(cmd);
  77. if (ret != ESP_OK) {
  78. ESP_LOGW(TAG, "I2C write failed");
  79. }
  80. return ret;
  81. }
  82. /****************************************************************************************
  83. *
  84. */
  85. uint8_t adac_read_byte(int i2c_addr, uint8_t reg) {
  86. uint8_t data = 255;
  87. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  88. i2c_master_start(cmd);
  89. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  90. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  91. i2c_master_start(cmd);
  92. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_READ, I2C_MASTER_NACK);
  93. i2c_master_read_byte(cmd, &data, I2C_MASTER_NACK);
  94. i2c_master_stop(cmd);
  95. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  96. i2c_cmd_link_delete(cmd);
  97. if (ret != ESP_OK) {
  98. ESP_LOGW(TAG, "I2C read failed");
  99. }
  100. return data;
  101. }
  102. /****************************************************************************************
  103. *
  104. */
  105. uint16_t adac_read_word(int i2c_addr, uint8_t reg) {
  106. uint8_t data[2] = { 255, 255 };
  107. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  108. i2c_master_start(cmd);
  109. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  110. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  111. i2c_master_start(cmd);
  112. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_READ, I2C_MASTER_NACK);
  113. i2c_master_read(cmd, data, 2, I2C_MASTER_NACK);
  114. i2c_master_stop(cmd);
  115. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  116. i2c_cmd_link_delete(cmd);
  117. if (ret != ESP_OK) {
  118. ESP_LOGW(TAG, "I2C read failed");
  119. }
  120. return (data[0] << 8) | data[1];
  121. }
  122. /****************************************************************************************
  123. *
  124. */
  125. esp_err_t adac_write_word(int i2c_addr, uint8_t reg, uint16_t val) {
  126. uint8_t data[] = { i2c_addr << 1, reg,
  127. val >> 8, val & 0xff };
  128. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  129. i2c_master_start(cmd);
  130. i2c_master_write(cmd, data, 4, I2C_MASTER_NACK);
  131. i2c_master_stop(cmd);
  132. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  133. i2c_cmd_link_delete(cmd);
  134. if (ret != ESP_OK) {
  135. ESP_LOGW(TAG, "I2C write failed");
  136. }
  137. return ret;
  138. }
  139. /****************************************************************************************
  140. *
  141. */
  142. esp_err_t adac_write(int i2c_addr, uint8_t reg, uint8_t *data, size_t count) {
  143. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  144. i2c_master_start(cmd);
  145. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  146. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  147. i2c_master_write(cmd, data, count, I2C_MASTER_NACK);
  148. i2c_master_stop(cmd);
  149. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 200 / portTICK_RATE_MS);
  150. i2c_cmd_link_delete(cmd);
  151. if (ret != ESP_OK) {
  152. ESP_LOGW(TAG, "I2C write failed");
  153. }
  154. return ret;
  155. }