From 27f71ff5c093f740c78a28644e3be2843c660b7d Mon Sep 17 00:00:00 2001
From: NIIBE Yutaka <gniibe@fsij.org>
Date: Tue, 14 Jul 2015 16:10:07 +0900
Subject: New sys.c

---
 example-cdc/sample.ld |  8 +++-----
 example-cdc/sys.c     | 32 +++++++++++++++++++++-----------
 2 files changed, 24 insertions(+), 16 deletions(-)

(limited to 'example-cdc')

diff --git a/example-cdc/sample.ld b/example-cdc/sample.ld
index 55ad7aa..322165b 100644
--- a/example-cdc/sample.ld
+++ b/example-cdc/sample.ld
@@ -14,9 +14,6 @@ MEMORY
     ram : org = 0x20000000, len = 20k
 }
 
-__flash_start__ = 0x08001000;
-__flash_end__   = 0x08020000;
-
 __ram_start__           = ORIGIN(ram);
 __ram_size__            = 20k;
 __ram_end__             = __ram_start__ + __ram_size__;
@@ -25,13 +22,14 @@ SECTIONS
 {
     . = 0;
 
-    .sys : ALIGN(16) SUBALIGN(8)
+    .sys : ALIGN(4) SUBALIGN(4)
     {
 	_sys = .;
 	KEEP(*(.vectors))
 	. = ALIGN(16);
 	KEEP(*(.sys.version))
-	KEEP(*(.sys.board))
+	KEEP(*(.sys.board_id))
+	KEEP(*(.sys.board_name))
 	build/sys.o(.text)
 	build/sys.o(.text.*)
 	build/sys.o(.rodata)
diff --git a/example-cdc/sys.c b/example-cdc/sys.c
index 19c31b1..968b133 100644
--- a/example-cdc/sys.c
+++ b/example-cdc/sys.c
@@ -243,16 +243,19 @@ flash_check_blank (const uint8_t *p_start, size_t size)
   return 1;
 }
 
-extern uint8_t __flash_start__, __flash_end__;
+#define FLASH_START_ADDR 0x08000000 /* Fixed for all STM32F1.  */
+#define FLASH_OFFSET     0x1000     /* First pages are not-writable.  */
+#define FLASH_START      (FLASH_START_ADDR+FLASH_OFFSET)
+#define CHIP_ID_REG      ((uint32_t *)0xe0042000)
+#define FLASH_SIZE_REG   ((uint16_t *)0x1ffff7e0)
 
 static int
 flash_write (uint32_t dst_addr, const uint8_t *src, size_t len)
 {
   int status;
-  uint32_t flash_start = (uint32_t)&__flash_start__;
-  uint32_t flash_end = (uint32_t)&__flash_end__;
+  uint32_t flash_end = FLASH_START + (*FLASH_SIZE_REG)*1024;
 
-  if (dst_addr < flash_start || dst_addr + len > flash_end)
+  if (dst_addr < FLASH_START || dst_addr + len > flash_end)
     return 0;
 
   while (len)
@@ -305,17 +308,21 @@ flash_protect (void)
 static void __attribute__((naked))
 flash_erase_all_and_exec (void (*entry)(void))
 {
-  uint32_t addr = (uint32_t)&__flash_start__;
-  uint32_t end = (uint32_t)&__flash_end__;
+  uint32_t addr = FLASH_START;
+  uint32_t end = FLASH_START + (*FLASH_SIZE_REG)*1024;
+  uint32_t page_size = 1024;
   int r;
 
+  if (((*CHIP_ID_REG) & 0xfff) == 0x0414)
+    page_size = 2048;
+
   while (addr < end)
     {
       r = flash_erase_page (addr);
       if (r != 0)
 	break;
 
-      addr += FLASH_PAGE_SIZE;
+      addr += page_size;
     }
 
   if (addr >= end)
@@ -416,9 +423,12 @@ handler vector[] __attribute__ ((section(".vectors"))) = {
 const uint8_t sys_version[8] __attribute__((section(".sys.version"))) = {
   3*2+2,	     /* bLength */
   0x03,		     /* bDescriptorType = USB_STRING_DESCRIPTOR_TYPE */
-  /* sys version: "2.0" */
-  '2', 0, '.', 0, '0', 0,
+  /* sys version: "2.1" */
+  '2', 0, '.', 0, '1', 0,
 };
 
-const uint8_t __attribute__((section(".sys.board")))
-sys_board[] = BOARD_NAME;
+const uint32_t __attribute__((section(".sys.board_id")))
+sys_board_id = BOARD_ID;
+
+const uint8_t __attribute__((section(".sys.board_name")))
+sys_board_name[] = BOARD_NAME;
-- 
cgit v1.2.3