10 years, 10 months ago.

Kl25z strange output printf

Hello guys, I'm having some trouble with my Kl25z specifically when outputing data to the PC for debugging. I'm running Ubuntu and using GNU Screen to read the data that the microcontroller prints. Sometimes the printing stops or wierd chars get printed on the screen. I've tried using different baud rates, different wait()'s in the program but the problem still exists.

void readSensors(int var[])
{
   for(int i = 0; i <= 5; i++)
        var[i] = 0;
   
   for(int i = 0; i <= 5; i++)
   {
        //pc.printf("Starting with sensor %d \r\n", i);
       
        int bw = 0;
        Sensors[i].output();
        Sensors[i].mode(PullUp);
        
        wait_us(2500);
        Sensors[i].mode(PullNone);
        Sensors[i] = 1;
        t.start();
        Sensors[i].input();
        while (Sensors[i] == 1 || t.read_us() < 3000)
            ;
        bw = t.read_us();        
        t.stop(); 
        t.reset();
        
       if (bw > 3100) { myled3 = 0; } else myled3 = 1;
            
       pc.printf("Sensor %d: %d \r\n", i, bw);
   }
}

Hello Andrei Cornescu,

what about to write a simple application which just prints some text each short period of time for example.

I haven't had any problems with printing on KL25Z, and I have used debug messages in some really quite often invoked routines. Build or reuse simple printing example, run it and share it with us, also with outcome.

Is that readSensors part of the interrupt handler?

Regards,
0xc0170

posted by Martin Kojtal 21 Nov 2013

3 Answers

10 years, 8 months ago.

I have the same issue. Very simple program:


  1. include "mbed.h"

Serial USB port Serial pc(USBTX, USBRX);

int main() { pc.baud (9600); while(1) printf("hey there\r\n"); }


As the original poster notes, I've tried several baud rates. Host is ubuntu 13.04, terminal is minicom. The symptom, in this case, is that the "hey there" string will occasionally drop a run of characters, and randomly flip into long runs of unprintable characters. I would like to continue debugging, but likely the path leads into sources that may not be published, i.e. either the mbed firmware, or some of the supporting libraries (I've imported mbed as a build library, but didn't see the associated sources). Are all the source files available somewhere on the mbed.org site?

Rick Spanbauer

Does it change if you add delay, like 1us? or any longer other, just to test?

posted by Martin Kojtal 29 Dec 2013

FYI the source code of the mbed lib you include is: http://mbed.org/users/mbed_official/code/mbed-src/. If you delete the regular mbed lib from your program and import that one you can change stuff if you want to. Although I would think it is unlikely the issue is the KL25z serial source code.

posted by Erik - 29 Dec 2013

Erik - thanks. In the meantime, I did find the source library, and re-compiled. Same results. Just for kicks, I also tried a new, out of the box KL25Z, with the same results. Next steps will be to attach a usb serial port adapter from Sparkfun to entirely bypass the USB controller on the KL25Z.

posted by Rick Spanbauer 29 Dec 2013

One more update. I connected the PTA2 line (TxOut) to a Sparkfun USB->serial adapter, and viewed the results via minicom. No more corruption of the serial output stream. This would seem to narrow down the problem domain somewhat, i.e. to either the USB controller cpu + firmware (I'm running bootloader v1.11 on my board), or possibly something in the Ubuntu usb driver stack as it interacts with the KL25Z usb software.

posted by Rick Spanbauer 29 Dec 2013
10 years, 10 months ago.

You should not use printf in interrupts nor use it in parallel threads. Also make sure that you dont use the pins PTA1 and/or PTA2 anywhere else. These pins are used by the serial port between the KL25Z and the host. Strange printf issues will show up when you dont follow the rules above.

10 years, 9 months ago.

PTA1 and PTA2 are not used. Also I'm not running printf in any interrupts nor in any parallel threads. This problem occurs even in the main() method when i try to print a variable : while(1) { i++; pc.printf("%d \r\n", i); wait(0.5); }

In that case you may either have electrical interference on the KL25Z (maybe an unexpected reset or low voltage) or more likely some issue with the receiving software application. Try using another terminal program like minicom.

posted by Wim Huiskamp 22 Nov 2013