int CESDDevice::setBaudRate()
{
	int iRetVal = 0;
	m_iErrorState = 0;
	switch( m_iBaudRate )
	{
		case 125:
			m_uiBaudRate=0x06;	// 125k
			break;
		case 250:
			m_uiBaudRate=0x04;	// 250k
			break;
		case 500:
			m_uiBaudRate=0x02;	// 500k
			break;
		case 1000:
			m_uiBaudRate=0x00;	// 1000k
			break;
		default:
			m_uiBaudRate=0x04;	// 250k
			break;
	}

	iRetVal = canSetBaudrate(m_hDevice, m_uiBaudRate);
	if(iRetVal != NTCAN_SUCCESS)
	{
		warning("can set baudrate 0x%x failed Errorcode: %d", m_uiBaudRate, iRetVal);
		getDeviceError(iRetVal);
		m_iErrorState = ERRID_DEV_INITERROR;
		return m_iErrorState;
	}	
	iRetVal = canSetBaudrate(m_hSyncDevice, m_uiBaudRate);
	if(iRetVal != NTCAN_SUCCESS)
	{
		warning("can set baudrate 0x%x failed Errorcode: %d", m_uiBaudRate, iRetVal);
		getDeviceError(iRetVal);
		m_iErrorState = ERRID_DEV_INITERROR;
		return m_iErrorState;
	}	
	return m_iErrorState;
}
Beispiel #2
0
int32_t initNTCAN(uint32_t baud,  uint32_t flags,
                  int32_t ID[], 	 int32_t net,
                  int32_t rxSize, int32_t rxTime,
                  int32_t txSize, int32_t txTime)
{
    NTCAN_HANDLE		handle;    	//
    NTCAN_RESULT		result;    	// Used for checking for error
    int32_t		i;       	  		// Index for multiple objects
    int32_t		timeout=0;   		// Current number of timeouts
    /* Open can */
    result = canOpen(net,flags,txSize,rxSize,   // Opens device
                     txTime,rxTime,&handle);
    if(errorCheck(CAN_OPEN,result) != 0)  			// Error check
    {
        return 0xFFFF;    // returns 1 if error
    }
//   printf("canOpen() success\n");
    /* Setting baudrate */
    result = canSetBaudrate(handle,baud);   		// sets baudrate
    if(errorCheck(CAN_SET_BAUDRATE,result) != 0) // Error check
    {
        return 0xFFFF;    // returns 1 if error
    }
    result = canGetBaudrate(handle,&baud);  		// Reads buadrate
//   printf("canSetBaudrate() success. Baudrate is %d\n",baud);
    /* Adding an ID */
    for(i=1; i<ID[0]; i++) {
        do  {
            result = canIdAdd(handle,ID[i]); 	// Adds ID to handle
            timeout++; 									// reapeat if Timeout
            if(timeout>MAX_TIMEOUT) {
                printf("Max timeout out reached, Aborting addID\n");
                return 0xFFFF;
            }  // return if repeated error
        } while( errorCheck(CAN_ID_ADD,result) == 2);
        if(errorCheck(CAN_ID_ADD,result) != 0) 	// error check
        {
            return 0xFFFF;
        }
//   printf("canIdAdd() successful\n");
    }
    printf("Initializing sucessfull\n");
    /* Flushing FIFO buffer */
    result = canIoctl(handle,NTCAN_IOCTL_FLUSH_RX_FIFO,NULL);
    if(errorCheck(CAN_IO_CTL,result) != 0) 		// flushing FIFO
    {
        return 0xFFFF;    // error check
    }
//   printf("System flushed, device ready to use\n");
    return handle; // returns handle for NTCAN device
}
Beispiel #3
0
/**
 *	Complete a full configuration of the UART.
 **/
static BT_ERROR canSetConfig(BT_HANDLE hCan, BT_CAN_CONFIG *pConfig) {
	volatile LPC17xx_CAN_REGS *pRegs = hCan->pRegs;

	BT_ERROR Error = BT_ERR_NONE;

	canSetBaudrate(hCan, pConfig->ulBaudrate);

	switch(pConfig->eMode) {
	case BT_UART_MODE_POLLED: {
		if(hCan->eMode !=  BT_CAN_MODE_POLLED) {

			if(hCan->hTxFifo) {
				BT_CloseHandle(hCan->hTxFifo);
				hCan->hTxFifo = NULL;
			}
			if(hCan->hRxFifo) {
				BT_CloseHandle(hCan->hRxFifo);
				hCan->hRxFifo = NULL;
			}

			// Disable TX and RX interrupts
			pRegs->CANIER &= ~LPC17xx_CAN_IER_RIE;	// Disable the interrupt

			hCan->eMode = BT_CAN_MODE_POLLED;
		}
		break;
	}

	case BT_UART_MODE_BUFFERED:
	{
		if(hCan->eMode != BT_CAN_MODE_BUFFERED) {
			if(!hCan->hRxFifo && !hCan->hTxFifo) {
				hCan->hRxFifo = BT_FifoCreate(pConfig->usRxBufferSize, sizeof(BT_CAN_MESSAGE), 0, &Error);
				hCan->hTxFifo = BT_FifoCreate(pConfig->usTxBufferSize, sizeof(BT_CAN_MESSAGE), 0, &Error);

				pRegs->CANIER |= LPC17xx_CAN_IER_RIE;	// Enable the interrupt
				hCan->eMode = BT_CAN_MODE_BUFFERED;
			}
		}
		break;
	}
	}

	LPC17xx_CAN_COMMON_REGS *pCMN = CAN_COMMON;

	pCMN->LPC17xx_CAN_AFMR |= LPC17xx_CAN_AFMR_ACCBP;

	return Error;
}
Beispiel #4
0
//-----------------------------------------------
CanESD::CanESD(int iIniBaudRate, int iNrNet)
{
	//initial values
	int net=1; /* logical net number (here: 0) */
	uint32_t mode=0; /* mode used for canOpen() */
	int32_t txqueuesize=100; /* size of transmit queue */
	int32_t rxqueuesize=100; /* size of receive queue */
	int32_t txtimeout=0; /* timeout for transmit operations in ms */
	int32_t rxtimeout=0; /* timeout for receive operations in ms */
	NTCAN_RESULT retvalue; /* return values of NTCAN API calls */
	//uint32_t baud=2; /* configured CAN baudrate (here: 500 kBit/s.) */
	CMSG cmsg[8]; /* can message buffer */
	int rtr=0; /* rtr bit */
	int32_t len; /* # of CAN messages */

	retvalue = canOpen(iNrNet, mode, txqueuesize, rxqueuesize, txtimeout, rxtimeout, &m_Handle);
	if (retvalue != NTCAN_SUCCESS)
	{
		LOGINFO("canOpen() failed: " << getErrorMessage(retvalue));
	}
	Sleep(300);
	retvalue = canSetBaudrate(m_Handle, getBaudRate(iIniBaudRate));
	if (retvalue != 0)
	{
		LOGINFO("canSetBaudrate() failed: " << getErrorMessage(retvalue)); 
		canClose(m_Handle);
	}
	else 
	{
		LOGINFO("Baudrate set to " << getBaudRate(iIniBaudRate));	
	}
	Sleep(300);
	
	//set Amtek Arm IDs
	int i;
	for(i=1; i < 2048; i++)
	{
		retvalue = canIdAdd(m_Handle, i);
		if (retvalue != NTCAN_SUCCESS)
		{
			LOGERROR("canIdAdd() failed: " << getErrorMessage(retvalue)); 
		}
	}

	Sleep(300);

	m_LastID = -1;
}
Beispiel #5
0
void openAllCAN(int vCan) {
	for ( size_t i = 0; i < 2; i ++ ) {
		int r = canOpen( i, //net
				 0, // flags
				 10, //txqueue
				 10, //rxqueue
				 100, //txtimeout
				 100, //rxtimeout
				 &hubo_socket[i] //handle
			);
		if( NTCAN_SUCCESS != r ) {
			fprintf(stderr, "Unable to open CAN %d: %s\n", i, canResultString(r));
			exit( EXIT_FAILURE );
		}

		r = canSetBaudrate( hubo_socket[i], NTCAN_BAUD_1000 );
		if( NTCAN_SUCCESS != r ) {
			fprintf(stderr, "Unable to set CAN %d baud: %s\n", i, canResultString(r));
			exit( EXIT_FAILURE );
		}
	}

}
Beispiel #6
0
//-----------------------------------------------
void CanESD::initIntern()
{	
	int ret=0;
/* Does this make any sense as the handle is opened somewhere later?!
	//m_Handle = 9;
	
	//m_Mutex.lock();

	//ret=canClose(m_Handle);

	if(ret == NTCAN_SUCCESS)
	{
		std::cout << "Can close ok ret = " << ret std::endl;
	}

	CAN_IF_STATUS *status;

	ret=canStatus(m_Handle,status);

	if(ret == NTCAN_SUCCESS)
	{
		std::cout << "Get status from can card ok ret = " << ret << std::endl;
	}
*/	
	ret = 0;
	int iCanNet = 1;
	//m_IniFile.GetKeyInt( "CanCtrl", "NetESD", &iCanNet, true);
	
	int iBaudrateVal = NTCAN_BAUD_250;
	//m_IniFile.GetKeyInt( "CanCtrl", "BaudrateVal", &iBaudrateVal, true);

	std::cout << "Initializing CAN network with id =" << iCanNet << ", baudrate=" << iBaudrateVal << std::endl;

	int iRet;
	if( m_bObjectMode )
		//iRet = canOpen(iCanNet, NTCAN_MODE_OBJECT, 10000, 10000, 0, 0, &m_Handle);
		iRet = canOpen(iCanNet, NTCAN_MODE_OBJECT, 10000, 10000, 1000, 0, &m_Handle);
	else
		//iRet = canOpen(iCanNet, 0, 10000, 10000, 0, 0, &m_Handle);
		iRet = canOpen(iCanNet, 0, 10000, 10000, 1000, 0, &m_Handle);
	sleep(0.3);

	if(iRet == NTCAN_SUCCESS)
		std::cout << "CanESD::CanESD(), init ok" << std::endl;
	else
		std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;

/*	ret=canClose(m_Handle);
	if(ret == NTCAN_SUCCESS)
	{
		std::cout << "Can close ok ret = " << ret << std::endl;
		if( m_bObjectMode )
			iRet = canOpen(iCanNet, NTCAN_MODE_OBJECT, 10000, 10000, 0, 0, &m_Handle);
		else
			iRet = canOpen(iCanNet, 0, 10000, 10000, 0, 0, &m_Handle);
			Sleep(300);

		if(iRet == NTCAN_SUCCESS)
			std::cout << "CanESD::CanESD(), init ok" << ret << std::endl;
		else
			std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
	}
*/
	iRet = canSetBaudrate(m_Handle, iBaudrateVal);
	if(iRet == NTCAN_SUCCESS)
		std::cout << "CanESD::CanESD(), canSetBaudrate ok" << std::endl;
	else
		std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
	sleep(0.3);

	long lArg;
	iRet = canIoctl(m_Handle, NTCAN_IOCTL_FLUSH_RX_FIFO, NULL);
	//if( iRet == NTCAN_SUCCESS )
	//	std::cout << (int)(lArg) << " messages in CAN receive queue" << std::endl;

	// MMB/24.02.2006: Add all 11-bit identifiers as there is no loss in performance.
	for( int i=0; i<=0x7FF; ++i ) {
		iRet = canIdAdd( m_Handle, i );
		if(iRet != NTCAN_SUCCESS)
			std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
	}


	/*
	iRet = canIdAddGroup(m_Handle, 0x98);
	
	// for COB Arm
	iRet = canIdAdd(m_Handle, 0x100);
	iRet = canIdAdd(m_Handle, 0x101);
	iRet = canIdAdd(m_Handle, 0x200);
	iRet = canIdAdd(m_Handle, 0x201);
	iRet = canIdAdd(m_Handle, 0x300);
	iRet = canIdAdd(m_Handle, 0x301);
	iRet = canIdAdd(m_Handle, 0x400);
	iRet = canIdAdd(m_Handle, 0x401);
	iRet = canIdAdd(m_Handle, 0x500);
	iRet = canIdAdd(m_Handle, 0x501);
	iRet = canIdAdd(m_Handle, 0x600);
	iRet = canIdAdd(m_Handle, 0x601);
	
	// for CAN-Open Harmonica
	iRet = canIdAdd(m_Handle, 0x283);
	iRet = canIdAdd(m_Handle, 0x282);
	iRet = canIdAdd(m_Handle, 0x583);
	iRet = canIdAdd(m_Handle, 0x582);
	iRet = canIdAdd(m_Handle, 0x603);
	iRet = canIdAdd(m_Handle, 0x602);

	// for CAN-Open Harmonica and Cello Mimro
	iRet = canIdAdd(m_Handle, 0x18a);
	iRet = canIdAdd(m_Handle, 0x28a);
	iRet = canIdAdd(m_Handle, 0x194);
	iRet = canIdAdd(m_Handle, 0x294);
	iRet = canIdAdd(m_Handle, 0x19e);
	iRet = canIdAdd(m_Handle, 0x29e);
	
	// for RCS5000 arm
	iRet = canIdAdd(m_Handle, 0x111);
	iRet = canIdAdd(m_Handle, 0x112);
	iRet = canIdAdd(m_Handle, 0x113);
	iRet = canIdAdd(m_Handle, 0x114);
	iRet = canIdAdd(m_Handle, 0x115);
	iRet = canIdAdd(m_Handle, 0x116);
	iRet = canIdAdd(m_Handle, 0x788);
	iRet = canIdAdd(m_Handle, 0x789);
	iRet = canIdAdd(m_Handle, 0x78A);
	iRet = canIdAdd(m_Handle, 0x78B);
	iRet = canIdAdd(m_Handle, 0x78C);
	iRet = canIdAdd(m_Handle, 0x78D);
	iRet = canIdAdd(m_Handle, 0x78E);
	iRet = canIdAdd(m_Handle, 0x78F);

	iRet = canIdAddGroup(m_Handle, 0x00);
	iRet = canIdAddGroup(m_Handle, 0x08);
	iRet = canIdAddGroup(m_Handle, 0x10);
	iRet = canIdAddGroup(m_Handle, 0x18);
	iRet = canIdAddGroup(m_Handle, 0x20);
	iRet = canIdAddGroup(m_Handle, 0x28);
	iRet = canIdAddGroup(m_Handle, 0x30);
	iRet = canIdAddGroup(m_Handle, 0x38);
	iRet = canIdAddGroup(m_Handle, 0x40);
	iRet = canIdAddGroup(m_Handle, 0x48);
	iRet = canIdAddGroup(m_Handle, 0x50);
	iRet = canIdAddGroup(m_Handle, 0x58);
	iRet = canIdAddGroup(m_Handle, 0x60);
	iRet = canIdAddGroup(m_Handle, 0x68);
	iRet = canIdAddGroup(m_Handle, 0x98);

	// for Mimro drive ctrl
	iRet = canIdAdd(m_Handle, 0x123);
	*/

/*
	for(int i=0; i<2047; i++)
	{
		if (iRet != NTCAN_SUCCESS)
		{
			TRACE("canIdAdd: %x Error \n", i);
		}
	}
*/
	sleep(0.3);

	m_LastID = -1;

	//m_Mutex.unlock();
}