2410RTC.c
Upload User: yzchenlin
Upload Date: 2022-03-09
Package Size: 712k
Code Size: 9k
Development Platform:

C/C++

  1. //====================================================================
  2. // File Name : 2410RTC.c
  3. // Function  : S3C2410 RTC Test Program
  4. // Program   : Shin, On Pil (SOP)
  5. // Date      : May 06, 2002
  6. // Version   : 0.0
  7. // History
  8. //   0.0 : Programming start (March 11, 2002) -> SOP
  9. //====================================================================
  10. #include "2410addr.h"
  11. #include "2410lib.h"
  12. #include "2410RTC.h"
  13. char *day[8] = {" ","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
  14. volatile int isRtcInt, isInit = 2;              //April 01, 2002 SOP
  15. volatile unsigned int sec_tick;
  16. //=======================================
  17. //Period = (n + 1) / 128 second
  18. //   n : Tick time count value (1~127)
  19. //=======================================
  20. //********************[ Display_Rtc ]*********************************
  21. void Display_Rtc(void)
  22. {
  23. //    int year,tmp;
  24.     int year,tmp,key;                   
  25.     int month,date,weekday,hour,min,sec;
  26.     Uart_Printf("[ Display RTC Test ]n");
  27.     Uart_Printf("0. RTC Initialize     1. RTC Time Setting    2. Only RTC Displaynn");
  28.     Uart_Printf("Selet : ");
  29.     key = Uart_GetIntNum();
  30.     Uart_Printf("nn");
  31.     
  32.     isInit = key;
  33.     if(isInit == 0)
  34.     {
  35.         Rtc_Init();
  36.         isInit = 2;
  37.     }
  38.     
  39.     else if(isInit == 1)
  40.     {
  41.         Rtc_TimeSet();
  42.         isInit = 2;        
  43.     }
  44.     rRTCCON = 0x01;    //No reset, Merge BCD counters, 1/32768, RTC Control enable
  45. //  Uart_Printf("This test should be excuted once RTC test(Alarm) for RTC initializationn");
  46.     Uart_Printf("Press any key to exit.nn");
  47.     while(!Uart_GetKey())
  48.     {    
  49.         while(1)
  50.         {
  51.             if(rBCDYEAR==0x99) 
  52.                 year = 0x1999;
  53.             else 
  54.                 year    = 0x2000 + rBCDYEAR;
  55.             month   = rBCDMON;
  56.             weekday = rBCDDAY;
  57.             date    = rBCDDATE;
  58.             hour    = rBCDHOUR;
  59.             min     = rBCDMIN;
  60.             sec     = rBCDSEC;
  61.             
  62.             if(sec!=tmp)                //Same time is not display
  63.             {
  64.                 tmp = sec;
  65.                 break;
  66.             }         
  67.         }
  68.         Uart_Printf("%2x : %2x : %2x  %10s,  %2x/%2x/%4xn",hour,min,sec,day[weekday],month,date,year);
  69. //        Uart_Printf("%2x : %2x : %2x  %9s,  %2x/%2x/%4x     rBCDDATE = %x, weekday= %xn",hour,min,sec,day[weekday],month,date,year,rBCDDATE,weekday);        
  70.     }
  71.     rRTCCON = 0x0; //No reset, Merge BCD counters, 1/32768, RTC Control disable(for power consumption)
  72. }
  73. //***********************[ RndRst_Rtc ]*********************************
  74. //Round boundary 30, 40 or 50 Sec
  75. //For example, when the current time is 23:37:47 and the round baundary is selected to 40 sec,
  76. //the round reset changes the current time is 23:38:00.
  77. void RndRst_Rtc(void)
  78. {
  79.     int year;
  80.     int month,date,weekday,hour,min,sec,tmp;
  81.     unsigned int save_GPFCON;
  82.     save_GPFCON = rGPFCON;
  83.     rEXTINT0 = 0x2;  //Falling edge triggered
  84.     rGPFCON  = 0x2;  //EINT0
  85.     pISR_EINT0 = (unsigned int)EINT0_int;
  86.     rINTMSK    = ~(BIT_EINT0);
  87.     if(isInit==0)
  88.     {
  89.         Rtc_Init();
  90.         isInit = 1;
  91.     }
  92.     rRTCCON = 0x01;    //No reset, Merge BCD counters, 1/32768, RTC Control enable
  93.     Uart_Printf("Press any key to exit.nn");
  94.     Uart_Printf("Press EINT0 key to test round reset.n");
  95.     
  96.     while(!Uart_GetKey())
  97.     {
  98.         while(1)
  99.         {
  100.             if(rBCDYEAR == 0x99) 
  101.                 year = 0x1999;
  102.             else 
  103.                 year    = 0x2000 + rBCDYEAR;
  104.             month   = rBCDMON;
  105.             weekday = rBCDDAY;
  106.             date    = rBCDDATE;
  107.             hour    = rBCDHOUR;
  108.             min     = rBCDMIN;
  109.             sec     = rBCDSEC;
  110.             
  111.             if(sec!=tmp)
  112.             {
  113.                 tmp = sec;
  114.                 break;
  115.             }
  116.         }      
  117.         Uart_Printf("%2x : %2x : %2x  %10s,  %2x/%2x/%4xn",hour,min,sec,day[weekday],month,date,year);
  118.     }
  119.     rRTCCON = 0x0;   //No reset, Merge BCD counters, 1/32768, RTC Control disable
  120.     rGPFCON = save_GPFCON;
  121. }
  122. //************************[ Test_Rtc_Alarm ]******************************
  123. void Test_Rtc_Alarm(void)  
  124. {
  125.     Uart_Printf("[ RTC Alarm Test for S3C2410 ]n");
  126.     Rtc_Init();
  127.     rRTCCON  = 0x01;                    //No reset, Merge BCD counters, 1/32768, RTC Control enable
  128.     rALMYEAR = TESTYEAR2 ;
  129.     rALMMON  = TESTMONTH2;
  130.     rALMDATE = TESTDATE2  ;
  131.     rALMHOUR = TESTHOUR2 ;
  132.     rALMMIN  = TESTMIN2  ;
  133.     rALMSEC  = TESTSEC2 + 9; 
  134.     isRtcInt = 0;
  135.     pISR_RTC = (unsigned int)Rtc_Int;
  136.     rRTCALM  = 0x7f;                    //Global,Year,Month,Day,Hour,Minute,Second alarm enable
  137.     rRTCCON  = 0x0;                     //No reset, Merge BCD counters, 1/32768, RTC Control disable
  138.     rINTMSK  = ~(BIT_RTC);
  139.     while(isRtcInt==0);
  140.     
  141.     rINTMSK = BIT_ALLMSK;
  142. //  rRTCCON = 0x0;  //RTC  Control disable(for power consumption), 1/32768, Normal(merge), No reset
  143. }
  144. //************************[ Rtc_Init ]*********************************
  145. void Rtc_Init(void)
  146. {
  147.     rRTCCON  = rRTCCON  & ~(0xf)  | 0x1;            //No reset, Merge BCD counters, 1/32768, RTC Control enable
  148.     
  149.     rBCDYEAR = rBCDYEAR & ~(0xff) | TESTYEAR;
  150.     rBCDMON  = rBCDMON  & ~(0x1f) | TESTMONTH;
  151.     rBCDDATE = rBCDDATE & ~(0x3f) | TESTDATE;         
  152.     rBCDDAY  = rBCDDAY  & ~(0x7)  | TESTDAY;       //SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
  153.     rBCDHOUR = rBCDHOUR & ~(0x3f) | TESTHOUR;
  154.     rBCDMIN  = rBCDMIN  & ~(0x7f) | TESTMIN;
  155.     rBCDSEC  = rBCDSEC  & ~(0x7f) | TESTSEC;
  156.     
  157.     rRTCCON  = 0x0;             //No reset, Merge BCD counters, 1/32768, RTC Control disable    
  158. }
  159. //************************[ Rtc_TimeSet ]*********************************
  160. void Rtc_TimeSet(void)
  161. {
  162.     int syear,smonth,sdate,shour,smin,ssec;
  163.     int sday;
  164.     Uart_Printf("[ RTC Time Setting ]n");
  165.     Rtc_Init();         //RTC Initialize
  166.     Uart_Printf("RTC Time Initialized ...n");
  167.     
  168.     Uart_Printf("Year (Two digit the latest)[0x??] : ");
  169.     syear = Uart_GetIntNum();
  170.     
  171.     Uart_Printf("Month                      [0x??] : ");
  172.     smonth = Uart_GetIntNum();
  173.     
  174.     Uart_Printf("Date                       [0x??] : ");
  175.     sdate = Uart_GetIntNum();
  176.           
  177.     Uart_Printf("n1:Sunday  2:Monday  3:Thesday  4:Wednesday  5:Thursday  6:Friday  7:Saturdayn");
  178.     Uart_Printf("Day of the week                   : ");
  179.     sday = Uart_GetIntNum();
  180.     
  181.     Uart_Printf("Hour                       [0x??] : ");
  182.     shour = Uart_GetIntNum();
  183.     
  184.     Uart_Printf("Minute                     [0x??] : ");
  185.     smin = Uart_GetIntNum();
  186.     
  187.     Uart_Printf("Second                     [0x??] : ");
  188.     ssec = Uart_GetIntNum();
  189.     
  190.     rRTCCON  = rRTCCON  & ~(0xf)  | 0x1;            //No reset, Merge BCD counters, 1/32768, RTC Control enable
  191.     
  192.     rBCDYEAR = rBCDYEAR & ~(0xff) | syear;
  193.     rBCDMON  = rBCDMON  & ~(0x1f) | smonth;
  194.     rBCDDAY  = rBCDDAY  & ~(0x7)  | sday;           //SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
  195.     rBCDDATE = rBCDDATE & ~(0x3f) | sdate;
  196.     rBCDHOUR = rBCDHOUR & ~(0x3f) | shour;
  197.     rBCDMIN  = rBCDMIN  & ~(0x7f) | smin;
  198.     rBCDSEC  = rBCDSEC  & ~(0x7f) | ssec;
  199.     
  200.     rRTCCON  = 0x0;    //No reset, Merge BCD counters, 1/32768, RTC Control disable    
  201. }
  202. //************************[ Test_Rtc_Tick ]****************************
  203. void Test_Rtc_Tick(void)
  204. {
  205.     Uart_Printf("[ RTC Tick interrupt(1 sec) test for S3C2410 ]n");
  206.     Uart_Printf("Press any key to exit.n"); 
  207.     Uart_Printf("n");  
  208.     Uart_Printf("n");      
  209.     Uart_Printf("   ");    
  210.         
  211.     pISR_TICK = (unsigned)Rtc_Tick;
  212.     sec_tick  = 1;
  213.     rINTMSK   = ~(BIT_TICK); 
  214.     rRTCCON   = 0x0;            //No reset[3], Merge BCD counters[2], BCD clock select XTAL[1], RTC Control disable[0]
  215.     rTICNT    = (1<<7) + 127;   //Tick time interrupt enable, Tick time count value 127
  216.                                 //Period = (n + 1) / 128 second    n:Tick time count value(1~127)
  217.     Uart_Getch();
  218.     
  219.     rINTMSK   = BIT_ALLMSK;
  220.     rRTCCON   = 0x0;            //No reset[3], Merge BCD counters[2], BCD clock select XTAL[1], RTC Control disable[0]
  221. }
  222. //-----------------------------------------------------------------------
  223. void __irq EINT0_int(void)
  224. {
  225.     rSRCPND = BIT_EINT0;   
  226.     rINTPND = BIT_EINT0;
  227.     (void)rINTPND;
  228.     rRTCRST = (1<<3) | 3;   //Round second reset enable, over than 30 sec
  229. }
  230. //-----------------------------------------------------------------------
  231. void __irq Rtc_Int(void)
  232. {
  233.     rSRCPND = BIT_RTC;   
  234.     rINTPND = BIT_RTC;
  235.     (void)rINTPND;
  236.     Uart_Printf("RTC Alarm Interrupt O.K.n");
  237.     isRtcInt = 1;  
  238. }
  239. //---------------------------------------------------------------------
  240. void __irq Rtc_Tick(void)
  241. {
  242.     rSRCPND = BIT_TICK;   
  243.     rINTPND = BIT_TICK;
  244.     (void)rINTPND;
  245.     Uart_Printf("bbbbbbb%03d sec",sec_tick++);
  246. }