.

Files at this revision

API Documentation at this revision

Comitter:
va009039
Date:
Sat Apr 09 15:30:12 2016 +0900
Parent:
1:dee9fae0c0c4
Commit message:
add Nucleo-F401RE.

Changed in this revision

ROMSLOT.h Show annotated file Show diff for this revision Revisions of this file
ROMSLOT_LPC1768.cpp Show annotated file Show diff for this revision Revisions of this file
ROMSLOT_STM32.cpp Show diff for this revision Revisions of this file
ROMSLOT_STM32F4.cpp Show annotated file Show diff for this revision Revisions of this file
ROMSLOT_STM32L1.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r dee9fae0c0c4 -r ecbff7f1e453 ROMSLOT.h
--- a/ROMSLOT.h	Fri Apr 08 06:47:00 2016 +0900
+++ b/ROMSLOT.h	Sat Apr 09 15:30:12 2016 +0900
@@ -1,7 +1,12 @@
-// ROMSLOT.h 2016/4/6
+// ROMSLOT.h 2016/4/9
 #include "mbed.h"
 
 class ROMSLOT {
 public:
-    static bool Write(uint32_t addr, const uint8_t* buf, uint32_t size);
+    ROMSLOT();
+    uint32_t New(uint32_t size);
+    bool Write(uint32_t addr, const uint8_t* buf, uint32_t size);
+
+private:
+    uint32_t base;
 };
diff -r dee9fae0c0c4 -r ecbff7f1e453 ROMSLOT_LPC1768.cpp
--- a/ROMSLOT_LPC1768.cpp	Fri Apr 08 06:47:00 2016 +0900
+++ b/ROMSLOT_LPC1768.cpp	Sat Apr 09 15:30:12 2016 +0900
@@ -1,7 +1,10 @@
-// ROMSLOT_LPC1768.cpp 2016/4/7
+// ROMSLOT_LPC1768.cpp 2016/4/9
 #if defined(TARGET_LPC1768)
 #include "ROMSLOT.h"
 
+static const uint32_t FLASH_SECTOR_BASE = 0x10000;
+static const uint32_t FLASH_SECTOR_SIZE = 0x8000; // 32 Kbytes
+
 #if defined(IAP_DEBUG)
 #define IAP_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0)
 #else
@@ -22,9 +25,28 @@
     return result[0];
 }
 
+ROMSLOT::ROMSLOT() {
+    base = FLASH_SECTOR_BASE;
+}
+
+uint32_t ROMSLOT::New(uint32_t size) {
+    uint32_t addr = base;
+    base += (size + FLASH_SECTOR_SIZE - 1) / FLASH_SECTOR_SIZE * FLASH_SECTOR_SIZE;
+    return addr;
+}
+
+static bool is_base(uint32_t addr) {
+    return addr % FLASH_SECTOR_SIZE == 0;
+}
+
+static uint32_t sector(uint32_t addr) {
+    MBED_ASSERT(addr >= FLASH_SECTOR_BASE);
+    uint32_t s = (addr - FLASH_SECTOR_BASE) / FLASH_SECTOR_SIZE + 16;
+    return s;
+}
+
 bool ROMSLOT::Write(uint32_t addr, const uint8_t buf[], uint32_t size) {
     IAP_ASSERT(addr%256 == 0);
-    IAP_ASSERT((uint32_t)buf%4 == 0);
     IAP_ASSERT(size == 256);
     if (memcmp((uint8_t*)addr, buf, size) == 0) { // skip ?
         return true;
@@ -32,24 +54,24 @@
     uint8_t temp[256];
     memcpy(temp, buf, sizeof(temp));
     IAP_ASSERT(addr >= 0x10000);
-    uint32_t sector = (addr - 0x10000) / 0x8000 + 16;
-    IAP_ASSERT(sector >= 16);
+    uint32_t sectorNo = sector(addr);
+    IAP_ASSERT(sectorNo >= 16);
     uint32_t cclk = SystemCoreClock / 1000;
-    if (addr%0x8000 == 0) {
-        if (iap(50, sector, sector) != 0) { // prepare
+    if (is_base(addr)) {
+        if (iap(50, sectorNo, sectorNo) != 0) { // prepare
             return false;
         }
-        if (iap(52, sector, sector, cclk) != 0) { // erase
+        if (iap(52, sectorNo, sectorNo, cclk) != 0) { // erase
             return false;
         }
     }
-    if (iap(50, sector, sector) != 0) { // prepare
+    if (iap(50, sectorNo, sectorNo) != 0) { // prepare
         return false;
     }
-    if (iap(51, (uint32_t)addr, (uint32_t)temp, size, cclk) != 0) { // Copy RAM to flash
+    if (iap(51, addr, (uint32_t)temp, size, cclk) != 0) { // Copy RAM to flash
         return false;
     }
-    if (iap(56, (uint32_t)addr, (uint32_t)temp, size) != 0) { // Compare
+    if (iap(56, addr, (uint32_t)temp, size) != 0) { // Compare
         return false;
     }
     return true;
diff -r dee9fae0c0c4 -r ecbff7f1e453 ROMSLOT_STM32.cpp
--- a/ROMSLOT_STM32.cpp	Fri Apr 08 06:47:00 2016 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-// ROMSLOT_STM32.cpp 2016/4/7
-#if defined(TARGET_STM)
-#include "ROMSLOT.h"
-
-bool ROMSLOT::Write(uint32_t addr, const uint8_t buf[], uint32_t size) {
-    MBED_ASSERT(addr%FLASH_PAGE_SIZE == 0);
-    MBED_ASSERT(size%FLASH_PAGE_SIZE == 0);
-    if (memcmp((uint8_t*)addr, buf, size) == 0) { // skip ?
-        return true;
-    }
-    HAL_FLASH_Unlock();
-    bool result = true;
-    for(uint32_t n = 0; n < size; n += sizeof(uint32_t)) {
-        if (n % FLASH_PAGE_SIZE == 0) {
-            FLASH_EraseInitTypeDef Erase;
-            Erase.NbPages = 1;
-            Erase.TypeErase = TYPEERASEDATA_BYTE;
-            Erase.PageAddress = addr + n;
-            uint32_t PageError = 0;
-            HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&Erase, &PageError);
-            MBED_ASSERT(status == HAL_OK);
-            if (status != HAL_OK) {
-                result = false;
-                break;
-            }
-        }
-        uint32_t data = buf[n]|buf[n+1]<<8|buf[n+2]<<16|buf[n+3]<<24;
-        HAL_StatusTypeDef status = HAL_FLASH_Program(TYPEPROGRAM_WORD, addr + n, (uint64_t)data);
-        MBED_ASSERT(status == HAL_OK);
-        if (status != HAL_OK) {
-            result = false;
-            break;
-        }
-    }
-    HAL_FLASH_Lock();
-    return result;
-}
-#endif // TARGET_STM
-
diff -r dee9fae0c0c4 -r ecbff7f1e453 ROMSLOT_STM32F4.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ROMSLOT_STM32F4.cpp	Sat Apr 09 15:30:12 2016 +0900
@@ -0,0 +1,63 @@
+// ROMSLOT_STM32F4.cpp 2016/4/9
+#if defined(TARGET_STM32F4)
+#include "ROMSLOT.h"
+
+static const uint32_t FLASH_SECTOR_SIZE = 0x20000; // 128 Kbytes
+
+ROMSLOT::ROMSLOT() {
+    base = 0x20000;
+}
+
+uint32_t ROMSLOT::New(uint32_t size) {
+    uint32_t addr = base;
+    base += (size + FLASH_SECTOR_SIZE - 1) / FLASH_SECTOR_SIZE * FLASH_SECTOR_SIZE;
+    return addr;
+}
+
+static bool is_base(uint32_t addr) {
+    return addr % FLASH_SECTOR_SIZE == 0;
+}
+
+static uint32_t sector(uint32_t addr) {
+    MBED_ASSERT(addr >= 0x20000 && addr <= 0x60000);
+    uint32_t s = (addr - 0x20000) / FLASH_SECTOR_SIZE + 5; // sector 5,6 und 7
+    return s;
+}
+
+bool ROMSLOT::Write(uint32_t addr, const uint8_t buf[], uint32_t size) {
+    uint32_t data;
+    MBED_ASSERT(addr % sizeof(data) == 0);
+    MBED_ASSERT(size % sizeof(data) == 0);
+    if (memcmp((uint8_t*)addr, buf, size) == 0) { // skip ?
+        return true;
+    }
+    HAL_FLASH_Unlock();
+    bool result = true;
+    for(uint32_t n = 0; n < size; n += sizeof(data)) {
+        if (is_base(addr + n)) {
+            FLASH_EraseInitTypeDef Erase;
+            Erase.TypeErase = FLASH_TYPEERASE_SECTORS;
+            Erase.VoltageRange = FLASH_VOLTAGE_RANGE_1;
+            Erase.NbSectors = 1;
+            Erase.Sector = sector(addr + n);
+            uint32_t PageError = 0;
+            HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&Erase, &PageError);
+            MBED_ASSERT(status == HAL_OK);
+            if (status != HAL_OK) {
+                result = false;
+                break;
+            }
+        }
+        memcpy(&data, buf + n, sizeof(data));
+        HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr + n, data);
+        MBED_ASSERT(status == HAL_OK);
+        if (status != HAL_OK) {
+            result = false;
+            break;
+        }
+    }
+    HAL_FLASH_Lock();
+    return result;
+}
+#endif // TARGET_STM32F4
+
diff -r dee9fae0c0c4 -r ecbff7f1e453 ROMSLOT_STM32L1.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ROMSLOT_STM32L1.cpp	Sat Apr 09 15:30:12 2016 +0900
@@ -0,0 +1,54 @@
+// ROMSLOT_STM32L1.cpp 2016/4/9
+#if defined(TARGET_STM32L1)
+#include "ROMSLOT.h"
+
+ROMSLOT::ROMSLOT() {
+    base = 0x20000;
+}
+
+uint32_t ROMSLOT::New(uint32_t size) {
+    uint32_t addr = base;
+    base += (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE * FLASH_PAGE_SIZE;
+    return addr;
+}
+
+static bool is_base(uint32_t addr) {
+    return addr % FLASH_PAGE_SIZE == 0;
+}
+
+bool ROMSLOT::Write(uint32_t addr, const uint8_t buf[], uint32_t size) {
+    uint32_t data;
+    MBED_ASSERT(addr % sizeof(data) == 0);
+    MBED_ASSERT(size % sizeof(data) == 0);
+    if (memcmp((uint8_t*)addr, buf, size) == 0) { // skip ?
+        return true;
+    }
+    HAL_FLASH_Unlock();
+    bool result = true;
+    for(uint32_t n = 0; n < size; n += sizeof(data)) {
+        if (is_base(addr + n)) {
+            FLASH_EraseInitTypeDef Erase;
+            Erase.NbPages = 1;
+            Erase.TypeErase = TYPEERASEDATA_BYTE;
+            Erase.PageAddress = addr + n;
+            uint32_t PageError = 0;
+            HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&Erase, &PageError);
+            MBED_ASSERT(status == HAL_OK);
+            if (status != HAL_OK) {
+                result = false;
+                break;
+            }
+        }
+        memcpy(&data, buf + n, sizeof(data));
+        HAL_StatusTypeDef status = HAL_FLASH_Program(TYPEPROGRAM_WORD, addr + n, (uint64_t)data);
+        MBED_ASSERT(status == HAL_OK);
+        if (status != HAL_OK) {
+            result = false;
+            break;
+        }
+    }
+    HAL_FLASH_Lock();
+    return result;
+}
+#endif // TARGET_STM32L1
+