/home/daqweb/fgddaq/c8051/Protocols/SMBus310_handler.c File Reference

#include "../mscbemb.h"
#include "SMBus310_handler.h"

Functions

void SMBus_EnableACKPoll (void)
void SMBus_SetSlaveAddr (unsigned char slaveAddr)
void SMBus_SetTXBuffer (unsigned char *pData, unsigned char dataLen)
void SMBus_SetRXBuffer (unsigned char *pData, unsigned char dataLen)
void SMBus_Wait (void)
void SMBus_Clear (void)
void SMBus_Start (void)
void SMBus_Init (void)
void SMBus_ISR (void)
void Timer3_ISR (void)

Variables

static unsigned char xdata SMB_DATA_OUT [SMB_MAX_BUFF_SIZE]
static unsigned char xdata SMB_DATA_OUT_LEN
static unsigned char * pSMB_DATA_IN
static unsigned char xdata SMB_DATA_IN_LEN
static unsigned char xdata SMB_TARGET
static bit SMB_BUSY
static bit SMB_RW
static bit SMB_ACKPOLL

Function Documentation

void SMBus_Clear ( void   ) 

00066                        {
00067   pSMB_DATA_IN = 0;
00068   SMB_DATA_IN_LEN = 0;
00069   SMB_DATA_OUT_LEN = 0;
00070   SMB_BUSY = 0;
00071   SMB_ACKPOLL = SMB_DISABLE_ACKPOLL;
00072 }

void SMBus_EnableACKPoll ( void   ) 

00029                                {
00030   SMB_ACKPOLL = SMB_ENABLE_ACKPOLL;
00031 }

void SMBus_Init ( void   ) 

Initializing the SMBus

00097                       {
00098   static char init = 0;
00099 
00100   if(!init) {
00101     init = 1;
00102 
00103     // Configuring the Timer3 Registers
00104     TMR3CN = 0x00;      // Turn Clock off and SYSCLK / 12
00105 
00106     TMR3RLL = 0x00; /* TIMER 3 RELOAD LOW BYTE */
00107     TMR3RLH = 0x00; /* TIMER 3 RELOAD HIGH BYTE */
00108     TMR3L = 0x00;   //Timer3 Low Bytes
00109     TMR3H = 0x00;   //Timer3 High Bytes
00110     TMR3CN = 0x04;  // Enable Timer3
00111 
00112     SMB0CF = 0x5D;   // SMbus Setup Hold Extension/Timeout Detection/Free Timeout Detection Enable
00113     // Timer1 Overflow is set as the clock source
00114     SMB0CF |= 0x80;  // Enable SMBus after all setting done.
00115 
00116 
00117     EIE1 |= 0x81;          // Enable SMBus interrupts/Enable Timer3 interrupts
00118     EA = 1;                      // Enable Global interrupts 
00119     
00120     SMBus_Clear();    
00121   }
00122 }

void SMBus_ISR ( void   ) 

00126                                 {
00127   bit FAIL;                                              // Used by the ISR to flag failed
00128   // transfers
00129   static unsigned char data_in;
00130   static unsigned char data_out;        
00131 
00132   switch (SMB0CN & 0xF0)            // Status vector
00133   {
00134     // Master Transmitter/Receiver: START condition transmitted.
00135   case SMB_MTSTA:
00136     SMB0DAT = (SMB_TARGET << 1) | SMB_RW; // Load address of the target slave and R/W bit
00137     STA = 0;                                      // Manually clear START bit
00138     data_in = 0;
00139     data_out = 0;
00140     break;
00141 
00142     // Master Transmitter: Data byte transmitted
00143   case SMB_MTDB:
00144     if (ACK) {                               // Slave ACK?
00145       if (data_out < SMB_DATA_OUT_LEN) {                                                                                 
00146         if (SMB_RW==SMB_WRITE) {// If this transfer is a WRITE,
00147           SMB0DAT = SMB_DATA_OUT[data_out++]; // send data byte
00148         } else {
00149                         // If this transfer is a READ, proceed with transfer
00150                         // without writing to SMB0DAT (switch to receive mode)
00151         }               
00152       } else if(data_in < SMB_DATA_IN_LEN) {
00153         SMB_RW = SMB_READ;
00154         STO = 0;
00155         STA = 1;      
00156       } else {
00157         STO = 1;
00158         SMBus_Clear();
00159       }
00160     } else if (SMB_ACKPOLL) {                                                     
00161       STO = 1;
00162       STA = 1;  
00163     } else {    // If slave NACK, and we are not expecting anything to read
00164       FAIL = 1; // Indicate failed transfer and handle at end of ISR                                                                                      
00165     }
00166     break;
00167 
00168     // Master Receiver: byte received
00169   case SMB_MRDB:
00170     *(pSMB_DATA_IN+data_in) = SMB0DAT;
00171     data_in++;                                                                                                                                                                
00172     if ( data_in >= SMB_DATA_IN_LEN) {                                                            
00173       ACK = 0;                 // Send NACK to indicate last byte
00174       STO = 1;               // Send STOP to terminate transfer
00175       SMBus_Clear();
00176     } else {
00177       ACK = 1;
00178       STO = 0;
00179     }
00180     break;
00181 
00182   default:
00183     FAIL = 1; // Indicate failed transfer and handle at end of ISR                                                                                       
00184     break;
00185   } // end switch
00186         
00187   // If the transfer failed
00188   if (FAIL) {                                                    
00189     SMB0CN &= ~0x40; // Reset communication
00190     SMB0CN |= 0x40;
00191     SMBus_Clear();
00192     STA = 0;
00193     STO = 0;
00194     ACK = 0;
00195     FAIL = 0;
00196   }
00197 
00198   SI=0; // clear interrupt flag
00199 }

void SMBus_SetRXBuffer ( unsigned char *  pData,
unsigned char  dataLen 
)

00053                                                                     {
00054   pSMB_DATA_IN = pData; 
00055   SMB_DATA_IN_LEN = dataLen;
00056 }

void SMBus_SetSlaveAddr ( unsigned char  slaveAddr  ) 

00035                                                  {
00036   SMB_TARGET = slaveAddr;
00037 }

void SMBus_SetTXBuffer ( unsigned char *  pData,
unsigned char  dataLen 
)

00041                                                                     {
00042   unsigned char i;
00043 
00044   for(i = 0; (i < dataLen) && (i < SMB_MAX_BUFF_SIZE); ++i) {
00045     SMB_DATA_OUT[i] = pData[i];
00046   }
00047   
00048   SMB_DATA_OUT_LEN = i;  
00049 }

void SMBus_Start ( void   ) 

00076                        {
00077   if(SMB_DATA_OUT_LEN > 0) {
00078     SMB_RW = SMB_WRITE;
00079   } else if(SMB_DATA_IN_LEN > 0) {
00080     SMB_RW = SMB_READ;
00081   } else {
00082     return;
00083   }
00084   
00085   SMB_BUSY = 1;
00086   STA = 1;
00087 
00088   SMBus_Wait();
00089   SMBus_Clear();
00090 }

void SMBus_Wait ( void   ) 

00060                       {
00061   while(SMB_BUSY);
00062 }

void Timer3_ISR ( void   ) 

00203                                    {
00204   SMB0CN &= ~0x40;      // Disable SMBus
00205   SMB0CN |= 0x40;       // Re-enable SMBus
00206   TMR3CN &= ~0x80;      // Clear Timer3 interrupt-pending flag
00207   SMBus_Clear();
00208 }       


Variable Documentation

unsigned char* pSMB_DATA_IN [static]

bit SMB_ACKPOLL [static]

bit SMB_BUSY [static]

unsigned char xdata SMB_DATA_IN_LEN [static]

unsigned char xdata SMB_DATA_OUT[SMB_MAX_BUFF_SIZE] [static]

unsigned char xdata SMB_DATA_OUT_LEN [static]

bit SMB_RW [static]

unsigned char xdata SMB_TARGET [static]


Generated on 10 Jun 2013 for FGDC8051 by  doxygen 1.4.7