void board_seq_update(u32_t seq) { u32_t loc, seq_map; int err; if (seq % SEQ_PER_BIT) { return; } loc = (SEQ_PAGE + ((seq / SEQ_PER_BIT) / 32)); err = flash_read(nvm, loc, &seq_map, sizeof(seq_map)); if (err) { printk("flash_read err %d\n", err); return; } seq_map >>= 1; flash_write_protection_set(nvm, false); err = flash_write(nvm, loc, &seq_map, sizeof(seq_map)); flash_write_protection_set(nvm, true); if (err) { printk("flash_write err %d\n", err); } }
static int cmd_test(const struct shell *shell, size_t argc, char *argv[]) { struct device *flash_dev; u32_t repeat; int result; u32_t addr; u32_t size; flash_dev = device_get_binding(FLASH_DEV_NAME); if (!flash_dev) { error(shell, "Flash driver was not found!"); return -ENODEV; } if (argc != 4) { error(shell, "3 parameters reqired."); return -EINVAL; } addr = strtoul(argv[1], NULL, 16); size = strtoul(argv[2], NULL, 16); repeat = strtoul(argv[3], NULL, 16); if (size > TEST_ARR_SIZE) { error(shell, "<size> must be at most 0x%x.", TEST_ARR_SIZE); return -EINVAL; } flash_write_protection_set(flash_dev, 0); for (u32_t i = 0; i < size; i++) { test_arr[i] = (u8_t)i; } while (repeat--) { result = flash_erase(flash_dev, addr, size); if (result) { error(shell, "Erase Failed, code %d.", result); return -EIO; } print(shell, "Erase OK."); if (flash_write(flash_dev, addr, test_arr, size) != 0) { error(shell, "Write internal ERROR!"); return -EIO; } print(shell, "Write OK."); } print(shell, "Erase-Write test done."); return 0; }
static u32_t get_seq(void) { u32_t seq_map, seq = 0; int err, i; for (i = 0; i < NRF_FICR->CODEPAGESIZE / sizeof(seq_map); i++) { err = flash_read(nvm, SEQ_PAGE + (i * sizeof(seq_map)), &seq_map, sizeof(seq_map)); if (err) { printk("flash_read err %d\n", err); return seq; } printk("seq_map 0x%08x\n", seq_map); if (seq_map) { seq = ((i * 32) + (32 - popcount(seq_map))) * SEQ_PER_BIT; if (!seq) { return 0; } break; } } seq += SEQ_PER_BIT; if (seq >= SEQ_MAX) { seq = 0; } if (seq) { seq_map >>= 1; flash_write_protection_set(nvm, false); err = flash_write(nvm, SEQ_PAGE + (i * sizeof(seq_map)), &seq_map, sizeof(seq_map)); flash_write_protection_set(nvm, true); if (err) { printk("flash_write err %d\n", err); } } else {
static int cmd_write(const struct shell *shell, size_t argc, char *argv[]) { u32_t check_array[BUF_ARRAY_CNT]; u32_t buf_array[BUF_ARRAY_CNT]; struct device *flash_dev; u32_t w_addr; int j = 0; flash_dev = device_get_binding(FLASH_DEV_NAME); if (!flash_dev) { error(shell, "Flash driver was not found!"); return -ENODEV; } if (argc < 2) { error(shell, "Missing address."); return -EINVAL; } if (argc <= 2) { error(shell, "Type data to be written."); return -EINVAL; } for (int i = 2; i < argc && i < BUF_ARRAY_CNT; i++) { buf_array[j] = strtoul(argv[i], NULL, 16); check_array[j] = ~buf_array[j]; j++; } flash_write_protection_set(flash_dev, 0); w_addr = strtoul(argv[1], NULL, 16); if (flash_write(flash_dev, w_addr, buf_array, sizeof(buf_array[0]) * j) != 0) { error(shell, "Write internal ERROR!"); return -EIO; } print(shell, "Write OK."); flash_read(flash_dev, w_addr, check_array, sizeof(buf_array[0]) * j); if (memcmp(buf_array, check_array, sizeof(buf_array[0]) * j) == 0) { print(shell, "Verified."); } else { error(shell, "Verification ERROR!"); return -EIO; } return 0; }
static int cmd_erase(const struct shell *shell, size_t argc, char *argv[]) { struct device *flash_dev; u32_t page_addr; int result; u32_t size; flash_dev = device_get_binding(FLASH_DEV_NAME); if (!flash_dev) { error(shell, "Flash driver was not found!"); return -ENODEV; } if (argc < 2) { error(shell, "Missing page address."); return -EINVAL; } page_addr = strtoul(argv[1], NULL, 16); if (argc > 2) { size = strtoul(argv[2], NULL, 16); } else { size = NRF_FICR->CODEPAGESIZE; } flash_write_protection_set(flash_dev, 0); result = flash_erase(flash_dev, page_addr, size); if (result) { error(shell, "Erase Failed, code %d.", result); } else { print(shell, "Erase success."); } return result; }
void main(void) { struct device *flash_dev; u32_t buf_array_1[4] = { TEST_DATA_WORD_0, TEST_DATA_WORD_1, TEST_DATA_WORD_2, TEST_DATA_WORD_3 }; u32_t buf_array_2[4] = { TEST_DATA_WORD_3, TEST_DATA_WORD_1, TEST_DATA_WORD_2, TEST_DATA_WORD_0 }; u32_t buf_array_3[8] = { TEST_DATA_WORD_0, TEST_DATA_WORD_1, TEST_DATA_WORD_2, TEST_DATA_WORD_3, TEST_DATA_WORD_0, TEST_DATA_WORD_1, TEST_DATA_WORD_2, TEST_DATA_WORD_3 }; u32_t buf_word = 0; u32_t i, offset; printf("\nNordic nRF5 Flash Testing\n"); printf("=========================\n"); flash_dev = device_get_binding(CONFIG_SOC_FLASH_NRF5_DEV_NAME); if (!flash_dev) { printf("Nordic nRF5 flash driver was not found!\n"); return; } printf("\nTest 1: Flash erase page at 0x%x\n", FLASH_TEST_OFFSET); if (flash_erase(flash_dev, FLASH_TEST_OFFSET, FLASH_PAGE_SIZE) != 0) { printf(" Flash erase failed!\n"); } else { printf(" Flash erase succeeded!\n"); } printf("\nTest 2: Flash write (word array 1)\n"); flash_write_protection_set(flash_dev, false); for (i = 0; i < ARRAY_SIZE(buf_array_1); i++) { offset = FLASH_TEST_OFFSET + (i << 2); printf(" Attempted to write %x at 0x%x\n", buf_array_1[i], offset); if (flash_write(flash_dev, offset, &buf_array_1[i], sizeof(u32_t)) != 0) { printf(" Flash write failed!\n"); return; } printf(" Attempted to read 0x%x\n", offset); if (flash_read(flash_dev, offset, &buf_word, sizeof(u32_t)) != 0) { printf(" Flash read failed!\n"); return; } printf(" Data read: %x\n", buf_word); if (buf_array_1[i] == buf_word) { printf(" Data read matches data written. Good!\n"); } else { printf(" Data read does not match data written!\n"); } } flash_write_protection_set(flash_dev, true); offset = FLASH_TEST_OFFSET - FLASH_PAGE_SIZE * 2; printf("\nTest 3: Flash erase (4 pages at 0x%x)\n", offset); if (flash_erase(flash_dev, offset, FLASH_PAGE_SIZE * 4) != 0) { printf(" Flash erase failed!\n"); } else { printf(" Flash erase succeeded!\n"); } printf("\nTest 4: Flash write (word array 2)\n"); flash_write_protection_set(flash_dev, false); for (i = 0; i < ARRAY_SIZE(buf_array_2); i++) { offset = FLASH_TEST_OFFSET + (i << 2); printf(" Attempted to write %x at 0x%x\n", buf_array_2[i], offset); if (flash_write(flash_dev, offset, &buf_array_2[i], sizeof(u32_t)) != 0) { printf(" Flash write failed!\n"); return; } printf(" Attempted to read 0x%x\n", offset); if (flash_read(flash_dev, offset, &buf_word, sizeof(u32_t)) != 0) { printf(" Flash read failed!\n"); return; } printf(" Data read: %x\n", buf_word); if (buf_array_2[i] == buf_word) { printf(" Data read matches data written. Good!\n"); } else { printf(" Data read does not match data written!\n"); } } flash_write_protection_set(flash_dev, true); printf("\nTest 5: Flash erase page at 0x%x\n", FLASH_TEST_OFFSET); if (flash_erase(flash_dev, FLASH_TEST_OFFSET, FLASH_PAGE_SIZE) != 0) { printf(" Flash erase failed!\n"); } else { printf(" Flash erase succeeded!\n"); } printf("\nTest 6: Non-word aligned write (word array 3)\n"); flash_write_protection_set(flash_dev, false); for (i = 0; i < ARRAY_SIZE(buf_array_3); i++) { offset = FLASH_TEST_OFFSET + (i << 2) + 1; printf(" Attempted to write %x at 0x%x\n", buf_array_3[i], offset); if (flash_write(flash_dev, offset, &buf_array_3[i], sizeof(u32_t)) != 0) { printf(" Flash write failed!\n"); return; } printf(" Attempted to read 0x%x\n", offset); if (flash_read(flash_dev, offset, &buf_word, sizeof(u32_t)) != 0) { printf(" Flash read failed!\n"); return; } printf(" Data read: %x\n", buf_word); if (buf_array_3[i] == buf_word) { printf(" Data read matches data written. Good!\n"); } else { printf(" Data read does not match data written!\n"); } } flash_write_protection_set(flash_dev, true); }