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