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

📄 sysmpc834xi2c.c

📁 Freescale mpc834x 的VxWorks平台的BSP源代码。
💻 C
📖 第 1 页 / 共 2 页
字号:
* i2cProgramSPD - Demonstration function to program an SPD EEPROM** This routine programs an SPD EEPROM with the contents of* defaultSPD.  This program is enabled by defining DEBUG_I2C.** RETURNS: OK/ERROR** ERRNO*/int i2cProgramEeprom    (    int unit,    int deviceAddress   /* address of I2C device - 0xa0, 0xa2 */    )    {    int index = 0;    char verifyData[REAL_SPD_SIZE];    for ( index=0;index<REAL_SPD_SIZE;index++ )        defaultSPD[index] = index ;    i2cWrite (unit,               deviceAddress,               I2C_DEVICE_TYPE_EEPROM_AT24C256,               0,               REAL_SPD_SIZE,               defaultSPD);    if ( i2cRead (unit,                   deviceAddress,                   I2C_DEVICE_TYPE_EEPROM_AT24C256,                   0,                   REAL_SPD_SIZE,                   verifyData) == OK )        {        for ( index=0;index<REAL_SPD_SIZE;index++ )            {            if ( verifyData[index] != defaultSPD[index] )                {                logMsg("SPD programming Failed: %d: %x != %x\r\n",                        index, verifyData[index], defaultSPD[index],0,0,0);                return(ERROR);                }            }        }    return(OK);    }/******************************************************************************** i2cShowEeprom - Dump i2c device** This routine prints the number "size" bytes of EEPROM addresses at* "deviceAddress".  Valid addresses for the prpmc600 are 0xa0 * for SPD SDRAM banks and VPD_BRD_EEPROM_ADRS for the board's VPD* EEPROM. ** RETURNS: OK/ERROR** ERRNO*/int i2cShowEeprom    (    int unit,    int deviceAddress,  /* address of I2C device - 0xa0, 0xa2 */    int size        /* number of bytes to read/display */    )    {    int i;    char i2cBuff[REAL_SPD_SIZE] = {0};    if ( size > 32768 )        return(ERROR);    memset(i2cBuff,0,REAL_SPD_SIZE);    if ( i2cRead (unit,                   deviceAddress,                   I2C_DEVICE_TYPE_EEPROM_AT24C256,                   0,                   size,                   i2cBuff) == ERROR )        {        logMsg("Driver returned error\n",0,0,0,0,0,0);        return(ERROR);        }    for ( i=0;i<size;i++ )        printf("%x ", i2cBuff[i]);    printf("\r\n");    return(0);    }/******************************************************************************** i2cFill - block fill a i2c device** This routine writes the number "size" bytes of EEPROM addresses at* "deviceAddress" with the value "value".  Valid addresses for the prpmc600* are 0xa0 for SPD SDRAM banks and VPD_BRD_EEPROM_ADRS for the board's VPD * EEPROM.  This will wipe out the existing data at the address. ** RETURNS: OK/ERROR** ERRNO*/int i2cFillEeprom    (    int unit,    int deviceAddress,  /* address of I2C device - 0xa0, 0xa2 */    int size,       /* number of bytes to write */    int value       /* value to fill */    )    {    int ii;    char ptr[1];    if ( size > REAL_SPD_SIZE )        return(ERROR);    for ( ii=0;ii<size;ii++ )        {        ptr[0] = value;        i2cWrite (unit,                   deviceAddress,                   I2C_DEVICE_TYPE_EEPROM_AT24C256,                   ii,                   1,                   ptr);        }    i2cShowEeprom (unit, deviceAddress, size);    return(OK);    }#define I2C_TSENSOR_SEEKER_ADRS 0/******************************************************************************** i2cShowTemp - read temperature sensor** This routine reads the temperature sensor on the I2C bus.** RETURNS: ERROR; or 0** ERRNO*/int i2cShowTemp    (    int unit,    int deviceAddress   /* address of I2C device - 0xa0, 0xa2 */    )    {    int i;    char i2cBuff[REAL_SPD_SIZE] = {0};    if ( deviceAddress != I2C_TSENSOR_SEEKER_ADRS )        {        printf("Bad I2C address 0x%x\n", deviceAddress);        return -1 ;        }    memset(i2cBuff,0,REAL_SPD_SIZE);    i2cBuff[0] = 0 ;    i2cWrite (unit,               deviceAddress,               I2C_DEVICE_TYPE_TEMP_SENSOR_AD7417,               1,               1,               i2cBuff) ;    taskDelay(1);    if ( i2cRead (unit,                   deviceAddress,                   I2C_DEVICE_TYPE_TEMP_SENSOR_AD7417,                   0,                   2,                   i2cBuff) == ERROR )        {        logMsg("Driver returned error\n",0,0,0,0,0,0);        return(ERROR);        }    for ( i=0;i<2;i++ )        printf("%x ", i2cBuff[i]);    printf("\r\n");    return(0);    }/******************************************************************************** i2cShowAdc - read the ADC** This routine reads the ADC sensor on the I2C bus.** RETURNS: ERROR; or 0** ERRNO*/int i2cShowAdc    (    int unit,    int deviceAddress   /* address of I2C device - 0x28 */    )    {    int i;    char i2cBuff[REAL_SPD_SIZE] = {0};    if ( deviceAddress != I2C_TSENSOR_SEEKER_ADRS )        {        printf("Bad I2C address 0x%x\n", deviceAddress);        return -1 ;        }    memset(i2cBuff,0,REAL_SPD_SIZE);    i2cBuff[0] = 0x60 ;    i2cWrite (unit,               deviceAddress,               I2C_DEVICE_TYPE_TEMP_SENSOR_AD7417,               1,               1,               i2cBuff) ;    i2cRead (unit,              deviceAddress,              I2C_DEVICE_TYPE_TEMP_SENSOR_AD7417,              1,              1,              i2cBuff) ;    printf("Read 0x%x from config register.\n", i2cBuff[0]) ;    taskDelay(1);    if ( i2cRead (unit,                   deviceAddress,                   I2C_DEVICE_TYPE_TEMP_SENSOR_AD7417,                   4,                   2,                   i2cBuff) == ERROR )        {        logMsg("Driver returned error\n",0,0,0,0,0,0);        return(ERROR);        }    for ( i=0;i<2;i++ )        printf("%x ", i2cBuff[i]);    printf("\r\n");    i2cBuff[0] = 0x80 ;    i2cWrite (unit,               deviceAddress,               I2C_DEVICE_TYPE_TEMP_SENSOR_AD7417,               1,               1,               i2cBuff) ;    i2cRead (unit,              deviceAddress,              I2C_DEVICE_TYPE_TEMP_SENSOR_AD7417,              1,              1,              i2cBuff) ;    printf("Read 0x%x from config register.\n", i2cBuff[0]) ;    taskDelay(1);    if ( i2cRead (unit,                   deviceAddress,                   I2C_DEVICE_TYPE_TEMP_SENSOR_AD7417,                   4,                   2,                   i2cBuff) == ERROR )        {        logMsg("Driver returned error\n",0,0,0,0,0,0);        return(ERROR);        }    for ( i=0;i<2;i++ )        printf("%x ", i2cBuff[i]);    printf("\r\n");    return(0);    }/******************************************************************************** i2cTempRegRead - read the temperature sensor register** This routine reads the temperature sensor register on the I2C.** RETURNS: OK, or ERROR** ERRNO*/int i2cTempRegRead    (    int unit,    int deviceAddress,    int reg,    int size,    UINT16 *data    )    {    if ( i2cRead (unit,                    deviceAddress,                   I2C_DEVICE_TYPE_TEMP_SENSOR_AD7417,                   reg,                   size,                  (char*)data) == ERROR )        {        logMsg("Driver returned error\n",0,0,0,0,0,0);        return(ERROR);        }    if ( size == 1 )        *data = *data>>8 ;    return(OK);    }/******************************************************************************** i2cTempRegWrite - write the temperature sensor register** This routine writes the temperature sensor register on the I2C.** RETURNS: OK, or ERROR** ERRNO*/int i2cTempRegWrite    (    int unit,    int deviceAddress,    int reg,    int size,    UINT16 *data    )    {    if ( size == 1 )        *data <<= 8 ;    if ( i2cWrite (unit, deviceAddress,                    I2C_DEVICE_TYPE_TEMP_SENSOR_AD7417,                    reg,                    size,                   (char*)data) == ERROR )        {        logMsg("Driver returned error\n",0,0,0,0,0,0);        return(ERROR);        }    return(OK);    }/******************************************************************************** i2cIOPortRegWrite - write the IO Port register** This routine writes the IO Port register on the I2C.** RETURNS: OK, or ERROR** ERRNO** NOTES:* Reg 0..7 * Actual read and writes require accessing a pair of registers.* command byte 0 : input port 0,1 * command byte 2 : output port 0,1 * command byte 4 : polarity inversion 0,1* command byte 6 : config port 0,1 * config port 0 = output, 1 = input, powerup all 1 * so if config port 1.4 = 0 then output port 1.4 is applicable */int i2cIOPortRegWrite     (    int unit,    int deviceAddress,    int reg,    UINT16 data    )    {    /* write register pair */    if ( i2cWrite (unit,                    deviceAddress,                    I2C_DEVICE_TYPE_IOPORT_PCA9555,                   reg,                    2,                    (char*)&data) == ERROR )        {        #if 0        logMsg("Driver returned error\n",0,0,0,0,0,0);        #endif        return(ERROR);        }    return OK ;    }/******************************************************************************** i2cIOPortRegRead - read the IO Port register** This routine reads the IO Port register on the I2C.** RETURNS: OK, or ERROR** ERRNO*/int i2cIOPortRegRead    (    int unit,    int deviceAddress,    int reg,    UINT16 *data    )    {    /* read register pair */    if ( i2cRead (unit,                    deviceAddress,                   I2C_DEVICE_TYPE_IOPORT_PCA9555,                  reg,                   2,  /* 2 bytes */                 (char*)&data) == ERROR )        {        logMsg("Driver returned error\n",0,0,0,0,0,0);        return(ERROR);        }    return OK ;    }/* Setup PIB board for PCI on PMC1 *//* Need 32bit, 66MHz card on PMC1 */void initPibBoard (void)    {    /* Input ports 0, 1 */    /* Output ports 2, 3 */    /* Polarity ports 4, 5 */    /* Configuration ports 6, 7 */    /* Set config reg bits for IO port direction. */    i2cIOPortRegWrite(0, 0x23, 6, 0x0000) ;    i2cIOPortRegWrite(0, 0x26, 6, 0x0034) ;    i2cIOPortRegWrite(0, 0x27, 6, 0x0000) ;    /* Set desired output port values */    i2cIOPortRegWrite(0, 0x23, 2, 0xffff);    #if 1    /* 66MHz */    i2cIOPortRegWrite(0, 0x26, 2, 0xf3ff);    #else    /* 33MHz */    i2cIOPortRegWrite(0, 0x26, 2, 0xf3bf);    #endif    i2cIOPortRegWrite(0, 0x27, 2, 0xffef);    }void testI2c (void)    {    UINT16 data ;    /* Try I2C1 */    logMsg("Adrs 0x23...\n",1,2,3,4,5,6);    i2cIOPortRegWrite(0, 0x23, 6, 0x55aa);    i2cIOPortRegRead(0, 0x23, 6, &data);    logMsg("config reg data : 0x%x.\n", data,2,3,4,5,6);    logMsg("Adrs 0x23...\n",1,2,3,4,5,6);    i2cIOPortRegWrite(0, 0x23, 6, 0xcafe);    i2cIOPortRegRead(0, 0x23, 6, &data);    logMsg("config reg data : 0x%x.\n", data,2,3,4,5,6);    logMsg("Adrs 0x26...\n",1,2,3,4,5,6);    i2cIOPortRegWrite(0, 0x26, 6, 0x3344);    i2cIOPortRegRead(0, 0x26, 6, &data);    logMsg("config reg data : 0x%x.\n", data,2,3,4,5,6);    logMsg("Adrs 0x27...\n",1,2,3,4,5,6);    i2cIOPortRegWrite(0, 0x27, 6, 0x0000);    i2cIOPortRegRead(0, 0x27, 6, &data);    logMsg("config reg data : 0x%x.\n", data,2,3,4,5,6);#if 0    /* Try I2C2 */    i2cIOPortRegWrite(1, 0x23, 6, 0xaa55);    i2cIOPortRegRead(1, 0x23, 6, &data);    logMsg("config reg data : 0x%x.\n", data,2,3,4,5,6);#endif    }#endif /* DEBUG_I2C */

⌨️ 快捷键说明

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