很奇怪的,当写到地址为139920时,数据无法写入,每页是528字节,正好是265页开始处,无法写入,非常奇怪的,朋友们帮忙看看!
//
#define OVERTIMES 200
#define PAGE_SIZE 528L
#define W_BUF_SIZE 528L
#define R_BUF_SIZE 528L
#define BLOCK_SIZE 4224L
#define REG_STATUS 0xD7
#define REG_WRITE_BUFF_1 0x84
#define REG_BUFF_MAIN_1 0x83
#define REG_ARRAY_READ 0xE8
#define REG_PAGEERASE 0x81
#define REG_BLOCKERASE 0x50
#define REG_DEEPSLEEP 0xB9
#define REG_RESUME_DEEPSLEEP 0xAB
//PA.7
#define AT45DBXX_CS_L GPIOA_BSRR_bit.BR7 = 1
#define AT45DBXX_CS_H GPIOA_BSRR_bit.BS7 = 1
#define AT45DBXX_CS_OUT GPIOA_CRL_bit.MODE7 = 1 ;\
GPIOA_CRL_bit.CNF7 = 0
//spi.c
extern void spi_io_init (void) ;
extern u8_t spi_read_byte (void) ;
extern void spi_write_byte (u8_t byte) ;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
static void at45dbxx_io_init (void)
{
spi_io_init () ;
//CS disable
AT45DBXX_CS_H ;
AT45DBXX_CS_OUT ;
}
static u8_t at45dbxx_wait (void)
{
u8_t status, try_cnt = OVERTIMES ;
do
{
AT45DBXX_CS_L ;
spi_write_byte (REG_STATUS) ;
status = spi_read_byte () ;
AT45DBXX_CS_H ;
lib_delay_us (10) ;
}while ((!(status & BIT7)) && (--try_cnt)) ;
if (try_cnt)
return TRUE ;
else
return FALSE ;
}
static void at45dbxx_write_buf (u8_t *cmd, u8_t *buf, u16_t buf_len)
{
u8_t i ;
AT45DBXX_CS_L ;
for (i=0; i<4; i++)
spi_write_byte (*cmd++) ;
while (buf_len -- > 0)
spi_write_byte (*buf++) ;
AT45DBXX_CS_H ;
}
static void at45dbxx_read_buf (u8_t *cmd, u8_t *buf, u16_t buf_len)
{
u8_t i ;
AT45DBXX_CS_L ;
for (i=0; i<4; i++)
spi_write_byte (*cmd++) ;
for (i=0; i<4; i++)
spi_write_byte (0x00) ;
while (buf_len -- > 0)
*buf++ = spi_read_byte () ;
AT45DBXX_CS_H ;
}
static u8_t at45dbxxMainMemoryToBufferTransfert (u8_t page)
{
AT45DBXX_CS_L ;
spi_write_byte (0x53) ;
spi_write_byte ((page >> 6) & 0x7F) ;
spi_write_byte ((page << 2) & 0xFC) ;
spi_write_byte (0x00) ;
AT45DBXX_CS_H ;
if (at45dbxx_wait () != TRUE)
return FALSE ;
return TRUE ;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
u8_t flash_read_buf (u32_t addr, u8_t *p_buf, u16_t len)
{
u8_t cmd[4] ;
u16_t page, adrInPage ;
at45dbxx_io_init () ;
if (at45dbxx_wait () != TRUE)
goto fail_exit ;
page = addr / PAGE_SIZE ;
adrInPage = addr % PAGE_SIZE ;
//
cmd[0] = REG_ARRAY_READ ;
cmd[1] = ((u16_t)(page) >> 6) & 0x7F ;
cmd[2] = ((u16_t)(page) << 2) & 0xFC ;
cmd[2] |= (((u16_t)(adrInPage) >> 8) & 0x03) ;
cmd[3] = (u16_t)(adrInPage) & 0xFF ;
at45dbxx_read_buf (cmd, p_buf, len) ;
FLAG_ERR_FLASH = FALSE ;
return TRUE ;
fail_exit:
FLAG_ERR_FLASH = TRUE ;
return FALSE ;
}
u8_t flash_write_buf (u32_t addr, u8_t *p_buf, u16_t len)
{
u8_t cmd[4] ;
u16_t w_len, page, adrInPage ;
at45dbxx_io_init () ;
if (at45dbxx_wait () != TRUE)
goto fail_exit ;
while (len > 0)
{
page = addr / PAGE_SIZE ;
adrInPage = addr % PAGE_SIZE ;
if (adrInPage)
{
if (at45dbxxMainMemoryToBufferTransfert (page) != TRUE)
return FALSE ;
w_len = PAGE_SIZE - (addr % PAGE_SIZE) ;
if (len < w_len)
w_len = len ;
}
else
{
if (len <= PAGE_SIZE)
w_len = len ;
else
w_len = PAGE_SIZE ;
}
//write buffer 1
cmd[0] = 0x84 ;
cmd[1] = 0x00;
cmd[2] = (adrInPage >> 8) ;
cmd[3] = adrInPage & 0xFF ;
at45dbxx_write_buf (cmd, p_buf, w_len) ;
if (at45dbxx_wait () != TRUE)
goto fail_exit ;
//write mainPage 1
cmd[0] = 0x83 ;
cmd[1] = (page >> 6) ;
cmd[2] = (page << 2) ;
cmd[3] = 0x00 ;
at45dbxx_write_buf (cmd, NULL, 0) ;
lib_delay_ms (40) ;
if (at45dbxx_wait () != TRUE)
goto fail_exit ;
p_buf += w_len ;
len -= w_len ;
addr += w_len ;
}
FLAG_ERR_FLASH = FALSE ;
return TRUE ;
fail_exit:
FLAG_ERR_FLASH = TRUE ;
return FALSE ;
}
|