📄 f30x_smbus_slave_multibyte.c
字号:
CKCON &= ~0x12; // Timer1 clock source = SYSCLK / 4
#endif
TMOD = 0x20; // Timer1 in 8-bit auto-reload mode
// Timer1 configured to overflow at 1/3 the rate defined by SMB_FREQUENCY
TH1 = -(SYSCLK/SMB_FREQUENCY/SCALE/3);
TL1 = TH1; // Init Timer1
TR1 = 1; // Timer1 enabled
}
//-----------------------------------------------------------------------------
// Timer2_Init()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Timer2 configured for use by the SMBus low timeout detect feature as
// follows:
// - Timer2 in 16-bit auto-reload mode
// - SYSCLK/12 as Timer2 clock source
// - Timer2 reload registers loaded for a 25ms overflow period
// - Timer2 pre-loaded to overflow after 25ms
// - Timer2 enabled
//
void Timer2_Init (void)
{
TMR2CN = 0x00; // Timer2 configured for 16-bit auto-
// reload, low-byte interrupt disabled
CKCON &= ~0x20; // Timer2 uses SYSCLK/12
TMR2RL = -(SYSCLK/12/40); // Timer2 configured to overflow after
TMR2 = TMR2RL; // ~25ms (for SMBus low timeout detect):
// 1/.025 = 40
IE |= 0x20; // Timer2 interrupt enable
TR2 = 1; // Start Timer2
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure the Crossbar and GPIO ports.
//
// P0.0 digital open-drain SMBus SDA
// P0.1 digital open-drain SMBus SCL
// P0.2 digital push-pull LED
//
// all other port pins unused
//
void PORT_Init (void)
{
P0MDOUT = 0x00; // All P0 pins open-drain output
P0MDOUT |= 0x04; // Make the LED (P0.2) a push-pull
// output
XBR1 = 0x04; // Enable SMBus
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P0 = 0xFF;
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SMBus Interrupt Service Routine (ISR)
//-----------------------------------------------------------------------------
//
// SMBus ISR state machine
// - Slave only implementation - no master states defined
// - All incoming data is written to global variable <SMB_data_IN>
// - All outgoing data is read from global variable <SMB_data_OUT>
//
void SMBus_ISR (void) interrupt 6
{
bit RXTX = 0; // Software flag for slave
// receiver-to-transmitter transition
static unsigned char sent_byte_counter;
static unsigned char rec_byte_counter;
if (ARBLOST == 0)
{
switch (SMB0CN & 0xF0) // Decode the SMBus status vector
{
// Slave Receiver: Start+Address received
case SMB_SRADD:
STA = 0; // Clear STA bit
sent_byte_counter = 1; // Reinitialize the data counters
rec_byte_counter = 1;
if((SMB0DAT&0xFE) == (SLAVE_ADDR&0xFE)) // Decode address
{ // If the received address matches,
ACK = 1; // ACK the received slave address
if((SMB0DAT&0x01) == READ) // If the transfer is a master READ,
{
// Prepare outgoing byte
SMB0DAT = SMB_DATA_OUT[sent_byte_counter-1];
sent_byte_counter++;
if(!(SMB0DAT&0x80)) // If data byte MSB = '0',
{
STO = 1; // Set STO to force outgoing MSB => '0'
RXTX = 1; // Set receiver-to-transmitter flag
}
}
}
else // If received slave address does not
{ // match,
ACK = 0; // NACK received address
}
break;
// Slave Receiver: Data received
case SMB_SRDB:
if (rec_byte_counter < NUM_BYTES_WR)
{
// Store incoming data
SMB_DATA_IN[rec_byte_counter-1] = SMB0DAT;
rec_byte_counter++;
ACK = 1; // ACK received data
}
else
{
// Store incoming data
SMB_DATA_IN[rec_byte_counter-1] = SMB0DAT;
DATA_READY = 1; // Indicate new data fully received
ACK = 1; // ACK received data
}
break;
// Slave Receiver: Stop received while either a Slave Receiver or
// Slave Transmitter
case SMB_SRSTO:
STO = 0; // STO must be cleared by software when
// a STOP is detected as a slave
break;
// Slave Transmitter: Data byte transmitted
case SMB_STDB:
if (ACK == 1) // If Master ACK's, send the next byte
{
if (sent_byte_counter <= NUM_BYTES_RD)
{
// Prepare next outgoing byte
SMB0DAT = SMB_DATA_OUT[sent_byte_counter-1];
sent_byte_counter++;
}
} // Otherwise, do nothing
break;
// Slave Transmitter: Arbitration lost, Stop detected
//
// This state will only be entered on a bus error condition.
// In normal operation, the slave is no longer sending data or has
// data pending when a STOP is received from the master, so the TXMODE
// bit is cleared and the slave goes to the SRSTO state.
case SMB_STSTO:
STO = 0; // STO must be cleared by software when
// a STOP is detected as a slave
break;
// Default: all other cases undefined
default:
SMB0CF &= ~0x80; // Reset communication
SMB0CF |= 0x80;
STA = 0;
STO = 0;
ACK = 0;
break;
}
}
// ARBLOST = 1, Abort failed transfer
else
{
STA = 0;
STO = 0;
ACK = 0;
}
SI = 0; // Clear SMBus interrupt flag
if (RXTX) // If RXTX flag is set, the last state
{ // was a receiver-to-transmitter
// transition;
while(!TXMODE); // Wait for slave to enter transmit
// mode,
STO = 0; // Clear the STO bit and the RXTX flag
RXTX = 0;
}
}
//-----------------------------------------------------------------------------
// Timer2 Interrupt Service Routine (ISR)
//-----------------------------------------------------------------------------
//
// A Timer2 interrupt indicates an SMBus SCL low timeout.
// The SMBus is disabled and re-enabled here
//
void Timer2_ISR (void) interrupt 5
{
SMB0CF &= ~0x80; // Disable SMBus
SMB0CF |= 0x80; // Re-enable SMBus
TF2H = 0; // Clear Timer2 interrupt-pending flag
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -