A trivial example of a UART comm. Works on all Nucleos.

Dependencies:   mbed

Committer:
Foxnec
Date:
Thu Mar 19 14:01:26 2015 +0000
Revision:
3:d470d455b369
Parent:
2:bdc32e32f6f1
Final version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Foxnec 3:d470d455b369 1 /**********************************************************************************
Foxnec 3:d470d455b369 2 * @file main.cpp
Foxnec 3:d470d455b369 3 * @author Petr Dousa, Daniel Toms
Foxnec 3:d470d455b369 4 * @version V0.1
Foxnec 3:d470d455b369 5 * @date 05-March-2015
Foxnec 3:d470d455b369 6 * @brief Nucleo UART example - sends and receives data over serial
Foxnec 3:d470d455b369 7 ***********************************************************************************/
Foxnec 3:d470d455b369 8
Foxnec 3:d470d455b369 9 /* Includes ----------------------------------------------------------------------*/
Foxnec 0:389db7a2ed7f 10 #include "mbed.h"
Foxnec 1:a149bb0c3d05 11 #include <string>
Foxnec 0:389db7a2ed7f 12
Foxnec 3:d470d455b369 13 #define INPUT_BUFFER_SIZE 256
Foxnec 3:d470d455b369 14
Foxnec 2:bdc32e32f6f1 15 void flushSerialPort();
Foxnec 0:389db7a2ed7f 16
Foxnec 0:389db7a2ed7f 17 Serial pc(SERIAL_TX, SERIAL_RX);
Foxnec 0:389db7a2ed7f 18
Foxnec 0:389db7a2ed7f 19 DigitalOut myled(LED1);
Foxnec 0:389db7a2ed7f 20
Foxnec 0:389db7a2ed7f 21 int main()
Foxnec 0:389db7a2ed7f 22 {
Foxnec 1:a149bb0c3d05 23 int i;
Foxnec 3:d470d455b369 24 char text[INPUT_BUFFER_SIZE]; //input buffer
Foxnec 1:a149bb0c3d05 25
Foxnec 3:d470d455b369 26 //------------------------------------
Foxnec 3:d470d455b369 27 // Hyperterminal configuration
Foxnec 3:d470d455b369 28 // 9600 bauds, 8-bit data, no parity
Foxnec 3:d470d455b369 29 // --------------------------------
Foxnec 3:d470d455b369 30 // Communication settings:
Foxnec 3:d470d455b369 31 // pc.format(bits,parity,stop_bits)
Foxnec 3:d470d455b369 32 // bits: 5-8
Foxnec 3:d470d455b369 33 // parity: SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0
Foxnec 3:d470d455b369 34 // stop_bits: 1 or 2
Foxnec 3:d470d455b369 35 // pc.baudrate(baud)
Foxnec 3:d470d455b369 36 // baud: The desired baudrate (speed)
Foxnec 3:d470d455b369 37 // --------------------------------
Foxnec 3:d470d455b369 38
Foxnec 3:d470d455b369 39 pc.format(8,SerialBase::None,1); // This is the default setting
Foxnec 3:d470d455b369 40 pc.baud(9600); // This is the default setting
Foxnec 3:d470d455b369 41
Foxnec 3:d470d455b369 42 //Sends a welcome message over UART (Serial port)
Foxnec 3:d470d455b369 43 //Checks if the port is writeable first
Foxnec 3:d470d455b369 44 if (pc.writeable())
Foxnec 3:d470d455b369 45 pc.printf("Hello World !\n-----------------\nWrite something...\n");
Foxnec 1:a149bb0c3d05 46
Foxnec 0:389db7a2ed7f 47 while(1) {
Foxnec 0:389db7a2ed7f 48
Foxnec 1:a149bb0c3d05 49 wait(1);
Foxnec 2:bdc32e32f6f1 50
Foxnec 3:d470d455b369 51 myled = !myled; //blinking led indicates activity
Foxnec 2:bdc32e32f6f1 52
Foxnec 3:d470d455b369 53 //NOTE: the data must be terminated with CR(Carriage return) '\r'
Foxnec 3:d470d455b369 54 // You can however choose a different terminating character.
Foxnec 2:bdc32e32f6f1 55
Foxnec 1:a149bb0c3d05 56 i=0;
Foxnec 2:bdc32e32f6f1 57 while ((text[i-1] != '\r') && (i<0xFF))
Foxnec 2:bdc32e32f6f1 58 {
Foxnec 3:d470d455b369 59 if (pc.readable()) //Checks if there are data to be received and stores them into the input buffer
Foxnec 3:d470d455b369 60 {
Foxnec 3:d470d455b369 61 text[i++] = getc(pc); //Note: you could use pc.scanf() instead of getc.
Foxnec 3:d470d455b369 62 //pc.scanf() has the same syntax like in regular C apps.
Foxnec 3:d470d455b369 63 }
Foxnec 1:a149bb0c3d05 64 }
Foxnec 1:a149bb0c3d05 65
Foxnec 3:d470d455b369 66 if (pc.writeable())
Foxnec 3:d470d455b369 67 {
Foxnec 3:d470d455b369 68 pc.printf("Received data: %s", text); //Sends the data back for confirmation.
Foxnec 3:d470d455b369 69 memset(&text, 0, i);
Foxnec 3:d470d455b369 70 }
Foxnec 3:d470d455b369 71
Foxnec 3:d470d455b369 72 //flushSerialPort(pc); //Erases all the data waiting to be received
Foxnec 1:a149bb0c3d05 73
Foxnec 0:389db7a2ed7f 74 }
Foxnec 0:389db7a2ed7f 75 }
Foxnec 0:389db7a2ed7f 76
Foxnec 3:d470d455b369 77 //Erases all the data waiting to be received
Foxnec 2:bdc32e32f6f1 78 void flushSerialPort()
Foxnec 0:389db7a2ed7f 79 {
Foxnec 0:389db7a2ed7f 80
Foxnec 2:bdc32e32f6f1 81 while(pc.readable())
Foxnec 2:bdc32e32f6f1 82 pc.getc();
Foxnec 2:bdc32e32f6f1 83
Foxnec 0:389db7a2ed7f 84 return;
Foxnec 0:389db7a2ed7f 85 }