Author Topic: fnWrite  (Read 13605 times)

Offline Renan

  • Newbie
  • *
  • Posts: 15
    • View Profile
fnWrite
« on: February 02, 2009, 07:46:54 PM »
Hi Mark,
I'm having problems when you send packets followed by fnwrite() function. I am sending 10 packages of 2 in 2 seconds and the system crash and getting stuck in the function:

__interrupt__ static void undef_int (void){
while (1) {}
}

It is normal that happen?

Best Regards.

Renan.

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: fnWrite
« Reply #1 on: February 02, 2009, 09:30:17 PM »
Hi Renan

This is not normal because it is probably an exception. Debugging exceptions is detailed here: http://www.utasker.com/forum/index.php?topic=123.msg468#msg468

Can you explain in more detail the problem? fnWrite is used for queues, UARTs, I2C, USB and Ethernet so I don't know over which interface you are sending data. fnWrite can crash if its handle is invalid. For example, if there is a buffer which is too short and when filled it overwrites the position where the handle is saved.

Regards

Mark



Offline Renan

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: fnWrite
« Reply #2 on: February 04, 2009, 02:33:00 PM »
Hi Mark,

I used the function fnwrite to send a burst of ethernet packets. I am testing sending 10 packets of 200 bytes in a range of 2 seconds. The coldfire just hangs when I receive the 2nd round.

fnWrite(Ethernet_handle, pointer , lenght);
fnWrite(Ethernet_handle, 0 , 0);

I am using the function correctly? how can I increase the buffer of output?

Thanks...

Renan.

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: fnWrite
« Reply #3 on: February 04, 2009, 05:31:22 PM »
Hi Renan

This looks OK. This is how the IP layer sends data.

Are you sure that the Ethernet interface has been configured before you start the tests? The Ethernet interface is initializes after 50ms in the demo project and anything trying to use it before then would probably crash. Check that the value of Ethernet_handle is valid (eg. not 0) before use.

Regards

Mark

Offline Renan

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: fnWrite
« Reply #4 on: February 04, 2009, 06:52:08 PM »
Yes, when I'm sending the package the value of Ethernet_handle is 8. In the Wireshark i can see the first 10 packets and the next 8 packets, in this moment the system stop to work in the function __interrupt__ static void undef_int (void).

Regards...

Renan

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: fnWrite
« Reply #5 on: February 04, 2009, 09:25:24 PM »
Hi Renan

The handle looks good. Are you checking it each time to ensure that it doesn't (for some reason) change? Eg. get corrupted by some data coping?

Since the same sequence is used by IP, and this can send many frames without a problem, I don't known where to otherwise look.

I suggest breaking in the exception and looking to see what the processor had done to cause the crash. See earlier post in this thread for a link to the guide to doing this.

Regards

Mark

Offline Renan

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: fnWrite
« Reply #6 on: February 05, 2009, 04:50:28 PM »
Hi Mark,

   
My routine is simple, follows below:

Code: [Select]
if (inicia!=1){
uTaskerGlobalMonoTimer( OWN_TASK, (DELAY_LIMIT)(2   * SEC), E_MYEVENT); 
inicia =1;
}

    while ( fnRead( PortIDInternal, ucInputMessage, HEADER_LENGTH )) {   // check input queue
        switch ( ucInputMessage[MSG_SOURCE_TASK] ) {            // switch depending on message source
        case TIMER_EVENT:
       
    if (ucInputMessage[MSG_TIMER_EVENT] == E_MYEVENT)
{
enviaARP();
a=0;
for (a = 0; a <= 10; a++)
                                {
if (Ethernet_handle!=0)
{enviaPKG();}
}
uTaskerGlobalStopTimer( OWN_TASK, E_MYEVENT );
uTaskerGlobalMonoTimer( OWN_TASK, (DELAY_LIMIT)(2 * SEC), E_MYEVENT );
}

I created an event that occurs in 2 to 2 seconds and if found sends a data packet. Below the function that sends the packet:

Code: [Select]
void enviaPKG(void)
{
static unsigned int  lengh1 = 60;
static unsigned char * ptr    = 0;
       unsigned int usPortNr = 1000;
       unsigned char meuIP[4]  = { 192,168,0,121};
unsigned char teste[] =
{
0x08, //ETH.TYPE
0x00,
0x00,
0x01,
0x08,
0x00,
0x06,
0x04,
0x00,
0x01,
0x01,
0x08,
0x06,
0x00,
0x01,
0x08,
0x00,
0x06,
0x04,
0x00,
0x01,
0x08,
0x00,
0x06,
0x04,
0x00,
0x01,
};

// Aloca memoria para pacote
ptr = uMalloc(lengh1);
/* Monta o pacote */
// Dst = MAC do CG
uMemcpy(ptr, &meuIP, MAC_LENGTH);
// Src = MAC da gerencia
uMemcpy(ptr+6, network.ucOurMAC, MAC_LENGTH);
// ethernet type
uMemcpy(ptr+12, &teste, sizeof(teste));
// dados
uMemcpy(ptr+22, network.ucOurMAC, MAC_LENGTH);
uMemcpy(ptr+28, network.ucOurIP, IPV4_LENGTH);
// Zera os demais bytes do pacote
uMemset (ptr+34, 5, 60-34);

fnWrite(Ethernet_handle, ptr, lengh1);
fnWrite(Ethernet_handle, 0, 0);

}

I Follow the steps in another post but did not understand the problem. My "view memory" is in Attached, Can you help?




Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: fnWrite
« Reply #7 on: February 05, 2009, 05:15:54 PM »
Hi Renan

I think that your problem is that you are using uMalloc() inside the loop to take memory space from the heap.
After several successful loops the HEAP has no more space and returns a zero pointer. You then perform a uMemcpy() to the zero pointer, which will cause an access violation and the crash that you are seeing.

It is always best to check that uMalloc() doesn't return a zero (no more memory) before using the memory pointer (to avoid a crash). However in this specific case there is no need to use HEAP memory to do what you are doing - simply use something like unsigned char ucBuffer[60]. Or else you can take the buffer from memory just the first time and use the same pointer (once it has been initialized) for every subsequent call.

Regards

Mark

Offline Renan

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: fnWrite
« Reply #8 on: February 05, 2009, 07:30:34 PM »
I was actually returns a zero pointer, but now I can send thousands of packages in sequence without problems, thank's Mark.

In my system, I'm implementing a "Watchdog for ICMP" where it has the function to send pings (SendPing ()) for some set of IP addresses. When I send more than 3 packets of ping (SendPing ()) in sequence, I looked at that some are not sent or are overwritten by the next, how could I check whether or not I can send the next ping?


Regards..

Renan

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: fnWrite
« Reply #9 on: February 05, 2009, 09:03:21 PM »
Renan

I don't know whether I have really understood correctly but the limit to the speed of sending messages is the LAN delivery speed - if there are collisions it may slow slightly. If the writes are made faster than the Ethernet can delivered them you will get a 0 as return value from fnWrite(). This means that it waited a short time but gave up because all output buffers were full. You can of course retry after a slight delay in this case. There is thus no real rule and it may be better to send always with say 50ms between frames since then you will never lose any.

regards

Mark

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: fnWrite
« Reply #10 on: February 05, 2009, 09:50:05 PM »
Renan

I noticed also the following:

         uTaskerGlobalStopTimer( OWN_TASK, E_MYEVENT );
         uTaskerGlobalMonoTimer( OWN_TASK, (DELAY_LIMIT)(2 * SEC), E_MYEVENT );


Since you are restarting the same timer you don't need to first stop it. The uTaskerGlobalMonoTimer() acts in this case as a trigger and automatically starts the timer with the full delay.

         uTaskerGlobalMonoTimer( OWN_TASK, (DELAY_LIMIT)(2 * SEC), E_MYEVENT );

should be adequate alone.

Regards

Mark

Offline Renan

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: fnWrite
« Reply #11 on: February 11, 2009, 12:34:44 PM »
Hi Mark,

I solved my problems with their tips and my system is working very well.

Regards,

Renan