8 years, 3 months ago.

Serial attach() function with mbed 2.0 Rev 125 example code

Hi Since the forum won't let us keep the existing thread going I am posting here. There indeed is a valid bug (which has been reported now) in version 125 for Nucleo 303 and other similar boards.

Here is some example code to demonstrate. This code was used to demo to another forum member how to use TxInterrupts so I decided to add Rx Interrupts to it.

#include "mbed.h"

DigitalOut myled(LED1);

RawSerial pc(USBTX, USBRX);
 
volatile uint32_t dont_optimize_me_out;
char my_string[] = "hello world\n";
const int n_my_string = sizeof(my_string)-1;
int ms_i=0;
char rxC = 0;

void rxIRQ()
{
    if(!rxC && pc.readable() )rxC = pc.getc();
}

void txIRQ()
{
        if(pc.writeable() && ms_i < n_my_string)pc.putc(my_string[ms_i++]);
}
 
int main()
{
    pc.baud(230400);
    myled = 1;
    pc.printf("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:%d:",n_my_string);
    wait(5);
    pc.printf("GO BABY GO!");
    pc.attach(txIRQ, RawSerial::TxIrq);
    pc.attach(rxIRQ, RawSerial::RxIrq);
 
    while (true) {
 
        // do some CPU work
        myled = !myled;
        dont_optimize_me_out = 10000000;
        while(dont_optimize_me_out)dont_optimize_me_out--;
        if (ms_i == n_my_string){
            ms_i = 0;
            pc.putc(my_string[ms_i++]);
        }
        if(rxC)pc.putc(rxC);
        rxC=0;

    }
}

Running this with version 122 works fine. Version 125 fails.

Be the first to answer this question.