Other Parts Discussed in Thread: MSP430F2272
Hi there. It's me again. :)
Again I have some issues with the MSP430F2272 and the ATMEL AT24C512B EEPROM.
The Issue is when trying to read from the eeprom it stops after the second byte. It should read 12 bytes.
On the picture I'm calling "fnReadSystemValues();" and you can see the first 2 bytes of "fnStoreSystemValues();
Attached you can find the code.
Is there a way to determine if the NACK has been created by the stop condition or by the EEPROM?
Thx for the support. working on this for several hours now and running totaly out of ideas.
/*
* I2C.c
*
* Created on: 02.08.2012
* Author: l-doerner
*/
#include "I2C.h"
UINT16 Address = 0;
UINT16 Length = 0;
UINT8 *pTransmit = 0;
UINT8 *pReceive = 0;
UINT8 *pAddress = 0;
UINT8 Target = WRITE_HISTORY;
UINT8 Addressing_Flag = 0;
void fnInitI2C (void)
{
UCB0CTL1 = UCSWRST;
/* I2C | master | single master | synchronous mode | slave address 7bit | own address 7bit */
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;
/* enable SW reset | use SMCLK | normal ACK | no START | no STOP | reciver */
UCB0CTL1 = UCSSEL_2 + UCSWRST;
/* 12MHz/120 = 100KHz */
UCB0BR0 = 0x78;
UCB0BR1 = 0x00;
//UCB0BR0 = 0x12; // set prescaler
// UCB0BR1 = 0;
UCB0I2CSA = EEPROM;
UCB0CTL1 &= ~UCSWRST;
UCB0I2CIE = UCNACKIE; /* Not-acknowledge interrupt enable*/
}
void fnStoreHistory (void)
{
// fnInitI2C();
Target = WRITE_HISTORY;
Address = System.Actual_Page * 128 + System.Page_Position;
pAddress = (UINT8 *)& Address;
pTransmit = (UINT8 *) &history.Cylce;
Length = sizeof(history);
Addressing_Flag = 1;
IE2 = UCB0TXIE; /* USCI_B0 transmit interrupt enable */
UCB0CTL1 |= UCTR + UCTXSTT; /* start condition I2C transmit */
while (UCB0STAT & UCBBUSY);
}
void fnStoreSystemValues (void )
{
// fnInitI2C();
Target = WRITE_SYSTEM;
Address = 0x0000;
pAddress = (UINT8 *) &Address;
pTransmit = (UINT8 *) &System.Cycle_Count;
Length = sizeof(System);
Addressing_Flag = 1;
IE2 = UCB0TXIE; /* USCI_B0 transmit interrupt enable */
UCB0CTL1 |= UCTR + UCTXSTT; /* start condition I2C transmit */
while (UCB0STAT & UCBBUSY);
}
void fnReadHistory (UINT16 Start_Address, UINT16 Length)
{
// fnInitI2C();
Target = READ_HISTORY;
Address = Start_Address;
pAddress = (UINT8 *) &Address;
Length = Length;
Addressing_Flag = 1;
IE2 = UCB0TXIE; /* USCI_B0 transmit interrupt enable */
UCB0CTL1 |= UCTR + UCTXSTT; /* start condition I2C transmit */
while (UCB0STAT & UCBBUSY);
}
void fnReadSystemValues (void)
{
// fnInitI2C();
Target = READ_SYSTEM;
Address = 0x0000;
pAddress = (UINT8 *) &Address;
pReceive = (UINT8 *)& System.Cycle_Count;
Length = sizeof(System);
Addressing_Flag = 1;
IE2 = UCB0TXIE; /* USCI_B0 transmit interrupt enable */
UCB0CTL1 |= UCTR + UCTXSTT; /* start condition I2C transmit */
while (UCB0STAT & UCBBUSY);
}
void fnCheckSystemValues (void)
{
fnReadSystemValues();
if (System.Cycle_Count == 0xFFFF)
{
System.Cycle_Count = 0;
System.Actual_Page = HISTORY_START_PAGE;
System.Page_Position = 0;
System.Completed_Cycles = 0;
System.cal_value = 0;
fnStoreSystemValues();
}
}
inline void fnCheck_Page_Overflow(void)
{
System.Page_Position++;
if(System.Page_Position == 128)
{
System.Page_Position = 0;
if (System.Actual_Page == 511)
{
System.Actual_Page = HISTORY_START_PAGE;
}
else
{
System.Actual_Page++;
}
Addressing_Flag = 1;
Address = System.Actual_Page * 128 + System.Page_Position;
pAddress = (UINT8 *) &Address;
}
}
void fnResetHistoryValues (void)
{
history.Cylce = 0;
history.ProfileNumber = 0;
history.Start_Voltage = 0;
history.End_Voltage = 0;
history.Ah_charged = 0;
history.Refresh_Counter = 0;
history.Faults = 0;
history.Completed_Cycles = 0;
}
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR (void)
{
static UINT8 Address_length = 2;
if (IFG2 & UCB0RXIFG) // UCB0RXIFG is set when UCB0RXBUF has received a complete character
{
switch (Target) {
case READ_HISTORY:
if ( Length == 13) // all Bytes read
{
UCB0CTL1 |= UCTXSTP; // stop condition I2C
IFG2 &= ~UCB0RXIFG; // USCI_B0 clear transmit interrupt flag
// UARTBUFFER= UCB0RXBUF; //UARTBUFFER todo
}
else
{
// UARTBUFFER= UCB0RXBUF;
Length--;
}
break;
case READ_SYSTEM:
if(Length == 1)
{
UCB0CTL1 |= UCTXSTP; // stop condition I2C
*pReceive = UCB0TXBUF;
IFG2 &= ~UCB0RXIFG; // USCI_B0 clear transmit interrupt flag
}
else
{
Length --;
*pReceive = UCB0TXBUF;
pReceive++;
IFG2 &= ~UCB0TXIFG; // USCI_B0 clear transmit interrupt flag
}
break;
default:
break;
}
}
else
if (IFG2 & UCB0TXIFG)
{
if (Addressing_Flag)
{
if(Address_length == 0)
{
Addressing_Flag = 0;
Address_length = 2;
switch (Target)
{
case WRITE_HISTORY:
UCB0TXBUF = *pTransmit;
pTransmit++;
Length--;
fnCheck_Page_Overflow();
break;
case WRITE_SYSTEM:
UCB0TXBUF = *pTransmit;
pTransmit++;
Length--;
break;
case READ_HISTORY:
case READ_SYSTEM:
IFG2 &= ~UCB0TXIFG; // USCI_B0 clear transmit interrupt flag
IE2 = UCB0RXIE; // USCI_B0 receive interrupt enable
UCB0CTL1 &= ~UCTR; // disable transmit
UCB0CTL1 |= UCTXSTT; // restart condition I2C receive
break;
default:
break;
}
}
else
{
UCB0TXBUF = *pAddress;
IFG2 &= ~UCB0TXIFG; // USCI_B0 clear transmit interrupt flag
pAddress++;
Address_length--;
}
}
else
{
if ( Length == 1)
{
UCB0CTL1 |= UCTXSTP; // stop condition I2C
IFG2 &= ~UCB0TXIFG; // USCI_B0 clear transmit interrupt flag
}
else
{
UCB0TXBUF = *pTransmit;
pTransmit++;
Length--;
if(Target == WRITE_HISTORY)
{
fnCheck_Page_Overflow();
}
}
}
}
}
