boolean SendCANFrame( uint32_t id, uint8_t size, uint8_t *pData ) { unsigned _cpsr; int i; char wrkbuf[ 10 ]; char canmsg[ 50 ]; const char *p; if ( size > 8 ) return false; _itoa( id, wrkbuf, 16 ); strcpy( canmsg, "T" ); // Add zeros in front of id so length always is 8 for ( i=0; i < ( 8 - strlen( wrkbuf ) ); i++ ) { strcat( canmsg, "0" ); } // Copy in id strcat( canmsg, wrkbuf ); // data size _itoa( size, wrkbuf, 16 ); strcat( canmsg, wrkbuf ); for ( i=0; i<size; i++ ) { _itoa( pData[ i ], wrkbuf, 16 ); if ( 1 == strlen( wrkbuf ) ) { strcat( canmsg, "0" ); } strcat( canmsg, wrkbuf ); } // add CR at end strcat( canmsg, "\r" ); // Send the frame //uart1Puts( canmsg ); p = canmsg; while ( 0 != *p ) { p = uart1Puts( p ); _cpsr = disableIRQ(); WDFEED = 0xAA; WDFEED = 0x55; restoreIRQ( _cpsr ); } //uart0Puts( canmsg ); //uart0Puts( "\r\n" ); return true; }
//#pragma import(__use_no_semihosting) // 向串口格式化输出字符串,如果不适用vsnprintf(),代码尺寸将大大减小。 void uart1_Printf(char *fmt,...) { va_list ap; char string[256]; va_start(ap,fmt); vsnprintf(string,sizeof(string),fmt,ap); uart1Puts(string); va_end(ap); }
int main(void) { int ch; int8_t res; systemInit(); gpioInit(); uart1Init(UART_BAUD(BAUD), UART_8N1, UART_FIFO_8); // setup the UART uart1Puts("\r\nMMC/SD Card Filesystem Test (P:LPC2138 L:EFSL)\r\n"); uart1Puts("efsl LPC2000-port and this Demo-Application\r\n"); uart1Puts("done by Martin Thomas, Kaiserslautern, Germany\r\n"); /* init efsl debug-output */ lpc2000_debug_devopen(uart1Putch); ledToggle(); rprintf("CARD init..."); if ( ( res = efs_init( &efs, 0 ) ) != 0 ) { rprintf("failed with %i\n",res); } else { rprintf("ok\n"); rprintf("Directory of 'root':\n"); ls_openDir( &list, &(efs.myFs) , "/"); while ( ls_getNext( &list ) == 0 ) { list.currentEntry.FileName[LIST_MAXLENFILENAME-1] = '\0'; rprintf( "%s ( %li bytes )\n" , list.currentEntry.FileName, list.currentEntry.FileSize ) ; } if ( file_fopen( &filer, &efs.myFs , LogFileName , 'r' ) == 0 ) { rprintf("File %s open. Content:\n", LogFileName); while ( ( e = file_read( &filer, 512, buf ) ) != 0 ) { buf[e]='\0'; uart1Puts((char*)buf); } rprintf("\n"); file_fclose( &filer ); } if ( file_fopen( &filew, &efs.myFs , LogFileName , 'a' ) == 0 ) { rprintf("File %s open for append. Appending...", LogFileName); strcpy((char*)buf, "Martin hat's angehaengt\r\n"); if ( file_write( &filew, strlen((char*)buf), buf ) == strlen((char*)buf) ) { rprintf("ok\n"); } else { rprintf("fail\n"); } file_fclose( &filew ); } if ( file_fopen( &filer, &efs.myFs , LogFileName , 'r' ) == 0 ) { rprintf("File %s open. Content:\n", LogFileName); while ( ( e = file_read( &filer, 512, buf ) ) != 0 ) { buf[e]='\0'; uart1Puts((char*)buf); } rprintf("\n"); file_fclose( &filer ); } fs_umount( &efs.myFs ) ; } while(1) { if ((ch = uart1Getch()) >= 0) { uart1Puts("You pressed : "); uart1Putch(ch); uart1Puts("\r\n"); if ( ch == 'M' ) { rprintf("Creating FS\n"); mkfs_makevfat(&efs.myPart); } ledToggle(); } } return 0; /* never reached */ }