[Atmel] 用ASF跑SAMD21程序(19)WDT CALLBACK

[复制链接]
3511|0
 楼主| ddllxxrr 发表于 2015-1-19 19:20 | 显示全部楼层 |阅读模式
本帖最后由 ddllxxrr 于 2015-1-19 19:56 编辑

首先新建ASF工程,然后加入WDT CALLBACK模块。


然后打开ASF EXPLORER打开里边的QUICK START



按照提示的步骤生成自己的程序。

注意程序里的个WDT时钟源指定。这个要注意,假如我要换的时钟源则程序得重新指定,并且,CLOCK_CONFIG的相应时钟源必须被配置。

然后编译,程序下进去后,不一会LED0灯亮了。

源程序如下:


  1. /**
  2. * \file
  3. *
  4. * \brief Empty user application template
  5. *
  6. */

  7. /**
  8. * \mainpage User Application template doxygen documentation
  9. *
  10. * \par Empty user application template
  11. *
  12. * This is a bare minimum user application template.
  13. *
  14. * For documentation of the board, go \ref group_common_boards "here" for a link
  15. * to the board-specific documentation.
  16. *
  17. * \par Content
  18. *
  19. * -# Include the ASF header files (through asf.h)
  20. * -# Minimal main function that starts with a call to system_init()
  21. * -# Basic usage of on-board LED and button
  22. * -# "Insert application code here" comment
  23. *
  24. */

  25. /*
  26. * Include header files for all drivers that have been imported from
  27. * Atmel Software Framework (ASF).
  28. */
  29. /**
  30. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  31. */
  32. #include <asf.h>
  33. void watchdog_early_warning_callback(void);
  34. void configure_wdt(void);
  35. void configure_wdt_callbacks(void);

  36. //! [setup]
  37. void watchdog_early_warning_callback(void)
  38. {
  39.         port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
  40. }

  41. void configure_wdt(void)
  42. {
  43.         /* Create a new configuration structure for the Watchdog settings and fill
  44.          * with the default module settings. */
  45.         //! [setup_1]
  46.         struct wdt_conf config_wdt;
  47.         //! [setup_1]
  48.         //! [setup_2]
  49.         wdt_get_config_defaults(&config_wdt);
  50.         //! [setup_2]

  51.         /* Set the Watchdog configuration settings */
  52.         //! [setup_3]
  53.         config_wdt.always_on            = false;
  54. #if !(SAML21)
  55.         config_wdt.clock_source         = GCLK_GENERATOR_2;
  56. #endif
  57.         config_wdt.timeout_period       = WDT_PERIOD_4096CLK;
  58.         config_wdt.early_warning_period = WDT_PERIOD_2048CLK;
  59.         //! [setup_3]

  60.         /* Initialize and enable the Watchdog with the user settings */
  61.         //! [setup_4]
  62.         wdt_set_config(&config_wdt);
  63.         //! [setup_4]
  64. }

  65. void configure_wdt_callbacks(void)
  66. {
  67.         //! [setup_5]
  68.         wdt_register_callback(watchdog_early_warning_callback,
  69.                 WDT_CALLBACK_EARLY_WARNING);
  70.         //! [setup_5]

  71.         //! [setup_6]
  72.         wdt_enable_callback(WDT_CALLBACK_EARLY_WARNING);
  73.         //! [setup_6]
  74. }
  75. //! [setup]

  76. int main(void)
  77. {
  78.         system_init();

  79.         //! [setup_init]
  80.         configure_wdt();
  81.         configure_wdt_callbacks();
  82.         //! [setup_init]

  83. //! [main]
  84. //! [main_1]
  85.         port_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE);
  86. //! [main_1]

  87. //! [main_2]
  88.         system_interrupt_enable_global();
  89. //! [main_2]

  90.         //! [main_3]
  91.         while (true) {
  92.                 /* Wait for callback */
  93.         }
  94.         //! [main_3]
  95. //! [main]
  96. }



时钟配置如下:


  1. /**
  2. * \file
  3. *
  4. * \brief SAM D21 Clock configuration
  5. *
  6. * Copyright (C) 2013-2014 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. *    this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. *    this list of conditions and the following disclaimer in the documentation
  20. *    and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. *    from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. *    Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /**
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #include <clock.h>

  47. #ifndef CONF_CLOCKS_H_INCLUDED
  48. #  define CONF_CLOCKS_H_INCLUDED

  49. /* System clock bus configuration */
  50. #  define CONF_CLOCK_CPU_CLOCK_FAILURE_DETECT     false
  51. #  define CONF_CLOCK_FLASH_WAIT_STATES            0
  52. #  define CONF_CLOCK_CPU_DIVIDER                  SYSTEM_MAIN_CLOCK_DIV_1
  53. #  define CONF_CLOCK_APBA_DIVIDER                 SYSTEM_MAIN_CLOCK_DIV_1
  54. #  define CONF_CLOCK_APBB_DIVIDER                 SYSTEM_MAIN_CLOCK_DIV_1

  55. /* SYSTEM_CLOCK_SOURCE_OSC8M configuration - Internal 8MHz oscillator */
  56. #  define CONF_CLOCK_OSC8M_PRESCALER              SYSTEM_OSC8M_DIV_1
  57. #  define CONF_CLOCK_OSC8M_ON_DEMAND              true
  58. #  define CONF_CLOCK_OSC8M_RUN_IN_STANDBY         false

  59. /* SYSTEM_CLOCK_SOURCE_XOSC configuration - External clock/oscillator */
  60. #  define CONF_CLOCK_XOSC_ENABLE                  false
  61. #  define CONF_CLOCK_XOSC_EXTERNAL_CRYSTAL        SYSTEM_CLOCK_EXTERNAL_CRYSTAL
  62. #  define CONF_CLOCK_XOSC_EXTERNAL_FREQUENCY      12000000UL
  63. #  define CONF_CLOCK_XOSC_STARTUP_TIME            SYSTEM_XOSC_STARTUP_32768
  64. #  define CONF_CLOCK_XOSC_AUTO_GAIN_CONTROL       true
  65. #  define CONF_CLOCK_XOSC_ON_DEMAND               true
  66. #  define CONF_CLOCK_XOSC_RUN_IN_STANDBY          false

  67. /* SYSTEM_CLOCK_SOURCE_XOSC32K configuration - External 32KHz crystal/clock oscillator */
  68. #  define CONF_CLOCK_XOSC32K_ENABLE               false
  69. #  define CONF_CLOCK_XOSC32K_EXTERNAL_CRYSTAL     SYSTEM_CLOCK_EXTERNAL_CRYSTAL
  70. #  define CONF_CLOCK_XOSC32K_STARTUP_TIME         SYSTEM_XOSC32K_STARTUP_65536
  71. #  define CONF_CLOCK_XOSC32K_AUTO_AMPLITUDE_CONTROL  false
  72. #  define CONF_CLOCK_XOSC32K_ENABLE_1KHZ_OUPUT    false
  73. #  define CONF_CLOCK_XOSC32K_ENABLE_32KHZ_OUTPUT  true
  74. #  define CONF_CLOCK_XOSC32K_ON_DEMAND            true
  75. #  define CONF_CLOCK_XOSC32K_RUN_IN_STANDBY       false

  76. /* SYSTEM_CLOCK_SOURCE_OSC32K configuration - Internal 32KHz oscillator */
  77. #  define CONF_CLOCK_OSC32K_ENABLE                false
  78. #  define CONF_CLOCK_OSC32K_STARTUP_TIME          SYSTEM_OSC32K_STARTUP_130
  79. #  define CONF_CLOCK_OSC32K_ENABLE_1KHZ_OUTPUT    true
  80. #  define CONF_CLOCK_OSC32K_ENABLE_32KHZ_OUTPUT   true
  81. #  define CONF_CLOCK_OSC32K_ON_DEMAND             true
  82. #  define CONF_CLOCK_OSC32K_RUN_IN_STANDBY        false

  83. /* SYSTEM_CLOCK_SOURCE_DFLL configuration - Digital Frequency Locked Loop */
  84. #  define CONF_CLOCK_DFLL_ENABLE                  false
  85. #  define CONF_CLOCK_DFLL_LOOP_MODE               SYSTEM_CLOCK_DFLL_LOOP_MODE_OPEN
  86. #  define CONF_CLOCK_DFLL_ON_DEMAND               false

  87. /* DFLL open loop mode configuration */
  88. #  define CONF_CLOCK_DFLL_COARSE_VALUE            (0x1f / 4)
  89. #  define CONF_CLOCK_DFLL_FINE_VALUE              (0xff / 4)

  90. /* DFLL closed loop mode configuration */
  91. #  define CONF_CLOCK_DFLL_SOURCE_GCLK_GENERATOR   GCLK_GENERATOR_1
  92. #  define CONF_CLOCK_DFLL_MULTIPLY_FACTOR         6
  93. #  define CONF_CLOCK_DFLL_QUICK_LOCK              true
  94. #  define CONF_CLOCK_DFLL_TRACK_AFTER_FINE_LOCK   true
  95. #  define CONF_CLOCK_DFLL_KEEP_LOCK_ON_WAKEUP     true
  96. #  define CONF_CLOCK_DFLL_ENABLE_CHILL_CYCLE      true
  97. #  define CONF_CLOCK_DFLL_MAX_COARSE_STEP_SIZE    (0x1f / 4)
  98. #  define CONF_CLOCK_DFLL_MAX_FINE_STEP_SIZE      (0xff / 4)

  99. /* SYSTEM_CLOCK_SOURCE_DPLL configuration - Digital Phase-Locked Loop */
  100. #  define CONF_CLOCK_DPLL_ENABLE                  false
  101. #  define CONF_CLOCK_DPLL_ON_DEMAND               true
  102. #  define CONF_CLOCK_DPLL_RUN_IN_STANDBY          false
  103. #  define CONF_CLOCK_DPLL_LOCK_BYPASS             false
  104. #  define CONF_CLOCK_DPLL_WAKE_UP_FAST            false
  105. #  define CONF_CLOCK_DPLL_LOW_POWER_ENABLE        false

  106. #  define CONF_CLOCK_DPLL_LOCK_TIME               SYSTEM_CLOCK_SOURCE_DPLL_LOCK_TIME_NO_TIMEOUT
  107. #  define CONF_CLOCK_DPLL_REFERENCE_CLOCK         SYSTEM_CLOCK_SOURCE_DPLL_REFERENCE_CLOCK_REF0
  108. #  define CONF_CLOCK_DPLL_FILTER                  SYSTEM_CLOCK_SOURCE_DPLL_FILTER_DEFAULT

  109. #  define CONF_CLOCK_DPLL_REFERENCE_FREQUENCY     32768
  110. #  define CONF_CLOCK_DPLL_REFEREMCE_DIVIDER       1
  111. #  define CONF_CLOCK_DPLL_OUTPUT_FREQUENCY        48000000

  112. /* Set this to true to configure the GCLK when running clocks_init. If set to
  113. * false, none of the GCLK generators will be configured in clocks_init(). */
  114. #  define CONF_CLOCK_CONFIGURE_GCLK               true

  115. /* Configure GCLK generator 0 (Main Clock) */
  116. #  define CONF_CLOCK_GCLK_0_ENABLE                true
  117. #  define CONF_CLOCK_GCLK_0_RUN_IN_STANDBY        false
  118. #  define CONF_CLOCK_GCLK_0_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  119. #  define CONF_CLOCK_GCLK_0_PRESCALER             1
  120. #  define CONF_CLOCK_GCLK_0_OUTPUT_ENABLE         false

  121. /* Configure GCLK generator 1 */
  122. #  define CONF_CLOCK_GCLK_1_ENABLE                true
  123. #  define CONF_CLOCK_GCLK_1_RUN_IN_STANDBY        false
  124. #  define CONF_CLOCK_GCLK_1_CLOCK_SOURCE           SYSTEM_CLOCK_SOURCE_ULP32K
  125. #  define CONF_CLOCK_GCLK_1_PRESCALER             1
  126. #  define CONF_CLOCK_GCLK_1_OUTPUT_ENABLE         false

  127. /* Configure GCLK generator 2 (RTC) */
  128. #  define CONF_CLOCK_GCLK_2_ENABLE                false
  129. #  define CONF_CLOCK_GCLK_2_RUN_IN_STANDBY        false
  130. #  define CONF_CLOCK_GCLK_2_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC32K
  131. #  define CONF_CLOCK_GCLK_2_PRESCALER             32
  132. #  define CONF_CLOCK_GCLK_2_OUTPUT_ENABLE         false

  133. /* Configure GCLK generator 3 */
  134. #  define CONF_CLOCK_GCLK_3_ENABLE                false
  135. #  define CONF_CLOCK_GCLK_3_RUN_IN_STANDBY        false
  136. #  define CONF_CLOCK_GCLK_3_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  137. #  define CONF_CLOCK_GCLK_3_PRESCALER             1
  138. #  define CONF_CLOCK_GCLK_3_OUTPUT_ENABLE         false

  139. /* Configure GCLK generator 4 */
  140. #  define CONF_CLOCK_GCLK_4_ENABLE                true
  141. #  define CONF_CLOCK_GCLK_4_RUN_IN_STANDBY        false
  142. #  define CONF_CLOCK_GCLK_4_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_ULP32K
  143. #  define CONF_CLOCK_GCLK_4_PRESCALER             32
  144. #  define CONF_CLOCK_GCLK_4_OUTPUT_ENABLE         false

  145. /* Configure GCLK generator 5 */
  146. #  define CONF_CLOCK_GCLK_5_ENABLE                false
  147. #  define CONF_CLOCK_GCLK_5_RUN_IN_STANDBY        false
  148. #  define CONF_CLOCK_GCLK_5_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  149. #  define CONF_CLOCK_GCLK_5_PRESCALER             1
  150. #  define CONF_CLOCK_GCLK_5_OUTPUT_ENABLE         false

  151. /* Configure GCLK generator 6 */
  152. #  define CONF_CLOCK_GCLK_6_ENABLE                false
  153. #  define CONF_CLOCK_GCLK_6_RUN_IN_STANDBY        false
  154. #  define CONF_CLOCK_GCLK_6_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  155. #  define CONF_CLOCK_GCLK_6_PRESCALER             1
  156. #  define CONF_CLOCK_GCLK_6_OUTPUT_ENABLE         false

  157. /* Configure GCLK generator 7 */
  158. #  define CONF_CLOCK_GCLK_7_ENABLE                false
  159. #  define CONF_CLOCK_GCLK_7_RUN_IN_STANDBY        false
  160. #  define CONF_CLOCK_GCLK_7_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_OSC8M
  161. #  define CONF_CLOCK_GCLK_7_PRESCALER             1
  162. #  define CONF_CLOCK_GCLK_7_OUTPUT_ENABLE         false

  163. #endif /* CONF_CLOCKS_H_INCLUDED */


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7001

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部