adac_core.c 4.5 KB

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