LUFA Library - DFU Class Bootloader
BootloaderDFU.c File Reference
#include "BootloaderDFU.h"

Functions

void Application_Jump_Check (void)
 
int main (void)
 
static void SetupHardware (void)
 
static void ResetHardware (void)
 
 ISR (TIMER1_OVF_vect, ISR_BLOCK)
 
void EVENT_USB_Device_ControlRequest (void)
 
static void DiscardFillerBytes (uint8_t NumberOfBytes)
 
static void ProcessBootloaderCommand (void)
 
static void LoadStartEndAddresses (void)
 
static void ProcessMemProgCommand (void)
 
static void ProcessMemReadCommand (void)
 
static void ProcessWriteCommand (void)
 
static void ProcessReadCommand (void)
 

Variables

static bool IsSecure = SECURE_MODE
 
static bool RunBootloader = true
 
static bool WaitForExit = false
 
static uint8_t DFU_State = dfuIDLE
 
static uint8_t DFU_Status = OK
 
static DFU_Command_t SentCommand
 
static uint8_t ResponseByte
 
static AppPtr_t AppStartPtr = (AppPtr_t)0x0000
 
static uint8_t Flash64KBPage = 0
 
static uint16_t StartAddr = 0x0000
 
static uint16_t EndAddr = 0x0000
 
uint16_t MagicBootKey
 

Detailed Description

Main source file for the DFU class bootloader. This file contains the complete bootloader logic.

Function Documentation

void Application_Jump_Check ( void  )

Special startup routine to check if the bootloader was started via a watchdog reset, and if the magic application start key has been loaded into MagicBootKey. If the bootloader started via the watchdog and the key is valid, this will force the user application to start via a software jump.

static void DiscardFillerBytes ( uint8_t  NumberOfBytes)
static

Routine to discard the specified number of bytes from the control endpoint stream. This is used to discard unused bytes in the stream from the host, including the memory program block suffix.

Parameters
[in]NumberOfBytesNumber of bytes to discard from the host from the control endpoint
void EVENT_USB_Device_ControlRequest ( void  )

Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to the device from the USB host before passing along unhandled control requests to the library for processing internally.

ISR ( TIMER1_OVF_vect  ,
ISR_BLOCK   
)

ISR to periodically toggle the LEDs on the board to indicate that the bootloader is active.

static void LoadStartEndAddresses ( void  )
static

Routine to concatenate the given pair of 16-bit memory start and end addresses from the host, and store them in the StartAddr and EndAddr global variables.

int main ( void  )

Main program entry point. This routine configures the hardware required by the bootloader, then continuously runs the bootloader processing routine until instructed to soft-exit, or hard-reset via the watchdog to start the loaded application code.

static void ProcessBootloaderCommand ( void  )
static

Routine to process an issued command from the host, via a DFU_DNLOAD request wrapper. This routine ensures that the command is allowed based on the current secure mode flag value, and passes the command off to the appropriate handler function.

static void ProcessMemProgCommand ( void  )
static

Handler for a Memory Program command issued by the host. This routine handles the preparations needed to write subsequent data from the host into the specified memory.

static void ProcessMemReadCommand ( void  )
static

Handler for a Memory Read command issued by the host. This routine handles the preparations needed to read subsequent data from the specified memory out to the host, as well as implementing the memory blank check command.

static void ProcessReadCommand ( void  )
static

Handler for a Data Read command issued by the host. This routine handles bootloader information retrieval commands such as device signature and bootloader version retrieval.

static void ProcessWriteCommand ( void  )
static

Handler for a Data Write command issued by the host. This routine handles non-programming commands such as bootloader exit (both via software jumps and hardware watchdog resets) and flash memory erasure.

static void ResetHardware ( void  )
static

Resets all configured hardware required for the bootloader back to their original states.

static void SetupHardware ( void  )
static

Configures all hardware required for the bootloader.

Variable Documentation

AppPtr_t AppStartPtr = (AppPtr_t)0x0000
static

Pointer to the start of the user application. By default this is 0x0000 (the reset vector), however the host may specify an alternate address when issuing the application soft-start command.

uint8_t DFU_State = dfuIDLE
static

Current DFU state machine state, one of the values in the DFU_State_t enum.

uint8_t DFU_Status = OK
static

Status code of the last executed DFU command. This is set to one of the values in the DFU_Status_t enum after each operation, and returned to the host when a Get Status DFU request is issued.

uint16_t EndAddr = 0x0000
static

Memory end address, indicating the end address to read from/write to in the memory being addressed (either FLASH of EEPROM depending on the issued command from the host).

uint8_t Flash64KBPage = 0
static

64-bit flash page number. This is concatenated with the current 16-bit address on USB AVRs containing more than 64KB of flash memory.

bool IsSecure = SECURE_MODE
static

Flag to indicate if the bootloader is currently running in secure mode, disallowing memory operations other than erase. This is initially set to the value set by SECURE_MODE, and cleared by the bootloader once a memory erase has completed in a bootloader session.

uint16_t MagicBootKey

Magic lock for forced application start. If the HWBE fuse is programmed and BOOTRST is unprogrammed, the bootloader will start if the /HWB line of the AVR is held low and the system is reset. However, if the /HWB line is still held low when the application attempts to start via a watchdog reset, the bootloader will re-start. If set to the value MAGIC_BOOT_KEY the special init function Application_Jump_Check() will force the application to start.

uint8_t ResponseByte
static

Response to the last issued Read Data DFU command. Unlike other DFU commands, the read command requires a single byte response from the bootloader containing the read data when the next DFU_UPLOAD command is issued by the host.

bool RunBootloader = true
static

Flag to indicate if the bootloader should be running, or should exit and allow the application code to run via a soft reset. When cleared, the bootloader will abort, the USB interface will shut down and the application jumped to via an indirect jump to location 0x0000 (or other location specified by the host).

DFU_Command_t SentCommand
static

Data containing the DFU command sent from the host.

uint16_t StartAddr = 0x0000
static

Memory start address, indicating the current address in the memory being addressed (either FLASH or EEPROM depending on the issued command from the host).

bool WaitForExit = false
static

Flag to indicate if the bootloader is waiting to exit. When the host requests the bootloader to exit and jump to the application address it specifies, it sends two sequential commands which must be properly acknowledged. Upon reception of the first the RunBootloader flag is cleared and the WaitForExit flag is set, causing the bootloader to wait for the final exit command before shutting down.