text: #include <atmel_start.h>
  #include <stdlib.h>
  
  #define STEP_SIZE 1
  #define MIN_NUM_BYTES STEP_SIZE
  #define MAX_NUM_BYTES 6000 //6496
  
  #define SLEEP
  
  #ifndef FLASH_PAGE_SIZE
  #define FLASH_PAGE_SIZE 64
  #endif
  
  #ifndef NVMCTRL_FLASH_SIZE
  #define NVMCTRL_FLASH_SIZE 65536
  #endif
  
  #ifndef NVMCTRL_ROW_PAGES
  #define NVMCTRL_ROW_PAGES 4
  #endif
  
  #ifndef NVMCTRL_ROW_SIZE
  #define NVMCTRL_ROW_SIZE (NVMCTRL_PAGE_SIZE * NVMCTRL_ROW_PAGES)
  #endif
  
  
  int main(void)
  {
  	/* Initializes MCU, drivers and middleware */
  	atmel_start_init();
  
  	uint8_t *input = malloc(sizeof(uint8_t) * MAX_NUM_BYTES);
  	
  	delay_ms(10);
  
  	for (size_t num_bytes = MIN_NUM_BYTES; num_bytes <= MAX_NUM_BYTES; num_bytes += STEP_SIZE) {
  		//size_t num_bytes = MAX_NUM_BYTES;
  		// Fill with sequential data.
  		for (size_t byte = 0; byte < num_bytes; byte++) {
  			input[byte] = byte; // Will wrap at 0xff.
  		}
  
  		START_MEASURE(DGI_GPIO2);
  		// Save to flash
  		// Put data at end of flash.
  		uint32_t target_addr = FLASH_ADDR + FLASH_SIZE - num_bytes;
  		target_addr -= target_addr % NVMCTRL_ROW_SIZE;
  		
  		for (uint32_t page_index = 0; page_index * FLASH_PAGE_SIZE < num_bytes; page_index += 1) {
  			if (page_index % NVMCTRL_ROW_PAGES == 0) {
  				/* Erase row in flash */
  				FLASH_0_erase_row(target_addr + page_index * FLASH_PAGE_SIZE);
  			}
  			FLASH_0_write_page(target_addr + page_index * FLASH_PAGE_SIZE, &input[page_index * FLASH_PAGE_SIZE], FLASH_PAGE_SIZE);
  			// TODO: decrease FLASH_PAGE_SIZE to correct number on last write if not multiple of FLASH_PAGE_SIZE.
  		}
  		STOP_MEASURE(DGI_GPIO2);
  		
  		SLEEP
  		
  		// Overwrite the memory
  		for (size_t byte = 0; byte < num_bytes; byte++) {
  			input[byte] = 0xfe;
  		}
  		
  		START_MEASURE(DGI_GPIO3);
  		// Read from flash
  		FLASH_0_read(target_addr, input, num_bytes);
  		STOP_MEASURE(DGI_GPIO3);
  		
  		//// Check if memory has correct data
  		//for (size_t byte = 0; byte < num_bytes; byte++) {
  		//	if (input[byte] !=  byte % 0xff) {
  		//		gpio_set_pin_level(DGI_GPIO2, GPIO_HIGH);
  		//	}
  		//}
  		
  	}
  
  	// Free the memory
  	free(input);
  
  	END_MEASUREMENT;
  }
  