Using GCC with the MBED system

Serial line driver example

Here is an example program using the MCP MBED board, GCC and a linux PC to echo a string from the board to a console on the host PC, (Console is acessed by running a command screen /dev/ttyACM0, assuming the MBed system is on port ttyACM0). the contents of the tar file are explained below.


How to write the Makefile for this project can be found here, giving the paths to the tool box.


The following code segment id the include file serial.h defining the local functions used in the program.

int read_usb_serial_none_blocking(char *buf,int length);
int write_usb_serial_blocking(char *buf,int length);
void serial_init(void);


Here is a simple example program to send a string to the USB serial port on the MBED board, it shows how to start up the device, and talk to the decide.

Firstly include files from the CMSIS device driver library

#include "lpc17xx_uart.h"
#include "lpc17xx_pinsel.h"
#include "lpc_types.h"
Local functions defined in the following include file
#include "serial.h"
Entery point for the program, setting the serial line up, sending a string to the serial line and then dropping into and endless look
void main(void)
{
	serial_init();
	write_usb_serial_blocking("USB test code\n\r",16);
	while(1);
}
Read function wrapper, calling the CMSIS function
int read_usb_serial_none_blocking(char *buf,int length)
{
	return(UART_Receive((LPC_UART_TypeDef *)LPC_UART0, (uint8_t *)buf, length, NONE_BLOCKING));
}
Write function wrapper, calling the CMSIS function
int write_usb_serial_blocking(char *buf,int length)
{
	return(UART_Send((LPC_UART_TypeDef *)LPC_UART0,(uint8_t *)buf,length, BLOCKING));
}
Init code for the USB serial line, or UART0,
void serial_init(void)
{
Data types needed to setup the serial port, defined here.
	UART_CFG_Type 		UARTConfigStruct;	// UART Configuration structure variable
	UART_FIFO_CFG_Type 	UARTFIFOConfigStruct;	// UART FIFO configuration Struct variable
	PINSEL_CFG_Type 	PinCfg;			// Pin configuration for UART
Initialize UART pins, this is the same for any device used on the MBED board see here for port number/pin/mode etc settings.
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	
	PinCfg.Portnum = 0;
	PinCfg.Pinnum = 2;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 3;
	PINSEL_ConfigPin(&PinCfg);
Initialize UART Configuration parameter structure to default state given in the following table. These can be changed once the structure is configured.
Baudrate9600bps
Data bits8
Stop bits1
ParityNone
		
	UART_ConfigStructInit(&UARTConfigStruct);
Initialize FIFOConfigStruct to default state, again these can be changed, once the structure is configured.
FIFO_DMAModeDISABLE
FIFO_LevelUART_FIFO_TRGLEV0
FIFO_ResetRxBufENABLE
FIFO_ResetTxBufENABLE
FIFO_StateENABLE
	
	UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
Setup the basic data structures for the device, lets start the device, in this case UART0 (USB serial device)
	UART_Init((LPC_UART_TypeDef *)LPC_UART0, &UARTConfigStruct);
Now the basic I/O is started, we need to initialize FIFO for UART0 peripheral
	UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &UARTFIFOConfigStruct);
Enable UART0 Transmit
	UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE);
	
}


pcc@cs.york.ac.uk
Return