打印

AT25DF641A总是读回0xFF

[复制链接]
535|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 whoisilang 于 2019-8-1 10:51 编辑

AT25DF641A与stm32f103的连接图应该是基本连接方式了(stm32f103中用到的是PB_15,PB_14,PB_13,PB_12分别对应接到AT25DF641A的DI,D0,CLK,CS),如后面的图所示。关键问题是写一个字节0x01数据再读回来的时候还是0xFF。代码用的是MBED中的Hotboards EEPROM修改后的(主要是原代码中没有AT25DF641A)

at25df641A.png (59.83 KB )

at25df641A.png

使用特权

评论回复
沙发
whoisilang|  楼主 | 2019-8-1 10:54 | 只看该作者
本帖最后由 whoisilang 于 2019-8-1 10:57 编辑
#include "Hotboards_eeprom.h"

#define READ     0x03
#define WRITE    0x02
#define WRDI     0x04
#define WREN     0x06
#define RDSR     0x05
#define WRSR     0x01
//<font color="#ff0000">PageSize和EepromSize是我修改过的地方</font>
const uint16_t PageSize[ ] = { 16,    16,  16,   16,   32,   32,   64,    64,    128,   256,    256,   256,   256,    256,    256,    256 };
const uint32_t EepromSize[ ] = { 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144,524288,1048576,2097152,4194304,8388608};

Hotboards_eeprom::Hotboards_eeprom( SPI &spi, PinName cs, uint8_t type )
    : _spi(spi), _cs_pin(cs)
{
    _cs_pin = cs;
    _type = type;
    _page = PageSize[ type ];
    _density = EepromSize[ type ];
}

/*
* just make sure the Cs pin is not clear
*/
void Hotboards_eeprom::init( void )
{
   _cs_pin = 1;

//        _cs_pin = 0;
//_spi.write( WREN );
//_cs_pin = 0;
//_spi.write( 0x01 );
//_spi.write( 0x00 );
//_cs_pin = 1;
}

/*
* write a single byte in a given eeprom address, this operation will take 5ms
* address: eeprom address where tha byte will be written
* data: the byte that will be written at the given address
*/
void Hotboards_eeprom::write( uint32_t address, uint8_t data )
{
    write( address, &data, 1 );
}

/*
* write a given number of bytes into the eeprom atarting at a given address
* address: eeprom address where tha byte will be written
* data: pointer to array of bytes that need to be written
* size: the number of bytes to write
*/
void Hotboards_eeprom::write( uint32_t address, uint8_t *data, uint16_t size )
{
    uint16_t temp;
    /* just to cover your ass, check if the address is a valid direction */
    if( ( address < _density ) && ( size != 0 ) )
    {
        /* check if the amount of bytes to be read from the requested address do not exced the eeprom
        memory map if so, then only read the bytes left*/
        if( ( address + size ) > _density )
        {
            size = _density - address;
        }

        /* Check how many bytes are left to be written within a single page */
        temp = _page - ( address % _page );
        if( temp >= size  )
        {
            /* Data lenght can be written witihn the current page */
            writePage( address, data, size );
        }
        else
        {
            /* Data can not be written within the current page, so write only the bytes left */
            writePage( address, data, temp );
            /* update pointer to data, lenght and addres */
            data = data + temp;
            size = size - temp;
            address = address + temp;
               
            /* Check if the remaining data is bigger than a page size */
            while( size > _page )
            {
                /* Write an entire page into eeprom */
                writePage( address, data, _page );
                size = size - _page;
                data = data + _page;
                address = address + _page;
            }
            /* Write one last time if there is still data to be written */
            if( size != 0 )
            {
                writePage( address, data, size );
            }
        }
    }
}

/*
* read a single byte from a given eeprom address
* address: eeprom address where the byte will be read
*/
uint8_t Hotboards_eeprom::read( uint32_t address )
{
    uint8_t data;
    read( address, &data, 1 );
    return data;  
}

/*
* read a given number of bytes from the eeprom starting at a given address
* address: eeprom address where the bytes will be read it
* data: pointer to array where data will be stored
* size: the number of bytes to read
*/
void Hotboards_eeprom::read( uint32_t address, uint8_t *data, uint16_t size )
{
    uint16_t i;
    /* just to cover your ass, check if the address is a valid direction */
    if( ( address < _density ) && ( size != 0 ) )
    {
        /* check if the amount of bytes to be read from the requested address do not exced the eeprom
        memory map if so, then only read the bytes left*/
        if( ( address + size ) > _density )
        {
            size = _density - address;
        }
        
        /* operation begins, select memory */
        _cs_pin = 0;
        
        /* Send command Read and address, depend on device is the number of address bytes neccesary */
        sendAddress( READ, address );

        for( i = 0 ; i<size ; i++ )
        {
            /* Read data from memory */
            *data = _spi.write( 0xAA );
            data++;
        };
        /* operation ended, unselect memory */
        _cs_pin = 1;
    }
}

void Hotboards_eeprom::sendAddress( uint8_t cmd, uint32_t address )
{
    if( _type < HT_EEPROM25xx_4Kb )
    {
        /* Send the command and one byte address */
        _spi.write( cmd );
        _spi.write( address );
    }
    else if( _type == HT_EEPROM25xx_4Kb )
    {
        /* Send the command coded in the MSB of the address and one LSB address */
        _spi.write( cmd | ( uint8_t )( address >> 8 ) );
        _spi.write( ( uint8_t )address );
    }
    else if( ( _type > HT_EEPROM25xx_4Kb ) && ( _type < HT_EEPROM25xx_1Mb ) )
    {
        /* Send command plus two byte address */
        _spi.write( cmd );
        _spi.write( ( uint8_t )( address >> 8 ) );
        _spi.write( ( uint8_t )address );
    }
    else if( _type >= HT_EEPROM25xx_1Mb )  //<font color="#ff0000">这个地方也是我修改过的</font>
    {
        /* Send command plus three byte address */
        _spi.write( cmd );
        _spi.write( ( uint8_t )( address >> 16 ) );
        _spi.write( ( uint8_t )( address >> 8 ) );
        _spi.write( ( uint8_t )address );
    }
}

void Hotboards_eeprom::writePage( uint32_t address, uint8_t *data, uint16_t size )
{
    uint16_t i;

    /* Select memory */
    _cs_pin = 0;  
    /* write eneable latch */
    _spi.write( WREN );
    /* Unselect eeprom */
    _cs_pin = 1;   
   
    /* Select memory */
    _cs_pin = 0;  
    /* Send comand write and address, depend on device is the number of address bytes neccesary */
    sendAddress( WRITE, address );
    /* Send data to eeprom */
    for( i = 0 ; i<size ; i++ )
    {
        /* Read data from memory */
        _spi.write( *data );
        data++;
    };
    /* Unselect eeprom */
    _cs_pin = 1;  
    /* wait until data has been recorded */
    wait( 0.006 );  
}

使用特权

评论回复
板凳
whoisilang|  楼主 | 2019-8-1 10:55 | 只看该作者
#ifndef Hotboards_eeprom_h
#define Hotboards_eeprom_h

#include "mbed.h"

//EEPROM size in kilobits. EEPROM part numbers are usually designated in k-bits.
typedef enum
{
       
    HT_EEPROM25xx_1Kb=0,
    HT_EEPROM25xx_2Kb,
    HT_EEPROM25xx_4Kb,
    HT_EEPROM25xx_8Kb,
    HT_EEPROM25xx_16Kb,
    HT_EEPROM25xx_32Kb,
    HT_EEPROM25xx_64Kb,
    HT_EEPROM25xx_128Kb,
    HT_EEPROM25xx_256Kb,
    HT_EEPROM25xx_512Kb,
    HT_EEPROM25xx_1Mb,
                HT_EEPROM25xx_2Mb,
                HT_EEPROM25xx_4Mb,
                HT_EEPROM25xx_8Mb       
}_eEEPROM;

class Hotboards_eeprom
{
    public :
        Hotboards_eeprom( SPI &spi, PinName cs, uint8_t type );
        void init( void );
        void write( uint32_t address, uint8_t data );
        void write( uint32_t address, uint8_t *data, uint16_t size );
        uint8_t read( uint32_t address );
        void read( uint32_t address, uint8_t *data, uint16_t size );
   
    protected :
        void sendAddress( uint8_t cmd, uint32_t address );
        void writePage( uint32_t address, uint8_t *data, uint16_t size );
   
        SPI _spi;   
        DigitalOut _cs_pin;
        uint8_t _type;
        uint16_t _page;
        uint32_t _density;
};

#endif

使用特权

评论回复
地板
whoisilang|  楼主 | 2019-8-1 17:30 | 只看该作者
问题找到了,因为没有0x39命令。结贴。

使用特权

评论回复
5
xixi2017| | 2019-8-5 23:31 | 只看该作者
发送命令错误啊?

使用特权

评论回复
6
zhuomuniao110| | 2019-8-5 23:57 | 只看该作者
这个芯片都搜不到,楼主应该先贡献一份手册。

使用特权

评论回复
7
alternate| | 2019-8-6 10:20 | 只看该作者
看来做事情之前还是要先好好看下手册

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

9

帖子

0

粉丝