Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SerialSPI_XMEGA.h 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. * \brief Master SPI Mode Serial USART Peripheral Driver (XMEGA)
  28. *
  29. * On-chip Master SPI mode USART driver for the XMEGA AVR microcontrollers.
  30. *
  31. * \note This file should not be included directly. It is automatically included as needed by the SPI Master
  32. * Mode USART driver dispatch header located in LUFA/Drivers/Peripheral/Serial.h.
  33. */
  34. /** \ingroup Group_SerialSPI
  35. * \defgroup Group_SerialSPI_XMEGA Master SPI Mode Serial USART Peripheral Driver (XMEGA)
  36. *
  37. * \section Sec_SerialSPI_XMEGA_ModDescription Module Description
  38. * On-chip serial USART driver for the XMEGA AVR microcontrollers.
  39. *
  40. * \note This file should not be included directly. It is automatically included as needed by the ADC driver
  41. * dispatch header located in LUFA/Drivers/Peripheral/SerialSPI.h.
  42. *
  43. * \section Sec_SerialSPI_XMEGA_ExampleUsage Example Usage
  44. * The following snippet is an example of how this module may be used within a typical
  45. * application.
  46. *
  47. * \code
  48. * // Initialize the Master SPI mode USART driver before first use, with 1Mbit baud
  49. * SerialSPI_Init(&USARTD0, (USART_SPI_SCK_LEAD_RISING | USART_SPI_SAMPLE_LEADING | USART_SPI_ORDER_MSB_FIRST), 1000000);
  50. *
  51. * // Send several bytes, ignoring the returned data
  52. * SerialSPI_SendByte(&USARTD0, 0x01);
  53. * SerialSPI_SendByte(&USARTD0, 0x02);
  54. * SerialSPI_SendByte(&USARTD0, 0x03);
  55. *
  56. * // Receive several bytes, sending a dummy 0x00 byte each time
  57. * uint8_t Byte1 = SerialSPI_ReceiveByte(&USARTD);
  58. * uint8_t Byte2 = SerialSPI_ReceiveByte(&USARTD);
  59. * uint8_t Byte3 = SerialSPI_ReceiveByte(&USARTD);
  60. *
  61. * // Send a byte, and store the received byte from the same transaction
  62. * uint8_t ResponseByte = SerialSPI_TransferByte(&USARTD0, 0xDC);
  63. * \endcode
  64. *
  65. * @{
  66. */
  67. #ifndef __SERIAL_SPI_XMEGA_H__
  68. #define __SERIAL_SPI_XMEGA_H__
  69. /* Includes: */
  70. #include "../../../Common/Common.h"
  71. #include <stdio.h>
  72. /* Enable C linkage for C++ Compilers: */
  73. #if defined(__cplusplus)
  74. extern "C" {
  75. #endif
  76. /* Preprocessor Checks: */
  77. #if !defined(__INCLUDE_FROM_SERIAL_SPI_H)
  78. #error Do not include this file directly. Include LUFA/Drivers/Peripheral/Serial.h instead.
  79. #endif
  80. /* Private Interface - For use in library only: */
  81. #if !defined(__DOXYGEN__)
  82. #define SERIAL_SPI_UBBRVAL(Baud) ((Baud < (F_CPU / 2)) ? ((F_CPU / (2 * Baud)) - 1) : 0)
  83. #endif
  84. /* Public Interface - May be used in end-application: */
  85. /* Macros: */
  86. /** \name SPI SCK Polarity Configuration Masks */
  87. //@{
  88. /** SPI clock polarity mask for \ref SerialSPI_Init(). Indicates that the SCK should lead on the rising edge. */
  89. #define USART_SPI_SCK_LEAD_RISING 0
  90. //@}
  91. /** \name SPI Sample Edge Configuration Masks */
  92. //@{
  93. /** SPI data sample mode mask for \ref SerialSPI_Init(). Indicates that the data should sampled on the leading edge. */
  94. #define USART_SPI_SAMPLE_LEADING 0
  95. /** SPI data sample mode mask for \ref SerialSPI_Init(). Indicates that the data should be sampled on the trailing edge. */
  96. #define USART_SPI_SAMPLE_TRAILING (1 << 1)
  97. //@}
  98. /** \name SPI Data Ordering Configuration Masks */
  99. //@{
  100. /** SPI data order mask for \ref SerialSPI_Init(). Indicates that data should be shifted out MSB first. */
  101. #define USART_SPI_ORDER_MSB_FIRST 0
  102. /** SPI data order mask for \ref SerialSPI_Init(). Indicates that data should be shifted out LSB first. */
  103. #define USART_SPI_ORDER_LSB_FIRST (1 << 2)
  104. //@}
  105. /* Inline Functions: */
  106. /** Initialize the USART module in Master SPI mode.
  107. *
  108. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  109. * \param[in] SPIOptions USART SPI Options, a mask consisting of one of each of the \c USART_SPI_SCK_*,
  110. * \c USART_SPI_SAMPLE_* and \c USART_SPI_ORDER_* masks.
  111. * \param[in] BaudRate SPI baud rate, in bits per second.
  112. */
  113. static inline void SerialSPI_Init(USART_t* const USART,
  114. const uint8_t SPIOptions,
  115. const uint32_t BaudRate) ATTR_NON_NULL_PTR_ARG(1);
  116. static inline void SerialSPI_Init(USART_t* const USART,
  117. const uint8_t SPIOptions,
  118. const uint32_t BaudRate)
  119. {
  120. uint16_t BaudValue = SERIAL_SPI_UBBRVAL(BaudRate);
  121. USART->BAUDCTRLB = (BaudValue >> 8);
  122. USART->BAUDCTRLA = (BaudValue & 0xFF);
  123. USART->CTRLC = (USART_CMODE_MSPI_gc | SPIOptions);
  124. USART->CTRLB = (USART_RXEN_bm | USART_TXEN_bm);
  125. }
  126. /** Turns off the USART driver, disabling and returning used hardware to their default configuration.
  127. *
  128. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  129. */
  130. static inline void SerialSPI_Disable(USART_t* const USART) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
  131. static inline void SerialSPI_Disable(USART_t* const USART)
  132. {
  133. USART->CTRLA = 0;
  134. USART->CTRLB = 0;
  135. USART->CTRLC = 0;
  136. }
  137. /** Sends and receives a byte through the USART SPI interface, blocking until the transfer is complete.
  138. *
  139. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  140. * \param[in] DataByte Byte to send through the USART SPI interface.
  141. *
  142. * \return Response byte from the attached SPI device.
  143. */
  144. static inline uint8_t SerialSPI_TransferByte(USART_t* const USART,
  145. const uint8_t DataByte) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
  146. static inline uint8_t SerialSPI_TransferByte(USART_t* const USART,
  147. const uint8_t DataByte)
  148. {
  149. USART->DATA = DataByte;
  150. while (!(USART->STATUS & USART_TXCIF_bm));
  151. USART->STATUS = USART_TXCIF_bm;
  152. return USART->DATA;
  153. }
  154. /** Sends a byte through the USART SPI interface, blocking until the transfer is complete. The response
  155. * byte sent to from the attached SPI device is ignored.
  156. *
  157. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  158. * \param[in] DataByte Byte to send through the USART SPI interface.
  159. */
  160. static inline void SerialSPI_SendByte(USART_t* const USART,
  161. const uint8_t DataByte) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
  162. static inline void SerialSPI_SendByte(USART_t* const USART,
  163. const uint8_t DataByte)
  164. {
  165. SerialSPI_TransferByte(USART, DataByte);
  166. }
  167. /** Sends a dummy byte through the USART SPI interface, blocking until the transfer is complete. The response
  168. * byte from the attached SPI device is returned.
  169. *
  170. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  171. *
  172. * \return The response byte from the attached SPI device.
  173. */
  174. static inline uint8_t SerialSPI_ReceiveByte(USART_t* const USART) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
  175. static inline uint8_t SerialSPI_ReceiveByte(USART_t* const USART)
  176. {
  177. return SerialSPI_TransferByte(USART, 0);
  178. }
  179. /* Disable C linkage for C++ Compilers: */
  180. #if defined(__cplusplus)
  181. }
  182. #endif
  183. #endif
  184. /** @} */