/*! \brief This is an example demonstrating the flash lock bits feature. */ static void flash_protect_example( void ) { printf("\r\nFlash protection example...\n\r"); /* Set the lock */ flashcalw_lock_page_region(NVRAM_PAGE_NUMBER, true); /* Try to access the locked area */ flashcalw_memset((void *)NVRAM_PAGE_ADDRESS, 0x0, 8, FLASH_PAGE_SIZE, true); /* A lock error will generated if access to the locked page */ if (flashcalw_is_lock_error()) { printf("Flash lock operation succeeded.\n\r"); } /* Unlock the page */ flashcalw_lock_page_region(NVRAM_PAGE_NUMBER, false); }
/** * \brief Test flashcalw protection. * * This test backups the region protection status, then validates the 3 types of * region protection interface and restores the default protection status. * * \param test Current test case. */ static void run_flashcalw_protection_test(const struct test_case *test) { uint32_t region_num; uint32_t region_lock_status = 0; uint32_t default_region_lock_status = 0; uint32_t i; /* Backup the flashcalw protection status */ for (i = 0; i < FLASH_NB_OF_REGIONS; i++) { if (flashcalw_is_region_locked(i)) { default_region_lock_status |= 1 << i; } } /* Lock all regions */ flashcalw_lock_all_regions(true); /* Get all region protection status */ for (i = 0; i < FLASH_NB_OF_REGIONS; i++) { if (flashcalw_is_region_locked(i)) { region_lock_status |= 1 << i; } } /* Validate the protect all regions function */ test_assert_true(test, ((region_lock_status & FLASHCALW_ALL_REGION_LOCK) == FLASHCALW_ALL_REGION_LOCK), "Test flashcalw protection: lock all regions error!"); /* Unlock all regions */ flashcalw_lock_all_regions(false); /* Get all region protection status */ region_lock_status = 0; for (i = 0; i < FLASH_NB_OF_REGIONS; i++) { if (flashcalw_is_region_locked(i)) { region_lock_status |= 1 << i; } } /* Validate the unlock all regions function */ test_assert_true(test, (region_lock_status == 0), "Test flashcalw protection: unlock all regions error!"); /* Lock region through page number */ flashcalw_lock_page_region(FLASHCALW_TEST_PAGE_NUM, true); /* Get page lock status */ region_lock_status = flashcalw_is_page_region_locked (FLASHCALW_TEST_PAGE_NUM); /* Validate the get region count function */ test_assert_true(test, region_lock_status, "Test flashcalw protection: page region lock operation error!"); /* Unlock region through page number */ flashcalw_lock_page_region(FLASHCALW_TEST_PAGE_NUM, false); /* Get region number through page number */ region_num = flashcalw_get_page_region(FLASHCALW_TEST_PAGE_NUM); /* Lock region through region number */ flashcalw_lock_region(region_num, true); /* Get region lock status */ region_lock_status = flashcalw_is_region_locked(region_num); /* Validate the lock region through region number function */ test_assert_true(test, region_lock_status, "Test flashcalw protection: region lock operation error!"); /* Restore default region protection status */ for (i = 0; i < FLASH_NB_OF_REGIONS; i++) { if (default_region_lock_status & 0x1) { flashcalw_lock_page_region(i, true); default_region_lock_status >>= 1; } }