es8311.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 "es8311.h"
  25. #include <string.h>
  26. #include "esp_log.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,
  91. 0x10},
  92. {18432000, 8000, 0x03, 0x02, 0x03, 0x03, 0x00, 0x05, 0xff, 0x18, 0x10,
  93. 0x10},
  94. {16384000, 8000, 0x08, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  95. 0x10},
  96. {8192000, 8000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  97. {6144000, 8000, 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  98. {4096000, 8000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  99. {3072000, 8000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  100. {2048000, 8000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  101. {1536000, 8000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  102. {1024000, 8000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  103. /* 11.025k */
  104. {11289600, 11025, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  105. 0x10},
  106. {5644800, 11025, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  107. 0x10},
  108. {2822400, 11025, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  109. 0x10},
  110. {1411200, 11025, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  111. 0x10},
  112. /* 12k */
  113. {12288000, 12000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  114. 0x10},
  115. {6144000, 12000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  116. 0x10},
  117. {3072000, 12000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  118. 0x10},
  119. {1536000, 12000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  120. 0x10},
  121. /* 16k */
  122. {12288000, 16000, 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  123. 0x10},
  124. {18432000, 16000, 0x03, 0x02, 0x03, 0x03, 0x00, 0x02, 0xff, 0x0c, 0x10,
  125. 0x10},
  126. {16384000, 16000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  127. 0x10},
  128. {8192000, 16000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  129. 0x10},
  130. {6144000, 16000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  131. 0x10},
  132. {4096000, 16000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  133. 0x10},
  134. {3072000, 16000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  135. 0x10},
  136. {2048000, 16000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  137. 0x10},
  138. {1536000, 16000, 0x03, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  139. 0x10},
  140. {1024000, 16000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  141. 0x10},
  142. /* 22.05k */
  143. {11289600, 22050, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  144. 0x10},
  145. {5644800, 22050, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  146. 0x10},
  147. {2822400, 22050, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  148. 0x10},
  149. {1411200, 22050, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  150. 0x10},
  151. /* 24k */
  152. {12288000, 24000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  153. 0x10},
  154. {18432000, 24000, 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  155. 0x10},
  156. {6144000, 24000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  157. 0x10},
  158. {3072000, 24000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  159. 0x10},
  160. {1536000, 24000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  161. 0x10},
  162. /* 32k */
  163. {12288000, 32000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  164. 0x10},
  165. {18432000, 32000, 0x03, 0x04, 0x03, 0x03, 0x00, 0x02, 0xff, 0x0c, 0x10,
  166. 0x10},
  167. {16384000, 32000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  168. 0x10},
  169. {8192000, 32000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  170. 0x10},
  171. {6144000, 32000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  172. 0x10},
  173. {4096000, 32000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  174. 0x10},
  175. {3072000, 32000, 0x03, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  176. 0x10},
  177. {2048000, 32000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  178. 0x10},
  179. {1536000, 32000, 0x03, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10,
  180. 0x10},
  181. {1024000, 32000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  182. 0x10},
  183. /* 44.1k */
  184. {11289600, 44100, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  185. 0x10},
  186. {5644800, 44100, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  187. 0x10},
  188. {2822400, 44100, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  189. 0x10},
  190. {1411200, 44100, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  191. 0x10},
  192. /* 48k */
  193. {12288000, 48000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  194. 0x10},
  195. {18432000, 48000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  196. 0x10},
  197. {6144000, 48000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  198. 0x10},
  199. {3072000, 48000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  200. 0x10},
  201. {1536000, 48000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  202. 0x10},
  203. /* 64k */
  204. {12288000, 64000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  205. 0x10},
  206. {18432000, 64000, 0x03, 0x04, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10,
  207. 0x10},
  208. {16384000, 64000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  209. 0x10},
  210. {8192000, 64000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  211. 0x10},
  212. {6144000, 64000, 0x01, 0x04, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10,
  213. 0x10},
  214. {4096000, 64000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  215. 0x10},
  216. {3072000, 64000, 0x01, 0x08, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10,
  217. 0x10},
  218. {2048000, 64000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  219. 0x10},
  220. {1536000, 64000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0xbf, 0x03, 0x18,
  221. 0x18},
  222. {1024000, 64000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10,
  223. 0x10},
  224. /* 88.2k */
  225. {11289600, 88200, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  226. 0x10},
  227. {5644800, 88200, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  228. 0x10},
  229. {2822400, 88200, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  230. 0x10},
  231. {1411200, 88200, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10,
  232. 0x10},
  233. /* 96k */
  234. {12288000, 96000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  235. 0x10},
  236. {18432000, 96000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  237. 0x10},
  238. {6144000, 96000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  239. 0x10},
  240. {3072000, 96000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10,
  241. 0x10},
  242. {1536000, 96000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10,
  243. 0x10},
  244. };
  245. static char* TAG = "DRV8311";
  246. #define ES_ASSERT(a, format, b, ...) \
  247. if ((a) != 0) { \
  248. ESP_LOGE(TAG, format, ##__VA_ARGS__); \
  249. return b; \
  250. }
  251. static int Es8311WriteReg(uint8_t regAdd, uint8_t data) {
  252. int res = 0;
  253. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  254. res |= i2c_master_start(cmd);
  255. res |= i2c_master_write_byte(cmd, ES8311_ADDR, 1 /*ACK_CHECK_EN*/);
  256. res |= i2c_master_write_byte(cmd, regAdd, 1 /*ACK_CHECK_EN*/);
  257. res |= i2c_master_write_byte(cmd, data, 1 /*ACK_CHECK_EN*/);
  258. res |= i2c_master_stop(cmd);
  259. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  260. i2c_cmd_link_delete(cmd);
  261. ES_ASSERT(res, "Es8311 Write Reg error", -1);
  262. return res;
  263. }
  264. int Es8311ReadReg(uint8_t regAdd) {
  265. uint8_t data;
  266. int res = 0;
  267. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  268. res |= i2c_master_start(cmd);
  269. res |= i2c_master_write_byte(cmd, ES8311_ADDR, 1 /*ACK_CHECK_EN*/);
  270. res |= i2c_master_write_byte(cmd, regAdd, 1 /*ACK_CHECK_EN*/);
  271. res |= i2c_master_stop(cmd);
  272. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  273. i2c_cmd_link_delete(cmd);
  274. cmd = i2c_cmd_link_create();
  275. res |= i2c_master_start(cmd);
  276. res |= i2c_master_write_byte(cmd, ES8311_ADDR | 0x01, 1 /*ACK_CHECK_EN*/);
  277. res |= i2c_master_read_byte(cmd, &data, 0x01 /*NACK_VAL*/);
  278. res |= i2c_master_stop(cmd);
  279. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  280. i2c_cmd_link_delete(cmd);
  281. ES_ASSERT(res, "Es8311 Read Reg error", -1);
  282. return (int)data;
  283. }
  284. static int Es7243WriteReg(uint8_t regAdd, uint8_t data) {
  285. int res = 0;
  286. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  287. res |= i2c_master_start(cmd);
  288. res |= i2c_master_write_byte(cmd, ES7243_ADDR, 1 /*ACK_CHECK_EN*/);
  289. res |= i2c_master_write_byte(cmd, regAdd, 1 /*ACK_CHECK_EN*/);
  290. res |= i2c_master_write_byte(cmd, data, 1 /*ACK_CHECK_EN*/);
  291. res |= i2c_master_stop(cmd);
  292. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  293. i2c_cmd_link_delete(cmd);
  294. ES_ASSERT(res, "Es7243 Write Reg error", -1);
  295. return res;
  296. }
  297. int Es7243ReadReg(uint8_t regAdd) {
  298. uint8_t data;
  299. int res = 0;
  300. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  301. res |= i2c_master_start(cmd);
  302. res |= i2c_master_write_byte(cmd, ES7243_ADDR, 1 /*ACK_CHECK_EN*/);
  303. res |= i2c_master_write_byte(cmd, regAdd, 1 /*ACK_CHECK_EN*/);
  304. res |= i2c_master_stop(cmd);
  305. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  306. i2c_cmd_link_delete(cmd);
  307. cmd = i2c_cmd_link_create();
  308. res |= i2c_master_start(cmd);
  309. res |= i2c_master_write_byte(cmd, ES7243_ADDR | 0x01, 1 /*ACK_CHECK_EN*/);
  310. res |= i2c_master_read_byte(cmd, &data, 0x01 /*NACK_VAL*/);
  311. res |= i2c_master_stop(cmd);
  312. res |= i2c_master_cmd_begin(0, cmd, 1000 / portTICK_PERIOD_MS);
  313. i2c_cmd_link_delete(cmd);
  314. ES_ASSERT(res, "Es7243 Read Reg error", -1);
  315. return (int)data;
  316. }
  317. esp_err_t Es7243Init(void) {
  318. esp_err_t ret = ESP_OK;
  319. ret |= Es7243WriteReg(0x00, 0x01);
  320. ret |= Es7243WriteReg(0x06, 0x00);
  321. ret |= Es7243WriteReg(0x05, 0x1B);
  322. ret |= Es7243WriteReg(0x01, 0x0C);
  323. ret |= Es7243WriteReg(0x08, 0x43);
  324. ret |= Es7243WriteReg(0x05, 0x13);
  325. if (ret) {
  326. ESP_LOGE(TAG, "Es7243 initialize failed!");
  327. return ESP_FAIL;
  328. }
  329. return ret;
  330. }
  331. static int I2cInit(i2c_config_t* conf, int i2cMasterPort) {
  332. int res;
  333. res = i2c_param_config(i2cMasterPort, conf);
  334. res |= i2c_driver_install(i2cMasterPort, conf->mode, 0, 0, 0);
  335. ES_ASSERT(res, "I2cInit error", -1);
  336. return res;
  337. }
  338. /*
  339. * look for the coefficient in coeff_div[] table
  340. */
  341. static int get_coeff(uint32_t mclk, uint32_t rate) {
  342. for (int i = 0; i < (sizeof(coeff_div) / sizeof(coeff_div[0])); i++) {
  343. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  344. return i;
  345. }
  346. return -1;
  347. }
  348. /*
  349. * set es8311 clock parameter and PCM/I2S interface
  350. */
  351. static void es8311_pcm_hw_params(uint32_t mclk, uint32_t lrck) {
  352. int coeff;
  353. uint8_t regv, datmp;
  354. ESP_LOGI(TAG, "Enter into es8311_pcm_hw_params()\n");
  355. coeff = get_coeff(mclk, lrck);
  356. if (coeff < 0) {
  357. ESP_LOGE(TAG, "Unable to configure sample rate %dHz with %dHz MCLK\n", lrck,
  358. mclk);
  359. return;
  360. }
  361. /*
  362. * set clock parammeters
  363. */
  364. if (coeff >= 0) {
  365. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG02) & 0x07;
  366. regv |= (coeff_div[coeff].prediv - 1) << 5;
  367. datmp = 0;
  368. switch (coeff_div[coeff].premulti) {
  369. case 1:
  370. datmp = 0;
  371. break;
  372. case 2:
  373. datmp = 1;
  374. break;
  375. case 4:
  376. datmp = 2;
  377. break;
  378. case 8:
  379. datmp = 3;
  380. break;
  381. default:
  382. break;
  383. }
  384. #if CONFIG_ESP32_KORVO_V1_1_BOARD
  385. datmp = 3;
  386. #endif
  387. regv |= (datmp) << 3;
  388. Es8311WriteReg(ES8311_CLK_MANAGER_REG02, regv);
  389. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG05) & 0x00;
  390. regv |= (coeff_div[coeff].adcdiv - 1) << 4;
  391. regv |= (coeff_div[coeff].dacdiv - 1) << 0;
  392. Es8311WriteReg(ES8311_CLK_MANAGER_REG05, regv);
  393. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG03) & 0x80;
  394. regv |= coeff_div[coeff].fsmode << 6;
  395. regv |= coeff_div[coeff].adcosr << 0;
  396. Es8311WriteReg(ES8311_CLK_MANAGER_REG03, regv);
  397. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG04) & 0x80;
  398. regv |= coeff_div[coeff].dacosr << 0;
  399. Es8311WriteReg(ES8311_CLK_MANAGER_REG04, regv);
  400. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG07) & 0xC0;
  401. regv |= coeff_div[coeff].lrck_h << 0;
  402. Es8311WriteReg(ES8311_CLK_MANAGER_REG07, regv);
  403. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG08) & 0x00;
  404. regv |= coeff_div[coeff].lrck_l << 0;
  405. Es8311WriteReg(ES8311_CLK_MANAGER_REG08, regv);
  406. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG06) & 0xE0;
  407. if (coeff_div[coeff].bclkdiv < 19) {
  408. regv |= (coeff_div[coeff].bclkdiv - 1) << 0;
  409. } else {
  410. regv |= (coeff_div[coeff].bclkdiv) << 0;
  411. }
  412. Es8311WriteReg(ES8311_CLK_MANAGER_REG06, regv);
  413. }
  414. }
  415. /*
  416. * set data and clock in tri-state mode
  417. * if tristate = 0, tri-state is disabled for normal mode
  418. * if tristate = 1, tri-state is enabled
  419. */
  420. // static void es8311_set_tristate(int tristate)
  421. // {
  422. // uint8_t regv;
  423. // ESP_LOGI(TAG, "Enter into es8311_set_tristate(), tristate = %d\n", tristate);
  424. // regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG07) & 0xcf;
  425. // if (tristate) {
  426. // Es8311WriteReg(ES8311_CLK_MANAGER_REG07, regv | 0x30);
  427. // } else {
  428. // Es8311WriteReg(ES8311_CLK_MANAGER_REG07, regv);
  429. // }
  430. // }
  431. /*
  432. * set es8311 dac mute or not
  433. * if mute = 0, dac un-mute
  434. * if mute = 1, dac mute
  435. */
  436. static void es8311_mute(int mute) {
  437. uint8_t regv;
  438. ESP_LOGI(TAG, "Enter into es8311_mute(), mute = %d\n", mute);
  439. regv = Es8311ReadReg(ES8311_DAC_REG31) & 0x9f;
  440. if (mute) {
  441. Es8311WriteReg(ES8311_SYSTEM_REG12, 0x02);
  442. Es8311WriteReg(ES8311_DAC_REG31, regv | 0x60);
  443. Es8311WriteReg(ES8311_DAC_REG32, 0x00);
  444. Es8311WriteReg(ES8311_DAC_REG37, 0x08);
  445. } else {
  446. Es8311WriteReg(ES8311_DAC_REG31, regv);
  447. Es8311WriteReg(ES8311_SYSTEM_REG12, 0x00);
  448. }
  449. }
  450. /*
  451. * set es8311 into suspend mode
  452. */
  453. // static void es8311_suspend(void)
  454. // {
  455. // ESP_LOGI(TAG, "Enter into es8311_suspend()\n");
  456. // Es8311WriteReg(ES8311_DAC_REG32, 0x00);
  457. // Es8311WriteReg(ES8311_ADC_REG17, 0x00);
  458. // Es8311WriteReg(ES8311_SYSTEM_REG0E, 0xFF);
  459. // Es8311WriteReg(ES8311_SYSTEM_REG12, 0x02);
  460. // Es8311WriteReg(ES8311_SYSTEM_REG14, 0x00);
  461. // Es8311WriteReg(ES8311_SYSTEM_REG0D, 0xFA);
  462. // Es8311WriteReg(ES8311_ADC_REG15, 0x00);
  463. // Es8311WriteReg(ES8311_DAC_REG37, 0x08);
  464. // Es8311WriteReg(ES8311_RESET_REG00, 0x00);
  465. // Es8311WriteReg(ES8311_RESET_REG00, 0x1F);
  466. // Es8311WriteReg(ES8311_CLK_MANAGER_REG01, 0x30);
  467. // Es8311WriteReg(ES8311_CLK_MANAGER_REG01, 0x00);
  468. // Es8311WriteReg(ES8311_GP_REG45, 0x01);
  469. // }
  470. /*
  471. * initialize es8311 codec
  472. */
  473. static void es8311_init(uint32_t mclk_freq, uint32_t lrck_freq) {
  474. int regv;
  475. Es8311WriteReg(ES8311_GP_REG45, 0x00);
  476. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, 0x30);
  477. Es8311WriteReg(ES8311_CLK_MANAGER_REG02, 0x00);
  478. Es8311WriteReg(ES8311_CLK_MANAGER_REG03, 0x10);
  479. Es8311WriteReg(ES8311_ADC_REG16, 0x24);
  480. Es8311WriteReg(ES8311_CLK_MANAGER_REG04, 0x10);
  481. Es8311WriteReg(ES8311_CLK_MANAGER_REG05, 0x00);
  482. Es8311WriteReg(ES8311_SYSTEM_REG0B, 0x00);
  483. Es8311WriteReg(ES8311_SYSTEM_REG0C, 0x00);
  484. Es8311WriteReg(ES8311_SYSTEM_REG10, 0x1F);
  485. Es8311WriteReg(ES8311_SYSTEM_REG11, 0x7F);
  486. Es8311WriteReg(ES8311_RESET_REG00, 0x80);
  487. /*
  488. * Set Codec into Master or Slave mode
  489. */
  490. regv = Es8311ReadReg(ES8311_RESET_REG00);
  491. /* set master/slave audio interface */
  492. switch (es8311_priv->master_slave_mode) {
  493. case MASTER_MODE: /* MASTER MODE */
  494. ESP_LOGI(TAG, "ES8311 in Master mode\n");
  495. regv |= 0x40;
  496. break;
  497. case SLAVE_MODE: /* SLAVE MODE */
  498. ESP_LOGI(TAG, "ES8311 in Slave mode\n");
  499. regv &= 0xBF;
  500. break;
  501. default:
  502. regv &= 0xBF;
  503. }
  504. Es8311WriteReg(ES8311_RESET_REG00, regv);
  505. Es8311WriteReg(ES8311_SYSTEM_REG0D, 0x01);
  506. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, 0x3F);
  507. /*
  508. * select clock source for internal mclk
  509. */
  510. switch (es8311_priv->mclk_src) {
  511. case FROM_MCLK_PIN:
  512. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG01);
  513. regv &= 0x7F;
  514. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, regv);
  515. break;
  516. case FROM_SCLK_PIN:
  517. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG01);
  518. regv |= 0x80;
  519. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, regv);
  520. break;
  521. default:
  522. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG01);
  523. regv &= 0x7F;
  524. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, regv);
  525. break;
  526. }
  527. es8311_pcm_hw_params(lrck_freq * 256, lrck_freq);
  528. /*
  529. * mclk inverted or not
  530. */
  531. if (es8311_priv->mclkinv == true) {
  532. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG01);
  533. regv |= 0x40;
  534. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, regv);
  535. } else {
  536. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG01);
  537. regv &= ~(0x40);
  538. Es8311WriteReg(ES8311_CLK_MANAGER_REG01, regv);
  539. }
  540. /*
  541. * sclk inverted or not
  542. */
  543. if (es8311_priv->sclkinv == true) {
  544. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG06);
  545. regv |= 0x20;
  546. Es8311WriteReg(ES8311_CLK_MANAGER_REG06, regv);
  547. } else {
  548. regv = Es8311ReadReg(ES8311_CLK_MANAGER_REG06);
  549. regv &= ~(0x20);
  550. Es8311WriteReg(ES8311_CLK_MANAGER_REG06, regv);
  551. }
  552. Es8311WriteReg(ES8311_SYSTEM_REG14, 0x1A);
  553. /*
  554. * pdm dmic enable or disable
  555. */
  556. if (es8311_priv->dmic_enable == true) {
  557. regv = Es8311ReadReg(ES8311_SYSTEM_REG14);
  558. regv |= 0x40;
  559. Es8311WriteReg(ES8311_SYSTEM_REG14, regv);
  560. } else {
  561. regv = Es8311ReadReg(ES8311_SYSTEM_REG14);
  562. regv &= ~(0x40);
  563. Es8311WriteReg(ES8311_SYSTEM_REG14, regv);
  564. }
  565. Es8311WriteReg(ES8311_SYSTEM_REG13, 0x10);
  566. Es8311WriteReg(ES8311_SYSTEM_REG0E, 0x02);
  567. Es8311WriteReg(ES8311_ADC_REG15, 0x40);
  568. Es8311WriteReg(ES8311_ADC_REG1B, 0x0A);
  569. Es8311WriteReg(ES8311_ADC_REG1C, 0x6A);
  570. Es8311WriteReg(ES8311_DAC_REG37, 0x48);
  571. Es8311WriteReg(ES8311_GPIO_REG44, 0x08);
  572. Es8311WriteReg(ES8311_DAC_REG32, 0xBF);
  573. #ifdef CONFIG_USE_ES7243
  574. Es7243Init();
  575. #endif
  576. }
  577. /*
  578. * set codec private data and initialize codec
  579. */
  580. void es8311_Codec_Startup(uint32_t mclk_freq, uint32_t lrck_freq) {
  581. ESP_LOGI(TAG, "Enter into es8311_Codec_Startup()\n");
  582. es8311_priv->dmic_enable = false;
  583. es8311_priv->mclkinv = false;
  584. es8311_priv->sclkinv = false;
  585. es8311_priv->pcm_format = I2S_FMT;
  586. es8311_priv->pcm_resolution = LENGTH_16BIT;
  587. es8311_priv->master_slave_mode = SLAVE_MODE;
  588. #ifdef CONFIG_ESP32_KORVO_V1_1_BOARD
  589. es8311_priv->mclk_src = FROM_SCLK_PIN;
  590. #else
  591. es8311_priv->mclk_src = FROM_MCLK_PIN;
  592. #endif
  593. es8311_init(mclk_freq, lrck_freq);
  594. ESP_LOGI(TAG, "Exit es8311_Codec_Startup()\n");
  595. }
  596. // static int Es8311SetAdcDacVolume(int mode, int volume, int dot)
  597. // {
  598. // int res = 0;
  599. // if ( volume < -96 || volume > 0 ) {
  600. // ESP_LOGI(TAG, "Warning: volume < -96! or > 0!\n");
  601. // if (volume < -96) {
  602. // volume = -96;
  603. // } else {
  604. // volume = 0;
  605. // }
  606. // }
  607. // dot = (dot >= 5 ? 1 : 0);
  608. // return res;
  609. // }
  610. esp_err_t Es8311GetRef(bool flag) {
  611. esp_err_t ret = ESP_OK;
  612. uint8_t regv = 0;
  613. if (flag) {
  614. regv = Es8311ReadReg(ES8311_GPIO_REG44);
  615. regv |= 0x50;
  616. ret |= Es8311WriteReg(ES8311_GPIO_REG44, regv);
  617. } else {
  618. ret |= Es8311WriteReg(ES8311_GPIO_REG44, 0x08);
  619. }
  620. return ret;
  621. }
  622. int Es8311Init(Es8311Config* cfg) {
  623. es8311_priv = calloc(1, sizeof(struct es8311_private));
  624. I2cInit(&cfg->i2c_cfg, cfg->i2c_port_num); // ESP32 in master mode
  625. es8311_Codec_Startup(11289600, 44100);
  626. return 0;
  627. }
  628. void Es8311Uninit() {
  629. Es8311WriteReg(ES8311_RESET_REG00, 0x3f);
  630. free(es8311_priv);
  631. es8311_priv = NULL;
  632. }
  633. int Es8311ConfigFmt(ESCodecModule mode, ESCodecI2SFmt fmt) {
  634. int res = 0;
  635. uint8_t regAdc = 0, regDac = 0;
  636. if (mode == ES_MODULE_ADC || mode == ES_MODULE_ADC_DAC) {
  637. res |= Es8311WriteReg(ES8311_ADC_REG17, 0xBF);
  638. }
  639. if (mode == ES_MODULE_DAC || mode == ES_MODULE_ADC_DAC) {
  640. res |= Es8311WriteReg(ES8311_SYSTEM_REG12, 0x00);
  641. }
  642. regAdc = Es8311ReadReg(ES8311_SDPIN_REG09);
  643. regDac = Es8311ReadReg(ES8311_SDPOUT_REG0A);
  644. switch (fmt) {
  645. case ES_I2S_NORMAL:
  646. ESP_LOGI(TAG, "ES8311 in I2S Format");
  647. regAdc &= ~0x03;
  648. regDac &= ~0x03;
  649. break;
  650. case ES_I2S_LEFT:
  651. case ES_I2S_RIGHT:
  652. ESP_LOGI(TAG, "ES8311 in LJ Format");
  653. regAdc &= ~0x03;
  654. regAdc |= 0x01;
  655. regDac &= ~0x03;
  656. regDac |= 0x01;
  657. break;
  658. case ES_I2S_DSP:
  659. ESP_LOGI(TAG, "ES8311 in DSP Format");
  660. regAdc |= 0x03;
  661. regDac |= 0x03;
  662. break;
  663. default:
  664. ESP_LOGE(TAG, "Not Supported Format");
  665. break;
  666. }
  667. res |= Es8311WriteReg(ES8311_SDPIN_REG09, regAdc);
  668. res |= Es8311WriteReg(ES8311_SDPOUT_REG0A, regDac);
  669. return res;
  670. }
  671. int Es8311I2sConfigClock(ESCodecI2sClock cfg) {
  672. int res = 0;
  673. return res;
  674. }
  675. int Es8311SetBitsPerSample(ESCodecModule mode, BitsLength bitPerSample) {
  676. int res = 0;
  677. uint8_t reg = 0;
  678. int bits = (int)bitPerSample;
  679. if (mode == ES_MODULE_ADC || mode == ES_MODULE_ADC_DAC) {
  680. reg = Es8311ReadReg(ES8311_SDPIN_REG09);
  681. reg = reg & 0xe3;
  682. res |= Es8311WriteReg(ES8311_SDPIN_REG09, reg | (bits << 2));
  683. }
  684. if (mode == ES_MODULE_DAC || mode == ES_MODULE_ADC_DAC) {
  685. reg = Es8311ReadReg(ES8311_SDPOUT_REG0A);
  686. reg = reg & 0xe3;
  687. res |= Es8311WriteReg(ES8311_SDPOUT_REG0A, reg | (bits << 2));
  688. }
  689. return res;
  690. }
  691. int Es8311Start(ESCodecModule mode) {
  692. int res = 0;
  693. if (mode == ES_MODULE_ADC || mode == ES_MODULE_ADC_DAC) {
  694. res |= Es8311WriteReg(ES8311_ADC_REG17, 0xBF);
  695. }
  696. if (mode == ES_MODULE_DAC || mode == ES_MODULE_ADC_DAC) {
  697. res |= Es8311WriteReg(ES8311_SYSTEM_REG12,
  698. Es8311ReadReg(ES8311_SYSTEM_REG12) & 0xfd);
  699. }
  700. return res;
  701. }
  702. int Es8311Stop(ESCodecModule mode) {
  703. int res = 0;
  704. if (mode == ES_MODULE_ADC || mode == ES_MODULE_ADC_DAC) {
  705. res |= Es8311WriteReg(ES8311_ADC_REG17, 0x00);
  706. }
  707. if (mode == ES_MODULE_DAC || mode == ES_MODULE_ADC_DAC) {
  708. res |= Es8311WriteReg(ES8311_SYSTEM_REG12,
  709. Es8311ReadReg(ES8311_SYSTEM_REG12) | 0x02);
  710. }
  711. return res;
  712. }
  713. int Es8311SetVoiceVolume(int volume) {
  714. int res = 0;
  715. if (volume == 0) {
  716. volume = 1;
  717. }
  718. Es8311WriteReg(ES8311_DAC_REG32, volume);
  719. return res;
  720. }
  721. int Es8311GetVoiceVolume(int* volume) {
  722. int res = ESP_OK;
  723. int regv = Es8311ReadReg(ES8311_DAC_REG32);
  724. if (regv == ESP_FAIL) {
  725. *volume = 0;
  726. res = ESP_FAIL;
  727. } else {
  728. *volume = regv * 100 / 256;
  729. }
  730. ESP_LOGI(TAG, "GET: res:%d, volume:%d\n", regv, *volume);
  731. return res;
  732. }
  733. int Es8311SetVoiceMute(int enable) {
  734. int res = 0;
  735. ESP_LOGI(TAG, "Es8311SetVoiceMute volume:%d\n", enable);
  736. es8311_mute(enable);
  737. return res;
  738. }
  739. int Es8311GetVoiceMute(int* mute) {
  740. int res = -1;
  741. uint8_t reg = 0;
  742. res = Es8311ReadReg(ES8311_DAC_REG31);
  743. if (res != ESP_FAIL) {
  744. reg = (res & 0x20) >> 5;
  745. }
  746. *mute = reg;
  747. return res;
  748. }
  749. int Es8311SetMicGain(MicGain gain) {
  750. int res = 0;
  751. uint8_t gain_n = Es8311ReadReg(ES8311_ADC_REG16) & 0x07;
  752. gain_n |= gain / 6;
  753. res = Es8311WriteReg(ES8311_ADC_REG16, gain_n); // MIC gain scale
  754. return res;
  755. }
  756. int Es8311ConfigAdcInput(AdcInput input) {
  757. int res = 0;
  758. return res;
  759. }
  760. int Es8311SetAdcVolume(uint8_t adc_vol) {
  761. int res = 0;
  762. res = Es8311WriteReg(ES8311_ADC_REG17, adc_vol); // MIC ADC Volume
  763. return res;
  764. }
  765. int ES8311WriteReg(uint8_t regAdd, uint8_t data) {
  766. return Es8311WriteReg(regAdd, data);
  767. }
  768. void Es8311ReadAll() {
  769. for (int i = 0; i < 0x4A; i++) {
  770. uint8_t reg = Es8311ReadReg(i);
  771. // ets_printf("REG:%02x, %02x\n", reg, i);
  772. }
  773. }