voice.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @file voice.h
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. /*******************************************************************************
  11. * @file voice.h
  12. * @brief Contains all functions support for voice driver
  13. * @version 0.0
  14. * @date 18. Jun. 2018
  15. * @author qing.han
  16. *
  17. *
  18. *******************************************************************************/
  19. #ifndef __VOICE__H__
  20. #define __VOICE__H__
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #include "types.h"
  25. #include "gpio.h"
  26. #define MAX_VOICE_SAMPLE_SIZE 512
  27. #define MAX_VOICE_SAMPLE_ID (MAX_VOICE_SAMPLE_SIZE-1)
  28. #define HALF_VOICE_SAMPLE_SIZE 256
  29. #define HALF_VOICE_SAMPLE_ID (HALF_VOICE_SAMPLE_SIZE-1)
  30. #define MAX_VOICE_WORD_SIZE 256
  31. #define HALF_VOICE_WORD_SIZE 128
  32. //#define BELOW_1V 0
  33. //#define UP_1V 1
  34. //#define ADC_INPUT_LEVEL BELOW_1V
  35. //#define ADC_INPUT_LEVEL UP_1V
  36. #define VOICE_BASE (0x40050800UL)
  37. #define VOICE_MID_BASE (0x40050a00UL)
  38. #define VOICE_SAMPLE_AUTO 0
  39. #define VOICE_SAMPLE_MANNUAL 1
  40. #define VOICE_SAMPLE_MODE SAMPLE_MANNUAL
  41. //#define ADC_SAMPLE_MODE SAMPLE_AUTO
  42. #define ENABLE_VOICE_INT *(volatile unsigned int *)0x40050034 |= 0x00000300
  43. //#define ENABLE_VOICE_INT *(volatile unsigned int *)0x40050034 &= 0xfffffcff
  44. #define MASK_VOICE_INT *(volatile unsigned int *)0x40050034 &= 0xfffffcff
  45. //#define MASK_VOICE_INT *(volatile unsigned int *)0x40050034 |= 0x00000300
  46. /*#define ADC_IRQ_ENABLE *(volatile unsigned int *) 0xe000e100 = BIT(29)
  47. #define ADC_IRQ_DISABLE *(volatile unsigned int *) 0xe000e100 = BIT(29)
  48. #define CLEAR_ADC_INT_CH0 *(volatile unsigned int *)0x40050038 |= BIT(0)
  49. #define CLEAR_ADC_INT_CH1 *(volatile unsigned int *)0x40050038 |= BIT(1)
  50. #define CLEAR_ADC_INT_CH2 *(volatile unsigned int *)0x40050038 |= BIT(2)
  51. #define CLEAR_ADC_INT_CH3 *(volatile unsigned int *)0x40050038 |= BIT(3)
  52. #define CLEAR_ADC_INT_CH4 *(volatile unsigned int *)0x40050038 |= BIT(4)
  53. #define CLEAR_ADC_INT_CH5 *(volatile unsigned int *)0x40050038 |= BIT(5)
  54. #define CLEAR_ADC_INT_CH6 *(volatile unsigned int *)0x40050038 |= BIT(6)
  55. #define CLEAR_ADC_INT_CH7 *(volatile unsigned int *)0x40050038 |= BIT(7)*/
  56. #define CLEAR_VOICE_HALF_INT *(volatile unsigned int *)0x40050038 |= BIT(8)
  57. #define CLEAR_VOICE_FULL_INT *(volatile unsigned int *)0x40050038 |= BIT(9)
  58. /*#define CLEAR_ADC_INT(n) *(volatile unsigned int *)0x40050038 |= BIT(n)
  59. #define IS_CLAER_ADC_INT_CH0 (*(volatile unsigned int *)0x4005003c) & BIT(0)
  60. #define IS_CLAER_ADC_INT_CH1 (*(volatile unsigned int *)0x4005003c) & BIT(1)
  61. #define IS_CLAER_ADC_INT_CH2 (*(volatile unsigned int *)0x4005003c) & BIT(2)
  62. #define IS_CLAER_ADC_INT_CH3 (*(volatile unsigned int *)0x4005003c) & BIT(3)
  63. #define IS_CLAER_ADC_INT_CH4 (*(volatile unsigned int *)0x4005003c) & BIT(4)
  64. #define IS_CLAER_ADC_INT_CH5 (*(volatile unsigned int *)0x4005003c) & BIT(5)
  65. #define IS_CLAER_ADC_INT_CH6 (*(volatile unsigned int *)0x4005003c) & BIT(6)
  66. #define IS_CLAER_ADC_INT_CH7 (*(volatile unsigned int *)0x4005003c) & BIT(7)*/
  67. #define IS_CLAER_VOICE_HALF_INT (*(volatile unsigned int *)0x4005003c) & BIT(8)
  68. #define IS_CLAER_VOICE_FULL_INT (*(volatile unsigned int *)0x4005003c) & BIT(9)
  69. //#define IS_CLAER_ADC_INT(n) (*(volatile unsigned int *)0x4005003c) & BIT(n)
  70. #ifndef GET_IRQ_STATUS
  71. #define GET_IRQ_STATUS (AP_ADCC->intr_status & 0x3ff)
  72. #endif
  73. #ifndef ENABLE_ADC
  74. #define ENABLE_ADC (AP_PCRM->ANA_CTL |= BIT(3))
  75. #endif
  76. #ifndef DISABLE_ADC
  77. #define DISABLE_ADC (AP_PCRM->ANA_CTL &= ~BIT(3))
  78. #endif
  79. #ifndef ADC_CLOCK_ENABLE
  80. #define ADC_CLOCK_ENABLE (AP_PCRM->CLKHF_CTL1 |= BIT(13))
  81. #endif
  82. #define ADC_DBLE_CLOCK_DISABLE (*(volatile unsigned int *)0x4000f044 &= ~BIT(21))
  83. #define POWER_DOWN_ADC (*(volatile unsigned int *)0x4000f048 &= ~BIT(3))
  84. #define POWER_UP_TEMPSENSOR (*(volatile unsigned int *)0x4000f048 |= BIT(29))
  85. #define REG_IO_CONTROL ((volatile unsigned int *)0x4000f020)
  86. #define ADCC_REG_BASE (0x4000F000)
  87. // Voice encode mode
  88. typedef enum {
  89. VOICE_ENCODE_PCMA = 0,
  90. VOICE_ENCODE_PCMU = 1,
  91. VOICE_ENCODE_CVSD = 2,
  92. VOICE_ENCODE_BYP = 3
  93. }VOICE_ENCODE_t;
  94. // Voice sample rate
  95. typedef enum {
  96. VOICE_RATE_64K = 0,
  97. VOICE_RATE_32K = 1,
  98. VOICE_RATE_16K = 2,
  99. VOICE_RATE_8K = 3
  100. }VOICE_RATE_t;
  101. // Voice notch filter configuration
  102. typedef enum {
  103. VOICE_NOTCH_BYP = 0,
  104. VOICE_NOTCH_1 = 1,
  105. VOICE_NOTCH_2 = 2,
  106. VOICE_NOTCH_3 = 3
  107. }VOICE_NOTCH_t;
  108. // Voice polarity selection
  109. typedef enum {
  110. VOICE_POLARITY_POS = 0,
  111. VOICE_POLARITY_NEG = 1
  112. }VOICE_POLARITY_t;
  113. enum{
  114. HAL_VOICE_EVT_DATA = 1,
  115. HAL_VOICE_EVT_FAIL = 0xff
  116. };
  117. // Voice configuration structure
  118. typedef struct _voice_Cfg_t{
  119. bool voiceSelAmicDmic;
  120. GpioPin_t dmicDataPin;
  121. GpioPin_t dmicClkPin;
  122. uint8_t amicGain;
  123. uint8_t voiceGain;
  124. VOICE_ENCODE_t voiceEncodeMode;
  125. VOICE_RATE_t voiceRate;
  126. bool voiceAutoMuteOnOff;
  127. }voice_Cfg_t;
  128. // Voice event structure
  129. typedef struct _voice_Evt_t{
  130. int type;
  131. uint32_t* data;
  132. uint32_t size;
  133. }voice_Evt_t;
  134. typedef void (*voice_Hdl_t)(voice_Evt_t* pev);
  135. // Voice context structure
  136. typedef struct _voice_Contex_t{
  137. bool enable;
  138. voice_Cfg_t cfg;
  139. voice_Hdl_t evt_handler;
  140. }voice_Ctx_t;
  141. // Enable voice core
  142. void hal_voice_enable(void);
  143. // Disable voice core
  144. void hal_voice_disable(void);
  145. // Select DMIC
  146. void hal_voice_dmic_mode(void);
  147. // Select AMIC
  148. void hal_voice_amic_mode(void);
  149. // Open a GPIO pin for DMIC
  150. void hal_voice_dmic_open(GpioPin_t dmicDataPin, GpioPin_t dmicClkPin);
  151. // Set PGA gain for AMIC
  152. void hal_voice_amic_gain(uint8_t amicGain);
  153. // Set voice process gain
  154. void hal_voice_gain(uint8_t voiceGain);
  155. // Set voice encoding mode
  156. void hal_voice_encode(VOICE_ENCODE_t voiceEncodeMode);
  157. // Set voice data rate
  158. void hal_voice_rate(VOICE_RATE_t voiceRate);
  159. // Enable voice auto-mute
  160. void hal_voice_amute_on(void);
  161. // Disable voice auto-mute
  162. void hal_voice_amute_off(void);
  163. /**************************************************************************************
  164. * @fn hal_VOICE_IRQHandler
  165. *
  166. * @brief This function process for adc interrupt
  167. *
  168. * input parameters
  169. *
  170. * @param None.
  171. *
  172. * output parameters
  173. *
  174. * @param None.
  175. *
  176. * @return None.
  177. **************************************************************************************/
  178. // Voice interrupt handler
  179. void __attribute__((weak)) hal_ADC_IRQHandler(void);
  180. /**************************************************************************************
  181. * @fn hal_voice_init
  182. *
  183. * @brief This function process for adc initial
  184. *
  185. * input parameters
  186. *
  187. * @param ADC_MODE_e mode: adc sample mode select;1:SAM_MANNUAL(mannual mode),0:SAM_AUTO(auto mode)
  188. * ADC_CH_e adc_pin: adc pin select;ADC_CH0~ADC_CH7 and ADC_CH_VOICE
  189. * ADC_SEMODE_e semode: signle-ended mode negative side enable; 1:SINGLE_END(single-ended mode) 0:DIFF(Differentail mode)
  190. * IO_CONTROL_e amplitude: input signal amplitude, 0:BELOW_1V,1:UP_1V
  191. *
  192. * output parameters
  193. *
  194. * @param None.
  195. *
  196. * @return None.
  197. **************************************************************************************/
  198. // Allocate memory and power manager for voice
  199. void hal_voice_init(void);
  200. // Configure voice capture
  201. int hal_voice_config(voice_Cfg_t cfg, voice_Hdl_t evt_handler);
  202. // Start voice capture
  203. int hal_voice_start(void);
  204. // Stop voice capture
  205. int hal_voice_stop(void);
  206. // Clear memory and power manager for voice
  207. int hal_voice_clear(void);
  208. #ifdef __cplusplus
  209. }
  210. #endif
  211. #endif