打印
[Atmel]

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

[复制链接]
852|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ddllxxrr|  楼主 | 2015-11-23 21:24 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
今天用库函数跑下RTT,这个程序很简单用串口同单片机通读,可以输入R复位RTT,也可输入S来设定报警时间、
程序如下:


#include "asf.h"
#include "stdio_serial.h"
#include "conf_board.h"
#include "conf_clock.h"

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

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

#define STRING_EOL    "\r"
#define STRING_HEADER "-- RTT Example --\r\n" \
                "-- "BOARD_NAME" --\r\n" \
                "-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL

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

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

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

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

        /* Display alarm */
        if (g_uc_alarmed) {
                puts("!!! ALARM !!!\r");
        }

        /* Main menu */
        if (g_uc_state == STATE_MAIN_MENU) {
                puts("Menu:\n\r"
                                " r - Reset timer\n\r"
                                " s - Set alarm\r");
                if (g_uc_alarmed) {
                        puts(" c - Clear alarm notification\r");
                }
                puts("\n\rChoice? ");
        } else {
                if (g_uc_state == STATE_SET_ALARM) {
                        puts("Enter alarm time: ");
                        if (g_ul_new_alarm != 0) {
                                printf("%u", (unsigned)g_ul_new_alarm);
                        }
                }
        }
}

/**
* \brief RTT configuration function.
*
* Configure the RTT to generate a one second tick, which triggers the RTTINC
* interrupt.
*/
static void configure_rtt(void)
{
        uint32_t ul_previous_time;

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

        ul_previous_time = rtt_read_timer_value(RTT);
        while (ul_previous_time == rtt_read_timer_value(RTT));

        /* Enable RTT interrupt */
        NVIC_DisableIRQ(RTT_IRQn);
        NVIC_ClearPendingIRQ(RTT_IRQn);
        NVIC_SetPriority(RTT_IRQn, 0);
        NVIC_EnableIRQ(RTT_IRQn);
        rtt_enable_interrupt(RTT, RTT_MR_RTTINCIEN);
}

/**
* \brief Configure the console UART.
*/
static void configure_console(void)
{
        const usart_serial_options_t uart_serial_options = {
                .baudrate = CONF_UART_BAUDRATE,
#ifdef CONF_UART_CHAR_LENGTH
                .charlength = CONF_UART_CHAR_LENGTH,
#endif
                .paritytype = CONF_UART_PARITY,
#ifdef CONF_UART_STOP_BITS
                .stopbits = CONF_UART_STOP_BITS,
#endif
        };

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

/**
* \brief Interrupt handler for the RTT.
*
* Display the current time on the terminal.
*/
void RTT_Handler(void)
{
        uint32_t ul_status;

        /* Get RTT status */
        ul_status = rtt_get_status(RTT);

        /* Time has changed, refresh display */
        if ((ul_status & RTT_SR_RTTINC) == RTT_SR_RTTINC) {
                refresh_display();
        }

        /* Alarm */
        if ((ul_status & RTT_SR_ALMS) == RTT_SR_ALMS) {
                g_uc_alarmed = 1;
                refresh_display();
        }
}

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

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

        /* Configure console UART */
        configure_console();

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

        /* Configure RTT */
        configure_rtt();

        /* Initialize state machine */
        g_uc_state = STATE_MAIN_MENU;
        g_uc_alarmed = 0;
        refresh_display();

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

                /* Main menu mode */
                if (g_uc_state == STATE_MAIN_MENU) {
                        /* Reset timer */
                        if (c == 'r') {
                                configure_rtt();
                                refresh_display();
                        } else if (c == 's') { /* Set alarm */
                                g_uc_state = STATE_SET_ALARM;
                                g_ul_new_alarm = 0;
                                refresh_display();
                        } else { /* Clear alarm */
                                if ((c == 'c') && g_uc_alarmed) {
                                        g_uc_alarmed = 0;
                                        refresh_display();
                                }
                        }
                } else if (g_uc_state == STATE_SET_ALARM) { /* Set alarm mode */
                        /* Number */
                        if ((c >= '0') && (c <= '9')) {
                                g_ul_new_alarm = g_ul_new_alarm * 10 + c - '0';
                                refresh_display();
                        } else if (c == ASCII_BS) {
                                printf("%c", c);
                                g_ul_new_alarm /= 10;
                                refresh_display();
                        } else if (c == ASCII_CR) {
                                /* Avoid newAlarm = 0 case */
                                if (g_ul_new_alarm != 0) {
                                        rtt_write_alarm_time(RTT, g_ul_new_alarm);
                                }

                                g_uc_state = STATE_MAIN_MENU;
                                refresh_display();
                        }
                }
        }
}
以下是运行截图:

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

2398

主题

6945

帖子

66

粉丝