SciFi (03.01.2012 18:22, просмотров: 69) ответил Vladimir Ljaschko на Кто EEPROM в STM8 юзал? Посоветуйте глобально
Ответ очевиден: зависит от применения. Баланс между простотой и объёмом кода, временем записи, долговечностью памяти. Цена вопроса копеечная, в следующем проекте можно сделать по-другому. Я сделал блоками. Код приложен, авось пригодится. /**
* @file flash.c
* @brief Flash memory driver
*
* Implements 2 functions: block erase and block write. Block size is 64 bytes.
* This is done to minimize EEPROM programming time.
* Since programming code must be executed from RAM, the machine code is in the
* form of static arrays with corresponding assembly and C code shown in
* comments.
*/
#include "flash.h"
#include "stm8regs.h"
/*
void
block_erase(uint8_t *dst)
{
FLASH_CR2 = 0x20;
FLASH_NCR2 = 0xDF;
*dst++ = 0;
*dst++ = 0;
*dst++ = 0;
*dst = 0;
}
*/
static uint8_t block_erase_code[] = {
0x35, 0x20, 0x50, 0x5B, /* MOV FLASH_CR2, #0x20 */
0x35, 0xDF, 0x50, 0x5C, /* MOV FLASH_NCR2, #0xDF */
0x4F, /* CLR A */
0xF7, /* LD (X), A */
0x5C, /* INCW X */
0xF7, /* LD (X), A */
0x5C, /* INCW X */
0xF7, /* LD (X), A */
0x5C, /* INCW X */
0xF7, /* LD (X), A */
0x81, /* RET */
};
static void (*const block_erase)(uint8_t *dst) = (void (*)(uint8_t *dst))block_erase_code;
/*
void
block_write(uint8_t *src, uint8_t *dst)
{
int8_t i;
FLASH_CR2 = 0x10;
FLASH_NCR2 = 0xEF;
for (i = 64; i > 0; i--)
{
*dst++ = *src++;
}
}
*/
static uint8_t block_write_code[] = {
0x35, 0x10, 0x50, 0x5B, /* MOV FLASH_CR2, #0x10 */
0x35, 0xEF, 0x50, 0x5C, /* MOV FLASH_NCR2, #0xDF */
0x35, 0x40, 0x00, 0x00, /* MOV ?b0, #0x40 */
0xF6, /* loop: LD A, (X) */
0x90, 0xF7, /* LD (Y), A */
0x5C, /* INCW X */
0x90, 0x5C, /* INCW Y */
0xB6, 0x00, /* LD A, ?b0 */
0x4A, /* DEC A */
0xB7, 0x00, /* LD ?b0, A */
0x26, 0xF3, /* JRNE loop */
0x81, /* RET */
};
static void (*const block_write)(uint8_t *src, uint8_t *dst) = (void (*)(uint8_t *src, uint8_t *dst))block_write_code;
static uint8_t block_buffer[64];
/**
* Unlock data EEPROM
*/
void
flash_init(void)
{
FLASH_DUCR = 0xAE;
FLASH_DUCR = 0x56;
}
/**
* Get a pointer to block buffer.
* Fill this buffer prior to calling flash_writeblock().
*/
void*
flash_getbuffer(void)
{
return block_buffer;
}
/**
* Erase a 64-byte block
*/
void
flash_eraseblock(void* addr)
{
block_erase(addr);
}
/**
* Write the contents of block_buffer into a 64-byte block
*/
void
flash_writeblock(void* addr)
{
block_write(block_buffer, addr);
}
ส็็็็็็็็็็็็็็็็็็็็็็็็็༼ ຈل͜ຈ༽ส้้้้้้้้้้้้้้้้้้้้้้้