[Atmel] 用SAM-BA或JLINK跑ATSAM4E16的程序(16)ASF RTT

[复制链接]
1935|0
 楼主| ddllxxrr 发表于 2015-11-23 21:24 | 显示全部楼层 |阅读模式
今天用库函数跑下RTT,这个程序很简单用串口同单片机通读,可以输入R复位RTT,也可输入S来设定报警时间、
程序如下:


  1. #include "asf.h"
  2. #include "stdio_serial.h"
  3. #include "conf_board.h"
  4. #include "conf_clock.h"

  5. /** Device state: in the main menu. */
  6. #define STATE_MAIN_MENU      0
  7. /** Device state: user is setting an alarm time. */
  8. #define STATE_SET_ALARM      1

  9. /** ASCII char definition for backspace. */
  10. #define ASCII_BS    8
  11. /** ASCII char definition for carriage return. */
  12. #define ASCII_CR    13

  13. #define STRING_EOL    "\r"
  14. #define STRING_HEADER "-- RTT Example --\r\n" \
  15.                 "-- "BOARD_NAME" --\r\n" \
  16.                 "-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL

  17. /** Current device state. */
  18. volatile uint8_t g_uc_state;

  19. /** New alarm time being currently entered. */
  20. volatile uint32_t g_ul_new_alarm;

  21. /** Indicate that an alarm has occurred but has not been cleared. */
  22. volatile uint8_t g_uc_alarmed;

  23. /**
  24. * \brief Refresh display on terminal.
  25. *
  26. * Update the terminal display to show the current menu and the current time
  27. * depending on the device state.
  28. */
  29. static void refresh_display(void)
  30. {
  31.         printf("%c[2J\r", 27);
  32.         printf("Time: %u\n\r", (unsigned int)rtt_read_timer_value(RTT));

  33.         /* Display alarm */
  34.         if (g_uc_alarmed) {
  35.                 puts("!!! ALARM !!!\r");
  36.         }

  37.         /* Main menu */
  38.         if (g_uc_state == STATE_MAIN_MENU) {
  39.                 puts("Menu:\n\r"
  40.                                 " r - Reset timer\n\r"
  41.                                 " s - Set alarm\r");
  42.                 if (g_uc_alarmed) {
  43.                         puts(" c - Clear alarm notification\r");
  44.                 }
  45.                 puts("\n\rChoice? ");
  46.         } else {
  47.                 if (g_uc_state == STATE_SET_ALARM) {
  48.                         puts("Enter alarm time: ");
  49.                         if (g_ul_new_alarm != 0) {
  50.                                 printf("%u", (unsigned)g_ul_new_alarm);
  51.                         }
  52.                 }
  53.         }
  54. }

  55. /**
  56. * \brief RTT configuration function.
  57. *
  58. * Configure the RTT to generate a one second tick, which triggers the RTTINC
  59. * interrupt.
  60. */
  61. static void configure_rtt(void)
  62. {
  63.         uint32_t ul_previous_time;

  64.         /* Configure RTT for a 1 second tick interrupt */
  65. #if SAM4N || SAM4S || SAM4E || SAM4C || SAM4CP || SAM4CM || SAMV71 || SAMV70 || SAME70 || SAMS70
  66.         rtt_sel_source(RTT, false);
  67. #endif
  68.         rtt_init(RTT, 32768);

  69.         ul_previous_time = rtt_read_timer_value(RTT);
  70.         while (ul_previous_time == rtt_read_timer_value(RTT));

  71.         /* Enable RTT interrupt */
  72.         NVIC_DisableIRQ(RTT_IRQn);
  73.         NVIC_ClearPendingIRQ(RTT_IRQn);
  74.         NVIC_SetPriority(RTT_IRQn, 0);
  75.         NVIC_EnableIRQ(RTT_IRQn);
  76.         rtt_enable_interrupt(RTT, RTT_MR_RTTINCIEN);
  77. }

  78. /**
  79. * \brief Configure the console UART.
  80. */
  81. static void configure_console(void)
  82. {
  83.         const usart_serial_options_t uart_serial_options = {
  84.                 .baudrate = CONF_UART_BAUDRATE,
  85. #ifdef CONF_UART_CHAR_LENGTH
  86.                 .charlength = CONF_UART_CHAR_LENGTH,
  87. #endif
  88.                 .paritytype = CONF_UART_PARITY,
  89. #ifdef CONF_UART_STOP_BITS
  90.                 .stopbits = CONF_UART_STOP_BITS,
  91. #endif
  92.         };

  93.         /* Configure console UART. */
  94.         sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
  95.         stdio_serial_init(CONF_UART, &uart_serial_options);
  96. }

  97. /**
  98. * \brief Interrupt handler for the RTT.
  99. *
  100. * Display the current time on the terminal.
  101. */
  102. void RTT_Handler(void)
  103. {
  104.         uint32_t ul_status;

  105.         /* Get RTT status */
  106.         ul_status = rtt_get_status(RTT);

  107.         /* Time has changed, refresh display */
  108.         if ((ul_status & RTT_SR_RTTINC) == RTT_SR_RTTINC) {
  109.                 refresh_display();
  110.         }

  111.         /* Alarm */
  112.         if ((ul_status & RTT_SR_ALMS) == RTT_SR_ALMS) {
  113.                 g_uc_alarmed = 1;
  114.                 refresh_display();
  115.         }
  116. }

  117. /**
  118. * \brief Application entry point for RTT example.
  119. *
  120. * Initialize the RTT, display the current time and allow the user to
  121. * perform several actions: clear the timer, set an alarm, etc.
  122. *
  123. * \return Unused (ANSI-C compatibility).
  124. */
  125. int main(void)
  126. {
  127.         uint8_t c;

  128.         /* Initialize the SAM system */
  129.         sysclk_init();
  130.         board_init();

  131.         /* Configure console UART */
  132.         configure_console();

  133.         /* Output example information */
  134.         puts(STRING_HEADER);

  135.         /* Configure RTT */
  136.         configure_rtt();

  137.         /* Initialize state machine */
  138.         g_uc_state = STATE_MAIN_MENU;
  139.         g_uc_alarmed = 0;
  140.         refresh_display();

  141.         /* User input loop */
  142.         while (1) {
  143.                 /* Wait for user input */
  144.                 scanf("%c", (char *)&c);

  145.                 /* Main menu mode */
  146.                 if (g_uc_state == STATE_MAIN_MENU) {
  147.                         /* Reset timer */
  148.                         if (c == 'r') {
  149.                                 configure_rtt();
  150.                                 refresh_display();
  151.                         } else if (c == 's') { /* Set alarm */
  152.                                 g_uc_state = STATE_SET_ALARM;
  153.                                 g_ul_new_alarm = 0;
  154.                                 refresh_display();
  155.                         } else { /* Clear alarm */
  156.                                 if ((c == 'c') && g_uc_alarmed) {
  157.                                         g_uc_alarmed = 0;
  158.                                         refresh_display();
  159.                                 }
  160.                         }
  161.                 } else if (g_uc_state == STATE_SET_ALARM) { /* Set alarm mode */
  162.                         /* Number */
  163.                         if ((c >= '0') && (c <= '9')) {
  164.                                 g_ul_new_alarm = g_ul_new_alarm * 10 + c - '0';
  165.                                 refresh_display();
  166.                         } else if (c == ASCII_BS) {
  167.                                 printf("%c", c);
  168.                                 g_ul_new_alarm /= 10;
  169.                                 refresh_display();
  170.                         } else if (c == ASCII_CR) {
  171.                                 /* Avoid newAlarm = 0 case */
  172.                                 if (g_ul_new_alarm != 0) {
  173.                                         rtt_write_alarm_time(RTT, g_ul_new_alarm);
  174.                                 }

  175.                                 g_uc_state = STATE_MAIN_MENU;
  176.                                 refresh_display();
  177.                         }
  178.                 }
  179.         }
  180. }
以下是运行截图:

本帖子中包含更多资源

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

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

本版积分规则

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

2404

主题

7001

帖子

68

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