9 years ago.

FRDM-K64F serial.attach() seems to only work on some ports?

Hi,

I have a FRDM-K64F board I have tested both the following examples found at http://developer.mbed.org/handbook/Serial with the usb-serial link to my computer and a GPS module connected to pins PTC15, PTC14 that outputs data regularly to aid testing:

Working Example: Provide a serial pass-through between the PC and an external UART

#include "mbed.h"
 
Serial pc(USBTX, USBRX); // tx, rx
Serial device(PTC15, PTC14);  // tx, rx
 
int main() {
    while(1) {
        if(pc.readable()) {
            device.putc(pc.getc());
        }
        if(device.readable()) {
            pc.putc(device.getc());
        }
    }
}

Working Example: Attach to RX Interrupt

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
 
Serial pc(USBTX, USBRX);
 
void callback() {
    // Note: you need to actually read from the serial to clear the RX interrupt
    printf("%c\n", pc.getc());
    led2 = !led2;
}
 
int main() {
    pc.attach(&callback);
    
    while (1) {
        led1 = !led1;
        wait(0.5);
    }
}

They work as expected - so the hardware setup works for both serial links and the GPS module connected to PTC15, PTC14 works.

Now if I run the following code, led2 comes on when a byte is received in the function callback as expected. (Using USBTX, USBRX)

Using USBTX/RX does work

#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);

Serial pc(USBTX, USBRX);
//Serial pc(PTC15, PTC14);

void callback() {
    pc.printf("%c\n", pc.getc());
    led2 = 0;   // on
}
 
int main() {
    pc.attach(&callback);
    led2 = 1;   // Off
    while(1) {
        led1 = !led1;
        wait(0.5);
    }
}

However if I run the following code, led2 does NOT come on when a byte is received in the function callback as expected. (Using PTC15, PTC14) Even though I know the port works and data is being fed into it. What's happening?

Using PTC15/14 does NOT work

#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);

//Serial pc(USBTX, USBRX);
Serial pc(PTC15, PTC14);

void callback() {
    pc.printf("%c\n", pc.getc());
    led2 = 0;   // on
}
 
int main() {
    pc.attach(&callback);
    led2 = 1;   // Off
    while(1) {
        led1 = !led1;
        wait(0.5);
    }
}

Many Thanks, Robin

Could possibly be a bug where it does not properly work with UART 3 and UART 4, since the K22F does not have these and they share a code base. However quick compile time test does say that they are correctly enabled.

posted by Erik - 19 Apr 2015

We should look at how PTC15 and 14 are set. ERik, KSDK is generic set of drivers , it should work :)

posted by Martin Kojtal 20 Apr 2015

1 Answer

8 years, 10 months ago.

What is preventing you from using PT17 and PT16?

What GPS module are you using? According to the manual, the Bluetooth module pins are intended to interface with JY-MCU BT board V1.05 BT. You can connect any serial module, but the signals do not conform to RS-232 and are only 0-3.3V. If you are using an RS-232 device, you could try to use the 5V pin on the board or use a level shifter.