Ejemplo n.º 1
0
status_code_t nvm_set_security_bit(void)
{
	if (!flash_enable_security_bit()) {
		return ERR_INVALID_ARG;
	}

	return STATUS_OK;
}
Ejemplo n.º 2
0
status_code_t nvm_set_security_bit(void)
{
#if SAM4L
	/*To Do */
#else
	if (!flash_enable_security_bit()) {
		return ERR_INVALID_ARG;
	}
#endif
	return STATUS_OK;
}
Ejemplo n.º 3
0
/**
 * Perform initialization and tests on flash.
 */
int main(void)
{
	uint32_t ul_test_page_addr = TEST_PAGE_ADDRESS;
	uint32_t *pul_test_page = (uint32_t *) ul_test_page_addr;
	uint32_t ul_rc;
	uint32_t ul_idx;
	uint8_t uc_key;
	uint32_t ul_page_buffer[IFLASH_PAGE_SIZE / sizeof(uint32_t)];

	/* Initialize the SAM system */
	sysclk_init();
	board_init();

	/* Initialize the console uart */
	configure_console();

	/* Output example information */
	puts(STRING_HEADER);

	/* Initialize flash: 6 wait states for flash writing. */
	ul_rc = flash_init(FLASH_ACCESS_MODE_128, 6);
	if (ul_rc != FLASH_RC_OK) {
		printf("-F- Initialization error %lu\n\r", (UL)ul_rc);
		return 0;
	}

	/* Unlock page */
	printf("-I- Unlocking test page: 0x%08x\r\n", ul_test_page_addr);
	ul_rc = flash_unlock(ul_test_page_addr,
			ul_test_page_addr + IFLASH_PAGE_SIZE - 1, 0, 0);
	if (ul_rc != FLASH_RC_OK) {
		printf("-F- Unlock error %lu\n\r", (UL)ul_rc);
		return 0;
	}

	/* Write page */
	printf("-I- Writing test page with walking bit pattern\n\r");
	for (ul_idx = 0; ul_idx < (IFLASH_PAGE_SIZE / 4); ul_idx++) {
		ul_page_buffer[ul_idx] = 1 << (ul_idx % 32);
	}

#if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CP || SAMG || SAM4CM || \
	 SAMV71 || SAMV70 || SAMS70 || SAME70)
	/* The EWP command is not supported for non-8KByte sectors in all devices
	 *  SAM4 series, so an erase command is requried before the write operation.
	 */
	ul_rc = flash_erase_sector(ul_test_page_addr);
	if (ul_rc != FLASH_RC_OK) {
		printf("-F- Flash programming error %lu\n\r", (UL)ul_rc);
		return 0;
	}

	ul_rc = flash_write(ul_test_page_addr, ul_page_buffer,
			IFLASH_PAGE_SIZE, 0);
#else
	ul_rc = flash_write(ul_test_page_addr, ul_page_buffer,
			IFLASH_PAGE_SIZE, 1);
#endif
	if (ul_rc != FLASH_RC_OK) {
		printf("-F- Flash programming error %lu\n\r", (UL)ul_rc);
		return 0;
	}

	/* Validate page */
	printf("-I- Checking page contents ");
	for (ul_idx = 0; ul_idx < (IFLASH_PAGE_SIZE / 4); ul_idx++) {
		printf(".");
		if (pul_test_page[ul_idx] != ul_page_buffer[ul_idx]) {
			printf("\n\r-F- data error\n\r");
			return 0;
		}
	}
	printf("OK\n\r");

#if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CP || SAMG || SAM4CM || \
	 SAMV71 || SAMV70 || SAMS70 || SAME70)
	/* The EWP command is not supported for non-8KByte sectors in some SAM4
	 * series, so an erase command is requried before the write operation.
	 */
	ul_rc = flash_erase_sector(ul_test_page_addr);
	if (ul_rc != FLASH_RC_OK) {
		printf("-F- Flash programming error %lu\n\r", (UL)ul_rc);
		return 0;
	}
#endif

	/* Lock page */
	printf("-I- Locking test page\n\r");
	ul_rc = flash_lock(ul_test_page_addr,
			ul_test_page_addr + IFLASH_PAGE_SIZE - 1, 0, 0);
	if (ul_rc != FLASH_RC_OK) {
		printf("-F- Flash locking error %lu\n\r", (UL)ul_rc);
		return 0;
	}

	/* Check if the associated region is locked. */
	printf("-I- Try to program the locked page ...\n\r");
	ul_rc = flash_write(ul_test_page_addr, ul_page_buffer,
			IFLASH_PAGE_SIZE,
#if (SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM || \
	 SAMV71 || SAMV70 || SAMS70 || SAME70)
			0);
#else
			1);
#endif
	if (ul_rc != FLASH_RC_OK) {
		printf("-I- The page to be programmed belongs to locked region. Error %lu\n\r",
				(UL)ul_rc);
	}

	printf("-I- Please open Segger's JMem program \n\r");
	printf("-I- Read memory at address 0x%08lx to check contents\n\r",
			(UL)ul_test_page_addr);
	printf("-I- Press any key to continue...\n\r");
	scanf("%c", (char *)&uc_key);

	printf("-I- Good job!\n\r"
			"-I- Now set the security bit \n\r"
			"-I- Press any key to continue to see what happened...\n\r");
	scanf("%c", (char *)&uc_key);

	/* Set security bit */
	printf("-I- Setting security bit \n\r");
	ul_rc = flash_enable_security_bit();
	if (ul_rc != FLASH_RC_OK) {
		printf("-F- Set security bit error %lu\n\r", (UL)ul_rc);
	}

	printf("-I- All tests done\n\r");

	while (1) {
		/* Do nothing */
	}
}