X-Git-Url: http://git.linex4red.de/pub/lufa.git/blobdiff_plain/eee252603be67c539f9986cde76454f756e55d95..refs/heads/ProMicro:/LUFA/Drivers/Peripheral/AVR8/SerialSPI_AVR8.h diff --git a/LUFA/Drivers/Peripheral/AVR8/SerialSPI_AVR8.h b/LUFA/Drivers/Peripheral/AVR8/SerialSPI_AVR8.h index e7e236b1d..cd9e59728 100644 --- a/LUFA/Drivers/Peripheral/AVR8/SerialSPI_AVR8.h +++ b/LUFA/Drivers/Peripheral/AVR8/SerialSPI_AVR8.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2012. + Copyright (C) Dean Camera, 2021. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2021 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -40,30 +40,30 @@ /** \ingroup Group_SerialSPI * \defgroup Group_SerialSPI_AVR8 Master SPI Mode Serial USART Peripheral Driver (AVR8) * - * \section Sec_ModDescription Module Description + * \section Sec_SerialSPI_AVR8_ModDescription Module Description * On-chip serial USART driver for the 8-bit AVR8 microcontrollers. * * \note This file should not be included directly. It is automatically included as needed by the SPI Master * driver dispatch header located in LUFA/Drivers/Peripheral/SerialSPI.h. * - * \section Sec_ExampleUsage Example Usage + * \section Sec_SerialSPI_AVR8_ExampleUsage Example Usage * The following snippet is an example of how this module may be used within a typical * application. * * \code * // Initialize the Master SPI mode USART driver before first use, with 1Mbit baud - * SerialSPI_Init((USART_SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING | SPI_ORDER_MSB_FIRST), 1000000); - * + * SerialSPI_Init((USART_SPI_SCK_LEAD_RISING | USART_SPI_SAMPLE_LEADING | USART_SPI_ORDER_MSB_FIRST), 1000000); + * * // Send several bytes, ignoring the returned data * SerialSPI_SendByte(0x01); * SerialSPI_SendByte(0x02); * SerialSPI_SendByte(0x03); - * + * * // Receive several bytes, sending a dummy 0x00 byte each time * uint8_t Byte1 = SerialSPI_ReceiveByte(); * uint8_t Byte2 = SerialSPI_ReceiveByte(); * uint8_t Byte3 = SerialSPI_ReceiveByte(); - * + * * // Send a byte, and store the received byte from the same transaction * uint8_t ResponseByte = SerialSPI_TransferByte(0xDC); * \endcode @@ -90,41 +90,49 @@ #endif /* Private Interface - For use in library only: */ - #if !defined(__DOXYGEN__) - #define SERIAL_SPI_UBBRVAL(Baud) ((Baud < (F_CPU / 2)) ? ((F_CPU / (2 * Baud)) - 1) : 0) - #endif + #if !defined(__DOXYGEN__) + #define SERIAL_SPI_UBBRVAL(Baud) ((Baud < (F_CPU / 2)) ? ((F_CPU / (2 * Baud)) - 1) : 0) + + /* Master USART SPI mode flag definitions missing in the AVR8 toolchain */ + #if !defined(UCPHA1) + #define UCPHA1 1 + #endif + #if !defined(UDORD1) + #define UDORD1 2 + #endif + #endif /* Public Interface - May be used in end-application: */ /* Macros: */ /** \name SPI SCK Polarity Configuration Masks */ - //@{ + /**@{*/ /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the rising edge. */ - #define USART_SPI_SCK_LEAD_RISING (0 << UCPOL) + #define USART_SPI_SCK_LEAD_RISING (0 << UCPOL1) /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the falling edge. */ - #define USART_SPI_SCK_LEAD_FALLING (1 << UCPOL) - //@} + #define USART_SPI_SCK_LEAD_FALLING (1 << UCPOL1) + /**@}*/ /** \name SPI Sample Edge Configuration Masks */ - //@{ + /**@{*/ /** SPI data sample mode mask for \ref SerialSPI_Init(). Indicates that the data should sampled on the leading edge. */ - #define USART_SPI_SAMPLE_LEADING (0 << UPCHA) + #define USART_SPI_SAMPLE_LEADING (0 << UCPHA1) /** SPI data sample mode mask for \ref SerialSPI_Init(). Indicates that the data should be sampled on the trailing edge. */ - #define USART_SPI_SAMPLE_TRAILING (1 << UPCHA) - //@} + #define USART_SPI_SAMPLE_TRAILING (1 << UCPHA1) + /**@}*/ /** \name SPI Data Ordering Configuration Masks */ - //@{ + /**@{*/ /** SPI data order mask for \ref SerialSPI_Init(). Indicates that data should be shifted out MSB first. */ - #define USART_SPI_ORDER_MSB_FIRST (0 << UDORD) + #define USART_SPI_ORDER_MSB_FIRST (0 << UDORD1) /** SPI data order mask for \ref SerialSPI_Init(). Indicates that data should be shifted out LSB first. */ - #define USART_SPI_ORDER_LSB_FIRST (1 << UDORD) - //@} + #define USART_SPI_ORDER_LSB_FIRST (1 << UDORD1) + /**@}*/ /* Inline Functions: */ - /** Initialize the USART module in Master SPI mode. + /** Initialize the USART module in Master SPI mode. * * \param[in] SPIOptions USART SPI Options, a mask consisting of one of each of the \c USART_SPI_SCK_*, * \c USART_SPI_SAMPLE_* and \c USART_SPI_ORDER_* masks. @@ -133,13 +141,13 @@ static inline void SerialSPI_Init(const uint8_t SPIOptions, const uint32_t BaudRate) { - UBRR1 = SERIAL_SPI_UBBRVAL(BaudRate); + DDRD |= ((1 << 3) | (1 << 5)); + PORTD |= (1 << 2); UCSR1C = ((1 << UMSEL11) | (1 << UMSEL10) | SPIOptions); UCSR1B = ((1 << TXEN1) | (1 << RXEN1)); - DDRD |= (1 << 3); - PORTD |= (1 << 2); + UBRR1 = SERIAL_SPI_UBBRVAL(BaudRate); } /** Turns off the USART driver, disabling and returning used hardware to their default configuration. */ @@ -151,10 +159,10 @@ UBRR1 = 0; - DDRD &= ~(1 << 3); + DDRD &= ~((1 << 3) | (1 << 5)); PORTD &= ~(1 << 2); } - + /** Sends and receives a byte through the USART SPI interface, blocking until the transfer is complete. * * \param[in] DataByte Byte to send through the USART SPI interface. @@ -188,7 +196,7 @@ { return SerialSPI_TransferByte(0); } - + /* Disable C linkage for C++ Compilers: */ #if defined(__cplusplus) }