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

📄 raw_i2c.cpp

📁 focus FS45x windows CE driver
💻 CPP
字号:
// RAW_I2C.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <windows.h>

#define READ_MAX_RETRIES        20
#define WRITE_MAX_RETRIES       20

#define VIA_RAW_I2C_START           0x430A
#define VIA_RAW_I2C_STOP            0x430B
#define VIA_RAW_I2C_WRITE_DATA      0x430C
#define VIA_RAW_I2C_READ_DATA       0x430D

void _I2C_START();
void _I2C_STOP();
int _I2C_WriteData(BYTE Data);
BYTE _I2C_ReadData();
void I2C_Write(int DeviceID, int SubAddress, BYTE Data);
BYTE I2C_Read(int DeviceID, int SubAddress);


void _I2C_START()
{
    HDC dc;
    dc = GetDC(NULL);
    
    ExtEscape(dc, VIA_RAW_I2C_START, 0, 0, 0, 0);
}

void _I2C_STOP()
{
    HDC dc;
    dc = GetDC(NULL);
    
    ExtEscape(dc, VIA_RAW_I2C_STOP, 0, 0, 0, 0);
}

int _I2C_WriteData(BYTE Data)
{
    HDC dc;
    int RetACK;
    dc = GetDC(NULL);
    
    ExtEscape(dc, VIA_RAW_I2C_WRITE_DATA, sizeof(BYTE), (LPSTR)&Data, sizeof(int), (LPSTR)&RetACK);
    return RetACK;
}

BYTE _I2C_ReadData()
{
    HDC dc;
    dc = GetDC(NULL);
    BYTE bTmp = 0;
    
    ExtEscape(dc, VIA_RAW_I2C_READ_DATA, 0, 0, sizeof(BYTE), (LPSTR)&bTmp);
    return bTmp;
}

//////////////////////////////////////////////////////
/// Write Data(one Byte) to Desired Device on I2C
void I2C_Write(int DeviceID, int SubAddress, BYTE Data)
{
    int Retry, Done=0;
    
    for(Retry=1; Retry<=WRITE_MAX_RETRIES; Retry++)
    {
        _I2C_START();
        
        if(!_I2C_WriteData(DeviceID)) {

            _I2C_STOP();
            continue;
        }
        if(!_I2C_WriteData(SubAddress)) {

                _I2C_STOP();
                continue;
        }
        if(!_I2C_WriteData((BYTE)Data&0xFF)) {

            _I2C_STOP();
            continue;
        }
        Done = 1;
        break;
    }

    _I2C_STOP();

}

//////////////////////////////////////////////////////
/// Read Data from Desired Device on I2C
BYTE I2C_Read(int DeviceID, int SubAddress)
{
    int     Retry;
    BYTE    btmp = 0;
   
    for(Retry=1; Retry<=READ_MAX_RETRIES; Retry++)
    {
        _I2C_START();
        if(!_I2C_WriteData(DeviceID&0xFE)) {

            _I2C_STOP();
            continue;
        }
        if(!_I2C_WriteData(SubAddress)) {
                _I2C_STOP();
                continue;
        }
        break;
    }

    for(Retry=1; Retry<=READ_MAX_RETRIES; Retry++)
    {
        _I2C_START();
        if(!_I2C_WriteData(DeviceID|0x01)) {

            _I2C_STOP();
            continue;
        }
        btmp = (BYTE)(_I2C_ReadData() & 0xFF);
        _I2C_STOP();
        break;
    }

    return btmp;
}

⌨️ 快捷键说明

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