/*
|
* 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¿ÉÓÃ
|
}
|
}
|