Xieyi code added
[gcopter.git] / people / chowming / flight_ctl_experiment / test-master-side / TWI_Master.c
blob25856e1db924930c007e4b555848508e68448347
1 /*****************************************************************************
3 * Atmel Corporation
5 * File : TWI_Master.c
6 * Compiler : IAR EWAAVR 2.28a/3.10c
7 * Revision : $Revision: 1.13 $
8 * Date : $Date: 24. mai 2004 11:31:20 $
9 * Updated by : $Author: ltwa $
11 * Support mail : avr@atmel.com
13 * Supported devices : All devices with a TWI module can be used.
14 * The example is written for the ATmega16
16 * AppNote : AVR315 - TWI Master Implementation
18 * Description : This is a sample driver for the TWI hardware modules.
19 * It is interrupt driveren. All functionality is controlled through
20 * passing information to and from functions. Se main.c for samples
21 * of how to use the driver.
24 ****************************************************************************/
26 #if __GNUC__ > 0
27 /* AVR-GCC/avr-libc */
29 # include <util/delay.h>
30 # include <avr/io.h>
31 # include <avr/interrupt.h>
32 # include <avr/pgmspace.h>
34 # define disable_interrupt cli
35 # define enable_interrupt sei
37 # define delay_cycles(n) _delay_loop_2((n) / 4)
39 # define FLASH_DECL(decl) decl PROGMEM
40 # define FLASH_READ_BYTE(addr) pgm_read_byte(addr)
41 # define FLASH_READ_WORD(addr) pgm_read_word(addr)
43 #elif defined(__ICCAVR__)
45 # include <inavr.h>
46 # include <ioavr.h>
48 # define disable_interrupt __disable_interrupt
49 # define enable_interrupt __enable_interrupt
51 # define delay_cycles(n) __delay_cycles(n)
53 # define FLASH_DECL(decl) __flash decl
54 # define FLASH_READ_BYTE(addr) *(addr)
55 # define FLASH_READ_WORD(addr) *(addr)
58 * Map interrupt vectors.
60 * Beware: nasty dragons here. Looks like a nice example for why
61 * you wouldn't want to use pragmas in a compiler, doesn't it?
63 * Do *NOT* try to reorder the macros below, or you'll suddenly find
64 * out about all kinds of IAR bugs...
66 #define PRAGMA(x) _Pragma( #x )
67 #define ISR(vec) PRAGMA( vector=vec ) __interrupt void handler_##vec(void)
69 #else
71 # error "Unsupported compiler."
73 #endif
75 #include "TWI_Master.h"
77 static unsigned char TWI_buf[ TWI_BUFFER_SIZE ]; // Transceiver buffer
78 static unsigned char TWI_msgSize; // Number of bytes to be transmitted.
79 static unsigned char TWI_state = TWI_NO_STATE; // State byte. Default set to TWI_NO_STATE.
81 union TWI_statusReg TWI_statusReg = {0}; // TWI_statusReg is defined in TWI_Master.h
83 /****************************************************************************
84 Call this function to set up the TWI master to its initial standby state.
85 Remember to enable interrupts from the main application after initializing the TWI.
86 ****************************************************************************/
87 void TWI_Master_Initialise(void)
89 TWBR = TWI_TWBR; // Set bit rate register (Baudrate). Defined in header file.
90 // TWSR = TWI_TWPS; // Not used. Driver presumes prescaler to be 00.
91 TWDR = 0xFF; // Default content = SDA released.
92 TWCR = (1<<TWEN)| // Enable TWI-interface and release TWI pins.
93 (0<<TWIE)|(0<<TWINT)| // Disable Interupt.
94 (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)| // No Signal requests.
95 (0<<TWWC); //
98 /****************************************************************************
99 Call this function to test if the TWI_ISR is busy transmitting.
100 ****************************************************************************/
101 unsigned char TWI_Transceiver_Busy( void )
103 return ( TWCR & (1<<TWIE) ); // IF TWI Interrupt is enabled then the Transceiver is busy
106 /****************************************************************************
107 Call this function to fetch the state information of the previous operation. The function will hold execution (loop)
108 until the TWI_ISR has completed with the previous operation. If there was an error, then the function
109 will return the TWI State code.
110 ****************************************************************************/
111 unsigned char TWI_Get_State_Info( void )
113 while ( TWI_Transceiver_Busy() ); // Wait until TWI has completed the transmission.
114 return ( TWI_state ); // Return error state.
117 /****************************************************************************
118 Call this function to send a prepared message. The first byte must contain the slave address and the
119 read/write bit. Consecutive bytes contain the data to be sent, or empty locations for data to be read
120 from the slave. Also include how many bytes that should be sent/read including the address byte.
121 The function will hold execution (loop) until the TWI_ISR has completed with the previous operation,
122 then initialize the next operation and return.
123 ****************************************************************************/
124 void TWI_Start_Transceiver_With_Data( unsigned char *msg, unsigned char msgSize )
126 unsigned char temp;
128 while ( TWI_Transceiver_Busy() ); // Wait until TWI is ready for next transmission.
130 TWI_msgSize = msgSize; // Number of data to transmit.
131 TWI_buf[0] = msg[0]; // Store slave address with R/W setting.
132 if (!( msg[0] & (TRUE<<TWI_READ_BIT) )) // If it is a write operation, then also copy data.
134 for ( temp = 1; temp < msgSize; temp++ )
135 TWI_buf[ temp ] = msg[ temp ];
137 TWI_statusReg.all = 0;
138 TWI_state = TWI_NO_STATE ;
139 TWCR = (1<<TWEN)| // TWI Interface enabled.
140 (1<<TWIE)|(1<<TWINT)| // Enable TWI Interupt and clear the flag.
141 (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)| // Initiate a START condition.
142 (0<<TWWC); //
145 /****************************************************************************
146 Call this function to resend the last message. The driver will reuse the data previously put in the transceiver buffers.
147 The function will hold execution (loop) until the TWI_ISR has completed with the previous operation,
148 then initialize the next operation and return.
149 ****************************************************************************/
150 void TWI_Start_Transceiver( void )
152 while ( TWI_Transceiver_Busy() ); // Wait until TWI is ready for next transmission.
153 TWI_statusReg.all = 0;
154 TWI_state = TWI_NO_STATE ;
155 TWCR = (1<<TWEN)| // TWI Interface enabled.
156 (1<<TWIE)|(1<<TWINT)| // Enable TWI Interupt and clear the flag.
157 (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)| // Initiate a START condition.
158 (0<<TWWC); //
161 /****************************************************************************
162 Call this function to read out the requested data from the TWI transceiver buffer. I.e. first call
163 TWI_Start_Transceiver to send a request for data to the slave. Then Run this function to collect the
164 data when they have arrived. Include a pointer to where to place the data and the number of bytes
165 requested (including the address field) in the function call. The function will hold execution (loop)
166 until the TWI_ISR has completed with the previous operation, before reading out the data and returning.
167 If there was an error in the previous transmission the function will return the TWI error code.
168 ****************************************************************************/
169 unsigned char TWI_Get_Data_From_Transceiver( unsigned char *msg, unsigned char msgSize )
171 unsigned char i;
173 while ( TWI_Transceiver_Busy() ); // Wait until TWI is ready for next transmission.
175 if( TWI_statusReg.lastTransOK ) // Last transmission competed successfully.
177 for ( i=0; i<msgSize; i++ ) // Copy data from Transceiver buffer.
179 msg[ i ] = TWI_buf[ i ];
182 return( TWI_statusReg.lastTransOK );
185 // ********** Interrupt Handlers ********** //
186 /****************************************************************************
187 This function is the Interrupt Service Routine (ISR), and called when the TWI interrupt is triggered;
188 that is whenever a TWI event has occurred. This function should not be called directly from the main
189 application.
190 ****************************************************************************/
191 ISR(TWI_vect)
193 static unsigned char TWI_bufPtr;
195 switch (TWSR)
197 case TWI_START: // START has been transmitted
198 case TWI_REP_START: // Repeated START has been transmitted
199 TWI_bufPtr = 0; // Set buffer pointer to the TWI Address location
200 case TWI_MTX_ADR_ACK: // SLA+W has been tramsmitted and ACK received
201 case TWI_MTX_DATA_ACK: // Data byte has been tramsmitted and ACK received
202 if (TWI_bufPtr < TWI_msgSize)
204 TWDR = TWI_buf[TWI_bufPtr++];
205 TWCR = (1<<TWEN)| // TWI Interface enabled
206 (1<<TWIE)|(1<<TWINT)| // Enable TWI Interupt and clear the flag to send byte
207 (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)| //
208 (0<<TWWC); //
209 }else // Send STOP after last byte
211 TWI_statusReg.lastTransOK = TRUE; // Set status bits to completed successfully.
212 TWCR = (1<<TWEN)| // TWI Interface enabled
213 (0<<TWIE)|(1<<TWINT)| // Disable TWI Interrupt and clear the flag
214 (0<<TWEA)|(0<<TWSTA)|(1<<TWSTO)| // Initiate a STOP condition.
215 (0<<TWWC); //
217 break;
218 case TWI_MRX_DATA_ACK: // Data byte has been received and ACK tramsmitted
219 TWI_buf[TWI_bufPtr++] = TWDR;
220 case TWI_MRX_ADR_ACK: // SLA+R has been tramsmitted and ACK received
221 if (TWI_bufPtr < (TWI_msgSize-1) ) // Detect the last byte to NACK it.
223 TWCR = (1<<TWEN)| // TWI Interface enabled
224 (1<<TWIE)|(1<<TWINT)| // Enable TWI Interupt and clear the flag to read next byte
225 (1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)| // Send ACK after reception
226 (0<<TWWC); //
227 }else // Send NACK after next reception
229 TWCR = (1<<TWEN)| // TWI Interface enabled
230 (1<<TWIE)|(1<<TWINT)| // Enable TWI Interupt and clear the flag to read next byte
231 (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)| // Send NACK after reception
232 (0<<TWWC); //
234 break;
235 case TWI_MRX_DATA_NACK: // Data byte has been received and NACK tramsmitted
236 TWI_buf[TWI_bufPtr] = TWDR;
237 TWI_statusReg.lastTransOK = TRUE; // Set status bits to completed successfully.
238 TWCR = (1<<TWEN)| // TWI Interface enabled
239 (0<<TWIE)|(1<<TWINT)| // Disable TWI Interrupt and clear the flag
240 (0<<TWEA)|(0<<TWSTA)|(1<<TWSTO)| // Initiate a STOP condition.
241 (0<<TWWC); //
242 break;
243 case TWI_ARB_LOST: // Arbitration lost
244 TWCR = (1<<TWEN)| // TWI Interface enabled
245 (1<<TWIE)|(1<<TWINT)| // Enable TWI Interupt and clear the flag
246 (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)| // Initiate a (RE)START condition.
247 (0<<TWWC); //
248 break;
249 case TWI_MTX_ADR_NACK: // SLA+W has been tramsmitted and NACK received
250 case TWI_MRX_ADR_NACK: // SLA+R has been tramsmitted and NACK received
251 case TWI_MTX_DATA_NACK: // Data byte has been tramsmitted and NACK received
252 // case TWI_NO_STATE // No relevant state information available; TWINT = “0”
253 case TWI_BUS_ERROR: // Bus error due to an illegal START or STOP condition
254 default:
255 TWI_state = TWSR; // Store TWSR and automatically sets clears noErrors bit.
256 // Reset TWI Interface
257 TWCR = (1<<TWEN)| // Enable TWI-interface and release TWI pins
258 (0<<TWIE)|(0<<TWINT)| // Disable Interupt
259 (0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)| // No Signal requests
260 (0<<TWWC); //