zynq的PL端iic使用


本文主要講述zynq的iic使用,iic作為主站使用,作為從站的本文不適合。

Iic的接口在PL端。(iic的接口在ps端的情況下,不適合本文)

如果iic的接口在ps端,請看:https://blog.csdn.net/weixin_36590806/article/details/111485711

轉載:https://blog.csdn.net/weixin_36590806/article/details/112850810

使用軟件版本:vivado2018.3

 

先說下PL端的設置吧:

然后設置地址啥的,就不多說了。

開始zynq端的編程了:

貌似不需要初始化的,我的沒有經過初始化就可以使用的,可能是因為只當作主站的原因吧。

 

接下來就是寫函數

unsigned iic_write(unsigned char iic_base,unsigned char Address, unsigned char *BufferPtr, int ByteCount)
{
    volatile unsigned SentByteCount;
    volatile unsigned AckByteCount;
    u8 WriteBuffer[sizeof(Address) + 16];
    int Index;
    u32 CntlReg;
    char IicAddr;  
    for (Index = 0; Index < ByteCount; Index++) {
        WriteBuffer[sizeof(Address) + Index] = BufferPtr[Index];
    }   
    SentByteCount = XIic_Send(IIC_BASE_ADDRESS, IicAddr,
                  WriteBuffer, sizeof(Address) + ByteCount,
                  XIIC_STOP);   
    return SentByteCount - sizeof(Address);
}

說明下函數的內容:

iic_base:iic的地址,這個可以看原理圖,就知道iic的地址

Address:准備寫iic的寄存器地址

*BufferPtr:寫iic的數據內容

ByteCount:寫iic的數據長度

IIC_BASE_ADDRESS:就是這個iic的地址,這個可以從#include "xparameters.h"里面看到。

然后就是讀函數了:

unsigned iic_read(unsigned char iic_base,unsigned char Address, unsigned char *BufferPtr, int ByteCount)
{
    volatile unsigned ReceivedByteCount;
    u16 StatusReg;
    u32 CntlReg;
    do {
        StatusReg = XIic_ReadReg(IIC_BASE_ADDRESS, XIIC_SR_REG_OFFSET);
        if(!(StatusReg & XIIC_SR_BUS_BUSY_MASK)) {
            ReceivedByteCount = XIic_Send(IIC_BASE_ADDRESS,
                    iic_base,
                            (u8 *)&Address,
                            sizeof(Address),
                            XIIC_STOP);

            if (ReceivedByteCount != sizeof(Address)) {


                CntlReg = XIic_ReadReg(IIC_BASE_ADDRESS,
                            XIIC_CR_REG_OFFSET);
                XIic_WriteReg(IIC0_BASE_ADDRESS, XIIC_CR_REG_OFFSET,
                        CntlReg | XIIC_CR_TX_FIFO_RESET_MASK);
                XIic_WriteReg(IIC0_BASE_ADDRESS,
                        XIIC_CR_REG_OFFSET,
                        XIIC_CR_ENABLE_DEVICE_MASK);
            }
        }

    } while (ReceivedByteCount != sizeof(Address));/* Send is aborted so reset Tx FIFO */


    ReceivedByteCount = XIic_Recv(IIC_BASE_ADDRESS, iic_base,
                    BufferPtr, ByteCount, XIIC_STOP);
    return ReceivedByteCount;
}
說明下函數的內容:

iic_base:iic的地址,這個可以看原理圖,就知道iic的地址

Address:准備讀iic的寄存器地址

*BufferPtr:讀到iic的數據內容。

ByteCount:讀iic的數據長度

 

以上就是pl端iic的使用了,這樣就可以愉快的使用了。嘻嘻嘻。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM