⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 packet.c

📁 PEAKAUDIO用于EV2板的MCU控制源码
💻 C
字号:
/*************************************************************************

 packet.c

 These are the routines to handle packets on the CobraNet eval board.

	Line command supported: PACKET
 
--Copyright (C) 2001-2004 by Cirrus Logic Inc. All Rights Reserved
*************************************************************************/

#include <stdio.h>
#include <intrpt.h>
#include <8051.h>
#include <string.h>
#include "serial.h"
#include "cnmutil.h"
#include "command.h"
#include "test.h"
#include "error.h"
#include "command.h"
#include "audio.h"
#include "hostport.h"
#include "mib.h"
#include "mystrings.h"
#include "packet.h"

extern unsigned char gWHICH_MODULE;	//defines whether CM1 or CM2 is present
extern unsigned char gProcess_Packets;
unsigned char gSkip_Packets;				//for testing
near unsigned short gMAC_Source[ 3 ];	//local MAC storage
static struct packet_struct packet_buffer;

extern void ( * Host_Interrupt_Ack )( void );
extern unsigned char ( * Host_Multiplex_OP )( unsigned char mux_op );
void packet_execute( unsigned char packet_selection, char * com_arg_ptr[] );
void packet_help( unsigned char detailed );
void form_tx_packet( char * string_ptr, unsigned char the_packet_type, char is_reply );
char * form_command_str( char * com_arg_ptr[] );
void packet_test( unsigned char num_trys );

#define cPACKET_COMMAND	0
#define cPACKET_TEXT		1
#define cPACKET_ON			2
#define cPACKET_OFF			3
#define cPACKET_TESTTX	4
#define cPACKET_TESTRX	5

/*************************************************************************
*															 PACKET COMMAND														 *
*************************************************************************/
void packet( char * com_arg_ptr[] );

code struct command_item_t packet_command =	{
	( code char * ) str_Com_packet,
	( code command_function_t ) packet
};

void packet( char * com_arg_ptr[] )	{
	unsigned char packet_ch;

	if ( com_arg_ptr[ 1 ] == NULL )	//look for the command without arguments
		packet_help( cHELP_SHORT );
	else	{
		if ( *com_arg_ptr[ 1 ] == '?' )	//look for detailed help packet
			packet_help( cHELP_LONG );
		else	{
			packet_ch = 0;
			while ( ( strCodecmp( packet_arguments[ packet_ch ], 
								com_arg_ptr[ 1 ] ) ) && 
									( packet_arguments[ packet_ch ] != NULL ) )
				packet_ch++;
			if ( packet_arguments[ packet_ch ] == NULL )
				printErrString( get_error_string( cERR_ARGUMENT ) );
			else
				packet_execute( packet_ch, com_arg_ptr );
		}
	}
}

void form_tx_packet( char * string_ptr, unsigned char the_packet_type, 
											char is_reply )	{
	//form transmit in normal, big endian manner
	unsigned char * char_ptr;
	char * local_ptr;
	struct packet_struct * packet_ptr = &packet_buffer;
	unsigned char i;
	unsigned short length;

	//put source and destination MACs in place
	for ( i = 0; i < 3; i++ )	{
		packet_ptr->destination_MAC[ i ] = 
			( is_reply ) ? packet_ptr->source_MAC[ i ] : gMAC_Destination[ i ];

		packet_ptr->source_MAC[ i ] = gMAC_Source[ i ];
	}
								
	packet_ptr->protocol_identifier = 0x8819;
	packet_ptr->protocol_version = 0x2;
	packet_ptr->packet_type = the_packet_type;		

	char_ptr = packet_ptr->packet_string;
	
	local_ptr = string_ptr;
	if ( local_ptr != NULL )
		strcpy( ( char * )char_ptr, ( char * )local_ptr );

	length = 0;
	while ( *char_ptr++ != cASCII_NULL )	//determine length of string
		length++;

	length++;								//add one more for null char

	if ( length & 0x1 )
		length++;						//needs to be an even number

	packet_ptr->section_tag_length = length >> 1;	//length in words
	packet_ptr->length = ( ( short ) length + 18 );
}

void put_packet( void )	{
//puts a packet into CobraNet packet memory
	short * local_ptr = ( short * ) &packet_buffer;	//treat as short array
	unsigned long buffer_ptr = cPACKET_TXBUF_ptr;//a long but really a pointer
	unsigned short temp_shorts[2];
	unsigned short i, num_writes;
	unsigned char error_code;

	if ( gWHICH_MODULE == cHOST_CM2 )	{
		num_writes = ( ( *local_ptr ) + 3 ) >> 2;		//number of longs to write,
																								//rounded up
		//send length
		Host_Poke( buffer_ptr++, ( ( long )( *local_ptr++ ) ) << 16 );
		for ( i = 0; i < num_writes; i++ )	{
			temp_shorts[0] = swapBytes( *local_ptr++ );
			temp_shorts[1] = swapBytes( *local_ptr++ );
			error_code = Host_Poke( buffer_ptr++, *( ( long * ) temp_shorts ) );
		}
	}
	else	{
		num_writes = *local_ptr >> 1;		//number of shorts to write
		Host_Poke( buffer_ptr++, ( ( long )( *local_ptr++ ) ) << 8 );//send length
		for ( i = 0; i < num_writes; i++ )
			error_code = Host_Poke( buffer_ptr++, 
													( ( long ) ( swapBytes( *local_ptr++ ) ) ) << 8 );
	}
}

void get_packet( void )	{
//getts a packet from CobraNet packet memory
	short * local_ptr = ( short * ) &packet_buffer;//treat as array of short
	unsigned long buffer_ptr = cPACKET_RXBUF_ptr;
	unsigned short i, num_reads;
	unsigned char error_code;
	unsigned long temp_long;

	if ( gWHICH_MODULE == cHOST_CM2 )	{
		*local_ptr = ( unsigned short )( ( Host_Peek( buffer_ptr++, 
																									&error_code ) ) >> 16 );
		num_reads = *local_ptr++ >> 2; //number of longs to read
		for ( i = 0; i < num_reads; i++ )	{
			temp_long = Host_Peek( buffer_ptr++, &error_code );
			*local_ptr++ = swapBytes( *( ( short * )&temp_long ) );
			*local_ptr++ = swapBytes( ( short ) temp_long );
		}
	}
	else	{
		*local_ptr = ( unsigned short )( ( Host_Peek( buffer_ptr++, 
																									&error_code ) ) >> 8 );
		num_reads = *local_ptr++ >> 1; //number of shorts to read
		for ( i = 0; i < num_reads; i++ )
			*local_ptr++ = swapBytes( ( ( unsigned short )
									( ( Host_Peek( buffer_ptr++, &error_code ) ) >> 8 ) ) );
	}
}

void packet_execute( unsigned char packet_selection, char * com_arg_ptr[] )	{
//execute the packet, either a line command or text.
	unsigned char error_code;

	switch( packet_selection )	{
		case cPACKET_COMMAND:
			if ( com_arg_ptr[ 2 ] == NULL )
				printErrString( get_error_string( cERR_MISSING_ARG ) );
			else	{
				form_tx_packet( form_command_str( com_arg_ptr ),
												cPACKET_TYPE_COMMAND, cNOT_RETURN_PACKET );
				put_packet();
				error_code = Host_Multiplex_OP( cMUXOP_TRANSMIT_PACKET );
			}
			break;
		case cPACKET_TEXT:
			if ( com_arg_ptr[ 2 ] == NULL )
				printErrString( get_error_string( cERR_MISSING_ARG ) );
			else	{
				form_tx_packet( form_command_str( com_arg_ptr ),
												cPACKET_TYPE_TEXT, cNOT_RETURN_PACKET );
				put_packet();
				error_code = Host_Multiplex_OP( cMUXOP_TRANSMIT_PACKET );
			}
			break;
		case cPACKET_ON:
			gProcess_Packets = 1;
			break;
		case cPACKET_OFF:
			gProcess_Packets = 0;
			break;
		case cPACKET_TESTTX:	//this is for testing and is not documented.
			if ( com_arg_ptr[ 2 ] == NULL )
				packet_test( 1 );
			else
				packet_test( ( unsigned char ) str2Long( com_arg_ptr[ 2 ] + 2 ) );
			break;
		case cPACKET_TESTRX:	//this is for testing and is not documented.
			if ( com_arg_ptr[ 2 ] == NULL )
				gSkip_Packets = 0;
			else
				gSkip_Packets = 1;
			break;
		default:
			break;
	};	//switch
}

char * form_command_str( char * com_arg_ptr[] )	{
	static char temp_ptr[ 256 ];
	char * local_ptr;
	char * new_ptr = temp_ptr;
	unsigned char i;
 
	i = 2;
	while ( com_arg_ptr[ i ] != NULL )	{	//parse the command line input
		local_ptr = com_arg_ptr[ i ];	
		while ( *local_ptr != cASCII_NULL )
			*new_ptr++ = *local_ptr++;
		*new_ptr++ = ' ';
		i++;
	}
	*--new_ptr = cASCII_NULL;

	return( temp_ptr );
}

extern void service_Packet( void )	{
	unsigned char error_code = 0;
	char * temp_ptr;

	//let's go get that packet
	if ( !( gSkip_Packets ) )
		get_packet();
	Host_Interrupt_Ack();	//acknowledge interrupt
	error_code = Host_Multiplex_OP( cMUXOP_PACKET_ACK );
	if ( !( gSkip_Packets ) )	{
		temp_ptr = ( char * ) ( packet_buffer.packet_string );

		if ( packet_buffer.packet_type == cPACKET_TYPE_COMMAND )	{
			do_packet_command( temp_ptr );
			if ( *temp_ptr != cASCII_NULL )	{
				form_tx_packet( NULL, cPACKET_TYPE_TEXT, cRETURN_PACKET );
				put_packet();
				error_code = Host_Multiplex_OP( cMUXOP_TRANSMIT_PACKET );
			}
		}
		else
			if ( packet_buffer.packet_type == cPACKET_TYPE_TEXT )	{
				//if a text packet just output the string to the serial console
				printStrC( temp_ptr );
				putch( '>' );
			}
	}
}

void packet_test( unsigned char num_trys )	{
	//this routine is for testing and is not documented.
	unsigned char i;
	unsigned char error_code;
	char * temp_ptr;
	long temp_long = 0;
	char * temp_long_ptr;

	temp_long_ptr = ( char * ) &temp_long;
	for ( i = 0; i < num_trys; i++ )	{
		temp_ptr = byte2str( i );
		temp_long_ptr[1] = *temp_ptr++;
		temp_long_ptr[0] = *temp_ptr;

		//Host_Poke( 0x21014, temp_long );
		mHEX_DATA_OUT = 1;
		//while ( Host_Peek( cBRIDGE_TXPKT_ptr, &error_code ) != Host_Peek( cBRIDGE_TXDONE_ptr, &error_code ) )
		//	;		
		waiting( 0x0604,0x00 );
		error_code = Host_Multiplex_OP( cMUXOP_TRANSMIT_PACKET );
		mHEX_DATA_OUT = 0;
	}
}

//packet return print routines. These print functions print directly
//to the packet buffer and are sent back to the sending EV board and
//sent out its serial port. The text is in response to a command
//initiated via a packet.
extern void print_packet_str( char * a_string )	{
	strcat( ( char * )packet_buffer.packet_string, a_string );
}

extern void	print_packet_strC( char * a_string )	{
	strcat( ( char * )packet_buffer.packet_string, a_string );
	strCodecat( ( char * )packet_buffer.packet_string, str_CRLF );
}

extern void print_packet_codestr( code char * str )	{
	unsigned short i,j;

	i = j = 0;
	while ( packet_buffer.packet_string[ i ] != cASCII_NULL )
		i++;
	while ( ( packet_buffer.packet_string[ i++ ] = str[ j++ ] ) != 
																										cASCII_NULL )
		; 
}

extern void print_packet_codestrC( code char * str )	{
	unsigned short i,j;

	i = j = 0;
	while ( packet_buffer.packet_string[ i ] != cASCII_NULL )
		i++;
	while ( ( packet_buffer.packet_string[ i++ ] = str[ j++ ] ) != 
																										cASCII_NULL )
		;
	packet_buffer.packet_string[ i++ ] = '\r';
	packet_buffer.packet_string[ i++ ] = '\n';
	packet_buffer.packet_string[ i ] = cASCII_NULL;
}

void packet_help( unsigned char detailed )	{
	printStrCodeC( packet_str_help1 );
	if ( detailed )
		printStrCodeC( packet_str_help2 );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -