Exemplo n.º 1
0
/**
 * \brief Test backup mode.
 *
 * \note To test backup mode, the program must run out of flash.
 */
static void test_backup_mode(void)
{
	puts(STRING_BACKUP);

#if SAMG55
	/* Wait for the transmission done before changing clock */
	while (!usart_is_tx_empty(CONSOLE_UART)) {
	}
#else
	/* Wait for the transmission done before changing clock */
	while (!uart_is_tx_empty(CONSOLE_UART)) {
	}
#endif

	/* GPBR0 is for recording times of entering into backup mode */
	gpbr_write(GPBR0, gpbr_read(GPBR0) + 1);

	/* Enable the PIO for wake-up */
	example_set_wakeup_from_backup_mode();

	/* Switch MCK to slow clock  */
	pmc_switch_mck_to_sclk(PMC_MCKR_PRES_CLK_1);

	/* Disable unused clock to save power */
	pmc_osc_disable_xtal(0);
	example_disable_pll();

	/* Enter into backup mode */
	pmc_enable_backupmode();

	/* Note: The core will reset when exiting from backup mode. */
}
Exemplo n.º 2
0
void bm_print_clear(void)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 3 /* length */);
	usart_putchar(BM_USART_USART, BM_PRINT_CLEAR);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemplo n.º 3
0
void bm_mouse_pointer_ctrl(bool state)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 4 /* length */);
	usart_putchar(BM_USART_USART, BM_POINTER_CTRL);
	usart_putchar(BM_USART_USART, state);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemplo n.º 4
0
static void XPROGTarget_SetRxMode(void)
{
	while(usart_is_tx_empty(USART_PDI) == 0);

	usart_disable_tx(USART_PDI);
	usart_enable_rx(USART_PDI);
	
	gpio_configure_pin(PIN_PDIDTX_GPIO, PIN_PDIDTX_IN_FLAGS);
	
	IsSending = false;
}
Exemplo n.º 5
0
void bm_pullup_twi(bool state)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 4 /* length */);
	usart_putchar(BM_USART_USART, BM_PULLUP_TWI);
	usart_putchar(BM_USART_USART, state);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemplo n.º 6
0
void bm_led_tgl(uint32_t led)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 4 /* length */);
	usart_putchar(BM_USART_USART, BM_LED_TGL);
	usart_putchar(BM_USART_USART, led);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemplo n.º 7
0
void bm_mouse_pointer_move(uint32_t x, uint32_t y)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 5 /* length */);
	usart_putchar(BM_USART_USART, BM_POINTER_MOVE);
	usart_putchar(BM_USART_USART, x);
	usart_putchar(BM_USART_USART, y);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemplo n.º 8
0
bool bm_get_mcu_current(uint32_t* sleep_mode, float* current)
{
	uint32_t current_d;
        uint32_t c;
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 4 /* length */);
	usart_putchar(BM_USART_USART, BM_MCU_GET_CURRENT);
	usart_putchar(BM_USART_USART, *sleep_mode);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
	usart_enable_rx(BM_USART_USART);
	// Check first caracter is start pattern
	usart_getchar(BM_USART_USART, &c);
	if (c == BM_MSG_STOP_PATTERN) {
		usart_getchar(BM_USART_USART, &c);
	}
	if (c != BM_MSG_START_PATTERN) {
		return false;
	}
	// Check second caracter is length
	usart_getchar(BM_USART_USART, &c);
	if (c != 8) {
		return false;
	}
	// Check third caracter is Current Command
	usart_getchar(BM_USART_USART, &c);
	if (c != BM_MCU_RET_CURRENT) {
		return false;
	}
	// Check third caracter is sleepmode
	usart_getchar(BM_USART_USART, &c);
	*sleep_mode = c;
	// Then read current
	usart_getchar(BM_USART_USART, &c);
	current_d = c<<24;
	usart_getchar(BM_USART_USART, &c);
	current_d |= c<<16;
	usart_getchar(BM_USART_USART, &c);
	current_d |= c<<8;
	usart_getchar(BM_USART_USART, &c);
	current_d |= c;
	*current = *(float*)& current_d;
	// Check last caracter is stop pattern
	usart_getchar(BM_USART_USART, &c);
	if (c != BM_MSG_STOP_PATTERN) {
		return false;
	}
	usart_disable_rx(BM_USART_USART);
	return true;
}
Exemplo n.º 9
0
bool bm_get_firmware_version(uint8_t* fw_minor_version, uint8_t* fw_major_version)
{
        uint32_t start, length, stop, cmd_id, c;

	// Wait for some microseconds in order to avoid fifo overrun
	//
	delay_ms(20);

	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 3 /* length */);
	usart_putchar(BM_USART_USART, BM_GET_FIRMWARE_VERSION);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);

	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
	usart_enable_rx(BM_USART_USART);
	
	// Check first character is start pattern
	usart_getchar(BM_USART_USART, &start);
	if (start == BM_MSG_STOP_PATTERN) {
		usart_getchar(BM_USART_USART, &start);
	}
	if (start != BM_MSG_START_PATTERN) {
		return false;
	}
	// Check second character is length
	usart_getchar(BM_USART_USART, &length);
	if (length != 5) {
		return false;
	}
	// Check third character is Current Command
	usart_getchar(BM_USART_USART, &cmd_id);
	if (cmd_id != BM_RET_FIRMWARE_VERSION) {
		return false;
	}

	// Get Fifo free size
	usart_getchar(BM_USART_USART, &c);
	*fw_major_version = c;
	usart_getchar(BM_USART_USART, &c);
	*fw_minor_version = c;

	// Check last character is stop pattern
	usart_getchar(BM_USART_USART, &stop);
	if (stop != BM_MSG_STOP_PATTERN) {
		return false;
	}
	usart_disable_rx(BM_USART_USART);
	return true;
}
Exemplo n.º 10
0
bool bm_get_fifo_free_size(uint16_t* free_size)
{
        uint32_t start, length, stop, cmd_id, c;

	// Wait for some microseconds in order to avoid fifo overrun
	//
	delay_ms(20);

	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 3 /* length */);
	usart_putchar(BM_USART_USART, BM_MCU_GET_FIFO_FREE_SIZE);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);

	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
	usart_enable_rx(BM_USART_USART);
	
	// Check first character is start pattern
	usart_getchar(BM_USART_USART, &start);
	if (start == BM_MSG_STOP_PATTERN) {
		usart_getchar(BM_USART_USART, &start);
	}
	if (start != BM_MSG_START_PATTERN) {
		return false;
	}
	// Check second character is length
	usart_getchar(BM_USART_USART, &length);
	if (length != 5) {
		return false;
	}
	// Check third character is Current Command
	usart_getchar(BM_USART_USART, &cmd_id);
	if (cmd_id != BM_MCU_RET_FIFO_FREE_SIZE) {
		return false;
	}

	// Get Fifo free size
	usart_getchar(BM_USART_USART, &c);
	*free_size = c << 8;
	usart_getchar(BM_USART_USART, &c);
	*free_size |= c;

	// Check last character is stop pattern
	usart_getchar(BM_USART_USART, &stop);
	if (stop != BM_MSG_STOP_PATTERN) {
		return false;
	}
	usart_disable_rx(BM_USART_USART);
	return true;
}
Exemplo n.º 11
0
void bm_tgl_button(uint32_t timeout_ms)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 7 /* length */);
	usart_putchar(BM_USART_USART, BM_TGL_BUTTON);
	usart_putchar(BM_USART_USART, timeout_ms & 0xff);
	usart_putchar(BM_USART_USART, (timeout_ms >> 8 ) & 0xff);
	usart_putchar(BM_USART_USART, (timeout_ms >> 16) & 0xff);
	usart_putchar(BM_USART_USART, timeout_ms >> 24);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemplo n.º 12
0
void bm_send_picouart_frame(uint8_t frame, uint32_t timeout_ms)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 8 /* length */);
	usart_putchar(BM_USART_USART, BM_PICOUART_SEND);
	usart_putchar(BM_USART_USART, frame);
	usart_putchar(BM_USART_USART, timeout_ms & 0xff);
	usart_putchar(BM_USART_USART, (timeout_ms >> 8 ) & 0xff);
	usart_putchar(BM_USART_USART, (timeout_ms >> 16) & 0xff);
	usart_putchar(BM_USART_USART, timeout_ms >> 24);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemplo n.º 13
0
void bm_send_mcu_status(uint32_t power_scaling, uint32_t sleep_mode,
		uint32_t cpu_freq, uint32_t cpu_src)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 10 /* length */);
	usart_putchar(BM_USART_USART, BM_MCU_STATUS);
	usart_putchar(BM_USART_USART, power_scaling);
	usart_putchar(BM_USART_USART, sleep_mode);
	usart_putchar(BM_USART_USART, cpu_freq >> 24);
	usart_putchar(BM_USART_USART, (cpu_freq >> 16) & 0xff);
	usart_putchar(BM_USART_USART, (cpu_freq >> 8 ) & 0xff);
	usart_putchar(BM_USART_USART, cpu_freq & 0xff);
	usart_putchar(BM_USART_USART, cpu_src);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
/**
* \brief transmit data.
*
* \param *p_buff  data to be transmitted
* \param ulsize size of all data.
*
*/
uint8_t func_transmit(const uint8_t *p_buff, uint32_t ulsize)
{
	Assert(p_buff);

	while(ulsize > 0) {
		if(0 == usart_write(BOARD_USART, *p_buff)){
			usart_enable_interrupt(BOARD_USART, US_IER_TXRDY | US_IER_TXEMPTY);
			ulsize--;
			p_buff++;
		}
	}

	while(!usart_is_tx_empty(BOARD_USART)) {
		;  /*waiting for transmit over*/
	}

	return 0;
}
Exemplo n.º 15
0
/**
 * \brief Test wait mode.
 */
static void test_wait_mode(void)
{
	puts(STRING_WAIT);

#if SAMG55
	/* Wait for the transmission done before changing clock */
	while (!usart_is_tx_empty(CONSOLE_UART)) {
	}
#else
	/* Wait for the transmission done before changing clock */
	while (!uart_is_tx_empty(CONSOLE_UART)) {
	}
#endif

	/* Configure fast RC oscillator */
	pmc_switch_mck_to_sclk(PMC_MCKR_PRES_CLK_1);
#if (SAMG)
	pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
#else
	pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
#endif
	pmc_switch_mck_to_mainck(PMC_PCK_PRES_CLK_1);

#if (SAMG)
	g_ul_current_mck = 8000000; /* 8MHz */
#else
	g_ul_current_mck = 4000000; /* 4MHz */
#endif
	/* Disable unused clock to save power */
	pmc_osc_disable_xtal(0);
	example_disable_pll();

	/* Set wakeup input for fast startup */
	example_set_wakeup_from_wait_mode();

	/* Enter into wait Mode */
	pmc_enable_waitmode();

	/* Set default clock and re-configure UART */
	set_default_working_clock();
	reconfigure_console(g_ul_current_mck, CONF_UART_BAUDRATE);

	puts("Exit from wait Mode.\r");
}
/**
* \brief transmit data in synchronous mode.
*
* \param *p_buff  data to be transmitted
* \param ulsize size of all data.
*
*/
static uint8_t transmit_mode_sync(uint8_t *p_buff, uint32_t ulsize)
{
	Assert(p_buff);

	while(ulsize > 0) {
		if(0 == usart_write(BOARD_USART, *p_buff)){
			usart_enable_interrupt(BOARD_USART, US_IER_TXRDY | US_IER_TXEMPTY);
			ulsize--;
			p_buff++;
		}
	}

	while(!usart_is_tx_empty(BOARD_USART)) {
				;  /*waiting for transmit over*/
	}

	g_ul_sent_done = true;
	return 0;
}
Exemplo n.º 17
0
/**
 * \brief Initialize the chip for low power test.
 */
static void init_chip(void)
{
#if SAMG55
	/* Wait for the transmission done before changing clock */
	while (!usart_is_tx_empty(CONSOLE_UART)) {
	}
#else
	/* Wait for the transmission done before changing clock */
	while (!uart_is_tx_empty(CONSOLE_UART)) {
	}
#endif

	/* Disable all the peripheral clocks */
	pmc_disable_all_periph_clk();

	/* Disable brownout detector */
	supc_disable_brownout_detector(SUPC);

	/* Initialize the specific board */
	init_specific_board();
}
Exemplo n.º 18
0
void bm_print_txt(uint8_t* str, uint8_t str_length)
{
#define BM_PRINT_TEXT_SIZE                  21
#define BM_PRINT_TEXT_DEEP                  6
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, BM_PRINT_TEXT_SIZE + 3 /* length */);
	usart_putchar(BM_USART_USART, BM_PRINT_TEXT);
	if (str_length < BM_PRINT_TEXT_SIZE) {
		for (uint8_t i=0;i<str_length;i++){
			usart_putchar(BM_USART_USART, str[i]);
		}
		for (uint8_t i=0;i < BM_PRINT_TEXT_SIZE - str_length;i++){
			usart_putchar(BM_USART_USART, '\0');
		}
	} else {
		for (uint8_t i=0;i<BM_PRINT_TEXT_SIZE;i++){
			usart_putchar(BM_USART_USART, str[i]);
		}
	}
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemplo n.º 19
0
/*! \brief Check whether there are data in Transmit Holding Register or
 *         Transmit Shift Register in SPI master mode.
 *
 * \param p_usart Base address of the USART instance.
 *
 * \retval 1      The two registers are empty.
 * \retval 0      One of the two registers contains data.
 */
uint32_t usart_spi_is_tx_empty(Usart *p_usart)
{
	return usart_is_tx_empty(p_usart);
}
Exemplo n.º 20
0
/**
 * \brief Change clock configuration.
 *
 * \param p_uc_str Hint string to be output on console before changing clock.
 */
static void user_change_clock(uint8_t *p_uc_str)
{
	uint8_t uc_key;
	uint32_t ul_id;

	/* Print menu */
	puts(CLOCK_LIST_MENU);

	scanf("%c", (char *)&uc_key);
	printf("Select option is: %c\n\r\n\r", uc_key);
	if (p_uc_str) {
		puts((char const *)p_uc_str);
	}

#if SAMG55
	/* Wait for the transmission done before changing clock */
	while (!usart_is_tx_empty(CONSOLE_UART)) {
	}
#else
	/* Wait for the transmission done before changing clock */
	while (!uart_is_tx_empty(CONSOLE_UART)) {
	}
#endif

	if ((uc_key >= MIN_CLOCK_FAST_RC_ITEM) &&
			(uc_key <= MAX_CLOCK_FAST_RC_ITEM)) {
		ul_id = uc_key - MIN_CLOCK_FAST_RC_ITEM;

		/* Save current clock */
		g_ul_current_mck = g_fastrc_clock_list[ul_id][0];

		/* Switch MCK to Slow clock  */
		pmc_switch_mck_to_sclk(PMC_MCKR_PRES_CLK_1);

		/* Switch mainck to fast RC */
		pmc_osc_enable_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
		pmc_switch_mainck_to_fastrc(g_fastrc_clock_list[ul_id][1]);

		/* Switch MCK to mainck */
		pmc_switch_mck_to_mainck(g_fastrc_clock_list[ul_id][2]);

		/* Disable unused clock to save power */
		pmc_osc_disable_xtal(0);
		example_disable_pll();

	} else if ((uc_key >= MIN_CLOCK_PLL_ITEM) &&
			(uc_key <= MAX_CLOCK_PLL_ITEM)) {
		ul_id = uc_key - MIN_CLOCK_PLL_ITEM;

		/* Save current clock */
		g_ul_current_mck = g_pll_clock_list[ul_id][0];

#if (SAMG)
		/* Switch MCK to main clock  */
		pmc_switch_mck_to_mainck(PMC_MCKR_PRES_CLK_1);
#else
		/* Switch MCK to slow clock  */
		pmc_switch_mck_to_sclk(PMC_MCKR_PRES_CLK_1);

		/* Switch mainck to external xtal */
		pmc_switch_mainck_to_xtal(0, BOARD_OSC_STARTUP_US);
#endif
		/* Configure PLL and switch clock */
		example_switch_clock(g_pll_clock_list[ul_id][1], PLL_COUNT,
				g_pll_clock_list[ul_id][2], g_pll_clock_list[ul_id][3]);

#if (!SAMG)
		/* Disable unused clock to save power */
		pmc_osc_disable_fastrc();
#endif
	} else {
		puts("Clock is not changed.\r");
	}
}
Exemplo n.º 21
0
/**
 * \brief Wait until test assert is done (no wave on assert port)
 */
static inline void wait_test_assert_idle(void)
{
	while(!usart_is_tx_empty(CONF_TEST_USART));
}