⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eeprom_24cxx.c

📁 IT projecotr reference design.
💻 C
📖 第 1 页 / 共 2 页
字号:
            if( PASS != ( cc = I2C_MasterWriteRestartRead( portNo, devAddr, addrSize,
                            (uint08*)&subAddr, 0, readCount, upper, tktimeout,
                             &count, &count )))
            {
                return _ccTestRtn( cc, EEPROM_API );
            }
        
            if( memcmp( dest, upper, readCount ))         /* if miscompare */
            {
                return _ccTestRtn( PASS, EEPROM_FAIL );
            }
        }
        
        numBytes -= readCount;                 /* bump remaining byte count */
        offset   += readCount;                       /* bump EEPROM address */
        dest      = (char*)dest + readCount;    /* bump data buffer address */
    }
       
    return _ccTestRtn( PASS, EEPROM_PASS );
}
  
  
  
/****************************************************************************/
/* Put data.                                                                */
/****************************************************************************/

EEPROM_CC EEPROM_PutData( BOOL mirror, uint16 offset, size_t numBytes, void *data )
{
    int08   cc;                                      /* API completion code */
    uint08  twrite[PMAX+4];                  /* lower buffer, page size max */
    uint08  devAddr;                  /* device address including page bits */
    uint16  subAddr;                                  /* device sub-address */
    uint08  writeCount;                          /* number of bytes to read */
    uint32  count;                                        /* transfer count */
    
    if( !isOpen )
        return EEPROM_CLOSED;
        
    if( mirror && (( offset + numBytes ) > ( memSize >> 1 )))
        return EEPROM_ADDR;
    else if(( offset + numBytes ) > memSize )
        return EEPROM_ADDR;

    if( RTA_SUCCESS != RTA_SemReserve( semID, tktimeout ))
        return EEPROM_SEM;

                        /****************************************************/
                        /* Write leading non-page aligned bytes.            */
                        /****************************************************/

    if( 0 != ( writeCount = ( offset % pageSize )))
    {
        writeCount = pageSize - writeCount;       /* non-page aligned bytes */
       
        if( writeCount > numBytes )      /* if fewer than remaining in page */
            writeCount = numBytes;
            
        (*pMakeAddr)( offset, &devAddr, &subAddr );       /* make addresses */
        
        memcpy( twrite, &subAddr, addrSize );            /* address byte(s) */
        memcpy( twrite + addrSize, data, writeCount );              /* data */
        
        if( PASS != ( cc = I2C_MasterWrite( portNo, devAddr,
                        writeCount + addrSize, twrite, 0, tktimeout, &count )))
        {
            return _ccTestRtn( cc, EEPROM_API );
        }
        
        if( PASS != ( cc = _waitComplete()))
        {
            return _ccTestRtn( cc, EEPROM_API );
        }
       
                        /****************************************************/
                        /* Write mirror.                                    */
                        /****************************************************/

        if( mirror )
        {
            (*pMakeAddr)( offset + ( memSize >> 1 ), &devAddr, &subAddr );
        
            memcpy( twrite, &subAddr, addrSize );        /* address byte(s) */
            
            if( PASS != ( cc = I2C_MasterWrite( portNo, devAddr,
                            writeCount + addrSize, twrite, 0, tktimeout, &count )))
            {
                return _ccTestRtn( cc, EEPROM_API );
            }
            
            if( PASS != ( cc = _waitComplete()))
            {
                return _ccTestRtn( cc, EEPROM_API );
            }
        }
     
        numBytes -= writeCount;                /* bump remaining byte count */
        offset   += writeCount;                      /* bump EEPROM address */
        data      = (char*)data + writeCount;          /* bump data address */
    }

                        /****************************************************/
                        /* Write remaining page-aligned bytes.              */
                        /****************************************************/

    while( numBytes )
    {
        writeCount = ( numBytes > pageSize ) ? pageSize : numBytes;/* count */
       
        (*pMakeAddr)( offset, &devAddr, &subAddr );       /* make addresses */
        
        memcpy( twrite, &subAddr, addrSize );            /* address byte(s) */
        memcpy( twrite + addrSize, data, writeCount );              /* data */
        
        if( PASS != ( cc = I2C_MasterWrite( portNo, devAddr,
                        writeCount + addrSize, twrite, 0, tktimeout, &count )))
        {
            return _ccTestRtn( cc, EEPROM_API );
        }
        
        if( PASS != ( cc = _waitComplete()))
        {
            return _ccTestRtn( cc, EEPROM_API );
        }
       
                        /****************************************************/
                        /* Write mirror.                                    */
                        /****************************************************/

        if( mirror )
        {
            (*pMakeAddr)( offset + ( memSize >> 1 ), &devAddr, &subAddr );

            memcpy( twrite, &subAddr, addrSize );        /* address byte(s) */
            
            if( PASS != ( cc = I2C_MasterWrite( portNo, devAddr,
                            writeCount + addrSize, twrite, 0, tktimeout, &count )))
            {
                return _ccTestRtn( cc, EEPROM_API );
            }
            
            if( PASS != ( cc = _waitComplete()))
            {
                return _ccTestRtn( cc, EEPROM_API );
            }
        }
     
        numBytes -= writeCount;                /* bump remaining byte count */
        offset   += writeCount;                      /* bump EEPROM address */
        data      = (char*)data + writeCount;          /* bump data address */
    }

    return _ccTestRtn( PASS, EEPROM_PASS );
}
  
  
  
/****************************************************************************/
/* Test completion code and reset i2c configuration on error.               */
/*                                                                          */
/* This function is called when exiting any function that has reserved the  */
/* i2c semaphore. An API completion code is passed in and examined for      */
/* error. If an error is seen, the port is reset.                           */
/****************************************************************************/

static EEPROM_CC _ccTestRtn( int08 APIcc, EEPROM_CC cc )
{
    I2CINIT saveConfig;                     /* saved i2c port configuration */

    if( PASS != APIcc )                         /* if an API error occurred */
    {
        I2C_GetConfig( portNo, &saveConfig );
        I2C_Reset( portNo );
        I2C_SetConfig( portNo, &saveConfig );
    }
    
    RTA_SemRelease( semID );
    
    return cc;
}
  
  
  
/****************************************************************************/
/* Wait for completion of EEPROM write operation.                           */
/*                                                                          */
/* This function uses polling mode to wait for completion for a time at     */
/* least equal to the worst-case write cyclem. The priority of the calling  */
/* task is adjusted to be the higher of the task's current priority or the  */
/* PRIORITY_EEPROM poll-delay priority. PRIORITY_EEPROM is set so that real */
/* time tasks are not blocked but high enough so that write completion is   */
/* not delayed due to preemption.                                           */
/****************************************************************************/

#define LOOPDELAY 100                                       /* microseconds */

static int08 _waitComplete( void )
{
    uint32 taskID = RTA_TaskGetCurrentTaskID();          /* calling task ID */
    uint32 taskPri;                                /* calling task priority */
    uint32 count;                                   /* number of bytes read */
    int    n;                                                 /* loop count */
    uint08 readChar;                                    /* temp read buffer */
    int08  cc = FAIL;                                    /* completion code */
    
    RTA_TaskGetPriority( taskID, &taskPri );
    
    if( taskPri > PRIORITY_EEPROM )      /* priority lower than EEPROM poll */
        RTA_TaskSetPriority( taskID, PRIORITY_EEPROM );
    
    for( n = 0; n < EEWRITECYCLE / LOOPDELAY; n++ )
    {
        TMR_Delay( LOOPDELAY );
        
        if( PASS == ( cc = I2C_PolledMasterRead( portNo,baseAddr, 1, &readChar, 0, 
                1,  &count )))
            break;;
    }
    
    if( taskPri > PRIORITY_EEPROM )      /* priority lower than EEPROM poll */
        RTA_TaskSetPriority( taskID, taskPri );
    
    return cc;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -