|
楼主 |
发表于 2015-3-2 20:26:16
|
显示全部楼层
拖了这么久还没完成,非常不好意思。
今天把串口与SHELL给整好了,很久没写代码有些手生,FREESCALE不熟再加上KDS用着很不爽,调试程序很是难受。下面先贴出SHELL和串口的主要代码(这些代码主要是参考网络修改整理的,在此感谢原作者):
- /*
- * shell.c
- *
- * Created on: 2015年3月2日
- * Author:
- */
- #include "shell.h"
- #include "uart.h"
- #include "string.h"
- //命令缓存及标志
- char gCmdBuf[CMD_BUF_SIZE+1] = {0};//增加一位结束符
- uint_8 gCmdBufCnt = 0;//缓存计数
- uint_8 gCmdBufStatus = 0; //0:接收中 1: 待处理 2:缓存满
- int Cmd_help(int argc, char *argv[]);
- tCmdLineEntry g_sCmdTable[] =
- {
- { "help", Cmd_help, " : Display list of commands" },
- { "h", Cmd_help, " : alias for help" },
- { "?", Cmd_help, " : alias for help" },
- { 0, 0, 0 }
- };
- /*
- * Cmd_help
- *
- * 打印命令帮助信息
- */
- int Cmd_help(int argc, char *argv[])
- {
- tCmdLineEntry *pEntry;
- // Print some header text.
- uart1_printf("\nAvailable commands\r\n");
- uart1_printf("------------------\r\n");
- // Point at the beginning of the command table.
- pEntry = g_sCmdTable;
- // Enter a loop to read each entry from the command table. The
- // end of the table has been reached when the command name is NULL.
- while(pEntry->pcCmd)
- {
- // Print the command name and the brief description.
- uart1_printf("%s%s\r\n", pEntry->pcCmd, pEntry->pcHelp);
- // Advance to the next entry in the table.
- pEntry++;
- }
- //
- // Return success.
- //
- return(0);
- }
- int CmdLineProcess(char *pcCmdLine)
- {
- static char *argv[CMDLINE_MAX_ARGS + 1];
- char *pcChar;
- int argc;
- int bFindArg = 1;
- int re = 0;
- tCmdLineEntry *pCmdEntry;
- //
- // Initialize the argument counter, and point to the beginning of the
- // command line string.
- //
- argc = 0;
- pcChar = pcCmdLine;
- //
- // Advance through the command line until a zero character is found.
- //
- while(*pcChar)
- {
- //
- // If there is a space, then replace it with a zero, and set the flag
- // to search for the next argument.
- //
- if(*pcChar == ' ')
- {
- *pcChar = 0;
- bFindArg = 1;
- }
- //
- // Otherwise it is not a space, so it must be a character that is part
- // of an argument.
- //
- else
- {
- //
- // If bFindArg is set, then that means we are looking for the start
- // of the next argument.
- //
- if(bFindArg)
- {
- //
- // As long as the maximum number of arguments has not been
- // reached, then save the pointer to the start of this new arg
- // in the argv array, and increment the count of args, argc.
- //
- if(argc < CMDLINE_MAX_ARGS)
- {
- argv[argc] = pcChar;
- argc++;
- bFindArg = 0;
- }
- //
- // The maximum number of arguments has been reached so return
- // the error.
- //
- else
- {
- return(CMDLINE_TOO_MANY_ARGS);
- }
- }
- }
- //
- // Advance to the next character in the command line.
- //
- pcChar++;
- }
- //
- // If one or more arguments was found, then process the command.
- //
- if(argc)
- {
- //
- // Start at the beginning of the command table, to look for a matching
- // command.
- //
- pCmdEntry = g_sCmdTable;
- //
- // Search through the command table until a null command string is
- // found, which marks the end of the table.
- //
- while(pCmdEntry->pcCmd)
- {
- //
- // If this command entry command string matches argv, then call
- // the function for this command, passing the command line
- // arguments.
- //
- if(!strcmp(argv[0], pCmdEntry->pcCmd))
- {
- re = pCmdEntry->pfnCmd(argc, argv);
- uart1_printf("\r\n$");
- }
- //
- // Not found, so advance to the next entry.
- //
- pCmdEntry++;
- }
- }
- //
- // Fall through to here means that no matching command was found, so return
- // an error.
- //
- return(CMDLINE_BAD_CMD);
- }
复制代码
- /*
- * shell.h
- *
- * Created on: 2015年3月2日
- * Author:
- */
- #ifndef SHELL_H_
- #define SHELL_H_
- #include "common.h" //包含公共要素头文件
- #include "sysinit.h"
- #define CMD_BUF_SIZE 100
- #define CMDLINE_MAX_ARGS 10
- #define CMDLINE_BAD_CMD 1
- #define CMDLINE_TOO_MANY_ARGS 2
- typedef int (*pfnCmdLine)(int argc, char *argv[]);
- //*****************************************************************************
- //
- //! Structure for an entry in the command list table.
- //
- //*****************************************************************************
- typedef struct
- {
- //命令字符串
- const char *pcCmd;
- //命令实现函数
- pfnCmdLine pfnCmd;
- //命令帮助信息
- const char *pcHelp;
- }tCmdLineEntry;
- extern char gCmdBuf[CMD_BUF_SIZE+1];//增加一位结束符
- extern uint_8 gCmdBufCnt;//缓存计数
- extern uint_8 gCmdBufStatus; //0:接收中 1: 待处理 2:缓存满
- extern int Cmd_help(int argc, char *argv[]);
- extern int CmdLineProcess(char *pcCmdLine);
- extern tCmdLineEntry g_sCmdTable[];
- #endif /* SHELL_H_ */
复制代码
串口苏大例程中加入了printf功能(不需要重定向的,稍加修改适用于任何开发环境哦):
- void uart1_printf(char* fmt, ...)
- {
- double vargflt = 0;
- int vargint = 0;
- char* vargpch = NULL;
- char vargch = 0;
- char* pfmt = NULL;
- va_list vp;
- va_start(vp, fmt);
- pfmt = fmt;
- while(*pfmt)
- {
- if(*pfmt == '%')
- {
- switch(*(++pfmt))
- {
- case 'c':
- vargch = va_arg(vp, int);
- /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
- mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
- printch(vargch);
- break;
- case 'd':
- case 'i':
- vargint = va_arg(vp, int);
- printdec(vargint);
- break;
- case 'f':
- vargflt = va_arg(vp, double);
- /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
- mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
- printflt(vargflt);
- break;
- case 's':
- vargpch = va_arg(vp, char*);
- printstr(vargpch);
- break;
- case 'b':
- case 'B':
- vargint = va_arg(vp, int);
- printbin(vargint);
- break;
- case 'x':
- case 'X':
- vargint = va_arg(vp, int);
- printhex(vargint);
- break;
- case '%':
- printch('%');
- break;
- default:
- break;
- }
- pfmt++;
- }
- else
- {
- printch(*pfmt++);
- }
- }
- va_end(vp);
- }
- void printch(char ch)
- {
- uart_send1(UART_1, ch);
- }
- void printdec(int dec)
- {
- if(dec==0)
- {
- return;
- }
- printdec(dec/10);
- printch( (char)(dec%10 + '0'));
- }
- void printflt(double flt)
- {
- int tmpint = 0;
- tmpint = (int)flt;
- printdec(tmpint);
- printch('.');
- flt = flt - tmpint;
- tmpint = (int)(flt * 1000000);
- printdec(tmpint);
- }
- void printstr(char* str)
- {
- while(*str)
- {
- printch(*str++);
- }
- }
- void printbin(int bin)
- {
- if(bin == 0)
- {
- printstr("0b");
- return;
- }
- printbin(bin/2);
- printch( (char)(bin%2 + '0'));
- }
- void printhex(int hex)
- {
- if(hex==0)
- {
- printstr("0x");
- return;
- }
- printhex(hex/16);
- if(hex < 10)
- {
- printch((char)(hex%16 + '0'));
- }
- else
- {
- printch((char)(hex%16 - 10 + 'a' ));
- }
- }
复制代码
串口中断接收程序
- //串口1接收中断服务例程
- void isr_uart1_re(void)
- {
- uint_8 ch;
- uint_8 flag = 1;
-
- DISABLE_INTERRUPTS;
-
- ch = uart_re1(UART_1, &flag);
- if (0 == flag)
- {
- uart_send1(UART_1, ch);
- if (ch != '\r')
- {
- if (gCmdBufStatus == 0)
- {
- gCmdBuf[gCmdBufCnt++] = ch;
- if (gCmdBufCnt == CMD_BUF_SIZE)
- gCmdBufStatus = 2;
- }
- }
- else
- {
- uart_send1(UART_1, '\n');
- gCmdBuf[gCmdBufCnt] = '\0';
- gCmdBufCnt = 0;
- gCmdBufStatus = 1;
- }
- }
-
- ENABLE_INTERRUPTS;
- }
复制代码 |
|