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

📄 at24c512.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 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           : at24c512.c
//* Object              : Serial EEPROM Atmel AT24C512 Driver.
//*
//* 1.0 30/06/00 PF     : Creation
//* 1.1 15/01/02 JPP    : Clean
//* 1.2 29/01/02 PFi    : Adaptation for EB55
//*---------------------------------------------------------------------------


//* --------------------------- include file ----------------------------------
#include    "parts/m55800/lib_m55800.h"     //* for &PIO_DESC
#include    "targets/eb55/eb55.h"         //* for pin & Clock description
#include    "at24c512.h"                    //* prototype at24 function & struc

/*---------------------------  Constants definition -------------------------*/


/* Global Variables */
I2Cdesc     I2C_line ;

/*--------------------  Function definition for I2C driver ------------------*/
//*-----------------------------------------------------------------------------
//* 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

    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

    //* change interrupt handler to at91_I2CTxSendStopSDA
    I2C_pt->I2CTCHandler = at91_I2CSendStopSDA;

}//* End
/*-----------------  External Function definition for at24c512 --------------*/

//*--------------------------------------------------------------------------------
//* Function Name       : at24_Init( u_int speed )
//* Object              : Init the at24c512 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  = &PIOA_DESC;            //* PIO field
    I2C_line.pioa_base      = PIOA_BASE;
    I2C_line.SDA_line       = SDA ;
    I2C_line.SCL_line       = SCL ;

    // EPROM AT24C512 address
    I2C_line.deviceAddress = AT24C512_I2C_ADDRESS;

    //* Timer 0 Counter field
    I2C_line.TCBase     = TCB0_BASE;
    I2C_line.timerBase  = (StructTC *) TCB0_BASE;
    I2C_line.channelId  = TC0_ID;

    //* IRQ field
    I2C_line.timerBase->TC_IDR = 0x1FF;             //* disable interrupt
    I2C_line.AICHandler = tc0_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 / AT24C512_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);
        at91_I2CWrite(&I2C_line,address+cpt*AT24C512_PAGE_LENGTH,AT24C512_PAGE_LENGTH);
        //*  wait write tine 10 ms
        at91_I2CWaitTime(&I2C_line,AT24C512_TWR,MCKKHz);

    }
    //* write modulo rest page
    if((num_page*AT24C512_PAGE_LENGTH) < num_bytes)
    {
        at91_I2CTransfertEnd(&I2C_line);
        at91_I2CWrite(&I2C_line,address+cpt*AT24C512_PAGE_LENGTH,(num_bytes-AT24C512_PAGE_LENGTH*cpt));
        //*  wait write tine 10 ms
        at91_I2CTransfertEnd(&I2C_line);
        at91_I2CWaitTime(&I2C_line,AT24C512_TWR,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);
    at91_I2CTransfertEnd(&I2C_line);

}//* End

⌨️ 快捷键说明

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