From: Keith Packard Date: Mon, 8 Jan 2024 00:47:47 +0000 (-0800) Subject: altos/stm32f1: Add eeprom emulation using flash X-Git-Tag: 1.9.18~2^2~65 X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=commitdiff_plain;h=e959061fa17e15dfdd75d35c6c67e68a0e5e98e2 altos/stm32f1: Add eeprom emulation using flash Use the last 2kB of flash to emulate eeprom storage for TeleLCO. This should also be useful for EasyMega. Signed-off-by: Keith Packard --- diff --git a/src/stm32f1/altos.ld b/src/stm32f1/altos.ld index 15be8fae..85493115 100644 --- a/src/stm32f1/altos.ld +++ b/src/stm32f1/altos.ld @@ -16,8 +16,9 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -__flash = 0x08001000; -__flash_size = 60K; +__flash = 0x08001000; +__eeprom_base = 0x0800e800; +__flash_size = 58K; __ram = 0x20000000; __ram_size = 20k; __stack_size = 512; diff --git a/src/stm32f1/ao_eeprom.c b/src/stm32f1/ao_eeprom.c new file mode 100644 index 00000000..ec6654d5 --- /dev/null +++ b/src/stm32f1/ao_eeprom.c @@ -0,0 +1,50 @@ +/* + * Copyright © 2024 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#include +#include + +extern uint8_t __eeprom_base[2048]; + +uint8_t +ao_eeprom_read(ao_pos_t pos, void *v, uint16_t len) +{ + memcpy(v, &__eeprom_base[pos], len); + return 1; +} + +uint8_t +ao_eeprom_write(ao_pos_t pos, void *v, uint16_t len) +{ + bool hsi_on = (stm_rcc.cr & (1UL << STM_RCC_CR_HSIRDY)) != 0; + + if (!hsi_on) { + stm_rcc.cr |= (1UL << STM_RCC_CR_HSION); + while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY))) + ao_arch_nop(); + + } + ao_flash_bytes(&__eeprom_base[pos], v, len); + + if (!hsi_on) { + stm_rcc.cr &= ~(1UL << STM_RCC_CR_HSION); + } + + return 1; +} + diff --git a/src/stm32f1/ao_flash.h b/src/stm32f1/ao_flash.h index d191d80d..9937a51f 100644 --- a/src/stm32f1/ao_flash.h +++ b/src/stm32f1/ao_flash.h @@ -25,4 +25,7 @@ ao_flash_erase_page(uint32_t *page); void ao_flash_page(uint32_t *page, uint32_t *src); +void +ao_flash_bytes(void *page, void *src, size_t size); + #endif /* _AO_FLASH_STM_H_ */ diff --git a/src/stm32f1/ao_flash_stm.c b/src/stm32f1/ao_flash_stm.c index 48c702f7..103e83a5 100644 --- a/src/stm32f1/ao_flash_stm.c +++ b/src/stm32f1/ao_flash_stm.c @@ -104,13 +104,13 @@ ao_flash_erase_page(uint32_t *page) } static void __attribute__ ((section(".sdata2.flash"), noinline)) -_ao_flash_page(uint16_t *dst, uint16_t *src) +_ao_flash_page(uint16_t *dst, uint16_t *src, unsigned int shorts) { uint8_t i; stm_flash.cr |= (1 << STM_FLASH_CR_PG); - for (i = 0; i < 128; i++) { + for (i = 0; i < shorts; i++) { *dst++ = *src++; ao_flash_wait_bsy(); } @@ -126,7 +126,24 @@ ao_flash_page(uint32_t *page, uint32_t *src) ao_arch_block_interrupts(); ao_flash_unlock(); - _ao_flash_page((uint16_t *) page, (uint16_t *) src); + _ao_flash_page((uint16_t *) page, (uint16_t *) src, 128); + + ao_flash_lock(); + ao_arch_release_interrupts(); +} + +/* Stores less than a full page while still smashing the full page */ +void +ao_flash_bytes(void *page, void *src, size_t size) +{ + unsigned int shorts = (unsigned int) ((size + 1) >> 1); + + ao_flash_erase_page(page); + + ao_arch_block_interrupts(); + ao_flash_unlock(); + + _ao_flash_page((uint16_t *) page, (uint16_t *) src, shorts); ao_flash_lock(); ao_arch_release_interrupts();