📄 i2c.cc
字号:
//// Copyright (c) 2004 by Istv醤 V醨adi//// This file is part of dxr3Player, a DVD player written specifically // for the DXR3 (aka Hollywood+) decoder card, but now handles other// hardware as well. These files contain a (mostly) user-space driver // for the Unichrome board found on Via's EPIA motherboards.//// The information for implementing this driver has been gathered// from the following sources://// - The DirectFB Unichrome driver// Copyright (c) 2003 Andreas Robinson, All rights reserved.//// - Andreas Robinson's MPEG-2 decoder for the Unichrome board.// Copyright (c) 2003 Andreas Robinson, All rights reserved.//// - Via's Unichrome Framebuffer driver// Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.// Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//------------------------------------------------------------------------------#include "I2C.h"#include "IO.h"//------------------------------------------------------------------------------using unichrome::I2C;//------------------------------------------------------------------------------I2C::I2C(IO& io) : io(io), currentData(0){}//------------------------------------------------------------------------------I2C::~I2C(){}//------------------------------------------------------------------------------unsigned I2C::write(unsigned address, unsigned subAddress, unsigned data){ unsigned ack = 1; sendStart(); if (ack) ack = writeData(address); if (ack) ack = writeData(subAddress); if (ack) ack = writeData(data); sendStop(); return ack;}//------------------------------------------------------------------------------void I2C::enable(){ io.sequencer.writeMasked(I2C_PORT_INDEX, 1<<BIT_I2C_ENABLE, MASK_I2C_ENABLE);}//------------------------------------------------------------------------------unsigned I2C::write(unsigned address, const void* from, size_t size){ unsigned count = 0, ack = 1; const unsigned char* source = reinterpret_cast<const unsigned char*>(from); sendStart(); if (ack) ack = writeData(address); for(size_t i = 0; i<size && ack; ++i, ++source) { ack = writeData(*source); if (ack) ++count; } sendStop(); return count;}//------------------------------------------------------------------------------int I2C::read(unsigned address, unsigned subAddress){ unsigned ack = 1; int data = -1; // If any acknowledge fails, it will remain -1 sendStart(); if (ack) ack = writeData(address); if (ack) ack = writeData(subAddress); sendStop(); if (ack) { sendStart(); ack = writeData(address+1); if (ack) { writeSDA(1); data = 0; for(size_t i = 0; i<8; ++i) { data <<= 1; writeSCL(1); data |= readSDA()&0x01; writeSCL(0); } } sendStop(); } return data;}//------------------------------------------------------------------------------void I2C::writeSCL(unsigned on){ on = on ? 1 : 0; io.sequencer.writeMasked(I2C_PORT_INDEX, (currentData<<BIT_I2C_DATA_OUT)|(on<<BIT_I2C_CLOCK), MASK_I2C_DATA_OUT|MASK_I2C_CLOCK);}//------------------------------------------------------------------------------void I2C::writeSDA(unsigned on){ currentData = on ? 1 : 0; io.sequencer.writeMasked(I2C_PORT_INDEX, currentData<<BIT_I2C_DATA_OUT, MASK_I2C_DATA_OUT);}//------------------------------------------------------------------------------unsigned I2C::readSDA(){ uint8_t i2c = io.sequencer.read(I2C_PORT_INDEX); return (i2c>>BIT_I2C_DATA_IN)&0x01;}//------------------------------------------------------------------------------unsigned I2C::getAcknowledgement(){ writeSDA(1); writeSCL(1); unsigned ack = !readSDA(); writeSCL(0); return ack;}//------------------------------------------------------------------------------void I2C::sendStart(){ writeSDA(1); writeSCL(1); writeSDA(0); writeSCL(0);}//------------------------------------------------------------------------------void I2C::sendStop(){ writeSDA(0); writeSCL(1); writeSDA(1);}//------------------------------------------------------------------------------unsigned I2C::writeData(unsigned data){ for(size_t i = 0; i<8; ++i, data<<=1) { writeSDA( data&0x80 ); writeSCL(1); writeSCL(0); } return getAcknowledgement();}//------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -