Introduction

This program makes use of two source files. serial.c provides a set of helper functions for serial communications. main.c provides a usage example.

Buffered interrupt driven communications


serial.c implements an interrupt driven serial communications driver that makes use of two circular buffers the size of which can be changed at compile time. The main functions that are exposed to the user are:

void initUART( int BaudRate ): This function initialize the port by configuring the TX and RX pins (PA2 and PA3) for use by USART2. It sets the baud rate to the specified baud rate (assuming the default clock setup using the RC Oscillator is used). It also enables transmission and reception interrupts and sets up a circular buffer for a transmit queue and another for the receive queue.

int ReadCom(int Max,unsigned char *Buffer) : Reads up to Max-1 characters (if any are available) into the provided buffer. Return value is the number of bytes read. A null terminator is placed at the end of the data. Returns -1 if the port is not open.

int WriteCom(int Count,unsigned char *Buffer): Writes Count bytes into the output queue. Returns -1 if the port is not open. Returns -2 if there is not enough room (in such a case, none of the data is queued).

int eputs(char *String) : Similar to fputs except the output stream in question is USART2. It is assumed that String is a null terminated string. The contents of String are queued for transmission

int egets(char *String, int size) : Similar to fgets except the input stream in question is USART2. The input stream is read until either size-1 characters are read or a carriage return character is received (ascii character 13). The returned String is null terminated. Returns the number of characters received.

Using the serial comms library


First of all you will need a 3.3V RS232 Line driver or similar. Olimex do a nice cable for this that also incorporates a USB Serial port
Here it is
This cable is connected to PA2 (Transmit from STM32F0), PA3 (Receive into STM32F0) and 0V. My setup looks like this:
Picture showing breadboard with stm32f0discovery and serial cable attached
Sample code that makes use of the serial library is shown below. This program simply says "Hello World", allow you input some data (not echoed while you type) and then prints back your input. Code, makefiles and scripts are provided here
#include "stm32f05xxx.h"
#include <stdio.h>
void delay(int dly)
{
	while( dly--);
}
char UserInput[10];
int main()
{
	int i=0;
	initUART(9600);
	while(1) {
	  eputs("Hello World\n");  
	  egets(UserInput,10);
	  eputs("You entered : ");
	  eputs(UserInput);
	  eputs("\n");
	  delay(100000);
	}
	return 0;
}

Libraries

The serial comms library needs to do an integer divide when it works out a clock divisor for baudrate (see initUART). The divide function is implemented in a library. On my system, that library is at /home/frank/sat/lib/gcc/arm-none-eabi/4.6.2/thumb/cortex-m0 . This was built when the summon arm toolchain built itself. Your library may be in a different location so you may have to edit the Makefile so that the linker looks in the right place.