ARM_CORTEX-M3应用实例开发详解光盘

源代码在线查看: command.c

软件大小: 5292 K
上传用户: xiao11tian
关键词: ARM_CORTEX-M 应用实例 光盘
下载地址: 免注册下载 普通下载 VIP

相关代码

				#include 
				#include 
				#include 
				#include 
				#include "rtc.h"
				#include "aux_lib.h"
				#include "uart_std.h"
				#include "uart2.h"
				#include "command.h"
				#include "cdrs_op.h"
				#include "tasks.h"
				#include "ff.h"
				#include "gui.h"
				
				
				static int command_test(int argc, char **argv);
				static int command_date(int argc, char **argv);
				static int command_lst(int argc, char **argv);
				static int command_help(int argc, char **argv);
				static int command_kill(int argc, char **argv);
				static int command_top(int argc, char **argv);
				static int command_clear(int argc, char **argv);
				static int command_cdrs(int argc, char **argv);
				static int command_atc(int argc, char **argv);
				static int command_dir(int argc, char **argv);
				static int command_prtsc(int argc, char **argv);
				
				struct cmd cmds[] = {
					// {"test", command_test, "Test some function."},
					{"atc",  command_atc,  "AT command."},
					{"lst", command_lst,   "List tasks."},
					{"date", command_date, "Show rtc time."},
					{"cdrs", command_cdrs, "Open/Close cdma or gprs."},
					{"run", command_run,   "Start a new task."},
					{"kill", command_kill, "Stop a running task."},
					{"top", command_top,   "Show running task info"},
					{"help", command_help, "Show this messages."},
					{"clear", command_clear, "Clear screen."},
					{"dir", command_dir, "List directory."},
					{"prtsc", command_prtsc, "Print screen to file."},
					{NULL, NULL, NULL}
				};
				
				FILINFO Finfo;
				
				char MenuStart(void);
				void DispMenuTop(void);
				int MENU_ShowStreamBMP(int x0, int y0,
						       void (*pfStream) (unsigned int, void *));
				void fun(unsigned int n, void *pd);
				
				
				
				
				static int command_test(int argc, char **argv)
				{
				//	unsigned short msg;
				//    int blk = 0;
				    return 0;
				}
				
				static int command_atc(int argc, char **argv)
				{
				   char buf[80];
				    int ch, i = 0;
				
				    printf("\r\nAt Command mode\r\n");
				    do {
				        ch = uart_peek_key(udev_std);
				
				        if (ch != EOF) {
				            if (ch == '\r' || ch == '\n') {
				                printf("\r\n");
				                buf[i] = 0;
				                strcat(buf, "\r\n");
				                for (i=0; buf[i] != '\0'; i++)        
				                    uart2_send_ch(buf[i]);
				                i = 0;
				                buf[0] = 0;
				            } else {
				                buf[i++] = ch;
				                printf("%c", ch);   
				            }
				        }
				        ch = uart2_peek_key();
				        if (ch != EOF) {
				            printf("%c", ch);
				        }
				        OSTimeDlyHMSM(0, 0, 0, 5);
				 
				    } while (!(buf[0] == 'q' || buf[0] == 'Q'));
				//    printf("\r\nClose CDMA ...");
				//    CDRS_Close();
				//    CDRS_SetPower(0);
				    printf("\r\nAT Command Exit!\r\n");
					return 0;
				}
				
				static int command_cdrs(int argc, char **argv)
				{
				    if (!strcmp(argv[1], "open")){
				        
				        CDRS_SetPower(1);
				        CDRS_Open();
				    } else if (!strcmp(argv[1], "close")) {
				        CDRS_Close();
				        CDRS_SetPower(0);    
				    } else {
				        printf("\r\n cdrs [open|close]");
				    } 
				    return 0;
				}
				
				static int command_date(int argc, char **argv)
				{
				    time_t timer;
				    struct tm stm;
				    // yyyy mm dd hh mm ss
				    if (argc == 7) {
				        //设置时间
				        memset(&stm, 0, sizeof stm);
				        
				        stm.tm_year = atoi(argv[1])-1900; /* years since 1900 */
				        stm.tm_mon  = atoi(argv[2]) - 1;    /* months since January, 0 to 11 */
				        stm.tm_mday = atoi(argv[3]);    /* day of the month, 1 to 31 */
				        stm.tm_hour = atoi(argv[4]);   /* hours since midnight, 0 to 23 */
				        stm.tm_min  = atoi(argv[5]);    /* minutes after the hour, 0 to 59 */
				
				        stm.tm_sec  = atoi(argv[6]);    /* seconds after the minute, 0 to 60
				         (0 - 60 allows for the occasional leap second) */
				
				      //  stm.tm_wday = 3;    /* days since Sunday, 0 to 6 */
				     //   stm.tm_yday = 0;    /* days since January 1, 0 to 365 */
				     //   stm.tm_isdst = 1;   /* Daylight Savings Time flag */
				        rtc_SetTime(&stm);
				    }
				    //显示时间
				    time(&timer);
				    printf("\r\n%s", ctime(&timer));  
				    return 0;
				}
				
				static int command_lst(int argc, char **argv)
				{
					struct user_task *ptask = &user_tasks[1];
					printf("\r\n All User Tasks :");
					for (; ptask->TaskName != NULL; ptask++) {
						printf("\r\n    \x1B[1;32m%-10s\x1B[40;37m  : %s", ptask->TaskName,
						       ptask->desc);
					}
					printf("\r\n");
					return 0;
				}
				
				int command_run(int argc, char **argv)
				{
					struct user_task *ptask = &user_tasks[0];
				
					if (argc == 1) {
						printf("\r\nrun ");
						return -1;
					}
				
					for (; ptask->TaskName != NULL; ptask++) {
						if (!strcmp(argv[1], (char *) ptask->TaskName))
							break;
					}
					if (ptask->TaskName == NULL) {
						printf("\r\nNo Task Named : %s", argv[1]);
						return -1;
					}
					if (OSTCBPrioTbl[ptask->prio] != (OS_TCB *) 0) {
						printf("\r\nTask : %s is running!", argv[1]);
						return -1;
					}
				
					ptask->parg = &argv[1];
				#if 0
					{
					int i;
				
					printf("\r\nargc = %d", argc);
					for (i = 0; i < argc; i++) {
						printf(" %s", argv[i]);
					}
					}
				#endif
				
					if (Run_Task(ptask) < 0) {
						printf("\r\nErr : Create Task.");
						return -1;
					}
					printf("\r\n");
					return 0;
				}
				static int command_kill(int argc, char **argv)
				{
				
					struct user_task *ptask = &user_tasks[0];
					int idxp = 0, idxn;
				
					if (argc == 1) {
						printf("\r\nkill ");
						return -1;
					}
					idxn = 1;
					if (argc > 2) {
						idxp = 1;
						idxn = 2;
					}
					for (; ptask->TaskName != NULL; ptask++) {
						if (!strcmp(argv[idxn], (char *) ptask->TaskName))
							break;
					}
					if (ptask->TaskName == NULL) {
						printf("\r\nNo Task Named : %s", argv[idxn]);
						return -1;
					}
					if (OSTCBPrioTbl[ptask->prio] == (OS_TCB *) 0) {
						printf("\r\nTask : %s isn't running!", argv[idxn]);
						return -1;
					}
					if (argc > 2
					    && (argv[idxp][idxp] == 'F' || argv[idxp][idxp] == 'f')) {
						if (OS_ERR_NONE != OSTaskDel(ptask->prio)) {
							printf("\r\nErr : Kill Task.");
							printf("\r\n");
							return -1;
						}
						return 0;
					}
					OSTaskDelReq(ptask->prio);
				
					printf("\r\n");
					return 0;
				}
				
				static int command_top(int argc, char **argv)
				{
					INT32U TotalStk;
					OS_TCB *ptcb;
					INT16U version;
					static char *TsakS[] =
					    { "RDY", "SEM", "MBOX", "Q", "SUSPEND", "FLAG", "", "MUTEX" };
					version = OSVersion();
				
					while (1) {
				
						INT32U cpuclk;
				
						cpuclk = OS_CPU_ClkFreq();
						printf("%c[%dA%c[2J", 0x1b, 100, 0x1b);	//清屏
				
						printf("\r\n                           uC/OS-II                       ");
						printf("\r\n                                           Version %d.%d  ", version / 100, version % 100);
						printf("\r\n #CPU Clock : %u", cpuclk / 1000000);
						cpuclk %= 1000000;
						if (cpuclk) {
							printf(".%u", cpuclk);
						}
						printf(" MHz");
						printf("\r\n");
						printf("\r\n Name            Prio  State   SwCtr   CPU   FreeStk   UsedStk   TotalStk");
						printf("\r\n ------------------------------------------------------------------------");
				
						ptcb = OSTCBList;
						do {
							TotalStk =
							    (INT32U) ptcb->OSTCBStkBase -
							    (INT32U) ptcb->OSTCBStkBottom;
				
							printf("\r\n \x1B[1;32m%-15s\x1B[40;37m %-5u %-7s %-7u %2u%%   %-8u  %-8u  %u",
							     ptcb->OSTCBTaskName, ptcb->OSTCBPrio, TsakS[ptcb->OSTCBStat], ptcb->OSTCBCtxSwCtr,	//ptcb->OSTCBCyclesTot,
							     ptcb->OSTCBCyclesTot * 100 / OSTimeGet(),	// total, //ptcb->OSTCBCyclesStart,
							     TotalStk - ptcb->OSTCBStkUsed,
							     ptcb->OSTCBStkUsed, TotalStk);
							ptcb = ptcb->OSTCBNext;
						} while (ptcb);
				
						printf("\r\n");
						printf("\r\n #Tasks : %-2d   #CPU Usage : %d%%   #Task switch/sec : %d",
						     OSTaskCtr, OSCPUUsage, OSCtxSwCtr);	/* Display #tasks running               */
						printf("\r\n #Power On : %us", OSTimeGet() / OS_TICKS_PER_SEC);
						if (peekch() == 0x1b)
							break;
						OSTimeDlyHMSM(0, 0, 1, 0);
					}
					printf("\r\n");
					return 0;
				}
				
				static int command_help(int argc, char **argv)
				{
					struct cmd *pcmd = cmds;
				
					printf("\r\n All commands :");
					for (; pcmd->cmdName != NULL; pcmd++) {
						printf("\r\n    \x1B[1;32m%-9s\x1B[40;37m : %s", pcmd->cmdName, pcmd->help);
					}
					printf("\r\n");
					return 0;
				}
				
				static int command_clear(int argc, char **argv)
				{
					printf("%c[%dA%c[2J", 0x1b, 100, 0x1b);	//清屏
					return 0;
				}
				
				int put_rc (FRESULT rc)
				{
					const char *p;
					static const char str[] =
						"OK\0" "DISK_ERR\0" "INT_ERR\0" "NOT_READY\0" "NO_FILE\0" "NO_PATH\0"
						"INVALID_NAME\0" "DENIED\0" "EXIST\0" "INVALID_OBJECT\0" "WRITE_PROTECTED\0"
						"INVALID_DRIVE\0" "NOT_ENABLED\0" "NO_FILE_SYSTEM\0" "MKFS_ABORTED\0" "TIMEOUT\0";
					FRESULT i;
				
					for (p = str, i = 0; i != rc && *p; i++) {
						while(*p++);
					}
					printf("\r\nrc=%u FR_%s\r\n", (UINT)rc, p);
				
					return 0;
				}
				
				int filinfo_list(FILINFO* finfo) {
					if (finfo->fattrib & AM_DIR) {
						printf("\r\n   %12s        %2d-%02d-%04d  %2d:%02d",
						  finfo->fname,
						  (finfo->fdate >> 5) & 15,
						  finfo->fdate & 31,
						  (finfo->fdate >> 9) + 1980,
						  (finfo->ftime >> 11),
						  (finfo->ftime >> 5) & 63
						);
					} else {
						printf("\r\n   %12s %10ld  %2d-%02d-%04d  %2d:%02d",
						  finfo->fname,
						  finfo->fsize,
						  (finfo->fdate >> 5) & 15,
						  finfo->fdate & 31,
						  (finfo->fdate >> 9) + 1980,
						  (finfo->ftime >> 11),
						  (finfo->ftime >> 5) & 63
						);
					}
					return 0;
				}
				
				int command_dir(int argc, char **argv)
				{
					const char* sdrive = "sd";
					char* ptr, filename[84];
					long p1;
					BYTE res;
					UINT s1, s2;
					FATFS *fs;	/* Pointer to file system object */
					DIR dir;	/* Directory object */
				
					if (argc < 2) {
						// printf("\r\ndir ");
						// return -1;
						argv[1] = "/";
						argc = 2;
					}
				
					ptr = argv[1];
					while (*ptr == ' ') ptr++;
				
					strncpy(&filename[3], ptr, 80);
					ptr = &filename[3];
				
					if (memcmp(ptr, "1:", 2) != 0) {
						if (ptr[0] != '/' && ptr[0] != '\\') {
							ptr--;
							ptr[0] = '\\';
						}
						ptr -= 2;
						memcpy(ptr, "1:", 2);
					}
				
					printf("\r\n Volume in drive %s%s\r\n", sdrive, "1");
					printf("\r\n Directory of %s%s\r\n", sdrive, ptr);
				
					res = f_opendir(&dir, ptr);
					if (res != FR_OK) {
						put_rc(res);
						// printf("Directory Unexist %d\r\n", res);
						return -1;
					}
					p1 = s1 = s2 = 0;
				
					for(;;) {
						res = f_readdir(&dir, &Finfo);
						if ((res != FR_OK) || !Finfo.fname[0]) break;
						if (Finfo.fattrib & AM_DIR) {
							s2++;
						} else {
							s1++;
							p1 += Finfo.fsize;
						}
						filinfo_list(&Finfo);
					}
					printf("\r\n");
					printf("          %4u File(s),%10lu bytes total\r\n", s1, p1);
					printf("          %4u Dir(s)", s2);
					if (f_getfree(ptr, (DWORD*)&p1, &fs) == FR_OK) {
						printf(", %10lu bytes free\r\n", p1 * fs->csize * 512);
					}
				
					return 0;
				}
				
				FIL prt_fil_buf[1];
				FIL* prt_fil = &prt_fil_buf[0];
				
				int prtsc_file_open(char* filename, int mode) {
					char fnambuf[0x80];
					int r;
				
					sprintf(fnambuf, "1:\\strpic\\%s", filename);
					r = f_open(prt_fil, fnambuf, FA_WRITE | FA_CREATE_NEW);
					if (FR_OK != r) {
						printf("\r\nopen file %s err %d", fnambuf, r);
						return -2;
					}
				
					return 0;	
				}
				
				void prtsc_wfile_clbk(U8 Data, void* p) {
					int br;
				
					f_write(prt_fil, &Data, 1, (UINT*)&br);
					return;
				}
				
				static int command_prtsc(int argc, char **argv) {
					char* ptr;
					int r;
				
					if (argc < 2) {
						printf("\r\nprtsc ");
						return -1;
					}
				
					ptr = argv[1];
					while (*ptr == ' ') ptr++;
				
					r = prtsc_file_open(ptr, 0);
					if (r < 0) {
						return -2;
					}
				
					GUI_StreamBMP(NULL, prtsc_wfile_clbk, NULL);
				
					f_sync(prt_fil);	
					f_close(prt_fil);
				
					return 0;
				}
				
							

相关资源