dmd.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:14k
- #include <string.h>
- #include "errors.h"
- #include "i2c.h"
- #include "osp.h"
- #include "timer.h"
- #include "dmd.h"
- #include "db.h"
- #include "sipsi.h"
- #include "tcmu30311.h"
- #include "Nvod.h"
- #include "Epg.h"
- #define DMD_INITIAL_TIMEOUT (3000) /* 3s acquisition time out */
- #define DMD_RECOVER_TIMEOUT (9000)
- /****************************************************************************
- ** Local function prototypes
- ****************************************************************************/
- static void DMDAcquisitionTask(void);
- static UINT32 DMDHandleConnectedState(UINT32 *);
- static UINT32 DMDHandleDroppedState(UINT32 *);
- static UINT32 DMDHandleWaitingState(UINT32 *);
- static void DMDHandleTimeout(void);
- typedef struct
- {
- KB_DMDType type; /* e.g. satellite (QPSK), terrestrial (COFDM) */
- UINT16 address; /* I2C bus address */
- UINT16 present; /* TRUE / FALSE */
- INT32 (*detect)(UINT16 addr);
- void (*initialize)(KB_DMDTunerParameters *pParam);
- INT32 (*connect)(void);
- void (*drop)(void);
- void (*process_int)(void);
- INT32 (*check_lock)(void);
- UINT32 (*read_signal_strength)(void);
- INT32 (*read_ber)(UINT32 *ber);
- INT32 (*read_snr)(UINT32 *snr);
- } T_NIM_Type;
- static T_NIM_Type supportedNIMs[] =
- {
- /* cable : STV 0297 NIMs */
- {
- DMD_TYPE_QAM,
- IIC_ADDR_NIM_30311,
- FALSE,
- SAMDetect,
- SAMInit,
- SAMStartConnect,
- SAMStopConnect,
- NULL,
- SAMCheckLock,
- NULL,
- NULL,
- NULL
- },
-
- {
- DMD_TYPE_NONE, 0x00, FALSE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
- }
- };
- static T_NIM_Type *NIM;
- static UINT32 DMDQueueID; /* Message queue for Acquisition task */
- static UINT32 AcquisitionTimer;
- static INT32 gLockFlag = 0;
- /***************************************************************************
- ** These are the Acquisition Task message ID assignments
- ***************************************************************************/
- #define CONNECT_REQUEST_MSG_ID (1) /* connect request message id */
- #define DROP_REQUEST_MSG_ID (2) /* drop request message id */
- #define TIMEOUT_MSG_ID (3) /* time out connecting message id */
- #define NIM_INT_MSG_ID (4) /* NIM interrupt message id */
- #define INT_LOCK_MSG_ID (5) /* lock interrupt message id */
- #define INT_LOST_LOCK_MSG_ID (6) /* lost lock interrupt message id */
- /************************************************************************
- ** Acquisition task state definitions
- *************************************************************************/
- #define DEMOD_ERROR (0) /* Demod error */
- #define DEMOD_DROPPED (1) /* Signal is dropped. Idle state */
- #define DEMOD_WAITING (2) /* Acquisition process has started. */
- #define DEMOD_CONNECTED (3) /* Signal successfully acquired */
- /*
- ** All publicly available functions that have their location
- ** modified have their initial location as a dummy function
- */
- static void dummyFunction(KB_DMDCallbackEvents a){ (void)(a);}
- static KB_DMD_CallbackFunc DMDCallbackFunction = dummyFunction;
- extern KB_DMDTunerParameters *KB_NVOD_GetTsParaFromTsSequence(UINT32 TsSequence);
- extern STB_State KB_GetCurState(void);
- INT32 KB_DMDInit(void)
- {
- UINT32 taskid;
- T_NIM_Type *availableNIM = &(supportedNIMs[0]);
- INT32 nim_types = DMD_TYPE_NONE;
-
- /* Find any supported NIMs */
- while(availableNIM->type != DMD_TYPE_NONE)
- {
- /* Mark available if the NIM is found */
- nim_types = availableNIM->detect( availableNIM->address );
-
- if(nim_types != DMD_TYPE_NONE)
- {
- availableNIM->present = TRUE;
- break;
- }
- availableNIM++;
- }
-
- /* Provide a dummy handler for the application's callback */
- DMDCallbackFunction = dummyFunction;
-
- if(nim_types != DMD_TYPE_NONE )
- {
- /*
- ** Create the message queue and the
- ** task used to control the NIM
- */
- KB_OSPQueInit("DMDQ", 16, &DMDQueueID);
- KB_OSPTaskInit("DMDT", 0x2000, (void(*)(void *))DMDAcquisitionTask,
- DMD_PRIORITY, NULL, &taskid);
- /*
- ** Initialize the first demod found
- */
- availableNIM->initialize(&CurrentSignal);
- NIM = availableNIM;
- }
- return nim_types;
- }
- static void DMDAcquisitionTask(void)
- {
- UINT32 message[4];
- INT32 rc;
-
- UINT32 state = DEMOD_DROPPED;
-
- AcquisitionTimer = KB_TimerCreate(KB_TIMER_ONCE,
- (KB_TIMER_FUNC_POINTER)DMDHandleTimeout, NULL);
-
- while(1)
- {
- rc = KB_OSPMsgGet(DMDQueueID, KB_Wait, 0, (KB_OSPMsgNode*)message);
- if(rc != Ret_OK)
- {
- continue;
- }
-
- switch (state)
- {
- case DEMOD_WAITING:
- state = DMDHandleWaitingState(message);
- break;
-
- //其他模块仅发信号连接消息过来, message的第一个参数设置为1(CONNECT_REQUEST_MSG_ID)
- //信号断开的直接调用KB_DMDDropSignal--->NIM->drop();
- case DEMOD_DROPPED:
- state = DMDHandleDroppedState(message);
- break;
-
- case DEMOD_CONNECTED:
- state = DMDHandleConnectedState(message);
- break;
-
- case DEMOD_ERROR:
- break;
-
- default:
- break;
- }
- if (state == DEMOD_CONNECTED)
- {
- gLockFlag = TRUE;
- }
- else
- {
- gLockFlag = FALSE;
- }
- }
- }
- static UINT32 DMDHandleConnectedState(UINT32 *message)
- {
- UINT32 newState = DEMOD_CONNECTED;
-
- switch (message[0])
- {
- case DROP_REQUEST_MSG_ID:
- newState = DEMOD_DROPPED;
- if( NIM != NULL ) NIM->drop();
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_DROPPED);
- break;
-
- case INT_LOST_LOCK_MSG_ID:
- newState = DEMOD_WAITING;
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
- /* Reset the acquisition timer */
- KB_TimerDisable(AcquisitionTimer);
- KB_TimerEnable(AcquisitionTimer, DMD_RECOVER_TIMEOUT);
- break;
- case CONNECT_REQUEST_MSG_ID:
- newState = DEMOD_WAITING;
- KB_TimerDisable(AcquisitionTimer);
- KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
- if( (NIM != NULL) && (TRUE == NIM->connect()) )
- {
- newState = DEMOD_CONNECTED;
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
- }
- else
- {
- newState = DEMOD_DROPPED;
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
- }
- break;
- case TIMEOUT_MSG_ID:
- KB_TimerDisable(AcquisitionTimer);
- if( (NIM != NULL) && (NIM->check_lock != NULL)
- && (TRUE == NIM->check_lock()) )
- {
- }
- else
- {
- newState = DEMOD_DROPPED;
- if( NIM != NULL) NIM->drop();
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
-
- {
- UINT32 msg[4];
- msg[0] = CONNECT_REQUEST_MSG_ID;
- KB_OSPMsgSend(DMDQueueID, (KB_OSPMsgNode*)msg);
- }
- }
- KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
- break;
-
- default:
- break;
- }
-
- return(newState);
- }
- static UINT32 DMDHandleDroppedState(UINT32 *message)
- {
- UINT32 newState = DEMOD_DROPPED;
-
- switch (message[0])
- {
- case INT_LOCK_MSG_ID:
- newState = DEMOD_CONNECTED;
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
- break;
-
- case CONNECT_REQUEST_MSG_ID:
- newState = DEMOD_WAITING;
- KB_TimerDisable(AcquisitionTimer);
- KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
- if( (NIM != NULL) && (TRUE == NIM->connect()) )
- {
- newState = DEMOD_CONNECTED;
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
- }
- else
- {
- newState = DEMOD_DROPPED;
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
- }
- break;
-
- case DROP_REQUEST_MSG_ID:
- newState = DEMOD_DROPPED;
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_DROPPED);
- break;
- case TIMEOUT_MSG_ID:
- KB_TimerDisable(AcquisitionTimer);
- if( (NIM != NULL) && (NIM->check_lock != NULL)
- && (TRUE == NIM->check_lock()) )
- {
- newState = DEMOD_CONNECTED;
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
- }
- else
- {
- UINT32 msg[4];
- msg[0] = CONNECT_REQUEST_MSG_ID;
- KB_OSPMsgSend(DMDQueueID, (KB_OSPMsgNode*)msg);
- }
- KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
- break;
-
- default:
- break;
- }
-
- return(newState);
- }
- static UINT32 DMDHandleWaitingState(UINT32 *message)
- {
- UINT32 newState = DEMOD_WAITING;
-
- switch (message[0])
- {
- case INT_LOCK_MSG_ID:
- newState = DEMOD_CONNECTED;
- KB_TimerDisable(AcquisitionTimer);
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
- break;
-
- case TIMEOUT_MSG_ID:
- KB_TimerDisable(AcquisitionTimer);
- if( (NIM != NULL) && (NIM->check_lock != NULL)
- && (TRUE == NIM->check_lock()) )
- {
- newState = DEMOD_CONNECTED;
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
- }
- else
- {
- newState = DEMOD_DROPPED;
- if( NIM != NULL) NIM->drop();
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
-
- }
- KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
- break;
- case DROP_REQUEST_MSG_ID:
- KB_TimerDisable(AcquisitionTimer);
- newState = DEMOD_DROPPED;
- if( NIM != NULL) NIM->drop();
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_DROPPED);
- break;
- case CONNECT_REQUEST_MSG_ID:
- newState = DEMOD_WAITING;
- KB_TimerDisable(AcquisitionTimer);
- KB_TimerEnable(AcquisitionTimer, DMD_INITIAL_TIMEOUT);
- if( (NIM != NULL) && (TRUE == NIM->connect()) )
- {
- newState = DEMOD_CONNECTED;
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
- }
- else
- {
- newState = DEMOD_DROPPED;
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_LOST);
- }
- break;
-
- default:
- break;
- }
-
- return(newState);
- }
- static void DMDHandleTimeout(void)
- {
- UINT32 message[4];
- message[0] = TIMEOUT_MSG_ID;
- KB_OSPMsgSend(DMDQueueID, (KB_OSPMsgNode*)message);
- }
- INT32 KB_DMDConnectSignal(KB_DMDTunerParameters *dmdParam)
- {
- UINT32 CurrentTsTsSequence;
- UINT16 TsID;
- unsigned short Fre;
- unsigned short Symbl;
- unsigned char Qam;
- UINT32 message[4];
- KB_DMDTunerParameters ts;
- if( (NIM == NULL) || (dmdParam == NULL) || (dmdParam->type != NIM->type) )
- {
- return RETFIAL1;
- }
- DBGPrint("[j_dmd.c] %d %d %dn", dmdParam->frequency, dmdParam->details.qam.symbolRatekSs,
- dmdParam->details.qam.modulation);
- if( (0 == memcmp(&CurrentSignal, dmdParam, sizeof(KB_DMDTunerParameters))) && (NIM->check_lock() == TRUE) )
- {
- if (State_NVOD == KB_GetCurState())
- {
- if (0 == dmdParam->frequency)
- {
- printf("nKB_DMDConnectSignal::serious error. fre = 0!");
- CurrentTsTsSequence = KB_NVOD_GetCurrentTsNum();
- if ((UINT32)(-1) != CurrentTsTsSequence
- && NULL != KB_NVOD_GetTsParaFromTsSequence(CurrentTsTsSequence))
- {
- KB_DMDConnectSignal(KB_NVOD_GetTsParaFromTsSequence(CurrentTsTsSequence));
- return RETOK;
- }
- }
-
- if (KB_DB_DTV_SUCCESS != KB_NVODInit2())
- {
- printf("nKB_DMDConnectSignal::KB_NVODInit2 failed!n");
- }
- else
- {
- printf("nKB_DMDConnectSignal::KB_NVODInit2 ok!n");
- }
- }
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_CONNECTED);
- return RETOK;
- }
-
- CurrentSignal = *dmdParam;
-
- if (State_NVOD == KB_GetCurState())
- {
- if (0 == dmdParam->frequency)
- {
- printf("nKB_DMDConnectSignal::serious error. fre = 0!");
- CurrentTsTsSequence = KB_NVOD_GetCurrentTsNum();
- if ((UINT32)(-1) != CurrentTsTsSequence
- && NULL != KB_NVOD_GetTsParaFromTsSequence(CurrentTsTsSequence))
- {
- KB_DMDConnectSignal(KB_NVOD_GetTsParaFromTsSequence(CurrentTsTsSequence));
- return RETOK;
- }
- }
-
- if (KB_DB_DTV_SUCCESS != KB_NVODInit2())
- {
- printf("nKB_DMDConnectSignal::KB_NVODInit2 failed!n");
- }
- else
- {
- printf("nKB_DMDConnectSignal::KB_NVODInit2 ok!n");
- }
- }
- if (State_EPG == KB_GetCurState())
- {
- if (0 == dmdParam->frequency)
- {
- printf("nKB_DMDConnectSignal::serious error. fre = 0!");
- CurrentTsTsSequence = KB_EPG_GetCurrentTsNum();
-
- if ((UINT32)(-1) != CurrentTsTsSequence)
- {
- TsID = (UINT16)KB_EPG_GetTsIdFromTsNum(CurrentTsTsSequence);
-
- if ((UINT32)(-1) != TsID
- && RET_OK == KB_DBGetChnlInfoByTsID(TsID, &Fre, &Symbl, &Qam))
- {
- KB_DMDDropSignal();
-
- ts.type = DMD_TYPE_QAM;
- ts.inversion = INVERSION_AUTO;
- ts.frequency = (UINT32)Fre * 100;
- ts.details.qam.symbolRatekSs = Symbl;
- ts.details.qam.modulation = Qam;
- KB_DMDConnectSignal(&ts);
- printf("nKB_DMDConnectSignal::ts(%d) rechange to %d!n",
- dmdParam->frequency, ts.frequency);
- }
- }
- return RETOK;
- }
- else
- {
- if (RET_OK == KB_DBGetTsIDByFre(&TsID,CurrentSignal.frequency))
- {
- CurrentTsTsSequence = KB_EPG_GetCurrentTsNum();
- if ((UINT32)(-1) != CurrentTsTsSequence
- && TsID != KB_EPG_GetTsIdFromTsNum(CurrentTsTsSequence))
- {
- printf("nKB_DMDConnectSignal::CurrentTs change!n");
- KB_SIStopEitSCH(KB_SI_STOP_ALL);
- KB_EPG_SetCurrentTsNum(TsID);
- }
- }
- }
- }
-
- message[0] = CONNECT_REQUEST_MSG_ID;
- KB_OSPMsgSend(DMDQueueID, (KB_OSPMsgNode*)message);
- return RETOK;
- }
- INT32 KB_DMDDropSignal(void)
- {
- if( NIM != NULL )
- {
- NIM->drop();
- }
- DMDCallbackFunction(DMD_CALLBACK_SIGNAL_DROPPED);
- return RETOK;
- }
- INT32 KB_DMDQuerySignalStatus(void)
- {
- return gLockFlag;
- }
- INT32 KB_DMDGetSignalParameters(KB_DMDTunerParameters *ptParameters)
- {
- if (ptParameters == NULL)
- {
- return RETFAIL3;
- }
-
- *ptParameters = CurrentSignal;
- return RETOK;
- }
- void KB_DMDCallBack(KB_DMD_CallbackFunc callback)
- {
- DMDCallbackFunction = callback; /* Register function in function table */
- }
- /* EOF --------------------------------------------------------------------- */