qipp
2025-09-16 e7ac407a6aa40e94a34a772dee14e8d5fb55c45b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <stdio.h>
#include <string.h>
#include <sru.h>
#include <alg.h>
#include <time.h>
#include <math.h>
#include <sysreg.h>
#include <alg.h>
#include "drv/global.h"
#include "drv/pll.h"
#include "drv/board_sru.h"
#include "drv/ad1939.h"
#include "drv/sport.h"
#include "drv/hmem.h"
#include "drv/spi.h"
#include "drv/fft.h"
#include "f2f.h"
#include "execute.h"
#include "queue.h"
#include "jitter.h"
#include "report.h"
 
extern double proc_secs;///////////////////////////////////////////////////////
extern void* hans,* haec, *hafc;
double cpu_get_process_ulitity(void);
 
#pragma section("scene_mem")
static   int mem_test = 0xefbcdda5;
 
 
//jitter_mem* pusb_input_jitter =0;
//jitter_mem* pusb_output_jitter =0;
 
 
static short cpu_cal_ulitily(void)
{    
    double aec_secs = alg_aec_get_ulitily(haec);
    double ans_secs = alg_ans_get_ulitily(hans);
    
    double total_secs;
    
    total_secs = proc_secs*7.5+ (aec_secs+ans_secs);
    proc_secs = 0 ;
    
    return (total_secs*100000/10);
 
}
 
void main()
{
 
    int i ;
    
    int led_on=0;
    int led_count=0;
    int loop_cnt=0;
    
    short value;
    short reply_status;
    unsigned short size;////////////////////////////////////////////////
    extern int clock_ok ;
    
    extern  int sp_flag ;
    extern  int inputcnt; 
    extern  int dma_count[2];
    extern int usb_dma_count;
    extern  void* htx_que;
    extern  int msg_process(void);
    extern  int dsp_index  ;
    extern int dsp_status;
    extern  int handshake_successful;
    extern int topo_state;
    
    int request_topo_count =0 ; //if no preset after handshare 8's, request one time.
    int clock_check_count =0;
    int first_scene_done = 0;
    int no_clock = 0;
    
    memset(dma_count, 0, sizeof(int)*2);
    
 
      initPLL_SDRAM();  //Initialize the PLL and SDRAM controller
          
    InitDAI();
    
    
    SRU(DPI_PB06_O, FLAG6_I);
    sysreg_bit_set(sysreg_FLAGS, FLG6);
    dsp_index = sysreg_bit_tst(sysreg_FLAGS , FLG6);
    
    //GPIO 
    SRU(HIGH, DPI_PBEN11_I); //0-busy,1-idle
    SRU(HIGH, DPI_PBEN12_I); //0-send, 1-recv
    SRU(LOW,DPI_PB11_I);
    SRU(LOW,DPI_PB12_I);
        
 
    SRU(HIGH,DPI_PBEN13_I);//led
    SRU(HIGH,DPI_PBEN14_I);
    for(i=0;i<3;i++){
        SRU(LOW,DPI_PB13_I);
        SRU(LOW,DPI_PB14_I);
        Delay(1000000);
        SRU(HIGH,DPI_PB13_I);
        SRU(HIGH,DPI_PB14_I);
        Delay(1000000);
    }
    
    
    hmem_init();
    //test();
    spi_init(DEVICE_DMA);
        
    htx_que = queue_create(SPI_PKT_SIZE*4);
 
    //sdram check help find problem
    if(mem_test != 0xefbcdda5){
        dsp_status = dsp_mem_err;
        device_status_report(DSP_RUNNING_STATUS,(short*)&dsp_status ,1);
        msg_report_dsp_status(MSG_DSP_STATUS_REPORT);
        
        //֪ͨMCUÆô¶¯Íê³É
        SRU(HIGH,DPI_PB11_I);
        SRU(HIGH,DPI_PB12_I);
        while(1){
            loop_cnt++;
            if(loop_cnt>=1000000){
                if(led_on){
                    SRU(HIGH,DPI_PB13_I);
                }
                else{
                    SRU(LOW,DPI_PB13_I);
                }    
                led_on = !led_on;
                loop_cnt =0;    
            }    
            msg_process();
        }
    }
    
    alg_lib_init();
    
    output_empty();
 
    
    //InitSRC();
    //InitSPORT(0x3,RATE_SAMPLES);
    
    //interruptcb(SIG_SP1, sport_isr);
    //interruptcb(SIG_SP3, sport3_isr);
    //interruptcb(SIG_SP5, sport_usb_isr);
 
    //debug״̬ÇëÇóMCUÖØÐ·¢ËÍÊý¾Ý
    device_status_report(DSP_EMULATE_DEBUG,0, 0);
    msg_report_dsp_status(MSG_DSP_STATUS_REPORT);
    
    
    //֪ͨMCUÆô¶¯Íê³É
    SRU(HIGH,DPI_PB11_I);
    SRU(HIGH,DPI_PB12_I);
        
    #define LED_PERIOD (1000*RATE_SAMPLES/(NUM_SAMPLES*1000))
 
    //main loop 
    while(1){
        if(haec != NULL ){
            alg_aec_process_asy(haec);
        }
        if(hans != NULL){
            alg_ans_process_asy(hans);
        }    
        
        msg_process();
            
 
        if(dma_count[0] - led_count >LED_PERIOD){
            if(led_on){
                SRU(LOW,DPI_PB13_I);
            }
            else{
                SRU(HIGH,DPI_PB13_I);
            }
 
            size =  hmem_perused(SRAM_AUTO);        
            device_status_report(DSP_MEM_UTILITY,(short*)&size, 1);
            
            value = cpu_cal_ulitily();
            device_status_report(DSP_CPU_UTILITY,&value , 1);
            
            reply_status = no_clock ? dsp_clock_ok : dsp_status;
            device_status_report(DSP_RUNNING_STATUS,(short*)&reply_status ,1);
            
            //do not use this function to other place .only here.
            msg_report_dsp_status(MSG_DSP_STATUS_REPORT);
            
            no_clock = 0;
            led_on=!led_on;
            led_count = dma_count[0];
        }
        
        //8s request
        if(clock_ok && dma_count[0] - request_topo_count > (8*(RATE_SAMPLES/NUM_SAMPLES))){
            if(topo_state == PRESET_NONE) {
                device_status_report(DSP_SCENE_REQUEST,0 ,0);    
            }
            else if(topo_state == PRESET_DONE && !first_scene_done){
                device_status_report(DSP_SCENE_DONE,0 ,0);    
                topo_state = PRESET_NORMAL;    
                first_scene_done = 1;
            }
            else if(topo_state == PRESET_DONE){ 
                //If the scene completion response is continuously reported, it will cause the ADC to keep resetting.
                //device_status_report(DSP_SCENE_DONE,0 ,0);    
                topo_state = PRESET_NORMAL;
            }
            request_topo_count = dma_count[0];    
        }
 
        //clock state will change after running,it need check always.
        //about 2.5s
        if(++loop_cnt>10000000){
            if(clock_ok && !no_clock && clock_check_count == dma_count[0]) {
                dsp_status = dsp_no_clock;
                device_status_report(DSP_RUNNING_STATUS,(short*)&dsp_status ,1);
                msg_report_dsp_status(MSG_DSP_STATUS_REPORT);
                
                no_clock = 1;
            }
            
            loop_cnt =0;
            clock_check_count = dma_count[0];    
        }
 
        if(clock_ok == 0 && dsp_status==dsp_running_ok && handshake_successful){
            //DSP²úÉúʱÖÓÉϱ¨MCUÕý³£ÔË×÷
            device_status_report(DSP_CLOCK_STATUS,0 ,0 );
            
            request_topo_count = dma_count[0];
            clock_ok = 1;    
        }
    }
}