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

📄 lib_twi.c

📁 ARM9200开发板的ROM boot程序源码1.0
💻 C
字号:
//*---------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*---------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*-----------------------------------------------------------------------------
//* File Name           : lib_TWI.c
//* Object              : TWI Library.
//*
//* 1.0 04/04/01 HI     : Creation
//*---------------------------------------------------------------------------

#include    "periph\twi\lib_twi.h"
#include    "periph\apmc\lib_apmc.h"


//*-----------------------------------------------------------------------------
//* Function Name       : at91_TWI_open
//* Object              : Open an TWI Channel
//* Input Parameters    : <TWI_desc> = TWI Descriptor pointer
//* Output Parameters   : TRUE
//* Functions called    : at91_pio_set_mode, at91_pio_close
//*-----------------------------------------------------------------------------
void at91_TWI_open(
					const AT91PS_APMC pApmc,
					const AT91PS_TWIDesc pTWIDesc,
					unsigned int mode )
//* Begin
{

    unsigned int	pin_mask = ((1 << pTWIDesc->pin_sck) | (1<<pTWIDesc->pin_sda)) ;
    unsigned int   open_drain_mask = 0 ;

    //* Enable the TWI Clock
    at91_apmc_pcer(pApmc, pTWIDesc->periph_id);

    open_drain_mask = ( (1 << pTWIDesc->pin_sck) | (1 << pTWIDesc->pin_sda) ) ;

	//* Setup IOs according the required configuration
    at91_pio_set_mode ( pTWIDesc->pio_base, open_drain_mask, PIO_OPENDRAIN_ON ) ;

	//* Define SCK and SDA as peripheralA pins
	at91_pio_set_periphmode(pTWIDesc->pio_base, pin_mask, 0);

	//* Define SCK and SDA as peripheral
    at91_pio_close ( pTWIDesc->pio_base, (1 << pTWIDesc->pin_sck) | (1 << pTWIDesc->pin_sda) ) ;

    //* Reset the TWI
	pTWIDesc->TWI_base->TWI_CR = TWI_SWRST ;

    pTWIDesc->TWI_base->TWI_MMR = 0x00500200 ;

    //* TWI Frequency limitation
	pTWIDesc->TWI_base->TWI_CWGR = 0x00060606;
}
//* End


//*-----------------------------------------------------------------------------
//* Function Name       : at91_TWI_close
//* Object              : Close an TWI Channel
//* Input Parameters    : <TWI_desc> = TWI Descriptor pointer
//* Output Parameters   :
//* Functions called    :
//*-----------------------------------------------------------------------------
void at91_TWI_close(const AT91PS_APMC pApmc, const AT91PS_TWIDesc pTWIDesc )
//* Begin
{
    unsigned int   pin_mask ;
    //* Disable the TWI
    pTWIDesc->TWI_base-> TWI_CR = TWI_SWRST ;

    //* Redefine all TWI signals as PIO
    pin_mask = ((1<<pTWIDesc->pin_sck) |
                (1<<pTWIDesc->pin_sda)) ;
    at91_pio_open ( pTWIDesc->pio_base, pin_mask, RESET_PIO_CONF ) ;

     //* Disable the TWI Clock
    at91_apmc_pcdr(pApmc, pTWIDesc->periph_id);
}
//* End

//*-----------------------------------------------------------------------------
//* Function Name       : at91_TWI_set_mode
//* Object              : Defines an TWI Mode Register
//* Input Parameters    : <TWI_desc> = TWI Descriptor pointer
//* Output Parameters   : none
//* Functions called    :
//*-----------------------------------------------------------------------------
void at91_TWI_set_mode(const AT91PS_TWIDesc pTWIDesc , unsigned int mode )
//* Begin
{
    pTWIDesc->TWI_base->TWI_MMR = mode ;
}
//* End

//*-----------------------------------------------------------------------------
//* Function Name       : at91_TWI_read
//* Object              :
//* Input Parameters    : <TWI_desc> = TWI Descriptor pointer
//* Output Parameters   : none
//* Functions called    :
//*-----------------------------------------------------------------------------
void at91_TWI_read(const AT91PS_TWIDesc pTWIDesc , short *data )
//* Begin
{
    *data = (pTWIDesc ->TWI_base->TWI_RHR) ;
}
//* End

//*-----------------------------------------------------------------------------
//* Function Name       : at91_TWI_write
//* Object              : Write data to the TWI
//* Input Parameters    : <TWI_desc> = TWI Descriptor pointer
//*                     : <data> = data buffer pointer
//* Output Parameters   : TRUE
//* Functions called    :
//*-----------------------------------------------------------------------------
void at91_TWI_write(const AT91PS_TWIDesc pTWIDesc , short *data)
//* Begin
{
    pTWIDesc->TWI_base->TWI_THR = *data ;
}
//* End

//*-----------------------------------------------------------------------------
//* Function Name       : at91_TWI_get_status
//* Object              : Return TWI Status Register
//* Input Parameters    : <TWI_desc> = TWI Descriptor pointer
//* Output Parameters   : none
//* Functions called    :
//*-----------------------------------------------------------------------------
unsigned int at91_TWI_get_status(const AT91PS_TWIDesc pTWIDesc )
//* Begin
{
    return ( pTWIDesc->TWI_base->TWI_SR ) ;
}
//* End

//*-----------------------------------------------------------------------------
//* Function Name       : at91_TWI_send_frame
//* Object              : Transmit a complete frame.
//* Input Parameters    : <TWI_desc> =  TWI pointer
//*                     : <pt_buffer> = the address of the receive buffer
//*                     : <max_size> = the maximum number of bytes to be
//*                     :              received
//* Output Parameters   :
//* Functions called    : none
//*-----------------------------------------------------------------------------
unsigned int at91_TWI_send_frame(const AT91PS_TWIDesc pTWIDesc, char *pt_buffer, unsigned int size_buf )
//* Begin
{
    //* Wait for previous transfer finished
    while (!( pTWIDesc->TWI_base->TWI_SR & TWI_TXCOMP ));

    pTWIDesc->TWI_base->TWI_CR |= ( TWI_START | TWI_MSEN );
    pTWIDesc->TWI_base->TWI_MMR &= ~TWI_MREAD ;

    //* Store the address of the buffer
    while (size_buf-- > 0)
    {
		pTWIDesc->TWI_base->TWI_THR = *pt_buffer ;
		// Wait THR Holding register to be empty
		while (!( pTWIDesc->TWI_base->TWI_SR & TWI_TXRDY ));
    }

    pTWIDesc->TWI_base->TWI_CR |= TWI_STOP ;
    pTWIDesc->TWI_base->TWI_CR |= TWI_MSDIS ;
    //* Return true
    return ( TRUE ) ;
}
//* End


//*-----------------------------------------------------------------------------
//* Function Name       : at91_TWI_receive_frame
//* Object              : Receive a complete frame.
//* Input Parameters    : <TWI_desc> =  TWI pointer
//*                     : <pt_buffer> = the address of the receive buffer
//*                     : <max_size> = the maximum number of bytes to be
//*                     :              received
//* Output Parameters   :
//* Functions called    : none
//*-----------------------------------------------------------------------------
unsigned int at91_TWI_receive_frame(const AT91PS_TWIDesc pTWIDesc, char *pt_buffer, unsigned int max_size )
//* Begin
{
    pTWIDesc->TWI_base->TWI_CR |= TWI_MSEN ;
    pTWIDesc->TWI_base->TWI_MMR |= TWI_MREAD ;

    //* Wait for previous transfer finished
    while (max_size-- > 0)
    {
		while (!( pTWIDesc->TWI_base->TWI_SR & TWI_RXRDY ));             // A time out function can be helpful...
		*pt_buffer = pTWIDesc->TWI_base->TWI_RHR ;
    }

    pTWIDesc->TWI_base->TWI_CR |= TWI_MSDIS ;

    return ( TRUE ) ;
}
//* End

⌨️ 快捷键说明

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