Simply UART communication for STM32F0 Discovery using mbed. Received char is sent back to the computer.

Dependencies:   mbed

Committer:
krepemar
Date:
Sat Apr 04 11:15:32 2015 +0000
Revision:
2:3f9a1eb39b79
Parent:
1:2cae2115481a
Child:
3:edbb83dae353
New comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
krepemar 1:2cae2115481a 1 /**********************************************************************************
krepemar 1:2cae2115481a 2 * @file main.cpp
krepemar 1:2cae2115481a 3 * @author Marta Krepelkova
krepemar 1:2cae2115481a 4 * @version V0.1
krepemar 1:2cae2115481a 5 * @date 22-March-2015
krepemar 1:2cae2115481a 6 * @brief Simply UART communication for STM32F0 Discovery kit.
krepemar 1:2cae2115481a 7 * Received character is sent back to your computer.
krepemar 1:2cae2115481a 8 ***********************************************************************************/
krepemar 1:2cae2115481a 9
krepemar 1:2cae2115481a 10 /**********************************************************************************/
krepemar 1:2cae2115481a 11 /* Table of used pins on STM32F0 Discovery kit with STM32F051R8 MCU (LQFP64) */
krepemar 1:2cae2115481a 12 /**********************************************************************************/
krepemar 1:2cae2115481a 13 /* LQFP64 pin | Discovery pin | ST Nucleo F030R8 pin | peripheral */
krepemar 1:2cae2115481a 14 /* 42 | PA_9 | PA_9 | SERIAL_TX */
krepemar 1:2cae2115481a 15 /* 43 | PA_10 | PA_10 | SERIAL_RX */
krepemar 1:2cae2115481a 16 /* 39 | PC_8 | PA_5 | LED */
krepemar 1:2cae2115481a 17 /* 40 | PC_9 | | LED */
krepemar 1:2cae2115481a 18 /**********************************************************************************/
krepemar 1:2cae2115481a 19
krepemar 1:2cae2115481a 20 /* Includes ----------------------------------------------------------------------*/
krepemar 0:0fb9dd105439 21 #include "mbed.h"
krepemar 0:0fb9dd105439 22
krepemar 1:2cae2115481a 23 /* Defines -----------------------------------------------------------------------*/
krepemar 1:2cae2115481a 24
krepemar 1:2cae2115481a 25 /* Function prototypes -----------------------------------------------------------*/
krepemar 1:2cae2115481a 26
krepemar 1:2cae2115481a 27 /* Variables ---------------------------------------------------------------------*/
krepemar 1:2cae2115481a 28 char buffer[255]; // for receive more characters from the computer
krepemar 1:2cae2115481a 29 int received=0; // how many characters was received from computer
krepemar 1:2cae2115481a 30 int sent=0; // how many characters was sent to computer
krepemar 1:2cae2115481a 31
krepemar 1:2cae2115481a 32 // mbed - initialization of peripherals
krepemar 1:2cae2115481a 33 Serial pc(PA_9, PA_10); // inicialize SERIAL_TX=PA_9, SERIAL_RX=PA_10
krepemar 1:2cae2115481a 34 DigitalOut blue(PC_8); // inicialize blue LED on STM32F0 discovery
krepemar 1:2cae2115481a 35 DigitalOut green(PC_9); // inicialize green LED on STM32F0 discovery
krepemar 0:0fb9dd105439 36
krepemar 1:2cae2115481a 37 //--------------------------------------
krepemar 1:2cae2115481a 38 // Hyperterminal configuration is default
krepemar 1:2cae2115481a 39 // 9600 bauds, 8-bit data, no parity
krepemar 1:2cae2115481a 40 //--------------------------------------
krepemar 2:3f9a1eb39b79 41 // Communication settings:
krepemar 2:3f9a1eb39b79 42 // pc.format(bits,parity,stop_bits)
krepemar 2:3f9a1eb39b79 43 // bits: 5-8
krepemar 2:3f9a1eb39b79 44 // parity: SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0
krepemar 2:3f9a1eb39b79 45 // stop_bits: 1 or 2
krepemar 2:3f9a1eb39b79 46 // pc.baudrate(baud)
krepemar 2:3f9a1eb39b79 47 // baud: The desired baudrate (speed)
krepemar 2:3f9a1eb39b79 48 //--------------------------------------
krepemar 2:3f9a1eb39b79 49 // Example for default settings:
krepemar 2:3f9a1eb39b79 50 // pc.format(8,SerialBase::None,1);
krepemar 2:3f9a1eb39b79 51 // pc.baud(9600);
krepemar 2:3f9a1eb39b79 52 //--------------------------------------
krepemar 1:2cae2115481a 53
krepemar 1:2cae2115481a 54 /* Functions----------------------------------------------------------------------*/
krepemar 0:0fb9dd105439 55
krepemar 1:2cae2115481a 56 /*******************************************************************************
krepemar 1:2cae2115481a 57 * Function Name : serialRx.
krepemar 1:2cae2115481a 58 * Description : Saves all received charracters to the buffer.
krepemar 1:2cae2115481a 59 * Input : None
krepemar 1:2cae2115481a 60 * Output : None.
krepemar 1:2cae2115481a 61 * Return : None
krepemar 2:3f9a1eb39b79 62 *******************************************************************************/
krepemar 0:0fb9dd105439 63
krepemar 0:0fb9dd105439 64 void serialRx()
krepemar 0:0fb9dd105439 65 {
krepemar 2:3f9a1eb39b79 66 while(pc.readable()) { // while there is a character available to read.
krepemar 1:2cae2115481a 67 char c=pc.getc(); // receive the charracter
krepemar 1:2cae2115481a 68 buffer[received++]=c; // save the charracter to the next place in buffer, increments number of received charracters
krepemar 0:0fb9dd105439 69 }
krepemar 0:0fb9dd105439 70 }
krepemar 0:0fb9dd105439 71
krepemar 1:2cae2115481a 72 /***********************************************************************************
krepemar 1:2cae2115481a 73 * Function Name : main.
krepemar 1:2cae2115481a 74 * Description : Main routine.
krepemar 1:2cae2115481a 75 * Input : None.
krepemar 1:2cae2115481a 76 * Output : None.
krepemar 1:2cae2115481a 77 * Return : None.
krepemar 1:2cae2115481a 78 ***********************************************************************************/
krepemar 0:0fb9dd105439 79 int main()
krepemar 0:0fb9dd105439 80 {
krepemar 1:2cae2115481a 81 int i = 1; // inkrements every second
krepemar 1:2cae2115481a 82 pc.printf("Program started !\r\n"); // text displayed on a computer
krepemar 2:3f9a1eb39b79 83 pc.attach(&serialRx,Serial::RxIrq); // Attach a function serialRx to call whenever a serial interrupt is generated
krepemar 0:0fb9dd105439 84 while(1) {
krepemar 1:2cae2115481a 85 while(sent<received) { // while the number of received characters is greater than the number of sent characters
krepemar 1:2cae2115481a 86 pc.printf("Received char: %c (%d).\r\n", buffer[sent],(int)buffer[sent]); // send the character and the character number
krepemar 1:2cae2115481a 87 blue = !blue; // indicate this by LED - inverse value of blue LED
krepemar 1:2cae2115481a 88 sent++; // increment number of sent charracters
krepemar 1:2cae2115481a 89 if(sent>received) { // if it sent all characters
krepemar 1:2cae2115481a 90 received=0; // number of received charracters is 0
krepemar 1:2cae2115481a 91 sent=0; // number of sent charracters is 0
krepemar 1:2cae2115481a 92 }
krepemar 0:0fb9dd105439 93 }
krepemar 1:2cae2115481a 94 wait(1); // wait 1 second
krepemar 1:2cae2115481a 95 pc.printf("This program runs since %d seconds.\r\n", i++); // sends how long is the program running
krepemar 1:2cae2115481a 96 green = !green; // inverse value of green LED
krepemar 0:0fb9dd105439 97 }
krepemar 1:2cae2115481a 98 }