void flashpage_write(int page, const void *data) { assert(page < FLASHPAGE_NUMOF); uint32_t *page_addr = (uint32_t *)flashpage_addr(page); /* erase given page (the ADDR register uses 16-bit addresses) */ _unlock(); NVMCTRL->ADDR.reg = (((uint32_t)page_addr) >> 1); NVMCTRL->CTRLA.reg = (NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_ER); while (!NVMCTRL->INTFLAG.bit.READY) {} _lock(); /* write data to page */ if (data != NULL) { flashpage_write_raw(page_addr, data, FLASHPAGE_SIZE); } }
static int cmd_write_raw(int argc, char **argv) { uint32_t addr; if (argc < 3) { printf("usage: %s <addr> <data>\n", argv[0]); return 1; } addr = getaddr(argv[1]); /* try to align */ memcpy(raw_buf, argv[2], strlen(argv[2])); flashpage_write_raw((void*)addr, raw_buf, strlen(raw_buf)); printf("wrote local data to flash address %#lx of len %u\n", addr, strlen(raw_buf)); return 0; }
/** * @brief Does a short raw write on last page available * * @note Since every hardware can have different flash layouts for * automated testing we always write to the last page available * so we are independent of the size or layout */ static int cmd_test_last_raw(int argc, char **argv) { (void) argc; (void) argv; /* try to align */ memcpy(raw_buf, "test12344321tset", 16); /* erase the page first */ flashpage_write(((int)FLASHPAGE_NUMOF - 1), NULL); flashpage_write_raw(flashpage_addr((int)FLASHPAGE_NUMOF - 1), raw_buf, strlen(raw_buf)); /* verify that previous write_raw effectively wrote the desired data */ if (memcmp(flashpage_addr((int)FLASHPAGE_NUMOF - 1), raw_buf, strlen(raw_buf)) != 0) { puts("error verifying the content of last page"); return 1; } puts("wrote raw short buffer to last flash page"); return 0; }