LUFA ships with several basic pre-made board drivers, to control hardware present on the supported board hardware - such as Dataflash ICs, LEDs, Joysticks, or other hardware peripherals. When compiling an application which makes use of one or more board drivers located in LUFA/Drivers/Board, you must also indicate which board hardware you are using in your project makefile. This is done by defining the BOARD
macro using the -D
switch passed to the compiler, with a constant of BOARD_{Name}
. For example, -DBOARD=BOARD_USBKEY
instructs the compiler to use the USBKEY board hardware drivers.
If your application does not use any board level drivers, you can omit the definition of the BOARD
macro. However, some users may wish to write their own custom board hardware drivers which are to remain compatible with the LUFA hardware API. To do this, the BOARD
macro should be defined to the value BOARD_USER
. This indicates that the board level drivers should be located in a folder named "Board" located inside the application's folder.
When used, the driver stub files located in the LUFA/CodeTemplates/DriverStubs
folder should be copied to the user application's Board/
directory, and filled out to include the values and code needed to control the custom board hardware. Once done, the existing LUFA board level APIs (accessed in the regular LUFA/Drivers/Board/
folder) will redirect to the user board drivers, maintaining code compatibility and allowing for a different board to be selected through the project makefile with no code changes.
Board Driver Templates
The templates for each board driver are reproduced below.
Template for USER <Board/Board.h>
#ifndef __BOARD_USER_H__
#define __BOARD_USER_H__
#if defined(__cplusplus)
extern "C" {
#endif
#if !defined(__INCLUDE_FROM_BOARD_H)
#error Do not include this file directly. Include LUFA/Drivers/Board/Board.h instead.
#endif
#if defined(__cplusplus)
}
#endif
#endif
Template for USER <Board/Buttons.h>
#ifndef __BUTTONS_USER_H__
#define __BUTTONS_USER_H__
#if defined(__cplusplus)
extern "C" {
#endif
#if !defined(__INCLUDE_FROM_BUTTONS_H)
#error Do not include this file directly. Include LUFA/Drivers/Board/Buttons.h instead.
#endif
#define BUTTONS_BUTTON1 // TODO: Add mask for first board button here
#if !defined(__DOXYGEN__)
{
}
{
}
{
}
#endif
#if defined(__cplusplus)
}
#endif
#endif
Template for USER <Board/Dataflash.h>
#ifndef __DATAFLASH_USER_H__
#define __DATAFLASH_USER_H__
#if !defined(__INCLUDE_FROM_DATAFLASH_H)
#error Do not include this file directly. Include LUFA/Drivers/Board/Dataflash.h instead.
#endif
#if !defined(__DOXYGEN__)
#define DATAFLASH_CHIPCS_MASK // TODO: Replace this with a mask of all the /CS pins of all Dataflashes
#define DATAFLASH_CHIPCS_DDR // TODO: Replace with the DDR register name for the board's Dataflash ICs
#define DATAFLASH_CHIPCS_PORT // TODO: Replace with the PORT register name for the board's Dataflash ICs
#endif
#define DATAFLASH_TOTALCHIPS 1 // TODO: Replace with the number of Dataflashes on the board, max 2
#define DATAFLASH_NO_CHIP 0
#define DATAFLASH_CHIP1 // TODO: Replace with mask with the pin attached to the first Dataflash /CS set
#define DATAFLASH_CHIP2 // TODO: Replace with mask with the pin attached to the second Dataflash /CS set
#define DATAFLASH_PAGE_SIZE // TODO: Replace with the page size for the Dataflash ICs
#define DATAFLASH_PAGES // TODO: Replace with the total number of pages inside one of the Dataflash ICs
#if !defined(__DOXYGEN__)
{
DATAFLASH_CHIPCS_DDR |= DATAFLASH_CHIPCS_MASK;
DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK;
}
{
}
{
}
{
}
{
return (~DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK);
}
{
DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT | DATAFLASH_CHIPCS_MASK) & ~ChipMask);
}
{
}
{
return;
#if (DATAFLASH_TOTALCHIPS == 2)
if (PageAddress & 0x01)
else
#else
#endif
}
{
}
{
}
const uint16_t BufferByte)
{
#if (DATAFLASH_TOTALCHIPS == 2)
PageAddress >>= 1;
#endif
}
#endif
#endif
Template for USER <Board/Joystick.h>
#ifndef __JOYSTICK_USER_H__
#define __JOYSTICK_USER_H__
#if defined(__cplusplus)
extern "C" {
#endif
#if !defined(__INCLUDE_FROM_JOYSTICK_H)
#error Do not include this file directly. Include LUFA/Drivers/Board/Joystick.h instead.
#endif
#define JOY_LEFT // TODO: Add mask to indicate joystick left position here
#define JOY_RIGHT // TODO: Add mask to indicate joystick right position here
#define JOY_UP // TODO: Add mask to indicate joystick up position here
#define JOY_DOWN // TODO: Add mask to indicate joystick down position here
#define JOY_PRESS // TODO: Add mask to indicate joystick pressed position here
#if !defined(__DOXYGEN__)
{
}
{
}
{
}
#endif
#if defined(__cplusplus)
}
#endif
#endif
Template for USER <Board/LEDs.h>
#ifndef __LEDS_USER_H__
#define __LEDS_USER_H__
#if defined(__cplusplus)
extern "C" {
#endif
#if !defined(__INCLUDE_FROM_LEDS_H)
#error Do not include this file directly. Include LUFA/Drivers/Board/LEDS.h instead.
#endif
#define LEDS_LED1 // TODO: Add mask for first board LED here
#define LEDS_LED2 // TODO: Add mask for second board LED here
#define LEDS_LED3 // TODO: Add mask for third board LED here
#define LEDS_LED4 // TODO: Add mask for fourth board LED here
#define LEDS_ALL_LEDS (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4)
#define LEDS_NO_LEDS 0
#if !defined(__DOXYGEN__)
{
}
{
}
{
}
{
}
{
}
static inline void LEDs_ChangeLEDs(
const uint8_t LEDMask,
const uint8_t ActiveMask)
{
}
{
}
static inline uint8_t
LEDs_GetLEDs(
void) ATTR_WARN_UNUSED_RESULT;
{
}
#endif
#if defined(__cplusplus)
}
#endif
#endif