BSP430  20141115
Board Support Package for MSP430 microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Bootstrapping a New Platform: Console Output

BSP430 provides a standard console facility which gives you a 9600 baud 8N1 terminal along with cprintf() and related routines to output formatted text. It also supports input with cgetchar().

This application loops forever, printing a counter and blinking an LED every half second, and displaying any input characters that were received between outputs. The LEDs are a visual cue that the program is running even if something's wrong with the serial output.

Be aware that, unless BSP430_CONSOLE_RX_BUFFER_SIZE is set to a positive value, the receive infrastructure has only the one byte UART buffer available, and typing more than that between display updates will result in lost characters. This can be achieved by adding the following to the make command line when building the application:

AUX_CPPFLAGS=-DBSP430_CONSOLE_RX_BUFFER_SIZE=16

APP_VERBOSE may be defined to 1 while building this application; with that flag, the number of characters transmitted and received, and the UART status register, are displayed on each iteration:

AUX_CPPFLAGS=-DAPP_VERBOSE=1

main.c

/* We're going to use LEDs so we need the interface. */
/* We want to know the nominal clock speed so we can delay the
* appropriate interval. */
#include <bsp430/clock.h>
/* We require console support as verified by <bsp430/platform.h> */
#ifndef BSP430_CONSOLE
#error No console UART PERIPH handle has been defined
#endif /* BSP430_CONSOLE */
void main ()
{
int rv;
#if (APP_VERBOSE - 0)
#endif /* APP_VERBOSE */
/* Initialize platform. */
/* Turn on the first LED */
/* Configure the console to use the default UART handle */
/* assert(0 == rv); */
#if (APP_VERBOSE - 0)
console = hBSP430console();
#endif /* APP_VERBOSE */
/* Indicate we made it this far. */
if (0 > rv) {
/* Failed to configure the UART HAL */
} else {
unsigned int counter = 0;
/* Now blink and display a counter value every nominal half
* second. The blink proves that the loop is running even if
* nothing's coming out the serial port. On each iteration,
* attempt to read a character and display it if one is
* present. */
while (1) {
int rc;
#if (BSP430_CONSOLE_RX_BUFFER_SIZE - 0) || (BSP430_CONSOLE_TX_BUFFER_SIZE - 0)
/* If we're using interrupt-driven transmission or reception, we'd
* better enable interrupts. */
#endif /* BSP430_CONSOLE_RX_BUFFER_SIZE */
#if (BSP430_CONSOLE_RX_BUFFER_SIZE - 0) || (BSP430_CONSOLE_TX_BUFFER_SIZE - 0)
#endif /* BSP430_CONSOLE_RX_BUFFER_SIZE */
while (0 <= ((rc = cgetchar()))) {
cputtext(" rx char ");
cputu(rc, 10);
cputtext(" '");
cputchar(rc);
cputtext("'");
}
cputtext("\nctr ");
cputu(counter, 10);
#if (APP_VERBOSE - 0)
cputtext("; tx ");
cputul(console->num_tx, 10);
cputtext("; rx ");
cputul(console->num_rx, 10);
cputtext("; stat 0x");
#if (configBSP430_SERIAL_USE_USCI - 0)
cputu(console->hpl.usci->stat, 16);
#endif
#if (configBSP430_SERIAL_USE_USCI5 - 0)
cputu(console->hpl.usci5->stat, 16);
#endif
#if (configBSP430_SERIAL_USE_EUSCI - 0)
cputu(console->hpl.euscia->statw, 16);
#endif
#endif /* APP_VERBOSE */
++counter;
}
}
}

bsp430_config.h

/* Application does output: support spin-for-jumper */
#ifndef configBSP430_PLATFORM_SPIN_FOR_JUMPER
#define configBSP430_PLATFORM_SPIN_FOR_JUMPER 1
#endif /* configBSP430_PLATFORM_SPIN_FOR_JUMPER */
/* Request console resources */
#define configBSP430_CONSOLE 1
/* In verbose mode, the number of characters transmitted and received, and
* the raw status of the UART, are displayed on each iteration. */
#ifndef APP_VERBOSE
#define APP_VERBOSE 0
#endif /* APP_VERBOSE */

Makefile

PLATFORM ?= exp430fr5739
MODULES=$(MODULES_PLATFORM) $(MODULES_CONSOLE)
SRC=main.c
include $(BSP430_ROOT)/make/Makefile.common