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

📄 twi.c

📁 at91sam9263 for rtc,其中rtc是npx公司的pcf8563
💻 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
//*--------------------------------------------------------------------------------------

#include "bsp.h"

//*=========================================================
//*		INIT
//*=========================================================
//*----------------------------------------------------------------------------
//* \fn    AT91F_SetTwiClock
//* \brief Initialization
//*----------------------------------------------------------------------------
void AT91F_SetTwiClock(const AT91PS_TWI pTwi)
{
	int sclock;

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

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

    pTwi->TWI_CWGR	= 0x00010000 | sclock | (sclock << 8);
    //pTwi->TWI_CWGR = 0x1ffff;
}

//*=========================================================
//*		WRITE
//*=========================================================
//*----------------------------------------------------------------------------
//* \fn    AT91F_TWI_Write
//* \brief Send n bytes to a slave device
//*----------------------------------------------------------------------------
int AT91F_TWI_Write(const AT91PS_TWI pTwi ,char slave ,int address, unsigned char *data2send, int size)
{
	unsigned int status;

	// Set the TWI Master Mode Register
	pTwi->TWI_MMR = ( (slave<<16) | AT91C_TWI_IADRSZ_1_BYTE ) & ~AT91C_TWI_MREAD;	
	
	// Set TWI Internal Address Register
	pTwi->TWI_IADR = address;

	status = pTwi->TWI_SR;
		
	pTwi->TWI_THR = *(data2send++);
	
	pTwi->TWI_CR = AT91C_TWI_START;
		
	while (size-- >1){

		// Wait THR Holding register to be empty
		while (!(pTwi->TWI_SR & AT91C_TWI_TXRDY_MASTER));
	
		// Send first byte
		pTwi->TWI_THR = *(data2send++);
		
	}

	pTwi->TWI_CR = AT91C_TWI_STOP;		

	status = pTwi->TWI_SR;

	// Wait transfer is finished
    while (!(pTwi->TWI_SR & AT91C_TWI_TXCOMP_MASTER));
		
	return TWI_OK;
}

//*=========================================================
//*		READ
//*=========================================================
//*----------------------------------------------------------------------------
//* \fn    AT91F_TWI_Read
//* \brief Read n bytes from a slave device
//*----------------------------------------------------------------------------
int AT91F_TWI_Read(const AT91PS_TWI pTwi ,char slave , int address, char *data, int size)
{
	
	
	// Set the TWI Master Mode Register
	pTwi->TWI_MMR = (slave<<16) | AT91C_TWI_IADRSZ_1_BYTE | AT91C_TWI_MREAD;	
	
	// Set TWI Internal Address Register
	pTwi->TWI_IADR = address;
	
	// Start transfer
	pTwi->TWI_CR |= AT91C_TWI_START;
		
	while (size-- >1){
		
		// Wait RHR Holding register is full
		while (!(pTwi->TWI_SR & AT91C_TWI_RXRDY));

		// Read byte
		*(data++) = pTwi->TWI_RHR;
	
	}
	
	pTwi->TWI_CR |= AT91C_TWI_STOP;

	//status = pTwi->TWI_SR;

	// Wait transfer is finished
    while (!(pTwi->TWI_SR & AT91C_TWI_TXCOMP_MASTER));

	// Read last byte
	*data = pTwi->TWI_RHR;
		
	return TWI_OK;
}

//*=========================================================
//*		READ
//*=========================================================
//*----------------------------------------------------------------------------
//* \fn    AT91F_TWI_Read
//* \brief Read n bytes from a slave device
//*----------------------------------------------------------------------------
unsigned char i2c_reg_read(char slave, char reg)
{

	char read;
	
	AT91F_TWI_Read(AT91C_BASE_TWI, slave,reg, (char *)&read, 1);
	
	return read;
}

//*=========================================================
//*		READ
//*=========================================================
//*----------------------------------------------------------------------------
//* \fn    AT91F_TWI_Read
//* \brief Read n bytes from a slave device
//*----------------------------------------------------------------------------
void i2c_reg_write (char slave, char reg, unsigned char val)
{
	
	AT91F_TWI_Write(AT91C_BASE_TWI, slave,reg, &val, 1); 
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_TWI_CfgPIO
//* \brief Configure PIO controllers to drive TWI signals
//*----------------------------------------------------------------------------
__inline void AT91F_TWI_CfgPIO (void)
{
	// Configure PIO controllers to periph mode
	AT91F_PIO_CfgPeriph(
		AT91C_BASE_PIOB, // PIO controller base address
		((unsigned int) AT91C_PB4_TWD     ) |
		((unsigned int) AT91C_PB5_TWCK    ), // Peripheral A
		0); // Peripheral B
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_PIO_CfgOpendrain
//* \brief Configure PIO in open drain
//*----------------------------------------------------------------------------
__inline void AT91F_PIO_CfgOpendrain(
	AT91PS_PIO pPio,             // \arg pointer to a PIO controller
	unsigned int multiDrvEnable) // \arg pio to be configured in open drain
{
	// Configure the multi-drive option
	pPio->PIO_MDDR = ~multiDrvEnable;
	pPio->PIO_MDER = multiDrvEnable;
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_PMC_EnablePeriphClock
//* \brief Enable peripheral clock
//*----------------------------------------------------------------------------
__inline void AT91F_PMC_EnablePeriphClock (
	AT91PS_PMC pPMC, // \arg pointer to PMC controller
	unsigned int periphIds)  // \arg IDs of peripherals to enable
{
	pPMC->PMC_PCER = periphIds;
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_TWI_CfgPMC
//* \brief Enable Peripheral clock in PMC for  TWI
//*----------------------------------------------------------------------------
__inline void AT91F_TWI_CfgPMC (void)
{
	AT91F_PMC_EnablePeriphClock(
		AT91C_BASE_PMC, // PIO controller base address
		((unsigned int) 1 << AT91C_ID_TWI));
}
//*=========================================================
//*		READ
//*=========================================================
//*----------------------------------------------------------------------------
//* \fn    AT91F_I2C_Init
//* \brief Read n bytes from a slave device
//*----------------------------------------------------------------------------

void AT91F_TWI_Init(void)
{
	
	AT91F_TWI_CfgPIO ();
	AT91F_PIO_CfgOpendrain(AT91C_BASE_PIOB, (unsigned int) AT91C_PB4_TWD);

	// Configure PMC by enabling TWI clock
	AT91F_TWI_CfgPMC ();

	// Configure TWI in master mode
	 // Reset the TWI
    AT91C_BASE_TWI->TWI_CR = AT91C_TWI_SWRST;

    // Set master mode
    AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN;
			
	// Set TWI Clock Waveform Generator Register	
	AT91F_SetTwiClock(AT91C_BASE_TWI);

	
}

⌨️ 快捷键说明

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