rtc.c
上传用户:duomei918
上传日期:2022-03-01
资源大小:19k
文件大小:112k
源码类别:

MTK

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*****************************************************************************
  36.  *
  37.  * Filename:
  38.  * ---------
  39.  *    rtc.c
  40.  *
  41.  * Project:
  42.  * --------
  43.  *   Maui_Software
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *   This Module defines the RTC driver.
  48.  *
  49.  * Author:
  50.  * -------
  51.  * -------
  52.  * -------
  53.  *
  54.  *============================================================================
  55.  *             HISTORY
  56.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  57.  *------------------------------------------------------------------------------
  58.  * removed!
  59.  * removed!
  60.  * removed!
  61.  *
  62.  * removed!
  63.  * removed!
  64.  * removed!
  65.  *
  66.  * removed!
  67.  * removed!
  68.  * removed!
  69.  *
  70.  * removed!
  71.  * removed!
  72.  * removed!
  73.  *
  74.  * removed!
  75.  * removed!
  76.  * removed!
  77.  *
  78.  * removed!
  79.  * removed!
  80.  * removed!
  81.  *
  82.  * removed!
  83.  * removed!
  84.  * removed!
  85.  *
  86.  * removed!
  87.  * removed!
  88.  * removed!
  89.  *
  90.  * removed!
  91.  * removed!
  92.  * removed!
  93.  *
  94.  * removed!
  95.  * removed!
  96.  * removed!
  97.  *
  98.  * removed!
  99.  * removed!
  100.  * removed!
  101.  *
  102.  * removed!
  103.  * removed!
  104.  * removed!
  105.  *
  106.  * removed!
  107.  * removed!
  108.  * removed!
  109.  *
  110.  * removed!
  111.  * removed!
  112.  * removed!
  113.  *
  114.  * removed!
  115.  * removed!
  116.  * removed!
  117.  *
  118.  * removed!
  119.  * removed!
  120.  * removed!
  121.  *
  122.  * removed!
  123.  * removed!
  124.  * removed!
  125.  *
  126.  * removed!
  127.  * removed!
  128.  * removed!
  129.  *
  130.  * removed!
  131.  * removed!
  132.  * removed!
  133.  *
  134.  * removed!
  135.  * removed!
  136.  * removed!
  137.  *
  138.  * removed!
  139.  * removed!
  140.  * removed!
  141.  *
  142.  * removed!
  143.  * removed!
  144.  * removed!
  145.  *
  146.  * removed!
  147.  * removed!
  148.  * removed!
  149.  *
  150.  * removed!
  151.  * removed!
  152.  * removed!
  153.  *
  154.  * removed!
  155.  * removed!
  156.  * removed!
  157.  *
  158.  * removed!
  159.  * removed!
  160.  * removed!
  161.  *
  162.  * removed!
  163.  * removed!
  164.  * removed!
  165.  *
  166.  * removed!
  167.  * removed!
  168.  * removed!
  169.  *
  170.  * removed!
  171.  * removed!
  172.  * removed!
  173.  *
  174.  * removed!
  175.  * removed!
  176.  * removed!
  177.  *
  178.  * removed!
  179.  * removed!
  180.  * removed!
  181.  *
  182.  * removed!
  183.  * removed!
  184.  * removed!
  185.  *
  186.  * removed!
  187.  * removed!
  188.  * removed!
  189.  *
  190.  * removed!
  191.  * removed!
  192.  * removed!
  193.  *
  194.  * removed!
  195.  * removed!
  196.  * removed!
  197.  *
  198.  * removed!
  199.  * removed!
  200.  * removed!
  201.  *
  202.  * removed!
  203.  * removed!
  204.  * removed!
  205.  *
  206.  * removed!
  207.  * removed!
  208.  * removed!
  209.  *
  210.  * removed!
  211.  * removed!
  212.  * removed!
  213.  *
  214.  * removed!
  215.  * removed!
  216.  * removed!
  217.  *
  218.  * removed!
  219.  * removed!
  220.  * removed!
  221.  *
  222.  * removed!
  223.  * removed!
  224.  * removed!
  225.  *
  226.  * removed!
  227.  * removed!
  228.  * removed!
  229.  *
  230.  * removed!
  231.  * removed!
  232.  * removed!
  233.  *
  234.  * removed!
  235.  * removed!
  236.  * removed!
  237.  *
  238.  * removed!
  239.  * removed!
  240.  * removed!
  241.  *
  242.  * removed!
  243.  * removed!
  244.  * removed!
  245.  *
  246.  * removed!
  247.  * removed!
  248.  * removed!
  249.  *
  250.  * removed!
  251.  * removed!
  252.  * removed!
  253.  *
  254.  * removed!
  255.  * removed!
  256.  * removed!
  257.  *
  258.  * removed!
  259.  * removed!
  260.  * removed!
  261.  *
  262.  * removed!
  263.  * removed!
  264.  * removed!
  265.  *
  266.  * removed!
  267.  * removed!
  268.  * removed!
  269.  *
  270.  * removed!
  271.  * removed!
  272.  * removed!
  273.  *
  274.  * removed!
  275.  * removed!
  276.  * removed!
  277.  *
  278.  * removed!
  279.  * removed!
  280.  * removed!
  281.  *
  282.  * removed!
  283.  * removed!
  284.  * removed!
  285.  *
  286.  * removed!
  287.  * removed!
  288.  * removed!
  289.  *
  290.  * removed!
  291.  * removed!
  292.  * removed!
  293.  *
  294.  * removed!
  295.  * removed!
  296.  * removed!
  297.  *
  298.  * removed!
  299.  * removed!
  300.  * removed!
  301.  *
  302.  * removed!
  303.  * removed!
  304.  * removed!
  305.  *
  306.  * removed!
  307.  * removed!
  308.  * removed!
  309.  *
  310.  * removed!
  311.  * removed!
  312.  * removed!
  313.  *
  314.  * removed!
  315.  * removed!
  316.  * removed!
  317.  *
  318.  * removed!
  319.  * removed!
  320.  * removed!
  321.  *
  322.  * removed!
  323.  * removed!
  324.  * removed!
  325.  *
  326.  * removed!
  327.  * removed!
  328.  * removed!
  329.  *
  330.  * removed!
  331.  * removed!
  332.  * removed!
  333.  *
  334.  * removed!
  335.  * removed!
  336.  * removed!
  337.  *
  338.  * removed!
  339.  * removed!
  340.  * removed!
  341.  *
  342.  * removed!
  343.  * removed!
  344.  * removed!
  345.  *
  346.  * removed!
  347.  * removed!
  348.  * removed!
  349.  *
  350.  * removed!
  351.  * removed!
  352.  * removed!
  353.  *
  354.  * removed!
  355.  * removed!
  356.  * removed!
  357.  *
  358.  * removed!
  359.  * removed!
  360.  * removed!
  361.  *
  362.  * removed!
  363.  * removed!
  364.  * removed!
  365.  *
  366.  * removed!
  367.  * removed!
  368.  * removed!
  369.  *
  370.  * removed!
  371.  * removed!
  372.  * removed!
  373.  *
  374.  * removed!
  375.  * removed!
  376.  * removed!
  377.  *
  378.  * removed!
  379.  * removed!
  380.  * removed!
  381.  *
  382.  * removed!
  383.  * removed!
  384.  * removed!
  385.  *
  386.  * removed!
  387.  * removed!
  388.  * removed!
  389.  * removed!
  390.  * removed!
  391.  * removed!
  392.  * removed!
  393.  * removed!
  394.  * removed!
  395.  * removed!
  396.  * removed!
  397.  * removed!
  398.  * removed!
  399.  * removed!
  400.  * removed!
  401.  * removed!
  402.  * removed!
  403.  * removed!
  404.  * removed!
  405.  * removed!
  406.  * removed!
  407.  * removed!
  408.  * removed!
  409.  * removed!
  410.  * removed!
  411.  * removed!
  412.  * removed!
  413.  * removed!
  414.  * removed!
  415.  * removed!
  416.  * removed!
  417.  * removed!
  418.  * removed!
  419.  * removed!
  420.  * removed!
  421.  * removed!
  422.  * removed!
  423.  * removed!
  424.  * removed!
  425.  * removed!
  426.  * removed!
  427.  * removed!
  428.  * removed!
  429.  * removed!
  430.  * removed!
  431.  * removed!
  432.  * removed!
  433.  * removed!
  434.  * removed!
  435.  * removed!
  436.  * removed!
  437.  * removed!
  438.  * removed!
  439.  * removed!
  440.  * removed!
  441.  * removed!
  442.  * removed!
  443.  * removed!
  444.  * removed!
  445.  * removed!
  446.  * removed!
  447.  * removed!
  448.  * removed!
  449.  * removed!
  450.  * removed!
  451.  * removed!
  452.  * removed!
  453.  * removed!
  454.  * removed!
  455.  * removed!
  456.  * removed!
  457.  * removed!
  458.  * removed!
  459.  * removed!
  460.  * removed!
  461.  * removed!
  462.  * removed!
  463.  * removed!
  464.  * removed!
  465.  * removed!
  466.  * removed!
  467.  * removed!
  468.  * removed!
  469.  * removed!
  470.  * removed!
  471.  * removed!
  472.  * removed!
  473.  * removed!
  474.  * removed!
  475.  * removed!
  476.  * removed!
  477.  * removed!
  478.  * removed!
  479.  * removed!
  480.  * removed!
  481.  * removed!
  482.  * removed!
  483.  *------------------------------------------------------------------------------
  484.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  485.  *============================================================================
  486.  ****************************************************************************/
  487. #include    "drv_features.h"
  488. #include  "kal_release.h"
  489. #include  "stack_common.h"  
  490. #include  "stack_msgs.h"
  491. #include  "app_ltlcom.h"       /* Task message communiction */
  492. #include  "drv_comm.h"
  493. #include    "drvsignals.h"
  494. #ifndef DRV_RTC_NOT_EXIST
  495. #include  "reg_base.h"
  496. #include  "rtc_hw.h"
  497. #include  "rtc_sw.h"
  498. #include  "intrCtrl.h"
  499. #include    "stack_ltlcom.h"       /*msg_send_ext_queue.....definitions*/
  500. #include    "stack_config.h"        /*MOD_UART1_HISR,MOD_UART2_HISR*/
  501. #include    "drv_hisr.h"
  502. #ifdef __MULTI_BOOT__
  503. #include "kal_release.h"
  504. #include "syscomp_config.h"
  505. #include "multiboot_config.h"
  506. #endif   /*__MULTI_BOOT__*/
  507. #include "bmt.h"
  508. #include    "nvram_struct.h"
  509. #include    "nvram_enums.h"
  510. #include    "nvram_data_items.h"
  511. #include    "nvram.h"
  512. #include    "nvram_interface.h"
  513. #include    "nvram_user_defs.h"
  514. #include    "drv_trc.h"
  515. #if defined(__GPS_SUPPORT__) // Indicate GPS task that the time is modified by upper layer
  516. #include "gps_struct.h"
  517. extern kal_bool gps_task_init_ok(void);
  518. #endif // #if defined(__GPS_SUPPORT__)
  519. //#include    "l1_core_private.h"
  520. /*#define  RTC_Test*/
  521. /* #define RTC_CAL_DEBUG */
  522. /* #define RTC_CAL_DEBUG2 */
  523. #if !defined(IC_BURNIN_TEST) && !defined(IC_MODULE_TEST) && !defined(__MAUI_BASIC__) && !defined(__FUE__) && !defined(DRV_RTC_NO_REG_COMM)
  524. #ifdef DRV_RTC_HW_CALI
  525. #define RTC_HW_CALIBRATION
  526. #else
  527. #define RTC_SW_CALIBRATION
  528. #endif
  529. #endif
  530. // Define to enable sleep workaround debug message
  531. #if defined(DRV_RTC_SLEEP_LOCKED_UP_WA)
  532. #if defined(MTK_SLEEP_ENABLE)
  533. // If the chip has sleep mode locked up issue, and sleep mechanism is enabled, 
  534. // We need to perform enable workaround to avoid locked up in sleep mode while loop
  535. #define SLEEP_WORKAROUND_IN_RTC
  536. // Define to enable debug message of SLEEP_WORKAROUND_IN_RTC
  537. //#define RTC_SLEEP_WA_DEBUG
  538. #endif // #if defined(MTK_SLEEP_ENABLE)
  539. #endif // #if defined(DRV_RTC_SLEEP_LOCKED_UP_WA)
  540. static kal_bool    First_PowerOn=KAL_FALSE;/*use a static variable to recode is this first power on*/
  541. #if !defined(__FUE__)
  542. static rtc_callbac RTC_CALLBAC;
  543. static rtc_module  RTCConfig;
  544. /*for rtc calibration*/
  545. #if defined(RTC_HW_CALIBRATION) || defined(RTC_SW_CALIBRATION)
  546.    #if defined(__USB_ENABLE__) && !defined(__NVRAM_IN_USB_MS__)
  547.    extern kal_bool INT_USBBoot(void);
  548.    #endif /*__USB_ENABLE__*/
  549.    extern kal_uint32 L1I_GetTimeStamp(void);
  550. /*for rtc calibration*/
  551. void rtc_cali_init(void *timer_param);
  552. /* Calibration State */
  553. typedef enum
  554. {
  555.    CAL_STATE_NONE = 0,
  556.    CAL_STATE_INIT,
  557.    CAL_STATE_INIT2,
  558.    CAL_STATE_START,
  559.    CAL_STATE_RESET
  560. } rtc_cali_state;
  561. static kal_uint8 cali_state = (kal_uint8)CAL_STATE_NONE;
  562. static kal_uint8 rtc_tc_int_var = TC_EN_None;
  563. static kal_uint16 tc_min_expire_cnt = 0;
  564. #if defined(RTC_HW_CALIBRATION)
  565. #define RTC_CALI_PERIOD_MINS   (6)  // unit: minutes
  566. #define RTC_CALI_PERIOD_SECS   (RTC_CALI_PERIOD_MINS*60)  // unit: minutes
  567. #define RTC_CALI_REPEAT_DUR_SECS    (8)   // spec: 8 seconds to do one repeate calibration    
  568. #define RTC_CALI_REPEAT_TOTAL_CNT   (RTC_CALI_PERIOD_SECS>>3)
  569. #define RTC_CALI_TICK_VALUE (13000*RTC_CALI_PERIOD_MINS)
  570. #define RTC_STANDARD_FREQ  (32768)
  571. #define RTC_STANDARD_FREQ_8SECS  (RTC_STANDARD_FREQ*8)
  572. #define RTC_CALI_REPEAT_MAX   (63)
  573. #define RTC_CALI_REPEAT_MIN   (-64)
  574. #define RTC_CALI_ONE_TIME_MAX   (2045)
  575. #define RTC_CALI_ONE_TIME_MIN   (-2048)
  576. static kal_uint32 rtc_current_frame_tick;
  577. static kal_uint32 rtc_current_freq_8secs;
  578. static kal_bool one_time_cali = KAL_FALSE;
  579. static kal_int32 inaccurate_total_cnt = 0;
  580. static kal_int32 no_diff_cnt = 0;
  581. #elif defined(RTC_SW_CALIBRATION)
  582. extern BMTStruct BMT;          /*Main BMT struct*/
  583. /* extern NVRAM API */
  584. //extern kal_bool nvram_external_read_data(kal_uint8 LID,kal_uint8 rec_index, kal_uint8* buffer, kal_uint16 buffer_size);
  585. kal_timerid   rtc_timer_id=0;
  586. void rtc_timer(void *timer_param); 
  587. void rtc_get_next_time(void);
  588. void rtc_forward_time(t_rtc *rtc_time, kal_uint16 hours, kal_uint16 mins, kal_uint16 secs);
  589. void rtc_backward_time(t_rtc *rtc_time, kal_uint16 hours, kal_uint16 mins, kal_uint16 secs);
  590. void rtc_write_to_nvram(void);
  591. #if defined(SLEEP_WORKAROUND_IN_RTC)
  592. // The tick to trigger rtc_sleep_wa_timer_cb to disable sleep mode
  593. // We may delayed waked up at 59:04, set kal timer to wake up after 52 sec
  594. // We may keep in sleep-disabled mode at least for 4 sec per hour
  595. #define RTC_SLEEP_WA_DISABLE_SLEEP_TICK 11266  // ~ 52 sec, (13000/60) * 52 
  596. kal_timerid   rtc_sleep_wa_timer_id=0;
  597. void rtc_sleep_wa_timer_cb(void *timer_param);
  598. void rtc_sleep_wa_enable_sleep_mode(void);
  599. void rtc_sleep_wa_disable_sleep_mode(void);
  600. kal_uint8 rtc_sleep_wa_handle;
  601. volatile kal_bool rtc_sleep_wa_disable_sleep_performed = KAL_FALSE; // Init value: disable sleep NOT performed
  602. // static t_rtc rtc_sleep_wa_ref;
  603. #endif // #ifdef SLEEP_WORKAROUND_IN_RTC
  604. #define rtc_cali_hr_period (1)/*hour*/ 
  605. #define rtc_cali_min_period (2)/*minute*/ 
  606. #define rtc_cali_tick_value (13000*60*rtc_cali_hr_period + 13000*rtc_cali_min_period)
  607. #define rtc_cali_tick_safe_margin   13000 /* ticks */
  608. #define rtc_tick_update_interval (217)/*1300/6 = 216 and 2/3*/
  609. #define rtc_sec_update_interval (1)
  610. static const kal_uint8 days_in_month[13] = 
  611. {
  612.    0, /*Null*/ 
  613.    31, /*Jan*/
  614.    28, /*Feb*/
  615.    31, /*Mar*/
  616.    30, /*Apr*/
  617.    31, /*May*/
  618.    30, /*Jun*/
  619.    31, /*Jul*/
  620.    31, /*Aug*/
  621.    30, /*Sep*/
  622.    31, /*Oct*/
  623.    30, /*Nov*/
  624.    31  /*Dec*/
  625. };
  626. static rtc_nvram_data  rtc_nvram_s;
  627. static t_rtc next_current_time;
  628. static kal_uint32 next_frame_tick, tick_left=0;
  629. static kal_uint16 one_third_tick_count = 0; /* 217 - 216and2/3 = 1/3. The counts for the 1/3. */
  630. static kal_uint8 rtc_al_int_var = AL_EN_None;
  631. static kal_int32 pwroff_inacc_ticks = 0;
  632. static kal_uint32 frame_tick1;
  633. static kal_uint32 frame_tick2;
  634. static kal_int32 frame_tick_1hr_diff;
  635. #endif /* RTC_SW_CALIBRATION */
  636. #endif /*  defined(RTC_HW_CALIBRATION) || defined(RTC_SW_CALIBRATION) */
  637. //extern void dbg_print(char *fmt,...);
  638. #ifdef RTC_Test
  639. void RTC_delay(void)
  640. {
  641. kal_uint16 i;
  642. for (i=0;i<2000;i++);
  643. }
  644. #endif
  645. void RTC_setBBPU(kal_uint16 data)
  646. {
  647.    data &= 0x00ff;
  648.    data |= 0x4300;
  649.    #if defined(DRV_RTC_WRITE_ENABLE)
  650.    data |= RTC_BBPU_WRITE_EN;
  651.    #endif // #if defined(DRV_RTC_WRITE_ENABLE)
  652.    DRV_WriteReg(RTC_BBPU,data);
  653. }
  654. #endif //#if !defined(__FUE__)
  655. #if defined(DRV_RTC_HW_CALI)
  656. /*
  657. * FUNCTION                                                            
  658. * RTC_write_trigger
  659. *
  660. * DESCRIPTION                                                           
  661. *    This function is to enable the transfers from core to RTC.
  662. *
  663. * CALLS  
  664. * It is called to trigger RTC register writing.
  665. *
  666. * PARAMETERS
  667. * None
  668. *
  669. * RETURNS
  670. * None
  671. *
  672. * GLOBALS AFFECTED
  673. *   external_global
  674. */
  675. void RTC_write_trigger(void)
  676. {
  677.    DRV_WriteReg(RTC_WRTGR, RTC_WRTGR_WRTGR);
  678. }
  679. /*
  680. * FUNCTION                                                            
  681. * RTC_write_trigger
  682. *
  683. * DESCRIPTION                                                           
  684. *    This function is to enable the transfers from core to RTC and wait till it's done.
  685. *
  686. * CALLS  
  687. * It is called to trigger RTC register writing.
  688. *
  689. * PARAMETERS
  690. * None
  691. *
  692. * RETURNS
  693. * None
  694. *
  695. * GLOBALS AFFECTED
  696. *   external_global
  697. */
  698. void RTC_write_trigger_wait(void)
  699. {
  700.    kal_uint32 count=0;
  701.    
  702.    DRV_WriteReg(RTC_WRTGR, RTC_WRTGR_WRTGR);
  703.    //while(count < 10000)
  704.    while(count < 0xffffffff)
  705.    {
  706.       if ((RTC_BBPU_CBUSY & DRV_Reg(RTC_BBPU))==0)
  707.          break;
  708.       count++;
  709.    }
  710. }
  711. #endif //#if defined(DRV_RTC_HW_CALI)
  712. #if !defined(__FUE__)
  713. /*
  714. * FUNCTION                                                            
  715. * isPWROnByRTC
  716. *
  717. * DESCRIPTION                                                           
  718. *    This function is to check whether "power on" is due to RTC.
  719. *
  720. * CALLS  
  721. * It is called to check whether "power on" is due to RTC.
  722. *
  723. * PARAMETERS
  724. * None
  725. *
  726. * RETURNS
  727. * KAL_TRUE: "Power on" is due to RTC.
  728. * KAL_FALSE: "Power on" is not due to RTC.
  729. *
  730. * GLOBALS AFFECTED
  731. *   external_global
  732. */
  733. kal_bool isPWROnByRTC(void)
  734. {
  735. #if defined(DRV_RTC_PWRON_BBPU_POLARITY) || defined(FPGA)
  736. kal_uint16 BBPU;
  737. kal_uint16 POLAR;
  738. kal_uint16 PWRSW;
  739. BBPU = DRV_Reg(RTC_BBPU);
  740. POLAR = BBPU & RTC_BBPU_PWR_POLARITY;
  741. POLAR >>= 1;
  742. PWRSW = BBPU & RTC_BBPU_PWR_SW;
  743. PWRSW >>= 4;
  744. if (POLAR == PWRSW)
  745. {
  746. return KAL_TRUE;
  747. }
  748. else
  749. {
  750. return KAL_FALSE;
  751. }
  752. #endif   /*DRV_RTC_PWRON_BBPU_POLARITY,FPGA*/
  753. #if defined(DRV_RTC_PWRON_BBPU_SW)
  754.    if (DRV_Reg(RTC_BBPU) & RTC_BBPU_PWR_SW)
  755.       return KAL_TRUE;
  756.    else
  757.       return KAL_FALSE;
  758. #endif   /*DRV_RTC_PWRON_BBPU_SW*/
  759. }
  760. //=================================================================
  761. /*
  762. * FUNCTION                                                            
  763. * RTC_IRQOneShotEnable
  764. *
  765. * DESCRIPTION                                                           
  766. *    This function is to setup that RTC interrupt is at oneshot mode
  767. * or at automatic reset mode. 
  768. *
  769. * CALLS  
  770. * It is called to setup that RTC interrupt is at oneshot mode
  771. * or at automatic reset mode. 
  772. *
  773. * PARAMETERS
  774. * oneShotEN:
  775. *     ONE_SHOT_DIS, automatic reset mode
  776. *     ONE_SHOT_EN, one shot mode
  777. *
  778. * RETURNS
  779. * None
  780. *
  781. * GLOBALS AFFECTED
  782. *   external_global
  783. */
  784. void RTC_IRQOneShotEnable(kal_uint8 oneShotEN)
  785. {
  786. if (oneShotEN == ONE_SHOT_DIS)
  787. {
  788. DRV_Reg(RTC_IRQ_EN) &= ~RTC_IRQ_EN_ONESHOT;
  789. }
  790. else
  791. {
  792. DRV_Reg(RTC_IRQ_EN) |= RTC_IRQ_EN_ONESHOT;
  793. }
  794. #if defined(DRV_RTC_HW_CALI)
  795.    RTC_write_trigger_wait();
  796. #endif
  797. }
  798. //=================================================================
  799. /*
  800. * FUNCTION                                                            
  801. * RTC_Config
  802. *
  803. * DESCRIPTION                                                           
  804. *    Configure the RTC interrupt 
  805. *
  806. * CALLS  
  807. * This function is called to enable RTC interrupt
  808. *
  809. * PARAMETERS
  810. * AL_EN:
  811. * AL_EN_None: Alarm interrupt is disable.
  812. * AL_EN_bySEC: Alarm interrupt is issued by second.
  813. * AL_EN_byMIN: Alarm interrupt is issued by minute.
  814. * TC_EN:
  815. * TC_EN_None: TC interrupt is disable
  816. * TC_EN_bySEC: TC interrupt is issued by second.
  817. * TC_EN_byMIN: TC interrupt is issued by minute.
  818. *
  819. * RETURNS
  820. * None
  821. *
  822. * GLOBALS AFFECTED
  823. *   external_global
  824. */
  825. void RTC_Config(kal_uint8 AL_EN,kal_uint8 TC_EN)
  826. {
  827. kal_uint16 tmp;
  828. kal_uint16 BBPU;
  829.    kal_uint32 savedMask;
  830.    #if defined(DRV_RTC_HW_CALI)
  831.    RTC_wait_busy();
  832.    #endif   
  833.    
  834.    #if defined(DRV_RTC_W_FLAG)
  835.    while (DRV_Reg(RTC_IRQ_EN) & 0x8000){;};
  836.    #endif // #if defined(DRV_RTC_W_FLAG)
  837.    savedMask = SaveAndSetIRQMask();   
  838. tmp = DRV_Reg(RTC_IRQ_EN);
  839. tmp &=~RTC_IRQ_EN_ONESHOT;
  840. BBPU = DRV_Reg(RTC_BBPU);
  841. #if defined(RTC_HW_CALIBRATION)
  842.    /* Save user's configuration for Alarm and Time Clock settings. */
  843.    if (TC_EN != TC_EN_NoChange)
  844.       rtc_tc_int_var = TC_EN;
  845.    if (cali_state == CAL_STATE_INIT)
  846.    {
  847.       /* In the calibration INIT state, we need "minute" interrupt. */
  848.       if ((TC_EN != TC_EN_ChkS) && (TC_EN != TC_EN_ChkM))
  849.          TC_EN = TC_EN_NoChange;
  850.    }
  851.    else if (cali_state == (kal_uint8)CAL_STATE_START)
  852.    {
  853.       if (one_time_cali == KAL_TRUE)
  854.       {
  855.          /* We need "seconds" interrupt. */
  856.          if (TC_EN != TC_EN_ChkS)
  857.             TC_EN = TC_EN_NoChange;
  858.       }
  859.       else
  860.       {
  861.          /* We need "minute" interrupt. */
  862.          if ((TC_EN != TC_EN_ChkS) && (TC_EN != TC_EN_ChkM))
  863.             TC_EN = TC_EN_NoChange;
  864.       }
  865.    }
  866. #elif defined(RTC_SW_CALIBRATION)
  867.    /* Save user's configuration for Alarm and Time Clock settings. */
  868.    if (AL_EN != AL_EN_NoChange)
  869.       rtc_al_int_var = AL_EN;
  870.    if (TC_EN != TC_EN_NoChange)
  871.       rtc_tc_int_var = TC_EN;
  872.    if (cali_state == (kal_uint8)CAL_STATE_INIT)
  873.    {
  874.       /* In the calibration INIT state, we need "second" interrupt. */
  875.       if (TC_EN != TC_EN_ChkS)
  876.          TC_EN = TC_EN_NoChange;
  877.    }
  878.    else if (cali_state == (kal_uint8)CAL_STATE_START)
  879.    {
  880.       /* In the calibration START state, we need "minute" interrupt. */
  881.       if ((TC_EN != TC_EN_ChkS) && (TC_EN != TC_EN_ChkM))
  882.          TC_EN = TC_EN_NoChange;
  883.    }
  884. #endif /* RTC_SW_CALIBRATION */
  885. switch(AL_EN)
  886. {
  887. case AL_EN_None:
  888. tmp &= ~RTC_IRQ_EN_AL;  /*RTC_ALIRQEnable(KAL_FALSE);*/
  889. BBPU &=~(RTC_BBPU_ALARM_SW);/*0227 TY adds*/
  890. break;
  891. case AL_EN_ChkS:
  892. tmp |= RTC_IRQ_EN_AL;
  893. DRV_WriteReg(RTC_AL_MASK,(~RTC_AL_MASK_SEC));
  894. BBPU |=(RTC_BBPU_ALARM_SW);/*0227 TY adds*/
  895. break;
  896. case AL_EN_ChkMS:
  897. tmp |= RTC_IRQ_EN_AL;
  898. DRV_WriteReg(RTC_AL_MASK,(~RTC_AL_MASK_MIN));
  899. BBPU |=(RTC_BBPU_ALARM_SW);/*0227 TY adds*/
  900. break;
  901.       
  902.       case AL_EN_ChkHMS:
  903.          tmp |= RTC_IRQ_EN_AL;
  904.          DRV_WriteReg(RTC_AL_MASK,(~RTC_AL_MASK_HOU));
  905.          BBPU |=(RTC_BBPU_ALARM_SW);/*0227 TY adds*/
  906. break;
  907.    
  908.    case AL_EN_ChkDHMS:
  909.       tmp |= RTC_IRQ_EN_AL;
  910.       DRV_WriteReg(RTC_AL_MASK,(~RTC_AL_MASK_DOM));
  911.       BBPU |=(RTC_BBPU_ALARM_SW);/*0227 TY adds*/
  912. break;
  913. case AL_EN_ChkWHMS:
  914.    tmp |= RTC_IRQ_EN_AL;
  915.    DRV_WriteReg(RTC_AL_MASK,(~RTC_AL_MASK_DOW));
  916.    BBPU |=(RTC_BBPU_ALARM_SW);/*0227 TY adds*/
  917. break;
  918.    case AL_EN_ChkMDHMS:
  919.       tmp |= RTC_IRQ_EN_AL;
  920.       DRV_WriteReg(RTC_AL_MASK,(~RTC_AL_MASK_MTH));
  921.       BBPU |=(RTC_BBPU_ALARM_SW);/*0227 TY adds*/
  922. break;
  923.    case AL_EN_ChkYMDHMS:
  924.       tmp |= RTC_IRQ_EN_AL;
  925.       DRV_WriteReg(RTC_AL_MASK,(~RTC_AL_MASK_YEA));
  926.       BBPU |=(RTC_BBPU_ALARM_SW);/*0227 TY adds*/
  927. break;
  928.    case AL_EN_NoChange:
  929.       break;
  930. default:
  931. break;
  932. }
  933. switch(TC_EN)
  934. {
  935. case TC_EN_None:
  936. tmp &= ~RTC_IRQ_EN_TC;
  937. break;
  938. case TC_EN_ChkS:
  939. tmp |= RTC_IRQ_EN_TC;
  940. DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_SEC);
  941. break;
  942. case TC_EN_ChkM:
  943. tmp |= RTC_IRQ_EN_TC;
  944. DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_MIN);
  945. break;
  946.    case TC_EN_ChkH:
  947.       tmp |= RTC_IRQ_EN_TC;
  948.       DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_HOU);
  949. break;
  950. case TC_EN_ChkD:
  951.       tmp |= RTC_IRQ_EN_TC;
  952.       DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_DOM);
  953. break;
  954. case TC_EN_ChkW:
  955.       tmp |= RTC_IRQ_EN_TC;
  956.       DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_DOW);
  957. break;
  958. case TC_EN_ChkMon:
  959.       tmp |= RTC_IRQ_EN_TC;
  960.       DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_MTH);
  961. break;
  962. case TC_EN_ChkY:
  963.       tmp |= RTC_IRQ_EN_TC;
  964.       DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_YEA);
  965. break;
  966.     
  967.       case TC_EN_NoChange:
  968.          break;
  969.          
  970. default:
  971. break;
  972. }
  973. DRV_WriteReg(RTC_IRQ_EN,tmp);
  974. #if defined(DRV_RTC_WRITE_ENABLE)
  975. BBPU |= RTC_BBPU_WRITE_EN;
  976. #endif // #if defined(DRV_RTC_WRITE_ENABLE)
  977. DRV_WriteReg(RTC_BBPU,(BBPU|RTC_BBPU_KEY));/*0227 TY adds*/
  978. #if defined(DRV_RTC_HW_CALI)
  979.    DRV_WriteReg(RTC_WRTGR, RTC_WRTGR_WRTGR);
  980.    //RTC_write_trigger();
  981. #endif /* DRV_RTC_HW_CALI */
  982. RestoreIRQMask(savedMask);
  983. #if defined(DRV_RTC_HW_CALI)
  984. RTC_wait_busy();
  985. #endif /* DRV_RTC_HW_CALI */
  986. }
  987. #endif /*!defined(__FUE__)*/
  988. #if ( defined(__GPS_SUPPORT__) || defined(RTC_HW_CALIBRATION) )
  989. extern kal_bool kal_query_systemInit(void);
  990. // Return KAL_TRUE: OS is running, we can send ilm at this moment
  991. // Return KAL_FALSE: OS is NOT running, we are called at boot time phase
  992. kal_bool IsOSRunning(void){
  993. // kal_query_systemInit() return KAL_TRUE: System is at init phase
  994. // kal_query_systemInit() return KAL_FALSE: System is NOT at init phase
  995. return (kal_bool)(!kal_query_systemInit());
  996. }
  997. #endif //#if ( defined(__GPS_SUPPORT__) || defined(RTC_HW_CALIBRATION) )
  998. /*
  999. * FUNCTION                                                            
  1000. * RTC_InitTC_Time
  1001. *
  1002. * DESCRIPTION                                                           
  1003. *    Setup the current time
  1004. *
  1005. * CALLS  
  1006. * This function is called to setup the current time
  1007. *
  1008. * PARAMETERS
  1009. * rtctime: the current time.
  1010. *
  1011. * RETURNS
  1012. * None
  1013. *
  1014. * GLOBALS AFFECTED
  1015. *   external_global
  1016. */
  1017. void RTC_InitTC_Time(t_rtc *rtctime) //validate
  1018. {
  1019.    kal_uint32 savedMask=0;
  1020. #if defined(__GPS_SUPPORT__)
  1021. ilm_struct *RTC_ilm;
  1022. rtc_gps_time_change_ind_struct *rtc_ind = NULL;
  1023. #endif //#if defined(__GPS_SUPPORT__)
  1024. #if !defined(DRV_RTC_HW_CALI)
  1025.    kal_uint32 loop=0;
  1026. #endif /* DRV_RTC_HW_CALI */
  1027.    
  1028. #if defined(DRV_RTC_HW_CALI) && !defined(__FUE__)
  1029. RTC_wait_busy();
  1030. #endif   
  1031.    
  1032. #if defined(RTC_SW_CALIBRATION)
  1033.    if(rtc_timer_id!=0)
  1034.    {
  1035.       cali_state = (kal_uint8)CAL_STATE_RESET;
  1036.     kal_cancel_timer(rtc_timer_id);
  1037.    }
  1038. #endif /* RTC_SW_CALIBRATION */
  1039. #if defined(__GPS_SUPPORT__) // Indicate GPS task that the time is modified by upper layer
  1040. if (IsOSRunning()){ // not use gps_task_init_ok, otherwise may encounter gps init later, but not set rtc_ind here.
  1041. rtc_ind = (rtc_gps_time_change_ind_struct*)
  1042. construct_local_para(sizeof(rtc_gps_time_change_ind_struct), TD_CTRL);
  1043. // Save current time to message
  1044. rtc_ind->previous_rtc.rtc_sec = DRV_Reg(RTC_TC_SEC);
  1045. rtc_ind->previous_rtc.rtc_min = DRV_Reg(RTC_TC_MIN);
  1046. rtc_ind->previous_rtc.rtc_hour = DRV_Reg(RTC_TC_HOU);
  1047. rtc_ind->previous_rtc.rtc_day = DRV_Reg(RTC_TC_DOM);
  1048. rtc_ind->previous_rtc.rtc_mon = DRV_Reg(RTC_TC_MTH);
  1049. rtc_ind->previous_rtc.rtc_year = DRV_Reg(RTC_TC_YEA);
  1050. }
  1051. #endif //#if defined(__GPS_SUPPORT__)
  1052.    #if defined(DRV_RTC_HW_CALI) && !defined(__FUE__)
  1053.    RTC_wait_busy();
  1054.    #endif
  1055. #if !defined(__FUE__)
  1056.    savedMask = SaveAndSetIRQMask();   
  1057. #endif /*!defined(__FUE__)*/
  1058. /*update time*/
  1059. DRV_WriteReg(RTC_TC_SEC,rtctime->rtc_sec);
  1060. DRV_WriteReg(RTC_TC_MIN,rtctime->rtc_min);
  1061. DRV_WriteReg(RTC_TC_HOU,rtctime->rtc_hour);
  1062. DRV_WriteReg(RTC_TC_DOM,rtctime->rtc_day);
  1063. /*DRV_WriteReg(RTC_TC_DOW,rtctime->rtc_wday);*/
  1064. DRV_WriteReg(RTC_TC_MTH,rtctime->rtc_mon);
  1065. DRV_WriteReg(RTC_TC_YEA,rtctime->rtc_year);
  1066. #if !defined(__FUE__)
  1067. RestoreIRQMask(savedMask);
  1068. #endif /*!defined(__FUE__)*/
  1069. #if defined(DRV_RTC_HW_CALI)
  1070.    RTC_write_trigger_wait();
  1071. #else /* DRV_RTC_HW_CALI */
  1072. //for(loop=0;loop<1000;loop++){};
  1073. // Use while loop with fields check instead of for loop
  1074. // We compare each field until all the fields are what we set to RTC module
  1075. loop = 0;
  1076. while(1){
  1077. if ( 
  1078. (DRV_Reg(RTC_TC_SEC) == rtctime->rtc_sec) &&
  1079. (DRV_Reg(RTC_TC_MIN) == rtctime->rtc_min) &&
  1080. (DRV_Reg(RTC_TC_HOU) == rtctime->rtc_hour) &&
  1081. (DRV_Reg(RTC_TC_DOM) == rtctime->rtc_day) &&
  1082. (DRV_Reg(RTC_TC_MTH) == rtctime->rtc_mon) &&
  1083. (DRV_Reg(RTC_TC_YEA) == rtctime->rtc_year) ){
  1084. break;
  1085. }
  1086. loop ++;
  1087. if (loop >= 10000){
  1088. // Warning: RTC may NOT complet the update
  1089. break;
  1090. }
  1091. }
  1092. #if !defined(LOW_COST_SUPPORT)
  1093. #if !defined(__FUE__)
  1094.   drv_trace8(TRACE_GROUP_9, RTC_INIT_TC_TIME, rtctime->rtc_year, rtctime->rtc_mon,
  1095.             rtctime->rtc_day, rtctime->rtc_hour, rtctime->rtc_min, rtctime->rtc_sec, 0, 0);
  1096. #endif
  1097. #endif
  1098. #endif /* DRV_RTC_HW_CALI */
  1099. #if defined(__GPS_SUPPORT__) // Indicate GPS task that the time is modified by upper layer
  1100. if (IsOSRunning()){ // use double check to prevent call gps_task_init_ok() before 2nd MAUI loaded.
  1101.    if (gps_task_init_ok()){    // use gps task init ok to verify is gps already init, otherwise in factory mode or charging mode has no gps task
  1102.    // Save new RTC time to message
  1103.   module_type module_id;
  1104.    rtc_ind->new_rtc.rtc_sec = DRV_Reg(RTC_TC_SEC);
  1105.    rtc_ind->new_rtc.rtc_min = DRV_Reg(RTC_TC_MIN);
  1106.   rtc_ind->new_rtc.rtc_hour = DRV_Reg(RTC_TC_HOU);
  1107.    rtc_ind->new_rtc.rtc_day = DRV_Reg(RTC_TC_DOM);
  1108.    rtc_ind->new_rtc.rtc_mon = DRV_Reg(RTC_TC_MTH);
  1109.   rtc_ind->new_rtc.rtc_year = DRV_Reg(RTC_TC_YEA);
  1110.    if(KAL_TRUE == kal_if_hisr())
  1111.    {
  1112.    module_id = MOD_DRV_HISR;
  1113.    }
  1114.    else
  1115.   {
  1116.    module_id = stack_get_active_module_id();
  1117.    }
  1118.   RTC_ilm = allocate_ilm(module_id);
  1119.    RTC_ilm->src_mod_id = module_id;
  1120.    RTC_ilm->dest_mod_id = MOD_GPS;
  1121.   RTC_ilm->sap_id = RTC_GPS_SAP;
  1122.    RTC_ilm->msg_id = MSG_ID_RTC_GPS_TIME_CHANGE_IND;
  1123.    RTC_ilm->local_para_ptr = (local_para_struct *)rtc_ind;
  1124.   RTC_ilm->peer_buff_ptr = NULL;
  1125.    msg_send_ext_queue(RTC_ilm);
  1126.    }
  1127. }
  1128. #endif //#if defined(__GPS_SUPPORT__)
  1129. #if defined(RTC_HW_CALIBRATION)
  1130. // For H/W calibration case, we only enable calibration process when OS is running case
  1131. // Or the calibration process may modify RTC register values to unexpected value during boot time
  1132. if (IsOSRunning()){
  1133. cali_state = (kal_uint8)CAL_STATE_NONE;
  1134. rtc_cali_init(0);
  1135. }
  1136. #elif defined(RTC_SW_CALIBRATION)
  1137. /*reset calibration, start after rtc initialization*/
  1138.    if(rtc_timer_id!=0)
  1139.    {
  1140.       next_current_time = *rtctime;
  1141.     rtc_cali_init((void*)rtc_timer_id);
  1142.      /* save current time to nvram */
  1143.       rtc_nvram_s.time_valid = KAL_TRUE;
  1144.       rtc_nvram_s.rtc_time = next_current_time;
  1145.       rtc_write_to_nvram();
  1146.    }
  1147. #endif /* RTC_SW_CALIBRATION */
  1148. #ifdef RTC_Test
  1149. RTC_delay();
  1150. #endif
  1151. }
  1152. #if defined(RTC_SW_CALIBRATION)
  1153. /*
  1154. * FUNCTION                                                            
  1155. * RTC_Cali_Time
  1156. *
  1157. * DESCRIPTION                                                           
  1158. *    update the current time
  1159. *
  1160. * CALLS  
  1161. * This function is to update the current time
  1162. *
  1163. * PARAMETERS
  1164. * rtctime: the current time.
  1165. *
  1166. * RETURNS
  1167. * None
  1168. *
  1169. * GLOBALS AFFECTED
  1170. *   external_global
  1171. */
  1172. void RTC_Cali_Time(t_rtc *rtctime) //validate
  1173. {   
  1174.    /*update time*/
  1175.    DRV_WriteReg(RTC_TC_SEC,rtctime->rtc_sec);
  1176. DRV_WriteReg(RTC_TC_MIN,rtctime->rtc_min);
  1177. DRV_WriteReg(RTC_TC_HOU,rtctime->rtc_hour);
  1178. DRV_WriteReg(RTC_TC_DOM,rtctime->rtc_day);
  1179. DRV_WriteReg(RTC_TC_MTH,rtctime->rtc_mon);
  1180. DRV_WriteReg(RTC_TC_YEA,rtctime->rtc_year);
  1181. #if !defined(LOW_COST_SUPPORT)
  1182. #if !defined(__FUE__)
  1183.   drv_trace8(TRACE_GROUP_9, RTC_CALI_TIME, rtctime->rtc_year, rtctime->rtc_mon,
  1184.             rtctime->rtc_day, rtctime->rtc_hour, rtctime->rtc_min, rtctime->rtc_sec, 0, 0);
  1185. #endif
  1186. #endif
  1187. }
  1188. /*
  1189. * FUNCTION                                                            
  1190. * RTC_Check_Alarm
  1191. *
  1192. * DESCRIPTION                                                           
  1193. *    check if alarm expires after calibrating time
  1194. *
  1195. * CALLS  
  1196. * This function is to check if alarm expires after calibrating time
  1197. *
  1198. * PARAMETERS
  1199. * rtctime: the current time.
  1200. *
  1201. * RETURNS
  1202. * None
  1203. *
  1204. * GLOBALS AFFECTED
  1205. *   external_global
  1206. */
  1207. void RTC_Check_Alarm(t_rtc rtctime) //validate
  1208. {   
  1209.    kal_uint8 irq=0;
  1210.    t_rtc alarm_time, current_time;
  1211.    kal_uint32 alarm_sec=0, current_sec=0, new_time=0;
  1212.    irq = DRV_Reg(RTC_IRQ_EN);
  1213.    /*configure alarm or not*/
  1214.    if(irq&RTC_IRQ_EN_AL)
  1215.    {
  1216.       RTC_GetALTime(&alarm_time);
  1217.       RTC_GetTime(&current_time);
  1218.       if(alarm_time.rtc_hour==0)
  1219.          alarm_sec=(kal_uint32)(24*3600+alarm_time.rtc_min*60+alarm_time.rtc_sec);
  1220.       else
  1221.          alarm_sec=(kal_uint32)(alarm_time.rtc_hour*3600+alarm_time.rtc_min*60+alarm_time.rtc_sec);
  1222.       if(current_time.rtc_hour==0)
  1223.          current_sec=(kal_uint32)(24*3600+current_time.rtc_min*60+current_time.rtc_sec);
  1224.       else
  1225.          current_sec=(kal_uint32)(current_time.rtc_hour*3600+current_time.rtc_min*60+current_time.rtc_sec);   
  1226.       /*exceed alarm or not*/         
  1227.       if(current_sec<alarm_sec)            
  1228.       {
  1229.          if(rtctime.rtc_hour==0)
  1230.             new_time=(kal_uint32)(24*3600+rtctime.rtc_min*60+rtctime.rtc_sec);
  1231.          else
  1232.             new_time=(kal_uint32)(rtctime.rtc_hour*3600+rtctime.rtc_min*60+rtctime.rtc_sec);   
  1233.          /*change alarm or not*/   
  1234.          if(new_time>=alarm_sec)               
  1235.          {
  1236.              RTC_SetAlarm(&rtctime);
  1237.          }   
  1238.       }   
  1239.    }    
  1240. }
  1241. #endif /* RTC_SW_CALIBRATION */
  1242. /*
  1243. * FUNCTION                                                            
  1244. * RTC_GetTime
  1245. *
  1246. * DESCRIPTION                                                           
  1247. *    Obtain the current time
  1248. *
  1249. * CALLS  
  1250. * This function is called to obtain the current time
  1251. *
  1252. * PARAMETERS
  1253. * rtctime: the current time.
  1254. *
  1255. * RETURNS
  1256. * None
  1257. *
  1258. * GLOBALS AFFECTED
  1259. *   external_global
  1260. */
  1261. void RTC_GetTime(t_rtc *rtctime ) //validate
  1262. {
  1263. #if !defined(__FUE__)
  1264.    kal_uint32 savedMask;
  1265. #if defined(DRV_RTC_HW_CALI)
  1266. RTC_wait_busy();
  1267. #endif   
  1268.    savedMask = SaveAndSetIRQMask();   
  1269. #endif /* !defined(__FUE__) */
  1270. rtctime->rtc_sec = DRV_Reg(RTC_TC_SEC);
  1271. rtctime->rtc_min = DRV_Reg(RTC_TC_MIN);
  1272. rtctime->rtc_hour = DRV_Reg(RTC_TC_HOU);
  1273. rtctime->rtc_day = DRV_Reg(RTC_TC_DOM);
  1274. rtctime->rtc_mon = DRV_Reg(RTC_TC_MTH);
  1275. rtctime->rtc_wday = DRV_Reg(RTC_TC_DOW);
  1276. rtctime->rtc_year = DRV_Reg(RTC_TC_YEA);
  1277. #if !defined(__FUE__)
  1278. RestoreIRQMask(savedMask);
  1279. #ifdef RTC_Test
  1280. RTC_delay();
  1281. #endif
  1282. #endif /* !defined(__FUE__) */
  1283.    // check the RTC validation, 
  1284. #if ( defined(__GPS_SUPPORT__) || defined(RTC_HW_CALIBRATION) )
  1285.    if((rtctime->rtc_sec>59) || 
  1286.       (rtctime->rtc_min>59) || 
  1287.       (rtctime->rtc_hour>23)|| 
  1288.       (rtctime->rtc_day>31) || (rtctime->rtc_day == 0) ||
  1289.       (rtctime->rtc_wday>7) || (rtctime->rtc_wday == 0) ||
  1290.       (rtctime->rtc_mon>12) || (rtctime->rtc_mon == 0)  ||
  1291.       (rtctime->rtc_year>127))
  1292.    {   
  1293.       if( IsOSRunning() && (RTC_isFisrtOn() == KAL_FALSE))
  1294.       {
  1295. #if defined(__PRODUCTION_RELEASE__)
  1296.          rtctime->rtc_sec=0; 
  1297.          rtctime->rtc_min=0;
  1298.          rtctime->rtc_hour=0;
  1299.          rtctime->rtc_day=1;
  1300.          rtctime->rtc_wday=1;  //Monday
  1301.          rtctime->rtc_mon=1;   //January
  1302.          rtctime->rtc_year = 4; //2004
  1303.          RTC_InitTC_Time(rtctime);
  1304. #else //#if defined(__PRODUCTION_RELEASE__)
  1305.          ASSERT(0);
  1306. #endif //#if defined(__PRODUCTION_RELEASE__)
  1307.       }
  1308.    }
  1309. #endif //#if ( defined(__GPS_SUPPORT__) || defined(RTC_HW_CALIBRATION) )
  1310. }
  1311. /*
  1312. * FUNCTION                                                            
  1313. * RTC_SetAlarm
  1314. *
  1315. * DESCRIPTION                                                           
  1316. *    Setup the alarm time
  1317. *
  1318. * CALLS  
  1319. * This function is called to setup the alarm time
  1320. *
  1321. * PARAMETERS
  1322. * rtctime: the expected alarm time.
  1323. *
  1324. * RETURNS
  1325. * None
  1326. *
  1327. * GLOBALS AFFECTED
  1328. *   external_global
  1329. */
  1330. void RTC_SetAlarm(t_rtc *rtctime ) //validate 
  1331. {
  1332. kal_uint32 loop;
  1333. #if !defined(__FUE__)
  1334.    //kal_uint32 savedMask=0;
  1335.    //savedMask = SaveAndSetIRQMask();
  1336. #endif /* !defined(__FUE__) */
  1337.    #if defined(DRV_RTC_HW_CALI) && !defined(__FUE__)
  1338.    RTC_wait_busy();
  1339.    #endif
  1340.     /* Add for loop to ensure that RTC alarm time has been written in. */
  1341.    for (loop = 0; loop < 1000000; loop++)
  1342.    {
  1343.       DRV_WriteReg(RTC_AL_SEC,rtctime->rtc_sec);
  1344.    DRV_WriteReg(RTC_AL_MIN,rtctime->rtc_min);
  1345.    DRV_WriteReg(RTC_AL_HOU,rtctime->rtc_hour);
  1346.    DRV_WriteReg(RTC_AL_DOM,rtctime->rtc_day);
  1347.    DRV_WriteReg(RTC_AL_MTH,rtctime->rtc_mon);
  1348.    DRV_WriteReg(RTC_AL_YEA,rtctime->rtc_year);
  1349.       #if defined(DRV_RTC_HW_CALI)
  1350.       RTC_write_trigger();
  1351.       #endif /*DRV_RTC_HW_CALI*/
  1352.       if ((DRV_Reg(RTC_AL_SEC) == rtctime->rtc_sec) && (DRV_Reg(RTC_AL_MIN) == rtctime->rtc_min)
  1353.          && (DRV_Reg(RTC_AL_HOU) == rtctime->rtc_hour) && (DRV_Reg(RTC_AL_DOM) == rtctime->rtc_day)
  1354.          && (DRV_Reg(RTC_AL_MTH) == rtctime->rtc_mon) && (DRV_Reg(RTC_AL_YEA) == rtctime->rtc_year))
  1355.          break;
  1356.    }
  1357. #if !defined(__FUE__)
  1358.    //RestoreIRQMask(savedMask);
  1359. #ifdef RTC_Test
  1360. RTC_delay();
  1361. #endif
  1362. #endif /* !defined(__FUE__) */
  1363. #if !defined(LOW_COST_SUPPORT)
  1364. #if !defined(__FUE__)
  1365.   drv_trace8(TRACE_GROUP_9, RTC_SET_ALARM, rtctime->rtc_year, rtctime->rtc_mon,
  1366.             rtctime->rtc_day, rtctime->rtc_hour, rtctime->rtc_min, rtctime->rtc_sec, 0, 0);
  1367. #endif
  1368. #endif
  1369. }
  1370. /*
  1371. * FUNCTION                                                            
  1372. * RTC_GetALTime
  1373. *
  1374. * DESCRIPTION                                                           
  1375. *    Obtain the alarm time
  1376. *
  1377. * CALLS  
  1378. * This function is called to obtain the alarm time setting
  1379. *
  1380. * PARAMETERS
  1381. * rtctime: the alarm time.
  1382. *
  1383. * RETURNS
  1384. * None
  1385. *
  1386. * GLOBALS AFFECTED
  1387. *   external_global
  1388. */
  1389. void RTC_GetALTime(t_rtc *rtctime ) //validate
  1390. {
  1391. #if defined(DRV_RTC_HW_CALI) && !defined(__FUE__)
  1392. RTC_wait_busy();
  1393. #endif   
  1394. rtctime->rtc_sec = DRV_Reg(RTC_AL_SEC);
  1395. rtctime->rtc_min = DRV_Reg(RTC_AL_MIN);
  1396. rtctime->rtc_hour = DRV_Reg(RTC_AL_HOU);
  1397. rtctime->rtc_day = DRV_Reg(RTC_AL_DOM);
  1398. rtctime->rtc_mon = DRV_Reg(RTC_AL_MTH);
  1399. rtctime->rtc_wday = DRV_Reg(RTC_AL_DOW);
  1400. rtctime->rtc_year = DRV_Reg(RTC_AL_YEA);
  1401. #ifdef RTC_Test
  1402. RTC_delay();
  1403. #endif
  1404. #if !defined(LOW_COST_SUPPORT)
  1405. #if !defined(__FUE__)
  1406.   drv_trace8(TRACE_GROUP_8, RTC_GET_AL_TIME, rtctime->rtc_year, rtctime->rtc_mon,
  1407.             rtctime->rtc_day, rtctime->rtc_hour, rtctime->rtc_min, rtctime->rtc_sec, 0, 0);
  1408. #endif
  1409. #endif
  1410. }
  1411. #if !defined(__FUE__)
  1412. //=================================================================
  1413. /*
  1414. * FUNCTION                                                            
  1415. * RTC_HISR
  1416. *
  1417. * DESCRIPTION                                                           
  1418. *    RTC interrupt handler
  1419. *
  1420. * CALLS  
  1421. * This function is called when RTC intr. is coming
  1422. *
  1423. * PARAMETERS
  1424. * None
  1425. *
  1426. * RETURNS
  1427. * None
  1428. *
  1429. * GLOBALS AFFECTED
  1430. *   external_global
  1431. */
  1432. void RTC_HISR(void)
  1433. {
  1434. kal_uint16 status;
  1435. #if defined(DRV_RTC_PWRON_BBPU_POLARITY)
  1436.    kal_uint16 BBPU;
  1437.    kal_uint16 POLAR;
  1438.    kal_uint16 PWRSW;
  1439.    
  1440.    BBPU = DRV_Reg(RTC_BBPU);
  1441.    POLAR = BBPU & RTC_BBPU_PWR_POLARITY;
  1442.    POLAR >>= 1;
  1443.    PWRSW = BBPU & RTC_BBPU_PWR_SW;
  1444.    #if defined(DRV_RTC_BBPU_AS_6205)
  1445.    PWRSW >>= 2;
  1446.    #else /*!MT6205*/
  1447.    PWRSW >>= 4;
  1448.    #endif   /*MT6205*/
  1449.    if (POLAR != PWRSW)
  1450.    {
  1451.       #ifndef __L1_STANDALONE__
  1452.       IRQMask(IRQ_RTC_CODE);
  1453.       ASSERT(0);
  1454.       return;
  1455.       #endif   /*__L1_STANDALONE__*/
  1456.    }
  1457. #endif   /*DRV_RTC_PWRON_BBPU_POLARITY*/ 
  1458. #if 0 /*remove this part because rtc initialization is before latch power*/
  1459. #if defined(DRV_RTC_PWRON_BBPU_SW)
  1460. /* under construction !*/
  1461. /* under construction !*/
  1462.       #ifndef __L1_STANDALONE__
  1463. /* under construction !*/
  1464. /* under construction !*/
  1465.       #endif   /*__L1_STANDALONE__*/
  1466. /* under construction !*/
  1467. #endif   /*DRV_RTC_PWRON_BBPU_SW*/
  1468. #endif 
  1469.    
  1470.    status = DRV_Reg(RTC_IRQ_STATUS);
  1471. if (status & RTC_IRQ_STATUS_AL_STAT)
  1472. {
  1473. RTC_CALLBAC.rtc_alfunc();
  1474.       #if defined(RTC_AS_MT6218)
  1475. // set AUTO_PDN bit of RTC_BBPU again which is cleared by RTC 
  1476. {
  1477.    kal_uint16 reg;
  1478.    reg = DRV_Reg(RTC_BBPU);
  1479.    reg |= (RTC_BBPU_KEY|RTC_BBPU_AUTOPDN); 
  1480.    DRV_WriteReg(RTC_BBPU, reg );
  1481.          #if defined(DRV_RTC_HW_CALI)
  1482.          RTC_write_trigger();
  1483.          #endif /*DRV_RTC_HW_CALI*/
  1484. }
  1485. #endif
  1486. }
  1487. if (status & RTC_IRQ_STATUS_TC_STAT)
  1488. {
  1489. RTC_CALLBAC.rtc_tcfunc();
  1490.     #if defined(DRV_RTC_HW_CALI)
  1491.     RTC_write_trigger();
  1492.     #endif /*DRV_RTC_HW_CALI*/
  1493. }
  1494. IRQClearInt(IRQ_RTC_CODE);
  1495. IRQUnmask(IRQ_RTC_CODE);
  1496. }
  1497. void RTC_LISR(void)
  1498. {
  1499.    IRQMask(IRQ_RTC_CODE);
  1500.    //kal_activate_hisr(RTCConfig.hisr);
  1501.     drv_active_hisr(DRV_RTC_HISR_ID);
  1502. }
  1503. #endif /*!defined(__FUE__)*/
  1504. /*
  1505. * FUNCTION                                                            
  1506. * RTC_isFisrtOn
  1507. *
  1508. * DESCRIPTION                                                           
  1509. *    Check whether RTC is first on
  1510. *
  1511. * CALLS  
  1512. * This function is called to check whether RTC is first on
  1513. *
  1514. * PARAMETERS
  1515. * None
  1516. *
  1517. * RETURNS
  1518. * KAL_TRUE: RTC is first on
  1519. * KAL_FALSE: RTC is not first on
  1520. *
  1521. * GLOBALS AFFECTED
  1522. *   external_global
  1523. */
  1524. kal_bool RTC_isFisrtOn(void) //validate
  1525. {
  1526. kal_uint16 key1 = DRV_Reg(RTC_POWERKEY1);
  1527. kal_uint16 key2 = DRV_Reg(RTC_POWERKEY2);
  1528. if ((key1 == RTC_POWERKEY1_KEY)&&(key2 == RTC_POWERKEY2_KEY))
  1529. {
  1530.    /*TY adds this 2004/10/21 to record this is first power on*/
  1531.    //First_PowerOn=KAL_FALSE;
  1532. return KAL_FALSE;
  1533. }
  1534. else
  1535. {
  1536.    /*TY adds this 2004/10/21 to record if this is first power on*/
  1537.    First_PowerOn=KAL_TRUE;
  1538. return KAL_TRUE;
  1539. }
  1540. }
  1541. #if !defined(__FUE__)
  1542. /*
  1543. * FUNCTION                                                            
  1544. * RTC_is_MS_FirstPowerOn
  1545. *
  1546. * DESCRIPTION                                                           
  1547. *    Check whether RTC is first power on
  1548. *
  1549. * CALLS  
  1550. * This function is called by MMI. MMI wants to knows this status
  1551. *  whenever he wants but the function "RTC_isFisrtOn" is only valid
  1552. *  before calling DRV_POWERON function
  1553. *
  1554. * PARAMETERS
  1555. * None
  1556. *
  1557. * RETURNS
  1558. * KAL_TRUE: RTC is first on
  1559. * KAL_FALSE: RTC is not first on
  1560. *
  1561. * GLOBALS AFFECTED
  1562. *   external_global
  1563. */
  1564. /*TY adds this 2004/10/21 to record if this is first power on*/    
  1565. kal_bool RTC_is_MS_FirstPowerOn(void) //validate
  1566. {
  1567. if (First_PowerOn==KAL_FALSE)
  1568. {    
  1569. return KAL_FALSE;
  1570. }
  1571. else
  1572. {    
  1573. return KAL_TRUE;
  1574. }
  1575. }
  1576. #endif /*!defined(__FUE__)*/
  1577. /*
  1578. * FUNCTION                                                            
  1579. * RTC_setPWRKey
  1580. *
  1581. * DESCRIPTION                                                           
  1582. *    Setup RTC power key
  1583. *
  1584. * CALLS  
  1585. * This function is called when RTC is first on
  1586. *
  1587. * PARAMETERS
  1588. * None
  1589. *
  1590. * RETURNS
  1591. * None
  1592. *
  1593. * GLOBALS AFFECTED
  1594. *   external_global
  1595. */
  1596. void RTC_setPWRKey(void) //validate
  1597. {
  1598. #if defined(DRV_RTC_HW_CALI) && !defined(__FUE__)
  1599. RTC_wait_busy();
  1600. #endif   
  1601. DRV_WriteReg(RTC_POWERKEY1,RTC_POWERKEY1_KEY);
  1602. DRV_WriteReg(RTC_POWERKEY2,RTC_POWERKEY2_KEY);
  1603. #if defined(DRV_RTC_HW_CALI)
  1604. RTC_write_trigger_wait();
  1605. #endif // #if defined(DRV_RTC_HW_CALI)
  1606. // After PWRKEY is configured to correct setting, set RTC XOSCCALI register
  1607. // This register is to control XOU swing
  1608. // This register is also configured in ADC_init(), but when PWRKEY is NOT matched, 
  1609. // the write operation of XOCCALI register will be failed
  1610. #if defined(DRV_RTC_XOSC_DEF_8)
  1611. DRV_WriteReg(RTC_XOSCCAL,0x8);
  1612. #else // #if defined(DRV_RTC_XOSC_DEF_8)
  1613. DRV_WriteReg(RTC_XOSCCAL,0x1f);
  1614. #endif // #if defined(DRV_RTC_XOSC_DEF_8)
  1615.    
  1616.    
  1617. #if defined(DRV_RTC_HW_CALI)
  1618. RTC_write_trigger_wait();
  1619. #endif // #if defined(DRV_RTC_HW_CALI)
  1620. }
  1621. #if !defined(__FUE__)
  1622. #if defined(RTC_HW_CALIBRATION)
  1623. /*
  1624. * FUNCTION                                                            
  1625. * rtc_get_cali_offset
  1626. *
  1627. * DESCRIPTION                                                           
  1628. *    To get the repeat calibration count value from register
  1629. *
  1630. * CALLS  
  1631. * This function is called when doing RTC HW calibration
  1632. *
  1633. * PARAMETERS
  1634. * None
  1635. *
  1636. * RETURNS
  1637. * the calibration count:  -0x40~0x3f (-64~63)
  1638. *
  1639. * GLOBALS AFFECTED
  1640. *   external_global
  1641. */
  1642. kal_int8 rtc_get_cali_offset(void)
  1643. {
  1644.    kal_uint16   data;
  1645.    kal_int8     ret_val=0;
  1646.    data = DRV_Reg(RTC_CALI);
  1647.    if (data&0x0040)
  1648.    {
  1649.       ret_val = (kal_int8)((~(data)+1)&0x007f);
  1650.       ret_val = -ret_val;
  1651.    }
  1652.    else
  1653.    {
  1654.       ret_val = (kal_int8)data;
  1655.    }
  1656.    return ret_val;
  1657. }
  1658. /*
  1659. * FUNCTION                                                            
  1660. * rtc_do_repeat_cali
  1661. *
  1662. * DESCRIPTION                                                           
  1663. *    To set the repeat calibration count value to register
  1664. *
  1665. * CALLS  
  1666. * This function is called when doing RTC HW calibration
  1667. *
  1668. * PARAMETERS
  1669. * None
  1670. *
  1671. * RETURNS
  1672. * the calibration count:  -0x40~0x3f (-64~63)
  1673. *
  1674. * GLOBALS AFFECTED
  1675. *   external_global
  1676. */
  1677. void rtc_do_repeat_cali(kal_int32  inaccurate_cnt)
  1678. {
  1679.    kal_int32   cali_cnt;
  1680.    kal_uint8   value;
  1681.    cali_cnt = inaccurate_cnt/RTC_CALI_REPEAT_TOTAL_CNT;
  1682.    /* divided by total count from last repeat calibration to this time. No that we don't do repeat
  1683.       calibration when there's no inaccurate count. */
  1684.    cali_cnt = cali_cnt/(1+no_diff_cnt);
  1685.    #ifdef RTC_CAL_DEBUG
  1686.    kal_prompt_trace(MOD_RTC_HISR, "Adjust %d count for repeat calibration registerrn", cali_cnt);
  1687.    #endif /* RTC_CAL_DEBUG */
  1688. #if !defined(LOW_COST_SUPPORT)
  1689.    drv_trace1(TRACE_GROUP_10, RTC_REPEAT_CALI_ADJUST_COUNT, cali_cnt);
  1690. #endif // #if !defined(LOW_COST_SUPPORT)
  1691.    cali_cnt += rtc_get_cali_offset();
  1692.    #ifdef RTC_CAL_DEBUG
  1693.    kal_prompt_trace(MOD_RTC_HISR, "Now the value for repeat calibration is %drn", cali_cnt);
  1694.    #endif /* RTC_CAL_DEBUG */
  1695. #if !defined(LOW_COST_SUPPORT)
  1696.    drv_trace1(TRACE_GROUP_10, RTC_REPEAT_CALI_VALUE, cali_cnt);
  1697. #endif // #if !defined(LOW_COST_SUPPORT)
  1698.    if (cali_cnt > RTC_CALI_REPEAT_MAX)
  1699.    {
  1700.       #ifdef RTC_CAL_DEBUG
  1701.       kal_prompt_trace(MOD_RTC_HISR, "Warning!! value %d exceeds repeat calibration MAX value!rn", cali_cnt);
  1702.       #endif /* RTC_CAL_DEBUG */
  1703. #if !defined(LOW_COST_SUPPORT)
  1704.       drv_trace1(TRACE_GROUP_10, RTC_REPEAT_CALI_VALUE_EXCEED_MAX, cali_cnt);
  1705. #endif // #if !defined(LOW_COST_SUPPORT)
  1706.       cali_cnt = RTC_CALI_REPEAT_MAX;
  1707.    }
  1708.    else if (cali_cnt < RTC_CALI_REPEAT_MIN)
  1709.    {
  1710.       #ifdef RTC_CAL_DEBUG
  1711.       kal_prompt_trace(MOD_RTC_HISR, "Warning!! value %d exceeds repeat calibration MIN value!rn", cali_cnt);
  1712.       #endif /* RTC_CAL_DEBUG */
  1713. #if !defined(LOW_COST_SUPPORT)
  1714.       drv_trace1(TRACE_GROUP_10, RTC_REPEAT_CALI_VALUE_EXCEED_MIN, cali_cnt);
  1715. #endif // #if !defined(LOW_COST_SUPPORT)
  1716.       cali_cnt = RTC_CALI_REPEAT_MIN;
  1717.    }
  1718.    if (cali_cnt < 0)
  1719.    {
  1720.       value = ((kal_uint8)(~(-cali_cnt)+1)) & 0x7f;
  1721.    }
  1722.    else
  1723.    {
  1724.       value = (kal_uint8)cali_cnt;
  1725.    }
  1726.    DRV_WriteReg(RTC_CALI, value);
  1727. }
  1728. /*
  1729. * FUNCTION                                                            
  1730. * rtc_do_one_time_cali
  1731. *
  1732. * DESCRIPTION                                                           
  1733. *    To do the one shot calibration
  1734. *
  1735. * CALLS  
  1736. * This function is called when doing RTC HW calibration
  1737. *
  1738. * PARAMETERS
  1739. * None
  1740. *
  1741. * RETURNS
  1742. * KAL_TRUE: need to do more one shot calibraiton
  1743. *  KAL_FASLE: No need to do more one shot calibration
  1744. *
  1745. * GLOBALS AFFECTED
  1746. *   external_global
  1747. */
  1748. kal_bool rtc_do_one_time_cali(void)
  1749. {
  1750.    kal_uint16  value;
  1751.    if (inaccurate_total_cnt > RTC_CALI_ONE_TIME_MAX)
  1752.    {
  1753.       /* Exceed max value. So calibration with RTC_CALI_ONE_TIME_MAX */
  1754.       inaccurate_total_cnt -= RTC_CALI_ONE_TIME_MAX;
  1755.       value = RTC_CALI_ONE_TIME_MAX;
  1756.       #ifdef RTC_CAL_DEBUG
  1757.       kal_prompt_trace(MOD_RTC_HISR, "Do one shot calibration for %d counts, remains: %d countsrn", RTC_CALI_ONE_TIME_MAX, inaccurate_total_cnt);
  1758.       #endif /* RTC_CAL_DEBUG */
  1759. #if !defined(LOW_COST_SUPPORT)
  1760.       drv_trace2(TRACE_GROUP_10, RTC_ONESHOT_CALI_PERFORM_COUNT, RTC_CALI_ONE_TIME_MAX, inaccurate_total_cnt);
  1761. #endif // #if !defined(LOW_COST_SUPPORT)
  1762.    }
  1763.    else if (inaccurate_total_cnt < RTC_CALI_ONE_TIME_MIN)
  1764.    {
  1765.       /* Less than the min value. So calibration with RTC_CALI_ONE_TIME_MIN */
  1766.       inaccurate_total_cnt -= RTC_CALI_ONE_TIME_MIN;
  1767.       /* 2's complement. */
  1768.       value = (~(-RTC_CALI_ONE_TIME_MIN) + 1) & 0x0fff;
  1769.       #ifdef RTC_CAL_DEBUG
  1770.       kal_prompt_trace(MOD_RTC_HISR, "Do one shot calibration for %d counts, remains: %d countsrn", RTC_CALI_ONE_TIME_MIN, inaccurate_total_cnt);
  1771.       #endif /* RTC_CAL_DEBUG */
  1772. #if !defined(LOW_COST_SUPPORT)
  1773.       drv_trace2(TRACE_GROUP_10, RTC_ONESHOT_CALI_PERFORM_COUNT, RTC_CALI_ONE_TIME_MAX, inaccurate_total_cnt);
  1774. #endif // #if !defined(LOW_COST_SUPPORT)
  1775.    }
  1776.    else
  1777.    {
  1778.       #ifdef RTC_CAL_DEBUG
  1779.       kal_prompt_trace(MOD_RTC_HISR, "Do one shot calibration for %d counts, remains: 0 countsrn", inaccurate_total_cnt);
  1780.       #endif /* RTC_CAL_DEBUG */
  1781. #if !defined(LOW_COST_SUPPORT)
  1782.       drv_trace2(TRACE_GROUP_10, RTC_ONESHOT_CALI_PERFORM_COUNT, inaccurate_total_cnt, 0);
  1783. #endif // #if !defined(LOW_COST_SUPPORT)
  1784.       /* calibration with inaccurate_total_cnt */
  1785.       if (inaccurate_total_cnt < 0)
  1786.       {
  1787.       /* 2's complement. */
  1788.          value = ((kal_uint16)(~(-inaccurate_total_cnt)+1)) & 0x0fff;
  1789.       }
  1790.       else
  1791.       {
  1792.          value = (kal_uint16)inaccurate_total_cnt;
  1793.       }
  1794.       inaccurate_total_cnt = 0;
  1795.    }
  1796.    #ifdef RTC_CAL_DEBUG
  1797.    kal_prompt_trace(MOD_RTC_HISR, "Write to one shot calib. value: %xrn", value);
  1798.    #endif /* RTC_CAL_DEBUG */
  1799. #if !defined(LOW_COST_SUPPORT)
  1800.    drv_trace1(TRACE_GROUP_10, RTC_ONESHOT_CALI_VALUE, value);
  1801. #endif // #if !defined(LOW_COST_SUPPORT)
  1802.    DRV_WriteReg(RTC_DIFF, value);
  1803.    if (inaccurate_total_cnt==0)
  1804.    {
  1805.       #ifdef RTC_CAL_DEBUG
  1806.       kal_prompt_trace(MOD_RTC_HISR, "Finish one shot calibration!!rn");
  1807.       #endif /* RTC_CAL_DEBUG */
  1808. #if !defined(LOW_COST_SUPPORT)
  1809.       drv_trace0(TRACE_GROUP_10, RTC_ONESHOT_CALI_FINISH);
  1810. #endif // #if !defined(LOW_COST_SUPPORT)
  1811.       return KAL_FALSE;
  1812.    }
  1813.    else
  1814.       return KAL_TRUE;
  1815. }
  1816. #endif /* RTC_HW_CALIBRATION */
  1817. /*
  1818. * FUNCTION                                                            
  1819. * RTC_TCintr
  1820. *
  1821. * DESCRIPTION                                                           
  1822. *    RTC Time tick interrupt handler
  1823. *
  1824. * CALLS  
  1825. * This function is called to handle TC interrupt
  1826. *
  1827. * PARAMETERS
  1828. * None
  1829. *
  1830. * RETURNS
  1831. * None
  1832. *
  1833. * GLOBALS AFFECTED
  1834. *   external_global
  1835. */
  1836. void RTC_TCintr(void)
  1837. {
  1838. #ifndef L4_NOT_PRESENT
  1839.  ilm_struct *RTC_ilm;
  1840.  RTC_Primitive *RTC_Prim;
  1841.    kal_bool msg_b; //for avoid Lint Warning 534: Ignoring return value of function 'msg_send_ext_queue"
  1842. #if defined(RTC_HW_CALIBRATION)
  1843.    kal_uint32 frame_tick;
  1844.    t_rtc current_time;
  1845.    kal_uint32 diff_frame_tick;
  1846.    if (cali_state != (kal_uint8)CAL_STATE_NONE)
  1847.    {
  1848.       kal_bool need_trigger;
  1849.       kal_int32  inaccurate_frame_tick = 0;
  1850.       need_trigger = KAL_FALSE;
  1851.       RTC_GetTime(&current_time);
  1852.       if (current_time.rtc_sec == 0)
  1853.       {
  1854.          if (cali_state == (kal_uint8)CAL_STATE_INIT)
  1855.          {
  1856.             /* Get the L1 frame tick for reference. */
  1857.             rtc_current_frame_tick = L1I_GetTimeStamp();
  1858.             cali_state = (kal_uint8)CAL_STATE_START;
  1859.             #ifdef RTC_CAL_DEBUG
  1860.             kal_prompt_trace(MOD_RTC_HISR, "Start to measure RTC inaccurate. current time - (%d:%d:%d)",
  1861.                current_time.rtc_hour, current_time.rtc_min, current_time.rtc_sec);
  1862.             kal_prompt_trace(MOD_RTC_HISR, "ini L1 ticks=%drn",rtc_current_frame_tick);
  1863.             #endif /* RTC_CAL_DEBUG */
  1864. #if !defined(LOW_COST_SUPPORT)
  1865.             drv_trace4(TRACE_GROUP_10, RTC_HW_CALI_START_MEASURE_INACCURATE, current_time.rtc_hour, current_time.rtc_min, current_time.rtc_sec, 0);
  1866.             drv_trace1(TRACE_GROUP_10, RTC_HW_CALI_INIT_MEASURE_ACCURATE_L1_TICK, rtc_current_frame_tick);
  1867. #endif // #if !defined(LOW_COST_SUPPORT)
  1868.          }
  1869.          else if (cali_state == (kal_uint8)CAL_STATE_START)
  1870.          {
  1871.             tc_min_expire_cnt++;
  1872.             if (RTC_CALI_PERIOD_MINS<=tc_min_expire_cnt)
  1873.             {
  1874.                tc_min_expire_cnt = 0;
  1875.                /* Get the L1 frame tick for computation. */
  1876.                frame_tick = L1I_GetTimeStamp();
  1877.                if(frame_tick>=rtc_current_frame_tick)
  1878.                 diff_frame_tick=(frame_tick - rtc_current_frame_tick);
  1879.                else
  1880.                {
  1881.                 diff_frame_tick=(0xffffffff-rtc_current_frame_tick+frame_tick+1);
  1882.                }
  1883.                /* Compute the difference between expected frame tick count and real frame tick count. */
  1884.                inaccurate_frame_tick = diff_frame_tick - RTC_CALI_TICK_VALUE;
  1885.                #ifdef RTC_CAL_DEBUG
  1886.                kal_prompt_trace(MOD_RTC_HISR, "Finish to measure RTC inaccurate for %d mins. current time - (%d:%d:%d)",
  1887.                   RTC_CALI_PERIOD_MINS, current_time.rtc_hour, current_time.rtc_min, current_time.rtc_sec);
  1888.                kal_prompt_trace(MOD_RTC_HISR, "L1 ticks=%d, inaccurate ticks=%drn",frame_tick, inaccurate_frame_tick);
  1889.                #endif /* RTC_CAL_DEBUG */
  1890. #if !defined(LOW_COST_SUPPORT)
  1891.                drv_trace4(TRACE_GROUP_10, RTC_HW_CALI_FINISH_MEASURE_INACCURATE, current_time.rtc_hour, current_time.rtc_min, current_time.rtc_sec, 0);
  1892.                drv_trace2(TRACE_GROUP_10, RTC_HW_CALI_FINISH_MEASURE_ACCURATE_L1_TICK, frame_tick, inaccurate_frame_tick);
  1893. #endif // #if !defined(LOW_COST_SUPPORT)
  1894.                /* The next expected frame tick count. */
  1895.                rtc_current_frame_tick += RTC_CALI_TICK_VALUE;
  1896.                if (inaccurate_frame_tick == 0)
  1897.                {
  1898.                   no_diff_cnt++;
  1899.                   #ifdef RTC_CAL_DEBUG
  1900.                   kal_prompt_trace(MOD_RTC_HISR, "No difference and no need do calibration!!  no_diff_cnt=%d rn", no_diff_cnt);
  1901.                   #endif /* RTC_CAL_DEBUG */
  1902. #if !defined(LOW_COST_SUPPORT)
  1903.                   drv_trace1(TRACE_GROUP_10, RTC_HW_CALI_NO_NEED_TO_CALI, no_diff_cnt);
  1904. #endif // #if !defined(LOW_COST_SUPPORT)
  1905.                }
  1906.                else
  1907.                {
  1908.                   if (inaccurate_frame_tick < 0)
  1909.                   {
  1910.                      /* Note that there are 650/3 frames ticks per second. */
  1911.                      inaccurate_total_cnt = (-inaccurate_frame_tick)*rtc_current_freq_8secs*3/8/650;
  1912.                      inaccurate_total_cnt = -inaccurate_total_cnt;
  1913.                   }
  1914.                   else
  1915.                   {
  1916.                      inaccurate_total_cnt = inaccurate_frame_tick*rtc_current_freq_8secs*3/8/650;
  1917.                   }
  1918.                   #ifdef RTC_CAL_DEBUG
  1919.                   kal_prompt_trace(MOD_RTC_HISR, "Do repeat calibration for %d countsrn", inaccurate_total_cnt);
  1920.                   #endif /* RTC_CAL_DEBUG */
  1921. #if !defined(LOW_COST_SUPPORT)
  1922.                   drv_trace1(TRACE_GROUP_10, RTC_REPEAT_CALI_PERFORM_CALI, inaccurate_total_cnt);
  1923. #endif // #if !defined(LOW_COST_SUPPORT)
  1924.                   rtc_do_repeat_cali(inaccurate_total_cnt);
  1925.                   one_time_cali = KAL_TRUE;
  1926.                   /* Enable TC seconds interrupt */
  1927.                   if (rtc_tc_int_var != TC_EN_ChkS)
  1928.                 DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_SEC);
  1929.                   need_trigger = KAL_TRUE;
  1930.                   no_diff_cnt = 0;
  1931.                }
  1932.             }
  1933.             else
  1934.             {
  1935.                #ifdef RTC_CAL_DEBUG
  1936.                kal_prompt_trace(MOD_RTC_HISR, "In progress for HW RTC calibration. current time - (%d:%d:%d)",
  1937.                   current_time.rtc_hour, current_time.rtc_min, current_time.rtc_sec);
  1938.                #endif /* RTC_CAL_DEBUG */
  1939. #if !defined(LOW_COST_SUPPORT)
  1940.                drv_trace4(TRACE_GROUP_10, RTC_HW_CALI_IN_PROGRESS, current_time.rtc_hour, current_time.rtc_min, current_time.rtc_sec, 0);
  1941. #endif // #if !defined(LOW_COST_SUPPORT)
  1942.                
  1943.             }
  1944.          }
  1945.       } /* (current_time.rtc_sec == 0) */
  1946.       if (one_time_cali == KAL_TRUE)
  1947.       {
  1948.          kal_bool ret_value;
  1949.          ret_value = rtc_do_one_time_cali();
  1950.          need_trigger = KAL_TRUE;
  1951.          if (ret_value == KAL_FALSE)
  1952.          {
  1953.             /* No need to do one shot calibration. Change back to minute interrupt if needed. */
  1954.             if (rtc_tc_int_var != TC_EN_ChkS)
  1955.             {
  1956.           DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_MIN);
  1957.             }
  1958.             one_time_cali = KAL_FALSE;
  1959.          }
  1960.       }
  1961.       if (need_trigger == KAL_TRUE)
  1962.       {
  1963.          RTC_write_trigger();
  1964.       }
  1965. #elif defined(RTC_SW_CALIBRATION)
  1966.    t_rtc current_time;
  1967.    kal_uint32 frame_tick;
  1968. #if defined(SLEEP_WORKAROUND_IN_RTC)
  1969. kal_uint32 rtc_sleep_wa_delay_tick;
  1970. static t_rtc previous_time;
  1971. #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  1972.    if (cali_state != (kal_uint8)CAL_STATE_NONE)
  1973.    {
  1974.       RTC_GetTime(&current_time);
  1975.       if (cali_state == (kal_uint8)CAL_STATE_INIT)
  1976.       {
  1977.        kal_uint16 reg_comm2;
  1978.        //kal_uint16 tmp;
  1979.       //tmp = DRV_Reg(RTC_IRQ_EN);
  1980.       //tmp &=~RTC_IRQ_EN_ONESHOT;
  1981.    //tmp |= RTC_IRQ_EN_TC;
  1982.        //DRV_WriteReg(RTC_IRQ_EN,tmp);
  1983.          #ifdef RTC_CAL_DEBUG2
  1984.          kal_prompt_trace(MOD_RTC_HISR, "Start to do poweroff calibration");
  1985.          #endif /* RTC_CAL_DEBUG */
  1986.          
  1987. #if !defined(LOW_COST_SUPPORT)
  1988.          drv_trace0(TRACE_GROUP_10, RTC_SW_CALI_START_POWEROFF_CALI);
  1989. #endif // #if !defined(LOW_COST_SUPPORT)
  1990.          /* Change to calibration start state. */
  1991.          cali_state = (kal_uint8)CAL_STATE_START;
  1992.          if (pwroff_inacc_ticks != 0)
  1993.          {
  1994.             kal_uint16 hours, mins, secs;
  1995.             kal_bool neg = KAL_FALSE;
  1996.             tc_min_expire_cnt = 0;
  1997.             /* Check the time has to be forward or backward. */
  1998.             if (pwroff_inacc_ticks < 0)
  1999.             {
  2000.                neg = KAL_TRUE;
  2001.                pwroff_inacc_ticks = -pwroff_inacc_ticks;
  2002.             }
  2003.             /* Calculate the time shift for hour(60*13000), minute(13000) and second(650/3). */
  2004.             hours = pwroff_inacc_ticks/(13000*60);
  2005.             pwroff_inacc_ticks = pwroff_inacc_ticks%(13000*60);
  2006.             mins = pwroff_inacc_ticks/13000;
  2007.             pwroff_inacc_ticks = pwroff_inacc_ticks%13000;
  2008.             secs = pwroff_inacc_ticks*3/650;
  2009.             #ifdef RTC_CAL_DEBUG2
  2010.             kal_prompt_trace(MOD_RTC_HISR, "current time before poweroff calibration- (%d:%d:%d)",
  2011.                current_time.rtc_hour, current_time.rtc_min, current_time.rtc_sec);
  2012.             #endif /* RTC_CAL_DEBUG */
  2013. #if !defined(LOW_COST_SUPPORT)
  2014.             drv_trace4(TRACE_GROUP_10, RTC_SW_CALI_START_POWEROFF_CALI_TIME, current_time.rtc_hour, current_time.rtc_min, current_time.rtc_sec, 0);
  2015. #endif // #if !defined(LOW_COST_SUPPORT)
  2016.             
  2017.             /* Calculate the ticks to reach the nearest second boundary. */
  2018.             if (neg==KAL_TRUE)
  2019.             {
  2020.                pwroff_inacc_ticks = pwroff_inacc_ticks - (secs*650/3);
  2021.                rtc_backward_time(&current_time, hours, mins, secs);
  2022.             }
  2023.             else
  2024.             {
  2025.                secs++;
  2026.                pwroff_inacc_ticks = (secs*650/3) - pwroff_inacc_ticks;
  2027.                rtc_forward_time(&current_time, hours, mins, secs);
  2028.             }
  2029.             /* Set the correct time here. */
  2030.             RTC_Cali_Time(&current_time);
  2031.             #ifdef RTC_CAL_DEBUG2
  2032.             kal_prompt_trace(MOD_RTC_HISR, "current time after poweroff calibration- (%d:%d:%d)",
  2033.                current_time.rtc_hour, current_time.rtc_min, current_time.rtc_sec);
  2034.             #endif /* RTC_CAL_DEBUG */
  2035. #if !defined(LOW_COST_SUPPORT)
  2036.             drv_trace4(TRACE_GROUP_10, RTC_SW_CALI_FINISH_POWEROFF_CALI_TIME, current_time.rtc_hour, current_time.rtc_min, current_time.rtc_sec, 0);
  2037. #endif // #if !defined(LOW_COST_SUPPORT)
  2038.             if (pwroff_inacc_ticks != 0)
  2039.             {
  2040.                #ifdef RTC_CAL_DEBUG2
  2041.                kal_prompt_trace(MOD_RTC_HISR, "wait %d ticks to do second poweroff calibration",
  2042.                   pwroff_inacc_ticks);
  2043.                #endif /* RTC_CAL_DEBUG */
  2044. #if !defined(LOW_COST_SUPPORT)
  2045.                drv_trace1(TRACE_GROUP_10, RTC_SW_CALI_FINISH_2ND_POWEROFF_CALI, pwroff_inacc_ticks);
  2046. #endif // #if !defined(LOW_COST_SUPPORT)
  2047.                /* Still some ticks hasn't been calibrated. So change to init2 state */
  2048.                cali_state = (kal_uint8)CAL_STATE_INIT2;
  2049.                /* Save the correct time in: next_current_time */
  2050.                next_current_time = current_time;
  2051.                /* We'll write the correct time again when the exact correspoding time(second) comes by using KAL timer. */
  2052.                kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_cali_init, NULL, pwroff_inacc_ticks,0);
  2053.             }
  2054.          }
  2055.          /* validate rtc nvram data */
  2056.        reg_comm2 = DRV_Reg(DRV_COMM_REG2);
  2057.          reg_comm2 |= DRV_COMM_REG2_RTC_NVRAM;
  2058.          DRV_WriteReg(DRV_COMM_REG2,reg_comm2);    
  2059.          /* Send msg to write current time to nvram */
  2060.          rtc_nvram_s.time_valid = KAL_TRUE;
  2061.          rtc_nvram_s.rtc_time = current_time;
  2062.          rtc_write_to_nvram();
  2063.          /* Turn on the TC minute interrupt if necessary. */
  2064.          if (rtc_tc_int_var != TC_EN_ChkS)
  2065.           DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_MIN);
  2066.          if (pwroff_inacc_ticks == 0)
  2067.          {
  2068.             tick_left=0;
  2069.             one_third_tick_count = 0;
  2070.             /* We may start calibration here. */
  2071.             next_current_time = current_time;
  2072.             kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_timer, NULL, rtc_cali_tick_value+rtc_cali_tick_safe_margin,0);
  2073.             #if (!defined(__L1_STANDALONE__)) 
  2074.             frame_tick=L1I_GetTimeStamp();
  2075.             #endif
  2076.             #ifdef RTC_CAL_DEBUG
  2077.             kal_prompt_trace(MOD_RTC_HISR, "Start poweron calibration");
  2078.             kal_prompt_trace(MOD_RTC_HISR, "(%d:%d:%d): ",next_current_time.rtc_hour,next_current_time.rtc_min,next_current_time.rtc_sec);
  2079.             kal_prompt_trace(MOD_RTC_HISR, "ini tick=%drn",frame_tick);                                                   
  2080.             #endif /* RTC_CAL_DEBUG */
  2081. #if !defined(LOW_COST_SUPPORT)
  2082.             drv_trace4(TRACE_GROUP_10, RTC_SW_CALI_START_POWERON_CALI_TIME, next_current_time.rtc_hour,next_current_time.rtc_min,next_current_time.rtc_sec, frame_tick);
  2083. #endif // #if !defined(LOW_COST_SUPPORT)
  2084.             next_frame_tick=frame_tick+rtc_cali_tick_value;         
  2085.             rtc_get_next_time();    
  2086.          }
  2087.       }
  2088.       else if (cali_state == (kal_uint8)CAL_STATE_START)
  2089.       {
  2090.          #if defined(SLEEP_WORKAROUND_IN_RTC)
  2091.          // When WA enabled, RTC ISR may be delayed, we can NOT guarantee trc_sec == 0 always
  2092.          // Our goal is to count tc_min_expire_cnt every minute
  2093.          if (current_time.rtc_min != /*lint -e(727)*/previous_time.rtc_min)
  2094.          #else
  2095.          // If WA not enabled, we just only need to confirm sec == 0
  2096.          /* rtc_sec is zero. Meaning it's TC minute expired. */
  2097.          if (current_time.rtc_sec == 0)
  2098.          #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2099.          {
  2100.             if (tc_min_expire_cnt == 0)
  2101.             {
  2102.                /* Save the frame stick as a refence. */
  2103.                frame_tick1=L1I_GetTimeStamp();
  2104.                #ifdef RTC_CAL_DEBUG2
  2105.                kal_prompt_trace(MOD_RTC_HISR, "Inaccuracy measurement start");
  2106.                #endif /* RTC_CAL_DEBUG */
  2107. #if !defined(LOW_COST_SUPPORT)
  2108.                drv_trace0(TRACE_GROUP_10, RTC_SW_CALI_POWERON_CALI_START_INACCURACY_MEASURE);
  2109. #endif // #if !defined(LOW_COST_SUPPORT)
  2110.                
  2111.             }
  2112. #if defined(SLEEP_WORKAROUND_IN_RTC)
  2113. else if (tc_min_expire_cnt == 59)
  2114. {
  2115. // Calculate how many ticks we should wait to disable sleep mode
  2116. // We plan to wakeup at 58 sec
  2117. rtc_sleep_wa_delay_tick = 58 - (kal_uint32)current_time.rtc_sec;
  2118. rtc_sleep_wa_delay_tick = (kal_uint32) ((rtc_sleep_wa_delay_tick * 1000) / 4.615);
  2119. // Set kal timer in order to disable sleep mode for RTC calibration
  2120. kal_set_timer(rtc_sleep_wa_timer_id, (kal_timer_func_ptr)rtc_sleep_wa_timer_cb, NULL, rtc_sleep_wa_delay_tick,0);
  2121. #ifdef RTC_SLEEP_WA_DEBUG
  2122. kal_prompt_trace(MOD_RTC_HISR, "SleepWA: (%d:%d), Start disable sleep timer with tick: %d", 
  2123. current_time.rtc_min, current_time.rtc_sec, rtc_sleep_wa_delay_tick);
  2124. #endif /* RTC_SLEEP_WA_DEBUG */
  2125. }
  2126. #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2127.             else if (tc_min_expire_cnt == 60)
  2128.             {
  2129. #if defined(SLEEP_WORKAROUND_IN_RTC)
  2130. // We only calculate 1hr diff when disable_sleep is performed
  2131. // This means there is no problem between kal timer and our rtc TC intr
  2132. if (rtc_sleep_wa_disable_sleep_performed)
  2133. {
  2134. #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2135.             
  2136.                /* Get the frame stick after 60 minutes passed. */
  2137.                frame_tick2=L1I_GetTimeStamp();
  2138.                /* Calculate the frame tick number for 1 hr of RTC time. */
  2139.                if(frame_tick2>=frame_tick1) 
  2140.                 frame_tick_1hr_diff=(kal_int32)(frame_tick2-frame_tick1 - 13000*60);
  2141.                else
  2142.                 frame_tick_1hr_diff=(kal_int32)(0xffffffff+frame_tick2-frame_tick1+1 - 13000*60);
  2143.                
  2144.                #ifdef RTC_CAL_DEBUG2
  2145.                kal_prompt_trace(MOD_RTC_HISR, "Inaccuracy measurement done for one hour: %d ticks", frame_tick_1hr_diff);
  2146.                #endif /* RTC_CAL_DEBUG */
  2147. #if !defined(LOW_COST_SUPPORT)
  2148.                drv_trace1(TRACE_GROUP_10, RTC_SW_CALI_POWERON_CALI_FINISH_INACCURACY_MEASURE, frame_tick_1hr_diff);
  2149. #endif // #if !defined(LOW_COST_SUPPORT)
  2150.                
  2151.                /* save frame_tick_1hr_diff(inaccuracy) to nvram here */
  2152.                //rtc_nvram_s.ticks_diff_valid = KAL_TRUE;
  2153.                //rtc_nvram_s.ticks_diff_per_hour = frame_tick_1hr_diff;
  2154.                //rtc_write_to_nvram();
  2155.                
  2156. #if defined(SLEEP_WORKAROUND_IN_RTC)
  2157. #ifdef RTC_SLEEP_WA_DEBUG
  2158. kal_prompt_trace(MOD_RTC_HISR, "SleepWA: (%d:%d), Calculate power off calibration info", 
  2159. current_time.rtc_min, current_time.rtc_sec);
  2160. #endif /* RTC_SLEEP_WA_DEBUG */
  2161. } // if (rtc_sleep_wa_disable_sleep_performed)
  2162. else
  2163. {
  2164. #ifdef RTC_SLEEP_WA_DEBUG
  2165. kal_prompt_trace(MOD_RTC_HISR, "SleepWA: (%d:%d), Warning!!, disable sleep mode NOT performed", 
  2166. current_time.rtc_min, current_time.rtc_sec);
  2167. #endif /* RTC_SLEEP_WA_DEBUG */
  2168. }
  2169. #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2170.                
  2171. #if defined(SLEEP_WORKAROUND_IN_RTC)
  2172. rtc_sleep_wa_disable_sleep_performed = KAL_FALSE;
  2173. // After RTC calibration processing is done, re-enabel sleep mode
  2174. // We always enable sleep mode in this stage
  2175. rtc_sleep_wa_enable_sleep_mode();
  2176. #ifdef RTC_SLEEP_WA_DEBUG
  2177. kal_prompt_trace(MOD_RTC_HISR, "SleepWA: (%d:%d), Enable sleep mode", 
  2178. current_time.rtc_min, current_time.rtc_sec);
  2179. #endif /* RTC_SLEEP_WA_DEBUG */
  2180. #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2181.             }
  2182.             #if defined(SLEEP_WORKAROUND_IN_RTC)
  2183.             // Only sec, min, hour info is enough for us
  2184.             previous_time.rtc_sec = current_time.rtc_sec;
  2185.             previous_time.rtc_min = current_time.rtc_min;
  2186.             previous_time.rtc_hour = current_time.rtc_hour;
  2187.             #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2188.             tc_min_expire_cnt++;
  2189.          }
  2190.       }
  2191. #endif /* RTC_SW_CALIBRATION */
  2192. #if defined(RTC_HW_CALIBRATION) || defined(RTC_SW_CALIBRATION)
  2193.       /* Check user's configuration for Time Clock interrupt. */
  2194.    switch(rtc_tc_int_var)
  2195.    {
  2196.    case TC_EN_None:
  2197.    return;
  2198.    break;
  2199.    
  2200.    case TC_EN_ChkS:
  2201.    break;
  2202.    
  2203.    case TC_EN_ChkM:
  2204.             #if defined(SLEEP_WORKAROUND_IN_RTC)
  2205.             // If WA enabled, we won't check sec, because it would be delayed
  2206.             ;
  2207.             #else
  2208.             // Original design: sec must be 0 if it is user TC setting
  2209.             if (current_time.rtc_sec != 0)
  2210.                return;
  2211.             #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2212.    break;
  2213.    
  2214.       case TC_EN_ChkH:
  2215.             #if defined(SLEEP_WORKAROUND_IN_RTC)
  2216.             // If WA enabled, we won't check sec, because it would be delayed
  2217.             if ((current_time.rtc_min != 0))
  2218.                return;
  2219.             #else
  2220.             // Original design: sec must be 0 if it is user TC setting
  2221.             if ((current_time.rtc_sec != 0) || (current_time.rtc_min != 0))
  2222.                return;
  2223.             #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2224.    break;
  2225.    
  2226.    case TC_EN_ChkD:
  2227.    case TC_EN_ChkW:
  2228.             #if defined(SLEEP_WORKAROUND_IN_RTC)
  2229.             // If WA enabled, we won't check sec, because it would be delayed
  2230.             if ((current_time.rtc_min != 0) ||(current_time.rtc_hour != 0))
  2231.                return;
  2232.             #else
  2233.             // Original design: sec must be 0 if it is user TC setting
  2234.             if ((current_time.rtc_sec != 0) || (current_time.rtc_min != 0) ||(current_time.rtc_hour != 0))
  2235.                return;
  2236.             #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2237.    break;
  2238.    case TC_EN_ChkMon:
  2239.             #if defined(SLEEP_WORKAROUND_IN_RTC)
  2240.             // If WA enabled, we won't check sec, because it would be delayed
  2241.             if ((current_time.rtc_min != 0) ||(current_time.rtc_hour != 0) 
  2242.                ||(current_time.rtc_day != 1))
  2243.                return;
  2244.             #else
  2245.             // Original design: sec must be 0 if it is user TC setting
  2246.             if ((current_time.rtc_sec != 0) || (current_time.rtc_min != 0) ||(current_time.rtc_hour != 0) 
  2247.                ||(current_time.rtc_day != 1))
  2248.                return;
  2249.             #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2250.    break;
  2251.    
  2252.    case TC_EN_ChkY:
  2253.             #if defined(SLEEP_WORKAROUND_IN_RTC)
  2254.             // If WA enabled, we won't check sec, because it would be delayed
  2255.             if ((current_time.rtc_min != 0) ||(current_time.rtc_hour != 0) 
  2256.                ||(current_time.rtc_day != 1) || (current_time.rtc_mon != 1))
  2257.                return;
  2258.             #else
  2259.             // Original design: sec must be 0 if it is user TC setting
  2260.             if ((current_time.rtc_sec != 0) || (current_time.rtc_min != 0) ||(current_time.rtc_hour != 0) 
  2261.                ||(current_time.rtc_day != 1) || (current_time.rtc_mon != 1))
  2262.                return;
  2263.             #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2264.    break;
  2265.  
  2266.          case TC_EN_NoChange:
  2267.             break;
  2268.       
  2269.    default:
  2270.    break;
  2271.    }
  2272.    } /*(cali_state != CAL_STATE_NONE)*/
  2273.    #ifdef RTC_CAL_DEBUG
  2274.    //frame_tick=L1I_GetTimeStamp();
  2275.    kal_prompt_trace(MOD_RTC_HISR, "TC interrupt for userrn");
  2276.    #endif /* RTC_CAL_DEBUG */
  2277.    drv_trace0(TRACE_GROUP_10, RTC_TC_INTR_FOR_USER);
  2278.    
  2279.    
  2280. #endif /* defined(RTC_HW_CALIBRATION) || defined(RTC_SW_CALIBRATION) */
  2281.    //RTC_ilm = allocate_ilm(MOD_RTC_HISR);
  2282.    RTC_ilm = allocate_ilm(MOD_DRV_HISR);
  2283.    RTC_Prim = (RTC_Primitive*)
  2284.    construct_local_para(sizeof(RTC_Primitive), TD_CTRL);
  2285.    RTC_Prim->rtc_ind = RTC_TC_IND;
  2286.    //RTC_ilm->src_mod_id = MOD_RTC_HISR;
  2287.    RTC_ilm->src_mod_id = MOD_DRV_HISR;
  2288.    RTC_ilm->dest_mod_id = MOD_UEM;
  2289.    RTC_ilm->sap_id = DRIVER_PS_SAP;
  2290.    RTC_ilm->msg_id = MSG_ID_DRVUEM_RTC_PERIOD_IND;
  2291.    RTC_ilm->local_para_ptr = (local_para_struct *)RTC_Prim;
  2292. RTC_ilm->peer_buff_ptr = NULL;
  2293.    /*lint -e(534)*/msg_b = msg_send_ext_queue(RTC_ilm);
  2294. #endif   /*L4_NOT_PRESENT*/   
  2295. }
  2296. void RTC_ALintr(void)
  2297. {
  2298.    kal_uint16 BBPU;
  2299. #ifndef L4_NOT_PRESENT
  2300.  kal_bool btmp;
  2301. #endif   /*L4_NOT_PRESENT*/ 
  2302. #ifndef L4_NOT_PRESENT
  2303. ilm_struct *RTC_ilm;
  2304. RTC_Primitive *RTC_Prim;
  2305. #endif   /*L4_NOT_PRESENT*/ 
  2306.    BBPU = DRV_Reg(RTC_BBPU);
  2307.    BBPU &=~(RTC_BBPU_ALARM_SW);/*0227 TY adds*/
  2308. #if defined(DRV_RTC_WRITE_ENABLE)
  2309. BBPU |= RTC_BBPU_WRITE_EN;
  2310. #endif // #if defined(DRV_RTC_WRITE_ENABLE)
  2311. DRV_WriteReg(RTC_BBPU,(BBPU|RTC_BBPU_KEY));/*0227 TY adds*/
  2312. #if defined(DRV_RTC_HW_CALI)
  2313.    RTC_write_trigger();
  2314. #endif /*DRV_RTC_HW_CALI*/
  2315.    drv_trace0(TRACE_GROUP_10, RTC_AL_INTR);
  2316.    
  2317. #ifndef L4_NOT_PRESENT
  2318.    //RTC_ilm = allocate_ilm(MOD_RTC_HISR);
  2319.    RTC_ilm = allocate_ilm(MOD_DRV_HISR);
  2320.    RTC_Prim = (RTC_Primitive*)
  2321.    construct_local_para(sizeof(RTC_Primitive), TD_CTRL);
  2322.    RTC_Prim->rtc_ind = RTC_AL_IND;
  2323.    //RTC_ilm->src_mod_id = MOD_RTC_HISR;
  2324.    RTC_ilm->src_mod_id = MOD_DRV_HISR;
  2325.    RTC_ilm->dest_mod_id = MOD_UEM;
  2326.    RTC_ilm->sap_id = DRIVER_PS_SAP;
  2327.    RTC_ilm->msg_id = MSG_ID_DRVUEM_RTC_PERIOD_IND;
  2328.    RTC_ilm->local_para_ptr = (local_para_struct *)RTC_Prim;
  2329. RTC_ilm->peer_buff_ptr = NULL;
  2330.    btmp = msg_send_ext_queue(RTC_ilm);
  2331. #endif   /*L4_NOT_PRESENT*/ 
  2332.    
  2333. }
  2334. #if defined(RTC_SW_CALIBRATION)
  2335. /*
  2336. * FUNCTION                                                            
  2337. * rtc_write_to_nvram
  2338. *
  2339. * DESCRIPTION                                                           
  2340. *    Save RTC SW calibration data to nvram
  2341. *
  2342. * CALLS  
  2343. * This function is called internally for RTC SW calibration
  2344. *
  2345. * PARAMETERS
  2346. * None
  2347. *
  2348. * RETURNS
  2349. * None
  2350. *
  2351. * GLOBALS AFFECTED
  2352. *  None
  2353. */
  2354. void rtc_write_to_nvram(void)
  2355. {
  2356.    ilm_struct *rtc_ilm_ptr=0;
  2357.    void  *parm_stream=0;
  2358.    void  *data_stream=0;
  2359.    kal_uint16 pdu_len;
  2360.    kal_bool msg_b; //for avoid Lint Warning 534: Ignoring return value of function 'msg_send_ext_queue"
  2361.    
  2362. #if !defined(LOW_COST_SUPPORT)
  2363.    drv_trace0(TRACE_GROUP_10, RTC_SW_CALI_WRITE_NVRAM);
  2364. #endif // #if !defined(LOW_COST_SUPPORT)
  2365.    
  2366.    rtc_ilm_ptr = allocate_ilm(MOD_RTC_HISR);
  2367.    rtc_ilm_ptr->msg_id = MSG_ID_NVRAM_WRITE_REQ;
  2368.    parm_stream = construct_local_para(sizeof(nvram_write_req_struct), TD_CTRL);
  2369.    data_stream = construct_peer_buff(sizeof(rtc_nvram_data), 0, 0, TD_CTRL);
  2370.    ((nvram_write_req_struct*) parm_stream)->file_idx =  (nvram_lid_enum)NVRAM_EF_RTC_DATA_LID;
  2371.    ((nvram_write_req_struct*) parm_stream)->para = 1;
  2372.    pdu_len = sizeof(rtc_nvram_data);
  2373.    /*lint -e(534)*/kal_mem_cpy(get_pdu_ptr(data_stream, &pdu_len), (void*)&rtc_nvram_s, sizeof(rtc_nvram_data));
  2374.    rtc_ilm_ptr->local_para_ptr = (local_para_struct*) parm_stream;
  2375.    rtc_ilm_ptr->peer_buff_ptr = (peer_buff_struct*) data_stream;
  2376.    rtc_ilm_ptr->src_mod_id  = MOD_RTC_HISR;
  2377.    rtc_ilm_ptr->dest_mod_id = MOD_NVRAM;
  2378.    rtc_ilm_ptr->sap_id = PS_NVRAM_SAP;
  2379.    msg_b = msg_send_ext_queue(rtc_ilm_ptr);
  2380. #if !defined(LOW_COST_SUPPORT)
  2381. #if !defined(__FUE__)
  2382.   drv_trace8(TRACE_GROUP_9, RTC_WRITE_TO_NVRAM, rtc_nvram_s.rtc_time.rtc_year, rtc_nvram_s.rtc_time.rtc_mon,
  2383.       rtc_nvram_s.rtc_time.rtc_day, rtc_nvram_s.rtc_time.rtc_hour, rtc_nvram_s.rtc_time.rtc_min, 
  2384.       rtc_nvram_s.rtc_time.rtc_sec, 0, 0);
  2385. #endif
  2386. #endif
  2387. }
  2388. /*
  2389. * FUNCTION                                                            
  2390. * rtc_compare_time
  2391. *
  2392. * DESCRIPTION                                                           
  2393. *  Compare the rtc time.
  2394. *
  2395. * CALLS  
  2396. * This function is called internally for RTC SW calibration
  2397. *
  2398. * PARAMETERS
  2399. * rtc_time1
  2400. * rtc_time2
  2401. *
  2402. * RETURNS
  2403. * KAL_TRUE if rtc_time1 > rtc_time2. Else, return KAL_FALSE
  2404. *
  2405. * GLOBALS AFFECTED
  2406. *  None
  2407. */
  2408. kal_bool rtc_compare_time(t_rtc *rtc_time1, t_rtc *rtc_time2)
  2409. {
  2410.    if (rtc_time1->rtc_year < rtc_time2->rtc_year)
  2411.       return KAL_FALSE;
  2412.    else if (rtc_time1->rtc_year == rtc_time2->rtc_year)
  2413.    {
  2414.       if (rtc_time1->rtc_mon < rtc_time2->rtc_mon)
  2415.          return KAL_FALSE;
  2416.       else if (rtc_time1->rtc_mon == rtc_time2->rtc_mon)
  2417.       {
  2418.          if (rtc_time1->rtc_day < rtc_time2->rtc_day)
  2419.             return KAL_FALSE;
  2420.          else if (rtc_time1->rtc_day == rtc_time2->rtc_day)
  2421.          {
  2422.             if (rtc_time1->rtc_hour < rtc_time2->rtc_hour)
  2423.                return KAL_FALSE;
  2424.             else if (rtc_time1->rtc_hour == rtc_time2->rtc_hour)
  2425.             {
  2426.                if (rtc_time1->rtc_min < rtc_time2->rtc_min)
  2427.                   return KAL_FALSE;
  2428.                else if (rtc_time1->rtc_min == rtc_time2->rtc_min)
  2429.                {
  2430.                   if (rtc_time1->rtc_sec <= rtc_time2->rtc_sec)
  2431.                      return KAL_FALSE;
  2432.                }
  2433.             }
  2434.          }
  2435.       }
  2436.    }
  2437.    return KAL_TRUE;
  2438. }
  2439. /*
  2440. * FUNCTION                                                            
  2441. * rtc_get_diff_time
  2442. *
  2443. * DESCRIPTION                                                           
  2444. *  Get the time different between 2 RTC time into hours and minute. Note that if the year difference is
  2445. *  more than 1 then we'll see it as invalid.
  2446. *
  2447. * CALLS  
  2448. * This function is called internally for RTC SW calibration
  2449. *
  2450. * PARAMETERS
  2451. * curr_time: INPUT for current RTC time
  2452. * prev_time: INPUT for previous RTC time
  2453. *  ret_hours: OUTPUT for hours of time difference between current time and previous time
  2454. *  ret_mins: OUTPUT for minutes of time difference between current time and previous time
  2455. *
  2456. * RETURNS
  2457. * KAL_TRUE: If the time difference is valid
  2458. *  KAL_FALSE: If the time difference is invalid such as curr_time is older, or the time difference is 
  2459. *             too large.
  2460. *
  2461. * GLOBALS AFFECTED
  2462. *  None
  2463. */
  2464. kal_bool rtc_get_diff_time(t_rtc *curr_time, t_rtc *prev_time, kal_uint16 *ret_hours, kal_uint16 *ret_mins)
  2465. {
  2466.    kal_int16 secs;
  2467.    kal_int16 mins;
  2468.    kal_int32 hours;
  2469.    kal_int16 days;
  2470.    /* Invalid if the current time is older. */
  2471.    if (KAL_FALSE == rtc_compare_time(curr_time, prev_time))
  2472.       return KAL_FALSE;
  2473.    /* Invalid if the year difference is more than 1. */
  2474.    if (curr_time->rtc_year>(prev_time->rtc_year+1))
  2475.       return KAL_FALSE;
  2476.    days = 0;
  2477.    secs = 0;
  2478.    mins = 0;
  2479.    hours = 0;
  2480.    if (curr_time->rtc_sec < prev_time->rtc_sec)
  2481.    {
  2482.       secs = 60;
  2483.       mins = -1;
  2484.    }
  2485.    secs += (curr_time->rtc_sec - prev_time->rtc_sec);
  2486.    if (curr_time->rtc_min < (prev_time->rtc_min - mins))
  2487.    {
  2488.       mins += 60;
  2489.       hours = -1;
  2490.    }
  2491.    mins += (curr_time->rtc_min - prev_time->rtc_min);
  2492.    if (secs >= 30)
  2493.       mins++;
  2494.    if (curr_time->rtc_hour < (prev_time->rtc_hour - hours))
  2495.    {
  2496.       hours += 24;
  2497.       days = -1;
  2498.    }
  2499.    hours += (curr_time->rtc_hour - prev_time->rtc_hour);
  2500.    if ((curr_time->rtc_year==prev_time->rtc_year) && (curr_time->rtc_mon==prev_time->rtc_mon))
  2501.    {
  2502.       days += (curr_time->rtc_day - prev_time->rtc_day);
  2503.    }
  2504.    else
  2505.    {
  2506.       kal_uint8   month;
  2507.       kal_uint8   month_end;
  2508.       days += (days_in_month[prev_time->rtc_mon] - prev_time->rtc_day);
  2509.       month_end = (curr_time->rtc_year == prev_time->rtc_year) ? curr_time->rtc_mon : 13; 
  2510.       for (month = (prev_time->rtc_mon+1); month < month_end; month++)
  2511.       {
  2512.          days += days_in_month[month];
  2513.       }
  2514.       if ((0==(prev_time->rtc_year%4))&&(prev_time->rtc_mon<=2))
  2515.       {
  2516.          if (curr_time->rtc_year != prev_time->rtc_year)
  2517.             days++;
  2518.          else if (curr_time->rtc_mon>2)
  2519.             days++;
  2520.       }
  2521.       month_end = (curr_time->rtc_year == prev_time->rtc_year) ? 0 : curr_time->rtc_mon; 
  2522.       for (month = 1; month < month_end; month++)
  2523.       {
  2524.          days += days_in_month[month];
  2525.       }
  2526.       days += curr_time->rtc_day;
  2527.       if ((curr_time->rtc_year != prev_time->rtc_year)&&(0==(curr_time->rtc_year%4))&&(curr_time->rtc_mon>2))
  2528.          days++;
  2529.    }
  2530.    hours += (days*24);
  2531.    *ret_mins = (kal_uint16)mins;
  2532.    *ret_hours = (kal_uint16)hours;
  2533.    return KAL_TRUE;
  2534. }
  2535. /*
  2536. * FUNCTION                                                            
  2537. * rtc_forward_time
  2538. *
  2539. * DESCRIPTION                                                           
  2540. *  Increase the RTC time by hours, minutes and seconds.
  2541. *
  2542. * CALLS  
  2543. * This function is called internally for RTC SW calibration
  2544. *
  2545. * PARAMETERS
  2546. * rtc_time: current RTC time
  2547. * hours: hours to increase time
  2548. *  mins: minutes to increase time
  2549. *  secs: seconds to increase time
  2550. *
  2551. * RETURNS
  2552. * None
  2553. *
  2554. * GLOBALS AFFECTED
  2555. *  None
  2556. */
  2557. void rtc_forward_time(t_rtc *rtc_time, kal_uint16 hours, kal_uint16 mins, kal_uint16 secs)
  2558. {
  2559.    secs += rtc_time->rtc_sec;
  2560.    mins += rtc_time->rtc_min;
  2561.    hours += rtc_time->rtc_hour;
  2562. #if !defined(LOW_COST_SUPPORT)
  2563.    //TIMER_HISR stack only 512 bytes, remove trace
  2564.    //drv_trace8(TRACE_GROUP_10, RTC_FORWARD_TIME_1, rtc_time->rtc_hour, rtc_time->rtc_min, rtc_time->rtc_sec, hours, mins, secs, 0, 0);
  2565. #endif // #if !defined(LOW_COST_SUPPORT)
  2566.    
  2567.    if (secs>59)
  2568.    {
  2569.       /*min*/
  2570.       mins+=secs/60;
  2571.       secs%=60;
  2572.    }
  2573.    rtc_time->rtc_sec = secs;
  2574.    if (mins>59)
  2575.    {
  2576.       /*hour*/
  2577.       hours+=mins/60;
  2578.       mins%=60;
  2579.    }
  2580.    rtc_time->rtc_min = mins;
  2581.    if (hours>23)
  2582.    {
  2583.       kal_uint16  days;
  2584.       kal_uint8   max_day;
  2585.       /*day of week*/
  2586.       days = hours/24;
  2587.       hours%=24;
  2588.       rtc_time->rtc_wday+=days;
  2589.       if(rtc_time->rtc_wday>7)
  2590.          rtc_time->rtc_wday-=7;
  2591.       /*day of month*/            
  2592.       rtc_time->rtc_day+=days;
  2593.       max_day = days_in_month[rtc_time->rtc_mon];
  2594.       if(rtc_time->rtc_mon==2)
  2595.       {
  2596.        kal_uint32 remender;
  2597.        remender=rtc_time->rtc_year%4;
  2598.        if(remender==0)
  2599.        max_day++;
  2600.       }    
  2601.       if(rtc_time->rtc_day>max_day)
  2602.       {
  2603.          rtc_time->rtc_day-=max_day;
  2604.          
  2605.          /*month of year*/
  2606.          rtc_time->rtc_mon++;
  2607.          if(rtc_time->rtc_mon>12)
  2608.          {
  2609.             rtc_time->rtc_mon=1;
  2610.             rtc_time->rtc_year++;
  2611.          }                                      
  2612.       }   
  2613.    }
  2614.    rtc_time->rtc_hour = hours;
  2615.    
  2616. #if !defined(LOW_COST_SUPPORT)
  2617.    //TIMER_HISR stack only 512 bytes, remove trace
  2618.    //drv_trace4(TRACE_GROUP_10, RTC_FORWARD_TIME_2, rtc_time->rtc_hour, rtc_time->rtc_min, rtc_time->rtc_sec, 0);
  2619. #endif // #if !defined(LOW_COST_SUPPORT)
  2620. }
  2621. /*
  2622. * FUNCTION                                                            
  2623. * rtc_backward_time
  2624. *
  2625. * DESCRIPTION                                                           
  2626. *  Decrease the RTC time by hours, minutes and seconds.
  2627. *
  2628. * CALLS  
  2629. * This function is called internally for RTC SW calibration
  2630. *
  2631. * PARAMETERS
  2632. * rtc_time: current RTC time
  2633. * hours: hours to decrease time
  2634. *  mins: minutes to decrease time
  2635. *  secs: seconds to decrease time
  2636. *
  2637. * RETURNS
  2638. * None
  2639. *
  2640. * GLOBALS AFFECTED
  2641. *  None
  2642. */
  2643. void rtc_backward_time(t_rtc *rtc_time, kal_uint16 hours, kal_uint16 mins, kal_uint16 secs)
  2644. {
  2645.    kal_uint8 day;
  2646. #if !defined(LOW_COST_SUPPORT)
  2647. #if !defined(__FUE__)
  2648.    drv_trace8(TRACE_GROUP_10, RTC_BACKWARD_TIME_1, rtc_time->rtc_hour, rtc_time->rtc_min, rtc_time->rtc_sec, hours, mins, secs, 0, 0);
  2649. #endif
  2650. #endif // #if !defined(LOW_COST_SUPPORT)
  2651.    day = 0;
  2652.    if (secs > rtc_time->rtc_sec)
  2653.    {
  2654.       rtc_time->rtc_sec += 60;
  2655.       mins++;
  2656.    }
  2657.    rtc_time->rtc_sec -= secs;
  2658.    if (mins > rtc_time->rtc_min)
  2659.    {
  2660.       rtc_time->rtc_min += 60;
  2661.       hours++;
  2662.    }
  2663.    rtc_time->rtc_min -= mins;   
  2664.    if (hours > rtc_time->rtc_hour)
  2665.    {
  2666.       rtc_time->rtc_hour += 24;
  2667.       day = 1;
  2668.    }
  2669.    rtc_time->rtc_hour -= hours;   
  2670.    if(day==1)
  2671.    {
  2672.       rtc_time->rtc_wday--;
  2673.       if (rtc_time->rtc_wday == 0)
  2674.          rtc_time->rtc_wday = 7;
  2675.       rtc_time->rtc_day--;
  2676.       if (rtc_time->rtc_day == 0)
  2677.       {
  2678.          rtc_time->rtc_mon--;
  2679.          if (rtc_time->rtc_mon == 0)
  2680.          {
  2681.             rtc_time->rtc_mon = 12;
  2682.             rtc_time->rtc_year--;
  2683.          }
  2684.          rtc_time->rtc_day = days_in_month[rtc_time->rtc_mon];
  2685.        if(rtc_time->rtc_mon==2)
  2686.        {
  2687.        kal_uint32 remender;
  2688.        remender=rtc_time->rtc_year%4;
  2689.        if(remender==0)
  2690.           rtc_time->rtc_day++;
  2691.        }    
  2692.       }
  2693.    }   
  2694. #if !defined(LOW_COST_SUPPORT)
  2695.    drv_trace4(TRACE_GROUP_10, RTC_BACKWARD_TIME_2, rtc_time->rtc_hour, rtc_time->rtc_min, rtc_time->rtc_sec, 0);
  2696. #endif // #if !defined(LOW_COST_SUPPORT)
  2697. }
  2698. /*
  2699. * FUNCTION                                                            
  2700. * rtc_update_time
  2701. *
  2702. * DESCRIPTION                                                           
  2703. *     update time according to time stamp
  2704. *
  2705. * CALLS  
  2706. * This function is to update time according to time stamp
  2707. *
  2708. * PARAMETERS
  2709. * None
  2710. *
  2711. * RETURNS
  2712. * None
  2713. *
  2714. * GLOBALS AFFECTED
  2715. *   external_global
  2716. */
  2717. void rtc_update_time(void)
  2718. {
  2719.    kal_uint32 current_frame_tick, diff_frame_tick;
  2720.    kal_uint32 sec;
  2721.    kal_uint32 tick_left_temp;
  2722.    kal_uint16 one_third_tick_count_temp;
  2723.    //kal_uint32 max_day=0;  
  2724.    #ifdef RTC_CAL_DEBUG
  2725.    t_rtc rtctime;
  2726.    #endif /* RTC_CAL_DEBUG */
  2727.    
  2728.    #if (!defined(__L1_STANDALONE__)) 
  2729.    current_frame_tick=L1I_GetTimeStamp();
  2730.    #endif
  2731.    /* Calculate the different ticks between the current time and expected time for calibration timeout. */
  2732.    if(current_frame_tick>=next_frame_tick) 
  2733.     diff_frame_tick=(current_frame_tick-next_frame_tick)+tick_left;   
  2734.    else
  2735.    {
  2736.     diff_frame_tick=(0xffffffff+current_frame_tick-next_frame_tick+1)+tick_left;   
  2737.       #ifdef RTC_CAL_DEBUG
  2738.       kal_prompt_trace(MOD_RTC_HISR, "KAL time out early, expected:%d (ticks), now:%d rn", next_frame_tick, current_frame_tick);
  2739.       #endif /* RTC_CAL_DEBUG */
  2740.       
  2741. #if !defined(LOW_COST_SUPPORT)
  2742.       drv_trace2(TRACE_GROUP_10, RTC_SW_CALI_KAL_EXPIRE_EARLY, next_frame_tick, current_frame_tick);
  2743. #endif // #if !defined(LOW_COST_SUPPORT)
  2744.    }
  2745.    /* Convert the ticks to minutes. */
  2746.    sec=(diff_frame_tick/rtc_tick_update_interval);
  2747.    sec=sec*rtc_sec_update_interval;
  2748.    /* the 1/3 tick for each second. (217 - 216&2/3)*/
  2749.    one_third_tick_count_temp = one_third_tick_count + sec;
  2750.    tick_left_temp=(diff_frame_tick%rtc_tick_update_interval) + one_third_tick_count_temp/3;
  2751.    /* record the left 1/3 tick count. */
  2752.    one_third_tick_count_temp %=3;
  2753.    /* The below calculation is neccessary because the tick_left_temp may be larger than
  2754.       rtc_tick_update_interval again. */
  2755.    while (tick_left_temp >= rtc_tick_update_interval)
  2756.    {
  2757.       sec += (tick_left_temp/rtc_tick_update_interval);
  2758.       one_third_tick_count_temp += (tick_left_temp/rtc_tick_update_interval);
  2759.       tick_left_temp = ((tick_left_temp%rtc_tick_update_interval)+one_third_tick_count_temp/3);
  2760.       one_third_tick_count_temp %=3;
  2761.    }
  2762.    /* 15 ticks is about 69 ms. */
  2763.    if (tick_left_temp > 15)
  2764.    {
  2765.       /* If the inaccuracy for second boundary is more than 15 ticks, set kal timer to reach the boundary and wait. */
  2766.       kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_timer, NULL, (rtc_tick_update_interval-tick_left_temp+1),0);
  2767.       #ifdef RTC_CAL_DEBUG2
  2768.       kal_prompt_trace(MOD_RTC_HISR, "calibration time out not in the second boundary:%d (ticks) rn", tick_left_temp);
  2769.       #endif /* RTC_CAL_DEBUG */
  2770. #if !defined(LOW_COST_SUPPORT)
  2771.       drv_trace1(TRACE_GROUP_10, RTC_SW_CALI_TIMOUE_NOT_IN_SEC_BOUNDARY, tick_left_temp);
  2772. #endif // #if !defined(LOW_COST_SUPPORT)
  2773.       return;
  2774.    }
  2775.    /* At this moment, it's around the second boundary. So we do the calibration here now. */
  2776.    tick_left = tick_left_temp;
  2777.    one_third_tick_count = one_third_tick_count_temp;
  2778.    tc_min_expire_cnt = 0;
  2779.    /* Plus some ticks to the timer period to guarantee it will expire longer than expected. */
  2780.    kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_timer, NULL, rtc_cali_tick_value+rtc_cali_tick_safe_margin,0);
  2781.    /*update time*/
  2782.    /*sec*/
  2783.    rtc_forward_time(&next_current_time, 0, 0, sec);
  2784.    RTC_Cali_Time(&next_current_time);
  2785.    /* Save current time and frame_tick_1hr_diff to nvram. */
  2786.    rtc_nvram_s.time_valid = KAL_TRUE;
  2787.    rtc_nvram_s.rtc_time = next_current_time;
  2788.    rtc_nvram_s.ticks_diff_valid = KAL_TRUE;
  2789.    rtc_nvram_s.ticks_diff_per_hour = frame_tick_1hr_diff;
  2790.    rtc_write_to_nvram();
  2791.    #ifdef RTC_CAL_DEBUG
  2792.    RTC_GetTime(&rtctime);
  2793.    kal_prompt_trace(MOD_RTC_HISR, "calibration time out(%d:%d:%d):%d rn"
  2794.       ,rtctime.rtc_hour,rtctime.rtc_min,rtctime.rtc_sec, current_frame_tick);
  2795.    kal_prompt_trace(MOD_RTC_HISR, "(update %d:%d:%d,):%d rn"
  2796.    ,next_current_time.rtc_hour,next_current_time.rtc_min,next_current_time.rtc_sec,current_frame_tick);
  2797.    #endif /* RTC_CAL_DEBUG */
  2798.    
  2799.    #ifdef RTC_CAL_DEBUG
  2800. #if !defined(LOW_COST_SUPPORT)
  2801.    drv_trace4(TRACE_GROUP_10, RTC_SW_CALI_TIMOUE_OUT, rtctime.rtc_hour, rtctime.rtc_min, rtctime.rtc_sec, current_frame_tick);
  2802. #endif // #if !defined(LOW_COST_SUPPORT)
  2803.    #endif /* RTC_CAL_DEBUG */
  2804. #if !defined(LOW_COST_SUPPORT)
  2805.    drv_trace4(TRACE_GROUP_10, RTC_SW_CALI_UPDATE_TIME, next_current_time.rtc_hour, next_current_time.rtc_min, next_current_time.rtc_sec, current_frame_tick);
  2806. #endif // #if !defined(LOW_COST_SUPPORT)
  2807.    /* Calculate the next calibration timeout in frame tick and time. */
  2808.    next_frame_tick=rtc_cali_tick_value+current_frame_tick;
  2809.    rtc_get_next_time();      
  2810. }
  2811. /*
  2812. * FUNCTION                                                            
  2813. * rtc_get_next_time
  2814. *
  2815. * DESCRIPTION                                                           
  2816. *     calculate next time out time
  2817. *
  2818. * CALLS  
  2819. * This function is to calculate nexe time out time
  2820. *
  2821. * PARAMETERS
  2822. * None
  2823. *
  2824. * RETURNS
  2825. * None
  2826. *
  2827. * GLOBALS AFFECTED
  2828. *   external_global
  2829. */
  2830. void rtc_get_next_time(void)
  2831. {
  2832.      /*next time*/
  2833.    rtc_forward_time(&next_current_time, rtc_cali_hr_period, rtc_cali_min_period, 0);
  2834. }   
  2835. /*
  2836. * FUNCTION                                                            
  2837. * rtc_poweroff_cali_init
  2838. *
  2839. * DESCRIPTION                                                           
  2840. *  Calculate the last poweroff period of the handset and the RTC inaccuracy between this period.
  2841. *
  2842. * CALLS  
  2843. * This function is called internally in handset power on sequence.
  2844. *
  2845. * PARAMETERS
  2846. * None
  2847. *
  2848. * RETURNS
  2849. * None
  2850. *
  2851. * GLOBALS AFFECTED
  2852. *   external_global
  2853. */
  2854. void rtc_poweroff_cali_init(void)
  2855. {
  2856. kal_uint16 reg_comm2;
  2857. kal_bool   btmp;
  2858.    pwroff_inacc_ticks = 0;
  2859.    btmp = nvram_external_read_data((kal_uint16)NVRAM_EF_RTC_DATA_LID, 1, (kal_uint8 *)&rtc_nvram_s, sizeof(rtc_nvram_data));
  2860.    #ifdef RTC_CAL_DEBUG2
  2861.    kal_prompt_trace(MOD_RTC_HISR, "Get nvram data");
  2862.    kal_prompt_trace(MOD_RTC_HISR, "time valid?: %d", rtc_nvram_s.time_valid);
  2863.    kal_prompt_trace(MOD_RTC_HISR, "%d-%d-%d (%d:%d:%d): ",rtc_nvram_s.rtc_time.rtc_year, rtc_nvram_s.rtc_time.rtc_mon,
  2864.       rtc_nvram_s.rtc_time.rtc_day, rtc_nvram_s.rtc_time.rtc_hour, rtc_nvram_s.rtc_time.rtc_min, rtc_nvram_s.rtc_time.rtc_sec);
  2865.    kal_prompt_trace(MOD_RTC_HISR, "tick diff valide?: %d", rtc_nvram_s.ticks_diff_valid);                                                   
  2866.    kal_prompt_trace(MOD_RTC_HISR, "ticks_diff_per_hour=%d", rtc_nvram_s.ticks_diff_per_hour);                                                   
  2867.    #endif /* RTC_CAL_DEBUG */
  2868. #if !defined(LOW_COST_SUPPORT)
  2869. #if !defined(__FUE__)
  2870.    drv_trace1(TRACE_GROUP_10, RTC_SW_CALI_POWEROFF_CALI_INIT1, rtc_nvram_s.time_valid);
  2871.    drv_trace8(TRACE_GROUP_10, RTC_SW_CALI_POWEROFF_CALI_INIT2, rtc_nvram_s.rtc_time.rtc_year, rtc_nvram_s.rtc_time.rtc_mon,
  2872.       rtc_nvram_s.rtc_time.rtc_day, rtc_nvram_s.rtc_time.rtc_hour, rtc_nvram_s.rtc_time.rtc_min, rtc_nvram_s.rtc_time.rtc_sec, 0, 0);
  2873.    drv_trace1(TRACE_GROUP_10, RTC_SW_CALI_POWEROFF_CALI_INIT3, rtc_nvram_s.ticks_diff_valid);
  2874.    drv_trace1(TRACE_GROUP_10, RTC_SW_CALI_POWEROFF_CALI_INIT3, rtc_nvram_s.ticks_diff_per_hour);
  2875. #endif
  2876. #endif // #if !defined(LOW_COST_SUPPORT)
  2877.    
  2878.    /* RTC_isFirstOn() may be called before in DRV_POWERON(). */
  2879. if ((First_PowerOn==KAL_TRUE) || (KAL_FALSE == RTC_isFisrtOn()))
  2880.    {
  2881.       reg_comm2 = DRV_Reg(DRV_COMM_REG2);
  2882.       /* DRV_COMM_REG2_RTC_NVRAM is to determine if the content of RTC nvram is valid or not. */
  2883.       if ((reg_comm2 & DRV_COMM_REG2_RTC_NVRAM) && (rtc_nvram_s.time_valid == KAL_TRUE) && (rtc_nvram_s.ticks_diff_valid == KAL_TRUE))
  2884.       {
  2885.          kal_uint16 hours, mins, secs;
  2886.          t_rtc time;
  2887.          kal_bool valid;
  2888.          /* Get the RTC current time. */
  2889.          RTC_GetTime(&time);
  2890.          #ifdef RTC_CAL_DEBUG2
  2891.          kal_prompt_trace(MOD_RTC_HISR, "The current power on time: %d-%d-%d (%d:%d:%d): ", time.rtc_year, time.rtc_mon,
  2892.             time.rtc_day, time.rtc_hour, time.rtc_min, time.rtc_sec);
  2893.          #endif /* RTC_CAL_DEBUG */
  2894. #if !defined(LOW_COST_SUPPORT)
  2895. #if !defined(__FUE__)
  2896.          drv_trace8(TRACE_GROUP_10, RTC_SW_CALI_CURR_POWER_TIME, time.rtc_year, time.rtc_mon,
  2897.             time.rtc_day, time.rtc_hour, time.rtc_min, time.rtc_sec, 0, 0);
  2898. #endif
  2899. #endif // #if !defined(LOW_COST_SUPPORT)
  2900.          
  2901.          
  2902.          /* calculate the power off period in hours and minutes. */
  2903.          valid = rtc_get_diff_time(&time, &(rtc_nvram_s.rtc_time), &hours, &mins);
  2904.          #ifdef RTC_CAL_DEBUG2
  2905.          kal_prompt_trace(MOD_RTC_HISR, "poweroff period - valid: %d, hours: %d, minutes: %d", valid, hours, mins);
  2906.          #endif /* RTC_CAL_DEBUG */
  2907. #if !defined(LOW_COST_SUPPORT)
  2908.          drv_trace4(TRACE_GROUP_10, RTC_SW_CALI_POWEROFF_PERIOD_VALID, valid, hours, mins, 0);
  2909. #endif // #if !defined(LOW_COST_SUPPORT)
  2910.          /* Check if these values are reasonable. */
  2911.          if ((valid == KAL_TRUE) && (hours < 7200) 
  2912.             && (rtc_nvram_s.ticks_diff_per_hour > -1300) && (rtc_nvram_s.ticks_diff_per_hour < 1300))
  2913.          {
  2914.             /* calculate the inaccurate difference. */
  2915.             pwroff_inacc_ticks = hours*rtc_nvram_s.ticks_diff_per_hour+ mins*rtc_nvram_s.ticks_diff_per_hour/60;
  2916.             #ifdef RTC_CAL_DEBUG2
  2917.             kal_prompt_trace(MOD_RTC_HISR, "inaccurate ticks during poweroff: %d", pwroff_inacc_ticks);
  2918.             #endif /* RTC_CAL_DEBUG */
  2919. #if !defined(LOW_COST_SUPPORT)
  2920.             drv_trace1(TRACE_GROUP_10, RTC_SW_CALI_INACCURATE_TICK_DURING_POWEROFF, pwroff_inacc_ticks);
  2921. #endif // #if !defined(LOW_COST_SUPPORT)
  2922.             /* Check if it's alarm power on and the time has to be backward. */
  2923.             if ((BMT.PWRon == (kal_uint8)RTCPWRON) && (pwroff_inacc_ticks < 0))
  2924.             {
  2925.                extern void DRV_POWEROFF(void);
  2926.                t_rtc alarm_time;
  2927. #if !defined(LOW_COST_SUPPORT)
  2928.                drv_trace0(TRACE_GROUP_10, RTC_SW_CALI_ALARM_POWERON_AND_NEED_BACKWARD_TIME);
  2929. #endif // #if !defined(LOW_COST_SUPPORT)
  2930.                pwroff_inacc_ticks = -pwroff_inacc_ticks;
  2931.                hours = pwroff_inacc_ticks/(13000*60);
  2932.                pwroff_inacc_ticks = pwroff_inacc_ticks%(13000*60);
  2933.                mins = pwroff_inacc_ticks/13000;
  2934.                pwroff_inacc_ticks = pwroff_inacc_ticks%13000;
  2935.                secs = pwroff_inacc_ticks*3/650;
  2936.                RTC_GetALTime(&alarm_time);
  2937.                /* calculate the correct time */
  2938.                rtc_backward_time(&time, hours, mins, secs);
  2939.                /* Check if the correct time is before the alarm time. */
  2940.                if (KAL_TRUE == rtc_compare_time(&alarm_time, &time))
  2941.                {
  2942.                   #ifdef RTC_CAL_DEBUG2
  2943.                   kal_prompt_trace(MOD_RTC_HISR, "Alarm power on, and power off the phone");
  2944.                   #endif /* RTC_CAL_DEBUG */
  2945. #if !defined(LOW_COST_SUPPORT)
  2946.                   drv_trace0(TRACE_GROUP_10, RTC_SW_CALI_ALARM_POWERON_AND_POWEROFF);
  2947. #endif // #if !defined(LOW_COST_SUPPORT)
  2948.                   
  2949.                   RTC_Cali_Time(&time);
  2950.                   /* invalidate DRV_COMM_REG2_RTC_NVRAM because time has been calibrated. */
  2951.                   reg_comm2 &= ~(DRV_COMM_REG2_RTC_NVRAM);
  2952.                   DRV_WriteReg(DRV_COMM_REG2,reg_comm2);
  2953.                   #if defined(DRV_RTC_HW_CALI)
  2954.                   RTC_write_trigger();
  2955.                   #endif /*DRV_RTC_HW_CALI*/
  2956.                   /* Power off the phone and wait for alarm power on again.*/
  2957.                   DRV_POWEROFF();
  2958.                }
  2959.             }
  2960.          }/* if ((valid == */
  2961.       } /* if((reg & DRV_COMM_REG2_RTC_NVRAM */
  2962.    } /* if((KAL_FALSE == RTC_isFisrtOn() */
  2963. }
  2964. /*
  2965. * FUNCTION                                                            
  2966. * rtc_timer
  2967. *
  2968. * DESCRIPTION                                                           
  2969. *     system timer callback function
  2970. *
  2971. * CALLS  
  2972. * This function is called when calibration timer time out
  2973. *
  2974. * PARAMETERS
  2975. * timer_param
  2976. *
  2977. * RETURNS
  2978. * None
  2979. *
  2980. * GLOBALS AFFECTED
  2981. *   external_global
  2982. */
  2983. void rtc_timer(void *timer_param)
  2984. {
  2985. //   kal_uint32 savedMask;
  2986.    kal_uint8  rtc_sec;
  2987. #if defined(SLEEP_WORKAROUND_IN_RTC)
  2988. #if (!defined(__L1_STANDALONE__)) 
  2989. kal_uint32 current_frame_tick;
  2990. #endif
  2991. #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  2992.    rtc_sec = DRV_Reg(RTC_TC_SEC);
  2993.    if ((rtc_tc_int_var != TC_EN_None) && (rtc_tc_int_var != TC_EN_ChkS))
  2994.    {
  2995.       /* avoid the situation when rtc_sec is around zero for minute interrupt. */
  2996.       if (rtc_sec < 3 || rtc_sec > 57)
  2997.       {
  2998.          #ifdef RTC_CAL_DEBUG
  2999.          kal_prompt_trace(MOD_RTC_HISR, "rtc time is in minute boundary :(%d): calibration delays 6 secs. ", rtc_sec);
  3000.          #endif /* RTC_CAL_DEBUG */
  3001. #if !defined(LOW_COST_SUPPORT)
  3002.          drv_trace1(TRACE_GROUP_10, RTC_SW_CALI_RTC_TIME_IN_MINUTE_BOUNDARY, rtc_sec);
  3003. #endif // #if !defined(LOW_COST_SUPPORT)
  3004.          
  3005.          /* To skip the miniute boundary, delay about 6 secs to do the calibration. */
  3006.          kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_timer, NULL, 1300 ,0);
  3007.          return;
  3008.       }
  3009.    }
  3010. #if defined(SLEEP_WORKAROUND_IN_RTC)
  3011. // Added by GuoXin begin
  3012. // New check case: If KAL timer expired early than the time we expected, 
  3013. // we set KAL timer to 6 sec later
  3014. #if (!defined(__L1_STANDALONE__)) 
  3015. current_frame_tick = L1I_GetTimeStamp();
  3016. /* Calculate the different ticks between the current time and expected time for calibration timeout. */
  3017. if(current_frame_tick < next_frame_tick){
  3018. #ifdef RTC_CAL_DEBUG
  3019. kal_prompt_trace(MOD_RTC_HISR, "(1)KAL time out early, expected:%d (ticks), now:%d rn", next_frame_tick, current_frame_tick);
  3020. #endif /* RTC_CAL_DEBUG */
  3021. kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_timer, NULL, 1300 ,0);
  3022. return;
  3023. }
  3024. #endif // #if (!defined(__L1_STANDALONE__)) 
  3025. // Added by GuoXin end
  3026. #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  3027.    if (rtc_al_int_var != AL_EN_None)
  3028.    {
  3029.       kal_uint8  al_rtc_sec;
  3030.       kal_uint8  sec_diff;
  3031.       al_rtc_sec = DRV_Reg(RTC_AL_SEC);
  3032.       sec_diff = (rtc_sec >= al_rtc_sec) ? (rtc_sec-al_rtc_sec):(al_rtc_sec-rtc_sec);
  3033.       /* avoid the situation when al_rtc_sec is around rtc_sec for alarm interrupt. */
  3034.       if (sec_diff < 3 || sec_diff > 57)
  3035.       {
  3036.          #ifdef RTC_CAL_DEBUG
  3037.          kal_prompt_trace(MOD_RTC_HISR, "rtc time is in alarm boundary :(%d): calibration delays 6 secs. ", rtc_sec);
  3038.          #endif /* RTC_CAL_DEBUG */
  3039. #if !defined(LOW_COST_SUPPORT)
  3040.          drv_trace1(TRACE_GROUP_10, RTC_SW_CALI_RTC_TIME_IN_ALARM_BOUNDARY, rtc_sec);
  3041. #endif // #if !defined(LOW_COST_SUPPORT)
  3042.          
  3043.          /* To skip the alarm boundary, delay about 6 secs to do the calibration. */
  3044.          kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_timer, NULL, 1300 ,0);
  3045.          return;
  3046.       }
  3047.    }
  3048. //   savedMask = SaveAndSetIRQMask();
  3049.    rtc_update_time();      
  3050. //   RestoreIRQMask(savedMask);   
  3051. }  
  3052. #endif /* RTC_SW_CALIBRATION */
  3053. #if defined(RTC_HW_CALIBRATION) || defined(RTC_SW_CALIBRATION)
  3054. /*
  3055. * FUNCTION                                                            
  3056. * rtc_cali_init
  3057. *
  3058. * DESCRIPTION                                                           
  3059. *     Initialize rtc calibration
  3060. *
  3061. * CALLS  
  3062. * This function is called at power on or user configures time.
  3063. *
  3064. * PARAMETERS
  3065. * None
  3066. *
  3067. * RETURNS
  3068. * None
  3069. *
  3070. * GLOBALS AFFECTED
  3071. *   external_global
  3072. */
  3073. void rtc_cali_init(void *timer_param)
  3074. {   
  3075. #if defined(RTC_SW_CALIBRATION)
  3076.    kal_uint32 frame_tick=0;
  3077. #endif /* RTC_SW_CALIBRATION */
  3078.    if (cali_state == (kal_uint8)CAL_STATE_NONE)
  3079.    {
  3080. #ifdef RTC_HW_CALIBRATION
  3081.       /* Change calibration state to init and enable RTC minute interrupt.
  3082.          And wait 'till the RTC minute expired to start calibration. */
  3083.       cali_state = (kal_uint8)CAL_STATE_INIT;
  3084.       if (rtc_tc_int_var != TC_EN_ChkM)
  3085. #elif defined(RTC_SW_CALIBRATION)
  3086.       /* Change calibration state to init and enable RTC second interrupt.
  3087.          And wait 'till the RTC second expired to start calibration. */
  3088.       cali_state = (kal_uint8)CAL_STATE_INIT;
  3089.       if (rtc_tc_int_var != TC_EN_ChkS)
  3090. #endif
  3091.       {
  3092.        kal_uint16 tmp;
  3093.          /**/
  3094.       tmp = DRV_Reg(RTC_IRQ_EN);
  3095.       tmp &=~RTC_IRQ_EN_ONESHOT;
  3096.    tmp |= RTC_IRQ_EN_TC;
  3097.        DRV_WriteReg(RTC_IRQ_EN,tmp);
  3098. #ifdef RTC_HW_CALIBRATION
  3099.    DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_MIN);
  3100.          RTC_write_trigger_wait();
  3101.       }
  3102.       tc_min_expire_cnt = 0;
  3103.       no_diff_cnt = 0;
  3104.       rtc_current_freq_8secs = RTC_STANDARD_FREQ_8SECS + rtc_get_cali_offset();
  3105.       #ifdef RTC_CAL_DEBUG
  3106.       kal_prompt_trace(MOD_RTC_HISR, "Initial: repeat calibration count is %d rn", (rtc_current_freq_8secs-RTC_STANDARD_FREQ_8SECS));
  3107.       #endif /* RTC_CAL_DEBUG */
  3108. #if !defined(LOW_COST_SUPPORT)
  3109.       drv_trace1(TRACE_GROUP_10, RTC_HW_CALI_INIT_REPEAT_CALI_COUNT, (rtc_current_freq_8secs-RTC_STANDARD_FREQ_8SECS));
  3110. #endif // #if !defined(LOW_COST_SUPPORT)
  3111.    }
  3112. #elif defined(RTC_SW_CALIBRATION)
  3113.    DRV_WriteReg(RTC_CII_EN,RTC_CII_EN_SEC);
  3114.       }
  3115.    }
  3116.    else if ((cali_state == (kal_uint8)CAL_STATE_INIT2) || (cali_state == (kal_uint8)CAL_STATE_RESET))
  3117.    {
  3118.       /* next_current_time has already been set before in RTC_InitTC_Time() or RTC_TCintr(). */
  3119.       if (cali_state == (kal_uint8)CAL_STATE_INIT2)
  3120.       {
  3121.          /* Set the adjusted correct time (power off calibration) in this moment. */
  3122.          RTC_Cali_Time(&next_current_time);
  3123.          #ifdef RTC_CAL_DEBUG2
  3124.          kal_prompt_trace(MOD_RTC_HISR, "current time after 2nd poweroff calibration- (%d:%d:%d)",
  3125.             next_current_time.rtc_hour, next_current_time.rtc_min, next_current_time.rtc_sec);
  3126.          #endif /* RTC_CAL_DEBUG */
  3127. #if !defined(LOW_COST_SUPPORT)
  3128.          drv_trace4(TRACE_GROUP_10, RTC_SW_CALI_AFTER_2ND_POWEROFF_CALI, next_current_time.rtc_hour, next_current_time.rtc_min, next_current_time.rtc_sec, 0);
  3129. #endif // #if !defined(LOW_COST_SUPPORT)
  3130.          
  3131.       }
  3132.       /* Start power on calibration by KAL timer. */
  3133.       kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_timer, NULL, rtc_cali_tick_value+rtc_cali_tick_safe_margin,0);
  3134.       #if (!defined(__L1_STANDALONE__)) 
  3135.       frame_tick=L1I_GetTimeStamp();
  3136.       #endif
  3137.       #ifdef RTC_CAL_DEBUG
  3138.       kal_prompt_trace(MOD_RTC_HISR, "Start poweron calibration");
  3139.       kal_prompt_trace(MOD_RTC_HISR, "(%d:%d:%d): ",next_current_time.rtc_hour,next_current_time.rtc_min,next_current_time.rtc_sec);
  3140.       kal_prompt_trace(MOD_RTC_HISR, "ini tick=%drn",frame_tick);                                                   
  3141.       #endif /* RTC_CAL_DEBUG */
  3142. #if !defined(LOW_COST_SUPPORT)
  3143.       drv_trace4(TRACE_GROUP_10, RTC_SW_CALI_POWERON_CALI_START,next_current_time.rtc_hour,next_current_time.rtc_min,next_current_time.rtc_sec, frame_tick);
  3144. #endif // #if !defined(LOW_COST_SUPPORT)
  3145.       
  3146.       tick_left=0;
  3147.       one_third_tick_count = 0;
  3148.       tc_min_expire_cnt = 0;
  3149.       next_frame_tick=frame_tick+rtc_cali_tick_value;         
  3150.       rtc_get_next_time();
  3151.       /* Change to "start" state. */
  3152.       cali_state = (kal_uint8)CAL_STATE_START;
  3153.    }
  3154. #endif /* RTC_SW_CALIBRATION */
  3155. }   
  3156. /*
  3157. * FUNCTION                                                            
  3158. * rtc_sleep_wa_timer_cb
  3159. *
  3160. * DESCRIPTION                                                           
  3161. *  The sleep mode workaround timer callback handler
  3162. *
  3163. * CALLS  
  3164. * This function is called when we are going to perform RTC S/W calibration hourly
  3165. *   When 59 min reaches, we enable kal timer with this function as callback function
  3166. *
  3167. * PARAMETERS
  3168. * None
  3169. *
  3170. * RETURNS
  3171. * None
  3172. *
  3173. * GLOBALS AFFECTED
  3174. *   external_global
  3175. */
  3176. #if defined(SLEEP_WORKAROUND_IN_RTC)
  3177. void rtc_sleep_wa_timer_cb(void *timer_param){
  3178. kal_uint32 rtc_sleep_wa_delay_tick;
  3179. t_rtc current_time;
  3180. // Get reference time of 59th min
  3181. RTC_GetTime(&current_time);
  3182. // Protection check, in case kal timer expires after we finished calibration process:
  3183. // to avoid the following case:
  3184. // calbration process -> enable sleep mode -> kal timer trigger to disable sleep mode
  3185. if ( (tc_min_expire_cnt == 60) ){
  3186. // Check if we expire early, if expire early ==> Re-start kal disable sleep timer
  3187. // We only do this when tc_min_expire_cnt == 60, or means we expire too late
  3188. if (current_time.rtc_sec < 58){
  3189. // Calculate how many ticks we should wait to disable sleep mode
  3190. // We plan to wakeup at 58 sec
  3191. rtc_sleep_wa_delay_tick = 58 - (kal_uint32)current_time.rtc_sec;
  3192. rtc_sleep_wa_delay_tick = (kal_uint32) ((rtc_sleep_wa_delay_tick * 1000 * 60) / 13);
  3193. // Set kal timer in order to disable sleep mode for RTC calibration
  3194. kal_set_timer(rtc_sleep_wa_timer_id, (kal_timer_func_ptr)rtc_sleep_wa_timer_cb, NULL, rtc_sleep_wa_delay_tick,0);
  3195. #ifdef RTC_SLEEP_WA_DEBUG
  3196. kal_prompt_trace(MOD_RTC_HISR, "SleepWA: (%d:%d), Re-start disable sleep timer with tick: %d", 
  3197. current_time.rtc_min, current_time.rtc_sec, rtc_sleep_wa_delay_tick);
  3198. #endif /* RTC_SLEEP_WA_DEBUG */
  3199. return;
  3200. }
  3201. // Disable sleep mode, because we are going to perform RTC calibration in few seconds
  3202. rtc_sleep_wa_disable_sleep_mode();
  3203. rtc_sleep_wa_disable_sleep_performed = KAL_TRUE;
  3204. #ifdef RTC_SLEEP_WA_DEBUG
  3205. kal_prompt_trace(MOD_RTC_HISR, "SleepWA: (%d:%d), Disable sleep mode", 
  3206. current_time.rtc_min, current_time.rtc_sec);
  3207. #endif /* RTC_SLEEP_WA_DEBUG */
  3208. }else{
  3209. #ifdef RTC_SLEEP_WA_DEBUG
  3210. kal_prompt_trace(MOD_RTC_HISR, "SleepWA: (%d:%d), Warning!!, disable sleep timer expired too late", 
  3211. current_time.rtc_min, current_time.rtc_sec);
  3212. #endif /* RTC_SLEEP_WA_DEBUG */
  3213. }
  3214. }
  3215. void rtc_sleep_wa_enable_sleep_mode(){
  3216. L1SM_SleepEnable(rtc_sleep_wa_handle);
  3217. }
  3218. void rtc_sleep_wa_disable_sleep_mode(){
  3219. L1SM_SleepDisable(rtc_sleep_wa_handle);
  3220. }
  3221. #endif // #ifdef SLEEP_WORKAROUND_IN_RTC
  3222. #endif /*defined(RTC_HW_CALIBRATION) || defined(RTC_SW_CALIBRATION)*/
  3223. /*
  3224. * FUNCTION                                                            
  3225. * RTC_start_cali
  3226. *
  3227. * DESCRIPTION                                                           
  3228. *  The entry function of RTC calibration.
  3229. *
  3230. * CALLS  
  3231. * This function is called in bmt_task_init()(in bmt_create.c) because at this moment of the 
  3232. *  function, the file system is ready for accessing nvram data.
  3233. *
  3234. * PARAMETERS
  3235. * None
  3236. *
  3237. * RETURNS
  3238. * None
  3239. *
  3240. * GLOBALS AFFECTED
  3241. *   external_global
  3242. */
  3243. void RTC_start_cali(void)
  3244. {
  3245. #if defined(RTC_SW_CALIBRATION) || defined(RTC_HW_CALIBRATION)
  3246.    #if defined(__USB_ENABLE__) && !defined(__NVRAM_IN_USB_MS__)
  3247.       /* In this compile option case, when it's USB mode powered on we can't do RTC calibration
  3248.          because L1 time stamp won't be updated (L1I_GetTimeStamp()).*/
  3249.       if (KAL_FALSE == INT_USBBoot() )
  3250.       {
  3251.    #endif
  3252.       #if (!defined(__L1_STANDALONE__)) 
  3253.             #if defined(RTC_HW_CALIBRATION)
  3254.                rtc_cali_init(0);
  3255.             #elif defined(RTC_SW_CALIBRATION)
  3256.       rtc_timer_id=kal_create_timer("RTCcal_Timer");
  3257.          rtc_poweroff_cali_init();
  3258.          kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_cali_init, NULL, 1, 0);
  3259.          #endif
  3260.          #endif /* (!defined(__L1_STANDALONE__)) */
  3261. #if defined(SLEEP_WORKAROUND_IN_RTC)
  3262. rtc_sleep_wa_timer_id = kal_create_timer("RTC_sleep_wa_Timer");
  3263. rtc_sleep_wa_handle = L1SM_GetHandle();
  3264. #endif // #if defined(SLEEP_WORKAROUND_IN_RTC)
  3265.    #if defined(__USB_ENABLE__) && !defined(__NVRAM_IN_USB_MS__)
  3266.       }
  3267.    #endif
  3268. #endif /* defined(RTC_SW_CALIBRATION) || defined(RTC_HW_CALIBRATION) */
  3269. }
  3270. /*
  3271. * FUNCTION                                                            
  3272. * RTC_init
  3273. *
  3274. * DESCRIPTION                                                           
  3275. *     RTC initialization function
  3276. *
  3277. * CALLS  
  3278. * This function is initialize RTC software
  3279. *
  3280. * PARAMETERS
  3281. * RTC_TCCallback: time update callback function
  3282. * RTC_ALCallback: alarm callback function
  3283. *
  3284. * RETURNS
  3285. * None
  3286. *
  3287. * GLOBALS AFFECTED
  3288. *   external_global
  3289. */
  3290. void RTC_init(void (*RTC_TCCallback)(void),void (*RTC_ALCallback)(void)) //validate
  3291. {
  3292. /*status = DRV_Reg(RTC_IRQ_STATUS);*/
  3293. RTC_CALLBAC.rtc_tcfunc = RTC_TCCallback;
  3294. RTC_CALLBAC.rtc_alfunc = RTC_ALCallback;
  3295. /**(volatile kal_uint16 *)RTC_IRQ_EN = RTC_IRQ_EN_ONESHOT;*/
  3296. /*DRV_WriteReg(RTC_IRQ_EN,RTC_IRQ_EN_ALLOFF);*/
  3297. if (RTCConfig.hisr == NULL)
  3298. {
  3299.    //RTCConfig.hisr = kal_create_hisr("RTC_HISR",2,512,RTC_HISR,NULL);
  3300.    RTCConfig.hisr=(void*)0x1234;
  3301.    DRV_Register_HISR(DRV_RTC_HISR_ID, RTC_HISR);
  3302. }   
  3303.    /*RTC IRQ configuration*/
  3304.    #ifdef __MULTI_BOOT__
  3305. if (INT_BootMode() == MTK_NORMAL_MODE)
  3306. {
  3307.       IRQ_Register_LISR(IRQ_RTC_CODE, RTC_LISR,"RTC handler");
  3308. IRQSensitivity(IRQ_RTC_CODE,LEVEL_SENSITIVE);
  3309. #if !defined(IC_MODULE_TEST) // Disable RTC interrupt in module test
  3310.       IRQUnmask(IRQ_RTC_CODE);
  3311.       #endif
  3312.    }
  3313.    #else /*__MULTI_BOOT__*/
  3314.    IRQ_Register_LISR(IRQ_RTC_CODE, RTC_LISR,"RTC handler");
  3315.    IRQSensitivity(IRQ_RTC_CODE,LEVEL_SENSITIVE);
  3316. #if !defined(IC_MODULE_TEST) // Disable RTC interrupt in module test
  3317.       IRQUnmask(IRQ_RTC_CODE);
  3318.       #endif
  3319.    #endif   /*__MULTI_BOOT__*/
  3320.    /*config RTC driving current*/
  3321.    /*#if defined(DRV_RTC_XOSC_DEF_8)
  3322.    DRV_WriteReg(RTC_XOSCCAL,0x8);
  3323.    #endif*/
  3324. }
  3325. #if defined(DRV_RTC_HW_CALI)
  3326. /*
  3327. * FUNCTION                                                            
  3328. * RTC_wait_debounce
  3329. *
  3330. * DESCRIPTION                                                           
  3331. *     To wait until RTC is not in debouncing
  3332. *
  3333. * CALLS  
  3334. * This function is called in RTC initialization
  3335. *
  3336. * PARAMETERS
  3337. * None
  3338. *
  3339. * RETURNS
  3340. * None
  3341. *
  3342. * GLOBALS AFFECTED
  3343. *   external_global
  3344. */
  3345. void RTC_wait_debounce(void)
  3346. {
  3347.    kal_uint32 count=0;
  3348.    while(count < 0xffffffff)
  3349.    {
  3350.       if ((RTC_BBPU_DBING & DRV_Reg(RTC_BBPU))==0)
  3351.          break;
  3352.       count++;
  3353.    }
  3354.    #ifdef RTC_CAL_DEBUG
  3355.    kal_prompt_trace(MOD_RTC_HISR, "RTC wait debounce count = %xrn",count);
  3356.    #endif /* RTC_CAL_DEBUG */
  3357. }
  3358. /*
  3359. * FUNCTION                                                            
  3360. * RTC_wait_busy
  3361. *
  3362. * DESCRIPTION                                                           
  3363. *     To wait until the read/write channels between RTC / Core is not busy
  3364. *
  3365. * CALLS  
  3366. * This function is called after RTC register is written and triggered
  3367. *
  3368. * PARAMETERS
  3369. * None
  3370. *
  3371. * RETURNS
  3372. * None
  3373. *
  3374. * GLOBALS AFFECTED
  3375. *   external_global
  3376. */
  3377. void RTC_wait_busy(void)
  3378. {
  3379.    kal_uint32 count=0;
  3380.    while(count < 0xffffffff)
  3381.    {
  3382.       if ((RTC_BBPU_CBUSY & DRV_Reg(RTC_BBPU))==0)
  3383.          break;
  3384.       count++;
  3385.    }
  3386.    #ifdef RTC_CAL_DEBUG
  3387.    kal_prompt_trace(MOD_RTC_HISR, "RTC wait busy count = %xrn",count);
  3388.    #endif /* RTC_CAL_DEBUG */
  3389. }
  3390. /*
  3391. * FUNCTION                                                            
  3392. * RTC_reload
  3393. *
  3394. * DESCRIPTION                                                           
  3395. *     Reload the values from RTC domain to Core domain
  3396. *
  3397. * CALLS  
  3398. * This function can be treated as a debugging function.
  3399. *
  3400. * PARAMETERS
  3401. * None
  3402. *
  3403. * RETURNS
  3404. * None
  3405. *
  3406. * GLOBALS AFFECTED
  3407. *   external_global
  3408. */
  3409. void RTC_reload(void)
  3410. {
  3411.    kal_uint16 reg;
  3412.    
  3413.    reg = DRV_Reg(RTC_BBPU);
  3414.    reg |= (RTC_BBPU_RELOAD|RTC_BBPU_KEY);
  3415. #if defined(DRV_RTC_WRITE_ENABLE)
  3416. reg |= RTC_BBPU_WRITE_EN;
  3417. #endif // #if defined(DRV_RTC_WRITE_ENABLE)
  3418.    DRV_WriteReg(RTC_BBPU, reg);
  3419. }
  3420. /*
  3421. * FUNCTION                                                            
  3422. * RTC_init
  3423. *
  3424. * DESCRIPTION                                                           
  3425. *     RTC hardware initialization function
  3426. *
  3427. * CALLS  
  3428. * This function is initialize RTC HW
  3429. *
  3430. * PARAMETERS
  3431. * None
  3432. *
  3433. * RETURNS
  3434. * None
  3435. *
  3436. * GLOBALS AFFECTED
  3437. *   external_global
  3438. */
  3439. void RTC_HW_Init(void)
  3440. {
  3441. // Wait H/W mechanism to issue reload command automatically at power on phase begin
  3442. RTC_wait_debounce();
  3443. RTC_wait_busy();
  3444.    // Wait H/W mechanism to issue reload command automatically at power on phase end
  3445. //We are afraid H/W mechanism failed ...
  3446.    RTC_reload();
  3447.    RTC_write_trigger_wait();
  3448.    //We are afraid H/W mechanism failed ...
  3449. }
  3450. #endif /* DRV_RTC_HW_CALI */
  3451. #endif /*!defined(__FUE__)*/
  3452. /*
  3453. * FUNCTION                                                            
  3454. * RTC_is_config_valid
  3455. *
  3456. * DESCRIPTION                                                           
  3457. *  Check if RTC time and Alarm are valid. If these
  3458. *  settings are not correct, we treat it as first power on and
  3459. *  reconfigure RTC.
  3460. *
  3461. * CALLS  
  3462. * This function is to check if RTC time and Alarm are valid.
  3463. *
  3464. * PARAMETERS
  3465. * valid or not
  3466. *
  3467. * RETURNS
  3468. * None
  3469. *
  3470. * GLOBALS AFFECTED
  3471. *   external_global
  3472. */
  3473. kal_bool RTC_is_config_valid(void)
  3474. {
  3475.    t_rtc time, alarm;
  3476.    
  3477.    RTC_GetTime(&time);
  3478.    RTC_GetALTime(&alarm);
  3479.    
  3480.    if((time.rtc_sec>59) ||(alarm.rtc_sec>59)||
  3481.       (time.rtc_min>59) ||(alarm.rtc_min>59)||
  3482.       (time.rtc_hour>23)||(alarm.rtc_hour>23)   ||
  3483.       (time.rtc_day>31) ||(alarm.rtc_day>31) || (time.rtc_day == 0) ||(alarm.rtc_day == 0) || 
  3484.       (time.rtc_wday>7) ||(alarm.rtc_wday>7) || (time.rtc_wday== 0) ||(alarm.rtc_wday== 0) || 
  3485.       (time.rtc_mon>12) ||(alarm.rtc_mon>12) || (time.rtc_mon == 0) ||(alarm.rtc_mon == 0) )
  3486.    {   
  3487. #if !defined(LOW_COST_SUPPORT)
  3488. #if !defined(__FUE__)
  3489.       drv_trace8(TRACE_GROUP_9, RTC_NOT_CONFIG_VALID_TIME, time.rtc_year, time.rtc_mon,
  3490.             time.rtc_day, time.rtc_hour, time.rtc_min, time.rtc_sec, 0, 0);
  3491.       drv_trace8(TRACE_GROUP_9, RTC_NOT_CONFIG_VALID_ALARM, alarm.rtc_year, alarm.rtc_mon,
  3492.             alarm.rtc_day, alarm.rtc_hour, alarm.rtc_min, alarm.rtc_sec, 0, 0);
  3493. #endif
  3494. #endif
  3495.       alarm.rtc_sec=0;
  3496.       alarm.rtc_min=0;
  3497.       alarm.rtc_hour=0;
  3498.       alarm.rtc_day=1;
  3499.       alarm.rtc_wday=1;
  3500.       alarm.rtc_mon=1;
  3501.       RTC_SetAlarm(&alarm);
  3502.       return KAL_FALSE;      
  3503. }
  3504. #if !defined(LOW_COST_SUPPORT)
  3505. #if !defined(__FUE__)
  3506.    drv_trace8(TRACE_GROUP_9, RTC_IS_CONFIG_VALID_TIME, time.rtc_year, time.rtc_mon,
  3507.             time.rtc_day, time.rtc_hour, time.rtc_min, time.rtc_sec, 0, 0);
  3508.    drv_trace8(TRACE_GROUP_9, RTC_IS_CONFIG_VALID_ALARM, alarm.rtc_year, alarm.rtc_mon,
  3509.             alarm.rtc_day, alarm.rtc_hour, alarm.rtc_min, alarm.rtc_sec, 0, 0);
  3510. #endif
  3511. #endif
  3512.    return KAL_TRUE;
  3513. }   
  3514. /*
  3515. * FUNCTION                                                            
  3516. * RTC_read_xosc_reg
  3517. *
  3518. * DESCRIPTION                                                           
  3519. *  Read RTC XOSC register value
  3520. *
  3521. * CALLS  
  3522. * This function is for enginner mode.
  3523. *
  3524. * PARAMETERS
  3525. *  None
  3526. *
  3527. * RETURNS
  3528. * RTC XOSC register value
  3529. *
  3530. * GLOBALS AFFECTED
  3531. *   external_global
  3532. */
  3533. kal_uint8 RTC_read_xosc_reg(void)
  3534. {
  3535.    kal_uint16   value;
  3536.    value = DRV_Reg(RTC_XOSCCAL);
  3537.    return ((kal_uint8)value);
  3538. }
  3539. /*
  3540. * FUNCTION                                                            
  3541. * RTC_write_xosc_reg
  3542. *
  3543. * DESCRIPTION                                                           
  3544. *  Set RTC XOSC register value.
  3545. *
  3546. * CALLS  
  3547. * This function is for enginner mode.
  3548. *
  3549. * PARAMETERS
  3550. *  RTC XOSC register value to set.
  3551. *
  3552. * RETURNS
  3553. * None
  3554. *
  3555. * GLOBALS AFFECTED
  3556. *   external_global
  3557. */
  3558. void RTC_write_xosc_reg(kal_uint8 reg_value)
  3559. {
  3560.    DRV_WriteReg(RTC_XOSCCAL, ((kal_uint16)reg_value));
  3561. #if defined(DRV_RTC_HW_CALI)
  3562.    RTC_write_trigger();
  3563. #endif
  3564. }
  3565. #endif /*DRV_RTC_NOT_EXIST*/