MessageIO.cpp 4.66 KB
//////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------//
//-- Copyright 2001 VAutomation Inc. Nashua NH USA. All rights reserved.//
//-- This software is provided under license and contains proprietary   //
//-- and confidential material which is the property of VAutomation Inc.//
//-- HTTP://www.vautomation.com											//
//----------------------------------------------------------------------//
//////////////////////////////////////////////////////////////////////////


// MessageIO.cpp: implementation of the CMessageIO class.
//
//////////////////////////////////////////////////////////////////////
#include "DeviceSpecificInclude.h"
#include "MessageIO.h"
#include "DataTypes.h"
#include <stdio.h>
#include <string.h>


CMessageIO::CMessageIO()
{
}

CMessageIO::~CMessageIO()
{
}

char CMessageIO::GetChar()
{

#ifdef MESSAGES_TO_UART

	//Typedefs used to access the UART
	typedef volatile unsigned char* RegisterAddressType;
	typedef volatile char* DataIOType; 
	typedef volatile char BusDataType;

	//Define the UART interface
	AddressType UART1BaseAddress					= (AddressType)0x00FC1000;
	RegisterAddressType RXEmptyRegister				= (RegisterAddressType)( 0x14 + UART1BaseAddress); 
	RegisterAddressType BaudLowRegister				= (RegisterAddressType)( 0x18 + UART1BaseAddress); 
	RegisterAddressType BaudHighRegister			= (RegisterAddressType)( 0x1C + UART1BaseAddress); 
	DataIOType RXDataRegister						= (DataIOType)( 0x10 + UART1BaseAddress); 
	
	static bool uart_init = false;

	//Only set the setup once
	if( uart_init == false )
	{
		*BaudLowRegister =	UART_BAUD_LOW; //30MHZ chip clock
		*BaudHighRegister = UART_BAUD_HIGH;
		uart_init = true;
	}
	 

	volatile char return_char;

	// wait till transmit_is_ready = 1 - ready to transmit
	volatile unsigned char receive_fifo_empty = 0x20;

	while(  receive_fifo_empty == 0x20 )
	{
		receive_fifo_empty = *RXEmptyRegister & 0x20;  
	} 

	return_char = *RXDataRegister;
	

	return( return_char );
#else

	return( '\0' );

#endif

}

void PrintUART( char* print_string )
{

#ifdef MESSAGES_TO_UART

	//Typedefs used to access the UART
	typedef volatile unsigned char* RegisterAddressType;
	typedef volatile char* DataIOType; 
	typedef volatile char BusDataType;

	//Define the UART interface
	AddressType UART1BaseAddress					= (AddressType)0x00FC1000;
	RegisterAddressType TXEmptyRegister				= (RegisterAddressType)( 0x14 + UART1BaseAddress); 
	RegisterAddressType BaudLowRegister				= (RegisterAddressType)( 0x18 + UART1BaseAddress); 
	RegisterAddressType BaudHighRegister			= (RegisterAddressType)( 0x1C + UART1BaseAddress); 
	DataIOType TXDataRegister						= (DataIOType)( 0x10 + UART1BaseAddress); 
	
	static bool uart_init = false;

	//Only set the setup once
	if( uart_init == false )
	{
		*BaudLowRegister =	UART_BAUD_LOW; //30MHZ chip clock
		*BaudHighRegister = UART_BAUD_HIGH;
		uart_init = true;
	}
	 
	//Get the size of the string.
	UnSignedLong chars_to_print = strlen( print_string );

	//Loop over the string and put chars on the interface
	for (UnSignedLong i=0; i< chars_to_print; i++ ) 
	{
		// wait till transmit_is_ready = 1 - ready to transmit
		volatile unsigned char transmit_is_ready = 0;

		while(  transmit_is_ready == 0 )
		{
			transmit_is_ready = *TXEmptyRegister >> 7;  //We want bit 7     
		} 

		BusDataType bus_char = print_string[i]; //Send char to uart, The TX data should be in bit 0-7 and should use volitile
		*TXDataRegister = bus_char;
	}

#endif

}



void PrintForTCLTerm( char* print_string )
{
	//Typedefs used to access the bus
	typedef volatile char* DataIOType; 
	typedef volatile char BusDataType;

	//Define the address for putting data on the bus
	DataIOType	bus_data_address = (DataIOType)DEVICE_1_OFFSET_ADDRESS;
	
	 
	//Get the size of the string.
	UnSignedLong chars_to_print = strlen( print_string );

	//Loop over the string and put chars on the bus to be grabbed by the term
	for (UnSignedLong i=0; i< chars_to_print; i++ ) 
	{
		BusDataType bus_char = print_string[i]; //Set the data to a volitile
		*bus_data_address = bus_char; //Send char to bus using the volitile
	}
}

void PrintStringStdioAll( char* string_buffer )
{
	//Print to all the outputs
	PrintUART( string_buffer );

	printf("%s",string_buffer );

	PrintForTCLTerm( string_buffer );
}

void PrintStringStdioLFAll( char* string_buffer )
{

	PrintUART( "\n\r" );
	PrintUART( string_buffer );

	printf("\n%s",string_buffer );

	PrintForTCLTerm( "\n" );
	PrintForTCLTerm( string_buffer );
}

void CMessageIO::PrintStringStdio( char* string_buffer )
{
	PrintStringStdioAll( string_buffer );
}

void CMessageIO::PrintStringStdioLF( char* string_buffer )
{
	PrintStringStdioLFAll( string_buffer );
}