anc_test.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "include.h"
  2. #define TRACE_EN 0
  3. #if TRACE_EN
  4. #define TRACE(...) printf(__VA_ARGS__)
  5. #define TRACE_R(...) print_r(__VA_ARGS__)
  6. #else
  7. #define TRACE(...)
  8. #define TRACE_R(...)
  9. #endif // TRACE_EN
  10. #define EQ_CRC_SEED 0xffff
  11. uint calc_crc(void *buf, uint len, uint seed);
  12. void tx_ack(uint8_t *packet, uint16_t len);
  13. u8 check_sum(u8 *buf, u16 size);
  14. #if ANC_EN
  15. static void bsp_anc_dbg_ack_inquiry(void)
  16. {
  17. u8 ack[4];
  18. memset(ack, 0, 4);
  19. ack[0] = 'A';
  20. ack[1] = 'N';
  21. ack[2] = 'C';
  22. ack[3] = '*';
  23. tx_ack(ack, 4);
  24. }
  25. static void bsp_anc_dbg_ack(u8 error_code)
  26. {
  27. u8 ack[4];
  28. memset(ack, 0, 4);
  29. ack[0] = 'Y';
  30. ack[2] = error_code;
  31. ack[3] = check_sum(ack, 3);
  32. tx_ack(ack, 4);
  33. }
  34. void bsp_anc_parse_cmd(void)
  35. {
  36. u8 *ptr = eq_rx_buf;
  37. #if TRACE_EN
  38. //print_r(eq_rx_buf, EQ_BUFFER_LEN);
  39. #endif
  40. if (ptr[3] == '?') {
  41. bsp_anc_dbg_ack_inquiry();
  42. return;
  43. }
  44. u16 size = ptr[4] + 3;
  45. u16 crc = calc_crc(ptr, size, 0xffff);
  46. if (crc != little_endian_read_16(ptr, size)) {
  47. bsp_anc_dbg_ack(1); //error code 1:crc error
  48. return;
  49. }
  50. u8 channel = ptr[5];
  51. u8 bit_ctl = ptr[6];
  52. s8 adjust0 = (s8)ptr[8]; //ch0 mute sta
  53. s8 adjust1 = (s8)ptr[9];
  54. bool anc_on = (bool)(bit_ctl & BIT(0)); //判断是否打开或关闭anc
  55. bool save_flag = (bool)(bit_ctl & BIT(1));//判断参数是否烧录
  56. bool eq_tool = (bool)(bit_ctl & BIT(4)); //判断是eq工具还是产测协议
  57. bool tp_flag = (bool)(bit_ctl & BIT(5)); //判断是anc模式测试,还是通透模式测试
  58. if (channel > 3 || channel == 0) { //L-1,R-2,L+R-3
  59. bsp_anc_dbg_ack(2); //error code 2:param error
  60. return;
  61. }
  62. if (!xcfg_cb.anc_en) {
  63. printf("anc close\n");
  64. bsp_anc_dbg_ack(3); //error code 3:anc close
  65. return;
  66. }
  67. if (!eq_tool && !anc_on) {
  68. bsp_anc_stop();
  69. bsp_anc_exit();
  70. bsp_anc_dbg_ack(0); //error code 0:succeed
  71. TRACE("anc parse cmd anc exit\n");
  72. return;
  73. } else {
  74. if (!tp_flag) {
  75. if ((!sys_cb.anc_start) || (sys_cb.anc_user_mode != 1)) {
  76. bsp_anc_set_mode(1);
  77. }
  78. } else {
  79. if ((!sys_cb.anc_start) || (sys_cb.anc_user_mode != 2)) {
  80. bsp_anc_set_mode(2);
  81. }
  82. }
  83. TRACE("anc parse cmd anc start\n");
  84. }
  85. bsp_anc_fade_enable(0);
  86. do {
  87. #if BT_TWS_EN
  88. u8 ch = channel - 1;
  89. if (xcfg_cb.bt_tws_en) {
  90. if (ch == 2 || (ch == sys_cb.tws_left_channel)) { //声道不相等,转发
  91. if (bt_tws_is_connected()) {
  92. if (ch == 2) { //把这个声道去掉,重新算下CRC
  93. ptr[5] = (sys_cb.tws_left_channel == 1) ? 2 : 1;
  94. u16 crc = calc_crc(ptr, 14, EQ_CRC_SEED);
  95. ptr[14] = crc;
  96. ptr[15] = crc >> 8;
  97. }
  98. bt_tws_sync_eq_param();
  99. }
  100. if (ch != 2) { //如果收到单声道的命令,转发后跳出
  101. break;
  102. }
  103. }
  104. if (xcfg_cb.anc_mode == MODE_TWS_FFFB) { //TWS FFFB模式只调MICL
  105. channel = 1;
  106. }
  107. }
  108. #endif
  109. bool ch0_mute = false;
  110. bool ch1_mute = false;
  111. if (xcfg_cb.anc_mode >= MODE_TWS_HYBRID) {
  112. ch0_mute = !anc_on || (bit_ctl & BIT(2));
  113. ch1_mute = !anc_on || (bit_ctl & BIT(3));
  114. TRACE("channel %d, ch0 mute %d, %d, %d, %d\n", channel, ch0_mute, ch1_mute, ch2_mute, ch3_mute);
  115. if (xcfg_cb.anc_mode == MODE_HYBRID) { //hybrid mode, ch0~ch3
  116. if (channel & 0x01) { //mute ch0 ch1
  117. bsp_anc_mic_mute(0, ch0_mute);
  118. bsp_anc_mic_mute(1, ch1_mute);
  119. if (!eq_tool) { //产测流程才会调整adjust value
  120. if (ch0_mute == 0) bsp_anc_gain_adjust(0, adjust0); //烧录gain adjust0值
  121. if (ch1_mute == 0) bsp_anc_gain_adjust(1, adjust1); //烧录gain adjust1值
  122. }
  123. if (!tp_flag) {
  124. bsp_param_write((u8*)&adjust0, PARAM_ANC_MIC0_VAL, 1);
  125. bsp_param_write((u8*)&adjust1, PARAM_ANC_MIC1_VAL, 1);
  126. } else {
  127. bsp_param_write((u8*)&adjust0, PARAM_ANC_TP_MIC0_VAL, 1);
  128. bsp_param_write((u8*)&adjust1, PARAM_ANC_TP_MIC1_VAL, 1);
  129. }
  130. }
  131. if (channel & 0x02) { //mute ch2 ch3
  132. bsp_anc_mic_mute(2, ch0_mute);
  133. bsp_anc_mic_mute(3, ch1_mute);
  134. if (!eq_tool) {
  135. if (ch0_mute == 0) bsp_anc_gain_adjust(2, adjust0); //烧录gain adjust0值
  136. if (ch1_mute == 0) bsp_anc_gain_adjust(3, adjust1); //烧录gain adjust1值
  137. }
  138. if (!tp_flag) {
  139. bsp_param_write((u8*)&adjust0, PARAM_ANC_MIC2_VAL, 1);
  140. bsp_param_write((u8*)&adjust1, PARAM_ANC_MIC3_VAL, 1);
  141. } else {
  142. bsp_param_write((u8*)&adjust0, PARAM_ANC_TP_MIC2_VAL, 1);
  143. bsp_param_write((u8*)&adjust1, PARAM_ANC_TP_MIC3_VAL, 1);
  144. }
  145. }
  146. } else { //tws hybrid mode, ch0、ch1
  147. bsp_anc_mic_mute(0, ch0_mute);
  148. bsp_anc_mic_mute(1, ch1_mute);
  149. if (!eq_tool) { //产测流程才会调整adjust value
  150. if (ch0_mute == 0) bsp_anc_gain_adjust(0, adjust0); //烧录gain adjust0值
  151. if (ch1_mute == 0) bsp_anc_gain_adjust(1, adjust1); //烧录gain adjust1值
  152. }
  153. if (save_flag) {
  154. if (!tp_flag) {
  155. bsp_param_write((u8*)&adjust0, PARAM_ANC_MIC0_VAL, 1);
  156. bsp_param_write((u8*)&adjust1, PARAM_ANC_MIC1_VAL, 1);
  157. } else {
  158. bsp_param_write((u8*)&adjust0, PARAM_ANC_TP_MIC0_VAL, 1);
  159. bsp_param_write((u8*)&adjust1, PARAM_ANC_TP_MIC1_VAL, 1);
  160. }
  161. }
  162. }
  163. if (eq_tool) { //eq工具设置模拟增益
  164. if (xcfg_cb.anc_mode == MODE_HYBRID) {
  165. if (channel & 0x01) {
  166. bsp_anc_set_mic_gain(0, 1, ptr[10]);
  167. bsp_anc_set_mic_gain(1, 1, ptr[11]);
  168. }
  169. if (channel & 0x02) {
  170. bsp_anc_set_mic_gain(2, 1, ptr[10]);
  171. bsp_anc_set_mic_gain(3, 1, ptr[11]);
  172. }
  173. } else {
  174. bsp_anc_set_mic_gain(0, 1, ptr[10]);
  175. bsp_anc_set_mic_gain(1, 1, ptr[11]);
  176. }
  177. }
  178. } else {
  179. bool ch_mute = !anc_on || (bit_ctl & BIT(2));
  180. if (channel & 0x01) {
  181. bsp_anc_mic_mute(0, ch_mute);
  182. if (!eq_tool) {
  183. bsp_anc_gain_adjust(0, adjust0);
  184. }
  185. if (save_flag) {
  186. if (!tp_flag) {
  187. bsp_param_write((u8*)&sys_cb.adjust_val[0], PARAM_ANC_MIC0_VAL, 1);
  188. } else {
  189. bsp_param_write((u8*)&sys_cb.adjust_val[0], PARAM_ANC_TP_MIC0_VAL, 1);
  190. }
  191. }
  192. }
  193. if (channel & 0x02) {
  194. bsp_anc_mic_mute(1, ch_mute);
  195. if (!eq_tool) {
  196. bsp_anc_gain_adjust(1, adjust0);
  197. }
  198. if (save_flag) {
  199. if (!tp_flag) {
  200. bsp_param_write((u8*)&sys_cb.adjust_val[1], PARAM_ANC_MIC1_VAL, 1);
  201. } else {
  202. bsp_param_write((u8*)&sys_cb.adjust_val[1], PARAM_ANC_TP_MIC1_VAL, 1);
  203. }
  204. }
  205. }
  206. //调整again,研发调试用
  207. if (eq_tool) {
  208. if (channel & 0x01) {
  209. bsp_anc_set_mic_gain(0, 1, ptr[10]);
  210. }
  211. if (channel & 0x02) {
  212. bsp_anc_set_mic_gain(1, 1, ptr[10]);
  213. }
  214. }
  215. }
  216. if (save_flag) {
  217. bsp_param_sync();
  218. }
  219. } while (0);
  220. bsp_anc_dbg_ack(0); //error code 0:succeed
  221. }
  222. #endif