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

📄 lib_twi.c

📁 EMB91SAM7S64开发板(全套资料)
💻 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           : twi.c
//* Object              : Basic TWI EEPROM driver
//* Translator          :
//* 1.0 25/11/02 NL		: Creation
//* 1.1 31/Jan/05 JPP   : Clean For basic
//*--------------------------------------------------------------------------------------

#include "board.h"
#include "lib_twi.h"
#include "main.h"

#define ERROR (AT91C_TWI_NACK)
//*=========================================================
//*		INIT
//*=========================================================
//*----------------------------------------------------------------------------
//* \fn    AT91F_SetTwiClock
//* \brief Initialization
//*----------------------------------------------------------------------------
void AT91F_SetTwiClock(void)
{
	int sclock;

	/* Here, CKDIV = 1 and CHDIV=CLDIV  ==> CLDIV = CHDIV = 1/4*((Fmclk/FTWI) -6)*/

	sclock = (10*MCK /AT91C_TWI_CLOCK);
sclock = (MCK /AT91C_TWI_CLOCK);	
	if (sclock % 10 >= 5)
		sclock = (sclock /10) - 5;
	else
		sclock = (sclock /10)- 6;
	sclock = (sclock + (4 - sclock %4)) >> 2;	// div 4

    AT91C_BASE_TWI->TWI_CWGR	= ( 1<<16 ) | (sclock << 8) | sclock  ;
}

//*=========================================================
//*		WRITE
//*=========================================================
//*----------------------------------------------------------------------------
//* \fn    AT91F_TWI_WriteByte
//* \brief Send a byte to a slave device
//*----------------------------------------------------------------------------
int AT91F_TWI_WriteByte(const AT91PS_TWI pTwi ,int mode, int int_address, char *data2send, int nb)
{
	unsigned int status,counter=0,error=0;

	// Set TWI Internal Address Register
	if ((mode & AT91C_TWI_IADRSZ) != 0) pTwi->TWI_IADR = int_address;

	// Set the TWI Master Mode Register
	  pTwi->TWI_MMR = mode & ~AT91C_TWI_MREAD;
	if(nb <2){
		pTwi->TWI_CR = AT91C_TWI_START | AT91C_TWI_MSEN | AT91C_TWI_STOP;
		pTwi->TWI_THR = *data2send;
	}
	else
	{
	// Set the TWI Master Mode Register
	  for(counter=0;counter<nb;counter++){
          pTwi->TWI_CR = AT91C_TWI_START | AT91C_TWI_MSEN;
          if (counter == (nb - 1)) pTwi->TWI_CR = AT91C_TWI_STOP;
          status = pTwi->TWI_SR;
          if ((status & ERROR) == ERROR) error++;
          while (!(status & AT91C_TWI_TXRDY)){
               status = pTwi->TWI_SR;
               if ((status & ERROR) == ERROR) error++;
          }
          pTwi->TWI_THR = *(data2send+counter);
	   }
	}
	status = pTwi->TWI_SR;
	if ((status & ERROR) == ERROR) error++;
	while (!(status & AT91C_TWI_TXCOMP)){
    		status = pTwi->TWI_SR;
    		if ((status & ERROR) == ERROR) error++;
    }
	return error;
}

//*=========================================================
//*		READ
//*=========================================================
//*----------------------------------------------------------------------------
//* \fn    AT91F_TWI_ReadByte
//* \brief Read a byte from a slave device
//*----------------------------------------------------------------------------
int AT91F_TWI_ReadByte(const AT91PS_TWI pTwi ,int mode, int int_address, char *data, int nb)
{
	unsigned int status,counter=0,error=0;


	// Set TWI Internal Address Register
	if ((mode & AT91C_TWI_IADRSZ) != 0) pTwi->TWI_IADR = int_address;

	// Set the TWI Master Mode Register
	pTwi->TWI_MMR = mode | AT91C_TWI_MREAD;

	// Start transfer
	if (nb == 1){
	   pTwi->TWI_CR = AT91C_TWI_START | AT91C_TWI_STOP;
	   status = pTwi->TWI_SR;
    	   if ((status & ERROR) == ERROR) error++;
	   while (!(status & AT91C_TWI_TXCOMP)){
    	      status = pTwi->TWI_SR;
              if ((status & ERROR) == ERROR) error++;
    	   }
	   *(data) = pTwi->TWI_RHR;
	}
 	else{
 	   pTwi->TWI_CR = AT91C_TWI_START | AT91C_TWI_MSEN;
	   status = pTwi->TWI_SR;
	   if ((status & ERROR) == ERROR) error++;

	// Wait transfer is finished
           while (!(status & AT91C_TWI_TXCOMP)){
   		status = pTwi->TWI_SR;
   		if ((status & ERROR )== ERROR) error++;
    		if(status & AT91C_TWI_RXRDY){
			*(data+counter++) = pTwi->TWI_RHR;
			if (counter == (nb - 1)) pTwi->TWI_CR = AT91C_TWI_STOP;
		}
	   }
	}
	return 0;
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_TWI_Open
//* \brief Initializes TWI device
//*----------------------------------------------------------------------------
void AT91F_TWI_Open(void)
{
	// Configure TWI PIOs
	AT91F_TWI_CfgPIO ();
	
AT91F_PIO_CfgOpendrain(AT91C_BASE_PIOA,(unsigned int)AT91C_PA3_TWD);	
	// Configure PMC by enabling TWI clock
	AT91F_TWI_CfgPMC ();

	// Configure TWI in master mode
	AT91F_TWI_Configure (AT91C_BASE_TWI);

	// Set TWI Clock Waveform Generator Register
	AT91F_SetTwiClock();
}



⌨️ 快捷键说明

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