SMBus相關問題
各位好!!
小弟最近正在研究如何使用SMBus來做傳輸,小弟現在問題卡在為什麼我一直收到的都是NACK,就是進行不到讀或寫的動作,是不是我的CODE有什麼問題呢? 以下是我用Sample code去改的code,是否可以幫忙看看是不是哪邊我弄錯了呢?
我對Slave(EEPROM)讀的流程如下
Start>Slave Address+w>ACK>Index Address 0/1>SR>Slave Address+R>ACK>DATA0/1>NA>P
//-----------------------------------------------------------------------------
// SMBus Interrupt Service Routine (ISR)
//-----------------------------------------------------------------------------
void SMBus_ISR (void) interrupt 7
{
bit FAIL = 0; // Used by the ISR to flag failed
// transfers
static char i; // Used by the ISR to count the
// number of data bytes sent or
// received
static bit SEND_START = 0; // Send a start
// WP = 0;
switch (SMB0CN & 0xF0) // Status vector
{
// Master Transmitter/Receiver: START condition transmitted.
case SMB_MTSTA:
SMB0DAT = TARGET; // Load address of the target slave
SMB0DAT &= 0xFE; // Clear the LSB of the address for the
// R/W bit
SMB0DAT |= SMB_RW; // Load R/W bit
STA = 0; // Manually clear START bit
// STO = 0;
i = 0; // Reset data byte counter
break;
// Master Transmitter: Data byte (or Slave Address) transmitted
case SMB_MTDB:
if (ACK) // Slave Address or Data Byte
{
//STA = 1;
//STO = 0;
SMB0DAT = 0x01; // index address
if (SEND_START)
{
STA = 1;
SEND_START = 0;
break;
}
if(SMB_SENDWORDADDR) // Are we sending the word address?
{
SMB_SENDWORDADDR = 0; // Clear flag
SMB0DAT = WORD_ADDR; // Send word address
if (SMB_RANDOMREAD)
{
SEND_START = 1; // Send a START after the next ACK cycle
SMB_RW = READ;
}
break;
}
if (SMB_RW==WRITE) // Is this transfer a WRITE?
{
if (i < SMB_DATA_LEN) // Is there data to send?
{
// send data byte
SMB0DAT = *pSMB_DATA_OUT;
// increment data out pointer
pSMB_DATA_OUT++;
// increment number of bytes sent
i++;
}
else
{
STO = 1; // Set STO to terminte transfer
SMB_BUSY = 0; // Clear software busy flag
}
}
else {} // If this transfer is a READ,
// then take no action. Slave
// address was transmitted. A
// separate 'case' is defined
// for data byte recieved.
}
else // If slave NACK,
{
if(SMB_ACKPOLL)
{
STA = 1; // Restart transfer
}
else
{
FAIL = 1; // Indicate failed transfer
} // and handle at end of ISR
}
break;
// Master Receiver: byte received
case SMB_MRDB:
if ( i < SMB_DATA_LEN ) // Is there any data remaining?
{
*pSMB_DATA_IN = SMB0DAT; // Store received byte
pSMB_DATA_IN++; // Increment data in pointer
i++; // Increment number of bytes received
ACK = 1; // Set ACK bit (may be cleared later
// in the code)
}
if (i == SMB_DATA_LEN) // This is the last byte
{
SMB_BUSY = 0; // Free SMBus interface
ACK = 0; // Send NACK to indicate last byte
// of this transfer
STO = 1; // Send STOP to terminate transfer
}
//SI = 0;
break;
default:
FAIL = 1; // Indicate failed transfer
// and handle at end of ISR
break;
}
if (FAIL) // If the transfer failed,
{
SMB0CF &= ~0x80; // Reset communication
SMB0CF |= 0x80;
STA = 0;
STO = 0;
ACK = 0;
SMB_BUSY = 0; // Free SMBus
FAIL = 0;
}
SI = 0; // Clear interrupt flag
}
void Timer3_ISR (void) interrupt 14
{
SMB0CF &= ~0x80; // Disable SMBus
SMB0CF |= 0x80; // Re-enable SMBus
TMR3CN &= ~0x80; // Clear Timer3 interrupt-pending flag
SMB_BUSY = 0; // Free bus
}
//-----------------------------------------------------------------------------
// Support Functions
//-----------------------------------------------------------------------------
void EEPROM_ByteWrite(unsigned char addr, unsigned char dat)
{
while (SMB_BUSY); // Wait for SMBus to be free.
SMB_BUSY = 1; // Claim SMBus (set to busy)
// Set SMBus ISR parameters
TARGET = EEPROM_ADDR; // Set target slave address
SMB_RW = WRITE; // Mark next transfer as a write
SMB_SENDWORDADDR = 1; // Send Word Address after Slave Address
SMB_RANDOMREAD = 0; // Do not send a START signal after
// the word address
SMB_ACKPOLL = 1; // Enable Acknowledge Polling (The ISR
// will automatically restart the
// transfer if the slave does not
// acknoledge its address.
// Specify the Outgoing Data
WORD_ADDR = addr; // Set the target address in the
// EEPROM's internal memory space
SMB_SINGLEBYTE_OUT = dat; // Store <dat> (local variable) in a
// global variable so the ISR can read
// it after this function exits
// The outgoing data pointer points to the <dat> variable
pSMB_DATA_OUT = &SMB_SINGLEBYTE_OUT;
SMB_DATA_LEN = 1; // Specify to ISR that the next transfer
// will contain one data byte
// Initiate SMBus Transfer
STA = 1;
}
unsigned char EEPROM_ByteRead(unsigned char addr)
{
unsigned char retval; // Holds the return value
while (SMB_BUSY); // Wait for SMBus to be free.
SMB_BUSY = 1; // Claim SMBus (set to busy)
// Set SMBus ISR parameters
TARGET = EEPROM_ADDR; // Set target slave address
SMB_RW = WRITE; // A random read starts as a write
// then changes to a read after
// the repeated start is sent. The
// ISR handles this switchover if
// the <SMB_RANDOMREAD> bit is set.
SMB_SENDWORDADDR = 1; // Send Word Address after Slave Address
SMB_RANDOMREAD = 1; // Send a START after the word address
SMB_ACKPOLL = 1; // Enable Acknowledge Polling
// Specify the Incoming Data
WORD_ADDR = addr; // Set the target address in the
// EEPROM's internal memory space
//retval = SMB0DAT; //percy
pSMB_DATA_IN = &retval; // The incoming data pointer points to
// the <retval> variable.
SMB_DATA_LEN = 1; // Specify to ISR that the next transfer
// will contain one data byte
// Initiate SMBus Transfer
STA = 1;
while(SMB_BUSY); // Wait until data is read
retval = * pSMB_DATA_IN;
return retval;
}