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

📄 at45db161.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           : at45db161.c
//* Object              : Data Flash Atmel AT45DB161 Driver.
//*
//* 1.0 04/01/00 JCZ    : Creation
//* 1.1 30/06/00 PF		: Clean Up
//*---------------------------------------------------------------------------

#include    "parts/m63200/lib_m63200.h"
#include    "drivers/serial_periph_1/serial_periph_1.h"
#include    "at45db161.h"

/* Timeout loop count */
#define TIME_OUT            10000

/* 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_data,
    0,                              /* SPI Mode Register */
    SP_CPOL|(7<<16)|(50<<8),        /* Peripheral Chip Select Register */
    SPI_NPCS0_USED,                 /* SPI pin mode */
    data_flash_asm_handler,         /* Assembler Handler */
    0                               /* Data Flash connected on NPCS0 */
} ;

//*-----------------------------------------------------------------------------
//* Function Name       : db161_open
//* Object              : Open the Data Flash connection
//* Input Parameters    :
//* Output Parameters   : The Data Flash status register
//* Functions called    :
//*-----------------------------------------------------------------------------
u_int db161_open ( void )
//* Begin
{
    u_int		time_out = 1000 ;
	u_char 		cmd[2] = {DB161_STATUS, 0} ;
	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       : db161_close
//* Object              : Close the connection to a Data Flash
//* Input Parameters    : None
//* Output Parameters   : None
//* Functions called    :
//*-----------------------------------------------------------------------------
void db161_close ( void )
//* Begin
{
    //* Close the SPI connection
    serial_periph_1_close ( &data_flash_desc ) ;
}
//* End

//*-----------------------------------------------------------------------------
//* Function Name       : db161_read
//* Object              : Read data from the DB161 memory
//* Input Parameters    : <*src> = Pointer to Main memory page
//*                     : <*dest> = data buffer pointer
//*                     : <size> = data buffer size
//*-----------------------------------------------------------------------------
void db161_read ( u_char *src, u_char *dest, int size )
//* Begin
{
    u_char cmd[8] = { DB161_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 % DB161_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-DB161_PAGE_SIZE ) > 0 )
    {
        //* Send a Read Command with Source Base Address
        cmd[0] = DB161_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  = DB161_PAGE_SIZE ;
        spi_data.rx_data_size = DB161_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 -= DB161_PAGE_SIZE ;
        dest += DB161_PAGE_SIZE ;
        src += DB161_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] = DB161_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       : db161_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 db161_write_buffer ( u_char *src, u_char *dest, int size )
//* Begin
{
    u_char cmd[4] = { DB161_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       : db161_read_buffer
//* Object              : Write data from the SPI
//* Input Parameters    : <*src> = source buffer
//*                     : <data> = data buffer pointer
//*                     : <size> = data buffer size
//*-----------------------------------------------------------------------------
void db161_read_buffer ( u_char *src, u_char *dest, int size )
//* Begin
{
    u_char cmd[5] = { DB161_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       : db161_write
//* Object              : Write data from the SPI
//* Input Parameters    : <*src> = Source buffer
//*                     : <*dest> = data buffer pointer
//*                     : <size> = data buffer size
//*-----------------------------------------------------------------------------
void db161_write ( u_char *src, u_char *dest, int size )
//* Begin
{
    u_char cmd[4] = { DB161_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 % DB161_PAGE_SIZE )) != 0 )
    {

    while (( size-DB161_PAGE_SIZE ) > 0 )
    {
        //* Send a Read Command with Source Base Address
        cmd[0] = DB161_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  = DB161_PAGE_SIZE ;
        spi_data.rx_data_size = DB161_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 -= DB161_PAGE_SIZE ;
        dest += DB161_PAGE_SIZE ;
        src += DB161_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] = DB161_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 + -