voice.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file voice.h
  30. * @brief Contains all functions support for voice driver
  31. * @version 0.0
  32. * @date 18. Jun. 2018
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2018, PhyPlus Microelectronics
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #ifndef __VOICE__H__
  40. #define __VOICE__H__
  41. #include "types.h"
  42. #include "gpio.h"
  43. #define MAX_VOICE_SAMPLE_SIZE 512
  44. #define MAX_VOICE_SAMPLE_ID (MAX_VOICE_SAMPLE_SIZE-1)
  45. #define HALF_VOICE_SAMPLE_SIZE 256
  46. #define HALF_VOICE_SAMPLE_ID (HALF_VOICE_SAMPLE_SIZE-1)
  47. #define MAX_VOICE_WORD_SIZE 256
  48. #define HALF_VOICE_WORD_SIZE 128
  49. //#define BELOW_1V 0
  50. //#define UP_1V 1
  51. //#define ADC_INPUT_LEVEL BELOW_1V
  52. //#define ADC_INPUT_LEVEL UP_1V
  53. #define VOICE_BASE (0x40050800UL)
  54. #define VOICE_MID_BASE (0x40050a00UL)
  55. #define VOICE_SAMPLE_AUTO 0
  56. #define VOICE_SAMPLE_MANNUAL 1
  57. #define VOICE_SAMPLE_MODE SAMPLE_MANNUAL
  58. //#define ADC_SAMPLE_MODE SAMPLE_AUTO
  59. #define ENABLE_VOICE_INT *(volatile unsigned int *)0x40050034 |= 0x00000300
  60. //#define ENABLE_VOICE_INT *(volatile unsigned int *)0x40050034 &= 0xfffffcff
  61. #define MASK_VOICE_INT *(volatile unsigned int *)0x40050034 &= 0xfffffcff
  62. //#define MASK_VOICE_INT *(volatile unsigned int *)0x40050034 |= 0x00000300
  63. /*#define ADC_IRQ_ENABLE *(volatile unsigned int *) 0xe000e100 = BIT(29)
  64. #define ADC_IRQ_DISABLE *(volatile unsigned int *) 0xe000e100 = BIT(29)
  65. #define CLEAR_ADC_INT_CH0 *(volatile unsigned int *)0x40050038 |= BIT(0)
  66. #define CLEAR_ADC_INT_CH1 *(volatile unsigned int *)0x40050038 |= BIT(1)
  67. #define CLEAR_ADC_INT_CH2 *(volatile unsigned int *)0x40050038 |= BIT(2)
  68. #define CLEAR_ADC_INT_CH3 *(volatile unsigned int *)0x40050038 |= BIT(3)
  69. #define CLEAR_ADC_INT_CH4 *(volatile unsigned int *)0x40050038 |= BIT(4)
  70. #define CLEAR_ADC_INT_CH5 *(volatile unsigned int *)0x40050038 |= BIT(5)
  71. #define CLEAR_ADC_INT_CH6 *(volatile unsigned int *)0x40050038 |= BIT(6)
  72. #define CLEAR_ADC_INT_CH7 *(volatile unsigned int *)0x40050038 |= BIT(7)*/
  73. #define CLEAR_VOICE_HALF_INT *(volatile unsigned int *)0x40050038 |= BIT(8)
  74. #define CLEAR_VOICE_FULL_INT *(volatile unsigned int *)0x40050038 |= BIT(9)
  75. /*#define CLEAR_ADC_INT(n) *(volatile unsigned int *)0x40050038 |= BIT(n)
  76. #define IS_CLAER_ADC_INT_CH0 (*(volatile unsigned int *)0x4005003c) & BIT(0)
  77. #define IS_CLAER_ADC_INT_CH1 (*(volatile unsigned int *)0x4005003c) & BIT(1)
  78. #define IS_CLAER_ADC_INT_CH2 (*(volatile unsigned int *)0x4005003c) & BIT(2)
  79. #define IS_CLAER_ADC_INT_CH3 (*(volatile unsigned int *)0x4005003c) & BIT(3)
  80. #define IS_CLAER_ADC_INT_CH4 (*(volatile unsigned int *)0x4005003c) & BIT(4)
  81. #define IS_CLAER_ADC_INT_CH5 (*(volatile unsigned int *)0x4005003c) & BIT(5)
  82. #define IS_CLAER_ADC_INT_CH6 (*(volatile unsigned int *)0x4005003c) & BIT(6)
  83. #define IS_CLAER_ADC_INT_CH7 (*(volatile unsigned int *)0x4005003c) & BIT(7)*/
  84. #define IS_CLAER_VOICE_HALF_INT (*(volatile unsigned int *)0x4005003c) & BIT(8)
  85. #define IS_CLAER_VOICE_FULL_INT (*(volatile unsigned int *)0x4005003c) & BIT(9)
  86. //#define IS_CLAER_ADC_INT(n) (*(volatile unsigned int *)0x4005003c) & BIT(n)
  87. #define GET_IRQ_STATUS ((*(volatile unsigned int *) 0x4005003c) & 0x3ff)
  88. #define ENABLE_ADC (*(volatile unsigned int *)0x4000f048 |= BIT(3))
  89. #define DISABLE_ADC (*(volatile unsigned int *)0x4000f048 &= ~BIT(3))
  90. #define ADC_CLOCK_ENABLE (*(volatile unsigned int *)0x4000f044 |= BIT(13))
  91. #define ADC_CLOCK_DISABLE (*(volatile unsigned int *)0x4000f044 &= ~BIT(13))
  92. #define ADC_DBLE_CLOCK_DISABLE (*(volatile unsigned int *)0x4000f044 &= ~BIT(21))
  93. #define POWER_DOWN_ADC (*(volatile unsigned int *)0x4000f048 &= ~BIT(3))
  94. #define POWER_UP_TEMPSENSOR (*(volatile unsigned int *)0x4000f048 |= BIT(29))
  95. #define REG_IO_CONTROL ((volatile unsigned int *)0x4000f020)
  96. #define ADCC_REG_BASE (0x4000F000)
  97. // Voice encode mode
  98. typedef enum {
  99. VOICE_ENCODE_PCMA = 0,
  100. VOICE_ENCODE_PCMU = 1,
  101. VOICE_ENCODE_CVSD = 2,
  102. VOICE_ENCODE_BYP = 3
  103. }VOICE_ENCODE_t;
  104. // Voice sample rate
  105. typedef enum {
  106. VOICE_RATE_64K = 0,
  107. VOICE_RATE_32K = 1,
  108. VOICE_RATE_16K = 2,
  109. VOICE_RATE_8K = 3
  110. }VOICE_RATE_t;
  111. // Voice notch filter configuration
  112. typedef enum {
  113. VOICE_NOTCH_BYP = 0,
  114. VOICE_NOTCH_1 = 1,
  115. VOICE_NOTCH_2 = 2,
  116. VOICE_NOTCH_3 = 3
  117. }VOICE_NOTCH_t;
  118. // Voice polarity selection
  119. typedef enum {
  120. VOICE_POLARITY_POS = 0,
  121. VOICE_POLARITY_NEG = 1
  122. }VOICE_POLARITY_t;
  123. enum{
  124. HAL_VOICE_EVT_DATA = 1,
  125. HAL_VOICE_EVT_FAIL = 0xff
  126. };
  127. // Voice configuration structure
  128. typedef struct _voice_Cfg_t{
  129. bool voiceSelAmicDmic;
  130. GPIO_Pin_e dmicDataPin;
  131. GPIO_Pin_e dmicClkPin;
  132. uint8_t amicGain;
  133. uint8_t voiceGain;
  134. VOICE_ENCODE_t voiceEncodeMode;
  135. VOICE_RATE_t voiceRate;
  136. bool voiceAutoMuteOnOff;
  137. }voice_Cfg_t;
  138. // Voice event structure
  139. typedef struct _voice_Evt_t{
  140. int type;
  141. uint32_t* data;
  142. uint32_t size;
  143. }voice_Evt_t;
  144. typedef void (*voice_Hdl_t)(voice_Evt_t* pev);
  145. // Voice context structure
  146. typedef struct _voice_Contex_t{
  147. bool enable;
  148. voice_Cfg_t cfg;
  149. voice_Hdl_t evt_handler;
  150. }voice_Ctx_t;
  151. // Enable voice core
  152. void hal_voice_enable(void);
  153. // Disable voice core
  154. void hal_voice_disable(void);
  155. // Select DMIC
  156. void hal_voice_dmic_mode(void);
  157. // Select AMIC
  158. void hal_voice_amic_mode(void);
  159. // Open a GPIO pin for DMIC
  160. void hal_voice_dmic_open(GPIO_Pin_e dmicDataPin, GPIO_Pin_e dmicClkPin);
  161. // Set PGA gain for AMIC
  162. void hal_voice_amic_gain(uint8_t amicGain);
  163. // Set voice process gain
  164. void hal_voice_gain(uint8_t voiceGain);
  165. // Set voice encoding mode
  166. void hal_voice_encode(VOICE_ENCODE_t voiceEncodeMode);
  167. // Set voice data rate
  168. void hal_voice_rate(VOICE_RATE_t voiceRate);
  169. // Enable voice auto-mute
  170. void hal_voice_amute_on(void);
  171. // Disable voice auto-mute
  172. void hal_voice_amute_off(void);
  173. /**************************************************************************************
  174. * @fn hal_VOICE_IRQHandler
  175. *
  176. * @brief This function process for adc interrupt
  177. *
  178. * input parameters
  179. *
  180. * @param None.
  181. *
  182. * output parameters
  183. *
  184. * @param None.
  185. *
  186. * @return None.
  187. **************************************************************************************/
  188. // Voice interrupt handler
  189. void __attribute__((weak)) hal_ADC_IRQHandler(void);
  190. /**************************************************************************************
  191. * @fn hal_voice_init
  192. *
  193. * @brief This function process for adc initial
  194. *
  195. * input parameters
  196. *
  197. * @param ADC_MODE_e mode: adc sample mode select;1:SAM_MANNUAL(mannual mode),0:SAM_AUTO(auto mode)
  198. * ADC_CH_e adc_pin: adc pin select;ADC_CH0~ADC_CH7 and ADC_CH_VOICE
  199. * ADC_SEMODE_e semode: signle-ended mode negative side enable; 1:SINGLE_END(single-ended mode) 0:DIFF(Differentail mode)
  200. * IO_CONTROL_e amplitude: input signal amplitude, 0:BELOW_1V,1:UP_1V
  201. *
  202. * output parameters
  203. *
  204. * @param None.
  205. *
  206. * @return None.
  207. **************************************************************************************/
  208. // Allocate memory and power manager for voice
  209. void hal_voice_init(void);
  210. // Configure voice capture
  211. int hal_voice_config(voice_Cfg_t cfg, voice_Hdl_t evt_handler);
  212. // Start voice capture
  213. int hal_voice_start(void);
  214. // Stop voice capture
  215. int hal_voice_stop(void);
  216. // Clear memory and power manager for voice
  217. int hal_voice_clear(void);
  218. #endif