1 /*
2  * ==========================================================
3  *
4  *    Copyright (C) 2020 QuickLogic Corporation
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  * 		http://www.apache.org/licenses/LICENSE-2.0
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  *    File      : eoss3_hal_audio.h
16  *    Purpose :
17  *
18  * ===========================================================
19  *
20  */
21 
22 #ifndef __AUDIO_CORE_H_
23 #define __AUDIO_CORE_H_
24 #include <stdbool.h>
25 
26 #include "eoss3_dev.h"
27 
28 #ifndef PDM2DEC_FACT
29 #define PDM2DEC_FACT 32
30 #endif
31 
32 //PDM Core Config Registers Setting
33 #define PDM_MODE_SEL            0       // 0=mono, 1=stereo
34 #define PDM_MONO_CHN_SEL        0       // 0=left, 1=right if in mono mode
35 //#define DEFAULT_PDM_PGA_L_VAL   8
36 //#define DEFAULT_PDM_PGA_R_VAL   8
37 #define DEFAULT_SOFT_MUTE       0
38 
39 #if     PDM2DEC_FACT==64
40 
41 #define  PDM2PCM_PGA_SYNC_RATE          (64)
42 #define  PDM2PCM_PGA_GAIN_VAL           (6)
43 #define  PDM2PCM_MCLKDIV_VAL            (0)
44 #define  PDM2PCM_CLK_C30                (HSOSC_2MHZ)
45 #define  PDM2PCM_CLK_C31                (HSOSC_512KHZ)
46 
47 #elif   PDM2DEC_FACT==48
48 
49 #define  PDM2PCM_PGA_SYNC_RATE          (48)
50 #define  PDM2PCM_PGA_GAIN_VAL           (8)
51 #define  PDM2PCM_MCLKDIV_VAL            (1)
52 #define  PDM2PCM_CLK_C30                (HSOSC_3MHZ)
53 #define  PDM2PCM_CLK_C31                (HSOSC_768KHZ)
54 
55 #elif   PDM2DEC_FACT==32
56 
57 #define  PDM2PCM_PGA_SYNC_RATE          (32)
58 #define  PDM2PCM_PGA_GAIN_VAL           (16)
59 #define  PDM2PCM_MCLKDIV_VAL            (1)
60 #define  PDM2PCM_CLK_C30                (HSOSC_2MHZ)
61 #define  PDM2PCM_CLK_C31                (HSOSC_512KHZ)
62 
63 #else
64 #error "PDM2DEC_FACT is not defined "
65 #endif
66 
67 #define PDM2PCM_DISABLE_HPF           (0)
68 #if PDM2PCM_DISABLE_HPF == 0
69 #define PDM2PCM_HPF_HPGAIN            (5)
70 #endif
71 //PDM Voice Config Registers Setting
72 enum VOICE_CONFIG_SCENARIO {
73   PDM_VOICE_SCENARIO1 = 0,
74   PDM_VOICE_SCENARIO2 = 1,
75   PDM_VOICE_SCENARIO3_MODE1 = 2,
76   PDM_VOICE_SCENARIO3_MODE2 = 3,
77   PDM_VOICE_SCENARIO3_MODE3 = 4
78 };
79 
80 /** @brief Enumerators for audio errors */
81 typedef enum {
82   AUDIO_ISR_EVENT_NO_BUFFER, ///< ISR could not obtain new buffer from FreeQ
83 } audio_event_types_t;
84 
85 #define NO_SWITCH 0
86 #define SWITCH_TO_AP 1
87 
88 #define DEFAULT_PDM_VOICE_SCENARIO PDM_VOICE_SCENARIO1
89 #define DEFAULT_PDM_MIC_SWITCH_TO_AP NO_SWITCH
90 
91 #define AUDIO_ISR_MESSAGE  ( 0x0ff & ('A' + 'I' + 'S' + 'R') )
92 #define AUDIO_TASK_MESSAGE  ( 0x0ff & ('T' + 'A' + 'S' + 'K') )
93 
94 
95 struct connector {
96   bool initialized;
97   bool started;
98   void (*init)();
99   void (*start)();
100   void (*stop)();
101 };
102 
103 typedef void* audio_handle_t;
104 
105 /*
106 * DigitalMic type
107 */
108 typedef enum DigitalMic {
109   DigitalMic_I2S,
110   DigitalMic_PDM,
111   DigitalMic_PDM_VoiceIQ
112 } DigitalMic_t;
113 
114 /*
115 * Event Notifier types
116 */
117 typedef enum event_type {
118   HAL_Audio_Event_Start,
119   HAL_Audio_Event_LPSD_ON,
120   HAL_Audio_Event_LPSD_OFF,
121   HAL_Audio_Event_DMA_BLOCK_DONE,
122   HAL_Audio_Event_DMA_BUFFER_DONE,
123   HAL_Audio_Event_End
124 } HAL_Audio_Event_type_t;
125 
126 /*
127 * Get a handle for audio core by passing digital mic type.
128 * Use this handle in all future calls.
129 */
130 audio_handle_t HAL_Audio_GetHandle(DigitalMic_t micType);
131 
132 /*
133 * Event Notifier prototype
134 * Application can define this function and pass to HAL_Audio_Init API to get event callbacks.
135 */
136 typedef void HAL_Audio_Event_Notifier_t(HAL_Audio_Event_type_t event_type, void *p_event_data);
137 
138 /*
139 * Initialize audio core.
140 * 	Turns on audio core
141 * 	Sets all registers of audio core and other components such as CRU
142 * 	Turns on interrupts at NVIC and M4 level, but audio core level interrupts are turned off
143 */
144 void HAL_Audio_Init(audio_handle_t handle, HAL_Audio_Event_Notifier_t *pevent_notifier);
145 
146 /**
147 * Starts audio core.
148 * 	Enable the audio interrupts at audio core to stop audio capture
149 */
150 void HAL_Audio_Start(audio_handle_t handle);
151 
152 /*
153 * Stops audio core.
154 * 	Disable the audio interrupts at audio core to stop audio capture
155 */
156 void HAL_Audio_Stop(audio_handle_t handle);
157 
158 /**
159 * Starts Audio DMA
160 */
161 void HAL_Audio_StartDMA( void );
162 
163 /**
164 * Stops Audio DMA
165 */
166 void HAL_Audio_StopDMA( void );
167 
168 void release_data_block_prev(void);
169 
170 /**
171 * Set LPSD module state
172 * value : 1 to enable, 0 to disable
173 */
174 void HAL_Audio_Set_LPSDMode(uint8_t value);
175 
176 // Audio Related isrs
177 void HAL_Audio_ISR_LpsdOn();
178 void HAL_Audio_ISR_LpsdOff();
179 void onDmicOn();
180 void onDmicOff();
181 void onDmac0BlockDone();
182 void onDmac0BufferDone();
183 void HAL_Audio_Set_LPSDMode(uint8_t value);
184 void handle_led_for_lpsd_on(void);
185 void handle_led_for_lpsd_off(void);
186 void handle_led_for_audio_stop(void);
187 /** User application should define and instantiate the following structure */
188 extern outQ_processor_t audio_isr_outq_processor ;
189 
190 void display_num_drop_count(void);
191 void reset_num_drop_count(void);
192 
193 #endif /* __AUDIO_CORE_H_ */
194 
195