Simply UART communication for STM32F0 Discovery using mbed. Received char is sent back to the computer.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 1:2cae2115481a
- Parent:
- 0:0fb9dd105439
- Child:
- 2:3f9a1eb39b79
--- a/main.cpp Fri Mar 13 09:43:37 2015 +0000 +++ b/main.cpp Sun Mar 22 12:17:15 2015 +0000 @@ -1,38 +1,87 @@ +/********************************************************************************** +* @file main.cpp +* @author Marta Krepelkova +* @version V0.1 +* @date 22-March-2015 +* @brief Simply UART communication for STM32F0 Discovery kit. +* Received character is sent back to your computer. +***********************************************************************************/ + +/**********************************************************************************/ +/* Table of used pins on STM32F0 Discovery kit with STM32F051R8 MCU (LQFP64) */ +/**********************************************************************************/ +/* LQFP64 pin | Discovery pin | ST Nucleo F030R8 pin | peripheral */ +/* 42 | PA_9 | PA_9 | SERIAL_TX */ +/* 43 | PA_10 | PA_10 | SERIAL_RX */ +/* 39 | PC_8 | PA_5 | LED */ +/* 40 | PC_9 | | LED */ +/**********************************************************************************/ + +/* Includes ----------------------------------------------------------------------*/ #include "mbed.h" -//------------------------------------ -// Hyperterminal configuration -// 9600 bauds, 8-bit data, no parity -//------------------------------------ +/* Defines -----------------------------------------------------------------------*/ + +/* Function prototypes -----------------------------------------------------------*/ + +/* Variables ---------------------------------------------------------------------*/ +char buffer[255]; // for receive more characters from the computer +int received=0; // how many characters was received from computer +int sent=0; // how many characters was sent to computer + +// mbed - initialization of peripherals +Serial pc(PA_9, PA_10); // inicialize SERIAL_TX=PA_9, SERIAL_RX=PA_10 +DigitalOut blue(PC_8); // inicialize blue LED on STM32F0 discovery +DigitalOut green(PC_9); // inicialize green LED on STM32F0 discovery -//SERIAL_TX=PA_2/PA_9 Discovery -//SERIAL_RX=PA_3/PA_10 Discovery -Serial pc(PA_2, PA_3); +//-------------------------------------- +// Hyperterminal configuration is default +// 9600 bauds, 8-bit data, no parity +//-------------------------------------- + + +/* Functions----------------------------------------------------------------------*/ -DigitalOut myled(PC_8); -char buffer[255]; -int prijato=0; +/******************************************************************************* +* Function Name : serialRx. +* Description : Saves all received charracters to the buffer. +* Input : None +* Output : None. +* Return : None +*******************************************************************************/ void serialRx() { - while(pc.readable()) { - char c=pc.getc(); - buffer[prijato++]=c; + while(pc.readable()) { // while there is a character available to read. + char c=pc.getc(); // receive the charracter + buffer[received++]=c; // save the charracter to the next place in buffer, increments number of received charracters } } +/*********************************************************************************** +* Function Name : main. +* Description : Main routine. +* Input : None. +* Output : None. +* Return : None. +***********************************************************************************/ int main() { - int i = 1; - pc.printf("Program started !\n"); - pc.attach(&serialRx,Serial::RxIrq); + int i = 1; // inkrements every second + pc.printf("Program started !\r\n"); // text displayed on a computer + pc.attach(&serialRx,Serial::RxIrq); // Attach a function serialRx to call whenever a serial interrupt is generated while(1) { - while(prijato) { - pc.printf("Prisel znak: %c (%d).\n", buffer[prijato-1],(int)buffer[prijato-1]); - prijato--; + while(sent<received) { // while the number of received characters is greater than the number of sent characters + pc.printf("Received char: %c (%d).\r\n", buffer[sent],(int)buffer[sent]); // send the character and the character number + blue = !blue; // indicate this by LED - inverse value of blue LED + sent++; // increment number of sent charracters + if(sent>received) { // if it sent all characters + received=0; // number of received charracters is 0 + sent=0; // number of sent charracters is 0 + } } - wait(1); - pc.printf("This program runs since %d seconds.\n", i++); - myled = !myled; + wait(1); // wait 1 second + pc.printf("This program runs since %d seconds.\r\n", i++); // sends how long is the program running + green = !green; // inverse value of green LED } -} +} \ No newline at end of file