00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 #include "board.h"
00110
00111 #include <stdint.h>
00112 #include <stdio.h>
00113 #include <stdarg.h>
00114
00115
00116
00117
00118
00119
00120 #define STATE_MENU 0
00121
00122 #define STATE_SET_TIME 1
00123
00124 #define STATE_SET_DATE 2
00125
00126 #define STATE_SET_TIME_ALARM 3
00127
00128 #define STATE_SET_DATE_ALARM 4
00129
00130
00131 #define MAX_EDIT_SIZE 10
00132
00133
00134 #define IsDigitChar(c) ((c) >= '0' && (c) <='9')
00135
00136 #define CharToDigit(c) ((c) - '0')
00137
00138
00139
00140
00141 volatile uint16_t Temperature=0;
00142 volatile uint32_t CountDownTimer = 0;
00143
00144 static unsigned int bState = STATE_MENU;
00145
00146
00147 static unsigned char newHour;
00148
00149 static unsigned char newMinute;
00150
00151 static unsigned char newSecond;
00152
00153
00154 static unsigned short newYear;
00155
00156 static unsigned char newMonth;
00157
00158 static unsigned char newDay;
00159
00160 static unsigned char newWeek;
00161
00162
00163 static unsigned char alarmTriggered = 0;
00164
00165
00166 static char rtc_time[8+1] = {'0', '0', ':', '0', '0', ':', '0', '0','\0'};
00167
00168 static char date[10+1] = {'0', '0', '/', '0', '0', '/', '0', '0', '0', '0', '\0'};
00169
00170 static char pDayNames[7][4] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
00171
00172 static char pEraseSeq[] = "\b \b";
00173
00174 static char calendar[80];
00175
00176 static unsigned int bMenuShown = 0;
00177
00178
00179
00180
00181
00182
00183
00184 static signed int _PrnToBuf( char *pBuf, const char *pFormat, ... )
00185 {
00186 va_list ap ;
00187 signed int rc ;
00188
00189 va_start( ap, pFormat ) ;
00190 rc = vsprintf( pBuf, pFormat, ap ) ;
00191 va_end( ap ) ;
00192
00193 return rc ;
00194 }
00195
00196
00197
00198
00199 static int _GetNewTime( void )
00200 {
00201 char ucKey ;
00202 int i=0 ;
00203
00204
00205 newHour = newMinute = newSecond = 0xFF;
00206
00207
00208 while ( 1 ) {
00209 ucKey = DBG_GetChar();
00210
00211
00212 if ( ucKey == 0x0d || ucKey == 0x0a ) {
00213 puts("\n\r");
00214 break;
00215 }
00216
00217
00218 if ( ucKey == 0x7f || ucKey == 0x08 ) {
00219 if ( i > 0 ) {
00220
00221 if ( !rtc_time[i] ) {
00222 --i ;
00223 }
00224
00225 puts( pEraseSeq ) ;
00226 --i ;
00227
00228
00229 if ( !IsDigitChar( rtc_time[i] ) && i > 0 ) {
00230 puts( pEraseSeq ) ;
00231 --i ;
00232 }
00233 }
00234 }
00235
00236
00237 if ( !rtc_time[i] ) {
00238 continue ;
00239 }
00240
00241 if ( !IsDigitChar( ucKey ) ) {
00242 continue ;
00243 }
00244
00245 DBG_PutChar( ucKey ) ;
00246 rtc_time[i++] = ucKey ;
00247
00248
00249 if ( !IsDigitChar( rtc_time[i] ) && i < 8 ) {
00250 DBG_PutChar( rtc_time[i] ) ;
00251 ++i ;
00252 }
00253 }
00254
00255 if ( i == 0 ) {
00256 return 0 ;
00257 }
00258
00259 if ( i != 0 && rtc_time[i] != '\0' ) {
00260 return 1 ;
00261 }
00262
00263 newHour = CharToDigit( rtc_time[0] ) * 10 + CharToDigit( rtc_time[1] ) ;
00264 newMinute = CharToDigit( rtc_time[3] ) * 10 + CharToDigit( rtc_time[4] ) ;
00265 newSecond = CharToDigit( rtc_time[6] ) * 10 + CharToDigit( rtc_time[7] ) ;
00266
00267
00268 return 0 ;
00269 }
00270
00271
00272
00273
00274 static char _CalcWeek( int year, int month, int day )
00275 {
00276 char week ;
00277
00278 if ( month == 1 || month == 2 ) {
00279 month += 12 ;
00280 --year ;
00281 }
00282
00283 week = (day + 2 * month + 3 * (month + 1 ) / 5 + year + year / 4 -
00284 year / 100 + year / 400 ) % 7;
00285 ++week ;
00286
00287 return week ;
00288 }
00289
00290
00291
00292
00293
00294 static int _GetNewDate( void )
00295 {
00296 char ucKey ;
00297 int i=0;
00298
00299
00300 newYear = 0xFFFF;
00301 newMonth = newDay= newWeek = 0xFF;
00302
00303
00304 while ( 1 ) {
00305 ucKey = DBG_GetChar() ;
00306
00307
00308 if ( ucKey == 0x0d || ucKey == 0x0a ) {
00309 puts( "\n\r" ) ;
00310 break ;
00311 }
00312
00313 if ( ucKey == 0x7f || ucKey == 0x08 ) {
00314 if ( i > 0 ) {
00315
00316 if ( !date[i] ) {
00317 --i ;
00318 }
00319
00320 puts( pEraseSeq ) ;
00321 --i ;
00322
00323
00324 if ( !IsDigitChar( date[i] ) && i > 0 ) {
00325 puts( pEraseSeq ) ;
00326 --i ;
00327 }
00328 }
00329 }
00330
00331
00332 if ( !date[i] ) {
00333 continue ;
00334 }
00335
00336 if ( !IsDigitChar( ucKey ) ) {
00337 continue;
00338 }
00339
00340 DBG_PutChar( ucKey ) ;
00341 date[i++] = ucKey ;
00342
00343
00344 if ( !IsDigitChar( date[i] ) && i < 10 ) {
00345 DBG_PutChar( date[i] ) ;
00346 ++i ;
00347 }
00348 }
00349
00350 if ( i == 0 ) {
00351 return 0 ;
00352 }
00353
00354 if ( i != 0 && date[i] != '\0' && i != 6 ) {
00355 return 1 ;
00356 }
00357
00358
00359 newMonth = CharToDigit( date[0] ) * 10 + CharToDigit( date[1] ) ;
00360 newDay = CharToDigit( date[3] ) * 10 + CharToDigit( date[4] ) ;
00361 if ( i != 6 )
00362 {
00363 newYear = CharToDigit(date[6]) * 1000 + CharToDigit(date[7]) * 100 +
00364 CharToDigit(date[8]) * 10 + CharToDigit(date[9] ) ;
00365 newWeek = _CalcWeek( newYear, newMonth, newDay ) ;
00366 }
00367
00368
00369 return 0 ;
00370 }
00371
00372
00373
00374
00375 void TC0_Handler( void )
00376 {
00377 volatile uint32_t dummy;
00378
00379 dummy = TC0->TC_CHANNEL[ 0 ].TC_SR ;
00380
00381 if(CountDownTimer >= 240 ) {
00382 RTC_ClockCalibration(RTC, Temperature);
00383 TRACE_INFO("RTC has been re-calibrated \n\r");
00384 CountDownTimer = 0;
00385 }
00386 CountDownTimer++;
00387 }
00388
00389
00390
00391
00392 static void _RefreshDisplay( void )
00393 {
00394 unsigned char hour, minute, second ;
00395 unsigned short year ;
00396 unsigned char month, day, week ;
00397
00398 if ( bState != STATE_MENU ) {
00399 } else {
00400
00401 RTC_GetTime( RTC, &hour, &minute, &second ) ;
00402 RTC_GetDate( RTC, &year, &month, &day, &week ) ;
00403
00404
00405 if ( !bMenuShown ) {
00406 printf( "\n\rMenu:\n\r" ) ;
00407 printf( " t - Set time\n\r" ) ;
00408 printf( " d - Set date\n\r" ) ;
00409 printf( " i - Set time alarm\n\r" ) ;
00410 printf( " m - Set date alarm\n\r" ) ;
00411 printf( " p - PPM calibration of RTC\n\r" ) ;
00412
00413 if ( alarmTriggered ) {
00414 printf( " c - Clear alarm notification\n\r" ) ;
00415 }
00416
00417 printf( " q - Quit!\n\r" ) ;
00418 printf( "\n\r" ) ;
00419 bMenuShown = 1 ;
00420 }
00421
00422
00423 _PrnToBuf( rtc_time, "%02d:%02d:%02d", hour, minute, second ) ;
00424 _PrnToBuf( date, "%02d/%02d/%04d", month, day, year ) ;
00425 _PrnToBuf( calendar, " [Time/Date: %s, %s %s ][Alarm status:%s]",
00426 rtc_time, date, pDayNames[week-1], alarmTriggered?"Triggered!":"" ) ;
00427 printf( "\r%s", calendar ) ;
00428 }
00429 }
00430
00431
00432
00433
00434 void RTC_Handler( void )
00435 {
00436 uint32_t dwStatus = RTC->RTC_SR ;
00437
00438
00439 if ( (dwStatus & RTC_SR_SEC) == RTC_SR_SEC ) {
00440
00441 RTC_DisableIt( RTC, RTC_IDR_SECDIS ) ;
00442
00443 _RefreshDisplay() ;
00444
00445 RTC->RTC_SCCR = RTC_SCCR_SECCLR ;
00446
00447 RTC_EnableIt( RTC, RTC_IER_SECEN ) ;
00448 }
00449
00450 else {
00451 if ( (dwStatus & RTC_SR_ALARM) == RTC_SR_ALARM ) {
00452
00453 RTC_DisableIt( RTC, RTC_IDR_ALRDIS ) ;
00454
00455 alarmTriggered = 1 ;
00456 _RefreshDisplay() ;
00457 bMenuShown = 0 ;
00458 RTC->RTC_SCCR = RTC_SCCR_ALRCLR ;
00459
00460 RTC_EnableIt( RTC, RTC_IER_ALREN ) ;
00461 }
00462 }
00463 }
00464
00465
00466
00467
00468 static void _ConfigureTc( void )
00469 {
00470 uint32_t div;
00471 uint32_t tcclks;
00472
00473
00474 PMC_EnablePeripheral( ID_TC0);
00475
00476
00477 TC_FindMckDivisor( 4, BOARD_MCK/2, &div, &tcclks, BOARD_MCK );
00478 TC_Configure( TC0, 0, tcclks | TC_CMR_CPCTRG );
00479 TC0->TC_CHANNEL[ 0 ].TC_RC = ( BOARD_MCK / div ) / 4;
00480
00481
00482 NVIC_ClearPendingIRQ(TC0_IRQn);
00483 NVIC_EnableIRQ(TC0_IRQn);
00484
00485 NVIC_SetPriority( TC0_IRQn , 2);
00486 TC0->TC_CHANNEL[ 0 ].TC_IER = TC_IER_CPCS ;
00487
00488 TC_Start( TC0, 0 );
00489
00490 }
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 extern int main( void )
00503 {
00504 uint8_t ucKey ;
00505
00506
00507 WDT_Disable( WDT ) ;
00508
00509
00510 SCB_EnableICache();
00511 SCB_EnableDCache();
00512
00513
00514 printf( "\n\r\n\r\n\r" ) ;
00515 printf( "-- RTC Example %s --\n\r", SOFTPACK_VERSION ) ;
00516 printf( "-- %s\n\r", BOARD_NAME ) ;
00517 printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00518
00519
00520 printf( "Configure TC.\n\r" );
00521 _ConfigureTc() ;
00522
00523 Temperature = 25;
00524
00525
00526 RTC_SetHourMode( RTC, 0 ) ;
00527 if ( RTC_SetTimeAlarm( RTC, 0, 0, 0 ) ) {
00528 printf( "\n\r Disable time alarm fail!" ) ;
00529 }
00530
00531 if ( RTC_SetDateAlarm( RTC, 0, 0 ) ) {
00532 printf( "\n\r Disable date alarm fail!" ) ;
00533 }
00534
00535
00536 NVIC_DisableIRQ( RTC_IRQn ) ;
00537 NVIC_ClearPendingIRQ( RTC_IRQn ) ;
00538 NVIC_SetPriority( RTC_IRQn, 0 ) ;
00539 NVIC_EnableIRQ( RTC_IRQn ) ;
00540 RTC_EnableIt( RTC, RTC_IER_SECEN | RTC_IER_ALREN ) ;
00541
00542
00543 _RefreshDisplay() ;
00544 newHour = 0; newMinute =0; newSecond = 30;
00545 RTC_SetTimeAlarm( RTC, &newHour, &newMinute, &newSecond ) ;
00546 bMenuShown = 0 ;alarmTriggered = 0 ;
00547 RTC_ClockCalibration(RTC, Temperature);
00548
00549
00550 while ( 1 ) {
00551 ucKey = DBG_GetChar() ;
00552
00553
00554 if ( ucKey == 't' ) {
00555 bState = STATE_SET_TIME;
00556
00557 do {
00558 printf( "\n\r\n\r Set time(hh:mm:ss): " ) ;
00559 } while( _GetNewTime() ) ;
00560
00561
00562 if ( newHour != 0xFF ) {
00563 if ( RTC_SetTime( RTC, newHour, newMinute, newSecond ) ) {
00564 printf( "\n\r Time not set, invalid input!\n\r" ) ;
00565 }
00566 }
00567
00568 bState = STATE_MENU ;
00569 bMenuShown = 0 ;
00570 _RefreshDisplay() ;
00571 CountDownTimer = 0;
00572 }
00573
00574 if ( ucKey == 'p' ) {
00575
00576 RTC_ClockCalibration(RTC, 30);
00577 bState = STATE_MENU ;
00578 bMenuShown = 0 ;
00579 _RefreshDisplay() ;
00580 }
00581
00582
00583 if ( ucKey == 'd' ) {
00584 bState = STATE_SET_DATE ;
00585
00586 do {
00587 printf( "\n\r\n\r Set date(mm/dd/yyyy): " ) ;
00588 } while ( _GetNewDate() ) ;
00589
00590
00591 if ( newYear !=0xFFFF ) {
00592 if ( RTC_SetDate( RTC, newYear, newMonth, newDay, newWeek ) ) {
00593 printf( "\n\r Date not set, invalid input!\n\r" ) ;
00594 }
00595 }
00596
00597
00598 if ( newMonth != 0xFF && newYear == 0xFFFF ) {
00599 printf( "\n\r Not Set for no year field!\n\r" ) ;
00600 }
00601
00602 bState = STATE_MENU ;
00603 bMenuShown = 0 ;
00604 CountDownTimer = 0;
00605 _RefreshDisplay() ;
00606 }
00607
00608
00609
00610 if ( ucKey == 'i') {
00611 bState = STATE_SET_TIME_ALARM ;
00612
00613 do {
00614 printf( "\n\r\n\r Set time alarm(hh:mm:ss): " ) ;
00615 } while ( _GetNewTime() ) ;
00616
00617 if ( newHour != 0xFF ) {
00618 if ( RTC_SetTimeAlarm( RTC, &newHour, &newMinute, &newSecond ) ) {
00619 printf( "\n\r Time alarm not set, invalid input!\n\r" ) ;
00620 } else {
00621 printf( "\n\r Time alarm is set at %02d:%02d:%02d!",
00622 newHour, newMinute, newSecond ) ;
00623 }
00624 }
00625 bState = STATE_MENU ;
00626 bMenuShown = 0 ;
00627 alarmTriggered = 0 ;
00628 CountDownTimer = 0;
00629 _RefreshDisplay() ;
00630 }
00631
00632
00633 if ( ucKey == 'm' ) {
00634 bState = STATE_SET_DATE_ALARM;
00635
00636 do {
00637 printf( "\n\r\n\r Set date alarm(mm/dd/): " ) ;
00638 } while ( _GetNewDate() ) ;
00639
00640 if ( newYear == 0xFFFF && newMonth != 0xFF ) {
00641 if ( RTC_SetDateAlarm( RTC, &newMonth, &newDay ) ) {
00642 printf( "\n\r Date alarm not set, invalid input!\n\r" ) ;
00643 } else {
00644 printf( "\n\r Date alarm is set on %02d/%02d!",
00645 newMonth, newDay ) ;
00646 }
00647
00648 }
00649 bState = STATE_MENU ;
00650 bMenuShown = 0 ;
00651 alarmTriggered = 0 ;
00652 CountDownTimer = 0;
00653 _RefreshDisplay() ;
00654 }
00655
00656
00657 if ( ucKey == 'c' ) {
00658 alarmTriggered = 0 ;
00659 bMenuShown = 0 ;
00660 _RefreshDisplay() ;
00661 }
00662
00663
00664 if ( ucKey == 'q' ) {
00665 break ;
00666 }
00667 }
00668
00669 return 0 ;
00670 }