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
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
/*
 * protocol.c
 * Description:
 *
 *  Created on: 2014-10-11
 *      Author: Graydon
 *  Modify:
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <alg.h>
#include "drv/board_sru.h"
#include "drv/global.h"
#include "drv/spi.h"
#include "drv/hmem.h"
#include "drv/sport.h"
#include "protocol.h"
#include "queue.h"
#include "scene.h"
#include "f2f.h"
#include "execute.h"
#include "sport.h"
 
int scene_loading= 1;
void* htx_que;
 
int input_num_channels = 16;
int output_num_channels = 16;
 
int local_input_channels_num =8;
int local_output_channels_num =8;
int dante_input_channels_num =8;
int dante_output_channels_num =8;
 
 
int dual_dsp =0;
int dsp_index =0;
int hardware_ver = 0;
int usb_master = 0;
 
int handshake_successful =0;
int clock_ok = 0;
int hardware_type =0;
int module_list =0;
 
extern int dsp_status;
extern void* netvoice_handle[4];
int topo_state = PRESET_NONE; //0-no preset,1-preset loading,2-preset done.
 
static int ClockConfig = 0;
 
void msg_send_level(void)
{    
    int i;
    int data_len;
    short* ptr;
    int size = GetLevelPtr(&ptr);
    int ntotals=(size+SPI_DATA_LEN-1)/SPI_DATA_LEN;
    extern short cpu_ulitily ;
    extern short mem_ulitily;
 
    tag_msg msg;
 
    //size = sizeof(Meter);
    
    msg.magic = 0x5aa5;
    msg.ctype = MSG_GET_LEVEL;
    msg.pkt_total_num = ntotals;
    msg.request_id_lsb = size;
    msg.request_id_msb = 0;
    
    //·Ö°ü
    for(i=0;i<ntotals;i++){
        if(i==ntotals-1){
            data_len = size-i*SPI_DATA_LEN;
        }
        else{
            data_len = SPI_DATA_LEN;
        }
                
        memcpy(msg.data,ptr+i*SPI_DATA_LEN,data_len);
                
        msg.len=data_len<<1;
        msg.pkt_no = i; 
        
        queue_push(htx_que,&msg,SPI_PKT_SIZE);
    }
}
 
int msg_handshake_ack(ptag_hwconf p)
{
    extern void DAI_map(void);
    
    //only 4 transfer queue, if already success do not send relpy avoid drop queue message.
    if(handshake_successful ){    
        return 0;
    }
    
    hardware_type = p->hardware_type;
    module_list = p->modulelist;
        
    //ÌØÊâ´¦Àí,32x32Dante ¸Ä³É16Ä£Äâ+16Dante,¶ÔDSPÎÞÓ°Ïì¡£
    if(p->dante_rx_num == 32 && p->dante_tx_num == 32
        && p->local_rx_num == 0 && p->local_tx_num == 0){
        local_input_channels_num = 16;
        local_output_channels_num = 16;
            
        dante_input_channels_num = 16;
        dante_output_channels_num = 16;
    }
    else{
        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 = local_input_channels_num+dante_input_channels_num;
    output_num_channels = local_output_channels_num+dante_output_channels_num;
    
    if(input_num_channels>32){
        input_num_channels = 32;
    }
            
    if(output_num_channels>32){
        output_num_channels = 32;
    }
    
 
    dual_dsp =  p->dual_dsp;
    dsp_index = p->dsp_index;
    hardware_ver = p->hardware_ver;
    usb_master = p->usb_master;
            
    p->scene_size = sizeof(tag_parameters);
    p->level_num =  0;
    
    //12x8, == 12x12+12x8
    if(dual_dsp==1 && dsp_index == 0 ){
        local_output_channels_num = local_input_channels_num;
        output_num_channels = local_output_channels_num+dante_output_channels_num;
    }
    
    if(ClockConfig == 0){
        if(hardware_ver == HARDWARE_NX){
            AudioConfigNx(dual_dsp, dsp_index, usb_master);
            InitSRC();
            InitSPORT(0x03,RATE_SAMPLES);
            
            interruptcb(SIG_SP1, sport_isr);
            interruptcb(SIG_SP3, sport3_isr);
            interruptcb(SIG_SP5, sport_usb_isr);
            //interrupts(SIG_SP1, sport_isr);
            //interrupts(SIG_SP3, sport3_isr);
            //interrupts(SIG_SP5, sport_usb_isr);
        }
        else{
            AudioConfigRule(dual_dsp, dsp_index, p->external_clock);
            InitSRC();
            InitSPORT(0x03,RATE_SAMPLES);
            
            interruptcb(SIG_SP1, sport_isr);
            interruptcb(SIG_SP5, sport_usb_isr);    
        }
        
        ClockConfig = 1;
    }    
 
    handshake_successful = 1;            
    
    return 1;            
}
 
 
int msg_dispatch(ptag_msg pmsg)
{
    int i;
    int data_len;
    int np,ntotals;
    static int on =0;
    static int error=0;
            
    if(pmsg->magic!=0x5aa5){        
        // =========FOR DEBUG============
        if(on){
            SRU(LOW, DPI_PB14_I);
        }
        else{
            SRU(HIGH,DPI_PB14_I);
        }
        error++;
        if((error&0xf)==0){    
            on=!on;
        }
        //===============================
        return -1;
    }        
        
    data_len=(pmsg->len>>1);
    np=pmsg->pkt_no&0xffff;
    ntotals =pmsg->pkt_total_num&0xffff;
    
    switch(pmsg->ctype&0xff){    
    case MSG_PARAM_CTRL:
        pmsg->data[2]=NORMALIZE(pmsg->data[2]);
        pmsg->data[3]=NORMALIZE(pmsg->data[3]);
        scene_ctrl(pmsg->data[0],pmsg->data[1],(int*)&pmsg->data[2],1);
        //queue_push(htx_que,pmsg,SPI_PKT_SIZE);
    break;
    case MSG_GET_LEVEL:
        if(scene_loading == 0 && clock_ok == 1){
            msg_send_level();
        }
        else{
            LevelEmpty();    
        }
    break;
    case MSG_NETVOICE_CHANNEL_REQ:
        if(pmsg->request_id_lsb == 0){
        //    alg_netvoice_put_frame(netvoice_handle[0], pmsg->data, data_len);
        }
        if(pmsg->request_id_lsb == 1){
        //    alg_netvoice_put_frame(netvoice_handle[2], pmsg->data, data_len);
        }
    break;
    case MSG_PARAM_CONFIG:
        scene_loading = 1;
        scene_cpy(np,(void*)pmsg->data,data_len);
    case MSG_PARAM_COMPLETED:
        if(np == ntotals-1){
            scene_destroy();
            scene_analyze();
            topo_state = PRESET_DONE;
            scene_loading = 0;
        }
    break;
    case MSG_ACK_REQ:
        if(msg_handshake_ack((ptag_hwconf)pmsg->data)){
            queue_push(htx_que,pmsg,SPI_PKT_SIZE);
        }
    break;
 
    case MSG_CHANNEL_COPY_REQ:
        scene_channel_copy((ptag_channel_copy)pmsg->data);
    break;
    case MSG_DSP_STATUS_REQ:
        pmsg->data[0] = dsp_status;
        pmsg->ctype = MSG_DSP_STATUS_RES;
        queue_push(htx_que,pmsg,SPI_PKT_SIZE);
    break;
    default:
    break;
    }
    
    
    return 0;
}
 
 
void msg_process()
{
    spi_status_e spi_status = spi_get_status();
    //tag_msg msg;
    
    if(spi_status == spi_completed){
        //ÅжÏÊǽÓÊÕ»¹ÊÇ·¢ËÍÍê³É
        spi_direction_e spi_dir = spi_get_direction();
        void* spi_buffer = spi_get_buffer();
        
        if(spi_dir == spi_rx){
            //memcpy(&msg, spi_buffer, 512);
            msg_dispatch(spi_buffer);            
        }
        
        //¸ù¾Ý·¢ËͶÓÁÐÅжÏÏÂÒ»°üÅäÖ÷¢ËÍ»¹ÊǽÓÊÕ
        if(queue_count(htx_que)>=SPI_PKT_SIZE){
            queue_pop(htx_que,spi_buffer,SPI_PKT_SIZE);    
            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¿ÉÓÃ
    }
}