Make XPLAINBridge serial bridge much more reliable for the reception of characters...
[pub/USBasp.git] / LUFA / Drivers / Peripheral / AVRU4U6U7 / TWI.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 * \brief TWI peripheral driver for the U7, U6 and U4 USB AVRs.
33 *
34 * Master mode TWI driver for the AT90USB1287, AT90USB1286, AT90USB647, AT90USB646, ATMEGA16U4 and ATMEGA32U4 AVRs.
35 *
36 * \note This file should not be included directly. It is automatically included as needed by the TWI driver
37 * dispatch header located in LUFA/Drivers/Peripheral/TWI.h.
38 */
39
40 /** \ingroup Group_TWI
41 * @defgroup Group_TWI_AVRU4U6U7 Series U4, U6 and U7 Model TWI Driver
42 *
43 * Master mode TWI driver for the AT90USB1287, AT90USB1286, AT90USB647, AT90USB646, ATMEGA16U4 and ATMEGA32U4 AVRs.
44 *
45 * \note This file should not be included directly. It is automatically included as needed by the TWI driver
46 * dispatch header located in LUFA/Drivers/Peripheral/TWI.h.
47 *
48 * @{
49 */
50
51 #ifndef __TWI_AVRU4U6U7_H__
52 #define __TWI_AVRU4U6U7_H__
53
54 /* Includes: */
55 #include "../../../Common/Common.h"
56
57 #include <avr/io.h>
58 #include <stdbool.h>
59 #include <util/twi.h>
60 #include <util/delay.h>
61
62 /* Enable C linkage for C++ Compilers: */
63 #if defined(__cplusplus)
64 extern "C" {
65 #endif
66
67 /* Preprocessor Checks: */
68 #if !defined(__INCLUDE_FROM_TWI_H)
69 #error Do not include this file directly. Include LUFA/Drivers/Peripheral/TWI.h instead.
70 #endif
71
72 /* Public Interface - May be used in end-application: */
73 /* Pseudo-Function Macros: */
74 #if defined(__DOXYGEN__)
75 /** Initializes the TWI hardware into master mode, ready for data transmission and reception. This must be
76 * before any other TWI operations.
77 */
78 static inline void TWI_Init(void);
79
80 /** Turns off the TWI driver hardware. If this is called, any further TWI operations will require a call to
81 * \ref TWI_Init() before the TWI can be used again.
82 */
83 static inline void TWI_ShutDown(void);
84 #else
85 #define TWI_Init() MACROS{ TWCR |= (1 << TWEN); }MACROE
86 #define TWI_ShutDown() MACROS{ TWCR &= ~(1 << TWEN); }MACROE
87 #endif
88
89 /* Inline Functions: */
90 /** Sends a TWI STOP onto the TWI bus, terminating communication with the currently addressed device. */
91 static inline void TWI_StopTransmission(void)
92 {
93 TWCR = ((1 << TWINT) | (1 << TWSTO) | (1 << TWEN));
94 }
95
96 /** Sends a byte to the currently addressed device on the TWI bus.
97 *
98 * \param[in] Byte Byte to send to the currently addressed device
99 *
100 * \return Boolean true if the recipient ACKed the byte, false otherwise
101 */
102 static inline bool TWI_SendByte(uint8_t Byte)
103 {
104 TWDR = Byte;
105 TWCR = ((1 << TWINT) | (1 << TWEN));
106 while (!(TWCR & (1 << TWINT)));
107
108 return ((TWSR & TW_STATUS_MASK) == TW_MT_DATA_ACK);
109 }
110
111 /** Receives a byte from the currently addressed device on the TWI bus.
112 *
113 * \param[in] Byte Location where the read byte is to be stored
114 * \param[in] LastByte Indicates if the byte should be ACKed if false, NAKed if true
115 *
116 * \return Boolean true if the byte reception successfully completed, false otherwise
117 */
118 static inline bool TWI_ReceiveByte(uint8_t* Byte, bool LastByte)
119 {
120 uint8_t TWCRMask = ((1 << TWINT) | (1 << TWEN));
121
122 if (!(LastByte))
123 TWCRMask |= (1 << TWEA);
124
125 TWCR = TWCRMask;
126 while (!(TWCR & (1 << TWINT)));
127 *Byte = TWDR;
128
129 return ((TWSR & TW_STATUS_MASK) == TW_MR_DATA_ACK);
130 }
131
132 /* Function Prototypes: */
133 /** Begins a master mode TWI bus communication with the given slave device address.
134 *
135 * \param[in] SlaveAddress Address of the slave TWI device to communicate with
136 * \param[in] TimeoutMS Timeout period within which the slave must respond, in milliseconds
137 *
138 * \return Boolean true if the device is ready for data, false otherwise
139 */
140 bool TWI_StartTransmission(uint8_t SlaveAddress, uint8_t TimeoutMS);
141
142 /* Disable C linkage for C++ Compilers: */
143 #if defined(__cplusplus)
144 }
145 #endif
146
147 #endif
148
149 /** @} */