📄 chiplib.c
字号:
//===========================================================================
// chiplib.c
//
// For this file, set tabs to 4 spaces.
//---------------------------------------------------------------------------
//
// Copyright (c) 2002 Epson Research and Development, Inc.
// All Rights Reserved.
//
//===========================================================================
#include <string.h>
#include "stdio.h"
#include "hal.h"
#include "chiplib.h"
//
// Application MUST always call InitChipLib() when starting up.
//
static Boolean gfUsing13716 = FALSE;
//---------------------------------------------------------------------------
// FUNCTION: InitChipLib()
//---------------------------------------------------------------------------
void InitChipLib( void )
{
gfUsing13716 = !strcmp( HalInfo.szChipId, "13716" );
}
//---------------------------------------------------------------------------
// FUNCTION: is13716Chip()
//---------------------------------------------------------------------------
int is13716Chip( void )
{
return gfUsing13716;
}
//---------------------------------------------------------------------------
// FUNCTION: GetChipName()
//---------------------------------------------------------------------------
char *GetChipName( void )
{
return HalInfo.szChipId;
}
//---------------------------------------------------------------------------
// FUNCTION: GetChipId()
//---------------------------------------------------------------------------
void GetChipId( UInt32 *pProductCode, UInt32 *pRevisionCode )
{
UInt8 Reg00 = halReadReg8( REG0000_PRODINFO0 );
*pProductCode = (Reg00 >> 2) & 0x3f;
*pRevisionCode = Reg00 & 0x03;
}
//---------------------------------------------------------------------------
// FUNCTION: GetMemSize()
//---------------------------------------------------------------------------
UInt32 GetMemSize( void )
{
UInt8 reg = halReadReg8( REG0001_PRODINFO1 );
return ( reg * 4096UL );
}
//---------------------------------------------------------------------------
// FUNCTION: SetBitsPerPixel()
//---------------------------------------------------------------------------
void SetBitsPerPixel( WindowDef Window, int bpp )
{
if ( Window == cl_OVERLAY_WINDOW )
{
UInt8 reg = halReadReg8( REG004C_DISPSETTING );
//
// Program the bits-per-pixel select bits
//
reg &= ~0x20;
if (bpp == 8)
reg |= 0x20;
halWriteReg8( REG004C_DISPSETTING, reg );
}
}
//---------------------------------------------------------------------------
// FUNCTION: GetBitsPerPixel()
//---------------------------------------------------------------------------
int GetBitsPerPixel( WindowDef Window )
{
if ( Window == cl_OVERLAY_WINDOW )
return (halReadReg8(REG004C_DISPSETTING) & 0x20) ? 8 : 16;
else
return 16;
}
//---------------------------------------------------------------------------
// FUNCTION: SetLutEntry()
//---------------------------------------------------------------------------
void SetLutEntry( int addr, UInt8 red, UInt8 green, UInt8 blue )
{
UInt32 reg = REG0400_LUTRED + addr*4;
halWriteReg8( reg++, red );
halWriteReg8( reg++, green );
halWriteReg8( reg, blue );
}
//---------------------------------------------------------------------------
// FUNCTION: GetLutEntry()
//---------------------------------------------------------------------------
void GetLutEntry( int addr, pUInt8 pRed, pUInt8 pGreen, pUInt8 pBlue )
{
UInt32 reg = REG0400_LUTRED + addr*4;
if ( pRed != NULL )
*pRed = halReadReg8( reg );
reg++;
if ( pGreen != NULL )
*pGreen = halReadReg8( reg );
reg++;
if ( pBlue != NULL )
*pBlue = halReadReg8( reg );
}
//---------------------------------------------------------------------------
// FUNCTION: SetLut()
//---------------------------------------------------------------------------
void SetLut( pUInt8 pLut, int size )
{
int i;
UInt32 reg = REG0400_LUTRED;
for ( i=0; i<size; i++ )
{
halWriteReg8( reg++, *pLut++ );
halWriteReg8( reg++, *pLut++ );
halWriteReg8( reg++, *pLut++ );
reg++; // Must skip over 4 bytes per LUT entry
}
}
//---------------------------------------------------------------------------
// FUNCTION: InitLut()
//---------------------------------------------------------------------------
void InitLut( UInt32 LutMode )
{
int i;
UInt8 red, green, blue, val8;
if (LutMode & cl_LUT_TEST_PATTERN)
{
LutMode &= ~cl_LUT_TEST_PATTERN;
switch (LutMode)
{
case cl_LUT_NOBYPASS_8BPP_256COLOR_LUT:
// LUT[0] - LUT[63] is red
// LUT[64] - LUT[64*2-1] is green
// LUT[64*2] - LUT[64*3-1] is blue
// LUT[64*3] - LUT[64*4-1] is gray shades
for ( i=0; i<64; i++ )
{
val8 = (UInt8) (i<<2);
SetLutEntry(i, val8, 0, 0);
SetLutEntry(i + 64, 0, val8, 0);
SetLutEntry(i + 64*2, 0, 0, val8);
SetLutEntry(i + 64*3, val8, val8, val8);
}
return;
break;
}
}
switch (LutMode)
{
default:
break;
case cl_LUT_NOBYPASS_8BPP_256COLOR_LUT:
SetLut(LutInfo.lut1, (sizeof(LutInfo.lut1) / sizeof(LutInfo.lut1[0]))/3);
break;
case cl_LUT_NOBYPASS_16BPP_565LUT:
for (i = 0; i < 64; ++i)
{
if (i < 32)
red = blue = (UInt8) ((i * 0xff) / 31);
else
red = blue = 0xff;
green = (UInt8) ((i * 0xff) / 63);
SetLutEntry(i, red, green, blue);
}
break;
}
}
//---------------------------------------------------------------------------
// FUNCTION: GetStartAddress()
//---------------------------------------------------------------------------
UInt32 GetStartAddress( WindowDef Window )
{
if ( Window == cl_OVERLAY_WINDOW )
return ((UInt32)(halReadReg8(REG0242_OSADDR2) & 0x01)<<16) | ((UInt32)(halReadReg8(REG0241_OSADDR1) & 0xFF)<<8) | ((UInt32)(halReadReg8(REG0240_OSADDR0) & 0xFF));
else
return ((UInt32)(halReadReg8(REG0226_YUVWRSTART2) & 0x01)<<16) | ((UInt32)(halReadReg8(REG0225_YUVWRSTART1) & 0xFF)<<8) | ((UInt32)(halReadReg8(REG0224_YUVWRSTART0) & 0xFF));
}
//---------------------------------------------------------------------------
// FUNCTION: SetStartAddress()
//---------------------------------------------------------------------------
void SetStartAddress( WindowDef Window,UInt32 Address )
{
if ( Window == cl_OVERLAY_WINDOW )
{
halWriteReg8(REG0242_OSADDR2,(UInt8)(Address>>16));
halWriteReg8(REG0241_OSADDR1,(UInt8)(Address>>8));
halWriteReg8(REG0240_OSADDR0,(UInt8)(Address));
}
else
{
halWriteReg8(REG0226_YUVWRSTART2,(UInt8)(Address>>16));
halWriteReg8(REG0225_YUVWRSTART1,(UInt8)(Address>>8));
halWriteReg8(REG0224_YUVWRSTART0,(UInt8)(Address));
}
}
//---------------------------------------------------------------------------
// FUNCTION: GetMemAddress()
//---------------------------------------------------------------------------
UInt32 GetMemAddress( UInt32 BaseAddress )
{
return BaseAddress;
}
//---------------------------------------------------------------------------
// FUNCTION: GetStride()
//---------------------------------------------------------------------------
UInt32 GetStride( WindowDef Window )
{
return GetDisplayWidth(Window) * GetBitsPerPixel(Window) / 8;
}
//---------------------------------------------------------------------------
// FUNCTION: GetDisplayWidth()
// GetDisplayHeight()
//---------------------------------------------------------------------------
int GetDisplayWidth( WindowDef Window )
{
if ( Window == cl_OVERLAY_WINDOW )
{
return halReadReg8(REG0244_OHRES) + 1;
}
else
{
UInt32 xStart, yStart, xEnd, yEnd;
GetResizerSize( &xStart, &yStart, &xEnd, &yEnd );
return xEnd - xStart + 1;
}
}
int GetDisplayHeight( WindowDef Window )
{
if ( Window == cl_OVERLAY_WINDOW )
{
return halReadReg8(REG0246_OVRES) + 1;
}
else
{
UInt32 xStart, yStart, xEnd, yEnd;
GetResizerSize( &xStart, &yStart, &xEnd, &yEnd );
return yEnd - yStart + 1;
}
}
//---------------------------------------------------------------------------
// FUNCTION: SetResizerSize()
//---------------------------------------------------------------------------
void SetResizerSize( UInt32 xStart, UInt32 yStart, UInt32 xEnd, UInt32 yEnd )
{
halWriteReg8( REG0204_RESIZESX0, (UInt8)xStart );
halWriteReg8( REG0205_RESIZESX1, (UInt8)(xStart>>8) );
halWriteReg8( REG0206_RESIZESY0, (UInt8)yStart );
halWriteReg8( REG0207_RESIZESY1, (UInt8)(yStart>>8) );
halWriteReg8( REG0208_RESIZEEX0, (UInt8)xEnd );
halWriteReg8( REG0209_RESIZEEX1, (UInt8)(xEnd>>8) );
halWriteReg8( REG020A_RESIZEEY0, (UInt8)yEnd );
halWriteReg8( REG020B_RESIZEEY1, (UInt8)(yEnd>>8) );
}
//---------------------------------------------------------------------------
// FUNCTION: GetResizerSize()
//---------------------------------------------------------------------------
void GetResizerSize( pUInt32 xStart, pUInt32 yStart, pUInt32 xEnd, pUInt32 yEnd )
{
if (xStart != NULL)
*xStart = halReadReg8(REG0204_RESIZESX0)|(halReadReg8(REG0205_RESIZESX1)<<8);
if (yStart != NULL)
*yStart = halReadReg8(REG0206_RESIZESY0)|(halReadReg8(REG0207_RESIZESY1)<<8);
if (xEnd != NULL)
*xEnd = halReadReg8(REG0208_RESIZEEX0) | (halReadReg8(REG0209_RESIZEEX1)<<8);
if (yEnd != NULL)
*yEnd = halReadReg8(REG020A_RESIZEEY0) | (halReadReg8(REG020B_RESIZEEY1)<<8);
}
//----------------------------------------------------------------------------
// GetPllPowerDown()
//----------------------------------------------------------------------------
Boolean GetPllPowerDown( void )
{
return (halReadReg8(REG0018_POWERSAVE)&0x01) ? TRUE : FALSE;
}
//---------------------------------------------------------------------------
// SetPll()
//---------------------------------------------------------------------------
Boolean SetPll( UInt32 freq )
{
UInt32 ClosestN, ClosestL, ClosestPll, ClosestDiff;
UInt32 n, l, pll, diff;
UInt32 ClosestVbits, v, vBits, factor;
UInt32 Kv;
Boolean PllProgrammed = FALSE;
UInt32 PllClk;
UInt8 val8;
#define MHZ 1000000UL
#define abs32(x, y) ( (x>y) ? (x-y) : (y-x) )
// Determine if the PLL clock is the CLKI External Clock Source,
// or the 32 kHz External Oscillator.
if ( halReadReg8(REG0014_CLKSRC) & 0x02 )
PllClk = 32768; // 32 kHz
else
PllClk = HalInfo.dwClkI;
//
// Find the closest PLL frequency based on N and L
//
ClosestN = 0;
ClosestL = 0;
ClosestPll = (ClosestN+1) * (ClosestL+1) * PllClk;
ClosestDiff = abs32(freq,ClosestPll);
Kv = 0;
n = 16;
while ( n-- ) // n must loop from 15 to 0
{
for ( l=0; l<=1023; ++l )
{
pll = (n+1) * (l+1) * PllClk;
diff = abs32(freq,pll);
if ( diff < ClosestDiff )
{
ClosestN = n;
ClosestL = l;
ClosestPll = (ClosestN+1) * (ClosestL+1) * PllClk;
ClosestDiff = abs32(freq,ClosestPll);
}
}
}
//
// Determine V based on PLL frequency
//
// 100 MHz <= PLL * V <= 410 MHz
// if V==2, V divider bits=1
// if V==4, V divider bits=2
// if V==8, V divider bits=3
//
ClosestVbits = 0;
//
// If the duty cycle is not important, lower power consumption can be achieved
// by setting the V-divider bits and VCO Kv Set bits to 0.
//
if ( !(HalInfo.dwFlags&fPLL_LOWPOWER) )
{
for ( vBits=1; (vBits<=3)&&!PllProgrammed; vBits++ )
{
v = 1 << vBits;
factor = ClosestPll * v;
// Check if V is valid
if ( (100*MHZ<=factor) && (factor<=410*MHZ) )
{
ClosestVbits = vBits;
// Determine Kv
//
// if (100 MHz <= PLL * V <= 200 MHz) then Kv=2
// if (200 MHz < PLL * V <= 300 MHz) then Kv=5
// if (300 MHz < PLL * V <= 410 MHz) then Kv=7
factor = ClosestPll * v;
Kv = 0;
if ( (100*MHZ<=factor) && (factor<=200*MHZ) )
{
Kv = 2;
PllProgrammed = TRUE;
}
else if ( (200*MHZ<factor) && (factor<=300*MHZ) )
{
Kv = 5;
PllProgrammed = TRUE;
}
else if ( (300*MHZ<factor) && (factor<=410*MHZ) )
{
Kv = 7;
PllProgrammed = TRUE;
}
}
}
if ( !PllProgrammed )
{
// Ensure valid values
if ( ClosestVbits == 0 )
ClosestVbits = 1;
if ( Kv==0 )
Kv = 2;
}
}
else
PllProgrammed = TRUE; // Don't need to program V or Kv
ClosestN &= 0x0f;
ClosestL &= 0x3ff;
ClosestVbits &= 0x03;
// Go into power save before writing to PLL registers
PowerSaveMode( TRUE );
halWriteReg8( REG0010_PLL0, (UInt8)((ClosestL<<2)|ClosestVbits) );
halWriteReg8( REG0011_PLL1, (UInt8)((ClosestN<<4)|((ClosestL>>6)&0x0f)) );
halWriteReg8( REG0012_PLL2, (UInt8)(Kv<<4) );
// Enable PLL
val8 = halReadReg8( REG0018_POWERSAVE );
val8 |= 0x04;
halWriteReg8( REG0018_POWERSAVE, val8 );
// Restore original setting
halDelayUS( 100 );
PowerSaveMode( FALSE );
return PllProgrammed;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -