apu.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _apu_H_
  16. #define _apu_H_
  17. #if defined(__cplusplus)
  18. extern "C" {
  19. #endif
  20. #define DIRECTION_RES 16
  21. #define I2S_FS 44100
  22. #define SOUND_SPEED 340
  23. typedef enum en_bf_dir
  24. {
  25. APU_DIR0 = 0,
  26. APU_DIR1,
  27. APU_DIR2,
  28. APU_DIR3,
  29. APU_DIR4,
  30. APU_DIR5,
  31. APU_DIR6,
  32. APU_DIR7,
  33. APU_DIR8,
  34. APU_DIR9,
  35. APU_DIR10,
  36. APU_DIR11,
  37. APU_DIR12,
  38. APU_DIR13,
  39. APU_DIR14,
  40. APU_DIR15,
  41. } en_bf_dir_t;
  42. typedef struct _apu_ch_cfg
  43. {
  44. /**
  45. * BF unit sound channel enable control bits.
  46. * Bit 'x' corresponds to enable bit for sound channel 'x' (x = 0, 1, 2,
  47. * . . ., 7). BF sound channels are related with I2S host RX channels.
  48. * BF sound channel 0/1 correspond to the left/right channel of I2S RX0;
  49. * BF channel 2/3 correspond to left/right channels of I2S RX1; and
  50. * things like that. 0x1: writing '1' to enable the corresponding BF
  51. * sound channel. 0x0: writing '0' to close the corresponding BF sound
  52. * channel.
  53. */
  54. uint32_t bf_sound_ch_en : 8;
  55. /**
  56. * Target direction select for valid voice output.
  57. * When the source voice direaction searching is done, software can use
  58. * this field to select one from 16 sound directions for the following
  59. * voice recognition. 0x0: select sound direction 0; 0x1: select sound
  60. * direction 1; . . . . . . 0xF: select sound direction 15.
  61. */
  62. uint32_t bf_target_dir : 4;
  63. /**
  64. * This is the audio sample gain factor. Using this gain factor to
  65. * enhance or reduce the stength of the sum of at most 8 source
  66. * sound channel outputs. This is a unsigned 11-bit fix-point number,
  67. * bit 10 is integer part and bit 9~0 are the fractional part.
  68. */
  69. uint32_t audio_gain : 11;
  70. uint32_t reserved1 : 1;
  71. /**
  72. * audio data source configure parameter. This parameter controls where
  73. * the audio data source comes from. 0x0: audio data directly sourcing
  74. * from apu internal buffer; 0x1: audio data sourcing from
  75. * FFT result buffer.
  76. */
  77. uint32_t data_src_mode : 1;
  78. uint32_t reserved2 : 3;
  79. /**
  80. * write enable for bf_sound_ch_en parameter.
  81. * 0x1: allowing updates made to 'bf_sound_ch_en'.
  82. * Access Mode: write only
  83. */
  84. uint32_t we_bf_sound_ch_en : 1;
  85. /**
  86. * write enable for bf_target_dir parameter.
  87. * 0x1: allowing updates made to 'bf_target_dir'.
  88. * Access Mode: write only
  89. */
  90. uint32_t we_bf_target_dir : 1;
  91. /**
  92. * write enable for audio_gain parameter.
  93. * 0x1: allowing updates made to 'audio_gain'.
  94. * Access Mode: write only
  95. */
  96. uint32_t we_audio_gain : 1;
  97. /**
  98. * write enable for data_out_mode parameter.
  99. * 0x1: allowing updates made to 'data_src_mode'.
  100. */
  101. uint32_t we_data_src_mode : 1;
  102. } __attribute__((packed, aligned(4))) apu_ch_cfg_t;
  103. typedef struct _apu_ctl_t
  104. {
  105. /**
  106. * Sound direction searching enable bit.
  107. * Software writes '1' to start sound direction searching function.
  108. * When all the sound sample buffers are filled full, this bit is
  109. * cleared by hardware (this sample buffers are used for direction
  110. * detect only). 0x1: enable direction searching.
  111. */
  112. uint32_t bf_dir_search_en : 1;
  113. /*
  114. *use this parameter to reset all the control logic on direction search processing path. This bit is self-clearing.
  115. * 0x1: apply reset to direction searching control logic;
  116. * 0x0: No operation.
  117. */
  118. uint32_t search_path_reset : 1;
  119. uint32_t reserved : 2;
  120. /**
  121. * Valid voice sample stream generation enable bit.
  122. * After sound direction searching is done, software can configure this
  123. * bit to generate a stream of voice samples for voice recognition. 0x1:
  124. * enable output of voice sample stream. 0x0: stop the voice samlpe
  125. * stream output.
  126. */
  127. uint32_t bf_stream_gen_en : 1;
  128. /*
  129. *use this parameter to reset all the control logic on voice stream generating path. This bit is self-clearing.
  130. * 0x1: apply reset to voice stream generating control logic;
  131. * 0x0: No operation.
  132. */
  133. uint32_t voice_gen_path_reset : 1;
  134. /*
  135. *use this parameter to switch to a new voice source direction. Software write '1' here and hardware will automatically clear it.
  136. * 0x1: write '1' here to request switching to new voice source direction.
  137. */
  138. uint32_t update_voice_dir : 1;
  139. uint32_t reserved1 : 1;
  140. //write enable for 'bf_dir_search_en' parameter.
  141. uint32_t we_bf_dir_search_en : 1;
  142. uint32_t we_search_path_rst : 1;
  143. uint32_t we_bf_stream_gen : 1;
  144. uint32_t we_voice_gen_path_rst : 1;
  145. uint32_t we_update_voice_dir : 1;
  146. uint32_t reserved2 : 19;
  147. } __attribute__((packed, aligned(4))) apu_ctl_t;
  148. typedef struct _apu_dir_bidx
  149. {
  150. uint32_t dir_rd_idx0 : 6;
  151. uint32_t reserved : 2;
  152. uint32_t dir_rd_idx1 : 6;
  153. uint32_t reserved1 : 2;
  154. uint32_t dir_rd_idx2 : 6;
  155. uint32_t reserved2 : 2;
  156. uint32_t dir_rd_idx3 : 6;
  157. uint32_t reserved3 : 2;
  158. } __attribute__((packed, aligned(4))) apu_dir_bidx_t;
  159. typedef struct _apu_fir_coef
  160. {
  161. uint32_t fir_tap0 : 16;
  162. uint32_t fir_tap1 : 16;
  163. } __attribute__((packed, aligned(4))) apu_fir_coef_t;
  164. typedef struct _apu_dwsz_cfg
  165. {
  166. /**
  167. * The down-sizing ratio used for direction searching.
  168. * 0x0: no down-sizing;
  169. * 0x1: 1/2 down sizing;
  170. * 0x2: 1/3 down sizing;
  171. * . . . . . .
  172. * 0xF: 1/16 down sizing.
  173. */
  174. uint32_t dir_dwn_siz_rate : 4;
  175. /**
  176. * The down-sizing ratio used for voice stream generation.
  177. * 0x0: no down-sizing;
  178. * 0x1: 1/2 down sizing;
  179. * 0x2: 1/3 down sizing;
  180. * . . . . . .
  181. * 0xF: 1/16 down sizing.
  182. */
  183. uint32_t voc_dwn_siz_rate : 4;
  184. /**
  185. * This bit field is used to perform sample precision reduction when
  186. * the source sound sample (from I2S0 host receiving channels)
  187. * precision is 20/24/32 bits.
  188. * 0x0: take bits 15~0 from the source sound sample;
  189. * 0x1: take bits 16~1 from the source sound sample;
  190. * 0x2: take bits 17~2 from the source sound sample;
  191. * . . . . . .
  192. * 0x10: take bits 31~16 from the source sound sample;
  193. */
  194. uint32_t smpl_shift_bits : 5;
  195. uint32_t reserved : 19;
  196. } __attribute__((packed, aligned(4))) apu_dwsz_cfg_t;
  197. /*0x31c*/
  198. typedef struct _apu_fft_cfg
  199. {
  200. uint32_t fft_shift_factor : 9;
  201. uint32_t reserved1 : 3;
  202. uint32_t fft_enable : 1;
  203. uint32_t reserved2 : 19;
  204. } __attribute__((packed, aligned(4))) apu_fft_cfg_t;
  205. /*0x328*/
  206. typedef struct _apu_int_stat
  207. {
  208. /**
  209. * sound direction searching data ready interrupt event.
  210. * Writing '1' to clear this interrupt event.
  211. * 0x1: data is ready for sound direction detect;
  212. * 0x0: no event.
  213. */
  214. uint32_t dir_search_data_rdy : 1;
  215. /**
  216. * voice output stream buffer data ready interrupt event.
  217. * When a block of 512 voice samples are collected, this interrupt event
  218. * is asserted. Writing '1' to clear this interrupt event. 0x1: voice
  219. * output stream buffer data is ready; 0x0: no event.
  220. */
  221. uint32_t voc_buf_data_rdy : 1;
  222. uint32_t reserved : 30;
  223. } __attribute__((packed, aligned(4))) apu_int_stat_t;
  224. /*0x32c*/
  225. typedef struct _apu_int_mask
  226. {
  227. /**
  228. * This is the interrupt mask to dir searching data ready interrupt.
  229. * 0x1: mask off this interrupt;
  230. * 0x0: enable this interrupt.
  231. */
  232. uint32_t dir_data_rdy_msk : 1;
  233. /**
  234. * This is the interrupt mask to voice output stream buffer ready
  235. * interrupt. 0x1: mask off this interrupt; 0x0: enable this interrupt.
  236. */
  237. uint32_t voc_buf_rdy_msk : 1;
  238. uint32_t reserved : 30;
  239. } __attribute__((packed, aligned(4))) apu_int_mask_t;
  240. typedef struct _apu_reg
  241. {
  242. //0x200
  243. apu_ch_cfg_t bf_ch_cfg_reg;
  244. //0x204
  245. apu_ctl_t bf_ctl_reg;
  246. //0x208
  247. apu_dir_bidx_t bf_dir_bidx[16][2];
  248. //0x288
  249. apu_fir_coef_t bf_pre_fir0_coef[9];
  250. //0x2ac
  251. apu_fir_coef_t bf_post_fir0_coef[9];
  252. //0x2d0
  253. apu_fir_coef_t bf_pre_fir1_coef[9];
  254. //0x2f4
  255. apu_fir_coef_t bf_post_fir1_coef[9];
  256. //0x318
  257. apu_dwsz_cfg_t bf_dwsz_cfg_reg;
  258. //0x31c
  259. apu_fft_cfg_t bf_fft_cfg_reg;
  260. // 0x320
  261. /**
  262. * This is the read register for system DMA to read data stored in
  263. * sample out buffers (the sample out buffers are used for sound
  264. * direction detect). Each data contains two sound samples.
  265. */
  266. volatile uint32_t sobuf_dma_rdata;
  267. // 0x324
  268. /**
  269. * This is the read register for system DMA to read data stored in voice
  270. * out buffers (the voice out buffers are used for voice recognition).
  271. * Each data contains two sound samples.
  272. */
  273. volatile uint32_t vobuf_dma_rdata;
  274. /*0x328*/
  275. apu_int_stat_t bf_int_stat_reg;
  276. /*0x32c*/
  277. apu_int_mask_t bf_int_mask_reg;
  278. /*0x330*/
  279. uint32_t saturation_counter;
  280. /*0x334*/
  281. uint32_t saturation_limits;
  282. } __attribute__((packed, aligned(4))) apu_reg_t;
  283. extern volatile apu_reg_t *const apu;
  284. /**
  285. * @brief Voice strength average value right shift factor. When performing sound direction detect,
  286. * the average value of samples from different channels is required, this right shift factor
  287. * is used to perform division.
  288. *
  289. * @param[in] gain value of audio gain.
  290. * 0x0: no right shift;
  291. * 0x1: right shift by 1-bit;
  292. * . . . . . .
  293. * 0xF: right shift by 15-bit.
  294. *
  295. */
  296. void apu_set_audio_gain(uint16_t gain);
  297. /**
  298. * @brief Set sampling shift.
  299. *
  300. * @param[in] smpl_shift vlaue of sampling shift
  301. *
  302. */
  303. void apu_set_smpl_shift(uint8_t smpl_shift);
  304. /**
  305. * @brief Get sampling shift
  306. *
  307. * @return vlaue of sampling shift
  308. */
  309. uint8_t apu_get_smpl_shift(void);
  310. /**
  311. * @brief APU unit sound channel enable control bits. Bit 'x' corresponds to enable bit for sound
  312. * channel 'x' (x = 0, 1, 2, . . ., 7). APU sound channels are related with I2S host RX channels.
  313. * APU sound channel 0/1 correspond to the left/right channel of I2S RX0; APU channel 2/3 correspond
  314. * to left/right channels of I2S RX1; and things like that. Software write '1' to enable a sound
  315. * channel and hardware automatically clear the bit after the sample buffers used for direction
  316. * searching is filled full.
  317. *
  318. *
  319. * @param[in] channel_bit APU sound channel.0x1: writing
  320. * '1' to enable the corresponding APU sound channel.
  321. *
  322. */
  323. void apu_set_channel_enabled(uint8_t channel_bit);
  324. /**
  325. * @brief I2S host beam-forming direction sample ibuffer read index configure register
  326. *
  327. * @param[in] dir_num the direction of index
  328. * @param[in] dir_bidx
  329. *
  330. */
  331. void apu_set_direction_delay(uint8_t dir_num, uint8_t *dir_bidx);
  332. /**
  333. * @brief I2S host beam-forming direction sample ibuffer read index configure register
  334. *
  335. * @param[in] radius radius
  336. * @param[in] mic_num_a_circle the num of mic per circle
  337. * @param[in] center 0: no center mic, 1:have center mic
  338. *
  339. */
  340. void apu_set_delay(float radius, uint8_t mic_num_a_circle, uint8_t center);
  341. /**
  342. * @brief Set ffp shift factor
  343. *
  344. * @param[in] enable_flag enable fft
  345. * @param[in] shift_factor shift factor
  346. *
  347. */
  348. void apu_set_fft_shift_factor(uint8_t enable_flag, uint16_t shift_factor);
  349. /**
  350. * @brief Set down-sizing ratio used for voice direction searching and voice stream generation.
  351. *
  352. * @param[in] dir_dwn_siz down-sizing ratio used for voice direction searching
  353. * 0x0: no down-sizing
  354. * 0x1: 1/2 down sizing
  355. * 0x2: 1/3 down sizing
  356. * . . . . . .
  357. * 0xF: 1/16 down sizing
  358. * @param[in] voc_dwn_siz down-sizing ratio used for voice stream generation
  359. * 0x0: no down-sizing
  360. * 0x1: 1/2 down sizing
  361. * 0x2: 1/3 down sizing
  362. * . . . . . .
  363. * 0xF: 1/16 down sizing
  364. */
  365. void apu_set_down_size(uint8_t dir_dwn_siz, uint8_t voc_dwn_siz);
  366. /**
  367. * @brief Set direction and voice interrupt mask
  368. *
  369. * @param[in] dir_int_mask direction interrupt mask
  370. * @param[in] voc_int_mask voice interrupt mask
  371. *
  372. */
  373. void apu_set_interrupt_mask(uint8_t dir_int_mask, uint8_t voc_int_mask);
  374. /**
  375. * @brief Enable direction searching.
  376. *
  377. */
  378. void apu_dir_enable(void);
  379. /**
  380. * @brief Reset direction searching.
  381. *
  382. */
  383. void apu_dir_reset(void);
  384. /**
  385. * @brief I2S host beam-forming Filter FIR16 Coefficient Register
  386. *
  387. * @param[in] fir_coef direction prev FIR
  388. *
  389. */
  390. void apu_dir_set_prev_fir(uint16_t *fir_coef);
  391. /**
  392. * @brief I2S host beam-forming Filter FIR16 Coefficient Register
  393. *
  394. * @param[in] fir_coef direction post FIR
  395. *
  396. */
  397. void apu_dir_set_post_fir(uint16_t *fir_coef);
  398. /**
  399. * @brief Set down-sizing ratio used for voice direction searching
  400. *
  401. * @param[in] dir_dwn_siz down-sizing ratio used for voice direction searching
  402. * 0x0: no down-sizing
  403. * 0x1: 1/2 down sizing
  404. * 0x2: 1/3 down sizing
  405. * . . . . . .
  406. * 0xF: 1/16 down sizing
  407. */
  408. void apu_dir_set_down_size(uint8_t dir_dwn_size);
  409. /**
  410. * @brief Set direction searching interrupt mask
  411. *
  412. * @param[in] dir_int_mask direction interrupt mask
  413. *
  414. */
  415. void apu_dir_set_interrupt_mask(uint8_t dir_int_mask);
  416. /**
  417. * @brief Clear direction interrupt
  418. *
  419. */
  420. void apu_dir_clear_int_state(void);
  421. /**
  422. * @brief Valid voice sample stream generation enable bit. After sound direction searching is done, software can
  423. * configure this bit to generate a stream of voice samples for voice recognition.
  424. *
  425. * @param[in] enable_flag 0x1: enable output of voice sample stream. 0x0: stop the voice samlpe stream output.
  426. *
  427. */
  428. void apu_voc_enable(uint8_t enable_flag);
  429. /**
  430. * @brief Reset voice sample
  431. *
  432. */
  433. void apu_voc_reset(void);
  434. /**
  435. * @brief Target direction select for valid voice output. When the source voice direaction searching
  436. * is done, software can use this field to select one from 16 sound directions for the following
  437. * voice recognition
  438. *
  439. * @param[in] direction 0x0: select sound direction 0;
  440. * 0x1: select sound direction 1;
  441. * . . . . . .
  442. * 0xF: select sound direction 15.
  443. */
  444. void apu_voc_set_direction(en_bf_dir_t direction);
  445. /**
  446. * @brief I2S host beam-forming Filter FIR16 Coefficient Register
  447. *
  448. * @param[in] fir_coef voice prev FIR
  449. *
  450. */
  451. void apu_voc_set_prev_fir(uint16_t *fir_coef);
  452. /**
  453. * @brief I2S host beam-forming Filter FIR16 Coefficient Register
  454. *
  455. * @param[in] fir_coef voice post FIR
  456. *
  457. */
  458. void apu_voc_set_post_fir(uint16_t *fir_coef);
  459. /**
  460. * @brief Set down-sizing ratio used for voice stream generation.
  461. *
  462. * @param[in] voc_dwn_siz down-sizing ratio used for voice stream generation
  463. * 0x0: no down-sizing
  464. * 0x1: 1/2 down sizing
  465. * 0x2: 1/3 down sizing
  466. * . . . . . .
  467. * 0xF: 1/16 down sizing
  468. */
  469. void apu_voc_set_down_size(uint8_t voc_dwn_size);
  470. /**
  471. * @brief Set voice stream generation interrupt mask
  472. *
  473. * @param[in] voc_int_mask voice interrupt mask
  474. *
  475. */
  476. void apu_voc_set_interrupt_mask(uint8_t voc_int_mask);
  477. /**
  478. * @brief Clear voice interrupt
  479. *
  480. */
  481. void apu_voc_clear_int_state(void);
  482. /**
  483. * @brief Reset saturation_counter
  484. *
  485. */
  486. void apu_voc_reset_saturation_counter(void);
  487. /**
  488. * @brief Get saturation counter
  489. *
  490. * @return vlaue of saturation counter.heigh 16 bit is counter, low 16 bit is total
  491. */
  492. uint32_t apu_voc_get_saturation_counter(void);
  493. /**
  494. * @brief set saturation limit
  495. *
  496. * @param[in] upper heigh 16 bit is counter
  497. * @param[in] bottom low 16 bit is total
  498. *
  499. */
  500. void apu_voc_set_saturation_limit(uint16_t upper, uint16_t bottom);
  501. /**
  502. * @brief Get saturation limit
  503. *
  504. * @return vlaue of saturation limit.heigh 16 bit is counter, low 16 bit is total
  505. */
  506. uint32_t apu_voc_get_saturation_limit(void);
  507. /**
  508. * @brief Print apu setting for debug
  509. *
  510. */
  511. void apu_print_setting(void);
  512. #if defined(__cplusplus)
  513. }
  514. #endif
  515. #endif