📄 stv199.c
字号:
/*****************************************************************************File Name : stv199.cDescription : STV0199A low-level device access.Copyright (C) 1999 STMicroelectronicsReference :ST API Definition "TUNER Driver API" DVD-API-06*****************************************************************************//* Includes --------------------------------------------------------------- */#include <string.h> /* Standard includes */#include <stdio.h>#include "stlite.h"#include "stddefs.h"#include "sttuner.h" /* STAPI Error codes, etc */#include "stv199.h" /* Low-level device access */#include "sti2c.h" /* I2C access */#include "stcommon.h"#include "sttbx.h"/* Private types/constants ------------------------------------------------ */typedef struct{ U32 M; U32 SignalQuality; U32 BitErrorRate;} STV0199A_SignalQuality_t;typedef struct{ S8 Agc; /* AGC register value */ U8 P; /* Power output */} STV0199A_AGCPower_t;/* Private variables ------------------------------------------------------ *//* Device property table -- this contains the bitmasks for each property * defined in the STV0199A. */static STV0199A_PropertyTable_t PropertyTable[] ={ STV0199A_IQ_PROPERTY, STV0199A_ROLL_OFF_PROPERTY, STV0199A_H_PROPERTY, STV0199A_TO_PROPERTY, STV0199A_SN_PROPERTY, STV0199A_CF_PROPERTY, STV0199A_LK_PROPERTY, STV0199A_E_PROPERTY, STV0199A_ALPHA_TIMING_PROPERTY, STV0199A_BETA_TIMING_PROPERTY, STV0199A_TLOOP_FREQUENCY_PROPERTY, STV0199A_BETA_CARRIER_PROPERTY, STV0199A_ALGORITHM_PROPERTY, STV0199A_ALPHA_CARRIER_PROPERTY, STV0199A_CLOOP_FREQUENCY_PROPERTY, STV0199A_AGC1_OPEN_DRAIN_PROPERTY, STV0199A_AGC1_REF_PROPERTY, STV0199A_AGC1_INTEGRATOR_PROPERTY, STV0199A_BETA_AGC1_PROPERTY, STV0199A_AGC2_COEFFICIENT_PROPERTY, STV0199A_AGC2_REF_PROPERTY, STV0199A_AGC2_GAIN_PROPERTY, STV0199A_SYMBOL_MSB_PROPERTY, STV0199A_SYMBOL_LSB_PROPERTY, STV0199A_M_DIVISOR_PROPERTY, STV0199A_DIR_CLOCK_PROPERTY, STV0199A_P_DIVISOR_PROPERTY, STV0199A_AUX_ENA_PROPERTY, STV0199A_P_AUX_PROPERTY, STV0199A_PUNCTURE_RATE_PROPERTY, STV0199A_TS_OUTPUT_MODE_PROPERTY, STV0199A_OUTPUT_CLOCK_CFG_PROPERTY, STV0199A_OUTPUT_CLOCK_PLR_PROPERTY};/* The following table maps the bit error rate values to signal quality. */static STV0199A_SignalQuality_t SignalQualityLUT[] ={ { 0, 100, 0 }, { 1, 97, 1 }, { 3, 94, 5 }, { 5, 91, 10 }, { 15, 88, 28 }, { 29, 85, 56 }, { 63, 82, 120 }, { 126, 79, 240 }, { 246, 76, 470 }, { 409, 73, 780 }, { 682, 70, 1300 }, { 1153, 67, 2200 }, { 1751, 64, 3340 }, { 2621, 61, 5000 }, { 3880, 58, 7400 }, { 5421, 55, 10300 }, { 7444, 52, 14200 }, { 10066, 49, 19200 }, { 12582, 46, 24000 }, { 16777, 43, 32000 }, { 20970, 40, 40000 }, { 25795, 37, 49200 }, { 30828, 34, 58800 }, { 36700, 31, 70000 }, { 41943, 28, 80000 }, { 65535, 0, 200000 }, { (U32)-1, 0 } /* Terminator */};static STV0199A_AGCPower_t AGCPowerLUT[] ={ { -128, 111 }, /* -72 dBm */ { 29, 213 }, /* -21 dBm */ { 35, 219 }, /* -18 dBm */ { 40, 225 }, /* -15 dBm */ { 127, 255 }, /* 0 dBm */ { 0, 0 }};#define STV0199_I2C_TIMEOUT 10/* Private macros --------------------------------------------------------- *//* Private function prototypes -------------------------------------------- */static U32 LongSqrt(U32 Value);/* API routines ----------------------------------------------------------- *//*****************************************************************************Name: STV0199A_Read()Description: I2C read access routine for the STV0199A device.Parameters: Hw_p, pointer to the STV0199A device. Dest_p, caller supplied area to copy the register value. Register, register offset in the STV0199A register space.Return Value: STV0199A_NO_ERROR, the operation completed without error. STV0199A_ERROR_BUS, there was a problem accessing the device.See Also: STV0199A_Write()*****************************************************************************/STV0199A_ErrorCode_t STV0199A_Read(STV0199A_Device_t *Hw_p, U8 *Dest_p, U8 Register){ ST_ErrorCode_t I2CError; STV0199A_ErrorCode_t Error = STV0199A_NO_ERROR; U32 NumberRetries = STV0199A_I2C_RETRIES; U32 NumberRead, NumberWritten; while(NumberRetries > 0) { /* Tell the device which register we wish to access */ I2CError = STI2C_Write(*(STI2C_Handle_t *)Hw_p->Handle_p, &Register, 1, STV0199_I2C_TIMEOUT, &NumberWritten);#ifdef STTUNER_DEBUG_I2C if (I2CError != ST_NO_ERROR) STTBX_Print(("STI2C_Write = 0x%x\n", I2CError));#endif /* Now read the register -- only if the write succeeded */ if (I2CError == ST_NO_ERROR) { I2CError = STI2C_Read(*(STI2C_Handle_t *)Hw_p->Handle_p, Dest_p, 1, STV0199_I2C_TIMEOUT, &NumberRead);#ifdef STTUNER_DEBUG_I2C if (I2CError != ST_NO_ERROR) STTBX_Print(("STI2C_Read = 0x%x\n", I2CError));#endif } if (I2CError != ST_NO_ERROR || NumberRead != 1) { STV0199A_Delay(1); /* Short delay before retry */ } else { Hw_p->RegisterMap[Register] = *Dest_p; break; } NumberRetries--; } /* If all retries have expired, then the operation has failed */ if (NumberRetries == 0) Error = I2CError; return Error;} /* STV0199A_Read() *//*****************************************************************************Name: STV0199A_Write()Description: I2C write access routine for the STV0199A device.Parameters: Hw_p, pointer to the DEMOD device. Value, new value to write to the register. Register, register offset in the STV0199A register space.Return Value: STV0199A_NO_ERROR, the operation completed without error. STV0199A_ERROR_BUS, there was a problem accessing the device.See Also: STV0199A_Read()*****************************************************************************/STV0199A_ErrorCode_t STV0199A_Write(STV0199A_Device_t *Hw_p, U8 Value, U8 Register){ U32 NumberRetries = STV0199A_I2C_RETRIES; STV0199A_ErrorCode_t Error = STV0199A_NO_ERROR; ST_ErrorCode_t I2CError; U8 STV0199ABuffer[2]; U32 NumberWritten; /* Build the STV0199A command; a register, value pair */ STV0199ABuffer[0] = Register; STV0199ABuffer[1] = Value; while(NumberRetries > 0) { /* Write out the command */ I2CError = STI2C_Write(*(STI2C_Handle_t *)Hw_p->Handle_p, STV0199ABuffer, 2, STV0199_I2C_TIMEOUT, &NumberWritten);#ifdef STTUNER_DEBUG_I2C if (I2CError != ST_NO_ERROR) STTBX_Print(("STI2C_Write = 0x%x\n", I2CError));#endif if (I2CError != ST_NO_ERROR || NumberWritten != 2) { STV0199A_Delay(1); /* Short delay before retry */ } else { break; } NumberRetries--; } /* If all retries have expired, then the operation has failed */ if (NumberRetries == 0) Error = I2CError; else Hw_p->RegisterMap[Register] = Value; /* Update our local copy */ return Error;} /* STV0199A_Write() *//*****************************************************************************Name: STV0199A_ReadBlock()Description: Routine for reading from contiguous bytes in the STV0199A register space.Parameters: Hw_p, pointer to the DEMOD device. Dest_p, pointer to the first byte of the contiguous array. Register, register offset in the STV0199A register space. NumberToRead, number of bytes to read.Return Value: STV0199A_NO_ERROR, the operation completed without error. STV0199A_ERROR_BUS, there was a problem accessing the device.See Also: STV0199A_Write()*****************************************************************************/STV0199A_ErrorCode_t STV0199A_ReadBlock(STV0199A_Device_t *Hw_p, U8 *Dest_p, U8 Register, U8 NumberToRead){ STV0199A_ErrorCode_t Error = STV0199A_NO_ERROR; while (NumberToRead > 0 && Error == STV0199A_NO_ERROR) { Error = STV0199A_Read(Hw_p, Dest_p, Register); /* Update for next byte */ Dest_p++; Register++; NumberToRead--; } return Error;} /* STV0199A_ReadBlock() *//*****************************************************************************Name: STV0199A_WriteBlock()Description: Routine for writing to contiguous bytes in the STV0199A register space.Parameters: Hw_p, pointer to the DEMOD device. Src_p, pointer to the first byte of the contiguous array. Register, register offset in the STV0199A register space. NumberToWrite, number of bytes to write.Return Value: STV0199A_NO_ERROR, the operation completed without error. STV0199A_ERROR_BUS, there was a problem accessing the device.See Also: STV0199A_ReadBlock()*****************************************************************************/STV0199A_ErrorCode_t STV0199A_WriteBlock(STV0199A_Device_t *Hw_p, U8 *Src_p, U8 Register, U8 NumberToWrite){ STV0199A_ErrorCode_t Error = STV0199A_NO_ERROR; while (NumberToWrite > 0 && Error == STV0199A_NO_ERROR) { Error = STV0199A_Write(Hw_p, *Src_p, Register); /* Update for next byte */ Src_p++; Register++; NumberToWrite--; } return Error;} /* STV0199A_WriteBlock() *//*****************************************************************************Name: STV0199A_ResetRegisters()Description: Resets the STV0199A registers to a known state.Parameters: Hw_p, pointer to the DEMOD device.Return Value: STV0199A_NO_ERROR, the operation completed without error. STV0199A_ERROR_BUS, there was a problem accessing the device.See Also: Nothing.*****************************************************************************/STV0199A_ErrorCode_t STV0199A_ResetRegisters(STV0199A_Device_t *Hw_p){ /* STV0199A register startup values */ static U8 STV0199A_RegisterReset[] = STV0199A_RESET_SETTINGS; /* Write out the start up values */ return STV0199A_WriteBlock(Hw_p, STV0199A_RegisterReset, 0, /* base register */ STV0199A_NUMBER_REGISTERS);}/*****************************************************************************Name: STV0199A_GetProperty()Description: This routine enables the caller to extract n-bit values from an arbitrary register on the STV0199A. Each property is defined by a bitmask, a register number and a signed/unsigned boolean.Parameters: Hw_p, pointer to the STV0199A device information. Property, property value to obtain - also contains data type. Dest_p, caller supplied area to copy value of property.Return Value: STV0199A_NO_ERROR, the operation completed without error. STV0199A_ERROR_BUS, unable to access device.See Also: STV0199A_SetProperty()*****************************************************************************/STV0199A_ErrorCode_t STV0199A_GetProperty(STV0199A_Device_t *Hw_p, STV0199A_Property_t Property, U8 *Dest_p){ STV0199A_ErrorCode_t Error; STV0199A_PropertyTable_t *Property_p; U8 Value; U8 BitOffset = 0; Property_p = &PropertyTable[Property]; /* We first calculate the bit offset of the first bit set in the mask */ Value = Property_p->BitMask; while ((Value & 0x01) != 1) { BitOffset++; Value >>= 1; } /* Attempt to read the property's register */ Error = STV0199A_Read(Hw_p, &Value, Property_p->Register); /* If the read fails, then we can't continue */ if (Error == STV0199A_NO_ERROR) { /* Mask the bits and shift them to bit 0 of the destination byte */ Value = (Value & Property_p->BitMask) >> BitOffset; *Dest_p = Value; } return Error;} /* STV0199A_GetProperty() *//*****************************************************************************Name: STV0199A_SetProperty()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -