adac_core.c 5.1 KB

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