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

📄 at25256.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           : at25256.c
//* Object              : AT25256 SPI EEPROM Memory Driver
//* Translator          : ARM Software Development Toolkit V2.5
//*
//* 1.0 09/06/01 PFi    : Creation
//* 1.0 03/01/02 PFi    : Clean up
//* 1.2 05/02/02 PFi    : at25_open() modified
//*--------------------------------------------------------------------------------------
#include    "drivers\serial_periph_1\serial_periph_1.h"
#include    "at25256.h"


/* Serial Peripheral Driver Descriptor. Defined externaly */
extern SerialPeriph1Desc    spi_eeprom_desc ;

/* Serial Peripheral Driver Data Descriptor */
extern SerialPeriph1DataDesc    spi_eeprom_data ;

//*--------------------------------------------------------------------------------------
//* Function Name       : at25_open
//* Object              : Open the SPI EEPROM connection and Enable writing
//* Input Parameters    :
//* Return value        : TRUE if Status Register Reads OK
//*--------------------------------------------------------------------------------------
u_int at25_open ( void )
{
//* Begin

    u_char  write_enable_cmd[1] = { AT25_WREN_CMD } ;
    u_char  open_rx_buffer[1] ;

    //* Open the SPI port between the AT91 and the SPI EEPROM AT25
    serial_periph_1_open ( &spi_eeprom_desc );

    //* Enable Writing to the Memory  : WREN Instruction (0x06)
    spi_eeprom_desc.spi_desc->spi_base->SP_RPR = (u_int)open_rx_buffer ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TPR = (u_int)write_enable_cmd ;
    spi_eeprom_desc.spi_desc->spi_base->SP_RCR = 1 ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TCR = 1 ;

    at25_wait_ready () ;

    if ( at25_read_status () != AT25_WREN )
        return ( FALSE );

    return ( TRUE ) ;
//* End
}

//*--------------------------------------------------------------------------------------
//* Function Name       : at25_wait_ready
//* Object              : Detect if the eeprom is ready or busy
//* Input Parameters    : None
//* Return value        : AT25_READY (0) if ready, else AT25_BUSY (1)
//*--------------------------------------------------------------------------------------
u_int at25_wait_ready ( void )
{
//* Begin
    u_int time_out = 1000 ;

    u_char status_reg = AT25_BUSY ;

    while ( status_reg == AT25_BUSY )
    {
        status_reg = ( at25_read_status () & AT25_STATUS_MASK_READY ) ;
        /* If timeout */
        if (time_out == 0)
        return ( AT25_BUSY );
        time_out-- ;
    }

    return ( AT25_READY ) ;

//* End
}

//*--------------------------------------------------------------------------------------
//* Function Name       : at25_read_status
//* Object              : Return the value of the Status Register
//* Input Parameters    : None
//* Return value        : <read_sr_buffer[1]> : Status Register's value
//*--------------------------------------------------------------------------------------
u_char at25_read_status ( void )
{
//* Begin
    u_char  read_sr_buffer[2] = { 0xAA, 0xBB } ;
    u_char  read_status_cmd[2] = { AT25_RDSR_CMD, 0x0 } ;
    u_int count ;

    for (count=5000; count > 0; count--);

    spi_eeprom_desc.spi_desc->spi_base->SP_RPR = (u_int)read_sr_buffer ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TPR = (u_int)read_status_cmd ;
    spi_eeprom_desc.spi_desc->spi_base->SP_RCR = 2 ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TCR = 2 ;

    for (count=5000; count > 0; count--);

    return ( read_sr_buffer[1] ) ;

//* End
}


//*--------------------------------------------------------------------------------------
//* Function Name       : at25_write_status_reg
//* Object              : Write a data to the Status Register
//* Input Parameters    : <status_reg_value> : Value to write to the status register
//* Return value        : None
//*--------------------------------------------------------------------------------------
void at25_write_status_reg ( u_char status_reg_value )
{
//* Begin
    u_char  write_status_cmd[2] = { AT25_WRSR_CMD, 0x0 } ;

    write_status_cmd[1] = status_reg_value ;

    spi_eeprom_desc.spi_desc->spi_base->SP_RPR = (u_int)write_status_cmd ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TPR = (u_int)write_status_cmd ;
    spi_eeprom_desc.spi_desc->spi_base->SP_RCR = 2 ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TCR = 2 ;

    at25_wait_ready () ;

//* End
}


//*--------------------------------------------------------------------------------------
//* Function Name       : at25_write_byte
//* Object              : Write a byte in memory
//* Input Parameters    : <address> : Address where to write
//*                     : <byte>    : Data to write
//* Return value        : None
//*--------------------------------------------------------------------------------------
void at25_write_byte ( u_short address, u_char byte )
{
//* Begin

    u_char  dummy_write_buffer[4] ;
    u_char  write_cmd[4] = { AT25_WRITE_CMD, 0x0, 0x0, 0x0 } ;

    //* Decompose <address> into 2 bytes
    write_cmd[1] = ((address & 0x0FF00) >> 8) ;
    write_cmd[2] = address & 0x0FF;
    write_cmd[3] = byte ;


    spi_eeprom_desc.spi_desc->spi_base->SP_RPR = (u_int)dummy_write_buffer ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TPR = (u_int)write_cmd ;
    spi_eeprom_desc.spi_desc->spi_base->SP_RCR = 4 ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TCR = 4 ;

    at25_wait_ready () ;

//* End
}


//*--------------------------------------------------------------------------------------
//* Function Name       : at25_write_page
//* Object              : Write a 64-byte (maximum) page in memory
//* Input Parameters    : <address> : First Address to write
//*                     : <nb_bytes>: Number of bytes to write from <address>
//*                     : <buffer>  : Pointer to a Buffer of data to write
//* Return value        : None
//*--------------------------------------------------------------------------------------
void at25_write_page ( u_short address, u_int nb_bytes, u_char *buffer )
{
//* Begin
    u_char  write_cmd[4] = { AT25_WRITE_CMD, 0x0, 0x0, 0x0 } ;

    //* decompose addr into 2 bytes
    write_cmd[1] = ((address & 0x0FF00) >> 8) ;
    write_cmd[2] = address & 0x0FF;
    write_cmd[3] = *buffer ;

    spi_eeprom_desc.spi_desc->spi_base->SP_TPR = (u_int)write_cmd ;
    spi_eeprom_desc.spi_desc->spi_base->SP_RCR = 0 ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TCR = nb_bytes ;

    at25_wait_ready () ;

//* End
}


//*--------------------------------------------------------------------------------------
//* Function Name       : at25_read_byte
//* Object              : Read a byte in memory
//* Input Parameters    : <address> : Address to read
//*                     : <*read_data> : Pointer to store the Data which has been read
//*
//* Return value        : None
//*--------------------------------------------------------------------------------------
void at25_read_byte ( u_short address, u_char *read_data )
{
//* Begin

    u_char  read_cmd[4] = { AT25_READ_CMD, 0x0, 0x0, 0x0 } ;
    u_char  read_byte_buffer[4] ;

    //* decompose addr into 2 bytes
    read_cmd[1] = ((address & 0x0FF00) >> 8) ;
    read_cmd[2] = address & 0x0FF;

    spi_eeprom_desc.spi_desc->spi_base->SP_RPR = (u_int)read_byte_buffer ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TPR = (u_int)read_cmd ;
    spi_eeprom_desc.spi_desc->spi_base->SP_RCR = 4 ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TCR = 4 ;

    at25_wait_ready () ;

    *read_data = read_byte_buffer[3] ;

//* End
}


//*--------------------------------------------------------------------------------------
//* Function Name       : at25_read_page
//* Object              : Read a 64-byte (maximum) page in memory
//* Input Parameters    : <address> : First Address to read
//*                     : <nb_bytes>: Number of bytes to read from <address>
//*                     : <buffer>  : Pointer to a Buffer of data to read
//*                     : <nb_bytes> : Number of bytes to read
//*
//* Return value        : Pointer to data
//*--------------------------------------------------------------------------------------
void at25_read_page ( u_short address, u_int nb_bytes, u_char *buffer )
{
//* Begin

    u_char  read_cmd[4] = { AT25_READ_CMD, 0x0, 0x0, 0x0 } ;

    //* decompose addr into 2 bytes
    read_cmd[1] = ((address & 0x0FF00) >> 8) ;
    read_cmd[2] = address & 0x0FF;

    spi_eeprom_desc.spi_desc->spi_base->SP_RPR = (u_int)buffer ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TPR = (u_int)read_cmd ;
    spi_eeprom_desc.spi_desc->spi_base->SP_RCR = 4 ;
    spi_eeprom_desc.spi_desc->spi_base->SP_TCR = 4 ;

    at25_wait_ready () ;


//* End
}

⌨️ 快捷键说明

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