diff --git a/src/D2xNvmFlash.cpp b/src/D2xNvmFlash.cpp
index 0974936c..4902d5b0 100644
--- a/src/D2xNvmFlash.cpp
+++ b/src/D2xNvmFlash.cpp
@@ -17,6 +17,7 @@
// along with this program. If not, see .
///////////////////////////////////////////////////////////////////////////////
+#include
#include "D2xNvmFlash.h"
// CMDEX field should be 0xA5 to allow execution of any command.
@@ -100,7 +101,7 @@ D2xNvmFlash::erase(uint32_t offset, uint32_t size)
// Issue erase command
uint32_t wordAddr = (eraseNum * eraseSize) / 2;
writeReg(NVM_REG_ADDR, wordAddr);
- command(NVM_CMD_ER);
+ command(NVM_CMD_ER, 6000);
}
}
@@ -227,7 +228,7 @@ D2xNvmFlash::writeOptions()
// Erase user row
writeReg(NVM_REG_ADDR, NVM_UR_ADDR / 2);
- command(NVM_CMD_EAR);
+ command(NVM_CMD_EAR, 6000);
// Write user row in page chunks
for (uint32_t offset = 0; offset < NVM_UR_SIZE; offset += _size)
@@ -247,7 +248,7 @@ D2xNvmFlash::writeOptions()
// Write the page
writeReg(NVM_REG_ADDR, (NVM_UR_ADDR + offset) / 2);
- command(NVM_CMD_WAP);
+ command(NVM_CMD_WAP, 2500);
}
}
@@ -286,7 +287,7 @@ D2xNvmFlash::writePage(uint32_t page)
_wordCopy.runv();
writeReg(NVM_REG_ADDR, addr / 2);
- command(NVM_CMD_WP);
+ command(NVM_CMD_WP,2500);
}
void
@@ -319,12 +320,13 @@ D2xNvmFlash::writeReg(uint8_t reg, uint32_t value)
}
void
-D2xNvmFlash::command(uint8_t cmd)
+D2xNvmFlash::command(uint8_t cmd, uint16_t wait)
{
waitReady();
writeReg(NVM_REG_CTRLA, CMDEX_KEY | cmd);
+ usleep(wait);
waitReady();
if (readReg(NVM_REG_INTFLAG) & 0x2)
diff --git a/src/D2xNvmFlash.h b/src/D2xNvmFlash.h
index d7c36948..eb9a2453 100644
--- a/src/D2xNvmFlash.h
+++ b/src/D2xNvmFlash.h
@@ -68,7 +68,7 @@ class D2xNvmFlash : public Flash
void writeReg(uint8_t reg, uint32_t value);
void waitReady();
- void command(uint8_t cmd);
+ void command(uint8_t cmd, uint16_t wait = 1);
void erase(uint32_t offset, uint32_t size);
void readUserRow(std::unique_ptr& userRow);
};
diff --git a/src/Device.cpp b/src/Device.cpp
index 0c94ae44..2981b6c8 100644
--- a/src/Device.cpp
+++ b/src/Device.cpp
@@ -424,6 +424,14 @@ Device::create()
case 0:
switch (deviceId & 0xffff00ff)
{
+ //
+ // SAMC21
+ //
+ case 0x11010000: // J18A
+ _family = FAMILY_SAMC21;
+ flashPtr = new D2xNvmFlash(_samba, "ATSAMC21x18", 4096, 64, 0x20004000, 0x20008000) ;
+ break;
+
//
// SAMD21
//
@@ -638,6 +646,7 @@ Device::reset()
{
switch (_family)
{
+ case FAMILY_SAMC21:
case FAMILY_SAMD21:
case FAMILY_SAMR21:
case FAMILY_SAML21:
diff --git a/src/Device.h b/src/Device.h
index 5475c557..c16eb135 100644
--- a/src/Device.h
+++ b/src/Device.h
@@ -64,6 +64,7 @@ class Device
FAMILY_SAM9XE,
+ FAMILY_SAMC21,
FAMILY_SAMD21,
FAMILY_SAMR21,
FAMILY_SAML21,