es8311.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * ESPRESSIF MIT License
  3. *
  4. * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #include <string.h>
  25. #include "esp_log.h"
  26. #include "es8311.h"
  27. // #include "board.h"
  28. /* ES8311 address
  29. * 0x32:CE=1;0x30:CE=0
  30. */
  31. #define ES8311_ADDR 0x32
  32. #define ES7243_ADDR 0x26
  33. /*
  34. * to define the clock soure of MCLK
  35. */
  36. #define FROM_MCLK_PIN 0
  37. #define FROM_SCLK_PIN 1
  38. /*
  39. * to define work mode(master or slave)
  40. */
  41. #define MASTER_MODE 0
  42. #define SLAVE_MODE 1
  43. /*
  44. * to define serial digital audio format
  45. */
  46. #define I2S_FMT 0
  47. #define LEFT_JUSTIFIED_FMT 1
  48. #define DPS_PCM_A_FMT 2
  49. #define DPS_PCM_B_FMT 3
  50. /*
  51. * to define resolution of PCM interface
  52. */
  53. #define LENGTH_16BIT 0
  54. #define LENGTH_24BIT 1
  55. #define LENGTH_32BIT 2
  56. /*
  57. * codec private data
  58. */
  59. struct es8311_private {
  60. bool dmic_enable;
  61. bool mclkinv;
  62. bool sclkinv;
  63. uint8_t master_slave_mode;
  64. uint8_t pcm_format;
  65. uint8_t pcm_resolution;
  66. uint8_t mclk_src;
  67. };
  68. static struct es8311_private *es8311_priv;
  69. /*
  70. * Clock coefficient structer
  71. */
  72. struct _coeff_div {
  73. uint32_t mclk; /* mclk frequency */
  74. uint32_t rate; /* sample rate */
  75. uint8_t prediv; /* the pre divider with range from 1 to 8 */
  76. uint8_t premulti; /* the pre multiplier with x1, x2, x4 and x8 selection */
  77. uint8_t adcdiv; /* adcclk divider */
  78. uint8_t dacdiv; /* dacclk divider */
  79. uint8_t fsmode; /* double speed or single speed, =0, ss, =1, ds */
  80. uint8_t lrck_h; /* adclrck divider and daclrck divider */
  81. uint8_t lrck_l;
  82. uint8_t bclkdiv; /* sclk divider */
  83. uint8_t adcosr; /* adc osr */
  84. uint8_t dacosr; /* dac osr */
  85. };
  86. /* codec hifi mclk clock divider coefficients */
  87. static const struct _coeff_div coeff_div[] = {
  88. //mclk rate prediv mult adcdiv dacdiv fsmode lrch lrcl bckdiv osr
  89. /* 8k */
  90. {12288000, 8000 , 0x06, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  91. {18432000, 8000 , 0x03, 0x02, 0x03, 0x03, 0x00, 0x05, 0xff, 0x18, 0x10, 0x10},
  92. {16384000, 8000 , 0x08, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  93. {8192000 , 8000 , 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  94. {6144000 , 8000 , 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  95. {4096000 , 8000 , 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  96. {3072000 , 8000 , 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  97. {2048000 , 8000 , 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  98. {1536000 , 8000 , 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  99. {1024000 , 8000 , 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  100. /* 11.025k */
  101. {11289600, 11025, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  102. {5644800 , 11025, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  103. {2822400 , 11025, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  104. {1411200 , 11025, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  105. /* 12k */
  106. {12288000, 12000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  107. {6144000 , 12000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  108. {3072000 , 12000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  109. {1536000 , 12000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  110. /* 16k */
  111. {12288000, 16000, 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  112. {18432000, 16000, 0x03, 0x02, 0x03, 0x03, 0x00, 0x02, 0xff, 0x0c, 0x10, 0x10},
  113. {16384000, 16000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  114. {8192000 , 16000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  115. {6144000 , 16000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  116. {4096000 , 16000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  117. {3072000 , 16000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  118. {2048000 , 16000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  119. {1536000 , 16000, 0x03, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  120. {1024000 , 16000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  121. /* 22.05k */
  122. {11289600, 22050, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  123. {5644800 , 22050, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  124. {2822400 , 22050, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  125. {1411200 , 22050, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  126. /* 24k */
  127. {12288000, 24000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  128. {18432000, 24000, 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  129. {6144000 , 24000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  130. {3072000 , 24000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  131. {1536000 , 24000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  132. /* 32k */
  133. {12288000, 32000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  134. {18432000, 32000, 0x03, 0x04, 0x03, 0x03, 0x00, 0x02, 0xff, 0x0c, 0x10, 0x10},
  135. {16384000, 32000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  136. {8192000 , 32000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  137. {6144000 , 32000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  138. {4096000 , 32000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  139. {3072000 , 32000, 0x03, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  140. {2048000 , 32000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  141. {1536000 , 32000, 0x03, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
  142. {1024000 , 32000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  143. /* 44.1k */
  144. {11289600, 44100, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  145. {5644800 , 44100, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  146. {2822400 , 44100, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  147. {1411200 , 44100, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  148. /* 48k */
  149. {12288000, 48000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  150. {18432000, 48000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  151. {6144000 , 48000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  152. {3072000 , 48000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  153. {1536000 , 48000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  154. /* 64k */
  155. {12288000, 64000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  156. {18432000, 64000, 0x03, 0x04, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10, 0x10},
  157. {16384000, 64000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  158. {8192000 , 64000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  159. {6144000 , 64000, 0x01, 0x04, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10, 0x10},
  160. {4096000 , 64000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  161. {3072000 , 64000, 0x01, 0x08, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10, 0x10},
  162. {2048000 , 64000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  163. {1536000 , 64000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0xbf, 0x03, 0x18, 0x18},
  164. {1024000 , 64000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
  165. /* 88.2k */
  166. {11289600, 88200, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  167. {5644800 , 88200, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  168. {2822400 , 88200, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  169. {1411200 , 88200, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
  170. /* 96k */
  171. {12288000, 96000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  172. {18432000, 96000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  173. {6144000 , 96000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  174. {3072000 , 96000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  175. {1536000 , 96000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
  176. };
  177. static char *TAG = "DRV8311";
  178. #define ES_ASSERT(a, format, b, ...) \
  179. if ((a) != 0) { \
  180. ESP_LOGE(TAG, format, ##__VA_ARGS__); \
  181. return b;\
  182. }
  183. static int Es8311WriteReg(uint8_t regAdd, uint8_t data)
  184. {
  185. int res = 0;
  186. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  187. res |= i2c_master_start(cmd);
  188. res |= i2c_master_write_byte(cmd, ES8311_ADDR, 1 /*ACK_CHECK_EN*/);
  189. res |= i2c_master_write_byte(cmd, regAdd, 1 /*ACK_CHECK_EN*/);
  190. res |= i2c_master_write_byte(cmd, data, 1 /*ACK_CHECK_EN*/);
  191. res |= i2c_master_stop(cmd);
  192. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  193. i2c_cmd_link_delete(cmd);
  194. ES_ASSERT(res, "Es8311 Write Reg error", -1);
  195. return res;
  196. }
  197. int Es8311ReadReg(uint8_t regAdd)
  198. {
  199. uint8_t data;
  200. int res = 0;
  201. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  202. res |= i2c_master_start(cmd);
  203. res |= i2c_master_write_byte(cmd, ES8311_ADDR, 1 /*ACK_CHECK_EN*/);
  204. res |= i2c_master_write_byte(cmd, regAdd, 1 /*ACK_CHECK_EN*/);
  205. res |= i2c_master_stop(cmd);
  206. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  207. i2c_cmd_link_delete(cmd);
  208. cmd = i2c_cmd_link_create();
  209. res |= i2c_master_start(cmd);
  210. res |= i2c_master_write_byte(cmd, ES8311_ADDR | 0x01, 1 /*ACK_CHECK_EN*/);
  211. res |= i2c_master_read_byte(cmd, &data, 0x01 /*NACK_VAL*/);
  212. res |= i2c_master_stop(cmd);
  213. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  214. i2c_cmd_link_delete(cmd);
  215. ES_ASSERT(res, "Es8311 Read Reg error", -1);
  216. return (int)data;
  217. }
  218. static int Es7243WriteReg(uint8_t regAdd, uint8_t data)
  219. {
  220. int res = 0;
  221. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  222. res |= i2c_master_start(cmd);
  223. res |= i2c_master_write_byte(cmd, ES7243_ADDR, 1 /*ACK_CHECK_EN*/);
  224. res |= i2c_master_write_byte(cmd, regAdd, 1 /*ACK_CHECK_EN*/);
  225. res |= i2c_master_write_byte(cmd, data, 1 /*ACK_CHECK_EN*/);
  226. res |= i2c_master_stop(cmd);
  227. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  228. i2c_cmd_link_delete(cmd);
  229. ES_ASSERT(res, "Es7243 Write Reg error", -1);
  230. return res;
  231. }
  232. int Es7243ReadReg(uint8_t regAdd)
  233. {
  234. uint8_t data;
  235. int res = 0;
  236. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  237. res |= i2c_master_start(cmd);
  238. res |= i2c_master_write_byte(cmd, ES7243_ADDR, 1 /*ACK_CHECK_EN*/);
  239. res |= i2c_master_write_byte(cmd, regAdd, 1 /*ACK_CHECK_EN*/);
  240. res |= i2c_master_stop(cmd);
  241. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  242. i2c_cmd_link_delete(cmd);
  243. cmd = i2c_cmd_link_create();
  244. res |= i2c_master_start(cmd);
  245. res |= i2c_master_write_byte(cmd, ES7243_ADDR | 0x01, 1 /*ACK_CHECK_EN*/);
  246. res |= i2c_master_read_byte(cmd, &data, 0x01 /*NACK_VAL*/);
  247. res |= i2c_master_stop(cmd);
  248. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  249. i2c_cmd_link_delete(cmd);
  250. ES_ASSERT(res, "Es7243 Read Reg error", -1);
  251. return (int)data;
  252. }
  253. esp_err_t Es7243Init(void)
  254. {
  255. esp_err_t ret = ESP_OK;
  256. ret |= Es7243WriteReg(0x00, 0x01);
  257. ret |= Es7243WriteReg(0x06, 0x00);
  258. ret |= Es7243WriteReg(0x05, 0x1B);
  259. ret |= Es7243WriteReg(0x01, 0x0C);
  260. ret |= Es7243WriteReg(0x08, 0x43);
  261. ret |= Es7243WriteReg(0x05, 0x13);
  262. if (ret) {
  263. ESP_LOGE(TAG, "Es7243 initialize failed!");
  264. return ESP_FAIL;
  265. }
  266. return ret;
  267. }
  268. static int I2cInit(i2c_config_t *conf, int i2cMasterPort)
  269. {
  270. int res;
  271. res = i2c_param_config(i2cMasterPort, conf);
  272. res |= i2c_driver_install(i2cMasterPort, conf->mode, 0, 0, 0);
  273. ES_ASSERT(res, "I2cInit error", -1);
  274. return res;
  275. }
  276. /*
  277. * look for the coefficient in coeff_div[] table
  278. */
  279. static int get_coeff(uint32_t mclk, uint32_t rate)
  280. {
  281. for (int i = 0; i < (sizeof(coeff_div) / sizeof(coeff_div[0])); i++) {
  282. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  283. return i;
  284. }
  285. return -1;
  286. }
  287. /*
  288. * set es8311 clock parameter and PCM/I2S interface
  289. */
  290. static void es8311_pcm_hw_params(uint32_t mclk, uint32_t lrck)
  291. {
  292. int coeff;
  293. uint8_t regv, datmp;
  294. ESP_LOGI(TAG, "Enter into es8311_pcm_hw_params()\n");
  295. coeff = get_coeff(mclk, lrck);
  296. if (coeff < 0) {
  297. ESP_LOGE(TAG, "Unable to configure sample rate %dHz with %dHz MCLK\n", lrck, mclk);
  298. return;
  299. }
  300. /*
  301. * set clock parammeters
  302. */
  303. if (coeff >= 0) {
  304. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG02) & 0x07;
  305. regv |= (coeff_div[coeff].prediv - 1) << 5;
  306. datmp = 0;
  307. switch (coeff_div[coeff].premulti) {
  308. case 1:
  309. datmp = 0;
  310. break;
  311. case 2:
  312. datmp = 1;
  313. break;
  314. case 4:
  315. datmp = 2;
  316. break;
  317. case 8:
  318. datmp = 3;
  319. break;
  320. default:
  321. break;
  322. }
  323. #if CONFIG_ESP32_KORVO_V1_1_BOARD
  324. datmp = 3;
  325. #endif
  326. regv |= (datmp) << 3;
  327. Es8311WriteReg(ES8311_CLK_MANAGER_REG02, regv);
  328. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG05) & 0x00;
  329. regv |= (coeff_div[coeff].adcdiv - 1) << 4;
  330. regv |= (coeff_div[coeff].dacdiv - 1) << 0;
  331. Es8311WriteReg(ES8311_CLK_MANAGER_REG05, regv);
  332. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG03) & 0x80;
  333. regv |= coeff_div[coeff].fsmode << 6;
  334. regv |= coeff_div[coeff].adcosr << 0;
  335. Es8311WriteReg(ES8311_CLK_MANAGER_REG03, regv);
  336. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG04) & 0x80;
  337. regv |= coeff_div[coeff].dacosr << 0;
  338. Es8311WriteReg(ES8311_CLK_MANAGER_REG04, regv);
  339. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG07) & 0xC0;
  340. regv |= coeff_div[coeff].lrck_h << 0;
  341. Es8311WriteReg(ES8311_CLK_MANAGER_REG07, regv);
  342. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG08) & 0x00;
  343. regv |= coeff_div[coeff].lrck_l << 0;
  344. Es8311WriteReg(ES8311_CLK_MANAGER_REG08, regv);
  345. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG06) & 0xE0;
  346. if (coeff_div[coeff].bclkdiv < 19) {
  347. regv |= (coeff_div[coeff].bclkdiv - 1) << 0;
  348. } else {
  349. regv |= (coeff_div[coeff].bclkdiv) << 0;
  350. }
  351. Es8311WriteReg(ES8311_CLK_MANAGER_REG06, regv);
  352. }
  353. }
  354. /*
  355. * set data and clock in tri-state mode
  356. * if tristate = 0, tri-state is disabled for normal mode
  357. * if tristate = 1, tri-state is enabled
  358. */
  359. // static void es8311_set_tristate(int tristate)
  360. // {
  361. // uint8_t regv;
  362. // ESP_LOGI(TAG, "Enter into es8311_set_tristate(), tristate = %d\n", tristate);
  363. // regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG07) & 0xcf;
  364. // if (tristate) {
  365. // Es8311WriteReg(ES8311_CLK_MANAGER_REG07, regv | 0x30);
  366. // } else {
  367. // Es8311WriteReg(ES8311_CLK_MANAGER_REG07, regv);
  368. // }
  369. // }
  370. /*
  371. * set es8311 dac mute or not
  372. * if mute = 0, dac un-mute
  373. * if mute = 1, dac mute
  374. */
  375. static void es8311_mute(int mute)
  376. {
  377. uint8_t regv;
  378. ESP_LOGI(TAG, "Enter into es8311_mute(), mute = %d\n", mute);
  379. regv = Es8311ReadReg(ES8311_DAC_REG31) & 0x9f;
  380. if (mute) {
  381. Es8311WriteReg(ES8311_SYSTEM_REG12, 0x02);
  382. Es8311WriteReg(ES8311_DAC_REG31, regv | 0x60);
  383. Es8311WriteReg(ES8311_DAC_REG32, 0x00);
  384. Es8311WriteReg(ES8311_DAC_REG37, 0x08);
  385. } else {
  386. Es8311WriteReg(ES8311_DAC_REG31, regv);
  387. Es8311WriteReg(ES8311_SYSTEM_REG12, 0x00);
  388. }
  389. }
  390. /*
  391. * set es8311 into suspend mode
  392. */
  393. // static void es8311_suspend(void)
  394. // {
  395. // ESP_LOGI(TAG, "Enter into es8311_suspend()\n");
  396. // Es8311WriteReg(ES8311_DAC_REG32, 0x00);
  397. // Es8311WriteReg(ES8311_ADC_REG17, 0x00);
  398. // Es8311WriteReg(ES8311_SYSTEM_REG0E, 0xFF);
  399. // Es8311WriteReg(ES8311_SYSTEM_REG12, 0x02);
  400. // Es8311WriteReg(ES8311_SYSTEM_REG14, 0x00);
  401. // Es8311WriteReg(ES8311_SYSTEM_REG0D, 0xFA);
  402. // Es8311WriteReg(ES8311_ADC_REG15, 0x00);
  403. // Es8311WriteReg(ES8311_DAC_REG37, 0x08);
  404. // Es8311WriteReg(ES8311_RESET_REG00, 0x00);
  405. // Es8311WriteReg(ES8311_RESET_REG00, 0x1F);
  406. // Es8311WriteReg(ES8311_CLK_MANAGER_REG01, 0x30);
  407. // Es8311WriteReg(ES8311_CLK_MANAGER_REG01, 0x00);
  408. // Es8311WriteReg(ES8311_GP_REG45, 0x01);
  409. // }
  410. /*
  411. * initialize es8311 codec
  412. */
  413. static void es8311_init(uint32_t mclk_freq, uint32_t lrck_freq)
  414. {
  415. int regv;
  416. Es8311WriteReg(ES8311_GP_REG45, 0x00);
  417. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, 0x30);
  418. Es8311WriteReg(ES8311_CLK_MANAGER_REG02, 0x00);
  419. Es8311WriteReg(ES8311_CLK_MANAGER_REG03, 0x10);
  420. Es8311WriteReg(ES8311_ADC_REG16, 0x24);
  421. Es8311WriteReg(ES8311_CLK_MANAGER_REG04, 0x10);
  422. Es8311WriteReg(ES8311_CLK_MANAGER_REG05, 0x00);
  423. Es8311WriteReg(ES8311_SYSTEM_REG0B, 0x00);
  424. Es8311WriteReg(ES8311_SYSTEM_REG0C, 0x00);
  425. Es8311WriteReg(ES8311_SYSTEM_REG10, 0x1F);
  426. Es8311WriteReg(ES8311_SYSTEM_REG11, 0x7F);
  427. Es8311WriteReg(ES8311_RESET_REG00, 0x80);
  428. /*
  429. * Set Codec into Master or Slave mode
  430. */
  431. regv = Es8311ReadReg(ES8311_RESET_REG00);
  432. /* set master/slave audio interface */
  433. switch (es8311_priv->master_slave_mode) {
  434. case MASTER_MODE: /* MASTER MODE */
  435. ESP_LOGI(TAG, "ES8311 in Master mode\n");
  436. regv |= 0x40;
  437. break;
  438. case SLAVE_MODE: /* SLAVE MODE */
  439. ESP_LOGI(TAG, "ES8311 in Slave mode\n");
  440. regv &= 0xBF;
  441. break;
  442. default:
  443. regv &= 0xBF;
  444. }
  445. Es8311WriteReg(ES8311_RESET_REG00, regv);
  446. Es8311WriteReg(ES8311_SYSTEM_REG0D, 0x01);
  447. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, 0x3F);
  448. /*
  449. * select clock source for internal mclk
  450. */
  451. switch (es8311_priv->mclk_src) {
  452. case FROM_MCLK_PIN:
  453. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG01);
  454. regv &= 0x7F;
  455. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, regv);
  456. break;
  457. case FROM_SCLK_PIN:
  458. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG01);
  459. regv |= 0x80;
  460. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, regv);
  461. break;
  462. default:
  463. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG01);
  464. regv &= 0x7F;
  465. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, regv);
  466. break;
  467. }
  468. es8311_pcm_hw_params(lrck_freq * 256, lrck_freq);
  469. /*
  470. * mclk inverted or not
  471. */
  472. if (es8311_priv->mclkinv == true) {
  473. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG01);
  474. regv |= 0x40;
  475. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, regv);
  476. } else {
  477. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG01);
  478. regv &= ~(0x40);
  479. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, regv);
  480. }
  481. /*
  482. * sclk inverted or not
  483. */
  484. if (es8311_priv->sclkinv == true) {
  485. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG06);
  486. regv |= 0x20;
  487. Es8311WriteReg(ES8311_CLK_MANAGER_REG06, regv);
  488. } else {
  489. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG06);
  490. regv &= ~(0x20);
  491. Es8311WriteReg(ES8311_CLK_MANAGER_REG06, regv);
  492. }
  493. Es8311WriteReg(ES8311_SYSTEM_REG14, 0x1A);
  494. /*
  495. * pdm dmic enable or disable
  496. */
  497. if (es8311_priv->dmic_enable == true) {
  498. regv = Es8311ReadReg(ES8311_SYSTEM_REG14);
  499. regv |= 0x40;
  500. Es8311WriteReg(ES8311_SYSTEM_REG14, regv);
  501. } else {
  502. regv = Es8311ReadReg(ES8311_SYSTEM_REG14);
  503. regv &= ~(0x40);
  504. Es8311WriteReg(ES8311_SYSTEM_REG14, regv);
  505. }
  506. Es8311WriteReg(ES8311_SYSTEM_REG13, 0x10);
  507. Es8311WriteReg(ES8311_SYSTEM_REG0E, 0x02);
  508. Es8311WriteReg(ES8311_ADC_REG15, 0x40);
  509. Es8311WriteReg(ES8311_ADC_REG1B, 0x0A);
  510. Es8311WriteReg(ES8311_ADC_REG1C, 0x6A);
  511. Es8311WriteReg(ES8311_DAC_REG37, 0x48);
  512. Es8311WriteReg(ES8311_GPIO_REG44, 0x08);
  513. Es8311WriteReg(ES8311_DAC_REG32, 0xBF);
  514. #ifdef CONFIG_USE_ES7243
  515. Es7243Init();
  516. #endif
  517. }
  518. /*
  519. * set codec private data and initialize codec
  520. */
  521. void es8311_Codec_Startup(uint32_t mclk_freq, uint32_t lrck_freq)
  522. {
  523. ESP_LOGI(TAG, "Enter into es8311_Codec_Startup()\n");
  524. es8311_priv->dmic_enable = false;
  525. es8311_priv->mclkinv = false;
  526. es8311_priv->sclkinv = false;
  527. es8311_priv->pcm_format = I2S_FMT;
  528. es8311_priv->pcm_resolution = LENGTH_16BIT;
  529. es8311_priv->master_slave_mode = SLAVE_MODE;
  530. #ifdef CONFIG_ESP32_KORVO_V1_1_BOARD
  531. es8311_priv->mclk_src = FROM_SCLK_PIN;
  532. #else
  533. es8311_priv->mclk_src = FROM_MCLK_PIN;
  534. #endif
  535. es8311_init(mclk_freq, lrck_freq);
  536. ESP_LOGI(TAG, "Exit es8311_Codec_Startup()\n");
  537. }
  538. // static int Es8311SetAdcDacVolume(int mode, int volume, int dot)
  539. // {
  540. // int res = 0;
  541. // if ( volume < -96 || volume > 0 ) {
  542. // ESP_LOGI(TAG, "Warning: volume < -96! or > 0!\n");
  543. // if (volume < -96) {
  544. // volume = -96;
  545. // } else {
  546. // volume = 0;
  547. // }
  548. // }
  549. // dot = (dot >= 5 ? 1 : 0);
  550. // return res;
  551. // }
  552. esp_err_t Es8311GetRef(bool flag)
  553. {
  554. esp_err_t ret = ESP_OK;
  555. uint8_t regv = 0;
  556. if (flag) {
  557. regv = Es8311ReadReg(ES8311_GPIO_REG44);
  558. regv |= 0x50;
  559. ret |= Es8311WriteReg(ES8311_GPIO_REG44, regv);
  560. } else {
  561. ret |= Es8311WriteReg(ES8311_GPIO_REG44, 0x08);
  562. }
  563. return ret;
  564. }
  565. int Es8311Init(Es8311Config *cfg)
  566. {
  567. es8311_priv = calloc(1, sizeof(struct es8311_private));
  568. I2cInit(&cfg->i2c_cfg, cfg->i2c_port_num); // ESP32 in master mode
  569. es8311_Codec_Startup(11289600, 44100);
  570. return 0;
  571. }
  572. void Es8311Uninit()
  573. {
  574. Es8311WriteReg(ES8311_RESET_REG00, 0x3f);
  575. free(es8311_priv);
  576. es8311_priv = NULL;
  577. }
  578. int Es8311ConfigFmt(ESCodecModule mode, ESCodecI2SFmt fmt)
  579. {
  580. int res = 0;
  581. uint8_t regAdc = 0, regDac = 0;
  582. if (mode == ES_MODULE_ADC || mode == ES_MODULE_ADC_DAC) {
  583. res |= Es8311WriteReg(ES8311_ADC_REG17, 0xBF);
  584. }
  585. if (mode == ES_MODULE_DAC || mode == ES_MODULE_ADC_DAC) {
  586. res |= Es8311WriteReg(ES8311_SYSTEM_REG12, 0x00);
  587. }
  588. regAdc = Es8311ReadReg(ES8311_SDPIN_REG09);
  589. regDac = Es8311ReadReg(ES8311_SDPOUT_REG0A);
  590. switch (fmt) {
  591. case ES_I2S_NORMAL:
  592. ESP_LOGI(TAG, "ES8311 in I2S Format");
  593. regAdc &= ~0x03;
  594. regDac &= ~0x03;
  595. break;
  596. case ES_I2S_LEFT:
  597. case ES_I2S_RIGHT:
  598. ESP_LOGI(TAG, "ES8311 in LJ Format");
  599. regAdc &= ~0x03;
  600. regAdc |= 0x01;
  601. regDac &= ~0x03;
  602. regDac |= 0x01;
  603. break;
  604. case ES_I2S_DSP:
  605. ESP_LOGI(TAG, "ES8311 in DSP Format");
  606. regAdc |= 0x03;
  607. regDac |= 0x03;
  608. break;
  609. default:
  610. ESP_LOGE(TAG, "Not Supported Format");
  611. break;
  612. }
  613. res |= Es8311WriteReg(ES8311_SDPIN_REG09, regAdc);
  614. res |= Es8311WriteReg(ES8311_SDPOUT_REG0A, regDac);
  615. return res;
  616. }
  617. int Es8311I2sConfigClock(ESCodecI2sClock cfg)
  618. {
  619. int res = 0;
  620. return res;
  621. }
  622. int Es8311SetBitsPerSample(ESCodecModule mode, BitsLength bitPerSample)
  623. {
  624. int res = 0;
  625. uint8_t reg = 0;
  626. int bits = (int)bitPerSample;
  627. if (mode == ES_MODULE_ADC || mode == ES_MODULE_ADC_DAC) {
  628. reg = Es8311ReadReg(ES8311_SDPIN_REG09);
  629. reg = reg & 0xe3;
  630. res |= Es8311WriteReg(ES8311_SDPIN_REG09, reg | (bits << 2));
  631. }
  632. if (mode == ES_MODULE_DAC || mode == ES_MODULE_ADC_DAC) {
  633. reg = Es8311ReadReg(ES8311_SDPOUT_REG0A);
  634. reg = reg & 0xe3;
  635. res |= Es8311WriteReg(ES8311_SDPOUT_REG0A, reg | (bits << 2));
  636. }
  637. return res;
  638. }
  639. int Es8311Start(ESCodecModule mode)
  640. {
  641. int res = 0;
  642. if (mode == ES_MODULE_ADC || mode == ES_MODULE_ADC_DAC) {
  643. res |= Es8311WriteReg(ES8311_ADC_REG17, 0xBF);
  644. }
  645. if (mode == ES_MODULE_DAC || mode == ES_MODULE_ADC_DAC) {
  646. res |= Es8311WriteReg(ES8311_SYSTEM_REG12, Es8311ReadReg(ES8311_SYSTEM_REG12) & 0xfd);
  647. }
  648. return res;
  649. }
  650. int Es8311Stop(ESCodecModule mode)
  651. {
  652. int res = 0;
  653. if (mode == ES_MODULE_ADC || mode == ES_MODULE_ADC_DAC) {
  654. res |= Es8311WriteReg(ES8311_ADC_REG17, 0x00);
  655. }
  656. if (mode == ES_MODULE_DAC || mode == ES_MODULE_ADC_DAC) {
  657. res |= Es8311WriteReg(ES8311_SYSTEM_REG12, Es8311ReadReg(ES8311_SYSTEM_REG12) | 0x02);
  658. }
  659. return res;
  660. }
  661. int Es8311SetVoiceVolume(int volume)
  662. {
  663. int res = 0;
  664. if (volume == 0) {
  665. volume = 1;
  666. }
  667. Es8311WriteReg(ES8311_DAC_REG32, volume);
  668. return res;
  669. }
  670. int Es8311GetVoiceVolume(int *volume)
  671. {
  672. int res = ESP_OK;
  673. int regv = Es8311ReadReg(ES8311_DAC_REG32);
  674. if (regv == ESP_FAIL) {
  675. *volume = 0;
  676. res = ESP_FAIL;
  677. } else {
  678. *volume = regv * 100 / 256;
  679. }
  680. ESP_LOGI(TAG, "GET: res:%d, volume:%d\n", regv, *volume);
  681. return res;
  682. }
  683. int Es8311SetVoiceMute(int enable)
  684. {
  685. int res = 0;
  686. ESP_LOGI(TAG, "Es8311SetVoiceMute volume:%d\n", enable);
  687. es8311_mute(enable);
  688. return res;
  689. }
  690. int Es8311GetVoiceMute(int *mute)
  691. {
  692. int res = -1;
  693. uint8_t reg = 0;
  694. res = Es8311ReadReg(ES8311_DAC_REG31);
  695. if (res != ESP_FAIL) {
  696. reg = (res & 0x20) >> 5;
  697. }
  698. *mute = reg;
  699. return res;
  700. }
  701. int Es8311SetMicGain(MicGain gain)
  702. {
  703. int res = 0;
  704. uint8_t gain_n = Es8311ReadReg(ES8311_ADC_REG16) & 0x07;
  705. gain_n |= gain / 6;
  706. res = Es8311WriteReg(ES8311_ADC_REG16, gain_n); // MIC gain scale
  707. return res;
  708. }
  709. int Es8311ConfigAdcInput(AdcInput input)
  710. {
  711. int res = 0;
  712. return res;
  713. }
  714. int Es8311SetAdcVolume(uint8_t adc_vol)
  715. {
  716. int res = 0;
  717. res = Es8311WriteReg(ES8311_ADC_REG17, adc_vol); // MIC ADC Volume
  718. return res;
  719. }
  720. int ES8311WriteReg(uint8_t regAdd, uint8_t data)
  721. {
  722. return Es8311WriteReg(regAdd, data);
  723. }
  724. void Es8311ReadAll()
  725. {
  726. for (int i = 0; i < 0x4A; i++) {
  727. uint8_t reg = Es8311ReadReg(i);
  728. // ets_printf("REG:%02x, %02x\n", reg, i);
  729. }
  730. }