📄 main2138.c
字号:
#include <stdc.h>#include <stdio.h>#include <types.h>#include <LPC21xx.h>#include <armVIC.h>#include <errno.h>#include <time.h>#include <sys/time.h>#include <sysdefs.h>#include <unistd.h>#include <uart.h>#include <ioctl.h>extern void lowInit(void);extern void initDevices (void);extern void initRTC(void);bool Tx5Active, Rx5Active;FILE * uart1;static void sysInit(void){ lowInit(); // setup clocks and processor port pins // set the interrupt controller defaults#if defined(RAM_RUN) MEMMAP = MEMMAP_SRAM; // map interrupt vectors space into SRAM#elif defined(ROM_RUN) MEMMAP = MEMMAP_FLASH; // map interrupt vectors space into FLASH#else#error RUN_MODE not defined!#endif VICIntEnClear = 0xFFFFFFFF; // clear all interrupts VICIntSelect = 0x00000000; // clear all FIQ selections VICDefVectAddr = (uint32_t)_reset_; // point unvectored IRQs to reset() serial1Init(19200, UART_8N1, UART_FIFO_8); // setup the UART initDevices(); // condition system devices. initRTC(); // make sure Real Time Clock is running.}unsigned int Lit=False;struct TIMER blinkTimer;static void blinker (void){ if (timeElapsed (&blinkTimer)) { setElapsed (50, &blinkTimer, TimerInMilli); if (Lit) { IO1CLR = LED_EVENT_BIT; Lit = !Lit; } else { IO1SET = LED_EVENT_BIT; Lit = !Lit; } }}static void printTime(const char * markText){// print current time.struct tm elapsed;struct timeval today; gettimeofday(&today, NULL); localtime_r(&today.tv_sec, &elapsed); serial1Puts(markText); serial1Puts(itoa(elapsed.tm_hour)); serial1Puts(":"); serial1Puts(itoa(elapsed.tm_min)); serial1Puts(":"); serial1Puts(itoa(elapsed.tm_sec)); serial1Puts("\r\n");}char foo[256];long test;static void testFileOps (void){// exercise file I/O for testing.FILE * testfile;int result;long i; // test file read. if ((testfile = fopen("/mmc/SYSTIME.C","r")) == NULL) { serial1Puts("\r\nfailed to open input file! "); serial1Puts("\r\n"); } else { fseek (testfile, 130l, SEEK_SET); test = ftell(testfile); while (fgets (foo, sizeof(foo), testfile)) { serial1Puts(foo); serial1Puts("\r"); } fseek (testfile, 3800l, SEEK_SET); test = ftell(testfile); while (fgets (foo, sizeof(foo), testfile)) { serial1Puts(foo); serial1Puts("\r"); } } test = ftell(testfile); serial1Puts("\r\ndone...\r\n"); if (feof(testfile)) { fputs("empty", stderr); } if (fclose(testfile)) { serial1Puts("fclose input error!"); } else { serial1Puts("fclose input okay"); } serial1Puts("\r\n"); // delete old file backup. unlink("/mmc/errorlog.bak"); // rename current file to backup rename("/mmc/errorlog.txt", "errorlog.bak"); for (i=0; i<sizeof(foo); i++) foo[i] = ' '; printTime("Starting time: "); // fill test file with pattern. if ((testfile = fopen("/mmc/errorlog.txt","w")) == NULL) { serial1Puts("\r\nfailed to open output file! "); serial1Puts("\r\n"); } else { for (i=0; i<2048; i++) { blinker(); strcpy (foo, itoa(i)); result = fwrite (foo, 1, sizeof(foo), testfile); if (result == -1) { serial1Puts("fwrite output error!"); serial1Puts(itoa(result)); serial1Puts("\r\n"); } } if (fclose(testfile)) { result = errno; serial1Puts("fclose output error!"); serial1Puts(itoa(result)); } else { serial1Puts("fclose output okay"); } serial1Puts("\r\n"); } printTime("Finish time: "); if ((testfile = fopen("/mmc/errorlog.txt","a+")) == NULL) { serial1Puts("\r\nfailed to open output file! "); serial1Puts("\r\n"); } else { fseek(testfile, sizeof(foo)*5, SEEK_SET); fread(foo, 1, sizeof(foo), testfile); strcpy(foo,"this is the 5th "); fseek(testfile, sizeof(foo)*5, SEEK_SET); fwrite(foo, 1, sizeof(foo), testfile); fclose(testfile); }}#if 0static void testTimeOps (void){time_t today;struct timeval setDate;struct tm * dateToday; today = time(NULL); dateToday = localtime(&today); setDate.tv_sec = today; setDate.tv_usec = 0; settimeofday(&setDate, NULL); today = time(NULL); dateToday = localtime(&today);}#endifint main(void){FILE * logging; // must call this first as it sets up hardware. sysInit();// testTimeOps(); enableIRQ(); serial1Puts("\r\nHello World! running...\r\n"); testFileOps(); if ((logging = fopen("/mmc/logfile.txt","w")) == NULL) { serial1Puts("log is dead\n"); } for (;;) { int ch; if ((ch = serial1Getch()) >= 0) { serial1Puts ("You pressed the <"); serial1Putch (ch); serial1Puts ("> key\r\n"); if (logging) { fputs ("You pressed the <", logging); fputc (ch, logging); fputs ("> key\r\n", logging); // pressing space will sync the dir contents with file contents. if (ch == ' ') fflushdir(logging); } } blinker(); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -