CB34EX problem

Discussion to talk about software related topics only.
Post Reply
buloup
Posts: 4
Joined: Mon Nov 28, 2011 4:02 am

CB34EX problem

Post by buloup »

Hi everybody,

I'm trying to make an Ethernet to CAN bridge using CB34EX. But i'm facing a very strange problem :evil: On one side I have an UDP java "server" that delivers each #3 milliseconds 64bits of random data to send over UDP. On the other side I have the netburner that is waiting for UDP packets. If it receives some data, it sends them on CAN bus.

Here are the two little programs :

Java "server" (run method of the thread) :

Code: Select all

public void run() {
		while (true) {
			int size = 8;//(int)(random.nextFloat()*8) + 1;
			bytesSent += size;
			System.out.println(bytesSent / 8);
			byte[] byteBuffer = new byte[size];
			random.nextBytes(byteBuffer);			
			
			DatagramPacket dgPacket = new DatagramPacket(byteBuffer, byteBuffer.length, destinationAddress, destinationPort);
			try {
				dgSocket.send(dgPacket);
			} catch (IOException e) {
				e.printStackTrace();
			}
			long t = System.nanoTime();
			while((System.nanoTime() - t) < 3*1000000);
		}
	}
Netburner C code :

Code: Select all

#include "predef.h"
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <canif.h>
#include <udp.h>

#define LED1_GREEN   (0x80)
#define LED1_RED     (0x40)
#define LED1_OFF     (0xC0)
#define LED2_GREEN   (0x20)
#define LED2_RED     (0x10)
#define LED2_OFF     (0x30) 

extern "C" {
void UserMain(void * pd);
}

const char * AppName="CanEthernet";

void UserMain(void * pd) {
    InitializeStack();
    OSChangePrio(MAIN_PRIO);
    EnableAutoUpdate();
    StartHTTP();

	/*
	 * CAN initialisation : must be done before any CAN function (except CanShutDown)  
	 * 1Mhz, null global mask (each message is accepteds) and default IRQ level = 4
	 */
	int canResponse = CanInit(1000000, 0x0000);
	
	/*
     * Prepare FIFO to recieve messages from UDP
     */
    OS_FIFO udpFifo;
    OSFifoInit(&udpFifo);
    
    /* 
     * Register UDP fifo using listen port parameter
     */
    RegisterUDPFifo(15001, &udpFifo);

	int freeCanChannel = FreeCanChannels();
	if(freeCanChannel == 16) putleds( LED2_GREEN );
	
	putleds( LED1_GREEN );
    while (canResponse == CAN_OK) {
      {//This is UDP receive scope stuff input
     		/*
     		 * create UDP packet waiting until something is comming from port
     		 */
			UDPPacket pkt(&udpFifo, 0);
			if(pkt.Validate()) {
				WORD nbBytesRead = pkt.GetDataSize();
				PBYTE udpByteBuffer = pkt.GetDataBuffer();
				//If we are here, it's because something is comming from UDP
				//Send it via CAN with message ID 1
				int currentPosition = 0;
				int bytesToSend = 0;
				while(currentPosition < nbBytesRead) {
					//Send message length of maximum 8 bytes 
					bytesToSend = nbBytesRead - currentPosition;
					if(bytesToSend > 8) bytesToSend = 8;
					canResponse = SendMessage(NormToNbId(1), &udpByteBuffer[currentPosition], bytesToSend, DONT_WAIT, 0);					
					currentPosition += bytesToSend;
					//USER_ENTER_CRITICAL();
					//for(int i = 0; i<1000; i++) asm("nop");
					//USER_EXIT_CRITICAL();
				}
			}
		}
    }
    
    freeCanChannel = FreeCanChannels();
    putleds( LED2_OFF );
    putleds( LED1_OFF );
    if(freeCanChannel < 16) putleds( LED2_RED );
    if(canResponse == CAN_CHANNEL_USED) putleds( LED1_RED );
    
    CanShutDown();
    UnregisterUDPFifo(15001);    
}
Most of the time, everything is working fine for several seconds (#100secs) and then it stops unexpectedly with error CAN_CHANNEL_USED (led1 is red) and less than 16 free channels (led2 is red).

If I replace DONT_WAIT with any other value, than the netburner hangs, stops, (after several seconds) never returning from SendMessage function and the CAN port sends always the same values.

I've not been able to find my error.
Any help would be very appreciated.
Thanks a lot,
frank
buloup
Posts: 4
Joined: Mon Nov 28, 2011 4:02 am

CB34EX

Post by buloup »

Hello again,

I've forgotten to notice that I was using NBEclipse 2.2rc3 for these tests and
need to use CAN bus at 1MHz.

I've done an upgrade to NBEclipse 2.5 and now the problem is solved 8-)
As far as I have understood from netburner technical support (by the
way Thanks a lot !), It was the CAN lib that was not stable enough when
bus is running at 1MHz.

So anybody who plan to use CAN in CB34EX should upgrade to 2.5.
Perhaps it is obvious to use latest software version, but it was not for me :mrgreen:

Hope this will help someone someday....

regards
frank
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: CB34EX problem

Post by rnixon »

Hi Frank,

Thanks for taking the time to post the solution. That's good to know.
Post Reply