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.

TWI_XMEGA.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 TWI Peripheral Driver (XMEGA)
  28. *
  29. * On-chip TWI driver for the XMEGA Family of AVR microcontrollers.
  30. *
  31. * \note This file should not be included directly. It is automatically included as needed by the TWI driver
  32. * dispatch header located in LUFA/Drivers/Peripheral/TWI.h.
  33. */
  34. /** \ingroup Group_TWI
  35. * \defgroup Group_TWI_XMEGA TWI Peripheral Driver (XMEGA)
  36. *
  37. * \section Sec_TWI_XMEGA_ModDescription Module Description
  38. * Master mode TWI driver for the 8-bit AVR microcontrollers which contain a hardware TWI module.
  39. *
  40. * \note This file should not be included directly. It is automatically included as needed by the TWI driver
  41. * dispatch header located in LUFA/Drivers/Peripheral/TWI.h.
  42. *
  43. * \section Sec_TWI_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. * <b>Low Level API Example:</b>
  48. * \code
  49. * // Initialize the TWI driver before first use at 200KHz
  50. * TWI_Init(&TWIC, TWI_BAUD_FROM_FREQ(200000));
  51. *
  52. * // Start a write session to device at device address 0xA0, internal address 0xDC with a 10ms timeout
  53. * if (TWI_StartTransmission(&TWIC, 0xA0 | TWI_ADDRESS_WRITE, 10) == TWI_ERROR_NoError)
  54. * {
  55. * TWI_SendByte(&TWIC, 0xDC);
  56. *
  57. * TWI_SendByte(&TWIC, 0x01);
  58. * TWI_SendByte(&TWIC, 0x02);
  59. * TWI_SendByte(&TWIC, 0x03);
  60. *
  61. * // Must stop transmission afterwards to release the bus
  62. * TWI_StopTransmission(&TWIC);
  63. * }
  64. *
  65. * // Start a read session to device at address 0xA0, internal address 0xDC with a 10ms timeout
  66. * if (TWI_StartTransmission(&TWIC, 0xA0 | TWI_ADDRESS_WRITE, 10) == TWI_ERROR_NoError)
  67. * {
  68. * TWI_SendByte(&TWIC, 0xDC);
  69. * TWI_StopTransmission(&TWIC);
  70. *
  71. * if (TWI_StartTransmission(&TWIC, 0xA0 | TWI_ADDRESS_READ, 10) == TWI_ERROR_NoError)
  72. * {
  73. * uint8_t Byte1, Byte2, Byte3;
  74. *
  75. * // Read three bytes, acknowledge after the third byte is received
  76. * TWI_ReceiveByte(&TWIC, &Byte1, false);
  77. * TWI_ReceiveByte(&TWIC, &Byte2, false);
  78. * TWI_ReceiveByte(&TWIC, &Byte3, true);
  79. *
  80. * // Must stop transmission afterwards to release the bus
  81. * TWI_StopTransmission(&TWIC);
  82. * }
  83. * }
  84. * \endcode
  85. *
  86. * <b>High Level API Example:</b>
  87. * \code
  88. * // Initialize the TWI driver before first use at 200KHz
  89. * TWI_Init(&TWIC, TWI_BAUD_FROM_FREQ(200000));
  90. *
  91. * // Start a write session to device at device address 0xA0, internal address 0xDC with a 10ms timeout
  92. * uint8_t InternalWriteAddress = 0xDC;
  93. * uint8_t WritePacket[3] = {0x01, 0x02, 0x03};
  94. *
  95. * TWI_WritePacket(&TWIC, 0xA0, 10, &InternalWriteAddress, sizeof(InternalWriteAddress),
  96. * &WritePacket, sizeof(WritePacket);
  97. *
  98. * // Start a read session to device at address 0xA0, internal address 0xDC with a 10ms timeout
  99. * uint8_t InternalReadAddress = 0xDC;
  100. * uint8_t ReadPacket[3];
  101. *
  102. * TWI_ReadPacket(&TWIC, 0xA0, 10, &InternalReadAddress, sizeof(InternalReadAddress),
  103. * &ReadPacket, sizeof(ReadPacket);
  104. * \endcode
  105. *
  106. * @{
  107. */
  108. #ifndef __TWI_XMEGA_H__
  109. #define __TWI_XMEGA_H__
  110. /* Includes: */
  111. #include "../../../Common/Common.h"
  112. #include <stdio.h>
  113. /* Enable C linkage for C++ Compilers: */
  114. #if defined(__cplusplus)
  115. extern "C" {
  116. #endif
  117. /* Preprocessor Checks: */
  118. #if !defined(__INCLUDE_FROM_TWI_H) && !defined(__INCLUDE_FROM_TWI_C)
  119. #error Do not include this file directly. Include LUFA/Drivers/Peripheral/TWI.h instead.
  120. #endif
  121. /* Public Interface - May be used in end-application: */
  122. /* Macros: */
  123. /** TWI slave device address mask for a read session. Mask with a slave device base address to obtain
  124. * the correct TWI bus address for the slave device when reading data from it.
  125. */
  126. #define TWI_ADDRESS_READ 0x01
  127. /** TWI slave device address mask for a write session. Mask with a slave device base address to obtain
  128. * the correct TWI bus address for the slave device when writing data to it.
  129. */
  130. #define TWI_ADDRESS_WRITE 0x00
  131. /** Mask to retrieve the base address for a TWI device, which can then be ORed with \ref TWI_ADDRESS_READ
  132. * or \ref TWI_ADDRESS_WRITE to obtain the device's read and write address respectively.
  133. */
  134. #define TWI_DEVICE_ADDRESS_MASK 0xFE
  135. /** Calculates the length of each bit on the TWI bus for a given target frequency. This may be used with
  136. * the \ref TWI_Init() function to convert a bus frequency to a number of clocks for the \c BitLength
  137. * parameter.
  138. *
  139. * \param[in] Frequency Desired TWI bus frequency in Hz.
  140. *
  141. * \return Bit length in clocks for the given TWI bus frequency at the given prescaler value.
  142. */
  143. #define TWI_BAUD_FROM_FREQ(Frequency) ((F_CPU / (2 * Frequency)) - 5)
  144. /* Enums: */
  145. /** Enum for the possible return codes of the TWI transfer start routine and other dependant TWI functions. */
  146. enum TWI_ErrorCodes_t
  147. {
  148. TWI_ERROR_NoError = 0, /**< Indicates that the command completed successfully. */
  149. TWI_ERROR_BusFault = 1, /**< A TWI bus fault occurred while attempting to capture the bus. */
  150. TWI_ERROR_BusCaptureTimeout = 2, /**< A timeout occurred whilst waiting for the bus to be ready. */
  151. TWI_ERROR_SlaveResponseTimeout = 3, /**< No ACK received at the nominated slave address within the timeout period. */
  152. TWI_ERROR_SlaveNotReady = 4, /**< Slave NAKed the TWI bus START condition. */
  153. TWI_ERROR_SlaveNAK = 5, /**< Slave NAKed whilst attempting to send data to the device. */
  154. };
  155. /* Inline Functions: */
  156. /** Initializes the TWI hardware into master mode, ready for data transmission and reception. This must be
  157. * before any other TWI operations.
  158. *
  159. * The generated SCL frequency will be according to the formula <pre>F_CPU / (2 * (5 + (BAUD)))</pre>.
  160. *
  161. * \attention The value of the \c BitLength parameter should not be set below 10 or invalid bus conditions may
  162. * occur, as indicated in the XMEGA microcontroller datasheet.
  163. *
  164. * \param[in] TWI Pointer to the base of the TWI peripheral within the device.
  165. * \param[in] Baud Value of the BAUD register of the TWI Master.
  166. */
  167. static inline void TWI_Init(TWI_t* const TWI,
  168. const uint8_t Baud) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
  169. static inline void TWI_Init(TWI_t* const TWI,
  170. const uint8_t Baud)
  171. {
  172. TWI->CTRL = 0x00;
  173. TWI->MASTER.BAUD = Baud;
  174. TWI->MASTER.CTRLA = TWI_MASTER_ENABLE_bm;
  175. TWI->MASTER.CTRLB = 0;
  176. TWI->MASTER.STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;
  177. }
  178. /** Turns off the TWI driver hardware. If this is called, any further TWI operations will require a call to
  179. * \ref TWI_Init() before the TWI can be used again.
  180. *
  181. * \param[in] TWI Pointer to the base of the TWI peripheral within the device.
  182. */
  183. static inline void TWI_Disable(TWI_t* const TWI) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
  184. static inline void TWI_Disable(TWI_t* const TWI)
  185. {
  186. TWI->MASTER.CTRLA &= ~TWI_MASTER_ENABLE_bm;
  187. }
  188. /** Sends a TWI STOP onto the TWI bus, terminating communication with the currently addressed device.
  189. *
  190. * \param[in] TWI Pointer to the base of the TWI peripheral within the device.
  191. */
  192. static inline void TWI_StopTransmission(TWI_t* const TWI) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
  193. static inline void TWI_StopTransmission(TWI_t* const TWI)
  194. {
  195. TWI->MASTER.CTRLC = TWI_MASTER_ACKACT_bm | TWI_MASTER_CMD_STOP_gc;
  196. }
  197. /* Function Prototypes: */
  198. /** Begins a master mode TWI bus communication with the given slave device address.
  199. *
  200. * \param[in] TWI Pointer to the base of the TWI peripheral within the device.
  201. * \param[in] SlaveAddress Address of the slave TWI device to communicate with.
  202. * \param[in] TimeoutMS Timeout period within which the slave must respond, in milliseconds.
  203. *
  204. * \return A value from the \ref TWI_ErrorCodes_t enum.
  205. */
  206. uint8_t TWI_StartTransmission(TWI_t* const TWI,
  207. const uint8_t SlaveAddress,
  208. const uint8_t TimeoutMS) ATTR_NON_NULL_PTR_ARG(1);
  209. /** Sends a byte to the currently addressed device on the TWI bus.
  210. *
  211. * \param[in] TWI Pointer to the base of the TWI peripheral within the device.
  212. * \param[in] Byte Byte to send to the currently addressed device
  213. *
  214. * \return Boolean \c true if the recipient ACKed the byte, \c false otherwise
  215. */
  216. bool TWI_SendByte(TWI_t* const TWI,
  217. const uint8_t Byte) ATTR_NON_NULL_PTR_ARG(1);
  218. /** Receives a byte from the currently addressed device on the TWI bus.
  219. *
  220. * \param[in] TWI Pointer to the base of the TWI peripheral within the device.
  221. * \param[in] Byte Location where the read byte is to be stored.
  222. * \param[in] LastByte Indicates if the byte should be ACKed if false, NAKed if true.
  223. *
  224. * \return Boolean \c true if the byte reception successfully completed, \c false otherwise.
  225. */
  226. bool TWI_ReceiveByte(TWI_t* const TWI,
  227. uint8_t* const Byte,
  228. const bool LastByte) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  229. /** High level function to perform a complete packet transfer over the TWI bus to the specified
  230. * device.
  231. *
  232. * \param[in] TWI Pointer to the base of the TWI peripheral within the device.
  233. * \param[in] SlaveAddress Base address of the TWI slave device to communicate with.
  234. * \param[in] TimeoutMS Timeout for bus capture and slave START ACK, in milliseconds.
  235. * \param[in] InternalAddress Pointer to a location where the internal slave read start address is stored.
  236. * \param[in] InternalAddressLen Size of the internal device address, in bytes.
  237. * \param[in] Buffer Pointer to a buffer where the read packet data is to be stored.
  238. * \param[in] Length Size of the packet to read, in bytes.
  239. *
  240. * \return A value from the \ref TWI_ErrorCodes_t enum.
  241. */
  242. uint8_t TWI_ReadPacket(TWI_t* const TWI,
  243. const uint8_t SlaveAddress,
  244. const uint8_t TimeoutMS,
  245. const uint8_t* InternalAddress,
  246. uint8_t InternalAddressLen,
  247. uint8_t* Buffer,
  248. uint8_t Length) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(4);
  249. /** High level function to perform a complete packet transfer over the TWI bus from the specified
  250. * device.
  251. *
  252. * \param[in] TWI Pointer to the base of the TWI peripheral within the device.
  253. * \param[in] SlaveAddress Base address of the TWI slave device to communicate with
  254. * \param[in] TimeoutMS Timeout for bus capture and slave START ACK, in milliseconds
  255. * \param[in] InternalAddress Pointer to a location where the internal slave write start address is stored
  256. * \param[in] InternalAddressLen Size of the internal device address, in bytes
  257. * \param[in] Buffer Pointer to a buffer where the packet data to send is stored
  258. * \param[in] Length Size of the packet to send, in bytes
  259. *
  260. * \return A value from the \ref TWI_ErrorCodes_t enum.
  261. */
  262. uint8_t TWI_WritePacket(TWI_t* const TWI,
  263. const uint8_t SlaveAddress,
  264. const uint8_t TimeoutMS,
  265. const uint8_t* InternalAddress,
  266. uint8_t InternalAddressLen,
  267. const uint8_t* Buffer,
  268. uint8_t Length) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(4);
  269. /* Disable C linkage for C++ Compilers: */
  270. #if defined(__cplusplus)
  271. }
  272. #endif
  273. #endif
  274. /** @} */