Ejemplo 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);

	/* Wait for the transmission done before changing clock */
	while (!uart_is_tx_empty(CONSOLE_UART)) {
	}

	/* 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. */
}
Ejemplo n.º 2
0
/**
 * Set configuration parameters in GPBR
 */
static void save_config(uint8_t cmd_start_mode)
{
	uint32_t ul_gpbr_value;

	ul_gpbr_value = cmd_start_mode;
	ul_gpbr_value += xAppPhyCfgTx.uc_coupling << 4;
	ul_gpbr_value += xAppPhyCfgTx.uc_channel << 12;
	ul_gpbr_value += xAppPhyCfgTx.xPhyMsg.uc_buff_id << 16;
	ul_gpbr_value += xAppPhyCfgTx.xPhyMsg.att_level << 20;
	ul_gpbr_value += xAppPhyCfgTx.xPhyMsg.scheme << 25;
	ul_gpbr_value += xAppPhyCfgTx.xPhyMsg.disable_rx << 28;
	ul_gpbr_value += xAppPhyCfgTx.xPhyMsg.mode << 29;
	gpbr_write(GPBR0, ul_gpbr_value);
	gpbr_write(GPBR1, xAppPhyCfgTx.ul_tx_period);
	ul_gpbr_value = xAppPhyCfgTx.xPhyMsg.data_len;
	ul_gpbr_value += xAppPhyCfgTx.uc_autodetect << 16;
	ul_gpbr_value += xAppPhyCfgTx.uc_impedance << 20;
	gpbr_write(GPBR2, ul_gpbr_value);
}
Ejemplo n.º 3
0
//set the relay and store the new state in 
//the backup register in case of software reset
int
mon_relay(int argc, char **argv){
  int state;
  state = gpbr_read(GPBR_RELAY_STATE);
  if(argc==1){ //print the relay state
    if(state)
      printf("relay [on]\n");
    else
      printf("relay [off]\n");
    return -1;
  }
  if(argc!=2){
    printf("relay [on|off|toggle]\n");
    return -1;
  }
  if(strcmp(argv[1],"on")==0){
    gpio_set_pin_high(RELAY_PIN);
    gpbr_write(GPBR_RELAY_STATE,1);
  }
  else if(strcmp(argv[1],"off")==0){
    gpio_set_pin_low(RELAY_PIN);
    gpbr_write(GPBR_RELAY_STATE,0);
  }
  else if(strstr(argv[1],"toggle")==argv[1]){
    gpio_toggle_pin(RELAY_PIN);
    if(state==0)
      state = 1;
    else
      state = 0;
    gpbr_write(GPBR_RELAY_STATE,state);
  }
  else{
    printf("bad argument to relay\n");
    return -1;
  }
  return 0;
}
Ejemplo n.º 4
0
/**
 * \brief Test GPBR read/write interfaces.
 *
 * \param test Current test case.
 */
static void run_gpbr_test(const struct test_case *test)
{
	uint32_t ul_read_value = 0;
	uint32_t ul_test_page_addr = TEST_PAGE_ADDRESS;
	uint32_t *ul_back_mode_flag_addr = (uint32_t *) ul_test_page_addr;
	uint32_t ul_normal_mode_flag = NORMAL_MODE_FLAG;
	uint32_t ul_backup_mode_flag = BACKUP_MODE_FLAG;
	uint8_t uc_write_success_flag = 1;

	/* Initialize flash: 6 wait states for flash writing. */
	flash_init(FLASH_ACCESS_MODE_128, FLASH_WAIT_STATE_NBR);

	/* Unlock flash page. */
	flash_unlock(ul_test_page_addr,
			ul_test_page_addr + IFLASH_PAGE_SIZE - 1, NULL, NULL);

	if ((*ul_back_mode_flag_addr) == BACKUP_MODE_FLAG) {
		/* Read the data from GPBR0 */
		ul_read_value = gpbr_read(GPBR0);

#if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CP || SAM4CM || SAMV70 || SAMV71 || SAME70 || SAMS70)
		/* Erase flag page */
		flash_erase_page(ul_test_page_addr, IFLASH_ERASE_PAGES_8);

		/* Clear backup mode flag */
		if (flash_write(ul_test_page_addr, (uint8_t *)&ul_normal_mode_flag,
				sizeof(uint32_t), 0) != FLASH_RC_OK) {
			uc_write_success_flag = 0;
		}
#else
		/* Clear backup mode flag */
		if (flash_write(ul_test_page_addr, (uint8_t *)&ul_normal_mode_flag,
				sizeof(uint32_t), 1) != FLASH_RC_OK) {
			uc_write_success_flag = 0;
		}
#endif

		/* Return test result */
		test_assert_true(test, (ul_read_value == GPBR_UNIT_TEST_CONST_DATA)
				&& uc_write_success_flag, "Test GPBR: GPBR write error!");

		/* Clear GPBR 0 */
		gpbr_write(GPBR0, 0);

		return;
	}

	/* Write the data to the backup register 0 */
	gpbr_write(GPBR0, GPBR_UNIT_TEST_CONST_DATA);

	/* Enable RTT wake up */
	supc_set_wakeup_mode(SUPC, SUPC_WUMR_RTTEN);

	/* Configure RTT */
	gpbr_test_configure_rtt();

	/* Wait for RTT alarm event */
	rtt_write_alarm_time(RTT, RTT_WAIT_TIME);

#if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CP || SAM4CM || SAMV70 || SAMV71 || SAME70 || SAMS70)
	/* Erase flag page */
	if(flash_erase_page(ul_test_page_addr, IFLASH_ERASE_PAGES_8) != FLASH_RC_OK)
		printf("erase page failed!\r\n");

	/* Write backup mode flag */
	if (flash_write(ul_test_page_addr, (uint8_t *) & ul_backup_mode_flag,
					sizeof(uint32_t), 0) != FLASH_RC_OK) {
		/* Flag write failed, return error */
		test_assert_true(test, 0, "Test GPBR: GPBR write error!");
	}
#else
	/* Write backup mode flag */
	if (flash_write(ul_test_page_addr, (uint8_t *) & ul_backup_mode_flag,
					sizeof(uint32_t), 1) != FLASH_RC_OK) {
		/* Flag write failed, return error */
		test_assert_true(test, 0, "Test GPBR: GPBR write error!");
	}
#endif

	/* Enter backup mode */
	pmc_enable_backupmode();
#if (!(SAM4S) && !(SAM4E) && !(SAM4N) && !(SAM4C) && !(SAM4CP) && !(SAM4CM) && !(SAMV70) && !(SAMV71) && !(SAME70) && !(SAMS70))
	supc_enable_backup_mode(SUPC);
#endif

	/* We should never reach here */
	test_assert_true(test, 0, "Test GPBR: GPBR write error!");
}
Ejemplo n.º 5
0
/**
 * \brief Application entry point for RTC tamper example.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint32_t ul_read_value[8] = {0, 0 ,0, 0, 0, 0, 0, 0};
	uint32_t ul_hour0, ul_minute0, ul_second0;
	uint32_t ul_year0, ul_month0, ul_day0, ul_week0;
	uint32_t ul_hour1, ul_minute1, ul_second1;
	uint32_t ul_year1, ul_month1, ul_day1, ul_week1;
	uint32_t tmp_src0, tmp_src1, tmp_cnt;
	uint8_t uc_key;

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

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

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

	/* Default RTC configuration, 24-hour mode */
	rtc_set_hour_mode(RTC, 0);
	rtc_set_waveform(RTC, RTC_OUT_CHN, RTC_OUT_SRC);

	if(rstc_get_reset_cause(RSTC) == RSTC_SR_RSTTYP_BackupReset) {
		/* Read the data from GPBR0 ~ 7 */
		ul_read_value[0] = gpbr_read(GPBR0);
		ul_read_value[1] = gpbr_read(GPBR1);
		ul_read_value[2] = gpbr_read(GPBR2);
		ul_read_value[3] = gpbr_read(GPBR3);
		ul_read_value[4] = gpbr_read(GPBR4);
		ul_read_value[5] = gpbr_read(GPBR5);
		ul_read_value[6] = gpbr_read(GPBR6);
		ul_read_value[7] = gpbr_read(GPBR7);

		if((ul_read_value[0] == 0) &&
				(ul_read_value[1] == 0) &&
				(ul_read_value[2] == 0) &&
				(ul_read_value[3] == 0) &&
				(ul_read_value[4] == 0) &&
				(ul_read_value[5] == 0) &&
				(ul_read_value[6] == 0) &&
				(ul_read_value[7] == 0)) {
			printf("The backup register is cleared when tamper event happen!\r\n");
		} else {
			printf("The backup register is not cleared when tamper event happen!\r\n");
		}

		/* Retrieve tamper date and time */
		rtc_get_tamper_time(RTC, &ul_hour0, &ul_minute0, &ul_second0, 0);
		rtc_get_tamper_date(RTC, &ul_year0, &ul_month0, &ul_day0, &ul_week0, 0);
		rtc_get_tamper_time(RTC, &ul_hour1, &ul_minute1, &ul_second1, 1);
		rtc_get_tamper_date(RTC, &ul_year1, &ul_month1, &ul_day1, &ul_week1, 1);
		tmp_cnt = rtc_get_tamper_event_counter(RTC);
		tmp_src0 = rtc_get_tamper_source(RTC, 0);
		tmp_src1 = rtc_get_tamper_source(RTC, 1);
		printf("The first tamper event TMP%u happen in %02u:%02u:%02u,%02u/%02u/%04u\r\n",
				(unsigned int)tmp_src0, (unsigned int)ul_hour0, (unsigned int)ul_minute0,
				(unsigned int)ul_second0, (unsigned int)ul_month0, (unsigned int)ul_day0,
				(unsigned int)ul_year0);
		printf("The last tamper event TMP%u happen in %02u:%02u:%02u,%02u/%02u/%04u\r\n",
				(unsigned int)tmp_src1, (unsigned int)ul_hour1, (unsigned int)ul_minute1,
				(unsigned int)ul_second1, (unsigned int)ul_month1, (unsigned int)ul_day1,
				(unsigned int)ul_year1);
		printf("The tamper event counter is %u \r\n", (unsigned int)tmp_cnt);
	}

	printf("Press any key to Enter Backup Mode!\r\n");

	while (uart_read(CONSOLE_UART, &uc_key));

	/* Write the data to the backup register GPBR0 ~ 7 */
	gpbr_write(GPBR0, GPBR_CONST_DATA);
	gpbr_write(GPBR1, GPBR_CONST_DATA);
	gpbr_write(GPBR2, GPBR_CONST_DATA);
	gpbr_write(GPBR3, GPBR_CONST_DATA);
	gpbr_write(GPBR4, GPBR_CONST_DATA);
	gpbr_write(GPBR5, GPBR_CONST_DATA);
	gpbr_write(GPBR6, GPBR_CONST_DATA);
	gpbr_write(GPBR7, GPBR_CONST_DATA);

	/* Enable TMP0 and TMP2 low power debouncer and clear GPBR when event happen */
	supc_set_wakeup_mode(SUPC, SUPC_WUMR_LPDBCEN0_ENABLE |
			SUPC_WUMR_LPDBCCLR_ENABLE | SUPC_WUMR_LPDBCEN2_ENABLE |
			SUPC_WUMR_LPDBC_2_RTCOUT0);
	/* Enable TMP0 and TMP2 wake-up input and set input type*/
	supc_set_wakeup_inputs(SUPC, SUPC_WUIR_WKUPEN0_ENABLE |
			SUPC_WUIR_WKUPEN14_ENABLE,
			SUPC_WUIR_WKUPT0_LOW | SUPC_WUIR_WKUPT14_LOW);

	printf("Enter Backup Mode!\r\n");
	printf("Please press button TMP0 or TMP2 to wake up!\r\n\r\n");

	/* Ensure TX is done before enter backup mode */
	while (!uart_is_tx_empty(CONSOLE_UART)) {
	}

	/* Enter backup mode */
	pmc_enable_backupmode();

	while (1) {
	}
}