673f1b0711c1bfefa15730db7084e6f734c94e8e
[pub/USBasp.git] / LUFA / Drivers / Peripheral / AVR8 / 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 8-Bit AVRs containing a hardware TWI module.
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_AVR8 Series U4, U6 and U7 Model TWI Driver
41 *
42 * Master mode TWI driver for the 8-Bit AVRs containing a hardware TWI module.
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_AVR8_H__
51 #define __TWI_AVR8_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
60 /* Enable C linkage for C++ Compilers: */
61 #if defined(__cplusplus)
62 extern "C" {
63 #endif
64
65 /* Preprocessor Checks: */
66 #if !defined(__INCLUDE_FROM_TWI_H)
67 #error Do not include this file directly. Include LUFA/Drivers/Peripheral/TWI.h instead.
68 #endif
69
70 /* Public Interface - May be used in end-application: */
71 /* Pseudo-Function Macros: */
72 #if defined(__DOXYGEN__)
73 /** Initializes the TWI hardware into master mode, ready for data transmission and reception. This must be
74 * before any other TWI operations.
75 */
76 static inline void TWI_Init(void);
77
78 /** Turns off the TWI driver hardware. If this is called, any further TWI operations will require a call to
79 * \ref TWI_Init() before the TWI can be used again.
80 */
81 static inline void TWI_ShutDown(void);
82 #else
83 #define TWI_Init() MACROS{ TWCR |= (1 << TWEN); }MACROE
84 #define TWI_ShutDown() MACROS{ TWCR &= ~(1 << TWEN); }MACROE
85 #endif
86
87 /* Inline Functions: */
88 /** Sends a TWI STOP onto the TWI bus, terminating communication with the currently addressed device. */
89 static inline void TWI_StopTransmission(void)
90 {
91 TWCR = ((1 << TWINT) | (1 << TWSTO) | (1 << TWEN));
92 }
93
94 /** Sends a byte to the currently addressed device on the TWI bus.
95 *
96 * \param[in] Byte Byte to send to the currently addressed device
97 *
98 * \return Boolean true if the recipient ACKed the byte, false otherwise
99 */
100 static inline bool TWI_SendByte(uint8_t Byte)
101 {
102 TWDR = Byte;
103 TWCR = ((1 << TWINT) | (1 << TWEN));
104 while (!(TWCR & (1 << TWINT)));
105
106 return ((TWSR & TW_STATUS_MASK) == TW_MT_DATA_ACK);
107 }
108
109 /** Receives a byte from the currently addressed device on the TWI bus.
110 *
111 * \param[in] Byte Location where the read byte is to be stored
112 * \param[in] LastByte Indicates if the byte should be ACKed if false, NAKed if true
113 *
114 * \return Boolean true if the byte reception sucessfully completed, false otherwise
115 */
116 static inline bool TWI_ReceiveByte(uint8_t* Byte, bool LastByte)
117 {
118 uint8_t TWCRMask = ((1 << TWINT) | (1 << TWEN));
119
120 if (!(LastByte))
121 TWCRMask |= (1 << TWEA);
122
123 TWCR = TWCRMask;
124 while (!(TWCR & (1 << TWINT)));
125 *Byte = TWDR;
126
127 return ((TWSR & TW_STATUS_MASK) == TW_MR_DATA_ACK);
128 }
129
130 /* Function Prototypes: */
131 /** Begins a master mode TWI bus communication with the given slave device address.
132 *
133 * \param[in] SlaveAddress Address of the slave TWI device to communicate with
134 *
135 * \return Boolean true if the device is ready for data, false otherwise
136 */
137 bool TWI_StartTransmission(uint8_t SlaveAddress);
138
139 /* Disable C linkage for C++ Compilers: */
140 #if defined(__cplusplus)
141 }
142 #endif
143
144 #endif
145
146 /** @} */