code:i2c_eeprom
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
code:i2c_eeprom [2008/11/06 00:24] – laurenceb | code:i2c_eeprom [2009/03/20 04:50] (current) – laurenceb | ||
---|---|---|---|
Line 11: | Line 11: | ||
void init_i2c() | void init_i2c() | ||
{ | { | ||
- | TWBR=(u08)((float)F_CPU/ | + | TWBR=(u08)(((float)F_CPU/ |
- | } //we dont need to enable the hardware - its done in write | + | } //we dont need to enable the hardware - its done in write |
void i2cstart() | void i2cstart() | ||
{ | { | ||
+ | u16 timeout=1; | ||
TWCR = (1<< | TWCR = (1<< | ||
- | while (!(TWCR & (1<< | + | while (!(TWCR & (1<< |
+ | timeout++; | ||
+ | if(!timeout) | ||
+ | I2Cerr|=16; | ||
+ | TWCR = (1<< | ||
if (((TWSR & 0xF8) != TW_START)&& | if (((TWSR & 0xF8) != TW_START)&& | ||
- | I2Cerr|=1; | + | I2Cerr|=1; |
} | } | ||
- | # | + | void i2cstop() |
+ | { | ||
+ | u16 timeout=1; | ||
+ | TWCR = (1<< | ||
+ | while (!(TWCR & (1<< | ||
+ | timeout++; | ||
+ | if(!timeout) | ||
+ | I2Cerr|=64; | ||
+ | } | ||
void i2cwrite(u08 c) | void i2cwrite(u08 c) | ||
{ | { | ||
- | TWDR = c; //load the data | + | u16 timeout=1; |
+ | TWDR = c; //load the data | ||
TWCR = (1<< | TWCR = (1<< | ||
- | while (!(TWCR & (1<< | + | while (!(TWCR & (1<< |
- | if ( ((TWSR & 0xF8) !=TW_MT_SLA_ACK) & ((TWSR & 0xF8) !=TW_MR_SLA_ACK) ) | + | timeout++; |
- | I2Cerr|=2; | + | if(!timeout) |
+ | I2Cerr|=32; | ||
+ | if ( ((TWSR& | ||
+ | I2Cerr|=2; | ||
} | } | ||
- | u08 i2cread(u08 | + | u08 i2cread(u08 |
{ | { | ||
u16 timeout=1; | u16 timeout=1; | ||
- | TWCR=AK; | + | TWCR=ak; |
- | while (!(TWCR & (1<< | + | while |
- | timeout++; | + | timeout++; |
if(!timeout) | if(!timeout) | ||
- | I2Cerr|=8; | + | I2Cerr|=8; |
- | if((TWSR & 0xF8) !=TW_MR_DATA_ACK) | + | if(((TWSR & 0xF8) !=TW_MR_DATA_ACK) && ((TWSR & 0xF8) !=TW_MR_DATA_NACK)) |
I2Cerr|=4; | I2Cerr|=4; | ||
return TWDR; | return TWDR; | ||
Line 49: | Line 66: | ||
//Designed for 12 byte records atm | //Designed for 12 byte records atm | ||
- | void set_address(u32 * address) //Sets an address from a paointer | + | void set_address(u32 * address) //Sets an address from a pointer |
{ | { | ||
i2cstart(); | i2cstart(); | ||
- | i2cwrite(SLA_W|(((*address)>> | + | i2cwrite(SLA_W|(((*address)>> |
- | i2cwrite((u08)((*address)>> | + | i2cwrite((u08)((*address)>> |
- | i2cwrite((u08)(*address)); | + | i2cwrite((u08)(*address)); |
} | } | ||
void write_data(u08 * c, u08 datasize, u32 * address)// | void write_data(u08 * c, u08 datasize, u32 * address)// | ||
{ | { | ||
- | u08 n; //up to 256 bytes can be written at once | + | u08 n; //up to 256 bytes can be written at once |
for(n=0; | for(n=0; | ||
{ | { | ||
- | if(!((u08)*address)&0xEF) //check the address for overflow for the 7 LSB | + | if(!((u08)*address)) //check the address for overflow for the 8 LSB (Atmel) |
{ | { | ||
- | i2cstop; | + | i2cstop(); //triggers a page write |
- | _delay_loop_2( (u16) ( (float)F_CPU*0.003/4.0 ) );// | + | _delay_loop_2( (u16) ( (float)F_CPU*0.006/4.0 ) );// |
- | set_address(address); | + | set_address(address); |
} | } | ||
- | i2cwrite(c[n]); | + | i2cwrite(c[n]); |
} | } | ||
} | } | ||
Line 77: | Line 94: | ||
datasize--; | datasize--; | ||
i2cstart(); | i2cstart(); | ||
- | i2cwrite(SLA_R|(((*address)>> | + | i2cwrite(SLA_R|((u08)((*address)>> |
- | for(n=0; | + | for(n=0; |
{ | { | ||
destination[n]=i2cread(_AK_); | destination[n]=i2cread(_AK_); | ||
- | (*address)++; | + | (*address)++; |
- | if(!(u16)*address) //we reached the end of the block | + | /*if(!(u16)(*address)) //we reached the end of the block |
{ | { | ||
- | i2cstop; | + | i2cstop(); |
set_address(address); | set_address(address); | ||
- | } | + | }This isn't needed on the Atmel chips*/ |
} | } | ||
- | destination[datasize+1]=i2cread(_NAK_); | + | destination[datasize]=i2cread(_NAK_); |
+ | i2cstop(); | ||
+ | (*address)++; | ||
} | } | ||
- | u32 findtop() //Find the address of record number where we enter painted eeprom | + | |
+ | u32 findtop() //Find the address of record number where we enter painted eeprom | ||
{ | { | ||
- | u32 top=65532; | + | u32 top=12*((u32)1<< |
- | u08 n; | + | u08 n, |
- | u08 probe; | + | u16 place=1<< |
- | for(n=17;n;n--) //start by probing half way through | + | for(n=14;endcond;) //start by probing half way through |
{ | { | ||
- | set_address(& | + | set_address(& |
- | probe=i2cread(_AK_); | + | if(n) |
- | if(probe!=MAGIC_NUMBER) //see if its unpainted | + | n--; // |
- | { | + | else |
- | top|=1<<(n-1); | + | endcond=FALSE; |
- | top = 12* ( (u32) ( (top | (1<< | + | i2cstart(); |
- | } | + | i2cwrite(SLA_R); |
+ | if(i2cread(_NAK_)==MAGIC_NUMBER) //see if its painted | ||
+ | place&=~(1<< | ||
+ | if(n) | ||
+ | place|=(1<< | ||
+ | i2cstop(); | ||
+ | if(place> | ||
+ | place=10922; | ||
+ | top = 12*( (u32)place ); //place is in units of 12 | ||
} | } | ||
+ | set_address(& | ||
+ | i2cstart(); | ||
+ | i2cwrite(SLA_R); | ||
+ | if(i2cread(_NAK_)!=MAGIC_NUMBER) // | ||
+ | place++; | ||
+ | top = 12*( (u32)place ); | ||
return top; | return top; | ||
} | } | ||
- | void painteeprom() //Paints every 12th location with the magic number | + | void painteeprom() //Paints every 12th location with the magic number |
{ | { | ||
- | u08 magic=MAGIC_NUMBER; | + | u08 magic=MAGIC_NUMBER; |
u32 n=0; | u32 n=0; | ||
- | for(n=0; | + | for(; |
{ | { | ||
- | set_address(& | + | /* |
- | write_data(& | + | set_address(& |
+ | i2cwrite(magic); | ||
+ | if((u08)n> | ||
+ | { | ||
+ | i2cstop(); | ||
+ | _delay_loop_2( (u16) ( (float)F_CPU*0.006/ | ||
+ | }*/ | ||
+ | if(!((u08)n)) //this code is for writing every byte | ||
+ | { | ||
+ | i2cstop(); | ||
+ | _delay_loop_2( (u16) ( (float)F_CPU*0.006/ | ||
+ | set_address(& | ||
+ | } | ||
+ | i2cwrite(magic); | ||
+ | wdt_reset(); | ||
} | } | ||
+ | i2cstop(); | ||
} | } | ||
#endif | #endif | ||
Line 124: | Line 172: | ||
===== I2Cmem.h ===== | ===== I2Cmem.h ===== | ||
- | <code c> | + | <code c># |
- | #include < | + | |
#include < | #include < | ||
#include " | #include " | ||
Line 139: | Line 186: | ||
void init_i2c(); | void init_i2c(); | ||
void i2cstart(); | void i2cstart(); | ||
+ | void i2cstop(); | ||
void i2cwrite(u08 c); | void i2cwrite(u08 c); | ||
u08 i2cread(u08 AK); | u08 i2cread(u08 AK); |
code/i2c_eeprom.1225931071.txt.gz · Last modified: 2008/11/06 00:24 by laurenceb