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

📄 at45db321.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           : at45db321.c
//* Object              : Data Flash Atmel AT45DB321 Driver.
//*
//* 1.0 30/06/00 PF     : Creation
//*---------------------------------------------------------------------------

#include    "parts/m55800/lib_m55800.h"
#include    "drivers/serial_periph_1/serial_periph_1.h"
#include    "at45db321.h"

/* Global Variables */
SerialPeriph1DataDesc    spi_data ;

/* Data Flash Assembler Interrupt Handler */
extern void data_flash_asm_handler (void) ;

/* Mount a Serial Peripheral 1Desc as the as Data Flash */
SerialPeriph1Desc   data_flash_desc =
{
    &SPI_DESC,                      /* SPI Mode Register */
    &spi_data,
    0,                              /* SPI pin mode */
    SP_CPOL|(7<<16)|(50<<8),        /* Peripheral Chip Select Register */
    SPI_NPCS2_USED,             /* Data Flash connected on NPCS1 */
    data_flash_asm_handler,         /* Assembler Handler */
    0
} ;

//*-----------------------------------------------------------------------------
//* Function Name       : db321_open
//* Object              : Open the Data Flash connection
//* Input Parameters    :
//* Output Parameters   : The Data Flash status register
//* Functions called    :
//*-----------------------------------------------------------------------------
u_int db321_open ( void )
//* Begin
{
    u_char  cmd[2] = {DB321_STATUS,0} ;
    u_int   time_out = 1000 ;
    u_int   i = 0 ;
    
    serial_periph_1_open ( &data_flash_desc ) ;
    
        spi_data.tx_cmd_pt = cmd ;
        spi_data.tx_cmd_size = 2 ;
        spi_data.rx_cmd_pt = cmd ;
        spi_data.rx_cmd_size = 2 ;
        spi_data.tx_data_size = 0 ;
        serial_periph_1_write ( &data_flash_desc ) ;
    
        while ( (serial_periph_1_get_status( &data_flash_desc ) != SERIAL_PERIPH_1_IDLE) &&  i < time_out )
        i++;
        
        //* If timeout
        if ( i < time_out )
        {
            return ( cmd[1] ) ;
        }
    
        else return ( FALSE ) ;
    }
//* End

//*-----------------------------------------------------------------------------
//* Function Name       : db321_close
//* Object              : Close the connection to a Data Flash
//* Input Parameters    : None
//* Output Parameters   : None
//* Functions called    :
//*-----------------------------------------------------------------------------
void db321_close ( void )
//* Begin
{
    //* Close the SPI connection
    serial_periph_1_close ( &data_flash_desc ) ;
}
//* End

//*-----------------------------------------------------------------------------
//* Function Name       : db321_read
//* Object              : Read data from the DB321 memory
//* Input Parameters    : <*src> = Pointer to Main memory page
//*                     : <*dest> = data buffer pointer
//*                     : <size> = data buffer size
//*-----------------------------------------------------------------------------
void db321_read ( u_char *src, u_char *dest, int size )
//* Begin
{
    u_char cmd[8] = { DB321_PAGE_READ, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF} ;

    spi_data.tx_cmd_pt = cmd ;
    spi_data.tx_cmd_size = 8 ;
    spi_data.rx_cmd_pt = cmd ;
    spi_data.rx_cmd_size = 8 ;

    //* If source does not fit a page start address
    if (( spi_data.tx_data_size = ((u_int) src % DB321_PAGE_SIZE )) != 0 )
    {
        //* Send first Read Command
        cmd[1] = (u_char)(((u_int)src & 0xFF0000)>>16) ;
        cmd[2] = (u_char)(((u_int)src & 0xFF00)>>8) ;
        cmd[3] = (u_char)((u_int)src & 0xFF) ;
        spi_data.rx_data_pt = dest ;
        spi_data.tx_data_pt = dest ;
        spi_data.rx_data_size = spi_data.tx_data_size  ;
        serial_periph_1_write ( &data_flash_desc ) ;

        //* Wait for the end of transmission
        while ( serial_periph_1_get_status ( &data_flash_desc ) != SERIAL_PERIPH_1_IDLE ) ;

        //* Update size, source and destination pointers
        size -= spi_data.tx_data_size ;
        dest += spi_data.tx_data_size ;
        src += spi_data.tx_data_size ;
    }

    while (( size-DB321_PAGE_SIZE ) > 0 )
    {
        //* Send a Read Command with Source Base Address
        cmd[0] = DB321_PAGE_READ ;
        cmd[1] = (u_char)(((u_int)src & 0xFF0000)>>16) ;
        cmd[2] = (u_char)(((u_int)src & 0xFF00)>>8) ;
        cmd[3] = (u_char)((u_int)src & 0xFF) ;
        spi_data.rx_data_pt = dest ;
        spi_data.tx_data_pt = dest ;
        spi_data.tx_data_size  = DB321_PAGE_SIZE ;
        spi_data.rx_data_size = DB321_PAGE_SIZE ;
        serial_periph_1_write ( &data_flash_desc ) ;

        //* Wait for the end of transmission
        while ( serial_periph_1_get_status ( &data_flash_desc ) != SERIAL_PERIPH_1_IDLE ) ;

        //* Update size, source and destination pointers
        size -= DB321_PAGE_SIZE ;
        dest += DB321_PAGE_SIZE ;
        src += DB321_PAGE_SIZE ;
    }

    //* If still some bytes to read
    if (( spi_data.tx_data_size = size ) > 0 )
    {
        //* Send a Read Command with Source Base Address
        cmd[0] = DB321_PAGE_READ ;
        cmd[1] = (u_char)(((u_int)src & 0xFF0000)>>16) ;
        cmd[2] = (u_char)(((u_int)src & 0xFF00)>>8) ;
        cmd[3] = (u_char)((u_int)src & 0xFF) ;
        spi_data.rx_data_pt = dest ;
        spi_data.tx_data_pt = dest ;
        spi_data.rx_data_size = spi_data.tx_data_size ;
        serial_periph_1_write ( &data_flash_desc ) ;

        serial_periph_1_write ( &data_flash_desc ) ;

        //* Wait for the end of transmission
        while ( serial_periph_1_get_status ( &data_flash_desc ) != SERIAL_PERIPH_1_IDLE ) ;
    }
}
//* End

//*-----------------------------------------------------------------------------
//* Function Name       : db321_write_buffer
//* Object              : Write data to the memory
//* Input Parameters    : <*src> = Data buffer
//*                     : <*dest> = Buffer 1 or buffer 2 of the memory
//*                     : <size> = data buffer size
//*-----------------------------------------------------------------------------
void db321_write_buffer ( u_char *src, u_char *dest, int size )
//* Begin
{
    u_char cmd[4] = { DB321_BUF1_WRITE, 0, 0, 0 } ;

    spi_data.tx_cmd_pt = cmd ;
    spi_data.tx_cmd_size = 4 ;
    spi_data.rx_cmd_pt = cmd ;
    spi_data.rx_cmd_size = 4 ;

    //* Send first Read Command
    cmd[1] = (u_char)(((u_int)dest & 0xFF0000)>>16) ;
    cmd[2] = (u_char)(((u_int)dest & 0xFF00)>>8) ;
    cmd[3] = (u_char)((u_int)dest & 0xFF) ;
    spi_data.rx_data_pt = src ;
    spi_data.tx_data_pt = src ;
    spi_data.rx_data_size = size ;
    spi_data.tx_data_size = size ;
    serial_periph_1_write ( &data_flash_desc ) ;

    //* Wait for the end of transmission
    while ( serial_periph_1_get_status ( &data_flash_desc ) != SERIAL_PERIPH_1_IDLE ) ;
}


//*-----------------------------------------------------------------------------
//* Function Name       : db321_read_buffer
//* Object              : Read data from the SPI memory
//* Input Parameters    : <*src> = source buffer
//*                     : <*dest> = destination buffer
//*                     : <size> = data buffer size
//*-----------------------------------------------------------------------------
void db321_read_buffer ( u_char *src, u_char *dest, int size )
//* Begin
{
    u_char cmd[5] = { DB321_BUF1_READ, 0, 0, 0, 0 } ;

    spi_data.tx_cmd_pt = cmd ;
    spi_data.tx_cmd_size = 5 ;
    spi_data.rx_cmd_pt = cmd ;
    spi_data.rx_cmd_size = 5 ;

    cmd[1] = (u_char)(((u_int)src & 0xFF0000)>>16) ;
    cmd[2] = (u_char)(((u_int)src & 0xFF00)>>8) ;
    cmd[3] = (u_char)((u_int)src & 0xFF) ;
    spi_data.rx_data_pt = dest ;
    spi_data.tx_data_pt = dest ;
    spi_data.rx_data_size = size ;
    spi_data.tx_data_size = size ;
    serial_periph_1_write ( &data_flash_desc ) ;

    //* Wait for the end of transmission
    while ( serial_periph_1_get_status ( &data_flash_desc ) != SERIAL_PERIPH_1_IDLE ) ;

}

//*-----------------------------------------------------------------------------
//* Function Name       : db321_write
//* Object              : Write data from the SPI
//* Input Parameters    : <*src> = Source buffer
//*                     : <data> = data buffer pointer
//*                     : <size> = data buffer size
//*-----------------------------------------------------------------------------
void db321_write ( u_char *src, u_char *dest, int size )
//* Begin
{
    u_char cmd[4] = { DB321_BUF1_WRITE, 0, 0, 0 } ;

    spi_data.tx_cmd_pt = cmd ;
    spi_data.tx_cmd_size = 4 ;
    spi_data.rx_cmd_pt = cmd ;
    spi_data.rx_cmd_size = 4 ;

    //* Send first Read Command
    cmd[1] = (u_char)(((u_int)dest & 0xFF0000)>>16) ;
    cmd[2] = (u_char)(((u_int)dest & 0xFF00)>>8) ;
    cmd[3] = (u_char)((u_int)dest & 0xFF) ;
    spi_data.rx_data_pt = src ;
    spi_data.tx_data_pt = src ;
    spi_data.rx_data_size = 0 ;
    spi_data.tx_data_size = size ;
    serial_periph_1_write ( &data_flash_desc ) ;

    //* Wait for the end of transmission
    while ( serial_periph_1_get_status ( &data_flash_desc ) != SERIAL_PERIPH_1_IDLE ) ;

    //* Update size, source and destination pointers
    size -= spi_data.tx_data_size ;
    dest += spi_data.tx_data_size ;
    src += spi_data.tx_data_size ;


#ifdef NEVER
    //* If source does not fit a page start address
    if (( spi_data.tx_data_size = ((u_int) src % DB321_PAGE_SIZE )) != 0 )
    {

    while (( size-DB321_PAGE_SIZE ) > 0 )
    {
        //* Send a Read Command with Source Base Address
        cmd[0] = DB321_PAGE_READ ;
        cmd[1] = (u_char)(((u_int)src & 0xFF0000)>>16) ;
        cmd[2] = (u_char)(((u_int)src & 0xFF00)>>8) ;
        cmd[3] = (u_char)((u_int)src & 0xFF) ;
        spi_data.rx_data_pt = dest ;
        spi_data.tx_data_pt = dest ;
        spi_data.tx_data_size  = DB321_PAGE_SIZE ;
        spi_data.rx_data_size = DB321_PAGE_SIZE ;
        serial_periph_1_write ( &data_flash_desc ) ;

        //* Wait for the end of transmission
        while ( serial_periph_1_get_status ( &data_flash_desc ) != SERIAL_PERIPH_1_IDLE ) ;

        //* Update size, source and destination pointers
        size -= DB321_PAGE_SIZE ;
        dest += DB321_PAGE_SIZE ;
        src += DB321_PAGE_SIZE ;
    }

    //* If still some bytes to read
    if (( spi_data.tx_data_size = size ) > 0 )
    {
        //* Send a Read Command with Source Base Address
        cmd[0] = DB321_PAGE_READ ;
        cmd[1] = (u_char)(((u_int)src & 0xFF0000)>>16) ;
        cmd[2] = (u_char)(((u_int)src & 0xFF00)>>8) ;
        cmd[3] = (u_char)((u_int)src & 0xFF) ;
        spi_data.rx_data_pt = dest ;
        spi_data.tx_data_pt = dest ;
        spi_data.rx_data_size = spi_data.tx_data_size ;
        serial_periph_1_write ( &data_flash_desc ) ;

        serial_periph_1_write ( &data_flash_desc ) ;

        //* Wait for the end of transmission
        while ( serial_periph_1_get_status ( &data_flash_desc ) != SERIAL_PERIPH_1_IDLE ) ;
    }
#endif
}
//* End

⌨️ 快捷键说明

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