/** * \brief Test sleep Mode. */ static void test_sleep_mode(void) { /* Configure button for waking up sleep mode */ configure_button(); /* Select clock for sleep mode */ user_change_clock(STRING_SLEEP); /* Disable UART */ pmc_disable_periph_clk(CONSOLE_UART_ID); /* Enter into sleep Mode */ pmc_enable_sleepmode(0); /* Set default clock and re-configure UART */ set_default_working_clock(); reconfigure_console(g_ul_current_mck, CONF_UART_BAUDRATE); puts("Exit from sleep Mode.\r"); }
/** * \brief getting-started Application entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { /* Initialize the SAM system */ sysclk_init(); board_init(); /* Initialize the console uart */ configure_console(); /* Output example information */ puts(STRING_HEADER); /* Configure systick for 1 ms */ puts("Configure system tick to get 1ms tick period.\r"); if (SysTick_Config(sysclk_get_cpu_hz() / BLINK_PERIOD)) { puts("-F- Systick configuration error\r"); while (1); } puts("Configure button.\r"); configure_button(); puts("Configure TC.\r"); configure_tc(); printf("Press %s to Start/Stop LED0 blinking.\r\n", BUTTON_0_NAME); while (1) { /* Wait for LED to be active */ while (!g_b_led0_active); /* Toggle LED state if active */ if (g_b_led0_active == 1) { LED_Toggle(LED0); printf("1 "); } /* Wait for 500ms */ mdelay(500); } }
/** * \brief Test active mode. */ static void test_active_mode(void) { /* Configure button for exiting from active mode */ configure_button(); g_ul_button_pressed = 0; /* Select clock for active mode */ user_change_clock(STRING_ACTIVE); /* Test active mode */ do { /* Run Fibonacci calculation, n = 10 (may be changed) */ fib(10); } while (!g_ul_button_pressed); /* Set default clock and re-configure UART */ set_default_working_clock(); reconfigure_console(g_ul_current_mck, CONF_UART_BAUDRATE); puts("Exit from active mode.\r"); }
/*---------------------------------------------------------------------------*/ static int configure(int type, int value) { switch(type) { case SENSORS_HW_INIT: configure_button(); return 1; case SENSORS_ACTIVE: if(value) { if(!interrupt_enabled) { timer_set(&debouncetimer, 0); extint_chan_enable_callback(BUTTON_0_EIC_LINE, EXTINT_CALLBACK_TYPE_DETECT); interrupt_enabled = 1; } } else { extint_chan_disable_callback(BUTTON_0_EIC_LINE, EXTINT_CALLBACK_TYPE_DETECT); interrupt_enabled = 0; } return 1; } return 0; }
/** * \brief Application entry point for WDT example. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint32_t wdt_mode, timeout_value; /* Initilize the system */ sysclk_init(); board_init(); /* Configure pins of console UART, LED and push button on board. */ configure_console(); configure_led(); configure_button(); /* Output example information. */ puts(STRING_HEADER); /* Systick configuration. */ puts("Configure systick to get 1ms tick period.\r"); if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) { puts("-F- Systick configuration error\r"); } /* Get timeout value. */ timeout_value = wdt_get_timeout_value(WDT_PERIOD * 1000, BOARD_FREQ_SLCK_XTAL); if (timeout_value == WDT_INVALID_ARGUMENT) { while (1) { /* Invalid timeout value, error. */ } } /* Configure WDT to trigger an interrupt (or reset). */ wdt_mode = WDT_MR_WDFIEN | /* Enable WDT fault interrupt. */ WDT_MR_WDRPROC | /* WDT fault resets processor only. */ WDT_MR_WDDBGHLT | /* WDT stops in debug state. */ WDT_MR_WDIDLEHLT; /* WDT stops in idle state. */ /* Initialize WDT with the given parameters. */ wdt_init(WDT, wdt_mode, timeout_value, timeout_value); printf("Enable watchdog with %d microseconds period\n\r", (int)wdt_get_us_timeout_period(WDT, BOARD_FREQ_SLCK_XTAL)); /* Configure and enable WDT interrupt. */ NVIC_DisableIRQ(WDT_IRQn); NVIC_ClearPendingIRQ(WDT_IRQn); NVIC_SetPriority(WDT_IRQn, 0); NVIC_EnableIRQ(WDT_IRQn); /* Initialize and enable push button (PIO) interrupt. */ pio_handler_set_priority(PUSHBUTTON_PIO, PUSHBUTTON_IRQn, 0); pio_enable_interrupt(PUSHBUTTON_PIO, PUSHBUTTON_MASK); printf("Press %s to simulate a deadlock loop.\n\r", PUSHBUTTON_STRING); while (1) { if (g_b_systick_event == true) { g_b_systick_event = false; /* Toggle LED at the given period. */ if ((g_ul_ms_ticks % BLINK_PERIOD) == 0) { #if (SAM4E || SAM4N || SAM4C || SAMG) LED_Toggle(LED0); #else LED_Toggle(LED0_GPIO); #endif } /* Restart watchdog at the given period. */ if ((g_ul_ms_ticks % WDT_RESTART_PERIOD) == 0) { wdt_restart(WDT); } } /* Simulate deadlock when button is pressed. */ if (g_b_button_event == true) { puts("Program enters infinite loop for triggering watchdog interrupt.\r"); while (1) { } } } }
int main(void) { uint32_t i; uint32_t ul_error; volatile uint32_t *p_test_page_data; uint32_t p_buffer[BUFFER_SIZE]; /* Initialize the system. */ sysclk_init(); board_init(); /* Configure UART for debug message output. */ configure_console(); /* Initialize flash: 6 wait states for flash writing. */ flash_init(FLASH_ACCESS_MODE_128, FLASH_WAIT_STATE_NBR); /* Configure Push Button. */ configure_button(); /* Output example information. */ puts(STRING_HEADER); puts("-I- Unlocking the whole flash.\r\n"); /* Unlock the whole flash. */ ul_error = flash_unlock(IFLASH_ADDR, (IFLASH_ADDR + IFLASH_SIZE - 1), 0, 0); if (FLASH_RC_OK != ul_error) { puts("Unlock internal flash failed.\r\n"); return 0; } /* Perform tests on the test page. */ p_test_page_data = (volatile uint32_t *)TEST_PAGE_ADDRESS; /* Write page with walking bit pattern (0x00000001, 0x00000002, ...). */ puts("-I- Writing test page with walking bit pattern.\r\n"); for (i = 0; i < BUFFER_SIZE; i++) { p_buffer[i] = 1 << (i % MAX_SHIFTING_NUMBER); } #if (SAM4E || SAM4C || SAMV71) /** * The EWP command can only be used in 8 KBytes sector for SAM4E, * so an erase command is requried before write operation. */ ul_error = flash_erase_sector(TEST_PAGE_ADDRESS); if (ul_error != FLASH_RC_OK) { printf("-F- Flash erase error %u\n\r", ul_error); return 0; } ul_error = flash_write(TEST_PAGE_ADDRESS, p_buffer, IFLASH_PAGE_SIZE, 0); #else ul_error = flash_write(TEST_PAGE_ADDRESS, p_buffer, IFLASH_PAGE_SIZE, 1); #endif if (FLASH_RC_OK != ul_error) { puts("Write the test page of internal flash failed.\r\n"); return 0; } /* Check page contents. */ puts("-I- Checking page contents.\r\n"); for (i = 0; i < BUFFER_SIZE; i++) { printf("."); if (p_test_page_data[i] != (1u << (i % MAX_SHIFTING_NUMBER))) { puts("The content in the test page isn't written correctly"); return 0; } } puts(" OK! \r\n"); /* Configure Erase pin NOT in Erase mode. */ puts("-I- Configure Erase pin in PIO mode.\r\n"); matrix_set_system_io(PIN_PIO_MODE_MSK); /** * Ask the user to close the erase jumper and then open it(200ms minimum). */ printf("-I- Please close the erase jumper and then open it "); printf("at least 200ms later.\r\n"); printf("Then press button %s to go on!\r\n", BUTTON_STRING); /* Wait until Push Button is pressed. */ while (!g_button_event) { } /** * Disable the PIO line interrupts to eliminate the wrong check of * key press. */ pio_disable_interrupt(PUSH_BUTTON_PIO, PUSH_BUTTON_PIN_MSK); g_button_event = 0; /* Read the page again, it should be unchanged. */ puts("-I- Reading the page\r\n"); for (i = 0; i < BUFFER_SIZE; i++) { printf("."); if (p_test_page_data[i] != (1u << (i % MAX_SHIFTING_NUMBER))) { puts("-F- Reading Error! \r\n"); return 0; } } puts("\r\n"); puts("Read OK! Erase is out of function!\r\n"); /* Configure Erase pin as Erase function. */ puts("-I- Configure Erase pin as Erase function\r\n"); matrix_set_system_io(PIN_ERASE_MODE_MSK); /** * Ask the user to close the erase jumper and then open it(200ms minimum). */ printf("-I- Please close the erase jumper and then open it "); printf("at least 200ms later.\r\n"); /** * Remind the users that after closing the erase jumper and then opening * it, codes are gone. */ printf("-I- As the internal flash has been erased and the code can't "); printf("be executed any more, users can press the reset button on EK "); printf("and will see there will be no output message any more.\r\n"); while (1) { } }