chenlh
2026-01-20 fe4d335b54ede7a47fd4bcf5c228fb427cbcc8c9
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
/*
 * messageproc.c
 *
 *  Created on: 2021年11月1日
 *      Author: graydon
 */
#include <string.h>
#include <stdlib.h>
#include <SRU.h>
#include "messageproc.h"
#include "../drv/spi.h"
#include "../drv/gpio.h"
 
#include "F2F.h"
#include "ModuleProAPI.h"
#include "tg/tg_adapter.h"
#include "tg/tg_scene.h"
#include "var_state.h"
#include "tob.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 )
{
    u32 ret=  txQueue->Push(*pmsg);
    return ret;
}
 
uvoid Message::ReportDspStatus(MSG* pmsg)
{
    s32 size = dsp_status_q.get_device_status_ptr(pmsg->data, MSG_DATA_LEN);
    if(size > 0) {
        pmsg->Enc(MsgType::MSG_DSP_STATUS_REPORT, 0, size);
        txQueue->Push(*pmsg);
    }
}
 
uvoid Message::ReportLevel(MSG* pmsg)
{
    int i;
    int data_len;
 
    ToB* tob = ToB::GetInstance();
    u16 LevelCnt = tob->GetLevels(Levels, levels_max_len);    //1688 levels
    u32 msgLen = LevelCnt * sizeof(*Levels);
    int Packegs = (msgLen+MSG_DATA_LEN-1)/MSG_DATA_LEN;
 
    pmsg->magic = 0x5aa5;
//    pmsg->dataLen = LevelCnt;
    pmsg->totalPkts = Packegs;
//    pmsg->pktNo = 1;
    pmsg->msgType = MSG_GET_LEVEL;
    pmsg->msgID = LevelCnt;
 
    // Split packeg
    for(i=0; i<Packegs; i++){
        if(i == Packegs-1){
            data_len = msgLen - i * MSG_DATA_LEN;
        }
        else{
            data_len = MSG_DATA_LEN;
        }
 
        memcpy(pmsg->data, Levels + i * MSG_DATA_LEN / sizeof(*Levels), data_len);
        pmsg->dataLen = data_len;
        pmsg->pktNo = i;
 
        pmsg->Enc(MsgType::MSG_GET_LEVEL, 0, data_len);
        txQueue->Push(*pmsg);
    }
}
 
s32 Message::ParamCtrl(MSG* pmsg)
{
#define PCTL_MAX_PKT 5
#pragma section("seg_ext_pmda")
    static s8 buffer[MSG_DATA_LEN*PCTL_MAX_PKT];
    struct ParamCtrl * ptr = (struct ParamCtrl *)buffer;
    ToB* tob = ToB::GetInstance();
 
    if(pmsg->totalPkts > PCTL_MAX_PKT || pmsg->pktNo >= pmsg->totalPkts) {
        return -1 ;
    }
    memcpy(buffer + pmsg->pktNo*MSG_DATA_LEN , pmsg->data, pmsg->dataLen);
 
    if(tob != NULL && pmsg->pktNo == pmsg->totalPkts - 1) {
        VarState& var = VarState::GetInstance();
//        u32 type = tob->GetModuleType(ptr->mid);
        s32 data_num = (pmsg->totalPkts-1)*MSG_DATA_LEN+pmsg->dataLen;
 
        s16* data = (s16*)(ptr + 1);
 
        data_num = (data_num - sizeof(struct ParamCtrl)) / sizeof(s16);
//        ptr->mid = var.pscene->get_module_id(ptr->mid, type, ptr->cmd) ;
//        dbg_printf("mID:%d pID:%d val[0]:%d val[1]:%d\n",ptr->mid, ptr->cmd, data[0], data[1]);
        tob->toCtrl(ptr->mid, ptr->cmd, data, data_num);
    }
 
    return 0;
}
 
 
s32 Message::PresetProcess(MSG* pmsg)
{
    VarState& var = VarState::GetInstance();
    static u8* bin = NULL;
 
    if(pmsg->pktNo == 0 && bin != NULL ) {
        sram_free(SRAM_DDR, bin);
        bin = NULL;
    }
 
    if(pmsg->pktNo == 0 && bin == NULL) {
        bin = (u8*)sram_malloc(SRAM_DDR, mem_any, pmsg->totalPkts*MSG_DATA_LEN);
    }
    if(bin == NULL) return -1;
//    dbg_printf("No:%d len %d\n", pmsg->pktNo,pmsg->dataLen);
    var.TopoLoading = utrue;
    memcpy(bin+pmsg->pktNo*MSG_DATA_LEN, pmsg->data , pmsg->dataLen);
 
    if(pmsg->pktNo == pmsg->totalPkts -1) {
        u32 size = (pmsg->totalPkts -1)*MSG_DATA_LEN+pmsg->dataLen;
 
        if(var.pscene) {
            ToB* tob = ToB::GetInstance();
 
            SetTxRxNullBufferPtr();
            for(s32 i =0 ;i < IntDataType::COUNT;i++){
                SetNumOfChannels(static_cast<IntDataType>(i) , 0, 0);
            }
            if(var.pscene->set_parameters_content(bin, size) != 0){
                //fail
                var.TopoStatus = PRESET_STA::PRESET_ERR;
            }
            else if(tob){
                int preset_size = sizeof(tag_parameters); // 45128 + sizeof(tag_fir) * MAX_OUTPUT_NUM;     // size + FIR
                u8* content = (u8*)sram_malloc(SRAM_DDR, mem_any, preset_size);
                s32 size ;
 
                tob->toClear();
 
                var.pscene->update_module();
                size = var.pscene->convert_to_bin(content);
                tob->toAnalysis(content, size);
 
                sram_free(SRAM_DDR, content);
                var.TopoStatus = PRESET_STA::PRESET_DONE;
            }
        }
        sram_free(SRAM_DDR, bin); bin = NULL;
        var.TopoLoading = ufalse ;
    }
 
    return 0;
}
 
s32 Message::HandshakeMessageProcess(MSG* pmsg)
{
//    s8 usb_rx_ch=2, usb_tx_ch=2;
    VarState& var = VarState::GetInstance();
 
    ptag_device_config device_config = (ptag_device_config)pmsg->data;
    struct DSPConfig dspconfig ;
 
    if(var.HandShakeSuccesful == ufalse) {
        //可以根据ptag_device_config.hardware_type动态适配型号.
        if(var.pscene){
            delete var.pscene;
            var.pscene = NULL;
        }
 
//        param_init(device_config);
        hw_adapter_t* _adapter = new tg_hw_adapter_t(device_config->dual_dsp, device_config->dsp_index
                            ,device_config->local_rx_num, device_config->local_tx_num
                            ,device_config->dante_rx_num, device_config->dante_tx_num);
        var.pscene = new(SRAM_DDR) tgScene(_adapter);
 
        _adapter->config_board(&dspconfig);
        new(SRAM_DDR) ToB(device_config->dual_dsp, device_config->dsp_index);
 
        SAMPLE_NUM = dspconfig.mSampleNum;
        var.master_intr = dspconfig.mIntrNo;
        var.g_level_report_interval = LEVEL_REPORT_TIME(dspconfig.mLevelReportInt);
 
        ModuleLeveldBUSetting(dspconfig.mConvertUnit);
        RouteConfig(dspconfig.routes, dspconfig.mRouteNum);
        SRCsConfig(0 , &dspconfig.srcs[0]);
        SRCsConfig(1 , &dspconfig.srcs[1]);
        PCGsConfig(dspconfig.pcgs);
        SportsConfig(dspconfig.sports);
        //LinportConfig(dspconfig.linkport);
 
        var.HandShakeSuccesful = utrue;
    }
 
    Send(MsgType::MSG_ACK_REQ, 0 , 0);
//    dbg_printf("HandShake OK\n");
    return 0;
}
 
// section equal - ret:1
// section not equal - ret:0
bool decide_modu_type_equal(ptag_module pmodu, s16 dst_ch, s16 src_ch)
{
    bool ret = 0;
    if (pmodu[dst_ch].proc_type == pmodu[src_ch].proc_type) {
        if (PROC_EQ == pmodu[dst_ch].proc_type) {
            ptag_eq proc_eq = (ptag_eq)&pmodu[dst_ch].proc_ins;
            u16 section_dst = proc_eq->nsection;
            proc_eq = (ptag_eq)&pmodu[src_ch].proc_ins;
            u16 section_src = proc_eq->nsection;
            if (section_src == section_dst)
                ret = 1;
        }
        else if (PROC_GEQ == pmodu[dst_ch].proc_type) {
            ptag_geq proc_geq = (ptag_geq)&pmodu[dst_ch].proc_ins;
            u16 section_dst = proc_geq->nsections;
            proc_geq = (ptag_geq)&pmodu[src_ch].proc_ins;
            u16 section_src = proc_geq->nsections;
            if (section_src == section_dst)
                ret = 1;
        }
        else
            ret = 1;
    }
    return ret;
}
 
s32 Message::ChannelParamCopy(MSG* pmag)
{
    s32 len = 0;
    ptag_ch_param_copy ch_copy = (ptag_ch_param_copy)pmag->data;
    VarState& var = VarState::GetInstance();
    void* params_ptr = var.pscene->get_parameters();
    if (params_ptr == nullptr) {
        return -1;
    }
    ptag_parameters params = static_cast<ptag_parameters>(params_ptr);
    ToB* tob = ToB::GetInstance();
    param_ctrl_t* paramset = tob->GetParamCtrl();
    s32** chin_mid = var.pscene->get_chin_mid();
    s32** chout_mid = var.pscene->get_chout_mid();
    tg_param_ctrl_t paramctrl;
 
    s32 max_ch = ch_copy->bOutput ? var.pscene->get_output_num() : var.pscene->get_input_num();
    if (ch_copy->src_ch_index >= max_ch || ch_copy->dst_ch_index >= max_ch || ch_copy->src_ch_index < 0 || ch_copy->dst_ch_index < 0) {
        dbg_printf("ch %s copy channel index error\n",  ch_copy->bOutput ? "out" : "in");
        return false;
    }
 
    if (!ch_copy->bOutput) {
        memcpy(&params->input.input[ch_copy->dst_ch_index], &params->input.input[ch_copy->src_ch_index], sizeof(params->input.input[0]));
        IModule* m = tob->GetModule(chin_mid[ch_copy->dst_ch_index][0]);    // [ch_idx][0] is the input type
        ParamCtrl_fn paramEntry = paramset->GetParamEntry(PROC_INPUT);
        paramEntry(m, (uvoid*)&params->input, len);
 
        ptag_module inmod_params[5] = {params->in1, params->in2, params->in3, params->in4, params->in5};
        for (s32 i = 0; i < 5; i++) {
            if (paramctrl.decide_modu_type_equal(static_cast<ptag_module>(inmod_params[i]), ch_copy->dst_ch_index, ch_copy->src_ch_index)) {    // 1
                memcpy(&inmod_params[i][ch_copy->dst_ch_index], &inmod_params[i][ch_copy->src_ch_index], sizeof(tag_module));
            }
            else {
                dbg_printf("ch dst module in%d type != src.\n", i+1);
                continue;
            }
 
            paramEntry = paramset->GetParamEntry(inmod_params[i][ch_copy->dst_ch_index].proc_type);
            if(paramEntry == NULL) {
                dbg_printf("paramEntry is NULL!\n");
                return -1;
            }
            m = tob->GetModule(chin_mid[ch_copy->dst_ch_index][i + 1]);
            u32 result = paramEntry(m, (uvoid*)&inmod_params[i][ch_copy->dst_ch_index].proc_ins, len);
        }
    }
    else {
        memcpy(&params->output.output[ch_copy->dst_ch_index], &params->output.output[ch_copy->src_ch_index], sizeof(params->output.output[0]));
        IModule* m = tob->GetModule(chout_mid[ch_copy->dst_ch_index][0]);    // [ch_idx][0] is the output type
        ParamCtrl_fn paramEntry = paramset->GetParamEntry(PROC_OUTPUT);
        paramEntry(m, (uvoid*)&params->output, len);
 
        ptag_module outmod_params[3] = {params->out1, params->out3, params->out4};
        for (s32 i = 0; i < 3; i++) {
            if (paramctrl.decide_modu_type_equal(static_cast<ptag_module>(outmod_params[i]), ch_copy->dst_ch_index, ch_copy->src_ch_index)) {    // 2
                memcpy(&outmod_params[i][ch_copy->dst_ch_index], &outmod_params[i][ch_copy->src_ch_index], sizeof(tag_module));
            }
            else {
                dbg_printf("ch dst module out%d type != src.\n", i+1);
                continue;
            }
 
            paramEntry = paramset->GetParamEntry(outmod_params[i][ch_copy->dst_ch_index].proc_type);
            if(paramEntry == NULL) {
                dbg_printf("paramEntry is NULL!\n");
                return -1;
            }
            m = tob->GetModule(chout_mid[ch_copy->dst_ch_index][(0==i) ? (i+1) : (i+2)]);
            u32 result = paramEntry(m, (uvoid*)&outmod_params[i][ch_copy->dst_ch_index].proc_ins, len);
//            printf("dst p adr:0x%x\n", (uvoid*)&outmod_params[i][ch_copy->dst_ch_index]);
        }
 
        ptag_module_fir outmod2_param = params->out2;
        if (paramctrl.decide_modu_type_equal(static_cast<ptag_module_fir>(outmod2_param), ch_copy->dst_ch_index, ch_copy->src_ch_index)) {    // 3
            memcpy(&outmod2_param[ch_copy->dst_ch_index], &outmod2_param[ch_copy->src_ch_index], sizeof(tag_module_fir));
        }
        else {
            dbg_printf("ch dst module out2 type != src.\n");
            return -1;
        }
 
        paramEntry = paramset->GetParamEntry(outmod2_param[ch_copy->dst_ch_index].proc_type);
        if(paramEntry == NULL) {
            dbg_printf("paramEntry is NULL!\n");
            return -1;
        }
        m = tob->GetModule(chout_mid[ch_copy->dst_ch_index][2]);
        u32 result = paramEntry(m, (uvoid*)&outmod2_param[ch_copy->dst_ch_index].proc_ins, len);
    }
 
    return 0;
}
 
s32 Message::RxMessageHandler(MSG* pmsg)
{
    extern ubool  systemMute;
    if(pmsg->magic != 0x5aa5) {
//        dbg_printf("magic error.\n");
        return -1;
    }
 
    switch(pmsg->msgType){
    case MsgType::MSG_PARAM_CTRL:
        ParamCtrl(pmsg);
        break;
    case MsgType::MSG_GET_LEVEL:
        ReportLevel(pmsg);
        break;
    case MsgType::MSG_PARAM_CONFIG:
    case MsgType::MSG_PARAM_COMPLETED:
        PresetProcess(pmsg);
        break;
    case MsgType::MSG_ACK_REQ:
        HandshakeMessageProcess(pmsg);
        break;
    case MsgType::MSG_CHANNEL_COPY_REQ:
        ChannelParamCopy(pmsg);
        break;
    default:
        break;
    }
 
    return 0;
}
 
 
 
uvoid Message::Proc()
{
    u32 status = 0;
//    static bool bSetOk = 0;
 
//    MSG* p = (MSG*)SPI_Rx_BUFFER;
//    if (0 == bSetOk) {
//        HandshakeMessageProcess(p);    //virtual communication
//        PresetProcess(p);
//        bSetOk = 1;
//    }
 
    if(SPIRxDone) {
        RxMessageHandler((MSG*)SPI_Rx_BUFFER);
        if(txQueue->Count() > 0) {
            txQueue->Pop(*(MSG*)SPI_Tx_BUFFER);
            GPIO_SetOutPut(GPIOA, GPIO_Pin12, GPIO_LOW);
        }
        else {
            SPI_Tx_BUFFER[0] = 0;
            GPIO_SetOutPut(GPIOA, GPIO_Pin12, GPIO_HIGH);
        }
        SPI2_SetTransMode(SPIStatus::SPI_TRX);
        GPIO_SetOutPut(GPIOB, GPIO_Pin5, GPIO_HIGH);
    }
}