Код
#define USE_STATUS_REG 0 //doesn't help any way
void LOGGER_PageWrite(uint32_t address, uint32_t length, uint8_t *buffer)
{
#if USE_STATUS_REG
uint32_t timer;
#endif
volatile uint32_t page_limit=0;
volatile uint32_t bytes_to_fit=0;
volatile uint32_t bytes_to_write=0;
//find a page limit for the address
while (address > page_limit)
{
page_limit += 64;
}
//actual page limit
page_limit--;
bytes_to_fit = page_limit - address;
bytes_to_write = bytes_to_fit;
// Note!!!Every write operation demands a write enable!!!
SPI_CSlow();
SPI_SendByte(SPIEEPROM_CMD_WREN);
SPI_CShigh();
// start write
SPI_CSlow();
// send write command
SPI_SendByte(SPIEEPROM_CMD_WRITE);
// send address
SPI_SendByte(address>>8);
SPI_SendByte(address&0x00FF);
while(bytes_to_write--)
{
// send data to be written
SPI_SendByte(*buffer++);
}
// stop write
SPI_CShigh();
#if USE_STATUS_REG
timer = LOGGER_DELAY;
//wait for write operation to complete
while(SPIEEPROM_ReadStatus() & SPIEEPROM_STATUS_WIP)
{
if(!--timer)
break;
}
#else
//needs 5ms for page write
Delay_us_sys(5000);
#endif
if (bytes_to_fit <= length)
{
address += bytes_to_fit+1;
bytes_to_write = length - bytes_to_fit;
// Note!!!Every write operation demands a write enable!!!
SPI_CSlow();
SPI_SendByte(SPIEEPROM_CMD_WREN);
SPI_CShigh();
// start write
SPI_CSlow();
// send write command
SPI_SendByte(SPIEEPROM_CMD_WRITE);
// send address
SPI_SendByte(address>>8);
SPI_SendByte(address&0xFF);
while(bytes_to_write--)
{
// send data to be written
SPI_SendByte(*buffer++);
}
// stop write
SPI_CShigh();
#if USE_STATUS_REG
timer = LOGGER_DELAY;
//wait for write operation to complete
while(SPIEEPROM_ReadStatus() & SPIEEPROM_STATUS_WIP)
{
if(!--timer)
break;
}
#else
//needs 5ms for page write
Delay_us_sys(5000);
#endif
}
}
void LOGGER_PageWrite(uint32_t address, uint32_t length, uint8_t *buffer)
{
#if USE_STATUS_REG
uint32_t timer;
#endif
volatile uint32_t page_limit=0;
volatile uint32_t bytes_to_fit=0;
volatile uint32_t bytes_to_write=0;
//find a page limit for the address
while (address > page_limit)
{
page_limit += 64;
}
//actual page limit
page_limit--;
bytes_to_fit = page_limit - address;
bytes_to_write = bytes_to_fit;
// Note!!!Every write operation demands a write enable!!!
SPI_CSlow();
SPI_SendByte(SPIEEPROM_CMD_WREN);
SPI_CShigh();
// start write
SPI_CSlow();
// send write command
SPI_SendByte(SPIEEPROM_CMD_WRITE);
// send address
SPI_SendByte(address>>8);
SPI_SendByte(address&0x00FF);
while(bytes_to_write--)
{
// send data to be written
SPI_SendByte(*buffer++);
}
// stop write
SPI_CShigh();
#if USE_STATUS_REG
timer = LOGGER_DELAY;
//wait for write operation to complete
while(SPIEEPROM_ReadStatus() & SPIEEPROM_STATUS_WIP)
{
if(!--timer)
break;
}
#else
//needs 5ms for page write
Delay_us_sys(5000);
#endif
if (bytes_to_fit <= length)
{
address += bytes_to_fit+1;
bytes_to_write = length - bytes_to_fit;
// Note!!!Every write operation demands a write enable!!!
SPI_CSlow();
SPI_SendByte(SPIEEPROM_CMD_WREN);
SPI_CShigh();
// start write
SPI_CSlow();
// send write command
SPI_SendByte(SPIEEPROM_CMD_WRITE);
// send address
SPI_SendByte(address>>8);
SPI_SendByte(address&0xFF);
while(bytes_to_write--)
{
// send data to be written
SPI_SendByte(*buffer++);
}
// stop write
SPI_CShigh();
#if USE_STATUS_REG
timer = LOGGER_DELAY;
//wait for write operation to complete
while(SPIEEPROM_ReadStatus() & SPIEEPROM_STATUS_WIP)
{
if(!--timer)
break;
}
#else
//needs 5ms for page write
Delay_us_sys(5000);
#endif
}
}
проверяю. пишу по середине большое количество байт чтоб перейти на другую страницу.
Код
uint8_t rxbuffer[50];
for (int i=0; i < 50; i++)
{
rxbuffer[i] = i+1;
}
LOGGER_PageWrite(80, 50, rxbuffer);
for (int i=0; i < 50; i++)
{
rxbuffer[i] = i+1;
}
LOGGER_PageWrite(80, 50, rxbuffer);
и потом считываю.
Код
uint8_t txbuffer[50];
LOGGER_Read(80, 50,txbuffer);
LOGGER_Read(80, 50,txbuffer);
результаты такие:
в адрессе 80-127 я вижу данные 1-47 и это правильно
в адрессе 128-130 я вижу /0, 48, 49
то есть первый адресс следующей страницы не прописывается. целый день бьюсь над проблемой не могу понять в чем дело.