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

📄 i2c_master.c

📁 CNC.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
        case STATE_READ_NEXT:
        {
            //
            // Read the received character.
            //
            *g_pucData++ = I2CMasterDataGet(I2C_MASTER_BASE);
            g_ulCount--;

            //
            // Continue the burst read.
            //
            I2CMasterControl(I2C_MASTER_BASE,
                             I2C_MASTER_CMD_BURST_RECEIVE_CONT);

            //
            // If there are two characters left to be read, make the next
            // state be the end of burst read state.
            //
            if(g_ulCount == 2)
            {
                g_ulState = STATE_READ_FINAL;
            }

            //
            // This state is done.
            //
            break;
        }

        //
        // The state for the end of a burst read.
        //
        case STATE_READ_FINAL:
        {
            //
            // Read the received character.
            //
            *g_pucData++ = I2CMasterDataGet(I2C_MASTER_BASE);
            g_ulCount--;

            //
            // Finish the burst read.
            //
            I2CMasterControl(I2C_MASTER_BASE,
                             I2C_MASTER_CMD_BURST_RECEIVE_FINISH);

            //
            // The next state is the wait for final read state.
            //
            g_ulState = STATE_READ_WAIT;

            //
            // This state is done.
            //
            break;
        }

        //
        // This state is for the final read of a single or burst read.
        //
        case STATE_READ_WAIT:
        {
            //
            // Read the received character.
            //
            *g_pucData++  = I2CMasterDataGet(I2C_MASTER_BASE);
            g_ulCount--;

            //
            // The state machine is now idle.
            //
            g_ulState = STATE_IDLE;

            //
            // This state is done.
            //
            break;
        }
    }
}

//*****************************************************************************
//
//! Reads from the main microcontroller via I2C.
//!
//! \param pucData is a pointer to the buffer into which received data is to be
//! written.
//! \param ulCount is the number of bytes to read from the main
//! microcontroller.
//!
//! This function reads a sequence of bytes from the main microcontroller.  The
//! read occurs in the background (i.e. this function returns before the read
//! has completed); I2CIdle() can be used to determine when the read has
//! completed.
//!
//! \note Nothing is done to prevent a write from starting before a previous
//! I2C transaction has completed.
//!
//! \return None.
//
//*****************************************************************************
void
I2CRead(unsigned char *pucData, unsigned long ulCount)
{
    //
    // Save the data buffer to be read.
    //
    g_pucData = pucData;
    g_ulCount = ulCount;

    //
    // Set the next state of the interrupt state machine based on the number of
    // bytes to read.
    //
    if(ulCount == 1)
    {
        g_ulState = STATE_READ_WAIT;
    }
    else if(ulCount == 2)
    {
        g_ulState = STATE_READ_FINAL;
    }
    else
    {
        g_ulState = STATE_READ_NEXT;
    }

    //
    // Set the address of the targeted I2C slave.
    //
    I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x34, true);

    //
    // Either perform a single read if only one byte was requested, or start a
    // burst read if more than one byte was requested.
    //
    if(ulCount == 1)
    {
        I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
    }
    else
    {
        I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
    }
}

//*****************************************************************************
//
//! Writes to the main microcontroller via I2C.
//!
//! \param pucData is a pointer to the buffer from which data is read.
//! \param ulCount is the number of bytes to write to the main microcontroller.
//!
//! This function writes a sequence of bytes to the main microcontroller.  The
//! write occurs in the background (i.e. this function returns before the write
//! has completed); I2CIdle() can be used to determine when the write has
//! completed.
//!
//! \note Nothing is done to prevent a write from starting before a previous
//! I2C transaction has completed.
//!
//! \return None.
//
//*****************************************************************************
void
I2CWrite(const unsigned char *pucData, unsigned long ulCount)
{
    //
    // Save the data buffer to be written.
    //
    g_pucData = (unsigned char *)pucData;
    g_ulCount = ulCount;

    //
    // Set the next state of the interrupt state machine based on the number of
    // bytes to write.
    //
    if(ulCount == 1)
    {
        g_ulState = STATE_WRITE_FINISH;
    }
    else if(ulCount == 2)
    {
        g_ulState = STATE_WRITE_FINAL;
    }
    else
    {
        g_ulState = STATE_WRITE_NEXT;
    }

    //
    // Set the slave address and setup for a transmit operation.
    //
    I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x34, false);

    //
    // Place the address to be written in the data register.
    //
    I2CMasterDataPut(I2C_MASTER_BASE, *g_pucData++);
    g_ulCount--;

    //
    // Start the burst cycle, writing the address as the first byte.
    //
    I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);
}

//*****************************************************************************
//
//! Determines if the I2C interface is idle.
//!
//! This function determines if an I2C transfer is in progress.  This should
//! be used to ensure that the I2C interface is idle before starting a new I2C
//! transfer.
//!
//! \return Returns \b true if the I2C interface is idle and \b false
//! otherwise.
//
//*****************************************************************************
tBoolean
I2CIdle(void)
{
    //
    // Return the state of the I2C interface.
    //
    return((g_ulState == STATE_IDLE) ? true : false);
}

//*****************************************************************************
//
//! Intializes the I2C interface.
//!
//! This function initializes the I2C master, preparing it for communication
//! with the main microcontroller.
//!
//! \return None.
//
//*****************************************************************************
void
I2CInit(void)
{
    //
    // Set the initial state of the I2C state machine.
    //
    g_ulState = STATE_IDLE;

    //
    // Initialize the data buffer variables.
    //
    g_pucData = 0;
    g_ulCount = 0;

    //
    // Configure the I2C pins for proper operation.
    //
    GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIO_DIR_MODE_HW);
    GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);

    //
    // Initialize the i2C master interface.
    //
    I2CMasterInit(I2C_MASTER_BASE, false);

    //
    // Enable the I2C master interrrupt.
    //
    I2CMasterIntEnable(I2C_MASTER_BASE);
    IntEnable(INT_I2C);
}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

⌨️ 快捷键说明

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