#include <inttypes.h>
#include <compat/twi.h>
#include "i2cmaster.h"
Include-Abhängigkeitsdiagramm für twimaster.c:
gehe zum Quellcode dieser Datei
Makrodefinitionen | |
#define | SCL_CLOCK 100000L |
Funktionen | |
void | i2c_init (void) |
initialize the I2C master interace. Need to be called only once | |
unsigned char | i2c_start (unsigned char address) |
Issues a start condition and sends address and transfer direction. | |
void | i2c_start_wait (unsigned char address) |
Issues a start condition and sends address and transfer direction. | |
unsigned char | i2c_rep_start (unsigned char address) |
Issues a repeated start condition and sends address and transfer direction. | |
void | i2c_stop (void) |
Terminates the data transfer and releases the I2C bus. | |
unsigned char | i2c_write (unsigned char data) |
Send one byte to I2C device. | |
unsigned char | i2c_readAck (void) |
read one byte from the I2C device, request more data from device | |
unsigned char | i2c_readNak (void) |
read one byte from the I2C device, read is followed by a stop condition |
#define SCL_CLOCK 100000L |
void i2c_init | ( | void | ) |
initialize the I2C master interace. Need to be called only once
void |
Definiert in Zeile 27 der Datei twimaster.c.
Benutzt SCL_CLOCK.
Wird benutzt von main().
00028 { 00029 /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */ 00030 00031 TWSR = 0; /* no prescaler */ 00032 TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */ 00033 00034 }/* i2c_init */
unsigned char i2c_readAck | ( | void | ) |
read one byte from the I2C device, request more data from device
Definiert in Zeile 180 der Datei twimaster.c.
00181 { 00182 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); 00183 while(!(TWCR & (1<<TWINT))); 00184 00185 return TWDR; 00186 00187 }/* i2c_readAck */
unsigned char i2c_readNak | ( | void | ) |
read one byte from the I2C device, read is followed by a stop condition
Definiert in Zeile 195 der Datei twimaster.c.
00196 { 00197 TWCR = (1<<TWINT) | (1<<TWEN); 00198 while(!(TWCR & (1<<TWINT))); 00199 00200 return TWDR; 00201 00202 }/* i2c_readNak */
unsigned char i2c_rep_start | ( | unsigned char | addr | ) |
Issues a repeated start condition and sends address and transfer direction.
addr | address and transfer direction of I2C device |
0 | device accessible | |
1 | failed to access device |
Definiert in Zeile 128 der Datei twimaster.c.
Benutzt i2c_start().
00129 { 00130 return i2c_start( address ); 00131 00132 }/* i2c_rep_start */
unsigned char i2c_start | ( | unsigned char | addr | ) |
Issues a start condition and sends address and transfer direction.
addr | address and transfer direction of I2C device |
0 | device accessible | |
1 | failed to access device |
Definiert in Zeile 41 der Datei twimaster.c.
Wird benutzt von i2c_rep_start() und main().
00042 { 00043 uint8_t twst; 00044 00045 // send START condition 00046 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); 00047 00048 // wait until transmission completed 00049 while(!(TWCR & (1<<TWINT))); 00050 00051 // check value of TWI Status Register. Mask prescaler bits. 00052 twst = TW_STATUS & 0xF8; 00053 if ( (twst != TW_START) && (twst != TW_REP_START)) return 1; 00054 00055 // send device address 00056 TWDR = address; 00057 TWCR = (1<<TWINT) | (1<<TWEN); 00058 00059 // wail until transmission completed and ACK/NACK has been received 00060 while(!(TWCR & (1<<TWINT))); 00061 00062 // check value of TWI Status Register. Mask prescaler bits. 00063 twst = TW_STATUS & 0xF8; 00064 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1; 00065 00066 return 0; 00067 00068 }/* i2c_start */
void i2c_start_wait | ( | unsigned char | addr | ) |
Issues a start condition and sends address and transfer direction.
If device is busy, use ack polling to wait until device ready
addr | address and transfer direction of I2C device |
Definiert in Zeile 77 der Datei twimaster.c.
Wird benutzt von main().
00078 { 00079 uint8_t twst; 00080 00081 00082 while ( 1 ) 00083 { 00084 // send START condition 00085 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); 00086 00087 // wait until transmission completed 00088 while(!(TWCR & (1<<TWINT))); 00089 00090 // check value of TWI Status Register. Mask prescaler bits. 00091 twst = TW_STATUS & 0xF8; 00092 if ( (twst != TW_START) && (twst != TW_REP_START)) continue; 00093 00094 // send device address 00095 TWDR = address; 00096 TWCR = (1<<TWINT) | (1<<TWEN); 00097 00098 // wail until transmission completed 00099 while(!(TWCR & (1<<TWINT))); 00100 00101 // check value of TWI Status Register. Mask prescaler bits. 00102 twst = TW_STATUS & 0xF8; 00103 if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) ) 00104 { 00105 /* device busy, send stop condition to terminate write operation */ 00106 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); 00107 00108 // wait until stop condition is executed and bus released 00109 while(TWCR & (1<<TWSTO)); 00110 00111 continue; 00112 } 00113 //if( twst != TW_MT_SLA_ACK) return 1; 00114 break; 00115 } 00116 00117 }/* i2c_start_wait */
void i2c_stop | ( | void | ) |
Terminates the data transfer and releases the I2C bus.
void |
Definiert in Zeile 138 der Datei twimaster.c.
Wird benutzt von main().
00139 { 00140 /* send stop condition */ 00141 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); 00142 00143 // wait until stop condition is executed and bus released 00144 while(TWCR & (1<<TWSTO)); 00145 00146 }/* i2c_stop */
unsigned char i2c_write | ( | unsigned char | data | ) |
Send one byte to I2C device.
data | byte to be transfered |
0 | write successful | |
1 | write failed |
Definiert in Zeile 156 der Datei twimaster.c.
Wird benutzt von i2c_writeData() und main().
00157 { 00158 uint8_t twst; 00159 00160 // send data to the previously addressed device 00161 TWDR = data; 00162 TWCR = (1<<TWINT) | (1<<TWEN); 00163 00164 // wait until transmission completed 00165 while(!(TWCR & (1<<TWINT))); 00166 00167 // check value of TWI Status Register. Mask prescaler bits 00168 twst = TW_STATUS & 0xF8; 00169 if( twst != TW_MT_DATA_ACK) return 1; 00170 return 0; 00171 00172 }/* i2c_write */