Subversion Repositories idreammicro-avr

Compare Revisions

Ignore whitespace Rev 47 → Rev 46

/trunk/libraries/twi/src/twi.c
181,23 → 181,23
twi__error_t
twi__start
(
void
void
){
// Send start condition.
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
// Send start condition.
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
 
// Wait for TWINT flag set. This indicates that the START condition has
// been transmitted.
while (!(TWCR & (1 << TWINT)));
// Wait for TWINT flag set. This indicates that the START condition has
// been transmitted.
while (!(TWCR & (1 << TWINT)));
 
// Check value of TWI Status Register. Mask prescaler bits.
if (TWSR & TW_NO_INFO != TW_START)
{
// If status is different from START (TW_START), return error.
return TWI__ERROR__ERROR;
}
 
return TWI__ERROR__NONE;
// Check value of TWI Status Register. Mask prescaler bits.
if (TWSR & TW_NO_INFO != TW_START)
{
// If status is different from START (TW_START), return error.
return TWI__ERROR__ERROR;
}
return TWI__ERROR__NONE;
}
 
/**************************************************************************//**
208,24 → 208,24
twi__error_t
twi__repeat_start
(
void
void
){
// TODO: check if TWSTO must be set.
// Send start condition.
TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTA) | (1 << TWSTO);
// TODO: check if TWSTO must be set.
// Send start condition.
TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTA) | (1 << TWSTO);
 
// Wait for TWINT flag set. This indicates that the START condition has
// been transmitted.
while (!(TWCR & (1 << TWINT)));
// Wait for TWINT flag set. This indicates that the START condition has
// been transmitted.
while (!(TWCR & (1 << TWINT)));
 
// Check value of TWI Status Register. Mask prescaler bits.
if (TWSR & TW_NO_INFO != TW_REP_START)
{
// If status different from START (TW_REP_START), return error.
return TWI__ERROR__ERROR;
}
 
return TWI__ERROR__NONE;
// Check value of TWI Status Register. Mask prescaler bits.
if (TWSR & TW_NO_INFO != TW_REP_START)
{
// If status different from START (TW_REP_START), return error.
return TWI__ERROR__ERROR;
}
return TWI__ERROR__NONE;
}
 
/**************************************************************************//**
240,24 → 240,24
(
uint8_t sla
){
// Load SLA into TWDR register.
TWDR = sla | TW_READ;
// Load SLA into TWDR register.
TWDR = sla | TW_READ;
 
// Clear TWINT bit in TWCR to start transmission of address.
TWCR = (1 << TWINT) | (1 << TWEN);
// Clear TWINT bit in TWCR to start transmission of address.
TWCR = (1 << TWINT) | (1 << TWEN);
 
// Wait for TWINT flag set. This indicates that the SLA+W (TW_WRITE) has
// been transmitted, and ACK/NACK has been received.
while (!(TWCR & (1 << TWINT)));
// Wait for TWINT flag set. This indicates that the SLA+W (TW_WRITE) has
// been transmitted, and ACK/NACK has been received.
while (!(TWCR & (1 << TWINT)));
 
// Check value of TWI Status Register. Mask prescaler bits. If status
// different from MT_SLA_ACK (TW_MR_SLA_ACK), return error.
if (TWSR & TW_NO_INFO != TW_MR_SLA_ACK)
{
return TWI__ERROR__ERROR;
}
// Check value of TWI Status Register. Mask prescaler bits. If status
// different from MT_SLA_ACK (TW_MR_SLA_ACK), return error.
if (TWSR & TW_NO_INFO != TW_MR_SLA_ACK)
{
return TWI__ERROR__ERROR;
}
 
return TWI__ERROR__NONE;
return TWI__ERROR__NONE;
}
 
/**************************************************************************//**
272,24 → 272,24
(
uint8_t sla
){
// Load SLA into TWDR register.
TWDR = sla | TW_WRITE;
// Load SLA into TWDR register.
TWDR = sla | TW_WRITE;
 
// Clear TWINT bit in TWCR to start transmission of address.
TWCR = (1 << TWINT) | (1 << TWEN);
// Clear TWINT bit in TWCR to start transmission of address.
TWCR = (1 << TWINT) | (1 << TWEN);
 
// Wait for TWINT flag set. This indicates that the SLA+W (TW_WRITE) has
// been transmitted, and ACK/NACK has been received.
while (!(TWCR & (1 << TWINT)));
// Wait for TWINT flag set. This indicates that the SLA+W (TW_WRITE) has
// been transmitted, and ACK/NACK has been received.
while (!(TWCR & (1 << TWINT)));
 
// Check value of TWI Status Register. Mask prescaler bits. If status
// different from MT_SLA_ACK (TW_MT_SLA_ACK), return error.
if (TWSR & TW_NO_INFO != TW_MT_SLA_ACK)
{
return TWI__ERROR__ERROR;
}
// Check value of TWI Status Register. Mask prescaler bits. If status
// different from MT_SLA_ACK (TW_MT_SLA_ACK), return error.
if (TWSR & TW_NO_INFO != TW_MT_SLA_ACK)
{
return TWI__ERROR__ERROR;
}
 
return TWI__ERROR__NONE;
return TWI__ERROR__NONE;
}
 
/**************************************************************************//**
302,26 → 302,26
twi__error_t
twi__send_data
(
uint8_t data
uint8_t data
){
// Load data into TWDR register.
TWDR = data;
// Load data into TWDR register.
TWDR = data;
// Clear TWINT bit in TWCR to start transmission of data.
TWCR = (1 << TWINT) | (1 << TWEN);
 
// Clear TWINT bit in TWCR to start transmission of data.
TWCR = (1 << TWINT) | (1 << TWEN);
// Wait for TWINT flag set. This indicates that the data has been
// transmitted, and ACK/NACK has been receiver.
while (!(TWCR & (1 << TWINT)));
 
// Wait for TWINT flag set. This indicates that the data has been
// transmitted, and ACK/NACK has been receiver.
while (!(TWCR & (1 << TWINT)));
 
// Check value of TWI status register. Mask prescaler bits. If status
// different from MT_DATA_ACK (TW_MT_DATA_ACK), return error.
if (TWSR & TW_NO_INFO != TW_MT_DATA_ACK)
{
return TWI__ERROR__ERROR;
}
 
return TWI__ERROR__NONE;
// Check value of TWI status register. Mask prescaler bits. If status
// different from MT_DATA_ACK (TW_MT_DATA_ACK), return error.
if (TWSR & TW_NO_INFO != TW_MT_DATA_ACK)
{
return TWI__ERROR__ERROR;
}
return TWI__ERROR__NONE;
}
 
/**************************************************************************//**
334,23 → 334,23
twi__error_t
twi__receive_data_ack
(
uint8_t* p_data
uint8_t* p_data
){
// Check the preconditions.
assert(NULL != p_data);
// Check the preconditions.
assert(NULL != p_data);
 
TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWEA);
TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWEA);
while (!(TWCR & (1 << TWINT)));
 
while (!(TWCR & (1 << TWINT)));
if ((TWSR & TW_NO_INFO) != TW_MR_DATA_ACK)
{
return TWI__ERROR__ERROR;
}
*p_data = TWDR;
 
if ((TWSR & TW_NO_INFO) != TW_MR_DATA_ACK)
{
return TWI__ERROR__ERROR;
}
 
*p_data = TWDR;
 
return TWI__ERROR__NONE;
return TWI__ERROR__NONE;
}
 
/**************************************************************************//**
363,23 → 363,23
twi__error_t
twi__receive_data_nack
(
uint8_t* p_data
uint8_t* p_data
){
// Check the preconditions.
assert(NULL != p_data);
// Check the preconditions.
assert(NULL != p_data);
 
TWCR = (1 << TWEN) | (1 << TWINT);
TWCR = (1 << TWEN) | (1 << TWINT);
while (!(TWCR & (1 << TWINT)));
 
while (!(TWCR & (1 << TWINT)));
 
if ((TWSR & TW_NO_INFO) != TW_MR_DATA_NACK)
{
return TWI__ERROR__ERROR;
}
 
*p_data = TWDR;
 
return TWI__ERROR__NONE;
if ((TWSR & TW_NO_INFO) != TW_MR_DATA_NACK)
{
return TWI__ERROR__ERROR;
}
*p_data = TWDR;
return TWI__ERROR__NONE;
}
 
/**************************************************************************//**
390,13 → 390,13
void
twi__stop
(
void
void
){
// Transmit stop operation.
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
 
// Wait for stop.
while (TWCR & (1 << TWSTO));
// Transmit stop operation.
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
// Wait for stop.
while (TWCR & (1 << TWSTO));
}
 
/******************************************************************************
/trunk/libraries/twi/twi.h
79,7 → 79,7
twi__error_t
twi__start
(
void
void
);
 
/**************************************************************************//**
90,7 → 90,7
twi__error_t
twi__repeat_start
(
void
void
);
 
/**************************************************************************//**
129,7 → 129,7
twi__error_t
twi__send_data
(
uint8_t data
uint8_t data
);
 
/**************************************************************************//**
142,7 → 142,7
twi__error_t
twi__receive_data_ack
(
uint8_t* p_data
uint8_t* p_data
);
 
/**************************************************************************//**
155,7 → 155,7
twi__error_t
twi__receive_data_nack
(
uint8_t* p_data
uint8_t* p_data
);
 
/**************************************************************************//**
166,7 → 166,7
void
twi__stop
(
void
void
);
 
#ifdef _cplusplus