chenlh
2025-09-18 345edc823540089925b8ae60af34404d608fdfb4
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
/*
 * main.cpp
 *
 *  Created on: 2025Äê7ÔÂ22ÈÕ
 *      Author: 86189
 */
#include <stdlib.h>
#include <stdio.h>
#include <sys/platform.h>
#include <sys/adi_core.h>
#include <ctime>
#include <heapnew>
#include <string.h>
#include <matrix.h>
#include "adi_initialize.h"
#include "../drv/gpio.h"
#include "../drv/spi.h"
#include "config.h"
#include "../drv/board.h"
#include "../drv/sport.h"
#include "../drv/linkport.h"
#include "../drv/memory.h"
#include "../drv/FIR_acc.h"
#include "messageproc.h"
#include "ModuleProAPI.h"
#include "queue.h"
#include "f2f.h"
#include "var_state.h"
#include "tob.h"
#include "tg/module_def.h"
#include "tg/tg_scene.h"
 
//È«¾Ö±äÁ¿
#define MAX_INTR 100
static u32 DMACount[MAX_INTR];
static ufloat asyn_proc_secs;
static ufloat proc_secs;
 
ufloat cpu_utility()
{
    ufloat total_secs ;
 
    total_secs = asyn_proc_secs + proc_secs*(SAMPLE_RATE*0.01f)/SAMPLE_NUM ;//
 
    return total_secs*1000*100/10;
}
 
inline uvoid blink_led(GPIO_Level level)
{
    GPIO_SetOutPut(GPIOB, GPIO_Pin10, level);
}
 
 
/*
 * 128 Â·
UpdateInput && UpdateOutput 7.37%
ÔÙ¼Ó128 Â·Dummy input ºÍoutput. ¹²12.370000%
 * */
extern "C" uvoid AudioProcCallBack (u32 iid)
{
    ToB* tob = ToB::GetInstance();
    ufloat clock_start,clock_stop;
    VarState& var = VarState::GetInstance();
 
    if(var.master_intr == iid){
        var.dsp_status = DSPStatus::dsp_running_ok;
    }
    if(tob == NULL) return ;
 
    if(var.TopoLoading){
        MuteOutput();
    }
    else if(var.master_intr == iid){
        clock_start = clock();
        UpdateInput(iid, DMACount[iid]&0x1);
        tob->toProc();
 
        UpdateOutput(iid, DMACount[iid]&0x1);
//        LP_transmit();
        ModulesAsynSetInterval(proc_secs);
        fir_acc_startup();
        clock_stop = clock();
        proc_secs = ((ufloat) (clock_stop - clock_start))/ CLOCKS_PER_SEC;
    }
    else{
        UpdateInput(iid, DMACount[iid]&0x1);
        UpdateOutput(iid, DMACount[iid]&0x1);
    }
 
    ++DMACount[iid];
}
 
int main(int argc, char *argv[])
{
    s32 result ;
    s32 total_mem = space_unused();
    s16 cpu,mem;
    s32 request_topo_count =0 ; //if no preset after handshare 8's, request one time.
    s32 clock_check_count =0;
 
     s32 ledOn = 0;
     uint32_t ledCount = 0;
    uint32_t levelCount = 0;
    u32 loop_cnt =0;
    MSG msg;
    VarState& var = VarState::GetInstance();
 
    adi_initComponents();
 
    spu_config();
 
    DDR_config();
 
    pinmux_config();
 
    sram_init();
    DAI_config();
 
    memset(DMACount, 0, sizeof(DMACount));
 
    Message* spiMsg = new Message(8);
 
    SPI2_Init();
 
    fir_acc_init();
 
    //֪ͨarmÆô¶¯Íê³É.
    //dir: pin12, High level DSP can receive data, low level DSP has data toz send .
    //busy: pin13/PB5, low level DSP is in busy state and cannot receive and send data, high level is free.
    GPIO_SetOutPut(GPIOA, GPIO_Pin12, GPIO_HIGH);
    GPIO_SetOutPut(GPIOB, GPIO_Pin5, GPIO_HIGH);
 
    //µÚÒ»´ÎÆô¶¯Éϱ¨ÌرðÊÇ·ÂÕæµÄʱºòÐèÒªarm'ÖØÐ·¢ÎÕÊÖ.
    spiMsg->DspStatusPush(DSP_EMULATE_DEBUG, 0, 0);
    spiMsg->ReportDspStatus(&msg);
 
    printf("free sram %d, L2 %d\n", sram_free_space(SRAM_DDR, mem_any), sram_free_space(SRAM_L2, mem_any));
 
    while(1) {
        asyn_proc_secs = ModulesAsynProcess();
 
        spiMsg->Proc();
 
        if(DMACount[var.master_intr] - ledCount > (SAMPLE_RATE/SAMPLE_NUM)) {
            blink_led((GPIO_Level)ledOn);
            ledOn = 1-ledOn;
            ledCount = DMACount[var.master_intr];
 
            cpu =  cpu_utility()*100; //all modules execute time.;
            mem = (1.0f - (float)space_unused()/total_mem)*10000;
 
            spiMsg->DspStatusPush(DSP_MEM_UTILITY, &mem, 1);
            spiMsg->DspStatusPush(DSP_CPU_UTILITY, &cpu, 1);
            spiMsg->DspStatusPush(DSP_RUNNING_STATUS,&var.dsp_status ,1);
 
            spiMsg->ReportDspStatus(&msg);
        }
 
        //if no preset , 8s request re-send.
        if(var.clock_ok && DMACount[var.master_intr] - request_topo_count > (8*(SAMPLE_RATE/SAMPLE_NUM))){
            if(var.TopoStatus == PRESET_STA::PRESET_NONE) {
                spiMsg->DspStatusPush(DSP_SCENE_REQUEST,0 ,0);
            }
            else if(var.TopoStatus == PRESET_STA::PRESET_DONE){
                spiMsg->DspStatusPush(DSP_SCENE_DONE,0 ,0);
                var.TopoStatus = PRESET_STA::PRESET_NORMAL;
            }
            request_topo_count = DMACount[var.master_intr];
        }
 
        //clock state will change after running,it need check always.
        //about 1s
        if(++loop_cnt > 5e+6){
            if (var.clock_ok && clock_check_count == DMACount[var.master_intr]) {
                var.dsp_status = dsp_no_clock;
                spiMsg->DspStatusPush(DSP_RUNNING_STATUS, &var.dsp_status ,1);
                spiMsg->ReportDspStatus(&msg);
                printf("clk0 ok\n");
            }
            loop_cnt =0;
            clock_check_count = DMACount[var.master_intr];
//            printf("clk1 ok\n");
        }
 
        if(!var.clock_ok && var.dsp_status == DSPStatus::dsp_running_ok && var.HandShakeSuccesful) {
            //notify arm to work normaly.
            spiMsg->DspStatusPush(DSP_CLOCK_STATUS, &var.dsp_status, 1);
            request_topo_count = DMACount[var.master_intr] ;
            var.clock_ok = utrue;
            spiMsg->ReportDspStatus(&msg);
            printf("clk2 ok\n");
        }
 
 
    }
 
}