Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mon.cpp
00001 //------------------------------------------------------------------------------------------------------------------- 00002 // Monitor program Ver.1 00003 // (c)2010 Kenji Arai / JH1PJL 00004 // http://www.page.sannet.ne.jp/kenjia/index.html 00005 // http://mbed.org/users/kenjiArai/ 00006 // May 9th,2010 Started 00007 // May 15th,2010 00008 //-------------------------------------------------------------------------------------------------------------------- 00009 // Reference 00010 // http://www.nxp.com/#/pip/pip=[pip=LPC1769_68_67_66_65_64_4]|pp=[t=pip,i=LPC1769_68_67_66_65_64_4] 00011 //-------------------------------------------------------------------------------------------------------------------- 00012 // Function 00013 // Show Memory contents, Digital port, Analog input port, CPU clock, RTC/Time and others in the mbed board 00014 // Connection 00015 // Analog input PIN 15,16,17 00016 // Digital input PIN 24,25,26 00017 // LCD PIN 5,6,7,21,22,23 00018 // RTC PIN 3 needs to connect 3V Battery 00019 // -> Please refer my program " RTC_w_COM" for time adjustment 00020 // at " http://mbed.org/users/kenjiArai/programs/RTC_w_COM/5yi9a/ " 00021 //--------------------------------------------------------------------------------------------------------------------- 00022 00023 #include "mbed.h" 00024 00025 // Define clocks 00026 #define XTAL (12000000UL) /* Oscillator frequency */ 00027 #define OSC_CLK ( XTAL) /* Main oscillator frequency */ 00028 #define RTC_CLK ( 32000UL) /* RTC oscillator frequency */ 00029 #define IRC_OSC ( 4000000UL) /* Internal RC oscillator frequency */ 00030 // Range check status 00031 #define ERR_NOTHING 0 00032 #define ERR_MODIFY_SIZ 1 00033 #define ERR_OUT_OF_RANGE 2 00034 00035 typedef struct{ 00036 unsigned long mstr; 00037 unsigned long msiz; 00038 unsigned long mtmp; 00039 unsigned long mold; 00040 unsigned char mflg; 00041 } MEMO; 00042 00043 Serial pc(USBTX, USBRX); 00044 AnalogIn ana0(p15); 00045 AnalogIn ana1(p16); 00046 AnalogIn ana2(p17); 00047 DigitalIn sw0(p24); 00048 DigitalIn sw1(p25); 00049 DigitalIn sw2(p26); 00050 00051 char linebuf[256]; 00052 00053 // Memory management 00054 static MEMO mem; 00055 unsigned long SystemFrequency; 00056 00057 // Memory range data 00058 const uint32_t mem_range[][2] = { // Memory access range 00059 { 0x00000000, 0x0007ffff }, // On-chip non-volatile memory //512KB Flash memory 00060 { 0x10000000, 0x10007fff }, // On-chip SRAM //32KB local RAM 00061 { 0x1fff0000, 0x1fff1fff }, // Boot ROM //8KB Boot ROM 00062 { 0x2007c000, 0x2007ffff }, // On-chip SRAM //16KB AHB SRAM 00063 { 0x20080000, 0x20083fff } // On-chip SRAM //16KB AHB SRAM 00064 }; 00065 00066 // Put \r\n 00067 void put_cr ( void ){ 00068 pc.putc('\r'); 00069 pc.putc('\n'); 00070 } 00071 00072 // Put \r 00073 void put_r ( void ){ 00074 pc.putc('\r'); 00075 } 00076 00077 // Help Massage 00078 void msg_hlp ( void ){ 00079 pc.printf("Monitor for mbed LPC1768 Ver. on "__DATE__" (" __TIME__ ")"); 00080 put_cr(); 00081 pc.printf("a - Show ADC data "); 00082 put_cr(); 00083 pc.printf("m - Entry Memory Mode"); 00084 put_cr(); 00085 pc.printf("m>? -> After entry the Memory Mode, adiitinal functions can see by ?"); 00086 put_cr(); 00087 pc.printf("p - Show Port status "); 00088 put_cr(); 00089 pc.printf("sf - System Clock"); 00090 put_cr(); 00091 pc.printf("sc - System / CPU information"); 00092 put_cr(); 00093 pc.printf("t [<year> <mon> <mday> <hour> <min> <sec>] "); 00094 put_cr(); 00095 pc.printf("/ [a],[p],[t] commands every 1 sec Exit =hit any key (not ENTER key) "); 00096 put_cr(); 00097 pc.printf("q - Quit (back to called routine)"); 00098 put_cr(); 00099 } 00100 00101 // Check Switch port status 00102 void check_port ( void ){ 00103 put_r(); 00104 pc.printf("Input Port Status"); 00105 put_cr(); 00106 pc.printf("mbed P26 (LPC1768 p2.0) = "); 00107 if ( sw2.read() ){ 00108 pc.printf("ON "); 00109 } else { 00110 pc.printf("OFF "); 00111 } 00112 put_cr(); 00113 pc.printf("mbed P25 (LPC1768 p2.1) = "); 00114 if ( sw1.read() ){ 00115 pc.printf("ON "); 00116 } else { 00117 pc.printf("OFF "); 00118 } 00119 put_cr(); 00120 pc.printf("mbed P24 (LPC1768 p2.2) = "); 00121 if ( sw0.read() ){ 00122 pc.printf("ON "); 00123 } else { 00124 pc.printf("OFF "); 00125 } 00126 put_cr(); 00127 } 00128 00129 void cpu_inf ( void ){ 00130 unsigned long m1, m2; 00131 00132 m1 = SCB->CPUID; 00133 m2 = ( m1 >> 24 ); 00134 if ( m2 == 0x41 ){ 00135 put_r(); 00136 pc.printf("CPU = ARM "); 00137 } else { 00138 put_r(); 00139 pc.printf("CPU = NOT ARM "); 00140 } 00141 m2 = ( m1 >> 4 ) & 0xfff; 00142 if ( m2 == 0xc23 ){ 00143 pc.printf("Cortex-M3"); 00144 put_cr(); 00145 } else { 00146 pc.printf("NOT Cortex-M3"); 00147 put_cr(); 00148 } 00149 m2 = ( m1 >> 20 ) & 0x0f; 00150 pc.printf("Variant:%x", m2); 00151 put_cr(); 00152 m2 = m1 & 0x7; 00153 pc.printf("Revision:%x", m2); 00154 put_cr(); 00155 } 00156 00157 // Calculate CPU System Clock Frequency 00158 void get_freq ( int pr ){ 00159 if(pr){ 00160 put_r(); 00161 pc.printf("System Clock = %dHz",SystemFrequency ); 00162 put_cr(); 00163 } 00164 if (((LPC_SC->PLL0STAT >> 24) & 3) == 3) {/* If PLL0 enabled and connected */ 00165 if(pr){ 00166 pc.printf("PLL0 enabled"); 00167 put_cr(); 00168 } 00169 switch (LPC_SC->CLKSRCSEL & 0x03) { 00170 case 0: /* Internal RC oscillator => PLL0 */ 00171 case 3: /* Reserved, default to Internal RC */ 00172 if(pr){ 00173 pc.printf("Internal RC Oscillator"); 00174 put_cr(); 00175 } 00176 SystemFrequency = (IRC_OSC * 00177 (((2 * ((LPC_SC->PLL0STAT & 0x7FFF) + 1))) / 00178 (((LPC_SC->PLL0STAT >> 16) & 0xFF) + 1)) / 00179 ((LPC_SC->CCLKCFG & 0xFF)+ 1)); 00180 break; 00181 case 1: /* Main oscillator => PLL0 */ 00182 if(pr){ 00183 pc.printf("Xtal Osc Clock = %dHz",XTAL ); 00184 put_cr(); 00185 } 00186 SystemFrequency = (OSC_CLK * 00187 (((2 * ((LPC_SC->PLL0STAT & 0x7FFF) + 1))) / 00188 (((LPC_SC->PLL0STAT >> 16) & 0xFF) + 1)) / 00189 ((LPC_SC->CCLKCFG & 0xFF)+ 1)); 00190 break; 00191 case 2: /* RTC oscillator => PLL0 */ 00192 if(pr){ 00193 pc.printf("RTC Xtal Oscillator f = %dHz", RTC_CLK ); 00194 put_cr(); 00195 } 00196 SystemFrequency = (RTC_CLK * 00197 (((2 * ((LPC_SC->PLL0STAT & 0x7FFF) + 1))) / 00198 (((LPC_SC->PLL0STAT >> 16) & 0xFF) + 1)) / 00199 ((LPC_SC->CCLKCFG & 0xFF)+ 1)); 00200 break; 00201 } 00202 } else { 00203 if(pr){ 00204 pc.printf("PLL0 disabled"); 00205 } 00206 switch (LPC_SC->CLKSRCSEL & 0x03) { 00207 case 0: /* Internal RC oscillator => PLL0 */ 00208 case 3: /* Reserved, default to Internal RC */ 00209 if(pr){ 00210 pc.printf("Internal RC Oscillator"); 00211 put_cr(); 00212 } 00213 SystemFrequency = IRC_OSC / ((LPC_SC->CCLKCFG & 0xFF)+ 1); 00214 break; 00215 case 1: /* Main oscillator => PLL0 */ 00216 if(pr){ 00217 pc.printf("Xtal Osc Clock = %dHz",XTAL ); 00218 put_cr(); 00219 } 00220 SystemFrequency = OSC_CLK / ((LPC_SC->CCLKCFG & 0xFF)+ 1); 00221 break; 00222 case 2: /* RTC oscillator => PLL0 */ 00223 if(pr){ 00224 pc.printf("RTC Xtal Oscillator f = %dHz", RTC_CLK ); 00225 put_cr(); 00226 } 00227 SystemFrequency = RTC_CLK / ((LPC_SC->CCLKCFG & 0xFF)+ 1); 00228 break; 00229 } 00230 } 00231 } 00232 00233 // Range check for Memory dump 00234 static void check_range( MEMO * mem ){ 00235 uint8_t i; 00236 uint32_t m; 00237 00238 mem->mflg = ERR_NOTHING; 00239 for ( i = 0 ; i < 5 ; i++ ){ 00240 if ( mem->mstr >= mem_range[i][0]){ 00241 if ( mem->mstr < mem_range[i][1] ){ 00242 m = mem->mstr + mem->msiz; 00243 if ( m < mem_range[i][1]){ 00244 return; // no modification 00245 } else { 00246 m = mem_range[i][1]; 00247 mem->msiz = m - mem->mstr + 1; 00248 mem->mflg = ERR_MODIFY_SIZ; 00249 return; // modified size 00250 } 00251 } 00252 } 00253 } 00254 mem->mflg = ERR_OUT_OF_RANGE; 00255 mem->mstr = 0; 00256 mem->msiz = 0; 00257 return ; 00258 } 00259 00260 // Memory dump error massage 00261 void error_print ( unsigned char flg ){ 00262 switch (flg) { 00263 case ERR_MODIFY_SIZ : 00264 put_r(); 00265 pc.printf("Reach to out of range "); 00266 put_cr(); 00267 break; 00268 case ERR_OUT_OF_RANGE : 00269 put_r(); 00270 pc.printf("Not in a memory area "); 00271 put_cr(); 00272 break; 00273 case ERR_NOTHING : 00274 default : 00275 ; 00276 } 00277 } 00278 00279 // Change string -> integer 00280 int xatoi (char **str, unsigned long *res){ 00281 unsigned long val; 00282 unsigned char c, radix, s = 0; 00283 00284 while ((c = **str) == ' ') (*str)++; 00285 if (c == '-') { 00286 s = 1; 00287 c = *(++(*str)); 00288 } 00289 if (c == '0') { 00290 c = *(++(*str)); 00291 if (c <= ' ') { 00292 *res = 0; return 1; 00293 } 00294 if (c == 'x') { 00295 radix = 16; 00296 c = *(++(*str)); 00297 } else { 00298 if (c == 'b') { 00299 radix = 2; 00300 c = *(++(*str)); 00301 } else { 00302 if ((c >= '0')&&(c <= '9')) 00303 radix = 8; 00304 else 00305 return 0; 00306 } 00307 } 00308 } else { 00309 if ((c < '1')||(c > '9')) 00310 return 0; 00311 radix = 10; 00312 } 00313 val = 0; 00314 while (c > ' ') { 00315 if (c >= 'a') c -= 0x20; 00316 c -= '0'; 00317 if (c >= 17) { 00318 c -= 7; 00319 if (c <= 9) return 0; 00320 } 00321 if (c >= radix) return 0; 00322 val = val * radix + c; 00323 c = *(++(*str)); 00324 } 00325 if (s) val = -val; 00326 *res = val; 00327 return 1; 00328 } 00329 00330 // Print memory contents 00331 void put_dump (const unsigned char *buff, unsigned long ofs, int cnt){ 00332 int n; 00333 00334 #if 1 00335 pc.printf("%08lX ", ofs); 00336 for(n = 0; n < cnt; n++){ 00337 pc.printf(" %02X", buff[n]); 00338 } 00339 pc.putc(' '); 00340 for(n = 0; n < cnt; n++) { 00341 if ((buff[n] < 0x20)||(buff[n] >= 0x7F)){ 00342 pc.putc('.'); 00343 } else { 00344 pc.putc(buff[n]); 00345 } 00346 } 00347 put_cr(); 00348 #endif 00349 00350 #if 0 00351 printf("%08lX ", ofs); 00352 for(n = 0; n < cnt; n++){ 00353 printf(" %02X", buff[n]); 00354 } 00355 pc.putc(' '); 00356 for(n = 0; n < cnt; n++) { 00357 if ((buff[n] < 0x20)||(buff[n] >= 0x7F)){ 00358 pc.putc('.'); 00359 } else { 00360 pc.putc(buff[n]); 00361 } 00362 } 00363 pc.puts("\r\n"); 00364 #endif 00365 } 00366 00367 // Get key input data 00368 void get_line (char *buff, int len){ 00369 char c; 00370 int idx = 0; 00371 00372 for (;;) { 00373 c = pc.getc(); 00374 // Added by Kenji Arai / JH1PJL May 9th, 2010 00375 if (c == '\r'){ 00376 buff[idx++] = c; 00377 break; 00378 } 00379 if ((c == '\b') && idx) { 00380 idx--; pc.putc(c); pc.putc(' '); pc.putc(c); 00381 } 00382 if (((uint8_t)c >= ' ') && (idx < len - 1)) { 00383 buff[idx++] = c; pc.putc(c); 00384 } 00385 } 00386 buff[idx] = 0; 00387 pc.putc('\n'); 00388 } 00389 00390 // Monitor Program 00391 int mon() { 00392 char *ptr; 00393 unsigned long p1; 00394 char c; 00395 time_t seconds; 00396 struct tm rtc; 00397 00398 pc.baud(115200); 00399 put_cr(); 00400 put_cr(); 00401 pc.printf("Monitor for mbed LPC1768 Ver. on "__DATE__" (" __TIME__ ") [Help:'?' key]"); 00402 put_cr(); 00403 get_freq(0); 00404 for (;;) { 00405 put_r(); 00406 pc.putc('>'); 00407 ptr = linebuf; 00408 get_line(ptr, sizeof(linebuf)); 00409 switch (*ptr++) { 00410 //-------------------------------------------------------------------------------------- 00411 // Memory 00412 //-------------------------------------------------------------------------------------- 00413 case 'm' : 00414 put_r(); 00415 pc.printf("Entry Memory Mode 1) d <address> [<count>], 2) s, 3) <ret>, 4) <esc>"); 00416 put_cr(); 00417 mem.mstr = 0; 00418 mem.msiz =256; 00419 mem.mold = 0; 00420 mem.mtmp = 0; 00421 mem.mflg = 0; 00422 for (;mem.mflg != 0xff;){ 00423 pc.printf("m>"); 00424 c = pc.getc(); 00425 pc.putc(c); 00426 switch (c) { 00427 case 'd' : /* d <address> [<count>] - Dump memory */ 00428 ptr = linebuf; 00429 get_line(ptr, sizeof(linebuf)); 00430 mem.mtmp = mem.mstr; 00431 if (!xatoi(&ptr, &mem.mstr)){ 00432 mem.mstr = mem.mtmp; 00433 } 00434 if (!xatoi(&ptr, &mem.msiz)){ 00435 mem.msiz = 256; 00436 } 00437 mem.mtmp = mem.msiz; 00438 check_range (&mem); 00439 for (ptr=(char*)mem.mstr; mem.msiz >= 16; ptr += 16, mem.msiz -= 16){ 00440 put_r(); 00441 put_dump((unsigned char*)ptr, (unsigned int)ptr, 16); 00442 } 00443 if (mem.msiz){ 00444 put_dump((unsigned char*)ptr, (unsigned int)ptr, mem.msiz); 00445 } 00446 error_print(mem.mflg); 00447 mem.mold = mem.mstr; 00448 mem.mstr += mem.mtmp; 00449 break; 00450 case 0x0d : // CR 00451 put_cr(); 00452 mem.msiz = 256; 00453 mem.mtmp = mem.msiz; 00454 check_range (&mem); 00455 for (ptr=(char*)mem.mstr; mem.msiz >= 16; ptr += 16, mem.msiz -= 16){ 00456 put_r(); 00457 put_dump((unsigned char*)ptr, (unsigned int)ptr, 16); 00458 } 00459 error_print(mem.mflg); 00460 mem.mold = mem.mstr; 00461 mem.mstr += 256; 00462 break; 00463 case 0x1b : // ESC 00464 mem.mflg = 0xff; 00465 break; 00466 case 0x08 : // Back Space 00467 put_cr(); 00468 mem.mstr = mem.mold; 00469 mem.msiz = 256; 00470 mem.mtmp = mem.msiz; 00471 check_range (&mem); 00472 for (ptr=(char*)mem.mstr; mem.msiz >= 16; ptr += 16, mem.msiz -= 16){ 00473 put_r(); 00474 put_dump((unsigned char*)ptr, (unsigned int)ptr, 16); 00475 } 00476 error_print(mem.mflg); 00477 mem.mstr += 256; 00478 break; 00479 case 's' : 00480 put_cr(); 00481 pc.printf("Memory Configuration"); 00482 put_cr(); 00483 pc.printf("FLASH 0x%08lx to 0x%08lx ", mem_range[0][0], mem_range[0][1]); 00484 put_cr(); 00485 pc.printf("S-RAM 0x%08lx to 0x%08lx ", mem_range[1][0], mem_range[1][1]); 00486 put_cr(); 00487 pc.printf("Boot-ROM 0x%08lx to 0x%08lx ", mem_range[2][0], mem_range[2][1]); 00488 put_cr(); 00489 pc.printf("AHB-RAM0 0x%08lx to 0x%08lx ", mem_range[3][0], mem_range[3][1]); 00490 put_cr(); 00491 pc.printf("AHB-RAM1 0x%08lx to 0x%08lx \r\n", mem_range[4][0], mem_range[4][1]); 00492 break; 00493 case '?' : 00494 put_cr(); 00495 pc.printf("d <address> [<count>] - Dump memory"); 00496 put_cr(); 00497 pc.printf("s - Show memory structure "); 00498 put_cr(); 00499 pc.printf("<RET> - Dump memory / next 128 bytes"); 00500 put_cr(); 00501 pc.printf("<BS> - Dump memory / same as before 128 bytes"); 00502 put_cr(); 00503 pc.printf("<ESC> - Exit memory mode"); 00504 put_cr(); 00505 break; 00506 default: 00507 put_cr(); 00508 } 00509 } 00510 pc.putc('>'); 00511 put_cr(); 00512 break; 00513 //-------------------------------------------------------------------------------------- 00514 // Port 00515 //-------------------------------------------------------------------------------------- 00516 case 'p' : /* p - Show Port status */ 00517 check_port(); 00518 break; 00519 //-------------------------------------------------------------------------------------- 00520 // ADC 00521 //-------------------------------------------------------------------------------------- 00522 case 'a' : /* a - Show ADC data */ 00523 put_r(); 00524 pc.printf("anao = %f, ana1 = %f, ana2 = %f", ana0.read(), ana1.read(), ana2.read()); 00525 put_cr(); 00526 break; 00527 //-------------------------------------------------------------------------------------- 00528 // Time 00529 //-------------------------------------------------------------------------------------- 00530 case 't' : /* t [<year> <mon> <mday> <hour> <min> <sec>] */ 00531 if (xatoi(&ptr, &p1)) { 00532 rtc.tm_year = (unsigned short)p1; 00533 xatoi(&ptr, &p1); rtc.tm_mon = (unsigned char)p1; 00534 xatoi(&ptr, &p1); rtc.tm_mday = (unsigned char)p1; 00535 xatoi(&ptr, &p1); rtc.tm_hour = (unsigned char)p1; 00536 xatoi(&ptr, &p1); rtc.tm_min = (unsigned char)p1; 00537 if (!xatoi(&ptr, &p1)) break; 00538 rtc.tm_sec = (unsigned char)p1; 00539 seconds = mktime(&rtc); 00540 set_time(seconds); 00541 } 00542 while ( seconds == time(NULL)) ; 00543 seconds = time(NULL); 00544 // 13:24:00 PM (2010/03/27) 00545 strftime(linebuf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); 00546 put_r(); 00547 pc.printf("Time = %s", linebuf); 00548 put_cr(); 00549 break; 00550 //-------------------------------------------------------------------------------------- 00551 // System 00552 //-------------------------------------------------------------------------------------- 00553 case 's' : /* System related information */ 00554 switch (*ptr++) { 00555 case 'f' : /* sc - show system clock frequency */ 00556 get_freq(1); 00557 break; 00558 case 'c' : /* sc - show system CPU information*/ 00559 cpu_inf(); 00560 break; 00561 case '?' : 00562 pc.printf("sc - System CPU information"); 00563 put_cr(); 00564 pc.printf("sf - System Clock"); 00565 put_cr(); 00566 break; 00567 } 00568 break; 00569 //-------------------------------------------------------------------------------------- 00570 // Repeat Command every second 00571 //-------------------------------------------------------------------------------------- 00572 case '/' : 00573 switch (*ptr++) { 00574 case 'a' : 00575 while(1){ 00576 put_r(); 00577 pc.printf("anao = %f, ana1 = %f, ana2 = %f", ana0.read(), ana1.read(), ana2.read()); 00578 put_cr(); 00579 if ( pc.readable() ){ 00580 break; 00581 } 00582 wait(1.0); // Wait 1sec 00583 } 00584 break; 00585 case 'p' : 00586 while(1){ 00587 check_port(); 00588 if ( pc.readable() ){ 00589 break; 00590 } 00591 wait(1.0); // Wait 1sec 00592 } 00593 break; 00594 case 't' : 00595 while(1){ 00596 while ( seconds == time(NULL)) ; 00597 seconds = time(NULL); 00598 // 13:24:00 PM (2010/03/27) 00599 strftime(linebuf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); 00600 put_r(); 00601 pc.printf("Time = %s", linebuf); 00602 put_cr(); 00603 if ( pc.readable() ){ 00604 break; 00605 } 00606 wait(1.0); // Wait 1sec 00607 } 00608 break; 00609 } 00610 break; 00611 //-------------------------------------------------------------------------------------- 00612 // Help 00613 //-------------------------------------------------------------------------------------- 00614 case '?' : 00615 msg_hlp(); 00616 break; 00617 //-------------------------------------------------------------------------------------- 00618 // Return to main routine 00619 //-------------------------------------------------------------------------------------- 00620 case 'q' : // Quit 00621 pc.printf("\rReturn to main but back to mon()\r\n"); 00622 return 0; 00623 } 00624 } 00625 }
Generated on Fri Jul 29 2022 23:39:36 by
1.7.2