dmd.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:14k
源码类别:

DVD

开发平台:

C/C++

  1. #include <string.h>
  2. #include "errors.h"
  3. #include "i2c.h"
  4. #include "osp.h"
  5. #include "timer.h"
  6. #include "dmd.h"
  7. #include "db.h"
  8. #include "sipsi.h"
  9. #include "tcmu30311.h"
  10. #include "Nvod.h"
  11. #include "Epg.h"
  12. #define DMD_INITIAL_TIMEOUT  (3000)   /* 3s acquisition time out   */
  13. #define DMD_RECOVER_TIMEOUT  (9000)  
  14. /****************************************************************************
  15. ** Local function prototypes
  16. ****************************************************************************/
  17. static  void    DMDAcquisitionTask(void);
  18. static  UINT32  DMDHandleConnectedState(UINT32 *);
  19. static  UINT32  DMDHandleDroppedState(UINT32 *);
  20. static  UINT32  DMDHandleWaitingState(UINT32 *);
  21. static  void    DMDHandleTimeout(void);
  22. typedef struct
  23. {
  24. KB_DMDType type; /* e.g. satellite (QPSK), terrestrial (COFDM) */
  25. UINT16 address; /* I2C bus address */
  26. UINT16 present; /* TRUE / FALSE */
  27. INT32 (*detect)(UINT16 addr);
  28. void (*initialize)(KB_DMDTunerParameters *pParam);
  29. INT32 (*connect)(void);
  30. void (*drop)(void);
  31. void (*process_int)(void);
  32. INT32 (*check_lock)(void);
  33. UINT32 (*read_signal_strength)(void);
  34. INT32  (*read_ber)(UINT32 *ber);
  35. INT32  (*read_snr)(UINT32 *snr);
  36. } T_NIM_Type;
  37. static T_NIM_Type supportedNIMs[] =
  38. {
  39. /* cable : STV 0297 NIMs */
  40. {
  41. DMD_TYPE_QAM,
  42. IIC_ADDR_NIM_30311,
  43. FALSE,
  44. SAMDetect,
  45. SAMInit,
  46. SAMStartConnect,
  47. SAMStopConnect,
  48. NULL,
  49. SAMCheckLock,
  50. NULL,
  51. NULL,
  52. NULL
  53. },
  54. {
  55. DMD_TYPE_NONE, 0x00, FALSE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  56. }
  57. };
  58. static T_NIM_Type       *NIM;
  59. static UINT32 DMDQueueID;       /* Message queue for Acquisition task */
  60. static UINT32 AcquisitionTimer;
  61. static INT32 gLockFlag = 0;
  62. /***************************************************************************
  63. ** These are the Acquisition Task message ID assignments 
  64. ***************************************************************************/
  65. #define CONNECT_REQUEST_MSG_ID  (1) /* connect request message id     */
  66. #define DROP_REQUEST_MSG_ID     (2) /* drop request message id        */
  67. #define TIMEOUT_MSG_ID          (3) /* time out connecting message id */
  68. #define NIM_INT_MSG_ID          (4) /* NIM interrupt message id       */
  69. #define INT_LOCK_MSG_ID         (5) /* lock interrupt message id      */
  70. #define INT_LOST_LOCK_MSG_ID    (6) /* lost lock interrupt message id */
  71. /************************************************************************
  72. ** Acquisition task state definitions
  73. *************************************************************************/
  74. #define DEMOD_ERROR     (0) /* Demod error */
  75. #define DEMOD_DROPPED   (1) /* Signal is dropped. Idle state    */
  76. #define DEMOD_WAITING   (2) /* Acquisition process has started. */
  77. #define DEMOD_CONNECTED (3) /* Signal successfully acquired     */
  78. /*
  79. ** All publicly available functions that have their location
  80. ** modified have their initial location as a dummy function
  81. */
  82. static void dummyFunction(KB_DMDCallbackEvents a){ (void)(a);}
  83. static KB_DMD_CallbackFunc DMDCallbackFunction = dummyFunction;
  84. extern KB_DMDTunerParameters *KB_NVOD_GetTsParaFromTsSequence(UINT32  TsSequence);
  85. extern STB_State KB_GetCurState(void);
  86. INT32 KB_DMDInit(void)
  87. {
  88. UINT32          taskid;
  89. T_NIM_Type      *availableNIM = &(supportedNIMs[0]);
  90. INT32           nim_types = DMD_TYPE_NONE;
  91. /* Find any supported NIMs */
  92. while(availableNIM->type != DMD_TYPE_NONE)
  93.     {
  94. /* Mark available if the NIM is found */
  95. nim_types = availableNIM->detect( availableNIM->address );
  96. if(nim_types != DMD_TYPE_NONE)
  97.         {
  98. availableNIM->present = TRUE;
  99. break;
  100.         }
  101. availableNIM++;
  102.     }
  103. /* Provide a dummy handler for the application's callback */
  104. DMDCallbackFunction = dummyFunction;
  105. if(nim_types != DMD_TYPE_NONE )
  106.     {
  107. /*
  108. ** Create the message queue and the 
  109. ** task used to control the NIM
  110. */
  111. KB_OSPQueInit("DMDQ", 16, &DMDQueueID);
  112. KB_OSPTaskInit("DMDT", 0x2000, (void(*)(void *))DMDAcquisitionTask, 
  113.                          DMD_PRIORITY, NULL, &taskid);
  114. /*
  115. ** Initialize the first demod found
  116. */
  117. availableNIM->initialize(&CurrentSignal);
  118. NIM = availableNIM;
  119.     }
  120. return nim_types;
  121. }
  122. static void DMDAcquisitionTask(void)
  123. {
  124. UINT32 message[4];  
  125. INT32  rc;
  126. UINT32 state   = DEMOD_DROPPED;
  127. AcquisitionTimer = KB_TimerCreate(KB_TIMER_ONCE,
  128.     (KB_TIMER_FUNC_POINTER)DMDHandleTimeout, NULL);
  129. while(1)
  130.     {
  131. rc = KB_OSPMsgGet(DMDQueueID, KB_Wait, 0, (KB_OSPMsgNode*)message);
  132. if(rc != Ret_OK)                                     
  133.         {
  134. continue;
  135.         }                         
  136. switch (state)
  137.         {
  138.         case DEMOD_WAITING:
  139. state = DMDHandleWaitingState(message);
  140. break;
  141.         //其他模块仅发信号连接消息过来, message的第一个参数设置为1(CONNECT_REQUEST_MSG_ID)
  142.         //信号断开的直接调用KB_DMDDropSignal--->NIM->drop();
  143.         case DEMOD_DROPPED:
  144. state = DMDHandleDroppedState(message);
  145. break;
  146.         case DEMOD_CONNECTED:
  147. state = DMDHandleConnectedState(message);
  148. break;
  149.         case DEMOD_ERROR:
  150. break;
  151.         default:
  152. break;
  153.         }
  154. if (state == DEMOD_CONNECTED)
  155. {
  156. gLockFlag = TRUE;
  157. }
  158. else
  159. {
  160. gLockFlag = FALSE;
  161.         }
  162.     }
  163. }
  164. static UINT32 DMDHandleConnectedState(UINT32 *message)
  165. {
  166. UINT32 newState = DEMOD_CONNECTED;
  167. switch (message[0])
  168.     {
  169.     case DROP_REQUEST_MSG_ID:
  170. newState = DEMOD_DROPPED;
  171. if( NIM != NULL ) NIM->drop();
  172. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_DROPPED);
  173. break;
  174.     case INT_LOST_LOCK_MSG_ID:
  175. newState = DEMOD_WAITING;
  176. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
  177. /* Reset the acquisition timer */
  178. KB_TimerDisable(AcquisitionTimer);
  179. KB_TimerEnable(AcquisitionTimer, DMD_RECOVER_TIMEOUT);
  180. break;
  181. case CONNECT_REQUEST_MSG_ID:
  182.        newState = DEMOD_WAITING;
  183. KB_TimerDisable(AcquisitionTimer);
  184. KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
  185. if( (NIM != NULL) && (TRUE == NIM->connect()) )
  186. {
  187. newState = DEMOD_CONNECTED;
  188. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
  189. }
  190. else
  191. {
  192. newState = DEMOD_DROPPED;
  193. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
  194. }
  195.        break;
  196. case TIMEOUT_MSG_ID:
  197. KB_TimerDisable(AcquisitionTimer);
  198. if( (NIM != NULL) && (NIM->check_lock != NULL)
  199.      && (TRUE == NIM->check_lock()) )
  200. {
  201.     }
  202. else
  203. {
  204. newState = DEMOD_DROPPED;
  205. if( NIM != NULL) NIM->drop();
  206. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
  207. {
  208. UINT32 msg[4];
  209. msg[0] = CONNECT_REQUEST_MSG_ID;
  210. KB_OSPMsgSend(DMDQueueID, (KB_OSPMsgNode*)msg);
  211. }
  212. }
  213. KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
  214. break;
  215.     default:
  216. break;
  217.     }
  218. return(newState);
  219. }
  220. static UINT32 DMDHandleDroppedState(UINT32 *message)
  221. {
  222. UINT32 newState = DEMOD_DROPPED;
  223. switch (message[0])
  224.     {
  225. case INT_LOCK_MSG_ID:
  226. newState = DEMOD_CONNECTED;
  227. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
  228. break;
  229.     case CONNECT_REQUEST_MSG_ID:
  230. newState = DEMOD_WAITING;
  231. KB_TimerDisable(AcquisitionTimer);
  232. KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
  233. if( (NIM != NULL) && (TRUE == NIM->connect()) )
  234. {
  235. newState = DEMOD_CONNECTED;
  236. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
  237. }
  238. else
  239. {
  240. newState = DEMOD_DROPPED;
  241. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
  242. }
  243. break;
  244.     case DROP_REQUEST_MSG_ID:
  245. newState = DEMOD_DROPPED;
  246. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_DROPPED);
  247. break;
  248. case TIMEOUT_MSG_ID:
  249. KB_TimerDisable(AcquisitionTimer);
  250. if( (NIM != NULL) && (NIM->check_lock != NULL)
  251.      && (TRUE == NIM->check_lock()) )
  252. {
  253.      newState = DEMOD_CONNECTED;
  254. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
  255.     }
  256. else
  257. {
  258. UINT32 msg[4];
  259. msg[0] = CONNECT_REQUEST_MSG_ID;
  260. KB_OSPMsgSend(DMDQueueID, (KB_OSPMsgNode*)msg);
  261. }
  262. KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
  263. break;
  264.     default:
  265. break;
  266.     }
  267. return(newState);
  268. }
  269. static UINT32 DMDHandleWaitingState(UINT32 *message)
  270. {
  271. UINT32 newState = DEMOD_WAITING;
  272. switch (message[0])
  273.     {
  274.     case INT_LOCK_MSG_ID:
  275. newState = DEMOD_CONNECTED;
  276. KB_TimerDisable(AcquisitionTimer);
  277. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
  278. break;          
  279.     case TIMEOUT_MSG_ID:
  280. KB_TimerDisable(AcquisitionTimer);
  281. if( (NIM != NULL) && (NIM->check_lock != NULL)
  282.      && (TRUE == NIM->check_lock()) )
  283. {
  284.      newState = DEMOD_CONNECTED;
  285. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
  286.     }
  287. else
  288. {
  289. newState = DEMOD_DROPPED;
  290. if( NIM != NULL) NIM->drop();
  291. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
  292. }
  293. KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
  294. break;
  295.     case DROP_REQUEST_MSG_ID:
  296. KB_TimerDisable(AcquisitionTimer);
  297. newState = DEMOD_DROPPED;
  298. if( NIM != NULL) NIM->drop();
  299. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_DROPPED);
  300. break;
  301.     case CONNECT_REQUEST_MSG_ID:
  302. newState = DEMOD_WAITING;
  303. KB_TimerDisable(AcquisitionTimer);
  304. KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
  305. if( (NIM != NULL) && (TRUE == NIM->connect()) )
  306. {
  307. newState = DEMOD_CONNECTED;
  308. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
  309. }
  310. else
  311. {
  312. newState = DEMOD_DROPPED;
  313. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
  314. }
  315. break;
  316.     default:
  317. break;
  318.     }
  319. return(newState);
  320. }
  321. static void DMDHandleTimeout(void)
  322. {
  323. UINT32 message[4];
  324. message[0] = TIMEOUT_MSG_ID;
  325. KB_OSPMsgSend(DMDQueueID, (KB_OSPMsgNode*)message);
  326. }
  327. INT32 KB_DMDConnectSignal(KB_DMDTunerParameters *dmdParam)
  328. {
  329.     UINT32 CurrentTsTsSequence;
  330.     UINT16 TsID;
  331.     unsigned short Fre;
  332.     unsigned short Symbl;
  333.     unsigned char  Qam;
  334. UINT32 message[4];
  335.     KB_DMDTunerParameters    ts;
  336.    if( (NIM == NULL) || (dmdParam == NULL) || (dmdParam->type != NIM->type) )
  337.     {
  338.      return RETFIAL1;
  339.     }
  340.     DBGPrint("[j_dmd.c] %d %d %dn", dmdParam->frequency, dmdParam->details.qam.symbolRatekSs,
  341.                dmdParam->details.qam.modulation);
  342. if( (0 == memcmp(&CurrentSignal, dmdParam, sizeof(KB_DMDTunerParameters))) && (NIM->check_lock() == TRUE) )
  343. {
  344.         if (State_NVOD == KB_GetCurState())
  345.         {
  346.             if (0 == dmdParam->frequency)
  347.             {
  348.                 printf("nKB_DMDConnectSignal::serious error. fre = 0!");
  349.                 CurrentTsTsSequence = KB_NVOD_GetCurrentTsNum();
  350.                 if ((UINT32)(-1) != CurrentTsTsSequence 
  351.                     && NULL != KB_NVOD_GetTsParaFromTsSequence(CurrentTsTsSequence))
  352.                 {
  353.                     KB_DMDConnectSignal(KB_NVOD_GetTsParaFromTsSequence(CurrentTsTsSequence));
  354.                     return RETOK;
  355.                 }
  356.             }
  357.             
  358.             if (KB_DB_DTV_SUCCESS != KB_NVODInit2())
  359.             {
  360.                 printf("nKB_DMDConnectSignal::KB_NVODInit2 failed!n");
  361.             }
  362.             else
  363.             {
  364.                 printf("nKB_DMDConnectSignal::KB_NVODInit2 ok!n");
  365.             }
  366.         }
  367. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
  368. return RETOK;
  369. }
  370. CurrentSignal = *dmdParam;
  371.       
  372.     if (State_NVOD == KB_GetCurState())
  373.     {
  374.         if (0 == dmdParam->frequency)
  375.         {
  376.             printf("nKB_DMDConnectSignal::serious error. fre = 0!");
  377.             CurrentTsTsSequence = KB_NVOD_GetCurrentTsNum();
  378.             if ((UINT32)(-1) != CurrentTsTsSequence 
  379.                 && NULL != KB_NVOD_GetTsParaFromTsSequence(CurrentTsTsSequence))
  380.             {
  381.                 KB_DMDConnectSignal(KB_NVOD_GetTsParaFromTsSequence(CurrentTsTsSequence));
  382.                 return RETOK;
  383.             }
  384.         }
  385.         
  386.         if (KB_DB_DTV_SUCCESS != KB_NVODInit2())
  387.         {
  388.             printf("nKB_DMDConnectSignal::KB_NVODInit2 failed!n");
  389.         }
  390.         else
  391.         {
  392.             printf("nKB_DMDConnectSignal::KB_NVODInit2 ok!n");
  393.         }
  394.     }
  395.     if (State_EPG == KB_GetCurState())
  396.     {
  397.         if (0 == dmdParam->frequency)
  398.         {
  399.             printf("nKB_DMDConnectSignal::serious error. fre = 0!");
  400.             CurrentTsTsSequence = KB_EPG_GetCurrentTsNum();
  401.             
  402.             if ((UINT32)(-1) != CurrentTsTsSequence)
  403.             {
  404.                 TsID = (UINT16)KB_EPG_GetTsIdFromTsNum(CurrentTsTsSequence);
  405.                 
  406.                 if ((UINT32)(-1) != TsID 
  407.                     && RET_OK == KB_DBGetChnlInfoByTsID(TsID, &Fre, &Symbl, &Qam))
  408.                 {
  409.                     KB_DMDDropSignal();
  410.                     ts.type      = DMD_TYPE_QAM;
  411.                     ts.inversion = INVERSION_AUTO;
  412.                     ts.frequency = (UINT32)Fre * 100;
  413.                     ts.details.qam.symbolRatekSs = Symbl;
  414.                     ts.details.qam.modulation    = Qam;
  415.                     KB_DMDConnectSignal(&ts);
  416.                     printf("nKB_DMDConnectSignal::ts(%d) rechange to %d!n",
  417.                            dmdParam->frequency, ts.frequency);
  418.                 }
  419.             }
  420.             return RETOK;
  421.         }
  422.         else
  423.         {
  424.             if (RET_OK == KB_DBGetTsIDByFre(&TsID,CurrentSignal.frequency))
  425.             {
  426.                 CurrentTsTsSequence = KB_EPG_GetCurrentTsNum();
  427.                 if ((UINT32)(-1) != CurrentTsTsSequence 
  428.                      && TsID != KB_EPG_GetTsIdFromTsNum(CurrentTsTsSequence))
  429.                 {
  430.                     printf("nKB_DMDConnectSignal::CurrentTs change!n");
  431.                     KB_SIStopEitSCH(KB_SI_STOP_ALL);
  432.                     KB_EPG_SetCurrentTsNum(TsID);
  433.                 }
  434.             }
  435.         }
  436.     }
  437.       
  438.     message[0] = CONNECT_REQUEST_MSG_ID;
  439. KB_OSPMsgSend(DMDQueueID, (KB_OSPMsgNode*)message);
  440. return RETOK;
  441. }
  442. INT32 KB_DMDDropSignal(void)
  443. {
  444. if( NIM != NULL ) 
  445. {
  446. NIM->drop();
  447. }
  448. DMDCallbackFunction(DMD_CALLBACK_SIGNAL_DROPPED);
  449. return RETOK;
  450. }
  451. INT32 KB_DMDQuerySignalStatus(void)
  452. {
  453. return gLockFlag;
  454. }
  455. INT32 KB_DMDGetSignalParameters(KB_DMDTunerParameters *ptParameters)
  456. {
  457.     if (ptParameters == NULL)
  458.     {
  459.         return RETFAIL3;
  460.     }
  461.     
  462.     *ptParameters = CurrentSignal;
  463.     return RETOK;
  464. }
  465. void KB_DMDCallBack(KB_DMD_CallbackFunc callback)
  466. {
  467. DMDCallbackFunction = callback; /* Register function in function table */
  468. }
  469. /* EOF --------------------------------------------------------------------- */