3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  32  *  \brief SPI Peripheral Driver (AVR8) 
  34  *  On-chip SPI driver for the 8-bit AVR microcontrollers. 
  36  *  \note This file should not be included directly. It is automatically included as needed by the SPI driver 
  37  *        dispatch header located in LUFA/Drivers/Peripheral/SPI.h. 
  40 /** \ingroup Group_SPI 
  41  *  \defgroup Group_SPI_AVR8 SPI Peripheral Driver (AVR8) 
  43  *  \section Sec_ModDescription Module Description 
  44  *  Driver for the hardware SPI port available on most 8-bit AVR microcontroller models. This 
  45  *  module provides an easy to use driver for the setup and transfer of data over the 
  48  *  \note This file should not be included directly. It is automatically included as needed by the SPI driver 
  49  *        dispatch header located in LUFA/Drivers/Peripheral/SPI.h. 
  51  *  \section Sec_ExampleUsage Example Usage 
  52  *  The following snippet is an example of how this module may be used within a typical 
  56  *      // Initialise the SPI driver before first use 
  57  *      SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING | 
  58  *               SPI_SAMPLE_TRAILING | SPI_MODE_MASTER); 
  60  *      // Send several bytes, ignoring the returned data 
  65  *      // Receive several bytes, sending a dummy 0x00 byte each time 
  66  *      uint8_t Byte1 = SPI_ReceiveByte(); 
  67  *      uint8_t Byte2 = SPI_ReceiveByte(); 
  68  *      uint8_t Byte3 = SPI_ReceiveByte(); 
  70  *      // Send a byte, and store the received byte from the same transaction 
  71  *      uint8_t ResponseByte = SPI_TransferByte(0xDC); 
  77 #ifndef __SPI_AVR8_H__ 
  78 #define __SPI_AVR8_H__ 
  81                 #include "../../../Common/Common.h" 
  83         /* Enable C linkage for C++ Compilers: */ 
  84                 #if defined(__cplusplus) 
  88         /* Preprocessor Checks: */ 
  89                 #if !defined(__INCLUDE_FROM_SPI_H) 
  90                         #error Do not include this file directly. Include LUFA/Drivers/Peripheral/SPI.h instead. 
  93         /* Private Interface - For use in library only: */ 
  94         #if !defined(__DOXYGEN__) 
  96                         #define SPI_USE_DOUBLESPEED            (1 << SPE) 
  99         /* Public Interface - May be used in end-application: */ 
 101                         /** \name SPI Prescaler Configuration Masks */ 
 103                         /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 2. */ 
 104                         #define SPI_SPEED_FCPU_DIV_2           SPI_USE_DOUBLESPEED 
 106                         /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 4. */ 
 107                         #define SPI_SPEED_FCPU_DIV_4           0 
 109                         /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 8. */ 
 110                         #define SPI_SPEED_FCPU_DIV_8           (SPI_USE_DOUBLESPEED | (1 << SPR0)) 
 112                         /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 16. */ 
 113                         #define SPI_SPEED_FCPU_DIV_16          (1 << SPR0) 
 115                         /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 32. */ 
 116                         #define SPI_SPEED_FCPU_DIV_32          (SPI_USE_DOUBLESPEED | (1 << SPR1)) 
 118                         /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 64. */ 
 119                         #define SPI_SPEED_FCPU_DIV_64          (SPI_USE_DOUBLESPEED | (1 << SPR1) | (1 << SPR0)) 
 121                         /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 128. */ 
 122                         #define SPI_SPEED_FCPU_DIV_128         ((1 << SPR1) | (1 << SPR0)) 
 125                         /** \name SPI SCK Polarity Configuration Masks */ 
 127                         /** SPI clock polarity mask for \c SPI_Init(). Indicates that the SCK should lead on the rising edge. */ 
 128                         #define SPI_SCK_LEAD_RISING            (0 << CPOL) 
 130                         /** SPI clock polarity mask for \c SPI_Init(). Indicates that the SCK should lead on the falling edge. */ 
 131                         #define SPI_SCK_LEAD_FALLING           (1 << CPOL) 
 134                         /** \name SPI Sample Edge Configuration Masks */ 
 136                         /** SPI data sample mode mask for \c SPI_Init(). Indicates that the data should sampled on the leading edge. */ 
 137                         #define SPI_SAMPLE_LEADING             (0 << CPHA) 
 139                         /** SPI data sample mode mask for \c SPI_Init(). Indicates that the data should be sampled on the trailing edge. */ 
 140                         #define SPI_SAMPLE_TRAILING            (1 << CPHA) 
 143                         /** \name SPI Data Ordering Configuration Masks */ 
 145                         /** SPI data order mask for \c SPI_Init(). Indicates that data should be shifted out MSB first. */ 
 146                         #define SPI_ORDER_MSB_FIRST            (0 << DORD) 
 148                         /** SPI data order mask for \c SPI_Init(). Indicates that data should be shifted out MSB first. */ 
 149                         #define SPI_ORDER_LSB_FIRST            (1 << DORD) 
 152                         /** \name SPI Mode Configuration Masks */ 
 154                         /** SPI mode mask for \c SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */ 
 155                         #define SPI_MODE_SLAVE                 (0 << MSTR) 
 157                         /** SPI mode mask for \c SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */ 
 158                         #define SPI_MODE_MASTER                (1 << MSTR) 
 161                 /* Inline Functions: */ 
 162                         /** Initialises the SPI subsystem, ready for transfers. Must be called before calling any other 
 165                          *  \param[in] SPIOptions  SPI Options, a mask consisting of one of each of the \c SPI_SPEED_*, 
 166                          *                         \c SPI_SCK_*, \c SPI_SAMPLE_*, \c SPI_ORDER_* and \c SPI_MODE_* masks. 
 168                         static inline void SPI_Init(const uint8_t SPIOptions
) 
 170                                 DDRB  
|=  ((1 << 1) | (1 << 2)); 
 171                                 DDRB  
&= ~((1 << 0) | (1 << 3)); 
 172                                 PORTB 
|=  ((1 << 0) | (1 << 3)); 
 174                                 SPCR   
= ((1 << SPE
) | SPIOptions
); 
 176                                 if (SPIOptions 
& SPI_USE_DOUBLESPEED
) 
 177                                   SPSR 
|= (1 << SPI2X
); 
 179                                   SPSR 
&= ~(1 << SPI2X
); 
 182                         /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */ 
 183                         static inline void SPI_Disable(void) 
 185                                 DDRB  
&= ~((1 << 1) | (1 << 2)); 
 186                                 PORTB 
&= ~((1 << 0) | (1 << 3)); 
 192                         /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete. 
 194                          *  \param[in] Byte  Byte to send through the SPI interface. 
 196                          *  \return Response byte from the attached SPI device. 
 198                         static inline uint8_t SPI_TransferByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
; 
 199                         static inline uint8_t SPI_TransferByte(const uint8_t Byte
) 
 202                                 while (!(SPSR 
& (1 << SPIF
))); 
 206                         /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response 
 207                          *  byte sent to from the attached SPI device is ignored. 
 209                          *  \param[in] Byte  Byte to send through the SPI interface. 
 211                         static inline void SPI_SendByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
; 
 212                         static inline void SPI_SendByte(const uint8_t Byte
) 
 215                                 while (!(SPSR 
& (1 << SPIF
))); 
 218                         /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response 
 219                          *  byte from the attached SPI device is returned. 
 221                          *  \return The response byte from the attached SPI device. 
 223                         static inline uint8_t SPI_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
; 
 224                         static inline uint8_t SPI_ReceiveByte(void) 
 227                                 while (!(SPSR 
& (1 << SPIF
))); 
 231         /* Disable C linkage for C++ Compilers: */ 
 232                 #if defined(__cplusplus)