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

📄 hostport.c

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

	Hostport.c

	This file specifies the routines to interface to the Host port on the
	CobraNet Module

	Copyright ( c) 2004 by Cirrus Logic Incorporated All Rights Reserved
*************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <intrpt.h>
#include <8051.h>
#include <string.h>
#include "command.h"
#include "test.h"
#include "serial.h"
#include "cnmutil.h"
#include "error.h"
#include "cm1_funcs.h"
#include "cm2_funcs.h"
#include "mystrings.h"
#include "hostport.h"

#define cMAX_TRANS_TRIES	5	//maximum number of times to check for
														//translation to complete

near long gPeekPointer = -1;
near long gPokePointer = -1;
near long gLast_hackTranslation = 0;

unsigned char ( * Host_Translate_Address )( unsigned long address );
void ( * Host_Garbage_Read )( void );
void ( * Host_Interrupt_Ack )( void );
unsigned char ( * Host_Multiplex_OP )( unsigned char mux_op );
unsigned char ( * write_host_long )( unsigned long the_data );
unsigned long ( * read_host_long )( unsigned char * error_code_ptr );

/************************************************************************/
/*                           Code starts here                           */
/************************************************************************/
//define error codes and error messages here 
#define cERR_TRANSLATE_TIMEOUT	1
#define cERR_INVALID_ADDRESS		2
//this routine prints a description of the error
//when given an error number.
extern code char * get_host_error( unsigned char err_num )	{
	static code char * code host_error_ptr[] =	{
		noErrorStr,
		err_Timeout_str,
		str_err_BadAddr
	};

  return( host_error_ptr[ err_num ] );
}

//sets up pointers to host interface functions depending on the
//module type.
extern void init_Host_funcs( unsigned char which_module )	{
	if ( which_module == cHOST_CM1 )	{
		Host_Translate_Address = Host_Translate_Address_CM1;
		Host_Garbage_Read = Host_Garbage_Read_CM1;
		Host_Interrupt_Ack = Host_Interrupt_Ack_CM1;
		write_host_long = write_host_long_CM1;
		read_host_long = read_host_long_CM1;
		Host_Multiplex_OP = Host_Multiplex_OP_CM1;
	}
	else	{
		Host_Translate_Address = Host_Translate_Address_CM2;
		Host_Garbage_Read = Host_Garbage_Read_CM2;
		Host_Interrupt_Ack = Host_Interrupt_Ack_CM2;
		write_host_long = write_host_long_CM2;
		read_host_long = read_host_long_CM2;
		Host_Multiplex_OP = Host_Multiplex_OP_CM2;
	}
}

extern void Host_Reset( unsigned char the_reset_param )	{
  char * iHost_ptr = ( char * ) cHOST_RESET_ptr;

  *( iHost_ptr ) = the_reset_param;
}

//the following routine is used to detect, after power up, which CobraNet
//module is present. For the CM-1, patterns are written to a specific
//register location, the pattern is then checked to see if it is the same
//as that which was written, if so it is the CM-1. The difference being
//that the register written on the CM-1 is the same register that is
//read whereas on the CM-2 they are differenct registers. For the CM-2, 
//a specific pattern should be available after a host command is issued.
extern unsigned char CM_Detect_Test( void )	{  
  unsigned char * iHost_ptr = ( char * ) cHOST_PORT_ptr;
	unsigned char temp_char;

	*( iHost_ptr + 1 ) = 0x55;
	*( iHost_ptr + 4 ) = 0xAA;		//to change the bus
	if ( *( iHost_ptr + 1 ) == 0x55 )	{	//do it again for robustness.
		*( iHost_ptr + 1 ) = 0x3C;
		*( iHost_ptr + 4 ) = 0xC3;	//to change the bus
		if ( *( iHost_ptr + 1 ) == 0x3C )
			return( cHOST_CM1 );
	}

	
	*( iHost_ptr + 2 ) = 0x07;
	*( iHost_ptr + 3 ) = 0xB5;
	waiting( mMSECOND10 );					//approx. 10 milliseconds
	temp_char = *( iHost_ptr + 7 ); //garbage read (for pipeline)
	if ( ( *( iHost_ptr + 4 ) == 'C' ) && ( *( iHost_ptr + 5 ) == 'S' ) &&
			 ( *( iHost_ptr + 6 ) == 0x46 ) && ( *( iHost_ptr + 7 ) == 0xB5 ) )
		return( cHOST_CM2 );
	else
		return( 0 );	//something is missing.
}

extern unsigned char Host_Sync_Translation( void )	{
  unsigned char error_code = cERR_NO_ERROR;

	if ( !( error_code = Host_Multiplex_OP( cMUXOP_GOTO_COUNTERS ) ) )	{
		gLast_hackTranslation = read_host_long( &error_code );//garbage read
		gLast_hackTranslation = read_host_long( &error_code );//hackStatus,
		gLast_hackTranslation = read_host_long( &error_code );//hackTranslations
	}
	return( error_code );
}

extern unsigned char SetAddress( unsigned long the_Address )	{
	unsigned long temp_long;
  unsigned short count = 0;
  unsigned char error_code = cERR_NO_ERROR;

	if ( !( error_code = Host_Translate_Address( the_Address ) ) )	{
		count = 0;
		do
			if ( !( error_code = Host_Multiplex_OP( cMUXOP_GOTO_COUNTERS ) ) ) {
				temp_long = read_host_long( &error_code );		//garbage read
				temp_long = read_host_long( &error_code );		//hackStatus, ignore
				temp_long = read_host_long( &error_code );		//hackTranslations
				count++;
			}
			else
				break;
		while ( ( temp_long == gLast_hackTranslation ) && 
						( count < cMAX_TRANS_TRIES ) );	//'do' construct
		if ( count >= cMAX_TRANS_TRIES )
			error_code = cERR_TRANSLATE_TIMEOUT;
		else	{
			temp_long = read_host_long( &error_code );		//hackSNMPModify
			temp_long = read_host_long( &error_code );		//hackReadLength
			if ( error_code )	{
				printStrCode( err_ReadLength );
				printStrC( byte2str( error_code ) );
			}
			if ( temp_long )	{
				gLast_hackTranslation = temp_long;
				if ( !( error_code = 
									Host_Multiplex_OP( cMUXOP_GOTO_TRANSLATION ) ) )	{
					gPeekPointer = the_Address;	// maintain local pointer
					gPokePointer = the_Address;
					Host_Garbage_Read();				//garbage read
				}
			}
			else	{
				printStrCode( str_err_BadAddr );
				printStrCode( str_HexPrefix );
				printStrC( long2str( the_Address, cLEADING_ZEROS, cSIX_BYTES ) );
				error_code = cERR_INVALID_ADDRESS;
			}
		}
	}
  return( error_code );
}

extern unsigned long Host_Peek( unsigned long the_Address, 
															  unsigned char * error_code_ptr )	{
  unsigned char error_code = cERR_NO_ERROR;
		//does the address need to be translated?
  if( the_Address != gPeekPointer )
     error_code = SetAddress( the_Address );

  if ( error_code == cERR_NO_ERROR )	{
		gPeekPointer++; /* maintain local pointer */
		return ( read_host_long( error_code_ptr ) );
	}
	*error_code_ptr = error_code;
  return( 0 );
}

extern unsigned char Host_Poke( unsigned long Address, 
																unsigned long Value )	{
  unsigned char error_code = cERR_NO_ERROR;

  if( Address != gPokePointer )
    error_code = SetAddress( Address );
  if ( error_code == cERR_NO_ERROR ) {
		gPokePointer++; /* maintain local pointer */
				/* force SetAddress() on next Peek() to freshen data */
		gPeekPointer = -1;
		return ( write_host_long( Value ) );
	}

	return( error_code );
}

⌨️ 快捷键说明

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