⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usart_menu.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 C
字号:
//*----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : mon_menu.c
//* Object              : Manage the monitor menu tolls dump and go
//*
//* 1.0 14/11/00 JPP    : Creation
//*----------------------------------------------------------------------------

//* --------------------------- include file ----------------------------------

#include <stdio.h>
#include <string.h>


/* Driver description  */
#include "drivers/com/com.h"                    //* Communication driver

/* Function description */
#include "usart_menu.h"                        //* Function description

//* --------------------------- Constant definition ---------------------------

//* ------------------------------  extern function  --------------------------

extern int get_val( int val ,char * type);
extern int Get_Command(const char * menu);

//* ------------------------------  extern global data  -----------------------
extern ComDesc      COM;
extern ComDesc      COM1;
extern ComDesc      *Xmodem_COM;

//* ------------------------------  Local variable ----------------------------

static const char main_menu[]=
{
"\n\r  COMMAND MENU: Usart\n\r"
"  0) Info\n\r"
"  1) SET COM Speed\n\r"
"  2) SET Xmodem port com\n\r"
"  3) TRANS COM0 <==> COM1\n\r"
" 99)  Quit function\n\r"
"Enter command to execute: "
};

//*----------------------------------------------------------------------------
//* Function Name       : at91_put_byte
//* Object              : Send a char (one byte to USART)
//* Input Parameters    : com descriptor & String pointer
//* Output Parameters   : TRUE if byte is out
//*----------------------------------------------------------------------------
int at91_put_byte(ComDesc *USART_pt, char buff)
//* Begin
{
    //* wait the USART Ready for transmission
    //* PDC wait
    if (USART_pt->usart->usart_base->US_TCR ==0)
    {
     //* Char wait
        if ((at91_usart_get_status(USART_pt->usart) & US_TXRDY) == US_TXRDY)
        {
             //* write Char
            at91_usart_write(USART_pt->usart,buff);
            return (TRUE);
        }
    }
    return (FALSE);
}
//* End

//*----------------------------------------------------------------------------
//* Function Name       : usart_info
//* Object              : print usart info
//* Input Parameters    : None
//* Output Parameters   : None
//*----------------------------------------------------------------------------
void usart_info(u_int mcki, UsartSpeed *usart_speed)
{
    int     count;
    char    message[512];
    UsartSpeed com_speed[2];

    if ( Xmodem_COM == &COM)
        at91_print(&COM," Xmodem conected on COM (USART 0)\n\r");
    else
        at91_print(&COM," Xmodem conected on COM1 (USART 1)\n\r");

    at91_print(&COM," 8 bits parity none 1 stop\n\r");
    //* compute the real baud rate
    com_speed[0].baud = (mcki/((COM.usart->usart_base->US_BRGR) *16));
    com_speed[0].error= at91_error_baud_com(mcki, com_speed[0].baud);
    com_speed[1].baud = (mcki/((COM1.usart->usart_base->US_BRGR) *16));
    com_speed[1].error= at91_error_baud_com(mcki, com_speed[1].baud);
    //* print the current baud rate
    for(count=0; count<2 ;count++)
    {
        sprintf(message,"COM %d Current Baud Rate  %d  error %d \n\r",count, com_speed[count].baud,com_speed[count].error);
        at91_print(&COM,message);
    }
    //* print the baud rate table
    for(count=0; count<NB_USART_SPEED ;count++)
    {
        sprintf(message,"Ind %d baud %6d  error %d \n\r",count, usart_speed[count].baud,usart_speed[count].error);
        at91_print(&COM,message);
    }
}
//*----------------------------------------------------------------------------
//* Function Name       : menu
//* Object              : Get information and compute this
//* Input Parameters    : None
//* Output Parameters   : None
//*----------------------------------------------------------------------------
void usart_menu(u_int mcki)
{
    int     command = 0;
    int     count,ind;
    ComDesc *curent_COM;
    u_char  buff_cmo1_com0[2048];
    int     ind_recp,ind_send,input;
    int     data_com;


    UsartSpeed usart_speed[NB_USART_SPEED]=
    {
        {38400,0,0},
        {57600,0,0},
        {115200,0,0},
        {230400,0,0},
        {460800,0,0},
        {576000,0,0},
        {691200,0,0},
        {921600,0,0},
    };

    //* Check the Baud rate
    for(count=0; count<NB_USART_SPEED ;count++)
    {
        usart_speed[count].cd = at91_baud_com(mcki, usart_speed[count].baud);
        usart_speed[count].error = at91_error_baud_com(mcki, usart_speed[count].baud);
    }
    usart_info(mcki, usart_speed);
    while(command!=99)
    {
        command = Get_Command(main_menu);

        switch(command)
        {
            case 0:
                usart_info(mcki, usart_speed);
            break;

            case 1:
                usart_info(mcki, usart_speed);

                at91_print(&COM,"COM (0 or 1):");
                ind=0;
                ind = get_val(ind,"%d");
                curent_COM = (ind==0 ? &COM : &COM1);

                at91_print(&COM,"Ind:");
                ind=0;
                ind = get_val(ind,"%d");
                at91_close_com(curent_COM);
                at91_open_com(curent_COM,(COM_8_BIT|COM_PAR_NONE|COM_NBSTOP_1|COM_FLOW_CONTROL_NONE),usart_speed[ind].cd);
            break;
            case 2:
                usart_info(mcki, usart_speed);
                ind = (Xmodem_COM == &COM ? 0 : 1);
                at91_print(&COM,"Get Xmodem USART (0 to 1):");
                Xmodem_COM = ( get_val(ind,"%d") == 0? &COM :&COM1);
            break;

            case 3:
                at91_print(&COM,"Usart in transparent for out use CTRL C\n\r");
                // set the fifo pointeur
                ind_recp = ind_send = input= 0;
                at91_print(&COM1,"\n\r");
                for(;;)
                {
                    // if recieved to COM0 sent to COM1
                    if ( !at91_getch(&COM,&data_com))
                    {
                        if ( (char)data_com == 0x03)    //* ctrl C
                        {
                            break;
                        }
                        at91_send_byte(&COM1,data_com);
                    }
                    // if recieved to COM1 sent to COM0
                    if (! at91_getch(&COM1,&data_com))
                    {
                        //* put data in tampon buffer
                        buff_cmo1_com0[ind_recp]=(u_char)data_com;
                        ind_recp++;
                        //* if recivied end line sent >
                        if ( (char)data_com == '\r')
                        {
                            buff_cmo1_com0[ind_recp]='>';
                            ind_recp++;
                            input++;
                            ind_recp = (ind_recp >= sizeof(buff_cmo1_com0) ? 0 : ind_recp);
                        }
                        ind_recp = (ind_recp >= sizeof(buff_cmo1_com0) ? 0 : ind_recp);
                        input++;
                    }
                    if (input !=0)
                    {
                        data_com = (u_int) buff_cmo1_com0[ind_send];
                        if (at91_put_byte (&COM,data_com))
                        {
                            input--;
                            ind_send++;
                            ind_send = (ind_send >= sizeof(buff_cmo1_com0) ? 0 : ind_send);
                        }

                    }

                }
            break;

        }// switch command end

    }// while command end
}
//* End

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -