⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cameraimagic.cpp

📁 Microsoft WinCE 6.0 BSP FINAL release source code for use with the i.MX27ADS TO2 WCE600_FINAL_MX27_S
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//------------------------------------------------------------------------------
//
//  Copyright (C) 2004, Motorola Inc. All Rights Reserved
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2006, Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT 
//
//------------------------------------------------------------------------------
//
//  File:  CameraImagicClass.cpp
//
//  Definitions for Imagic camera module specific
//
//------------------------------------------------------------------------------

#include <windows.h>

#include "cameradbg.h"
#include "bsp.h"
#include "i2cbus.h"
#include "CsiClass.h"
#include "CameraImagic.h"
#include "bspcsi.h"

//------------------------------------------------------------------------------
// External Functions


//------------------------------------------------------------------------------
// External Variables
extern csiSensorId gSensorInUse;
extern DWORD gVideoPort;
//extern DWORD gVideoFormat;


//------------------------------------------------------------------------------
// Defines
//#define IM8803_I2C_WR_ADDRESS   0xB8  // MT9V143 only allows writes to this address
#define IM8803_I2C_ADDRESS      0x48  // MT9V143 only allows writes to this address
#define IM8803_I2C_SPEED        81000

#define IM8201_I2C_ADDRESS      0x48  // MT9V111 only allows writes to this address
#define IM8201_I2C_SPEED        81000

#define SAA7113H_I2C_ADDRESS    0x25  //saa7113h, default 25; 
#define SAA7113H_I2C_SPEED      81000

#define CAMERA_FUNCTION_ENTRY() \
    DEBUGMSG(ZONE_FUNCTION, (TEXT("++%s\r\n"), __WFUNCTION__))
#define CAMERA_FUNCTION_EXIT() \
    DEBUGMSG(ZONE_FUNCTION, (TEXT("--%s\r\n"), __WFUNCTION__))
    
//------------------------------------------------------------------------------
// Types


//------------------------------------------------------------------------------
// Global Variables


//------------------------------------------------------------------------------
// Local Variables
HANDLE hI2C;


//------------------------------------------------------------------------------
// Local Functions


//------------------------------------------------------------------------------
//
// Function: CameraImagicInit
//
// Initializes the Imagic camera sensor.
//
// Parameters:
//      None.
//
// Returns:
//      None.
//
//------------------------------------------------------------------------------
void CameraImagicInit(void)
{
    INT iResult;
    DWORD dwFrequency;
    BYTE bySlaveAddr, byCsiAddr;
    
    CAMERA_FUNCTION_ENTRY();

    byCsiAddr = CSI_I2C_ADDRESS;

    switch (gSensorInUse)
    {
     case csiSensorId_Imagic8803:
           dwFrequency = IM8803_I2C_SPEED;
           bySlaveAddr = IM8803_I2C_ADDRESS;
           break;
                 
     case csiSensorId_Imagic8201:
           dwFrequency = IM8201_I2C_SPEED;
           bySlaveAddr = IM8201_I2C_ADDRESS;
           break;

     default:
           DEBUGMSG(ZONE_ERROR, (TEXT("%s: We don't support this kind of sensor!\r\n"), __WFUNCTION__));
           return;
    }        

    hI2C = CreateFile(I2C_FID_CAM,                              // name of device
               GENERIC_READ|GENERIC_WRITE,              // desired access
               FILE_SHARE_READ|FILE_SHARE_WRITE,     // sharing mode
               NULL,                                                             // security attributes (ignored)
               OPEN_EXISTING,                                        // creation disposition
               FILE_FLAG_RANDOM_ACCESS,                  // flags/attributes
               NULL);

    if (hI2C == INVALID_HANDLE_VALUE)
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("%s: CreateFile for I2C failed!\r\n"), __WFUNCTION__));
    }

    // Initialize the device internal fields
    if (!I2C_MACRO_SET_FREQUENCY(hI2C, dwFrequency))
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("%s: Set I2C frequency failed!\r\n"), __WFUNCTION__));
    }
    if (!I2C_MACRO_SET_SELF_ADDR(hI2C, byCsiAddr))
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("%s: Set I2C self address failed!\r\n"), __WFUNCTION__));
    }

    // switch to sensor registers
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x01, 0x00, 0x04, &iResult);

    if (iResult != I2C_NO_ERROR)
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("%s: I2C communication with camera sensor failed!  Result = %x\r\n"), __WFUNCTION__, iResult));
    }

    //soft reset
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x0D, 0x00, 0x01, &iResult);
    Sleep(10);
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x0D, 0x00, 0x00, &iResult);

    // Output Control register
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x07, 0x00, 0x03, &iResult); // enable changes

    // Shutter width
//    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x09, 0xB9, 0x01, &iResult);

    // Pixel clock speed
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x0A, 0x00, 0x00, &iResult); // min PIXCLK

    // Read Mode
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x20, 0xD0, 0xA1, &iResult); // read from top to bottom (rotate by 180 deg)

    // Zoom
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x1E, 0x00, 0x00, &iResult); // 1x

/*
    // First Row
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x08, 0x00, 0x08, &iResult); // 8

    // First Column
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x02, 0x00, 0x14, &iResult); // 20

    // Window Height
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x03, 0x01, 0xDF, &iResult); // 479

    // Window Width
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x04, 0x02, 0x7F, &iResult); // 639

    // Zoom Colstart
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x12, 0x00, 0x00, &iResult); // 0

    // Zoom Rowstart
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x13, 0x00, 0x00, &iResult); // 0
*/

    // Min H-blank
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x05, 0x00, 0x7B, &iResult); // 123

    // Min V-blank
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x06, 0x00, 0x03, &iResult); // 3

    // Changes effective
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x07, 0x00, 0x02, &iResult); // changes become effective

    // Switch to IFP registers
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x01, 0x00, 0x01, &iResult);

    // Operation Mode Control
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x06, 0x70, 0x8E, &iResult);

    // Output Format
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x08, 0xC8, 0x00, &iResult);

/*
    // Output Format 2
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x3A, 0x00, 0x00, &iResult);

    //workaround for read mode control
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x37, 0x00, 0x01, &iResult); // fix for flickering
*/

    CAMERA_FUNCTION_EXIT();
}

//------------------------------------------------------------------------------
//
// Function: CameraImagicDeinit
//
// Deinitialize the Imagic Sensor.
//
// Parameters:
//      None.
//
// Returns:
//      None.
//
//------------------------------------------------------------------------------
void CameraImagicDeinit(void)
{
    CAMERA_FUNCTION_ENTRY();

    CloseHandle(hI2C);

    CAMERA_FUNCTION_EXIT();
}


//------------------------------------------------------------------------------
//
// Function: CameraImagicSetOutputFormat
//
// Set interface to configure camera output format.
//
// Parameters:
//      outputFormat
//          [in] structure indicating the sensor output format:
//              csiSensorOutputFormat_CCIR656 - CCIR656
//              csiSensorOutputFormat_RGB565 - RGB565
//
// Returns:
//      None.
//
//------------------------------------------------------------------------------
void CameraImagicSetOutputFormat(cameraImagicOutputFormat outputFormat)
{
    WORD data;
    INT iResult;
    BYTE bySlaveAddr;

    if (csiSensorId_Saa7113h == gSensorInUse)
        return;

    CAMERA_FUNCTION_ENTRY();

    switch (gSensorInUse)
    {
       case  csiSensorId_Imagic8803:
               bySlaveAddr = IM8803_I2C_ADDRESS;
               break;
               
       case csiSensorId_Imagic8201:
               bySlaveAddr = IM8201_I2C_ADDRESS;
               break;

       default:
               DEBUGMSG(ZONE_ERROR, (TEXT("%s: We don't support this kind of sensor!\r\n"), __WFUNCTION__));
               return;
    }        
  
    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x01, 0x00, 0x01, &iResult); // select IFP register
    data = I2CReadTwoBytes(hI2C, bySlaveAddr, 0x08, &iResult);
    
    switch (outputFormat)
    {
        case cameraImagicOutputFormat_CCIR656:
              data &= ~0x1000;    // Clear bit 12
              break;
        case cameraImagicOutputFormat_RGB565:        
              data |= 0x1000;        // Set bit 12
              break;
    }

    I2CWriteTwoBytes(hI2C, bySlaveAddr, 0x08, *((BYTE *)&data + 1), (BYTE)data, &iResult); // select IFP register

    CAMERA_FUNCTION_EXIT();
}


//------------------------------------------------------------------------------
//
// Function: CameraImagicSetOutputResolution
//
// Set interface to configure camera output resolution.
//
// Parameters:
//      outputResolution
//          [in]  Structure indicating the camera output resolution:
//              csiSensorOutputResolution outputResolution :
//              csiSensorOutputResolution_VGA - VGA 640x480
//              csiSensorOutputResolution_QVGA - QVGA 320x240
//              csiSensorOutputResolution_CIF - CIF 352x288
//              csiSensorOutputResolution_QCIF - QCIF 176x144
//              csiSensorOutputResolution_QQVGA - QQVGA 160x120
//
// Returns:
//      None.
//
//------------------------------------------------------------------------------
void CameraImagicSetOutputResolution(csiSensorOutputResolution outputResolution)
{
    INT iResult;
    BYTE bySlaveAddr;

    if (csiSensorId_Saa7113h == gSensorInUse)
        return;

    CAMERA_FUNCTION_ENTRY();

    switch (gSensorInUse)
    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -