qipp
2025-11-07 744722688187eb4991c72464e2bb5e06b244fee7
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
/*
 * protocol.c
 * Description:
 *
 *  Created on: 2014-10-11
 *      Author: Graydon
 *  Modify:
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "drv/board_sru.h"
#include "drv/global.h"
#include "drv/spi.h"
#include "protocol.h"
#include "queue.h"
#include "scene.h"
#include "f2f.h"
 
 
 
int scene_loading= 1;
void* htx_que;
 
int input_num_channels = 16;
int output_num_channels = 16;
 
 
int dual_dsp =0;
int dsp_index =0;
int handshake_successful =0;
int clock_ok = 0;
 
extern int dsp_status;
 
void msg_request(int type)
{
    tag_msg msg;
    msg.magic = 0x5aa5;
    msg.ctype = type;//;
    msg.len =0;
    
    queue_push(htx_que,&msg,SPI_PKT_SIZE);
}
 
 
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);
    }
}
 
void msg_handshake_ack(ptag_hwconf p)
{
    //printf("arm scene size:%d\n", p->scene_size);
    //printf("dsp scene size:%d\n", sizeof(tag_parameters));
    //printf("local rx:%d dante rx:%d\n", p->local_rx_num, p->dante_rx_num);
    //printf("local tx:%d dante tx:%d\n", p->local_tx_num, p->dante_tx_num);
    p->scene_size = sizeof(tag_parameters);
    p->level_num =  0;    
    
    input_num_channels = p->local_rx_num + p->dante_rx_num;
    output_num_channels = p->local_tx_num + p->dante_tx_num;
    
    handshake_successful = 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]);
        //printf("proc_id:%d cmd:%d val[0]:%d val[1]:%d\n",pmsg->data[0],pmsg->data[1],pmsg->data[2],pmsg->data[3]);
        scene_ctrl(pmsg->data[0],pmsg->data[1],(int*)&pmsg->data[2],1);
    break;
    case MSG_GET_LEVEL:
        if(scene_loading == 0 && clock_ok == 1){
            msg_send_level();
        }
    break;
    case MSG_PARAM_CONFIG:
        scene_loading = 1;
        scene_cpy(np,(void*)pmsg->data,data_len);
        
    case MSG_PARAM_COMPLETED:
        if(np == ntotals-1){
            //for(i=0;i<0x1ffff;i++);
            scene_destroy();
            scene_analyze();
 
            scene_loading = 0;
        }        
    break;
    case MSG_ACK_REQ:
        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();
 
    if(spi_status == spi_completed){
        //ÅжÏÊǽÓÊÕ»¹ÊÇ·¢ËÍÍê³É
        
        spi_direction_e spi_dir = spi_get_direction();
        void* spi_buffer = spi_get_buffer();
        
        if(spi_dir == spi_rx){
            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¿ÉÓÃ
    }
}