📄 at25010.c
字号:
/*C**************************************************************************
* NAME: at25010.c
*----------------------------------------------------------------------------
* Copyright (c) 2002 Atmel.
*----------------------------------------------------------------------------
* RELEASE: ix2-lib-spi-1_2_0
* REVISION: 1.2
*----------------------------------------------------------------------------
* PURPOSE:
* This file provides all functions to write and read data from an ATMEL AT25010 SPI EEPROM.
* All functions are based on SPI library for T89C51RC/RB2 products
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h"
/*_____ M A C R O S ________________________________________________________*/
/*_____ D E F I N I T I O N ________________________________________________*/
/*_____ D E C L A R A T I O N ______________________________________________*/
/*_____ D E F I N I T I O N ________________________________________________*/
/*F**************************************************************************
* NAME: at25010_read_status
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS: none
* return: status
*----------------------------------------------------------------------------
* PURPOSE:
* Read the status byte in SPI EEPROM
*----------------------------------------------------------------------------
* NOTE:
* If a current SPI transfert is on going, the function perform
* an active wait polling on SPI event
*****************************************************************************/
unsigned char at25010_read_status(void )
{
unsigned char spi_data;
AT25010_SET_CS;
spi_data=SPI_transmit_byte(AT25010_CMD_RDSR);
spi_data=SPI_transmit_byte(0x55);
spi_data=SPI_get_data();
AT25010_CLEAR_CS;
return spi_data;
}
/*F**************************************************************************
* NAME: at25010_set_write_enable_latch
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS: none
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Set write enable latch in SPI EEPROM
*----------------------------------------------------------------------------
* NOTE:
*****************************************************************************/
void at25010_set_write_enable_latch(void )
{
unsigned char spi_data;
AT25010_SET_CS;
spi_data=SPI_transmit_byte(AT25010_CMD_WREN);
spi_data=SPI_get_data();
AT25010_CLEAR_CS;
}
/*F**************************************************************************
* NAME: at25010_clear_write_enable_latch
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS: none
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Clear write enable latch in SPI EEPROM
*----------------------------------------------------------------------------
* NOTE:
*****************************************************************************/
void at25010_clear_write_enable_latch(void )
{
unsigned char spi_data;
AT25010_SET_CS;
spi_data=SPI_transmit_byte(AT25010_CMD_WRDI);
spi_data=SPI_get_data();
AT25010_CLEAR_CS;
}
/*F**************************************************************************
* NAME: at25010_write_byte
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS: adr: adress of byte to write
* to_write: data to write
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Write a byte in SPI EEPROM
*----------------------------------------------------------------------------
* NOTE:
*****************************************************************************/
void at25010_write_byte(unsigned char adr, unsigned char to_write )
{
unsigned char spi_data;
AT25010_SET_CS;
spi_data=SPI_transmit_byte(AT25010_CMD_WRITE);
spi_data=SPI_transmit_byte(adr);
spi_data=SPI_transmit_byte(to_write);
spi_data=SPI_get_data();
AT25010_CLEAR_CS;
}
/*F**************************************************************************
* NAME: at25010_read_byte
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS: adr: adress of byte to read
* return: data
*----------------------------------------------------------------------------
* PURPOSE:
* Read a byte in SPI EEPROM
*----------------------------------------------------------------------------
* NOTE:
*****************************************************************************/
unsigned char at25010_read_byte(unsigned char adr )
{
unsigned char spi_data;
AT25010_SET_CS;
spi_data=SPI_transmit_byte(AT25010_CMD_READ);
spi_data=SPI_transmit_byte(adr);
spi_data=SPI_transmit_byte(0xff);
spi_data=SPI_get_data();
AT25010_CLEAR_CS;
return spi_data;
}
/*F**************************************************************************
* NAME: at25010_ready
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS: none
* return: bit
*----------------------------------------------------------------------------
* PURPOSE:
* Test BUSY flag in SPI EEPROM
*----------------------------------------------------------------------------
* NOTE:
*****************************************************************************/
bit at25010_ready(void)
{
if ( (at25010_read_status() & AT25010_MSK_RDY) == AT25010_MSK_RDY )
return 0; //E2 write in progress ( busy ! )
else
return 1; //E2 ready ( OK )
}
/*F**************************************************************************
* NAME: at25010_write_burst_polling
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS: adr: adress of start first byte to write
* *ptr_buf: data buffer to write
* nb_data: number of data
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Write a string in SPI EEPROM
*----------------------------------------------------------------------------
* NOTE:
* This function allow to write more than 8 bytes in AT25010 SPI EEPROM.
* This function format the data stream to fit the AT25010 architecture.
*****************************************************************************/
void at25010_write_burst_polling( unsigned char adr, unsigned char *ptr_buf, unsigned char nb_data)
{
unsigned char nb_page;
unsigned char lack;
lack=0;
nb_page=0;
AT25010_SET_CS;
SPI_transmit_byte(AT25010_CMD_WRITE);
SPI_transmit_byte(adr);
SPI_get_data();
lack=(((adr/AT25010_MAX_PAGE_SIZE)+1)*AT25010_MAX_PAGE_SIZE)-adr;
if ( lack!=0 || lack!=AT25010_MAX_PAGE_SIZE )
{
SPI_transmit_burst_polling( ptr_buf,lack);
AT25010_CLEAR_CS;
ptr_buf+=lack;
nb_data-=lack;
while ( ~at25010_ready() ); //Wait for write process !
at25010_set_write_enable_latch();
AT25010_SET_CS;
SPI_transmit_byte(AT25010_CMD_WRITE);
SPI_transmit_byte(adr + nb_page*AT25010_MAX_PAGE_SIZE + lack);
SPI_get_data();
}
while ( nb_data > AT25010_MAX_PAGE_SIZE )
{
SPI_transmit_burst_polling( ptr_buf, AT25010_MAX_PAGE_SIZE);
AT25010_CLEAR_CS;
ptr_buf+=AT25010_MAX_PAGE_SIZE;
nb_data-=AT25010_MAX_PAGE_SIZE;
while ( ~at25010_ready() ); //Wait for write process !
nb_page++;
at25010_set_write_enable_latch();
AT25010_SET_CS;
SPI_transmit_byte(AT25010_CMD_WRITE);
SPI_transmit_byte(adr + nb_page*AT25010_MAX_PAGE_SIZE + lack);
SPI_get_data();
}
SPI_transmit_burst_polling( ptr_buf, nb_data);
AT25010_CLEAR_CS;
while ( ~at25010_ready() ); //Wait for write process !
}
/*F**************************************************************************
* NAME: at25010_read_burst_polling
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS: adr: adress of start first byte to read
* *ptr_buf: data buffer
* nb_data: number of data
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Read a string from SPI EEPROM in polling mode
*----------------------------------------------------------------------------
* NOTE:
*****************************************************************************/
void at25010_read_burst_polling( unsigned char adr, unsigned char *ptr_buf, unsigned char nb_data)
{
unsigned char spi_data;
AT25010_SET_CS;
SPI_transmit_byte(AT25010_CMD_READ);
SPI_transmit_byte(adr);
spi_data=SPI_get_data();
SPI_transmit_burst_polling( ptr_buf, nb_data);
AT25010_CLEAR_CS;
}
/*F**************************************************************************
* NAME: at25010_read_burst_interrupt
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS: adr: adress of start first byte to read
* *ptr_buf: data buffer
* nb_data: number of data
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Read a string from SPI EEPROM in interrupt mode
*----------------------------------------------------------------------------
* NOTE:
*****************************************************************************/
void at25010_read_burst_interrupt( unsigned char adr, unsigned char *ptr_buf, unsigned char nb_data)
{
unsigned char spi_data;
AT25010_SET_CS;
SPI_transmit_byte(AT25010_CMD_READ);
SPI_transmit_byte(adr);
spi_data=SPI_get_data();
if ( ~SPI_transmit_burst_it(ptr_buf, nb_data) )
{
printf("\SPI transfert on going");
}
while (b_SPI_busy);
AT25010_CLEAR_CS;
}
/*F**************************************************************************
* NAME: at25010_write_burst_interrupt
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS: adr: adress of start first byte to write
* *ptr_buf: data buffer
* nb_data: number of data
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* write a string from SPI EEPROM in interrupt mode
*----------------------------------------------------------------------------
* NOTE:
*****************************************************************************/
void at25010_write_burst_interrupt( unsigned char adr, unsigned char *ptr_buf, unsigned char nb_data)
{
AT25010_SET_CS;
at25010_set_write_enable_latch();
AT25010_SET_CS;
SPI_transmit_byte(AT25010_CMD_WRITE);
SPI_transmit_byte(adr);
SPI_transmit_burst_it(ptr_buf, nb_data);
while (b_SPI_busy);
AT25010_CLEAR_CS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -