qipp
2025-11-07 1db34b4e65f72d8ae8b1f0efbc7a4395f9bbcae8
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * messageproc.c
 *
 *  Created on: 2021Äê11ÔÂ1ÈÕ
 *      Author: graydon
 */
#include <string.h>
#include <stdlib.h>
#include <sru.h>
#include <def1802.h>
#include <cdef1802.h>
#include <heapnew>
#include "messageproc.h"
#include "../drv/spi.h"
#include "../drv/memory.h"
#include "queue.h"
#include "tob.h"
#include "F2F.h"
//#include "../drv/sru_dai.h"
#include "../drv/fir_acc.h"
#include <time.h>
//#include "convert.h"
#include "protocol.h"
#include "scene.h"
#include "ParamCtrl.h"
 
 
s32 Message::Send(MsgType msg_type, uvoidptr buffer, u32 len)
{
    MSG msg;
 
    msg.Enc(msg_type, buffer, len);
    return txQueue->Push(msg);
}
 
s32 Message::Send(MSG* pmsg )
{
    return txQueue->Push(*pmsg);
}
 
 
s32 Message::LevelProcess(MSG* pmsg)
{
    ToB* tob = ToB::GetInstance();
    if(tob ) {
        s16 buffer[1600];
        u32 n = tob->GetLevels(buffer);
        u32 data_len ;
        u32 total_pkts = (n + SPI_DATA_LEN-1)/SPI_DATA_LEN;
 
        //for(u32 i =0 ;i < n;i++) buffer[i] = i;
        pmsg->totalPkts = total_pkts;
        pmsg->msgIDLSB = n;
        pmsg->msgIDMSB = 0;
 
        for(u32 i=0; i< total_pkts; i++){
            if(i == total_pkts-1){
                data_len = n - i*SPI_DATA_LEN;
            }
            else{
                data_len = SPI_DATA_LEN;
            }
 
            pmsg->pktNo = i;
            pmsg->Enc(MsgType::MSG_LEVEL_GET_REQ, buffer+i*SPI_DATA_LEN, data_len<<1);
            Send(pmsg) ;
        }
    }
    return 0;
}
 
s32 Message::PresetProcess(MSG* pmsg)
{
//    static u8* bin = NULL;
//    extern ubool  TopoLoading;
//
//    if(pmsg->pktNo == 0 && bin != NULL ) {
//        DDRFree(bin);
//        bin = NULL;
//        printf("PresetProcess pmsg->pktNo\n");
//    }
//    if(pmsg->pktNo == 0 && bin == NULL) {
//        bin = (u8*)DDRMalloc(pmsg->totalPkts*MSG_DATA_LEN);
//        dbg_printf("preset address %p.\n", bin);
//    }
//    if(bin == NULL) {
//        printf("PresetProcess\n");
//        return -1;
//    }
//    memcpy(bin+pmsg->pktNo*MSG_DATA_LEN, pmsg->data , pmsg->dataLen);
//    if(pmsg->pktNo == pmsg->totalPkts -1) {
//        ToB* tob = ToB::GetInstance();
//        u32 size = (pmsg->totalPkts -1)*MSG_DATA_LEN+pmsg->dataLen;
//
//        if(tob) tob->toCtrl(bin, size);
//
//        DDRFree( bin);
//        bin = NULL;
//        TopoLoading = ufalse;
//        Send(MsgType::MSG_PRESET_RES, 0 ,0) ;
//    }
 
    return 0;
}
 
s32 Message::PresetProcess(MSG* pmsg, int pad)
{
    extern ubool  TopoLoading;
    s32 ret = 0;
 
    ToB* tob = ToB::GetInstance();
    tob->SetMute(utrue);
    TopoLoading = utrue;
 
    scene_cpy(pmsg->pktNo,(void*)pmsg->data,pmsg->dataLen);
 
    if(pmsg->pktNo == pmsg->totalPkts -1) {
        ret = TopoProcess();
 
        TopoLoading = ufalse;
 
 
        Send(MsgType::MSG_PRESET_RES, (u8*)&ret ,1) ;
    }
 
    return 0;
}
 
s32 Message::TopoProcess(MSG* pmsg)
{
//    extern ubool  TopoLoading;
//    extern uvoid SetNumOfChannels(u16 rxNum, u16 txNum);
//    static u8* bin = NULL;
//    ToB* tob = ToB::GetInstance();
//    u32 size =0;
//
//    tob->SetMute(utrue);
//    TopoLoading = utrue;
//
//    if(pmsg->pktNo == 0 && bin != NULL ) {
//        DDRFree(bin);
//        bin = NULL;
//    }
//    if(pmsg->pktNo == 0 && bin == NULL) {
//        bin = (u8*)DDRMalloc(pmsg->totalPkts*MSG_DATA_LEN);
//        size =0 ;
//    }
//    if(bin == NULL) return -1;
//
//    memcpy(bin+pmsg->pktNo*MSG_DATA_LEN, pmsg->data , pmsg->dataLen);
//    if(pmsg->pktNo == pmsg->totalPkts -1) {
//        size = (pmsg->totalPkts -1)*MSG_DATA_LEN+pmsg->dataLen;
//        SetNumOfChannels(0, 0);
//        if(tob) {
//            fir_acc_delete();
//            tob->toClear();
//            tob->CreateModule(bin, size);
//            fir_acc_startup();
//        }
//        DDRFree( bin);
//        bin = NULL;
//
//        Send(MsgType::MSG_TOPO_RES, 0 ,0) ;
//    }
 
    return 0;
}
 
s32 Message::TopoProcess()
{
    extern ubool  TopoLoading;
    extern uvoid SetNumOfChannels(u16 rxNum, u16 txNum);
    char *bin = (char*)DDRMalloc(8*1024);//new char[8*1024];//
    int size = 0;
    int scene_size = 0;
 
    ToB* tob = ToB::GetInstance();
 
    //tob->SetMute(utrue);
    //TopoLoading = utrue;
 
    if(bin == NULL) return -1;
 
    scene_type_update();
 
    SetNumOfChannels(0, 0);
    if(tob) {
        fir_acc_delete();
        tob->toClear();
        SceneToDSPFlowchart(bin, &size);
        tob->CreateModule((u8*)bin, size,NULL);
        fir_acc_startup();
    }
 
    DDRFree(bin);
    //delete[] bin;
    bin = NULL;
 
    return scene_size;
}
 
s32 Message::ParamCtrl(MSG* pmsg)
{
    ToB* tob = ToB::GetInstance();
 
    if(!ConfiGurable){
        ptag_cmd pcmd = (ptag_cmd)pmsg->data;
        if(tob != NULL){
            tob->toCtrl(pcmd->proc_id, pcmd->cmd, pcmd->val);
            dbg_printf("proc_id %d .\n", pcmd->proc_id);
        }
 
        return 0;
    }
 
 
//    if(pmsg->totalPkts > 1) {
//        static s16* bin =  NULL;
//
//        if(pmsg->pktNo == 0 && bin != NULL) {
//            delete[] bin; bin = NULL;
//        }
//        if(bin == NULL) {
//            bin = new s16[pmsg->totalPkts*MSG_DATA_LEN];
//        }
//        memcpy(bin+ pmsg->pktNo*MSG_DATA_LEN, pmsg->data , pmsg->dataLen);
//        dbg_printf("ParamCtrl %d(%d).\n", pmsg->totalPkts,pmsg->pktNo);
//        if(pmsg->pktNo == pmsg->totalPkts-1) {
//            Param * ptr = (Param *)bin;
//            s16 * data = (s16*)(bin+sizeof(Param));
//
//            if(tob != NULL){
//                tob->toCtrl(ptr->mID, ptr->pID, data, ptr->num);
//                dbg_printf("PID %d num %d.\n", ptr->pID,ptr->num);
//            }
//            delete[] bin; bin = NULL;
//        }
//    }
//    else {
//        Param * ptr = (Param *)pmsg->data;
//        s16 * data = (s16*)(pmsg->data+sizeof(Param));
//
//        if(tob != NULL){
//            tob->toCtrl(ptr->mID, ptr->pID, data, ptr->num);
//            dbg_printf("PID %d num %u.\n", ptr->pID, ptr->num);
//        }
//    }
 
    return 0;
}
 
#if 0
s32 Message::SystemConfigSetProcess(MSG* pmsg)
{
    extern ubool  HandShakeSuccesful;
    extern u16 mDspIndex;
    DSPConfig* conf = (DSPConfig*)pmsg->data;
 
    if(HandShakeSuccesful == ufalse) {
        SAMPLE_NUM = conf->mSampleNum;
        SAMPLE_RATE = (conf->mSampleRateH<<16)|conf->mSampleRateL;
 
        mDspIndex = conf->mDspIndex;
        new ToB(conf->mDualDsp , conf->mDspIndex);
 
        RouteConfig(conf->routes,conf->mRouteNum);
        SportsConfig(conf->sports);
        SRCsConfig(conf->srcs);
        PCGsConfig(conf->pcgs);
 
        SetCh(conf->mLocalRxNum, conf->mLocalTxNum, conf->mDanteRxNum, conf->mDanteTxNum);
        HandShakeSuccesful = utrue;
    }
 
    Send(MsgType::MSG_HANDSHAKE_RES, 0 , 0);
 
    return 0;
}
#else
s32 Message::SystemConfigSetProcess(MSG* pmsg)
{
    extern ubool  HandShakeSuccesful;
    extern u16 mDspIndex;
 
    extern int input_num_channels;
    extern int output_num_channels;
 
    extern int local_input_channels_num;
    extern int local_output_channels_num;
    extern int dante_input_channels_num;
    extern int dante_output_channels_num;
 
    if(HandShakeSuccesful == ufalse) {
        ptag_hwconf p = (ptag_hwconf)pmsg->data;
 
        local_input_channels_num = p->local_rx_num;
        local_output_channels_num = p->local_tx_num;
        dante_input_channels_num = p->dante_rx_num;
        dante_output_channels_num = p->dante_tx_num;
 
        input_num_channels = p->local_rx_num + p->dante_rx_num;
        output_num_channels = p->local_tx_num + p->dante_tx_num;
 
        p->scene_size = sizeof(tag_parameters);
 
        mDspIndex = p->dsp_index;
        new ToB(p->dual_dsp, p->dsp_index);
 
        Audio_Config();
 
        HandShakeSuccesful = utrue;
    }
 
    Send(MsgType::MSG_HANDSHAKE_RES, 0 , 0);
 
    return 0;
}
#endif
 
s32 Message::PushDSPStatus(u16 ID, s16 value)
{
    if(mDevStatusNum < 32) {
        mDevStatus[mDevStatusNum].ID = ID;
        mDevStatus[mDevStatusNum].value[0] = value;
        mDevStatusNum++ ;
    }
 
    return 0;
}
s32 Message::DSPStatusProcess(MSG* pmsg)
{
    s16* data = (s16*)pmsg->data;
    u32 n = mDevStatusNum*sizeof(DeviceStatus);
 
    if( n > 0) {
        memcpy(data , mDevStatus , n);
        pmsg->Enc(MsgType::MSG_DSP_STATUS_RES, 0 , n*2);
        mDevStatusNum = 0;
        Send(pmsg);
    }
    return n;
}
 
s32 Message::RxMessageHandler(MSG* pmsg)
{
    if(pmsg->magic != 0x5aa5) {
        return -1;
    }
 
    pmsg->dataLen = (pmsg->dataLen>>1);
    switch(pmsg->msgType&0xff){
    case MsgType::MSG_LEVEL_GET_REQ:
        LevelProcess(pmsg);
        break;
    case MsgType::MSG_PRESET_REQ:
        dbg_printf("preset %d(%d).\n", pmsg->totalPkts ,pmsg->pktNo);
        //if(ConfiGurable) PresetProcess(pmsg);
        break;
    case MsgType::MSG_TOPO_REQ:
        dbg_printf("topo %d(%d).\n", pmsg->totalPkts ,pmsg->pktNo);
        if(ConfiGurable) TopoProcess(pmsg);
        else PresetProcess(pmsg,0);
        break;
    case MsgType::MSG_PARMA_CTRL_REQ:
        ParamCtrl(pmsg);
        break;
    case MsgType::MSG_HANDSHAKE_REQ:
        SystemConfigSetProcess(pmsg);
        break;
    case MsgType::MSG_CHANNEL_COPY_REQ:
        scene_channel_copy((ptag_channel_copy)pmsg->data);
        break;
    case MsgType::MSG_DSP_STATUS_REQ:
        DSPStatusProcess(pmsg);
        break;
    default:
        break;
    }
 
    return 0;
}
 
uvoid Message::Proc()
{
    spi_status_e spi_status = spi_get_status();
 
    if(spi_status == spi_completed){
        //ÅжÏÊǽÓÊÕ»¹ÊÇ·¢ËÍÍê³É
        spi_direction_e spi_dir = spi_get_direction();
        MSG* spi_buffer = (MSG*)spi_get_buffer();
 
        if(spi_dir == spi_rx){
            //memcpy(&msg, spi_buffer, 512);
            RxMessageHandler(spi_buffer);
        }
 
        //¸ù¾Ý·¢ËͶÓÁÐÅжÏÏÂÒ»°üÅäÖ÷¢ËÍ»¹ÊǽÓÊÕ
        if(txQueue->Count() > 0){
            txQueue->Pop(*spi_buffer);
            spi_config_dma(spi_tx);
            SRU(LOW,DPI_PB12_I);
        }
        else{
            spi_config_dma(spi_rx);
            SRU(HIGH,DPI_PB12_I);
        }
        SRU(HIGH,DPI_PB11_I);//֪ͨMCU SPI¿ÉÓÃ
    }
}