示例#1
0
文件: example.c 项目: Dewb/mod
/**
 * \brief Card R/W tests
 *
 * \param slot   SD/MMC slot to test
 */
static void main_test_memory(uint8_t slot)
{
	uint32_t last_blocks_addr, i, nb_trans;
	uint32_t tick_start, time_ms;

	// Compute the last address
	last_blocks_addr = sd_mmc_get_capacity(slot) *
			(1024 / SD_MMC_BLOCK_SIZE);
	if (last_blocks_addr < (TEST_MEM_START_OFFSET / 512lu)) {
		printf("[Memory is too small.]\n\r");
		return;
	}
	last_blocks_addr -= (TEST_MEM_START_OFFSET / SD_MMC_BLOCK_SIZE);

	printf("Card R/W test:\n\r");

	// Read the last block
	printf("    Read... ");
	tick_start = time_tick_get();
	if (SD_MMC_OK != sd_mmc_init_read_blocks(slot,
			last_blocks_addr,
			TEST_MEM_AREA_SIZE / SD_MMC_BLOCK_SIZE)) {
		printf("[FAIL]\n\r");
		return;
	}
	for (nb_trans = 0; nb_trans < (TEST_MEM_AREA_SIZE / TEST_MEM_ACCESS_SIZE);
			nb_trans++) {
		if (SD_MMC_OK != sd_mmc_start_read_blocks(buf_test,
					TEST_MEM_ACCESS_SIZE / SD_MMC_BLOCK_SIZE)) {
			printf("[FAIL]\n\r");
			return;
		}
		if (SD_MMC_OK != sd_mmc_wait_end_of_read_blocks()) {
			printf("[FAIL]\n\r");
			return;
		}		
	}
	time_ms = time_tick_calc_delay(tick_start, time_tick_get());
	if (time_ms) { // Valid time_ms
		printf(" %d KBps ", (int)(((TEST_MEM_AREA_SIZE
				* 1000lu) / 1024lu) / time_ms));
	}
	printf("[OK]\n\r");

	if (sd_mmc_is_write_protected(slot)) {
		printf("Card is write protected [WRITE TEST SKIPPED]\n\r");
		return;
	}
	
	// Fill buffer with a pattern
	for (i = 0; i < (TEST_MEM_ACCESS_SIZE / sizeof(uint32_t)); i++) {
		((uint32_t*)buf_test)[i] = TEST_FILL_VALUE_U32;
	}

	printf("    Write pattern... ");
	if (SD_MMC_OK != sd_mmc_init_write_blocks(slot,
			last_blocks_addr,
			TEST_MEM_AREA_SIZE / SD_MMC_BLOCK_SIZE)) {
		printf("[FAIL]\n\r");
		return;
	}
	tick_start = time_tick_get();
	for (nb_trans = 0; nb_trans < (TEST_MEM_AREA_SIZE / TEST_MEM_ACCESS_SIZE);
			nb_trans++) {
		((uint32_t*)buf_test)[0] = nb_trans; // Unique value for each area
		if (SD_MMC_OK != sd_mmc_start_write_blocks(buf_test,
				TEST_MEM_ACCESS_SIZE / SD_MMC_BLOCK_SIZE)) {
			printf("[FAIL]\n\r");
			return;
		}
		if (SD_MMC_OK != sd_mmc_wait_end_of_write_blocks()) {
			printf("[FAIL]\n\r");
			return;
		}
	}
	time_ms = time_tick_calc_delay(tick_start, time_tick_get());
	if (time_ms) { // Valid time_ms
		printf(" %d KBps ", (int)(((TEST_MEM_AREA_SIZE
				* 1000lu) / 1024lu) / time_ms));
	}
	printf("[OK]\n\r");

	printf("    Read and check pattern... ");
	if (SD_MMC_OK != sd_mmc_init_read_blocks(slot,
			last_blocks_addr,
			TEST_MEM_AREA_SIZE / SD_MMC_BLOCK_SIZE)) {
		printf("Read [FAIL]\n\r");
		return;
	}
	for (nb_trans = 0; nb_trans < (TEST_MEM_AREA_SIZE / TEST_MEM_ACCESS_SIZE);
			nb_trans++) {
		// Clear buffer
		for (i = 0; i < (TEST_MEM_ACCESS_SIZE / sizeof(uint32_t)); i++) {
			((uint32_t*)buf_test)[i] = 0xFFFFFFFF;
		}
		// Fill buffer
		if (SD_MMC_OK != sd_mmc_start_read_blocks(buf_test,
					TEST_MEM_ACCESS_SIZE / SD_MMC_BLOCK_SIZE)) {
			printf("Read [FAIL]\n\r");
			return;
		}
		if (SD_MMC_OK != sd_mmc_wait_end_of_read_blocks()) {
			printf("Read [FAIL]\n\r");
			return;
		}
		// Check the unique value of the area
		if (((uint32_t*)buf_test)[0] != nb_trans) {
			printf("Check [FAIL]\n\r");
			return;
		}
		// Check buffer
		for (i = 1; i < (TEST_MEM_ACCESS_SIZE / sizeof(uint32_t)); i++) {
			if (((uint32_t*)buf_test)[i] != TEST_FILL_VALUE_U32) {
				printf("Check [FAIL]\n\r");
				return;
			}
		}
	}
	printf("[OK]\n\r");
}
示例#2
0
/**
 *  \brief Reconfigure serial console.
 */
static void reconfigure_com_port(void)
{
	const sam_usart_opt_t opt = {
		.baudrate = CONF_UART_BAUDRATE,
		.char_length = CONF_UART_CHAR_LENGTH,
		.parity_type = CONF_UART_PARITY,
		.stop_bits = CONF_UART_STOP_BITS,
		.channel_mode = US_MR_CHMODE_NORMAL,
		.irda_filter = 0,
	};
	usart_init_rs232(CONF_UART, &opt, LOWER_SYS_CLK);
	usart_enable_tx(CONF_UART);
}

/**
 *  \brief Recursively calculate the nth Fibonacci number.
 *
 * \param n Indicates which (positive) Fibonacci number to compute.
 *
 * \return The nth Fibonacci number.
 */
static uint32_t recfibo(uint32_t n)
{
	if (n == 0 || n == 1) {
		return n;
	}

	return recfibo(n - 2) + recfibo(n - 1);
}

/**
 * \brief This is an example demonstrating Fibonacci calculation
 *         with and without PicoCache.
 *
 * \param caption     Caption to print before running the example.
 * \param pico_enable Enable PicoCache or not.
 */
static void flash_picocache_example(const char *caption, bool pico_enable)
{
	uint32_t tick_start, time_ms;

	printf("\n\r--------------\n\r%s\n\r", caption);

	/* Enable PicoCache if required */
	if (pico_enable) {
		flashcalw_picocache_enable();

		flashcalw_picocache_set_monitor_mode(HCACHE_MCFG_MODE_IHIT);
		flashcalw_picocache_enable_monitor();
		flashcalw_picocache_reset_monitor();
	} else {
		flashcalw_picocache_disable();
		flashcalw_picocache_disable_monitor();
	}

	/* Get current time tick */
	tick_start = time_tick_get();

	/* Do the Fibonacci calculation. */
	recfibo(FIBONACCI_NUM);

	/* Calculate the Fibonacci spent time */
	time_ms = time_tick_calc_delay(tick_start, time_tick_get());
	if (time_ms) {
		printf("Time spent: %u ms\r\n", (unsigned int)time_ms);
	}

	/* Display cache hit counter */
	if (pico_enable) {
		printf("Pico cache instruction hit: %u \r\n",
			(unsigned int)flashcalw_picocache_get_monitor_cnt());
	}
}