3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
33 * Master mode TWI driver for the 8-Bit AVRs containing a hardware TWI module.
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.
39 /** \ingroup Group_TWI
40 * @defgroup Group_TWI_AVR8 8-Bit AVR TWI Driver
42 * Master mode TWI driver for the 8-Bit AVRs containing a hardware TWI module.
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.
50 #ifndef __TWI_AVR8_H__
51 #define __TWI_AVR8_H__
58 /* Enable C linkage for C++ Compilers: */
59 #if defined(__cplusplus)
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.
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.
74 static inline void TWI_Init(void);
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.
79 static inline void TWI_ShutDown(void);
81 #define TWI_Init() MACROS{ TWCR |= (1 << TWEN); }MACROE
82 #define TWI_ShutDown() MACROS{ TWCR &= ~(1 << TWEN); }MACROE
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)
89 TWCR
= ((1 << TWINT
) | (1 << TWSTO
) | (1 << TWEN
));
92 /** Sends a byte to the currently addressed device on the TWI bus.
94 * \param[in] Byte Byte to send to the currently addressed device
96 * \return Boolean true if the recipient ACKed the byte, false otherwise
98 static inline bool TWI_SendByte(uint8_t Byte
)
101 TWCR
= ((1 << TWINT
) | (1 << TWEN
));
102 while (!(TWCR
& (1 << TWINT
)));
104 return ((TWSR
& TW_STATUS_MASK
) == TW_MT_DATA_ACK
);
107 /** Receives a byte from the currently addressed device on the TWI bus.
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
112 * \return Boolean true if the byte reception sucessfully completed, false otherwise
114 static inline bool TWI_ReceiveByte(uint8_t* Byte
, bool LastByte
)
116 uint8_t TWCRMask
= ((1 << TWINT
) | (1 << TWEN
));
119 TWCRMask
|= (1 << TWEA
);
122 while (!(TWCR
& (1 << TWINT
)));
125 return ((TWSR
& TW_STATUS_MASK
) == TW_MR_DATA_ACK
);
128 /* Function Prototypes: */
129 /** Begins a master mode TWI bus communication with the given slave device address.
131 * \param[in] SlaveAddress Address of the slave TWI device to communicate with
133 * \return Boolean true if the device is ready for data, false otherwise
135 bool TWI_StartTransmission(uint8_t SlaveAddress
);
137 /* Disable C linkage for C++ Compilers: */
138 #if defined(__cplusplus)