Changeset - 9aa54f04a19a
[Not reviewed]
default
0 2 0
kripperger@CL-SEC241-09.cedarville.edu - 12 years ago 2013-03-19 23:47:01
kripperger@CL-SEC241-09.cedarville.edu
Timeouts
2 files changed with 91 insertions and 20 deletions:
0 comments (0 inline, 0 general)
slave/slave/lib/i2c.c
Show inline comments
 
@@ -7,12 +7,14 @@
 
 
#include <inttypes.h>
 
#include <compat/twi.h>
 
#include "loopTimer.h"
 
#include "../config.h"
 
#include "i2c.h"
 
 
uint32_t startTime = 0;
 
 
/* I2C clock in Hz */
 
#define SCL_CLOCK  100000L
 
#define SCL_CLOCK  300000L
 
 
 
/*************************************************************************
 
@@ -20,10 +22,11 @@
 
*************************************************************************/
 
void i2c_init(void)
 
{
 
  /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
 
  /* initialize TWI clock: 300 kHz clock, TWPS = 0 => prescaler = 1 */
 
  
 
  TWSR = 0;                         /* no prescaler */
 
  TWBR = ((F_CPU/SCL_CLOCK)-16)/2;  /* must be > 10 for stable operation */
 
  
 
 
}/* i2c_init */
 
 
@@ -41,7 +44,14 @@ unsigned char i2c_start(unsigned char ad
 
	TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
 
 
	// wait until transmission completed
 
	while(!(TWCR & (1<<TWINT)));
 
	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
	}
 
 
	// check value of TWI Status Register. Mask prescaler bits.
 
	twst = TW_STATUS & 0xF8;
 
@@ -52,7 +62,14 @@ unsigned char i2c_start(unsigned char ad
 
	TWCR = (1<<TWINT) | (1<<TWEN);
 
 
	// wail until transmission completed and ACK/NACK has been received
 
	while(!(TWCR & (1<<TWINT)));
 
	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
	}
 
 
	// check value of TWI Status Register. Mask prescaler bits.
 
	twst = TW_STATUS & 0xF8;
 
@@ -81,7 +98,14 @@ void i2c_start_wait(unsigned char addres
 
	    TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
 
    
 
    	// wait until transmission completed
 
    	while(!(TWCR & (1<<TWINT)));
 
		startTime = time_millis();
 
    	while(!(TWCR & (1<<TWINT)))
 
		{
 
			if ((time_millis() - startTime) > 10)
 
			{
 
				break;	// Timeout Reached!
 
			}
 
		}
 
    
 
    	// check value of TWI Status Register. Mask prescaler bits.
 
    	twst = TW_STATUS & 0xF8;
 
@@ -92,7 +116,14 @@ void i2c_start_wait(unsigned char addres
 
    	TWCR = (1<<TWINT) | (1<<TWEN);
 
    
 
    	// wail until transmission completed
 
    	while(!(TWCR & (1<<TWINT)));
 
		startTime = time_millis();
 
    	while(!(TWCR & (1<<TWINT)))
 
		{
 
			if ((time_millis() - startTime) > 10)
 
			{
 
				break;	// Timeout Reached!
 
			}
 
		}
 
    
 
    	// check value of TWI Status Register. Mask prescaler bits.
 
    	twst = TW_STATUS & 0xF8;
 
@@ -102,7 +133,14 @@ void i2c_start_wait(unsigned char addres
 
	        TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
 
	        
 
	        // wait until stop condition is executed and bus released
 
	        while(TWCR & (1<<TWSTO));
 
			startTime = time_millis();
 
	        while(TWCR & (1<<TWSTO))
 
			{
 
				if ((time_millis() - startTime) > 10)
 
				{
 
					break;	// Timeout Reached!
 
				}
 
			}
 
	        
 
    	    continue;
 
    	}
 
@@ -139,7 +177,14 @@ void i2c_stop(void)
 
	TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
 
	
 
	// wait until stop condition is executed and bus released
 
	while(TWCR & (1<<TWSTO));
 
	startTime = time_millis();
 
	while(TWCR & (1<<TWSTO))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
	}
 
 
}/* i2c_stop */
 
 
@@ -161,8 +206,16 @@ unsigned char i2c_writeX( unsigned char 
 
	TWCR = (1<<TWINT) | (1<<TWEN);
 
 
	// wait until transmission completed
 
	while(!(TWCR & (1<<TWINT)));
 
 
	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
	}
 
	
 
	
 
	// check value of TWI Status Register. Mask prescaler bits
 
	twst = TW_STATUS & 0xF8;
 
	if( twst != TW_MT_DATA_ACK) return 1;
 
@@ -180,7 +233,15 @@ unsigned char i2c_writeX( unsigned char 
 
unsigned char i2c_readAck(void)
 
{
 
	TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
 
	while(!(TWCR & (1<<TWINT)));    
 
	
 
	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
	}  
 
 
    return TWDR;
 
 
@@ -196,7 +257,15 @@ unsigned char i2c_readAck(void)
 
unsigned char i2c_readNak(void)
 
{
 
	TWCR = (1<<TWINT) | (1<<TWEN);
 
	while(!(TWCR & (1<<TWINT)));
 
	
 
	startTime = time_millis();
 
	while(!(TWCR & (1<<TWINT)))
 
	{
 
		if ((time_millis() - startTime) > 10)
 
		{
 
			break;	// Timeout Reached!
 
		}
 
	}
 
	
 
    return TWDR;
 
slave/slave/slave.c
Show inline comments
 
@@ -41,20 +41,18 @@ bool WDTreset = false;
 
void micro_setup()
 
{
 
	// Generic microcontroller config options
 
	sei();			// Enable interrupts
 
	WDTreset = ((MCUSR & 0b00001000) > 0);	// Set variable if WDT reset occured
 
	WDTreset = ((MCUSR & 0b00001000) > 0);	// Check if WDT reset occured
 
	MCUSR = 0;		// Clear reset flags
 
	wdt_disable();	// Disable WDT
 
	_delay_ms(20);	// Power debounce
 
	sei();			// Enable interrupts
 
}
 

	
 

	
 

	
 
int main(void)
 
{
 
	// Writes ID to EEPROM, change for all modules and delete after programming
 
	// 0 is for generic setup,	 1 is for sensors,	 2 is for Geiger,	3 is for cameras
 
	//i2c_write(EEPROM_ADDR, 0x05, 0x03);
 

	
 
		
 
	// Initialize	
 
	micro_setup();			// Generic microcontroller config options
 
@@ -67,13 +65,12 @@ int main(void)
 
	serial1_setup();		// Config serial port 1
 
	
 
	_delay_ms(50);	// Setup hold delay
 
	if(WDTreset) led_on(1);	// Turn on LED if WDT reset has occurred	
 
	
 
	io_readModuleId();
 
	modules_setup(io_getModuleId());	// Run setup functions for specific module
 

	
 
	uint32_t lastLoop = 0;
 
	
 
	if(WDTreset) led_on(1);	// Turn on LED if WDT reset has occured
 
	uint32_t lastLoop = 0;	// Time in ms when last loop occurred 
 
	
 
	// Serial output //DEBUG
 
	char buff[128];						//Buffer for serial output //DEBUG
 
@@ -101,6 +98,11 @@ int main(void)
 
			
 
			led_off(0);
 
			lastLoop = time_millis();
 
			
 
// Writes ID to EEPROM, change for all modules and delete after programming
 
// 0 is for generic setup,	 1 is for sensors,	 2 is for Geiger,	3 is for cameras
 
//i2c_write(EEPROM_ADDR, 0x05, 0x03);			
 
			
 
		}
 

	
 
    }
0 comments (0 inline, 0 general)