2016-05-16 164 views
0

在解釋的專業開發板上發現Atmel SAMB11存在問題。我已經加載了一個來自Atmel的非常簡單的例子,其中32KHz定時器被初始化以從睡眠中喚醒μC並打開LED。問題是,控制器根本沒有睡覺。它只是立即激活LED,不會等待中斷。Cortex M0未進入睡眠模式

#include <asf.h> 

// Callback Func to enable LED 
static void aon_sleep_timer_callback(void) 
{ 
    gpio_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE); 
} 
//Configure LED 
static void configure_gpio_pins(void) 
{ 
    struct gpio_config config_gpio_pin; 
    gpio_get_config_defaults(&config_gpio_pin); 
    config_gpio_pin.direction = GPIO_PIN_DIR_OUTPUT; 
    gpio_pin_set_config(LED_0_PIN, &config_gpio_pin); 
    gpio_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE); 
} 
// Configure Timer with 10sec to overflow 
static void configure_aon_sleep_timer(void) 
{ 
    struct aon_sleep_timer_config config_aon_sleep_timer; 
    aon_sleep_timer_get_config_defaults(&config_aon_sleep_timer); 
    config_aon_sleep_timer.counter = 320000; // Wait about 10sec 
    aon_sleep_timer_init(&config_aon_sleep_timer); 
} 
// Configure Callback and enable Interrupt 
static void configure_aon_sleep_timer_callback(void) 
{ 
    aon_sleep_timer_register_callback(aon_sleep_timer_callback); 
    NVIC_EnableIRQ(AON_SLEEP_TIMER_IRQn); 
} 

int main(void) 
{ 
    // Setup Clock, LED and Timer 
    system_clock_config(CLOCK_RESOURCE_XO_26_MHZ, CLOCK_FREQ_26_MHZ); 
    configure_gpio_pins(); 
    configure_aon_sleep_timer(); 
    configure_aon_sleep_timer_callback(); 

    // wait for timer to be active 
    while(!aon_sleep_timer_sleep_timer_active()); 
    // Go to sleep 
    asm volatile ("wfi"); 
    asm volatile ("nop"); 
    // Enable LED immediately if sleep doesn't work 
    gpio_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE); 
    while (true) {} 
} 

代碼似乎不言自明,但WFI命令在這裏不起作用。任何人都可以幫忙

+1

'aon_sleep_timer_sleep_timer_active()'做了什麼 - 它只是輪詢一個狀態寄存器或什麼?使用兩個不同的LED會很方便,因此您可以判斷您是實際上是在立即採取中斷還是由於其他事件而中斷。 – Notlikethat

回答

0

WFI調用起作用,它在調用後幾乎立即收到一箇中斷,這會導致WFI調用停止阻塞,然後繼續執行到下一行。

你可以安全地刪除所有低於// Go to sleep的東西,這將允許主函數返回。 AON計時器仍然會執行其回調。但是,這種方法存在一些潛在的缺點:

  • 這將不允許SAMB11轉換到較低功耗模式。
  • 這將刪除您在主循環結束時的while循環。在當前狀態下,while循環不是必需的,但您可能有計劃稍後添加代碼。

下面是配置AON,配置SAMB11使用低功耗模式,然後循環等待平臺和/或BLE事件的示例。目前沒有任何循環接收事件。如果您希望迴路接收事件,則可以修改AON回調以使用at_ble_event_user_defined_post函數發佈事件或修改main()以在進入迴路之前配置BLE模塊。

使用ASF嚮導將任何BLE模塊添加到您的項目中以編譯此示例。

#include <asf.h> 
#include "platform.h" 

// Configure LED 
static void configure_gpio_pins(void) 
{ 
    struct gpio_config config_gpio_pin; 
    gpio_get_config_defaults(&config_gpio_pin); 
    config_gpio_pin.direction = GPIO_PIN_DIR_OUTPUT; 
    gpio_pin_set_config(LED_0_PIN, &config_gpio_pin); 
    gpio_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE); 
} 

// Callback Func to toggle LED 
static bool led_is_on = false; 
static void aon_sleep_timer_callback(void) 
{ 
    configure_gpio_pins(); 
    if(led_is_on) { 
     gpio_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE); 
     led_is_on = false; 
    } else { 
     gpio_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE); 
     led_is_on = true; 
    } 
} 

// Configure Timer to fire periodically 
static void configure_aon_sleep_timer(void) 
{ 
    struct aon_sleep_timer_config config_aon_sleep_timer; 
    aon_sleep_timer_get_config_defaults(&config_aon_sleep_timer); 
    config_aon_sleep_timer.counter = 32000; // Wait about 1 sec 
    config_aon_sleep_timer.mode = AON_SLEEP_TIMER_RELOAD_MODE; 
    aon_sleep_timer_init(&config_aon_sleep_timer); 
} 
// Configure Callback and enable Interrupt 
static void configure_aon_sleep_timer_callback(void) 
{ 
    aon_sleep_timer_register_callback(aon_sleep_timer_callback); 
    NVIC_EnableIRQ(AON_SLEEP_TIMER0_IRQn); 
} 

int main(void) 
{ 
    // Setup Clock 
    system_clock_config(CLOCK_RESOURCE_XO_26_MHZ, CLOCK_FREQ_26_MHZ); 

    plf_drv_status plf_status; 
    if((plf_status = platform_driver_init()) == STATUS_SUCCESS) { 

     // Setup LED and Timer 
     configure_gpio_pins(); 
     configure_aon_sleep_timer(); 
     configure_aon_sleep_timer_callback(); 

     // wait for timer to be active 
     while(!aon_sleep_timer_sleep_timer_active()); 

     // Go to sleep 
     release_sleep_lock(); 
     while(true) { 
      // Replace platform_event_wait with at_ble_event_get if you would like to read the received event. 
      plf_status = platform_event_wait(0); 
     } 
    } 
} 

關於WFI,下面的示例示出了如何關閉大多數中斷,並使用WFI到方框main()。每次接收到中斷時,LED都會切換。我不建議使用這個,因爲我不確定爲什麼最初啓用這些中斷。這只是爲了顯示WFI如何在SAMB11上阻塞。

#include <asf.h> 
#include "platform.h" 

// Configure LED 
static void configure_gpio_pins(void) 
{ 
    struct gpio_config config_gpio_pin; 
    gpio_get_config_defaults(&config_gpio_pin); 
    config_gpio_pin.direction = GPIO_PIN_DIR_OUTPUT; 
    gpio_pin_set_config(LED_0_PIN, &config_gpio_pin); 
    gpio_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE); 
} 

// Callback Func to toggle LED 
static bool led_is_on = false; 
static void toggle_led(void) 
{ 
    configure_gpio_pins(); 
    if(led_is_on) { 
     gpio_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE); 
     led_is_on = false; 
    } else { 
     gpio_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE); 
     led_is_on = true; 
    } 
} 

int main(void) 
{ 
    // Setup Clock 
    system_clock_config(CLOCK_RESOURCE_XO_26_MHZ, CLOCK_FREQ_26_MHZ); 

    // Clear all interrupts. 
    NVIC->ICER[0] = 0xFFFFFFFF; 

    // During testing, interrupts were received about once per second; stopped receiving interrupts (LED stopped flashing) after about 2 minutes. 
    int loop_count = 0; 
    while(true) { 
     __WFI(); 
     toggle_led(); 
    } 
} 
0

只是要添加到由Prestige Worldwide的答案。

確保AO_GPIO0/1/2爲低電平(建議下拉),並且沒有發生AON睡眠定時器中斷,因爲它們會將SAMB11從ULP中喚醒。

另請注意,在SWD上運行調試會話時,ULP模式似乎無法按預期工作。

當運行調試和睡眠/喚醒時,我有各種奇怪的行爲,但在沒有調試時運行相同的代碼時根本沒有問題。請注意,這是使用Atmel ICE。 Xplored板包含EDBG,這個調試器似乎可以與ULP一起工作。

簡歷回調從未爲我解僱過,也許是ASF中的一個bug。但我不需要它,因爲我可以在平臺等待後安裝所有GPIO /設備。

+0

這不提供問題的答案。一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你將能夠[評論任何職位](http://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/13200186) – Inian

+0

我已經提供了3個原因,爲什麼'Cortex M0不能進入睡眠模式'會出現,作者需要檢查它是否能夠解決問題。這不是一個答案? –

相關問題