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

📄 twi.c

📁 IAR5.2下 AT91SAM9260 ARM 对 MCP2515 控制源化码
💻 C
📖 第 1 页 / 共 2 页
字号:
}

//-----------------------------------------------------------------------------
/// Starts a write operation on the TWI to access the selected slave, then
/// returns immediately. A byte of data must be provided to start the write;
/// other bytes are written next.
/// \param pTwi  Pointer to an AT91S_TWI instance.
/// \param address  Address of slave to acccess on the bus.
/// \param iaddress  Optional slave internal address.
/// \param isize  Number of internal address bytes.
/// \param byte  First byte to send.
//-----------------------------------------------------------------------------
void TWI_StartWrite(AT91S_TWI *pTwi,
                    unsigned char address,
                    unsigned int iaddress,
                    unsigned char isize,
                    unsigned char byte)
{
    trace_LOG(trace_DEBUG, "-D- TWI_StartWrite()\n\r");
    SANITY_CHECK(pTwi);
    SANITY_CHECK((address & 0x80) == 0);
    SANITY_CHECK((iaddress & 0xFF000000) == 0);
    SANITY_CHECK(isize < 4);

    // Set slave address and number of internal address bytes
    pTwi->TWI_MMR = (isize << 8) | (address << 16);

    // Set internal address bytes
    pTwi->TWI_IADR = iaddress;

    // Write first byte to send
    TWI_WriteByte(pTwi, byte);
}

//-----------------------------------------------------------------------------
/// Returns 1 if a byte has been received and can be read on the given TWI
/// peripheral; otherwise, returns 0. This function resets the status register
/// of the TWI.
/// \param pTwi  Pointer to an AT91S_TWI instance.
//-----------------------------------------------------------------------------
unsigned char TWI_ByteReceived(AT91S_TWI *pTwi)
{
    return ((pTwi->TWI_SR & AT91C_TWI_RXRDY) == AT91C_TWI_RXRDY);
}

//-----------------------------------------------------------------------------
/// Returns 1 if a byte has been sent, so another one can be stored for
/// transmission; otherwise returns 0. This function clears the status register
/// of the TWI.
/// \param pTwi  Pointer to an AT91S_TWI instance.
//-----------------------------------------------------------------------------
unsigned char TWI_ByteSent(AT91S_TWI *pTwi)
{
    return ((pTwi->TWI_SR & AT91C_TWI_TXRDY) == AT91C_TWI_TXRDY);
}

//-----------------------------------------------------------------------------
/// Returns 1 if the current transmission is complete (the STOP has been sent);
/// otherwise returns 0.
/// \param pTwi  Pointer to an AT91S_TWI instance.
//-----------------------------------------------------------------------------
unsigned char TWI_TransferComplete(AT91S_TWI *pTwi)
{
    return ((pTwi->TWI_SR & AT91C_TWI_TXCOMP) == AT91C_TWI_TXCOMP);
}

//-----------------------------------------------------------------------------
/// Enables the selected interrupts sources on a TWI peripheral.
/// \param pTwi  Pointer to an AT91S_TWI instance.
/// \param sources  Bitwise OR of selected interrupt sources.
//-----------------------------------------------------------------------------
void TWI_EnableIt(AT91S_TWI *pTwi, unsigned int sources)
{
    SANITY_CHECK(pTwi);
    SANITY_CHECK((sources & 0xFFFFFEF8) == 0);

    pTwi->TWI_IER = sources;
}

//-----------------------------------------------------------------------------
/// Disables the selected interrupts sources on a TWI peripheral.
/// \param pTwi  Pointer to an AT91S_TWI instance.
/// \param sources  Bitwise OR of selected interrupt sources.
//-----------------------------------------------------------------------------
void TWI_DisableIt(AT91S_TWI *pTwi, unsigned int sources)
{
    SANITY_CHECK(pTwi);
    SANITY_CHECK((sources & 0xFFFFFEF8) == 0);

    pTwi->TWI_IDR = sources;
}

//-----------------------------------------------------------------------------
/// Returns the current status register of the given TWI peripheral. This
/// resets the internal value of the status register, so further read may yield
/// different values.
/// \param pTwi  Pointer to an AT91S_TWI instance.
//-----------------------------------------------------------------------------
unsigned int TWI_GetStatus(AT91S_TWI *pTwi)
{
    SANITY_CHECK(pTwi);

    return pTwi->TWI_SR;
}

//-----------------------------------------------------------------------------
/// Returns the current status register of the given TWI peripheral, but
/// masking interrupt sources which are not currently enabled.
/// This resets the internal value of the status register, so further read may
/// yield different values.
/// \param pTwi  Pointer to an AT91S_TWI instance.
//-----------------------------------------------------------------------------
unsigned int TWI_GetMaskedStatus(AT91S_TWI *pTwi)
{
    unsigned int status;
    
    SANITY_CHECK(pTwi);
    
    status = pTwi->TWI_SR;
    status &= pTwi->TWI_IMR;

    return status;
}


//*============================================================================
//* 函数名称:TWI_Write
//* 函数功能:写 size 字节数据到从设备
//* 入口参数:pTwi	:指向TWI结构的首地址-----设为AT91C_BASE_TWI
//*	      slvAddr	: 从设备地址
//*	      address	: 从设备内部地址(要写入数据的位置)
//*	      *data2send: 要写入数据的起始地址
//*	      size		: 要写入数据的长度
//* 返回值  :0  : 写正常
//*	      非0:写过程发生错误
//*============================================================================
int TWI_Write(const AT91PS_TWI pTwi ,char slvAddr,int address, char *data2send, int size)
{
    unsigned int status,SLV_ADDR,error = 0;
	
    SLV_ADDR = slvAddr << 16;
    // 设置 TWI 为主发送模式,内部地址为 1 字节
    pTwi->TWI_MMR = ( SLV_ADDR | AT91C_TWI_IADRSZ_1_BYTE ) & ~AT91C_TWI_MREAD;	
	
    // 装载从器件内部地址
    pTwi->TWI_IADR = address;

    status = pTwi->TWI_SR;
		
    pTwi->TWI_THR = *(data2send++);
    //delay_ms();
    pTwi->TWI_CR = AT91C_TWI_START;
		
    while (size-- >1)
    {
        // 等待发送保持寄存器空
        while (!(pTwi->TWI_SR & AT91C_TWI_TXRDY))
        {
            // 读取状态寄存器
            status = pTwi->TWI_SR;

            // 如果发送完一个字节后没收到应答信号,则表明有一次传输错误
            if ((status & ERROR) == ERROR)   
                error++;
        }
	
        // 发送下一个字节
        pTwi->TWI_THR = *(data2send++);	
        //delay_ms();	
    }
	
    // 发送停止位
    pTwi->TWI_CR = AT91C_TWI_STOP;		

    status = pTwi->TWI_SR;
    if ((status & ERROR) == ERROR)   
    	error++;
    	
    // 等待数据发送完毕
    while (!(pTwi->TWI_SR & AT91C_TWI_TXCOMP))
    {
    	status = pTwi->TWI_SR;
        if ((status & ERROR) == ERROR)   
            error++;
    }	
    return error;
}


//*============================================================================
//* 函数名称:TWI_Read
//* 函数功能:从从设备中读取 size 字节数据
//* 入口参数:pTwi	: 指向TWI结构的首地址-----设为AT91C_BASE_TWI
//*	      slvAddr	: 从设备地址
//*	      address	: 从设备内部地址(要读取数据的位置)
//*	      *data	: 要读取数据的起始地址
//*	      size	: 要读取数据的长度
//* 返回值  :0  : 读正常
//*	      非0:读过程发生错误
//*============================================================================
int TWI_Read(const AT91PS_TWI pTwi ,char slvAddr, int address, char *data, int size)
{
    unsigned int status,SLV_ADDR,error;
	
    SLV_ADDR = slvAddr << 16;
    // 设置 TWI 为主接收模式,内部地址为 1 字节
    pTwi->TWI_MMR = SLV_ADDR | AT91C_TWI_IADRSZ_1_BYTE | AT91C_TWI_MREAD;	
	
    // 装载从设备地址
    pTwi->TWI_IADR = address;
	
    // 发送起始位
    pTwi->TWI_CR = AT91C_TWI_START;
	
    status = pTwi->TWI_SR;
		
    while (size-- >1)
    {	
        // 等待接收保存寄存器满
        while (!(pTwi->TWI_SR & AT91C_TWI_RXRDY))
        {
            // 读取状态寄存器
            status = pTwi->TWI_SR;
            // 如果发送完一个字节后没收到应答信号,则表明有一次传输错误
            if ((status & ERROR) == ERROR)   
                error++;
        }

        // 接收数据
        *(data++) = pTwi->TWI_RHR;
    }
	
    // 发送停止位
    pTwi->TWI_CR = AT91C_TWI_STOP;

    status = pTwi->TWI_SR;
    if ((status & ERROR) == ERROR)   
        error++;

    // 等待数据传输完毕
    while (!(pTwi->TWI_SR & AT91C_TWI_TXCOMP))
    {
    	status = pTwi->TWI_SR;
        if ((status & ERROR) == ERROR)   
            error++;	
    }

    // 接收最后一字节
    *data = pTwi->TWI_RHR;
		
    return error;
}

⌨️ 快捷键说明

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