wordboot
Macros | Functions | Variables
wordboot.c File Reference

Bootloader for the Wordclock project. More...

#include <avr/boot.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <inttypes.h>
#include "stk500.h"
#include <util/setbaud.h>

Macros

#define WORDBOOT_MAJOR_VERSION   6
 Major version of the bootloader. More...
 
#define WORDBOOT_MINOR_VERSION   0
 Minor version of the bootloader. More...
 
#define LED_START_FLASHES   3
 Default number of LED flashes during startup.
 
#define BAUD_RATE   9600L
 Default baud rate used by the UART hardware module.
 
#define BAUD   BAUD_RATE
 Helper macro to define the baud rate as expected by <util/setbaud.h>
 
#define BOOTLOADER_TIMEOUT_MS   1000
 Default timeout (in ms) after which the application is started. More...
 
#define BOOTLOADER_TIMEOUT_COMPARE_VALUE   F_CPU / 256 / 256 * 1000 / BOOTLOADER_TIMEOUT_MS
 Compare value for timeout. More...
 

Functions

int main (int argc, char *argv[]) __attribute__((OS_main)) __attribute__((section(".init9")))
 
void __attribute__ ((noinline))
 
void put_ch (char ch)
 Outputs a single character via UART.
 
uint8_t get_ch ()
 Returns character received via UART. More...
 
void drop_ch (uint8_t count)
 Drops given amount of characters. More...
 
void verify_command_terminator ()
 Verifies that a valid command terminator has been received. More...
 
void flash_start_leds (uint8_t count)
 Flashes the LEDs at startup. More...
 
void toggle_minute_leds ()
 Toggles the minute LEDs. More...
 
void start_application ()
 Resets the used hardware and jumps to the application. More...
 
static void write_memory (char memtype, uint8_t *buffer, uint16_t address, uint8_t length)
 Writes data from given buffer to the specified memory location. More...
 
static void read_memory (char memtype, uint16_t address, uint8_t length)
 Read chunk of memory and put it out via UART. More...
 

Variables

static uint8_t mcusr
 Content of the MCU status register. More...
 
static uint8_t page_buffer [256]
 Buffer for memory write operations. More...
 

Detailed Description

Bootloader for the Wordclock project.

This file contains the officially supported bootloader for the Wordclock project. It is based upon optiboot, but was heavily modified. These modifications not only include massive clean-ups and simplifications, but also add support to indicate the current status on the Wordclock frontpanel.

Macro Definition Documentation

#define BOOTLOADER_TIMEOUT_COMPARE_VALUE   F_CPU / 256 / 256 * 1000 / BOOTLOADER_TIMEOUT_MS

Compare value for timeout.

This is the actual value that the timeout counter is being compared against to determine whether the bootloader should be exited.

See also
BOOTLOADER_TIMEOUT_MS
get_ch()
#define BOOTLOADER_TIMEOUT_MS   1000

Default timeout (in ms) after which the application is started.

See also
get_ch()
#define WORDBOOT_MAJOR_VERSION   6

Major version of the bootloader.

This version number is being returned by the bootloader (along with WORDBOOT_MINOR_VERSION) and is evaluated by the programmer, e.g. avrdude.

See also
WORDBOOT_MINOR_VERSION
#define WORDBOOT_MINOR_VERSION   0

Minor version of the bootloader.

This version number is being returned by the bootloader (along with WORDBOOT_MAJOR_VERSION) and is evaluated by the programmer, e.g. avrdude.

See also
WORDBOOT_MAJOR_VERSION

Function Documentation

void drop_ch ( uint8_t  count)

Drops given amount of characters.

This retrieves count amount of characters from the UART hardware and simply drops them. It is useful whenever bytes are expected as part of a command, but don't actually need to be evaluated.

See also
get_ch()
void flash_start_leds ( uint8_t  count)

Flashes the LEDs at startup.

This flashes the leds count times at the startup to indicate that the bootloader has been accessed. It is polling the overflow flag of Timer1, and toggling the LEDs by writing to the appropriate PIN register.

Parameters
countNumber of flashes
uint8_t get_ch ( )

Returns character received via UART.

This function busy waits until a new character has been received. Concurrently a counter is incremented and compared against BOOTLOADER_TIMEOUT_COMPARE_VALUE. Once the value is reached, the actual application is started.

Returns
Received character, void if start_application() is executed
See also
BOOTLOADER_TIMEOUT_COMPARE_VALUE
start_application()
static void read_memory ( char  memtype,
uint16_t  address,
uint8_t  length 
)
inlinestatic

Read chunk of memory and put it out via UART.

This reads length locations of the specified memory (EEPROM and/or Flash) starting at address and puts them directly out via UART.

Parameters
memtype'E' for EEPROM, 'F' (or anything else) for Flash
addressAddress to start to read from
lengthNumber of address locations to read
See also
put_ch()
eeprom_read_byte()
pgm_read_byte()
void start_application ( )

Resets the used hardware and jumps to the application.

This resets all of the used hardware by writing zero to the appropriate registers. Before jumping to the actual application the content of mcusr is put into r2, so it can be accessed by the application itself.

See also
mcusr
void toggle_minute_leds ( )

Toggles the minute LEDs.

This toggles the PWM channels by writing to the appropriate PINx register. As only the minute LEDs are enabled, this effectively will toggle the minute LEDs.

void verify_command_terminator ( )

Verifies that a valid command terminator has been received.

This verifies that Sync_CRC_EOP has been received, which terminates each command and in return responds with Resp_STK_INSYNC. In case no valid command terminator was received the watchdog is enabled to reset the MCU.

See also
Sync_CRC_EOP
Resp_STK_INSYNC
static void write_memory ( char  memtype,
uint8_t *  buffer,
uint16_t  address,
uint8_t  length 
)
inlinestatic

Writes data from given buffer to the specified memory location.

This writes length bytes of data from the given buffer to the specified memory (EEPROM and/or flash) location, starting at address. It uses eeprom_write_byte() for writing to the EEPROM and functions from <avr/boot.h> for the flash memory.

Parameters
memtype'E' for EEPROM, 'F' (or anything else) for Flash
addressAddress to start to write to
lengthNumber of bytes to write
See also
put_ch()
eeprom_write_byte()
<avr/boot.h>

Variable Documentation

uint8_t mcusr
static

Content of the MCU status register.

This variable is being used to save the content of MCUSR during start up. MCUSR is reset immediately, so the source of the next reset can be determined reliably. This variable is passed to the main application using r2 before jumping to the actual reset vector.

See also
main()
start_application()
uint8_t page_buffer[256]
static

Buffer for memory write operations.

This buffer is used for write operations to memory. Before a write is initiated, a page worth of content is received and put into the buffer. The buffer is then passed on to write_memory().

See also
write_memory()