5 months, 3 weeks ago.

UART Communication Nucleo F072

Hi, i want to send all 2 seconds a command over uart. The answer I want to convert in an analog voltage. At the moment I just want to send the command and show the answer on the pc.

Uart communication

#include "mbed.h"
#define size 512

Ticker write_ticker;

Serial pc(USBTX,USBRX,9600); // UART to PC
Serial easerial(A0, A1,115200);  //Uart to device

char rxBuff[size];//array to store
unsigned int bufferWritingIndex = 0;//Writing Pointer
unsigned int bufferReadingIndex = 0;//Reading Pointer

void RxInterrupt(void);
void readPc(void);
void write_EA(void);
int main(void) 
    // Set setting for UART
    easerial.attach(&RxInterrupt, Serial::RxIrq);
    wait(1);  // 1000 millisecond
    write_ticker.attach(&write_EA,1); // Write all second
    //easerial.printf("moti \r");    // Send command
void RxInterrupt(void)
void readPc(void)
    char rxByte;
    while(bufferReadingIndex != bufferWritingIndex)
        rxByte = rxBuff[bufferReadingIndex++];
        pc.putc(rxByte);                // Write answer of device to pc 

void write_EA() {
      easerial.printf("moti \r");    // Send command

This answer i get at the pc, line 1,2,3 are right and the fourth line is wrong:

include the mbed library with this snippet

<\0><\0>moti <\r><\n>

Why I don’t get every two seconds a response of the device? Why are most symbols of the last line wrong?

Comment on this question

1 Answer

5 months, 2 weeks ago.

Hi Ireno,

The printf() function shouldn't be put in ISR, please refer to Ticker documentation.


No blocking code in ISR: avoid any call to wait, infinite while loop or blocking calls in general.

No printf, malloc or new in ISR: avoid any call to bulky library functions. In particular, certain library functions (such as printf, malloc and new) are not re-entrant, and their behavior could be corrupted when called from an ISR.

While an event is attached to a Ticker, deep sleep is blocked to maintain accurate timing. If you don't need microsecond precision, consider using the LowPowerTicker class instead because that does not block deep sleep mode.

That would cause unexpected behavior.

Desmond, team Mbed

A simple fix:

volatile bool write_Now = false;

// in main()
        if (write_Now) {
          write_Now = false;
          easerial.printf("moti \r");

void write_EA() {
    write_Now  = true;

This won't be exactly 1 second depending on the amount of serial data readPc() has to deal with but it should be fairly close.

posted by Andy A 28 Jan 2019

To post an answer, please log in.