adac_core.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. i2c_port_t i2c_port = -1;
  24. /****************************************************************************************
  25. * init
  26. */
  27. int adac_init(sys_dac_config *config) {
  28. int i2c_addr = 0;
  29. // configure i2c
  30. i2c_config_t i2c_config = {
  31. .mode = I2C_MODE_MASTER,
  32. .sda_io_num = -1,
  33. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  34. .scl_io_num = -1,
  35. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  36. .master.clk_speed = 250000,
  37. };
  38. i2c_addr = config->addr;
  39. if(config->has_i2c){
  40. i2c_config.sda_io_num = config->i2c.sda;
  41. i2c_config.scl_io_num = config->i2c.scl;
  42. i2c_config.master.clk_speed = config->i2c.speed>0?config->i2c.speed:i2c_config.master.clk_speed;
  43. }
  44. if (i2c_config.sda_io_num == -1 || i2c_config.scl_io_num == -1) {
  45. ESP_LOGW(TAG, "DAC does not use i2c");
  46. return i2c_addr;
  47. }
  48. i2c_port = config->i2c.port-sys_i2c_port_PORT0;
  49. ESP_LOGI(TAG, "DAC uses I2C port:%s, sda:%d, scl:%d", sys_i2c_port_name(config->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. uint8_t data[] = { i2c_addr << 1, reg,
  123. val >> 8, val & 0xff };
  124. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  125. i2c_master_start(cmd);
  126. i2c_master_write(cmd, data, 4, I2C_MASTER_NACK);
  127. i2c_master_stop(cmd);
  128. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  129. i2c_cmd_link_delete(cmd);
  130. if (ret != ESP_OK) {
  131. ESP_LOGW(TAG, "I2C write failed");
  132. }
  133. return ret;
  134. }
  135. /****************************************************************************************
  136. *
  137. */
  138. esp_err_t adac_write(int i2c_addr, uint8_t reg, uint8_t *data, size_t count) {
  139. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  140. i2c_master_start(cmd);
  141. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  142. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  143. i2c_master_write(cmd, data, count, I2C_MASTER_NACK);
  144. i2c_master_stop(cmd);
  145. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 200 / portTICK_RATE_MS);
  146. i2c_cmd_link_delete(cmd);
  147. if (ret != ESP_OK) {
  148. ESP_LOGW(TAG, "I2C write failed");
  149. }
  150. return ret;
  151. }