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

📄 conn4.c

📁 DEVICENET规范及例程 很详细
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
 *
 * Microchip DeviceNet Stack (Cyclic/COS Messaging Connection Object Source)
 *
 *****************************************************************************
 * FileName:        conn4.c
 * Dependencies:    
 * Processor:       PIC18F with CAN
 * Compiler:       	C18 02.20.00 or higher
 * Linker:          MPLINK 03.40.00 or higher
 * Company:         Microchip Technology Incorporated
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the "Company") is intended and supplied to you, the Company's
 * customer, for use solely and exclusively with products manufactured
 * by the Company. 
 *
 * The software is owned by the Company and/or its supplier, and is 
 * protected under applicable copyright laws. All rights are reserved. 
 * Any use in violation of the foregoing restrictions may subject the 
 * user to criminal sanctions under applicable laws, as well as to 
 * civil liability for the breach of the terms and conditions of this 
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 *
 * This file contains Cyclic/COS messaging support for the Connection Object 
 * described in Section 5-4 and Chapter 7 of Volume 1 of the DeviceNet 
 * specification. Additional support is in the UsrConn.c file.
 * 
 *
 *
 * Author               Date        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Ross Fosler			04/28/03	...	
 * 
 *****************************************************************************/

#include	"dnet.def"			// Global definitions file

#include 	"typedefs.h"

#if SUPPORT_COS || SUPPORT_CYCLIC

#include	"conn.h"			// Connection prototypes and symbols

#include	"services.h"		// Service codes
#include	"errors.h"			// Error codes
#include	"class.h"			// Class codes

#include	"route.h"			// Global symbols defined by the router
#include	"dnet.h"			// DeviceNet prototypes and symbols
#include	"frag.h"			// Fragmentation control
#include	"CAN.h"

#pragma	udata
/*********************************************************************
 * Connection related variables
 ********************************************************************/
CONN_COS 	uConn4;

//unsigned char	uConn4RxBuffer[CONN_COS_CYCLIC_RX_SIZE];
//unsigned char	uConn4TxBuffer[CONN_COS_CYCLIC_TX_SIZE];



/*********************************************************************
 * Function:        unsigned char _Conn4Create(void)
 *
 * PreCondition:    The CAN (or other) driver must be prepared to 
 *					accept some filter settings.
 *
 * Input:       	none	
 *                  
 * Output:      	unsigned char handle to the connection	
 *
 * Side Effects:    none
 *
 * Overview:        This function creates a connection 
 *					in the predefined set and returns a 
 *					handle to the connection. The connection is 
 *					placed into a 'configuring' state. 
 *					
 *					This function will call out to the users code
 *					for additional resource allocation. If the 
 *					user returns NULL then the allocation will not
 *					occur.
 *
 * Note:            This function is not called directly by 
 *					application code.
 ********************************************************************/
unsigned char _Conn4Create()
{
	if (UsrConnCreateEvent(4))
	{

		//Initialize the connection attributes
		uConn4.attrib.state = _STATE_CONFIGURING;
		uConn4.attrib.transportClass.byte = 0x83;
		uConn4.attrib.produced_cid.bytes.MSB = (uDNet.MACID >> 3) | (0xD << 3);
		uConn4.attrib.produced_cid.bytes.LSB = uDNet.MACID << 5;
		uConn4.attrib.consumed_cid.bytes.MSB = uDNet.MACID | 0x80; 
		uConn4.attrib.consumed_cid.bytes.LSB = 0x40;
		uConn4.attrib.expected_packet_rate.word = 0;
		
		_existentFlags.bits.cos = 1;
		
		// Setup the pointer and other info for the receiving side
		uConn4.rx.lenMax = uConn4.attrib.consumed_con_size.bytes.LSB;
		uConn4.rx.fragState = 0;
		uConn4.rx.oldFrag = 0;
		
		// Setup the pointer and other info for the transmitting side
		uConn4.tx.lenMax = uConn4.attrib.produced_con_size.bytes.LSB;
		uConn4.tx.fragState = 0;
		uConn4.tx.oldFrag = 0;
				
		//Issue a request to start receiving the CID
		CANSetFilter(uConn4.attrib.consumed_cid.word);
		return (4);
	}
	else return(0);
}


/*********************************************************************
 * Function:        unsigned char _Conn4Close()
 *
 * PreCondition:    The connection should have already been open.
 *
 * Input:       	none
 *                  
 * Output:      	unsigned char instance that closed
 *
 * Side Effects:    none
 *
 * Overview:        Closes the specified connection. Users code is 
 *					notified to release any used resources.
 *
 * Note:            none
 ********************************************************************/
unsigned char _Conn4Close()
{
	// Transition to the non-existent state
	uConn4.attrib.state = _STATE_NON_EXISTENT;
	
	_establishFlags.bits.cos = 0;
	_existentFlags.bits.cos = 0;
	
	// Issue a request to the driver to stop receiving the message
	CANClrFilter(uConn4.attrib.consumed_cid.word);

	UsrConnCloseEvent(4);

	return(4);
}




/*********************************************************************
 * Function:        void _Conn4TimerEvent(void)
 *
 * PreCondition:    None
 *
 * Input:       	None	
 *                  
 * Output:      	None
 *
 * Side Effects:    None
 *
 * Overview:        Update timer and process any timer events. If the 
 *					timer overflows then the state of the connection
 *					is changed.
 *
 * Note:            None
 ********************************************************************/
void _Conn4TimerEvent(void)
{	
	// Process the watchdog if the packet rate is other than 0
	if (uConn4.attrib.expected_packet_rate.word)
	{
		// Adjust the time
		uConn4.timer.word -= TICK_RESOLUTION;

		// If the wdt expires then change the state of the connection
		if (uConn4.timer.word == 0) 
		{
			uConn4.attrib.state = _STATE_TIMED_OUT;
			_establishFlags.bits.cos = 0;
		}
	}	 
	
	// Process the production inhibit timer if other than 0
//	if (uConn4.attrib.production_inhibit_time.word)
//	{
//		if (uConn4.inhTimer.word)
//		{
//			// Adjust the time
//			uConn4.inhTimer.word -= TICK_RESOLUTION;
//		}
//		else
//			uConn4.tx.fragFlags.bits.b2 = 0;
//	}  	   
}




/*********************************************************************
 * Function:        void _Conn4RxEvent(void)
 *
 * PreCondition:    none
 *
 * Input:       	none
 *                  
 * Output:      	none
 *
 * Side Effects:    none
 *
 * Overview:        Process received data for this connection.
 *
 * Note:            This event occures when data has been received
 *					for this connection instance.
 ********************************************************************/
void _Conn4RxEvent(void)
{
	#if CONN_COS_CYCLIC_RX_FRAG	
	// If fragmented
	if (uConn4.attrib.consumed_con_size.word > 8)
	{
		// // Process the fragment
		_mFragNoAckConsume(uConn4.rx);

		// Test the status of the process	
		if (uConn4.rx.fragFlags.bits.b0)
		{
			// Reset the fragment finish flag
			uConn4.rx.fragFlags.bits.b0 = 0;
		
			// Reset the connection wdt
			uConn4.timer.word = uConn4.attrib.expected_packet_rate.word << 2;

			// Indicate message has been received
			_rxFlag.bits.cos = 1;
			
			// Notify the application
			UsrConnRxDataEvent(4);
		}
	}
	else
	#endif

	// else process a non-fragmented message
	{ 
		uConn4.rx.len = CANGetRxCnt();

		// Copy the message to the connection buffer
		CANGetRxDataTyp0(uConn4.rx.pMsg);
		//*((_MSG_DATA *)(uConn4.rx.pMsg)) = *((_MSG_DATA *)CANGetRxDataPtr());
		    
		// Reset the connection wdt
		uConn4.timer.word = uConn4.attrib.expected_packet_rate.word << 2;	
	
		// Indicate message has been received (located in conn.c)	
		_rxFlag.bits.cos = 1;
		
		// Notify the application
		UsrConnRxDataEvent(4);
	}
	 
	// Release the hardware to continue receiving
	CANRead();
}




/*********************************************************************
 * Function:        void _Conn4TxOpenEvent(void)
 *
 * PreCondition:    A transmit request must have been made.
 *
 * Input:       	none	
 *                  
 * Output:      	none
 *
 * Side Effects:    none
 *
 * Overview:        Process open transmit que event
 *
 * Note:            This event occurs when the buffer is available 
 *					for this connection instance to transmit. A 
 *					transmit request must have been made to enter 
 *					this function.
 ********************************************************************/
void _Conn4TxOpenEvent(void)
{
	// Set the produced CID
   	CANPutTxCID(uConn2.attrib.produced_cid.word);

	// Process only if transmit is not inhibited
	if (!uConn4.tx.fragFlags.bits.b2)
	{
		// Inhibit transmission
		uConn4.tx.fragFlags.bits.b2 = 1;
	
		#if CONN_COS_CYCLIC_TX_FRAG		
		// If fragmented
		if (uConn4.attrib.produced_con_size.word > 8)
		{
			// Process the fragment
			_mFragNoAckProduce(uConn4.tx);		
		
			// Request the hardware to queue the message to send
			CANSend(4);
			
			// Test the status of the process		
			if (uConn4.tx.fragFlags.bits.b0)
			{			
				//Clear the transmit flag to open access to the write buffer
				_txFlag.bits.cos = 0;
			}
		}
		else
		#endif
	
		// else process a non-fragmented message
		{
			// Copy the message to the hardware buffer

⌨️ 快捷键说明

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