altos/stm32f1: Add eeprom emulation using flash
authorKeith Packard <keithp@keithp.com>
Mon, 8 Jan 2024 00:47:47 +0000 (16:47 -0800)
committerKeith Packard <keithp@keithp.com>
Thu, 1 Feb 2024 01:50:19 +0000 (17:50 -0800)
Use the last 2kB of flash to emulate eeprom storage for TeleLCO. This should
also be useful for EasyMega.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/stm32f1/altos.ld
src/stm32f1/ao_eeprom.c [new file with mode: 0644]
src/stm32f1/ao_flash.h
src/stm32f1/ao_flash_stm.c

index 15be8fae0378434266da17eae130057b40780121..854931154a7e165134123f0fc38edc192a9c737f 100644 (file)
@@ -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 (file)
index 0000000..ec6654d
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright © 2024 Keith Packard <keithp@keithp.com>
+ *
+ * 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 <ao.h>
+#include <ao_flash.h>
+
+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;
+}
+
index d191d80d0d194452701bf98020dd886e4b8d1ffc..9937a51f17b37ac9a55cf3172ece04f00c5421b7 100644 (file)
@@ -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_ */
index 48c702f7a8d893a9e5af316d5323610326846b75..103e83a5e4b1df5892992736bbfe7d6aa124ef23 100644 (file)
@@ -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();