L4Drv.C
上传用户:lqx1163
上传日期:2014-08-13
资源大小:9183k
文件大小:33k
源码类别:

MTK

开发平台:

C/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.  * L4Drv.C
  40.  *
  41.  * Project:
  42.  * --------
  43.  *   MAUI
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *   This file is intends for Timer/Clock control.
  48.  *
  49.  * Author:
  50.  * -------
  51.  * -------
  52.  *
  53.  *============================================================================
  54.  *             HISTORY
  55.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  56.  *------------------------------------------------------------------------------
  57.  * removed!
  58.  *
  59.  * removed!
  60.  * removed!
  61.  * removed!
  62.  *
  63.  * removed!
  64.  * removed!
  65.  * removed!
  66.  * removed!
  67.  *
  68.  * removed!
  69.  * removed!
  70.  * removed!
  71.  *
  72.  * removed!
  73.  * removed!
  74.  * removed!
  75.  *
  76.  * removed!
  77.  * removed!
  78.  * removed!
  79.  *
  80.  * removed!
  81.  * removed!
  82.  * removed!
  83.  *
  84.  * removed!
  85.  * removed!
  86.  * removed!
  87.  *
  88.  * removed!
  89.  * removed!
  90.  * removed!
  91.  *
  92.  * removed!
  93.  * removed!
  94.  * removed!
  95.  * removed!
  96.  *
  97.  * removed!
  98.  * removed!
  99.  * removed!
  100.  *
  101.  * removed!
  102.  * removed!
  103.  * removed!
  104.  *
  105.  * removed!
  106.  * removed!
  107.  * removed!
  108.  *
  109.  * removed!
  110.  * removed!
  111.  * removed!
  112.  *
  113.  * removed!
  114.  * removed!
  115.  * removed!
  116.  *
  117.  * removed!
  118.  * removed!
  119.  * removed!
  120.  *
  121.  * removed!
  122.  * removed!
  123.  * removed!
  124.  *
  125.  * removed!
  126.  * removed!
  127.  * removed!
  128.  *
  129.  * removed!
  130.  * removed!
  131.  * removed!
  132.  *
  133.  * removed!
  134.  * removed!
  135.  * removed!
  136.  *
  137.  * removed!
  138.  * removed!
  139.  * removed!
  140.  *
  141.  * removed!
  142.  * removed!
  143.  * removed!
  144.  *
  145.  * removed!
  146.  * removed!
  147.  * removed!
  148.  *
  149.  * removed!
  150.  * removed!
  151.  * removed!
  152.  *
  153.  * removed!
  154.  * removed!
  155.  * removed!
  156.  *
  157.  * removed!
  158.  * removed!
  159.  * removed!
  160.  *
  161.  * removed!
  162.  * removed!
  163.  * removed!
  164.  *
  165.  * removed!
  166.  * removed!
  167.  * removed!
  168.  *
  169.  * removed!
  170.  * removed!
  171.  * removed!
  172.  *
  173.  *------------------------------------------------------------------------------
  174.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  175.  *============================================================================
  176.  ****************************************************************************/
  177. /*******************************************************************************
  178.  * Copyright Notice
  179.  * ?2002 - 2003, Pixtel Communications, Inc., 1489 43rd Ave. W.,
  180.  * Vancouver, B.C. V6M 4K8 Canada. All Rights Reserved.
  181.  *  (It is illegal to remove this copyright notice from this software or any
  182.  *  portion of it)
  183. *******************************************************************************/
  184. /*******************************************************************************
  185.  *
  186.  * Filename:
  187.  * ---------
  188.  * L4Drv.c
  189.  *
  190.  * Project:
  191.  * --------
  192.  *   MediaTeck GSM
  193.  *
  194.  * Description:
  195.  * ------------
  196.  *   This Module defines the l4 driver routines.
  197.  *
  198.  * Author:
  199.  * -------
  200.  * -------
  201.  *             
  202.  *
  203.  * HISTORY
  204.  * -------
  205.  * Created On 13 Dec
  206. *******************************************************************************/
  207. /* ------- Include Files ----------------------------- */
  208. #ifndef MMI_ON_WIN32
  209. /*  Include: MMI header file */
  210. #include "MMI_features.h"
  211. #include "stdC.h"
  212. #include "L4Dr1.h"
  213. #include "PixtelDataTypes.h"
  214. /* ... Add More MMI header */
  215. #include "globalconstants.h"
  216. #include "QueueGprot.h"
  217. #include "OslMemory.h"
  218. /* amit Audio L4 integration header file */
  219. #include "AudioInc.h"
  220. #include "TimerEvents.h"
  221. #include "TaskInit.h"
  222. #include "KeyBrd.h"
  223. #include "DebugInitDef.h"
  224. #include "l4dr.h"
  225. #include "MMI_trc.h"
  226. #include "gpioInc.h"
  227. #if defined(__MMI_TOUCH_SCREEN__) || defined(__MMI_HANDWRITING_PAD__)
  228. #include "TouchScreenGprot.h"
  229. #endif 
  230. #include "MMITaskProt.h"
  231. /***************************************************************************** 
  232. * Define
  233. *****************************************************************************/
  234. /* the recommended value: 12 or 24 */
  235. #define SIMULTANEOUS_TIMER_NUM         (12)
  236. /* MSB(Most Significant Bit) of g_timer_table.timer_id[i] is align_timer_mask */
  237. #define NO_ALIGNMENT_TIMER_MASK        (0x8000)
  238. #define ALIGNMENT_TIMER_MASK           (0)
  239. /* ticks of 20 millisecond. because KAL did not define the valye. define in floating falue to avoid early rouning */
  240. #define MMI_TICKS_20_MSEC  (KAL_TICKS_1_SEC/50.0)
  241. /* ticks of 5 millisecond. because KAL did not define the valye. define in floating falue to avoid early rouning */
  242. #define MMI_TICKS_5_MSEC  (KAL_TICKS_1_SEC/200.0)
  243. /***************************************************************************** 
  244. * Typedef 
  245. *****************************************************************************/
  246. typedef void (*L4FuncPtr) (oslParaType *);
  247. typedef struct
  248. {
  249.     U16 eventID;
  250.     L4FuncPtr entryFuncPtr;
  251. } L4eventInfo;
  252. /* 
  253.  * MTK added for two kinds of timer, one is for exactly time, another allow to delay.
  254.  * TO Pixtel, This two defines should move to a proper place where 
  255.  * can be used by other files.
  256.  */
  257. typedef struct timertable
  258. {
  259.     /* store the timer_id. MSB(Most Significant Bit) is align_timer_mask */
  260.     U16 timer_id[SIMULTANEOUS_TIMER_NUM];
  261.     /* store the event_id that returns from evshed_set_event() */
  262.     eventid event_id[SIMULTANEOUS_TIMER_NUM];
  263.     /* store the timer_expiry_func */
  264.     oslTimerFuncPtr callback_func[SIMULTANEOUS_TIMER_NUM];
  265.     /* point to the next TIMERTABLE data */
  266.     struct timertable *next;
  267. } TIMERTABLE;
  268. /***************************************************************************** 
  269. * Local Variable
  270. *****************************************************************************/
  271. /*
  272.  * In most case, the timers won't run simultaneously.
  273.  * We run-time allocate the memory to store the timer information. 
  274.  */
  275. static TIMERTABLE g_timer_table = {0};
  276. static U16 g_timer_table_size = 0;
  277. static U16 g_timer_table_used = 0;
  278. static oslTaskFuncPtr get_clocktime_callback_func;
  279. static oslTaskFuncPtr set_clocktime_callback_func;
  280. /***************************************************************************** 
  281. * Local Function
  282. *****************************************************************************/
  283. //#define __MMI_FRM_TIMER_UNIT_TEST__
  284. #ifdef __MMI_FRM_TIMER_UNIT_TEST__
  285. static void mmi_frm_ut_timer();
  286. static void mmi_frm_ut_align_timer(void *p);
  287. static void mmi_frm_ut_nonalign_timer(void *p);
  288. #endif /* __MMI_FRM_TIMER_UNIT_TEST__ */
  289. /***************************************************************************** 
  290. * Global Variable
  291. *****************************************************************************/
  292. extern stack_timer_struct base_timer1;
  293. extern stack_timer_struct base_timer2;
  294. extern event_scheduler *event_scheduler1_ptr, *event_scheduler2_ptr;
  295. /***************************************************************************** 
  296. * Global Function
  297. *****************************************************************************/
  298. /*****************************************************************************
  299.  * FUNCTION
  300.  *  L4SetClock
  301.  * DESCRIPTION
  302.  *  This function is to set the clock request.
  303.  * PARAMETERS
  304.  *  datetime        [IN]        
  305.  *  FuncRspPtr      [IN]        
  306.  *  a(?)            [IN]        Void
  307.  * RETURNS
  308.  *  void
  309.  *****************************************************************************/
  310. void L4SetClock(rtc_format_struct datetime, oslTaskFuncPtr FuncRspPtr)
  311. {
  312.     /*----------------------------------------------------------------*/
  313.     /* Local Variables                                                */
  314.     /*----------------------------------------------------------------*/
  315.     ilm_struct *ilm_ptr = NULL;
  316.     mmi_eq_set_rtc_time_req_struct *local_data;
  317.     /*----------------------------------------------------------------*/
  318.     /* Code Body                                                      */
  319.     /*----------------------------------------------------------------*/
  320.     /* store the set time clock callback function */
  321.     set_clocktime_callback_func = FuncRspPtr;
  322.     ilm_ptr = allocate_ilm(MOD_MMI);
  323.     ilm_ptr->msg_id = (U16) MSG_ID_MMI_EQ_SET_RTC_TIME_REQ;
  324.     local_data = (mmi_eq_set_rtc_time_req_struct*)
  325.         construct_local_para(sizeof(mmi_eq_set_rtc_time_req_struct), TD_CTRL);
  326.     local_data->set_type = RTC_SETTING_TYPE_DATETIME;
  327.     local_data->rtc_type = RTC_TIME_CLOCK_IND;
  328.     local_data->info.alarm_format = 0;
  329.     local_data->info.alarm_index = 0;
  330.     local_data->info.type = 0;
  331.     local_data->info.text[0] = '';
  332.     local_data->info.recurr = 0;
  333.     local_data->info.data_time.rtc_year = datetime.rtc_year;
  334.     local_data->info.data_time.rtc_wday = datetime.rtc_wday;
  335.     local_data->info.data_time.rtc_mon = datetime.rtc_mon;
  336.     local_data->info.data_time.rtc_day = datetime.rtc_day;
  337.     local_data->info.data_time.rtc_hour = datetime.rtc_hour;
  338.     local_data->info.data_time.rtc_min = datetime.rtc_min;
  339.     local_data->info.data_time.rtc_sec = datetime.rtc_sec;
  340.     ilm_ptr->local_para_ptr = (local_para_struct*) local_data;
  341.     ilm_ptr->peer_buff_ptr = NULL;
  342.     SEND_ILM(MOD_MMI, MOD_L4C, MMI_L4C_SAP, ilm_ptr);
  343. }
  344. /*****************************************************************************
  345.  * FUNCTION
  346.  *  L4SetClockRSP
  347.  * DESCRIPTION
  348.  *  This function is to get to the result of clock set response.
  349.  * PARAMETERS
  350.  *  buf         [?]         
  351.  *  a(?)        [IN]        Buf
  352.  * RETURNS
  353.  *  void
  354.  *****************************************************************************/
  355. void L4SetClockRSP(void *buf)
  356. {
  357.     /*----------------------------------------------------------------*/
  358.     /* Local Variables                                                */
  359.     /*----------------------------------------------------------------*/
  360.     /*----------------------------------------------------------------*/
  361.     /* Code Body                                                      */
  362.     /*----------------------------------------------------------------*/
  363.     if (set_clocktime_callback_func != NULL)
  364.     {
  365.         set_clocktime_callback_func(buf);
  366.     }
  367. }
  368. /*****************************************************************************
  369.  * FUNCTION
  370.  *  L4GetClockTime
  371.  * DESCRIPTION
  372.  *  This function is to request to get the clock.
  373.  * PARAMETERS
  374.  *  FuncRspPtr      [IN]        
  375.  *  a(?)            [IN]        Void
  376.  * RETURNS
  377.  *  void
  378.  *****************************************************************************/
  379. void L4GetClockTime(oslTaskFuncPtr FuncRspPtr)
  380. {
  381.     /*----------------------------------------------------------------*/
  382.     /* Local Variables                                                */
  383.     /*----------------------------------------------------------------*/
  384.     ilm_struct *ilm_ptr = NULL;
  385.     mmi_eq_get_rtc_time_req_struct *local_data;
  386.     /*----------------------------------------------------------------*/
  387.     /* Code Body                                                      */
  388.     /*----------------------------------------------------------------*/
  389.     /* store the get time clock callback function */
  390.     get_clocktime_callback_func = FuncRspPtr;
  391.     ilm_ptr = allocate_ilm(MOD_MMI);
  392.     local_data = (mmi_eq_get_rtc_time_req_struct*)
  393.         construct_local_para(sizeof(mmi_eq_get_rtc_time_req_struct), TD_CTRL);
  394.     local_data->rtc_type = RTC_TIME_CLOCK_IND;
  395.     ilm_ptr->msg_id = (U16) MSG_ID_MMI_EQ_GET_RTC_TIME_REQ;
  396.     ilm_ptr->local_para_ptr = (local_para_struct*) local_data;
  397.     ilm_ptr->peer_buff_ptr = NULL;
  398.     SEND_ILM(MOD_MMI, MOD_L4C, MMI_L4C_SAP, ilm_ptr);
  399. }
  400. /*****************************************************************************
  401.  * FUNCTION
  402.  *  L4GetClockTimeRSP
  403.  * DESCRIPTION
  404.  *  This function is to receive the clock response.
  405.  * PARAMETERS
  406.  *  buf         [?]         
  407.  *  a(?)        [IN]        Buf
  408.  * RETURNS
  409.  *  void
  410.  *****************************************************************************/
  411. void L4GetClockTimeRSP(void *buf)
  412. {
  413.     /*----------------------------------------------------------------*/
  414.     /* Local Variables                                                */
  415.     /*----------------------------------------------------------------*/
  416.     /*----------------------------------------------------------------*/
  417.     /* Code Body                                                      */
  418.     /*----------------------------------------------------------------*/
  419.     if (get_clocktime_callback_func != NULL)
  420.     {
  421.         get_clocktime_callback_func(buf);
  422.     }
  423. }
  424. /*****************************************************************************
  425.  * FUNCTION
  426.  *  L4InitTimer
  427.  * DESCRIPTION
  428.  *  This function is to init the timer while task create.
  429.  * PARAMETERS
  430.  *  void
  431.  *  a(?)        [IN]        Void
  432.  * RETURNS
  433.  *  void
  434.  *****************************************************************************/
  435. void L4InitTimer(void)
  436. {
  437.     /*----------------------------------------------------------------*/
  438.     /* Local Variables                                                */
  439.     /*----------------------------------------------------------------*/
  440.     TIMERTABLE *p;
  441.     TIMERTABLE *pp;
  442.     /*----------------------------------------------------------------*/
  443.     /* Code Body                                                      */
  444.     /*----------------------------------------------------------------*/
  445.     /* Try to free TIMERTABLE list exclude g_timer_table */
  446.     p = g_timer_table.next;
  447.     pp = NULL;
  448.     do
  449.     {
  450.         if (p != NULL)
  451.         {
  452.             pp = p->next;
  453.             OslMfree(p);
  454.         }
  455.         p = pp;
  456.     } while (p != NULL);
  457.     /* reset g_timer_talbe */
  458.     memset(&g_timer_table, 0, sizeof(TIMERTABLE));
  459.     g_timer_table_size = SIMULTANEOUS_TIMER_NUM;
  460.     g_timer_table_used = 0;
  461.     /* Initiate the clock time callback function. */
  462.     get_clocktime_callback_func = NULL;
  463.     set_clocktime_callback_func = NULL;
  464.     /* Initate the no alignment stack timer */
  465.     stack_init_timer(&base_timer1, "MMI_Base_Timer1", MOD_MMI);
  466.     /* Create a no alignment timer schedule */
  467.     event_scheduler1_ptr = new_evshed(
  468.                             &base_timer1,
  469.                             L4StartBaseTimer,
  470.                             L4StopBaseTimer,
  471.                             0,
  472.                             kal_evshed_get_mem,
  473.                             kal_evshed_free_mem,
  474.                             0);
  475.     /* Initate the alignment stack timer */
  476.     stack_init_timer(&base_timer2, "MMI_Base_Timer2", MOD_MMI);
  477.     /* Create an alignment timer schedule */
  478.     event_scheduler2_ptr = new_evshed(
  479.                             &base_timer2,
  480.                             L4StartBaseTimer,
  481.                             L4StopBaseTimer,
  482.                             0,
  483.                             kal_evshed_get_mem,
  484.                             kal_evshed_free_mem,
  485.                             254);
  486.     #ifdef __MMI_FRM_TIMER_UNIT_TEST__   
  487.     mmi_frm_ut_timer();
  488.     #endif /* __MMI_FRM_TIMER_UNIT_TEST__ */
  489. }
  490. /*****************************************************************************
  491.  * FUNCTION
  492.  *  L4StopBaseTimer
  493.  * DESCRIPTION
  494.  *  This function is to assign the stop timer base.
  495.  * PARAMETERS
  496.  *  base_timer_ptr      [?]         
  497.  *  a(?)                [IN]        Base_timer_ptr
  498.  * RETURNS
  499.  *  void
  500.  *****************************************************************************/
  501. void L4StopBaseTimer(void *base_timer_ptr)
  502. {
  503.     /*----------------------------------------------------------------*/
  504.     /* Local Variables                                                */
  505.     /*----------------------------------------------------------------*/
  506.     /*----------------------------------------------------------------*/
  507.     /* Code Body                                                      */
  508.     /*----------------------------------------------------------------*/
  509.     stack_stop_timer((stack_timer_struct*) base_timer_ptr);
  510. }
  511. /*****************************************************************************
  512.  * FUNCTION
  513.  *  L4StartBaseTimer
  514.  * DESCRIPTION
  515.  *  This function is to assign the start timer base.
  516.  * PARAMETERS
  517.  *  base_timer_ptr      [?]         
  518.  *  time_out            [IN]        
  519.  *  a(?)                [IN]        Base_timer_ptr
  520.  *  b(?)                [IN]        Time_out
  521.  * RETURNS
  522.  *  void
  523.  *****************************************************************************/
  524. void L4StartBaseTimer(void *base_timer_ptr, unsigned int time_out)
  525. {
  526.     /*----------------------------------------------------------------*/
  527.     /* Local Variables                                                */
  528.     /*----------------------------------------------------------------*/
  529.     /*----------------------------------------------------------------*/
  530.     /* Code Body                                                      */
  531.     /*----------------------------------------------------------------*/
  532.     stack_start_timer((stack_timer_struct*) base_timer_ptr, 0, time_out);
  533. }
  534. /*****************************************************************************
  535.  * FUNCTION
  536.  *  L4CallBackTimer
  537.  * DESCRIPTION
  538.  *  This function is to execute the timer expire.
  539.  * PARAMETERS
  540.  *  p           [?]         
  541.  *  a(?)        [IN]        P
  542.  * RETURNS
  543.  *  void
  544.  *****************************************************************************/
  545. void L4CallBackTimer(void *p)
  546. {
  547.     /*----------------------------------------------------------------*/
  548.     /* Local Variables                                                */
  549.     /*----------------------------------------------------------------*/
  550.     U32 nTimerId = (U32) p;
  551.     TIMERTABLE *pTable = &g_timer_table;
  552.     U32 i = 0;
  553.     oslTimerFuncPtr pTimerExpiry = NULL;
  554.     /*----------------------------------------------------------------*/
  555.     /* Code Body                                                      */
  556.     /*----------------------------------------------------------------*/
  557.     MMI_ASSERT(nTimerId < MAX_TIMERS);
  558.     /* find the nTimerId in TIMERTABLE list */
  559.     do
  560.     {
  561.         /* MSB(Most Significant Bit) of timer_id[i] is align_timer_mask */
  562.         if ((pTable->timer_id[i] & (~NO_ALIGNMENT_TIMER_MASK)) == (U16) nTimerId)
  563.         {
  564.             /* find out nTimerId */
  565.             if (pTable->callback_func[i] != NULL)
  566.             {
  567.                 pTimerExpiry = pTable->callback_func[i];
  568.                 MMI_TRACE((MMI_TRACE_G1_FRM, MMI_FRM_INFO_L4DRV_CBTIMER_HDLR, nTimerId, pTimerExpiry));
  569.                 g_timer_table_used--;
  570.                 pTable->event_id[i] = 0;
  571.                 pTable->timer_id[i] = 0;
  572.                 pTable->callback_func[i] = NULL;
  573.                 /*
  574.                  * we process g_timer_table_used, event_id and timer_id ... first 
  575.                  * because the user may call stoptimer() in the timer_expiry_func
  576.                  */
  577.                 pTimerExpiry(p);
  578.             }
  579.             break;
  580.         }
  581.         i++;
  582.         if (i >= SIMULTANEOUS_TIMER_NUM)
  583.         {
  584.             pTable = pTable->next;
  585.             i = 0;
  586.         }
  587.     } while (pTable != NULL);
  588.     /* can't find nTimerId, do nothing */
  589.     mmi_frm_fetch_msg_from_extQ_to_circularQ();
  590.     /* 
  591.      * Because we modify MMI process protocol events and key events mechanism,
  592.      * we don't need to process key events here.
  593.      */
  594. }
  595. /*****************************************************************************
  596.  * FUNCTION
  597.  *  L4TimerUsePreciseTick
  598.  * DESCRIPTION
  599.  *  Typically we round timer period to multiple of 100ms. However, some timers need to be
  600.  *  more accurate.
  601.  * PARAMETERS
  602.  *  nTimerId        [IN]        Timer ID
  603.  * RETURNS
  604.  *  void
  605.  *****************************************************************************/
  606. MMI_BOOL L4TimerUsePreciseTick(unsigned short nTimerId)
  607. {
  608.     /*----------------------------------------------------------------*/
  609.     /* Local Variables                                                */
  610.     /*----------------------------------------------------------------*/
  611.     /*----------------------------------------------------------------*/
  612.     /* Code Body                                                      */
  613.     /*----------------------------------------------------------------*/
  614.     switch (nTimerId)
  615.     {
  616.     #if defined(__MMI_TOUCH_SCREEN__) || defined(__MMI_HANDWRITING_PAD__)
  617.         case PEN_POLLING_TIMER:
  618.             return MMI_TRUE;
  619.     #endif /* defined(__MMI_TOUCH_SCREEN__) || defined(__MMI_HANDWRITING_PAD__) */ 
  620.         default:
  621.             return MMI_FALSE;
  622.     }
  623. }
  624. /*****************************************************************************
  625.  * FUNCTION
  626.  *  L4StartTimer
  627.  * DESCRIPTION
  628.  *  This function is to start timer. To added for two kinds of timer, one is for exactly time, another allow to delay.
  629.  * PARAMETERS
  630.  *  nTimerId            [IN]        
  631.  *  TimerExpiry         [IN]        
  632.  *  funcArg             [?]         
  633.  *  nTimeDuration       [IN]        
  634.  *  alignment           [IN]        
  635.  *  e(?)                [IN]        Alignment
  636.  *  a(?)                [IN]        NTimerId
  637.  *  d(?)                [IN]        NTimeDuration
  638.  *  c(?)                [IN]        FuncArg
  639.  *  b(?)                [IN]        TimerExpiry
  640.  * RETURNS
  641.  *  void
  642.  *****************************************************************************/
  643. void L4StartTimer(
  644.         unsigned short nTimerId,
  645.         oslTimerFuncPtr TimerExpiry,
  646.         void *funcArg,
  647.         unsigned long nTimeDuration,
  648.         unsigned char alignment)
  649. {
  650.     /*----------------------------------------------------------------*/
  651.     /* Local Variables                                                */
  652.     /*----------------------------------------------------------------*/
  653.     TIMERTABLE *pTable = NULL;
  654.     U16 i = 0;
  655.     U32 temp;
  656.     /*----------------------------------------------------------------*/
  657.     /* Code Body                                                      */
  658.     /*----------------------------------------------------------------*/
  659.     if (TimerExpiry == NULL)
  660.     {   /* If TimerExpiry is NULL, we don't start the timer */
  661.         MMI_ASSERT(0);
  662.         return;
  663.     }
  664.     MMI_ASSERT(nTimerId < MAX_TIMERS);
  665.     if (L4TimerUsePreciseTick(nTimerId))
  666.     {
  667.         temp = (nTimeDuration * KAL_TICKS_10_MSEC) / 10;
  668.         if (temp == 0)
  669.         {
  670.             temp = KAL_TICKS_10_MSEC;
  671.         }
  672.         alignment = TIMER_IS_NO_ALIGNMENT;
  673.     }
  674.     else
  675.     {
  676.         if (nTimeDuration == 1000)
  677.         {
  678.             temp = KAL_TICKS_1_SEC - 4;
  679.         }
  680.         else
  681.         {
  682.             temp = (U32)((nTimeDuration / 5) * MMI_TICKS_5_MSEC);
  683.         }
  684.         if (temp == 0)
  685.         {
  686.             /* Cause by by rounding. If expire immediately, MoDIS boot-up failure because MMI keeps running and block NVRAM task */
  687.             temp = (U32)MMI_TICKS_5_MSEC;
  688.         }
  689.     }   /* if (L4TimerUsePreciseTick(nTimerId)) */
  690.     MMI_TRACE((MMI_TRACE_G1_FRM, MMI_FRM_INFO_L4DRV_STARTTIMER_HDLR, nTimerId, TimerExpiry, temp, alignment));
  691.     pTable = &g_timer_table;
  692.     if (g_timer_table_used >= g_timer_table_size)
  693.     {
  694.         /*
  695.          * TIMERTABLE list doesn't have enough space, allocate the memory 
  696.          *
  697.          * If we need to allocate the memeory, it means that MMI may have 
  698.          * such many timers run simultaneously. We won't free the memory 
  699.          * after we allocate more memory in TIMERTABLE list.
  700.          */
  701.         do
  702.         {
  703.             if (pTable->next == NULL)
  704.             {
  705.                 pTable->next = OslMalloc(sizeof(TIMERTABLE));
  706.                 memset(pTable->next, 0, sizeof(TIMERTABLE));
  707.                 g_timer_table_size += SIMULTANEOUS_TIMER_NUM;
  708.                 pTable = pTable->next;
  709.                 i = 0;
  710.                 break;
  711.             }
  712.             pTable = pTable->next;
  713.         } while (pTable != NULL);
  714.     }
  715.     else
  716.     {
  717.         /* find the empty record in g_timer_table list */
  718.         i = 0;
  719.         do
  720.         {
  721.             if (pTable->event_id[i] == NULL)
  722.             {   /* find the empty space */
  723.                 break;
  724.             }
  725.             i++;
  726.             if (i >= SIMULTANEOUS_TIMER_NUM)
  727.             {
  728.                 pTable = pTable->next;
  729.                 i = 0;
  730.             }
  731.         } while (pTable != NULL);
  732.         if (pTable == NULL)
  733.         {
  734.             /* Can't find the empty space in TIMERTABLE list, assert!!! */
  735.             MMI_ASSERT(0);
  736.         }
  737.     }   /* if (g_timer_table_used >= g_timer_table_size) */
  738.     /* 
  739.      * already find the empty record, and then start timer 
  740.      *
  741.      * event_sheduler1 = NO alignment scherulder
  742.      * event_sheduler2 = alignment scherulder (low power)
  743.      */
  744.     if (alignment == TIMER_IS_NO_ALIGNMENT)
  745.     {
  746.         /* MSB(Most Significant Bit) is align_timer_mask */
  747.         pTable->timer_id[i] = nTimerId | NO_ALIGNMENT_TIMER_MASK;
  748.         pTable->event_id[i] = evshed_set_event(
  749.                                 event_scheduler1_ptr,
  750.                                 (kal_timer_func_ptr) L4CallBackTimer,
  751.                                 (void*)nTimerId,
  752.                                 temp);
  753.         pTable->callback_func[i] = TimerExpiry;
  754.         g_timer_table_used++;
  755.     }
  756.     else if (alignment == TIMER_IS_ALIGNMENT)
  757.     {
  758.         /* MSB(Most Significant Bit) is align_timer_mask */
  759.         pTable->timer_id[i] = nTimerId | ALIGNMENT_TIMER_MASK;
  760.         pTable->event_id[i] = evshed_set_event(
  761.                                 event_scheduler2_ptr,
  762.                                 (kal_timer_func_ptr) L4CallBackTimer,
  763.                                 (void*)nTimerId,
  764.                                 temp);
  765.         pTable->callback_func[i] = TimerExpiry;
  766.         g_timer_table_used++;
  767.     }
  768. }
  769. /*****************************************************************************
  770.  * FUNCTION
  771.  *  L4StopTimer
  772.  * DESCRIPTION
  773.  *  This function is to stop timer. To added for two kinds of timer, one is for exactly time, another allow to delay.
  774.  * PARAMETERS
  775.  *  nTimerId        [IN]        
  776.  *  a(?)            [IN]        NTimerId
  777.  * RETURNS
  778.  *  void
  779.  *****************************************************************************/
  780. void L4StopTimer(unsigned short nTimerId)
  781. {
  782.     /*----------------------------------------------------------------*/
  783.     /* Local Variables                                                */
  784.     /*----------------------------------------------------------------*/
  785.     TIMERTABLE *pTable = &g_timer_table;
  786.     U16 i = 0;
  787.     /*----------------------------------------------------------------*/
  788.     /* Code Body                                                      */
  789.     /*----------------------------------------------------------------*/
  790.     MMI_ASSERT(nTimerId < MAX_TIMERS);
  791.     MMI_TRACE((MMI_TRACE_G1_FRM, MMI_FRM_INFO_L4DRV_STOPTIMER_HDLR, nTimerId));
  792.     /* find the nTimerId in TIMERTABLE list */
  793.     do
  794.     {
  795.         /* MSB(Most Significant Bit) of timer_id[i] is align_timer_mask */
  796.         if ((pTable->timer_id[i] & (~NO_ALIGNMENT_TIMER_MASK)) == nTimerId)
  797.         {
  798.             /* find the nTimerId */
  799.             if (pTable->event_id[i] != NULL)
  800.             {
  801.                 if ((pTable->timer_id[i] & NO_ALIGNMENT_TIMER_MASK) == NO_ALIGNMENT_TIMER_MASK)
  802.                 {   /* NO_ALIGNMENT_TIMER */
  803.                     evshed_cancel_event(event_scheduler1_ptr, &(pTable->event_id[i]));
  804.                 }
  805.                 else
  806.                 {   /* ALIGNMENT_TIMER */
  807.                     evshed_cancel_event(event_scheduler2_ptr, &(pTable->event_id[i]));
  808.                 }
  809.                 g_timer_table_used--;
  810.                 pTable->event_id[i] = NULL;
  811.                 pTable->timer_id[i] = 0;
  812.                 pTable->callback_func[i] = NULL;
  813.             }
  814.             break;
  815.         }
  816.         i++;
  817.         if (i >= SIMULTANEOUS_TIMER_NUM)
  818.         {
  819.             pTable = pTable->next;
  820.             i = 0;
  821.         }
  822.     } while (pTable != NULL);
  823.     /* if can't find nTimerId, do nothing */
  824. }
  825. /*****************************************************************************
  826.  * FUNCTION
  827.  *  mmi_frm_is_align_base_timer
  828.  * DESCRIPTION
  829.  *  is the given base timer an alignment one
  830.  * PARAMETERS
  831.  *  base_timer_ptr        [IN]        base timer pointer
  832.  * RETURNS
  833.  *  IMER_IS_ALIGNMENT (MMI_TRUE) or TIMER_IS_NO_ALIGNMENT (MMI_FALSE)
  834.  *****************************************************************************/
  835. MMI_BOOL mmi_frm_is_align_base_timer(void *base_timer_ptr)
  836. {
  837.     if ( base_timer_ptr == (void *)&base_timer2 )
  838.         return MMI_TRUE;
  839.     else
  840.         return MMI_FALSE;
  841. }
  842. /***************************************************************************** 
  843. * Local Function
  844. *****************************************************************************/
  845. #ifdef __MMI_FRM_TIMER_UNIT_TEST__
  846. #define ALIGN_TEST_TIMEOUT      1000   /* ms */
  847. #define NON_ALIGN_TEST_TIMEOUT  1100   /* ms */
  848. #define ALIGN_TEST_TM_ID        MMI_TIMER_BASE
  849. #define NON_ALIGN_TEST_TM_ID    KEY_TIMER_ID_NONE
  850. /*****************************************************************************
  851.  * FUNCTION
  852.  *  mmi_frm_ut_timer
  853.  * DESCRIPTION
  854.  *  timer unit-test
  855.  * PARAMETERS
  856.  *  none
  857.  * RETURNS
  858.  *  none
  859.  *****************************************************************************/
  860. static void mmi_frm_ut_timer()
  861. {
  862.     /* unit test for non-alignment timer in NVRAM access */
  863.     L4StartTimer(NON_ALIGN_TEST_TM_ID, (oslTimerFuncPtr)mmi_frm_ut_nonalign_timer, 
  864.         (void *)NON_ALIGN_TEST_TM_ID, NON_ALIGN_TEST_TIMEOUT, TIMER_IS_NO_ALIGNMENT);
  865.     
  866.     /* unit test for alignment timer in NVRAM access */
  867.     L4StartTimer(ALIGN_TEST_TM_ID, (oslTimerFuncPtr)mmi_frm_ut_align_timer, 
  868.         (void *)ALIGN_TEST_TM_ID, ALIGN_TEST_TIMEOUT, TIMER_IS_ALIGNMENT);
  869. }
  870. /*****************************************************************************
  871.  * FUNCTION
  872.  *  mmi_frm_ut_nonalign_timer
  873.  * DESCRIPTION
  874.  *  timer callback for non-align timer unit test
  875.  * PARAMETERS
  876.  *  timer id
  877.  * RETURNS
  878.  *  none
  879.  *****************************************************************************/
  880. static void mmi_frm_ut_nonalign_timer(void *p)
  881. {
  882.     if (IsInNVRAMProcedure())
  883.     {
  884.         Trace2(TRACE_GROUP_1, "[UT] non-align timer works in nvram access");
  885.         //kal_print("n[UT] non-align timer works in nvram accessn");
  886.     }
  887.     else
  888.     {
  889.         L4StartTimer(NON_ALIGN_TEST_TM_ID, (oslTimerFuncPtr)mmi_frm_ut_nonalign_timer, 
  890.             (void *)NON_ALIGN_TEST_TM_ID, NON_ALIGN_TEST_TIMEOUT, TIMER_IS_NO_ALIGNMENT);
  891.     }    
  892. }
  893. /*****************************************************************************
  894.  * FUNCTION
  895.  *  mmi_frm_ut_align_timer
  896.  * DESCRIPTION
  897.  *  timer callback for align timer unit test
  898.  * PARAMETERS
  899.  *  timer id
  900.  * RETURNS
  901.  *  none
  902.  *****************************************************************************/
  903. static void mmi_frm_ut_align_timer(void *p)
  904. {
  905.     if (IsInNVRAMProcedure())
  906.     {
  907.         Trace2(TRACE_GROUP_1, "[UT] align timer will die in nvram access");
  908.         MMI_ASSERT(0);
  909.     }
  910.     else
  911.     {
  912.         L4StartTimer(ALIGN_TEST_TM_ID, (oslTimerFuncPtr)mmi_frm_ut_align_timer, 
  913.             (void *)ALIGN_TEST_TM_ID, ALIGN_TEST_TIMEOUT, TIMER_IS_ALIGNMENT);
  914.     }    
  915. }
  916. #endif /* __MMI_FRM_TIMER_UNIT_TEST__ */
  917. #endif /* MMI_ON_WIN32 */