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