at24c512.c

来自「使用JTAG口对AT91R40008芯片进行FLASH编程的程序」· C语言 代码 · 共 228 行

C
228
字号
//*---------------------------------------------------------------------------
//*      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           : at24c512.c
//* Object              : Serial EEPROM Atmel AT24C512 Driver.
//*
//* 1.0 30/06/00 PF		: Creation
//* 1.1 15/01/02 JPP    : Clean
//*---------------------------------------------------------------------------

//* --------------------------- include file ----------------------------------

#include	"at24c512.h"					//* prototype at24 function & struc
#include    "global.h"
/*---------------------------  Constants definition -------------------------*/

#define OTHER_I2C

static u_short current_address;
#define DEBUG_TRACE_COM
/*--------------------  Function definition for I2C driver ------------------*/
#ifndef OTHER_I2C
//*-----------------------------------------------------------------------------
//* Function Name       :  no_handler_tc (I2Cdesc *I2C_pt)
//* Object              : I2C Error
//* Input Parameters    : <I2C_pt> = I2C Peripheral Descriptor pointer
//* Output Parameters   : none
//*-----------------------------------------------------------------------------
void no_handler_tc (I2Cdesc *I2C_pt)
{//* Begin
#ifdef DEBUG_TRACE_COM
	at91_print(&COM0,"\n\rat24: no_handler_tc ERROR !\n\r");
#endif  //* DEBUG_TRACE_COM
	at91_I2C_lineClose(&I2C_line);
}//* End

//*-----------------------------------------------------------------------------
//* Function Name       :  at91_I2CError(I2Cdesc *I2C_pt)
//* Object              : I2C Error
//* Input Parameters    : <I2C_pt> = I2C Peripheral Descriptor pointer
//*			: loadAddress  Address on slave
//*			: nbByte	   Read to bytes
//*			: I2C_address  I2C address Divice
//* Output Parameters   : none
//*-----------------------------------------------------------------------------
void at91_I2CError(I2Cdesc *I2C_pt)
{//* Begin
#ifdef DEBUG_TRACE_COM
	at91_print(&COM0,"\n\rat24: I2C EPROM ERROR !\n\r");
#endif  //* DEBUG_TRACE_COM
    //* change interrupt handler to at91_I2CTxSendStopSDA
    I2C_pt->I2CTCHandler = at91_I2CSendStopSDA;

}//* End
#endif // OTHER_I2C
/*-----------------  External Function definition for at24c512 --------------*/

//*--------------------------------------------------------------------------------
//* Function Name       : at24_Init( u_int speed )
//* Object              : Init the at14c512 driver
//* Input Parameters    : speed define in timer value by ((MCKI /4 ) / (I2C Freq))
//* Output Parameters   : None
//*---------------------------------------------------------------------------------
void at24_Init( u_int speed )
{//* Begin

    /*I2C init */
    I2C_line.pio_ctrl_desc  = &PIO_DESC;			//* PIO field
    I2C_line.pioa_base		= PIO_BASE;
    I2C_line.SDA_line 		= PIO_SDA ;
    I2C_line.SCL_line 		= PIO_SCL ;

    // EPROM AT24C512 address
    I2C_line.deviceAddress = AT24C512_I2C_ADDRESS;

    //* Timer 0 Counter field
    I2C_line.TCBase 	= TCB0_BASE;
    I2C_line.timerBase 	= TC2_BASE;
    I2C_line.channelId 	= TC2_ID;

    //* IRQ field
    I2C_line.timerBase->TC_IDR = 0x1FF;				//* disable interrupt
    I2C_line.AICHandler = tc2_interrupt_handler;
    I2C_line.I2CTCHandler = no_handler_tc;

    at91_I2C_lineOpen(&I2C_line, speed);


} //* End
//*---------------------------------------------------------------------------------------
//* Function Name       : at24_TransfertEnd(void)
//* Object              : Wait the end I2C transfert when I2C_line.state != OK
//* Input Parameters    : None
//* Output Parameters   : none
//*---------------------------------------------------------------------------------------
void  at24_TransfertEnd(void)
{//* Begin
	at91_I2CTransfertEnd(&I2C_line);
}//* End

//*---------------------------------------------------------------------------------------
//* Function Name       : at24_Write (u_short address, u_char *data_array, u_int num_bytes )
//* Object              : Write to serial EEPROM note: the data input buffer are free when
//*                       I2C_line.state != OK
//* Input Parameters    :
//*			address   : devive assress to write
//*         pointer   : data for write
//*			num_bytes : nbbyte
//* Output Parameters   : none
//*---------------------------------------------------------------------------------------
void at24_Write( u_short address, u_char *data_array, u_int num_bytes)
{//* Begin
    u_short num_page = num_bytes / AT25C512_PAGE_LENGTH;
    u_short cpt;

	//* set global buffer aadres
    I2C_line.TxEnd  = I2C_line.TxPtr  = data_array;

    //* Trig the timer
    I2C_line.timerBase->TC_CCR = TC_SWTRG;
	//* write modulo page block
	for (cpt=0; cpt<num_page; cpt++)
	{
		at91_I2CTransfertEnd(&I2C_line);
    	I2C_line.timerBase->TC_CCR = TC_SWTRG;
		at91_I2CWrite(&I2C_line,address+cpt*AT25C512_PAGE_LENGTH,AT25C512_PAGE_LENGTH);
    	I2C_line.timerBase->TC_CCR = TC_SWTRG;
    	//* Trig the timer
		at91_I2CTransfertEnd(&I2C_line);
	    //*  wait write tine 10 ms
    	I2C_line.timerBase->TC_CCR = TC_SWTRG;
    	at91_I2CWaitTime(&I2C_line,100,MCKKHz);

	}
    //* write modulo rest page
	if((num_page*AT25C512_PAGE_LENGTH) < num_bytes)
	{
		at91_I2CTransfertEnd(&I2C_line);
    	I2C_line.timerBase->TC_CCR = TC_SWTRG;
		at91_I2CWrite(&I2C_line,address+cpt*AT25C512_PAGE_LENGTH,(num_bytes-AT25C512_PAGE_LENGTH*cpt));
	    //*  wait write tine 10 ms
	    I2C_line.timerBase->TC_CCR = TC_SWTRG;
	    at91_I2CTransfertEnd(&I2C_line);
	    at91_I2CWaitTime(&I2C_line,100,MCKKHz);
	}

}//* End

//*---------------------------------------------------------------------------------------
//* Function Name       : at24_Read( u_short address, u_char *data_array, u_int num_bytes )
//* Object              : Reading the serial EEPROM  note: the data outout buffer are full
//*                       when I2C_line.state != OK
//* Input Parameters    :
//*			address   : devive assress to Read
//*         pointer   : data to read
//*			num_bytes : nbbyte
//* Output Parameters   : none
//*---------------------------------------------------------------------------------------
void  at24_Read(u_short address, u_char *data_array, u_int num_bytes)
{//* Begin

    //* Set the recivied buffer
    I2C_line.RxEnd = I2C_line.RxPtr  = data_array;

    //* Trig the timer
    I2C_line.timerBase->TC_CCR = TC_SWTRG;
	at91_I2CTransfertEnd(&I2C_line);
    at91_I2CRead(&I2C_line,address,num_bytes);

}//* End

/*-----------------------  special xmodem function  -------------------------*/
//*---------------------------------------------------------------------------------------
//* Function Name       : at24_init( u_short BaseAddress )
//* Object              : init the serial I2C eeprom driver at 50000 Khz and save the base
//*						  works address
//* Input Parameters    :
//*			BaseAddress : works address
//* Output Parameters   : none
//*---------------------------------------------------------------------------------------
void at24_init( u_short BaseAddress )
{//* Begin
	//* Set the base address
	current_address = BaseAddress;
	//* set at 50 kHz
	at24_Init((66000000/4)/50000);
}//* End

//*---------------------------------------------------------------------------------------
//* Function Name       : at24_write_block ( u_char *buffer,int size)
//* Object              : This function is called by xmodem function and write in the eeprom
//* Input Parameters    :
//*			 buffer - buffer address to write
//*			 size   - Size to write
//* Output Parameters   : TRUE
//*---------------------------------------------------------------------------------------
int at24_write_block ( u_char *buffer,int size)

{//* Begin
	at24_Write( current_address, buffer, size);
	current_address += size;
	//* waite the end of write
	at91_I2CTransfertEnd(&I2C_line);
	return(TRUE);
}//* End

//*---------------------------------------------------------------------------------------
//* Function Name       : at24_read_block ( u_int address, u_char *buffer,int size)
//* Object              : This function is called by xmodem function and read in the eeprom
//* Input Parameters    :
//*			 address- add to read
//*			 buffer - buffer address to Read
//*			 size   - Size to Read
//* Output Parameters   : TRUE
//*---------------------------------------------------------------------------------------
void at24_read_block ( int address, char *buffer,int size)
{//* Begin
	at24_Read( (u_short) address, (u_char *) buffer,(u_int) size);
	//* waite the end of read
	at91_I2CTransfertEnd(&I2C_line);
}//* End

⌨️ 快捷键说明

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