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