Problem with Serial

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

M.H.Roohi
Posts: 17
Joined: Wed Mar 20, 2013 1:33 pm

Problem with Serial

Post by M.H.Roohi »

hi every body.
I'm new to ChibiOS but I found it very useful.
I'm trying to print a string into serial port:

Code: Select all

chprintf((BaseChannel *)&SD2, "test....\r\n");
      chThdSleepMilliseconds(1);

but in the terminal i just see "tes" !
but when i increase the delay in "chThdSleepMilliseconds" the whole of the string ("test...") is appear.

please help me, i want to print some strings whit the frequency of for example 1 ms.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Problem with Serial

Post by Giovanni »

Hi,

The behavior is not normal, the whole string should be printed. Without the whole code is hard to guess what the cause could be. I would verify if there is a stack overflow somewhere that is masked by the chThdSleepMilliseconds() call.

If you are doing that into a thread try increasing the stack sizes, also activate all the debug options in chconf.h.

Giovanni
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Problem with Serial

Post by Tectu »

First of all, I'm not sure if you reall want to call chprintf() every 1ms.
For another, there have been many issues together around the entire string manipulation and chprintf() stuff which around. Many many have been related to the used toolchain. I cannot tell you why some or most of them don't work as expected, but I can tell you that this one works for 100% sure.

However, you might want to dump the full code here as Giovanni suggested.


~ Tectu
M.H.Roohi
Posts: 17
Joined: Wed Mar 20, 2013 1:33 pm

Re: Problem with Serial

Post by M.H.Roohi »

Thanks for reply.
i use MDK-KEIL
Here is my simple code:

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ch.h"
#include "hal.h"
#include "chprintf.h"

int main(void) {
  /*
   * Shell thread
   */
  halInit();
  chSysInit();

   sdStart(&SD2, NULL);
  palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
  palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));
   //while(TRUE)
   {   
      chprintf((BaseChannel *)&SD2, "test....\r\n");
      chThdSleepMilliseconds(1);
   }
}

and i also tried following code, but with the same result:

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ch.h"
#include "hal.h"
#include "chprintf.h"

int main(void) {
  /*
   * Shell thread
   */
  halInit();
  chSysInit();

   sdStart(&SD2, NULL);
  palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
  palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));
   //while(TRUE)
   {   
      sdPut(&SD2,'t');
      sdPut(&SD2,'e');
      sdPut(&SD2,'s');
      sdPut(&SD2,'t');
      sdPut(&SD2,'.');
      sdPut(&SD2,'.');
      sdPut(&SD2,'.');
      chThdSleepMilliseconds(1);
   }
}


Regards
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Problem with Serial

Post by Tectu »

KEIL really shouldn't be a problem.
Please try to cast to (BaseSequentialStream*) instead of (BaseChannel*). Otherwise I've no idea. I can try the same code with my hardware this evening.


~ Tectu
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: Problem with Serial

Post by steved »

I saw something similar with the UART driver at one point, which I think was because each new sendbuffer cancels the remainder of the previous one.

Even if that's not the problem, is your serial port fast enough to output a continuous stream of data when you're adding 10 characters each millisecond? Suggests to me that at 115200 baud you're running pretty much flat out.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Problem with Serial

Post by Giovanni »

The serial driver, unlike the UART driver, is buffered so no character should be lost, the write function waits for the string to be completely inserted into the buffer.

It should not lose chars regardless those delays.

Giovanni
M.H.Roohi
Posts: 17
Joined: Wed Mar 20, 2013 1:33 pm

Re: Problem with Serial

Post by M.H.Roohi »

i solve the problem....!
just by following stupid code!!

Code: Select all

   chprintf((BaseChannel *)&SD2, "test....\r\n");
      while(1);



i dont know why, but it works well
can somebody tell me why??? maybe micro doing something else?
Regards
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Problem with Serial

Post by Tectu »

Huh? Am I misunderstanding something? I thought you want to call the chprintf() in a regular intervall? What you're doing now is just calling it once and then you don't do anything anymore.


~ Tectu
M.H.Roohi
Posts: 17
Joined: Wed Mar 20, 2013 1:33 pm

Re: Problem with Serial

Post by M.H.Roohi »

the following code didn't work:

Code: Select all

chprintf((BaseChannel *)&SD2, "test....\r\n");
      chThdSleepMilliseconds(1);


but this code is working:

Code: Select all

       chprintf((BaseChannel *)&SD2, "test....\r\n");
          while(1);


and also this code is working properly:

Code: Select all

     
while(1)
 chprintf((BaseChannel *)&SD2, "test....\r\n");
         
Post Reply