Merge pull request #83 from gsmcmullin/samd_no_ap

samd: Remove low level ADIv5 calls an favour of target_mem_write.
This commit is contained in:
Gareth McMullin 2015-03-18 19:07:03 -07:00
commit 7372fca2f6
1 changed files with 100 additions and 119 deletions

View File

@ -374,21 +374,23 @@ bool samd_probe(struct target_s *target)
uint32_t pid = samd_read_pid(target);
/* Check the ARM Coresight Component and Perhiperal IDs */
if (cid == SAMD_CID_VALUE &&
(pid & SAMD_PID_MASK) == SAMD_PID_CONST_VALUE) {
if ((cid != SAMD_CID_VALUE) ||
((pid & SAMD_PID_MASK) != SAMD_PID_CONST_VALUE))
return false;
/* Read the Device ID */
uint32_t did = target_mem_read32(target, SAMD_DSU_DID);
/* If the Device ID matches */
if ((did & SAMD_DID_MASK) == SAMD_DID_CONST_VALUE) {
if ((did & SAMD_DID_MASK) != SAMD_DID_CONST_VALUE)
return false;
uint32_t ctrlstat = target_mem_read32(target,
SAMD_DSU_CTRLSTAT);
struct samd_descr samd = samd_parse_device_id(did);
/* Protected? */
int protected = (ctrlstat & SAMD_STATUSB_PROT);
bool protected = (ctrlstat & SAMD_STATUSB_PROT);
/* Part String */
if (protected) {
@ -446,10 +448,6 @@ bool samd_probe(struct target_s *target)
}
return true;
}
}
return false;
}
/**
@ -489,7 +487,7 @@ static int samd_flash_erase(struct target_s *target, uint32_t addr, size_t len)
SAMD_CTRLA_CMD_KEY | SAMD_CTRLA_CMD_ERASEROW);
/* Poll for NVM Ready */
while ((target_mem_read32(target, SAMD_NVMC_INTFLAG) & SAMD_NVMC_READY) == 0)
if(target_check_error(target))
if (target_check_error(target))
return -1;
/* Lock */
@ -508,7 +506,6 @@ static int samd_flash_erase(struct target_s *target, uint32_t addr, size_t len)
static int samd_flash_write(struct target_s *target, uint32_t dest,
const uint8_t *src, size_t len)
{
ADIv5_AP_t *ap = adiv5_target_ap(target);
/* Find the size of our 32-bit data buffer */
uint32_t offset = dest % 4;
uint32_t words = (offset + len + 3) / 4;
@ -527,24 +524,19 @@ static int samd_flash_write(struct target_s *target, uint32_t dest,
uint32_t first_page = dest & ~(SAMD_PAGE_SIZE - 1);
/* The start address of the last page involved in the write */
uint32_t last_page = (dest + len - 1) & ~(SAMD_PAGE_SIZE - 1);
uint32_t end_of_this_page;
uint32_t next_page;
uint32_t length;
for (uint32_t page = first_page; page <= last_page; page += SAMD_PAGE_SIZE) {
end_of_this_page = page + (SAMD_PAGE_SIZE - 4);
next_page = page + SAMD_PAGE_SIZE;
length = MINIMUM(end + 4, next_page) - addr;
if (addr > page || (page == last_page && end < end_of_this_page)) {
/* Setup write */
adiv5_ap_write(ap, ADIV5_AP_CSW, ap->csw |
ADIV5_AP_CSW_SIZE_WORD | ADIV5_AP_CSW_ADDRINC_SINGLE);
adiv5_ap_write(ap, ADIV5_AP_TAR, addr);
adiv5_dp_write(ap->dp, ADIV5_DP_SELECT,
((uint32_t)ap->apsel << 24)|(ADIV5_AP_DRW & 0xF0));
/* Write within a single page. This may be part or all of the page */
target_mem_write(target, addr, &data[i], length);
addr += length; i += (length >> 2);
/* Partial, manual page write */
for (; addr <= MINIMUM(end, end_of_this_page); addr += 4, i++) {
adiv5_dp_write(ap->dp, ADIV5_AP_DRW, data[i]);
}
/* If MANW=0 (default) we may have triggered an automatic
* write. Ignore this */
/* Unlock */
samd_unlock_current_address(target);
@ -552,29 +544,10 @@ static int samd_flash_write(struct target_s *target, uint32_t dest,
/* Issue the write page command */
target_mem_write32(target, SAMD_NVMC_CTRLA,
SAMD_CTRLA_CMD_KEY | SAMD_CTRLA_CMD_WRITEPAGE);
} else {
/* Write first word to set address */
target_mem_write32(target, addr, data[i]); addr += 4; i++;
/* Unlock */
samd_unlock_current_address(target);
/* Set up write */
adiv5_ap_write(ap, ADIV5_AP_CSW, ap->csw |
ADIV5_AP_CSW_SIZE_WORD | ADIV5_AP_CSW_ADDRINC_SINGLE);
adiv5_ap_write(ap, ADIV5_AP_TAR, addr);
adiv5_dp_write(ap->dp, ADIV5_DP_SELECT,
((uint32_t)ap->apsel << 24)|(ADIV5_AP_DRW & 0xF0));
/* Full, automatic page write */
for (; addr < page + SAMD_PAGE_SIZE; addr += 4, i++) {
adiv5_dp_write(ap->dp, ADIV5_AP_DRW, data[i]);
}
}
/* Poll for NVM Ready */
while ((target_mem_read32(target, SAMD_NVMC_INTFLAG) & SAMD_NVMC_READY) == 0)
if(target_check_error(target))
if (target_check_error(target))
return -1;
/* Lock */
@ -591,7 +564,8 @@ static bool samd_cmd_erase_all(target *t)
{
/* Clear the DSU status bits */
target_mem_write32(t, SAMD_DSU_CTRLSTAT,
(SAMD_STATUSA_DONE | SAMD_STATUSA_PERR | SAMD_STATUSA_FAIL));
SAMD_STATUSA_DONE | SAMD_STATUSA_PERR |
SAMD_STATUSA_FAIL);
/* Erase all */
target_mem_write32(t, SAMD_DSU_CTRLSTAT, SAMD_CTRL_CHIP_ERASE);
@ -600,7 +574,7 @@ static bool samd_cmd_erase_all(target *t)
uint32_t status;
while (((status = target_mem_read32(t, SAMD_DSU_CTRLSTAT)) &
(SAMD_STATUSA_DONE | SAMD_STATUSA_PERR | SAMD_STATUSA_FAIL)) == 0)
if(target_check_error(t))
if (target_check_error(t))
return false;
/* Test the protection error bit in Status A */
@ -642,7 +616,7 @@ static bool samd_set_flashlock(target *t, uint16_t value)
/* Poll for NVM Ready */
while ((target_mem_read32(t, SAMD_NVMC_INTFLAG) & SAMD_NVMC_READY) == 0)
if(target_check_error(t))
if (target_check_error(t))
return -1;
/* Modify the high byte of the user row */
@ -658,14 +632,17 @@ static bool samd_set_flashlock(target *t, uint16_t value)
return true;
}
static bool samd_cmd_lock_flash(target *t)
{
return samd_set_flashlock(t, 0x0000);
}
static bool samd_cmd_unlock_flash(target *t)
{
return samd_set_flashlock(t, 0xFFFF);
}
static bool samd_cmd_read_userrow(target *t)
{
gdb_outf("User Row: 0x%08x%08x\n",
@ -674,6 +651,7 @@ static bool samd_cmd_read_userrow(target *t)
return true;
}
/**
* Reads the 128-bit serial number from the NVM
*/
@ -689,6 +667,7 @@ static bool samd_cmd_serial(target *t)
return true;
}
/**
* Returns the size (in bytes) of the current SAM D20's flash memory.
*/
@ -703,6 +682,7 @@ static uint32_t samd_flash_size(target *t)
/* Shift the maximum flash size (256KB) down as appropriate */
return (0x40000 >> (devsel % 5));
}
/**
* Runs the Memory Built In Self Test (MBIST)
*/
@ -722,7 +702,7 @@ static bool samd_cmd_mbist(target *t)
uint32_t status;
while (((status = target_mem_read32(t, SAMD_DSU_CTRLSTAT)) &
(SAMD_STATUSA_DONE | SAMD_STATUSA_PERR | SAMD_STATUSA_FAIL)) == 0)
if(target_check_error(t))
if (target_check_error(t))
return false;
/* Test the protection error bit in Status A */
@ -752,7 +732,7 @@ static bool samd_cmd_ssb(target *t)
/* Poll for NVM Ready */
while ((target_mem_read32(t, SAMD_NVMC_INTFLAG) & SAMD_NVMC_READY) == 0)
if(target_check_error(t))
if (target_check_error(t))
return -1;
gdb_outf("Set the security bit! "
@ -760,3 +740,4 @@ static bool samd_cmd_ssb(target *t)
return true;
}