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

Dependencies:   mbed

Committer:
krepemar
Date:
Sun Mar 22 12:17:15 2015 +0000
Revision:
1:2cae2115481a
Parent:
0:0fb9dd105439
Child:
2:3f9a1eb39b79
new comments and variable names

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 1:2cae2115481a 41
krepemar 1:2cae2115481a 42
krepemar 1:2cae2115481a 43 /* Functions----------------------------------------------------------------------*/
krepemar 0:0fb9dd105439 44
krepemar 1:2cae2115481a 45 /*******************************************************************************
krepemar 1:2cae2115481a 46 * Function Name : serialRx.
krepemar 1:2cae2115481a 47 * Description : Saves all received charracters to the buffer.
krepemar 1:2cae2115481a 48 * Input : None
krepemar 1:2cae2115481a 49 * Output : None.
krepemar 1:2cae2115481a 50 * Return : None
krepemar 1:2cae2115481a 51 *******************************************************************************/
krepemar 0:0fb9dd105439 52
krepemar 0:0fb9dd105439 53 void serialRx()
krepemar 0:0fb9dd105439 54 {
krepemar 1:2cae2115481a 55 while(pc.readable()) { // while there is a character available to read.
krepemar 1:2cae2115481a 56 char c=pc.getc(); // receive the charracter
krepemar 1:2cae2115481a 57 buffer[received++]=c; // save the charracter to the next place in buffer, increments number of received charracters
krepemar 0:0fb9dd105439 58 }
krepemar 0:0fb9dd105439 59 }
krepemar 0:0fb9dd105439 60
krepemar 1:2cae2115481a 61 /***********************************************************************************
krepemar 1:2cae2115481a 62 * Function Name : main.
krepemar 1:2cae2115481a 63 * Description : Main routine.
krepemar 1:2cae2115481a 64 * Input : None.
krepemar 1:2cae2115481a 65 * Output : None.
krepemar 1:2cae2115481a 66 * Return : None.
krepemar 1:2cae2115481a 67 ***********************************************************************************/
krepemar 0:0fb9dd105439 68 int main()
krepemar 0:0fb9dd105439 69 {
krepemar 1:2cae2115481a 70 int i = 1; // inkrements every second
krepemar 1:2cae2115481a 71 pc.printf("Program started !\r\n"); // text displayed on a computer
krepemar 1:2cae2115481a 72 pc.attach(&serialRx,Serial::RxIrq); // Attach a function serialRx to call whenever a serial interrupt is generated
krepemar 0:0fb9dd105439 73 while(1) {
krepemar 1:2cae2115481a 74 while(sent<received) { // while the number of received characters is greater than the number of sent characters
krepemar 1:2cae2115481a 75 pc.printf("Received char: %c (%d).\r\n", buffer[sent],(int)buffer[sent]); // send the character and the character number
krepemar 1:2cae2115481a 76 blue = !blue; // indicate this by LED - inverse value of blue LED
krepemar 1:2cae2115481a 77 sent++; // increment number of sent charracters
krepemar 1:2cae2115481a 78 if(sent>received) { // if it sent all characters
krepemar 1:2cae2115481a 79 received=0; // number of received charracters is 0
krepemar 1:2cae2115481a 80 sent=0; // number of sent charracters is 0
krepemar 1:2cae2115481a 81 }
krepemar 0:0fb9dd105439 82 }
krepemar 1:2cae2115481a 83 wait(1); // wait 1 second
krepemar 1:2cae2115481a 84 pc.printf("This program runs since %d seconds.\r\n", i++); // sends how long is the program running
krepemar 1:2cae2115481a 85 green = !green; // inverse value of green LED
krepemar 0:0fb9dd105439 86 }
krepemar 1:2cae2115481a 87 }