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

#include "../mscbemb.h"
#include "SMBus_handler.h"

Functions

void SMBus_EnableACKPoll (void)
void SMBus_SetSlaveAddr (unsigned char slaveAddr)
void SMBus_SetTXBuffer (const 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 (unsigned char force)
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   ) 

00074                        {
00075   pSMB_DATA_IN = 0;
00076   SMB_DATA_IN_LEN = 0;
00077   SMB_DATA_OUT_LEN = 0;
00078   SMB_BUSY = 0;
00079   SMB_ACKPOLL = SMB_DISABLE_ACKPOLL;
00080 }

void SMBus_EnableACKPoll ( void   ) 

00037                                {
00038   SMB_ACKPOLL = SMB_ENABLE_ACKPOLL;
00039 }

void SMBus_Init ( unsigned char  force  ) 

Initializing the SMBus

00107                                      {
00108   static char init = 0;
00109 
00110   if(!init || force) {
00111     init = 1;
00112 
00113     // Configuring the Timer3 Registers
00114     SFRPAGE = TMR3_PAGE;
00115     TMR3CN = 0x00; // Turn Clock off
00116     TMR3CF = 0x00; // SYSCLK / 12
00117 
00118     RCAP3L = 0x00; /* TIMER 3 CAPTURE/RELOAD LOW BYTE */
00119     RCAP3H = 0x00; /* TIMER 3 CAPTURE/RELOAD HIGH BYTE */
00120 
00121     TMR3L = 0x00;  //Timer3 Low Bytes
00122     TMR3H = 0x00;  //Timer3 High Bytes
00123 
00124     TMR3CN = 0x04; // Enable Timer3
00125 
00126     SFRPAGE = SMB0_PAGE;
00127     SMB0CN = 0x43;
00128     EIP1 |= 0x02; // SMB High priority 
00129     EIE1 |= 0x02; // Enable SMBus interrupts
00130     EIE2 |= 0x01; // Enable Timer3 interrupts
00131     EA = 1;   // Enable Global interrupts     
00132     SMBus_Clear();    
00133 
00134   }
00135 }

void SMBus_ISR ( void   ) 

00139                                  {
00140         bit FAIL = 0;
00141         static unsigned char data_in;
00142         static unsigned char data_out;
00143 
00144         switch(SMB0STA) {
00145         case SMB_STATE_START:
00146                 data_in = 0;
00147                 data_out = 0;
00148         case SMB_STATE_REP_START:
00149                 SMB0DAT = (SMB_TARGET << 1) | SMB_RW;
00150                 STA = 0;                                                // clear START bit
00151                 break;
00152 
00153         case SMB_STATE_MT_SLAVE_ACK:
00154                 if(data_out < SMB_DATA_OUT_LEN) {
00155                         SMB0DAT = SMB_DATA_OUT[data_out++];
00156                 } else {
00157                         FAIL = 1;
00158                 }
00159                 break;
00160 
00161         case SMB_STATE_MT_SLAVE_NACK:
00162                 if(SMB_ACKPOLL) {
00163                         STO = 1;
00164                         STA = 1;
00165                 } else {
00166                         FAIL = 1;
00167                 }
00168                 break;
00169 
00170         case SMB_STATE_MT_DATA_ACK:
00171                 if(data_out < SMB_DATA_OUT_LEN) {
00172                         SMB0DAT = SMB_DATA_OUT[data_out++];
00173                 } else if (data_in < SMB_DATA_IN_LEN) {
00174                         SMB_RW = SMB_READ;
00175                         STO = 0;
00176                         STA = 1;
00177                 } else {
00178       if(SMB_BUSY) {
00179                         STO = 1;
00180                         SMB_BUSY = 0;
00181             }
00182         }
00183                 break;
00184 
00185         case SMB_STATE_MT_DATA_NACK:
00186                 // This is the place to add DATA retry
00187                 STO = 1;
00188                 break;
00189 
00190         case SMB_STATE_MR_SLAVE_ACK:
00191                 if(SMB_DATA_IN_LEN == 1) {
00192                         // only expecting one byte, so NACK reply to it to end transfer
00193                         AA = 0;
00194                 } else {
00195                         AA = 1;
00196                 }
00197                 break;
00198 
00199         case SMB_STATE_MR_SLAVE_NACK:
00200                 if(SMB_ACKPOLL) {
00201                         STO = 1;
00202                         STA = 1;
00203                 } else {
00204                         FAIL = 1;
00205                 }
00206                 break;
00207 
00208         case SMB_STATE_MR_DATA_ACK:
00209                 if(data_in == (SMB_DATA_IN_LEN - 1)) {
00210                         // Last Byte, send NACK
00211                         AA = 0;
00212                 } else {
00213                         AA = 1;
00214                 }
00215 
00216                 if(data_in < SMB_DATA_IN_LEN) {
00217                         *(pSMB_DATA_IN+data_in) = SMB0DAT;
00218                         data_in++;
00219                 } else {
00220                         FAIL = 1;
00221                 }
00222                 break;
00223 
00224         case SMB_STATE_MR_DATA_NACK:
00225                 // Save last byte received
00226                 *(pSMB_DATA_IN+data_in) = SMB0DAT;
00227                 data_in++;
00228                 // Stop Communication
00229                 STO = 1;
00230                 SMB_BUSY = 0;
00231                 break;
00232 
00233         default:
00234                 FAIL = 1;
00235                 break;
00236 
00237         } // switch()
00238 
00239         if(FAIL) {
00240                 SMB0CN &= ~0x40;        // Disable SMBus
00241                 SMB0CN |= 0x40; // Re-enable SMBus
00242                 STA = 0;
00243                 STO = 0;
00244                 AA = 0;
00245 
00246                 SMB_BUSY = 0;
00247                 FAIL = 0;
00248         }
00249 
00250         SI = 0;
00251 }

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

00061                                                                     {
00062   pSMB_DATA_IN = pData; 
00063   SMB_DATA_IN_LEN = dataLen;
00064 }

void SMBus_SetSlaveAddr ( unsigned char  slaveAddr  ) 

00043                                                  {
00044   SMB_TARGET = slaveAddr;
00045 }

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

00049                                                                           {
00050   unsigned char i;
00051 
00052   for(i = 0; (i < dataLen) && (i < SMB_MAX_BUFF_SIZE); ++i) {
00053     SMB_DATA_OUT[i] = pData[i];
00054   }
00055 
00056   SMB_DATA_OUT_LEN = i;  
00057 }

void SMBus_Start ( void   ) 

00084                        {
00085   if(SMB_DATA_OUT_LEN > 0) {
00086     SMB_RW = SMB_WRITE;
00087   } else if(SMB_DATA_IN_LEN > 0) {
00088     SMB_RW = SMB_READ;
00089   } else {
00090     return;
00091   }
00092 
00093   SMB_BUSY = 1;
00094   SFRPAGE = SMB0_PAGE;
00095   STA = 1;
00096 
00097   SMBus_Wait();
00098   SMBus_Clear();
00099 }

void SMBus_Wait ( void   ) 

00068                       {
00069   while(SMB_BUSY);
00070 }

void Timer3_ISR ( void   ) 

00255                                    {
00256   SFRPAGE = SMB0_PAGE;
00257   SMB0CN &= ~0x40; // Disable SMBus
00258   SMB0CN |= 0x40; // Re-enable SMBus
00259 
00260   SFRPAGE = TMR3_PAGE;
00261   TMR3CN &= ~0x80; // Clear Timer3 interrupt-pending flag
00262   SMBus_Clear();
00263 } 


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