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

📄 i2c_test.c

📁 dsp在音频处理中的运用
💻 C
📖 第 1 页 / 共 2 页
字号:
        if (CSL_i2cGetHwStatus(hI2c, CSL_I2C_QUERY_BUS_BUSY, &response) != CSL_SOK){
            printf("Error reading bus busy status\n");
            return 0;
        }
    }while(response == CSL_I2C_BUS_BUSY);

    /* Configure Options for Receiving - Master Reciever mode */
   /* I2C_CON = 0
            | I2C_CON_I2C_EN    // I2C Enable
            | I2C_CON_MST       // Master Mode
            | I2C_CON_RX        // Receive Mode
            ;*/
	 /*cmden=CMD(ENABLE)|CMD(MODE_MASTER)|CMD(DIR_RECEIVE);*/
    /*CSL_i2cHwControl(hI2c,CSL_I2C_CMD_ENABLE,NULL);
     CSL_i2cHwControl(hI2c,CSL_I2C_CMD_MODE_MASTER,NULL);
     CSL_i2cHwControl(hI2c,CSL_I2C_CMD_DIR_RECEIVE,NULL);
    /*CSL_i2cHwControl(hI2c,cmden,NULL);*/
    /*  Set mode:
     *  OSK5912_I2C_MODE_SADP // (S)tart..(A)ddr..(D)ata..(n)..sto(P)
     *  OSK5912_I2C_MODE_SADD // (S)tart..(A)ddr..(D)ata..(n)..(D)ata */
    /*I2C_CON |= OSK5912_I2C_MODE_SADP;*/
	/*CSL_i2cHwControl(hI2c,CSL_I2C_CMD_START,NULL);
	CSL_i2cHwControl(hI2c,CSL_I2C_CMD_STOP,NULL);


    /* Poll until complete */
    /*for ( timecount = 0 ; timecount < timeout ; timecount++ )
    {
        /* Read I2C Status register */
        /*status = I2C_STAT;*/

        /* Check for NACK */
		/*CSL_i2cGetHwStatus(hI2c, CSL_I2C_QUERY_NACK, &response);
        if ( response != 0x0001 )
        {
            /* Check for ARDY */
			/*CSL_i2cGetHwStatus(hI2c, CSL_I2C_QUERY_ACS_RDY, &response);
            if ( response != CSL_I2C_ACS_READY )
            {
                /* Check for RRDY */
                /*CSL_i2cGetHwStatus(hI2c, CSL_I2C_QUERY_RX_RDY, &response);
                if ( response == CSL_I2C_RX_READY )
                {
                    /* Read Data */
                    /*if ( length == 1 )
                    {
                        /**pdata = I2C_DATA & 0xFF;*/
					  /*status=CSL_i2cRead(hI2c,pdata);
						*pdata=*pdata&0xFF;
                        length--;
                    }
                    else
                    {
                        /**pdata++ = I2C_DATA;*/
						/*CSL_i2cRead(hI2c,pdata);
						pdata++;
                        length -= 2;
                    }

                    /* Clear Receive Ready field */
                   /* I2C_STAT &= I2C_STAT_RRDY;*/
				   /*do{cmdclear=CSL_I2C_CLEAR_RRDY;
				   CSL_i2cHwControl(hI2c,CSL_I2C_CMD_CLEAR_STATUS,&cmdclear);
				   CSL_i2cGetHwStatus(hI2c, CSL_I2C_QUERY_RX_RDY, &response);}
				   while(response==CSL_I2C_RX_READY);

                }
            }

            /* Check for Errors - refresh status register */
           /* status = I2C_STAT;*/

            /* Check for Arbitration Lost */
            /*CSL_i2cGetHwStatus(hI2c,CSL_I2C_QUERY_AL, &response);
            if ( response == CSL_I2C_ARBITRATION_LOST )
            {   CSL_i2cClose(hI2c);
                /*I2C_STAT &= I2C_STAT_AL;*/
               // return 1001;
            //}

            /* Check for NACK */
			/*CSL_i2cGetHwStatus(hI2c,CSL_I2C_QUERY_NACK, &response);
            if ( response == 0x0001 )
            {   CSL_i2cClose(hI2c);
                /*I2C_STAT &= I2C_STAT_NACK;*/
                //return 1002;
           // }

            /* Check for Register Access Ready - Good Condition */
			/*CSL_i2cGetHwStatus(hI2c,CSL_I2C_QUERY_SBD, &response);*/
			/*{return 0;}*/
			/*CSL_i2cGetHwStatus(hI2c,CSL_I2C_QUERY_ACS_RDY, &response);//why response!=1?  
            if ( response == CSL_I2C_ACS_READY )
            {   do{cmdclear=CSL_I2C_CLEAR_ARDY;
			       CSL_i2cHwControl(hI2c,CSL_I2C_CMD_CLEAR_STATUS,&cmdclear);
				   CSL_i2cGetHwStatus(hI2c,CSL_I2C_QUERY_ACS_RDY, &response);}
				   while(response==CSL_I2C_ACS_READY);
                 CSL_i2cClose(hI2c);
                /*I2C_STAT &= I2C_STAT_ARDY;*/
                //return 0;
            //}
       // }
   // }
    /*CSL_i2cClose(hI2c);
    /* I2C Timeout */
    //return 2002;
//}


Uint16 OSK5912_I2C_read( Uint16 slave_addr, Uint8* data, Uint16 length )
{
    Uint16 *pdata = ( Uint16 * )data;
    Uint16 status;
    Int16 i, timecount, timeout = 0x1000;

    /* Set slave address */
    I2C_SA = slave_addr;

    /* Set data count */
    I2C_CNT = length;

    /* Poll BB field on I2C_STAT - Wait for bus to become free */
    for ( i = 0 ; I2C_STAT & I2C_STAT_BB ; i++ )
        if ( i > timeout )
            return 2001;

    /* Configure Options for Receiving - Master Reciever mode */
    I2C_CON = 0
            | I2C_CON_I2C_EN    // I2C Enable
            | I2C_CON_MST       // Master Mode
            | I2C_CON_RX        // Receive Mode
            ;

    /*  Set mode:
     *  OSK5912_I2C_MODE_SADP // (S)tart..(A)ddr..(D)ata..(n)..sto(P)
     *  OSK5912_I2C_MODE_SADD // (S)tart..(A)ddr..(D)ata..(n)..(D)ata */
    I2C_CON |= OSK5912_I2C_MODE_SADP;


    /* Poll until complete */
    for ( timecount = 0 ; timecount < timeout ; timecount++ )
    {
        /* Read I2C Status register */
        status = I2C_STAT;

        /* Check for NACK */
        if ( ( status & I2C_STAT_NACK ) != I2C_STAT_NACK )
        {
            /* Check for ARDY */
            if ( ( status & I2C_STAT_ARDY ) != I2C_STAT_ARDY )
            {
                /* Check for RRDY */
                if ( ( status & I2C_STAT_RRDY ) == I2C_STAT_RRDY )
                {
                    /* Read Data */
                    if ( length == 1 )
                    {
                        *pdata = I2C_DATA & 0xFF;
                        length--;
                    }
                    else
                    {
                        *pdata++ = I2C_DATA;
                        length -= 2;
                    }

                    /* Clear Receive Ready field */
                    I2C_STAT &= I2C_STAT_RRDY;
                }
            }

            /* Check for Errors - refresh status register */
            status = I2C_STAT;

            /* Check for Arbitration Lost */
            if ( ( status  & I2C_STAT_AL ) == I2C_STAT_AL )
            {
                I2C_STAT &= I2C_STAT_AL;
                return 1001;
            }

            /* Check for NACK */
            if ( ( status & I2C_STAT_NACK ) == I2C_STAT_NACK )
            {
                I2C_STAT &= I2C_STAT_NACK;
                return 1002;
            }

            /* Check for Register Access Ready - Good Condition */
            if ( ( I2C_STAT & I2C_STAT_ARDY ) == I2C_STAT_ARDY )
            {
                I2C_STAT &= I2C_STAT_ARDY;
                return 0;
            }
        }
    }

    /* I2C Timeout */
    return 2002;
}


/* ------------------------------------------------------------------------ *
 *  OSK5912_I2C_write( Uint16 slave_addr, Uint8* data, Uint16 length )      *
 *      Write to I2C                                                        *
 * ------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------ *
 *  OSK5912_I2C_write( slave_addr, data, length )                           *
 *      Write to I2C                                                        *
 *      Returns:                                                            *
 *          0 - Pass                                                        *
 *          1001 - Fail ( Errors - Arbitration Lost )                       *
 *          1002 - Fail ( Errors - NACK )                                   *
 *          2001 - Fail ( Timeout - Bus Busy )                              *
 *          2002 - Fail ( Timeout - Write )                                 *
 * ------------------------------------------------------------------------ */
Uint16 OSK5912_I2C_write( Uint16 slave_addr, Uint8* data, Uint16 length )
{
    Uint16 *pdata = ( Uint16 * )data;
    Uint16 last_byte = ( data[length-1] & 0xFF );
    Uint16 status;
    Int16 i, timecount, timeout = 0x1000;

    /* Set slave address */
    I2C_SA = slave_addr;

    /* Set data count */
    I2C_CNT = length;

    /* Poll BB field on I2C_STAT - Wait for bus to become free */
    for ( i = 0 ; I2C_STAT & I2C_STAT_BB ; i++ )
        if ( i > timeout )
            return 2001;

    /* Configure Options for Transmitting - Master Transmit mode */
    I2C_CON = 0
            | I2C_CON_I2C_EN    // I2C Enable
            | I2C_CON_MST       // Master Mode
            | I2C_CON_TX        // Transmit Mode
            ;

    /*  Set Mode:
     *  OSK5912_I2C_MODE_SADP // (S)tart..(A)ddr..(D)ata..(n)..sto(P)
     *  OSK5912_I2C_MODE_SADD // (S)tart..(A)ddr..(D)ata..(n)..(D)ata */
    I2C_CON |= OSK5912_I2C_MODE_SADP;

    /* Poll until complete */
    for ( timecount = 0 ; timecount < timeout ; timecount++ )
    {
        /* Read I2C Status register */
        status = I2C_STAT;

        /* Check for NACK */
        if ( ( status & I2C_STAT_NACK ) != I2C_STAT_NACK )
        {
            /* Check for ARDY */
            if ( ( status & I2C_STAT_ARDY ) != I2C_STAT_ARDY )
            {
                /* Check for XRDY */
                if ( ( status & I2C_STAT_XRDY ) == I2C_STAT_XRDY )
                {
                    /* Send Data */
                    if ( length == 1 )
                    {
                        I2C_DATA = last_byte;
                        length--;
                    }
                    else
                    {
                        I2C_DATA = *pdata++;
                        length -= 2;
                    }

                    /* Clear Transmit Ready field */
                    I2C_STAT &= I2C_STAT_XRDY;
                }
            }

            /* Check for Errors - refresh status register */
            status = I2C_STAT;

            /* Check for Arbitration Lost */
            if ( ( status  & I2C_STAT_AL ) == I2C_STAT_AL )
            {
                I2C_STAT &= I2C_STAT_AL;
                return 1001;
            }

            /* Check for NACK */
            if ( ( status & I2C_STAT_NACK ) == I2C_STAT_NACK )
            {
                I2C_STAT &= I2C_STAT_NACK;
                return 1002;
            }

            /* Check for Register Access Ready - Good Condition */
            if ( ( I2C_STAT & I2C_STAT_ARDY ) == I2C_STAT_ARDY )
            {
                I2C_STAT &= I2C_STAT_ARDY;
                return 0;
            }
        }
    }

    /* I2C Timeout */
    return 2002;
}







⌨️ 快捷键说明

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