1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2008, Atmel Corporation
\r
6 * All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions are met:
\r
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\r
14 * Atmel's name may not be used to endorse or promote products derived from
\r
15 * this software without specific prior written permission.
\r
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
27 * ----------------------------------------------------------------------------
\r
30 //------------------------------------------------------------------------------
\r
35 /// Interface for configuration the Two Wire Interface (TWI) peripheral.
\r
39 /// -# Configures a TWI peripheral to operate in master mode, at the given
\r
40 /// frequency (in Hz) using TWI_ConfigureMaster().
\r
41 /// -# or if hardware possible, configures a TWI peripheral to operate in
\r
42 /// slave mode, at the given frequency (in Hz) using TWI_ConfigureSlave().
\r
43 /// -# Sends a STOP condition on the TWI using TWI_Stop().
\r
44 /// -# Starts a read operation on the TWI bus with the specified slave using
\r
45 /// TWI_StartRead(). Data must then be read using TWI_ReadByte() whenever
\r
46 /// a byte is available (poll using TWI_ByteReceived()).
\r
47 /// -# Starts a write operation on the TWI to access the selected slave using
\r
48 /// TWI_StartWrite(). A byte of data must be provided to start the write;
\r
49 /// other bytes are written next.
\r
50 /// -# Sends a byte of data to one of the TWI slaves on the bus using TWI_WriteByte().
\r
51 /// This function must be called once before TWI_StartWrite() with the first byte of data
\r
52 /// to send, then it shall be called repeatedly after that to send the remaining bytes.
\r
53 /// -# Check if a byte has been received and can be read on the given TWI
\r
54 /// peripheral using TWI_ByteReceived().
\r
55 /// Check if a byte has been sent using TWI_ByteSent().
\r
56 /// -# Check if the current transmission is complete (the STOP has been sent)
\r
57 /// using TWI_TransferComplete().
\r
58 /// -# Enables & disable the selected interrupts sources on a TWI peripheral
\r
59 /// using TWI_EnableIt() and TWI_DisableIt().
\r
60 /// -# Get current status register of the given TWI peripheral using
\r
61 /// TWI_GetStatus(). Get current status register of the given TWI peripheral, but
\r
62 /// masking interrupt sources which are not currently enabled using
\r
63 /// TWI_GetMaskedStatus().
\r
64 //------------------------------------------------------------------------------
\r
69 //------------------------------------------------------------------------------
\r
71 //------------------------------------------------------------------------------
\r
75 //------------------------------------------------------------------------------
\r
76 // Global definitions
\r
77 //------------------------------------------------------------------------------
\r
79 // Missing AT91C_TWI_TXRDY definition.
\r
80 #ifndef AT91C_TWI_TXRDY
\r
81 #define AT91C_TWI_TXRDY AT91C_TWI_TXRDY_MASTER
\r
84 // Missing AT91C_TWI_TXCOMP definition.
\r
85 #ifndef AT91C_TWI_TXCOMP
\r
86 #define AT91C_TWI_TXCOMP AT91C_TWI_TXCOMP_MASTER
\r
89 //------------------------------------------------------------------------------
\r
91 //------------------------------------------------------------------------------
\r
93 /// Returns 1 if the TXRDY bit (ready to transmit data) is set in the given
\r
94 /// status register value.
\r
95 #define TWI_STATUS_TXRDY(status) ((status & AT91C_TWI_TXRDY) == AT91C_TWI_TXRDY)
\r
97 /// Returns 1 if the RXRDY bit (ready to receive data) is set in the given
\r
98 /// status register value.
\r
99 #define TWI_STATUS_RXRDY(status) ((status & AT91C_TWI_RXRDY) == AT91C_TWI_RXRDY)
\r
101 /// Returns 1 if the TXCOMP bit (transfer complete) is set in the given
\r
102 /// status register value.
\r
103 #define TWI_STATUS_TXCOMP(status) ((status & AT91C_TWI_TXCOMP) == AT91C_TWI_TXCOMP)
\r
105 //------------------------------------------------------------------------------
\r
106 // Global functions
\r
107 //------------------------------------------------------------------------------
\r
109 extern void TWI_ConfigureMaster(AT91S_TWI *pTwi, unsigned int twck, unsigned int mck);
\r
111 #ifdef AT91C_TWI_SVEN // TWI slave
\r
112 extern void TWI_ConfigureSlave(AT91S_TWI *pTwi, unsigned char slaveAddress);
\r
115 extern void TWI_Stop(AT91S_TWI *pTwi);
\r
117 extern void TWI_StartRead(
\r
119 unsigned char address,
\r
120 unsigned int iaddress,
\r
121 unsigned char isize);
\r
123 extern unsigned char TWI_ReadByte(AT91S_TWI *pTwi);
\r
125 extern void TWI_WriteByte(AT91S_TWI *pTwi, unsigned char byte);
\r
127 extern void TWI_StartWrite(
\r
129 unsigned char address,
\r
130 unsigned int iaddress,
\r
131 unsigned char isize,
\r
132 unsigned char byte);
\r
134 extern unsigned char TWI_ByteReceived(AT91S_TWI *pTwi);
\r
136 extern unsigned char TWI_ByteSent(AT91S_TWI *pTwi);
\r
138 extern unsigned char TWI_TransferComplete(AT91S_TWI *pTwi);
\r
140 extern void TWI_EnableIt(AT91S_TWI *pTwi, unsigned int sources);
\r
142 extern void TWI_DisableIt(AT91S_TWI *pTwi, unsigned int sources);
\r
144 extern unsigned int TWI_GetStatus(AT91S_TWI *pTwi);
\r
146 extern unsigned int TWI_GetMaskedStatus(AT91S_TWI *pTwi);
\r
148 extern void TWI_SendSTOPCondition(AT91S_TWI *pTwi);
\r
150 #endif //#ifndef TWI_H
\r