Files
dusklight/libs/dolphin/src/vi/i2c.c
T
Luke Street 4df8ccc871 Reorganize library code into libs/ (#3119)
* Reorganize files into libs/{dolphin,JSystem,PowerPC_EABI_Support,revolution,TRK_MINNOW_DOLPHIN}

* Update configure.py and project.py for new libs structure

* Refactor `#include <dolphin/x.h>` -> `<x.h>`

* Remove `__REVOLUTION_SDK__` forwards from dolphin

* Fix dolphin/ references in revolution

* Wrap `#include <dolphin.h>` in `!__REVOLUTION_SDK__`

* Always build TRK against dolphin headers

* Resolve revolution SDK header resolution issues
2026-03-01 14:35:36 -08:00

106 lines
1.7 KiB
C

#include <dolphin/vi.h>
#include "__vi.h"
static int lastError;
static int wait4ClkHigh(void) {
int n;
for (n = 0; n < 1000; n++) {
if (__VIGetSCL() != 0) {
return 1;
}
}
lastError = 2;
return 0;
}
static int sendSlaveAddr(u8 slaveAddr) {
int i;
__VISetSDA(0);
__VISetSCL(0);
for (i = 0; i < 8; i++) {
if (slaveAddr & 0x80) {
__VISetSDA(1);
} else {
__VISetSDA(0);
}
__VISetSCL(1);
if (wait4ClkHigh() == 0) {
return 0;
}
__VISetSCL(0);
slaveAddr <<= 1;
}
__VISetSDA(1);
__VISetSCL(1);
if (wait4ClkHigh() == 0) {
return 0;
}
if (__VIGetSDA() != 0) {
lastError = 1;
return 0;
}
__VISetSCL(0);
return 1;
}
int __VISendI2CData(u8 slaveAddr, u8* pData, int nBytes) {
s32 i;
u8 data;
if (sendSlaveAddr(slaveAddr) == 0) {
return 0;
}
while (nBytes != 0) {
data = *pData++;
for (i = 0; i < 8; i++) {
if (data & 0x80) {
__VISetSDA(1);
} else {
__VISetSDA(0);
}
__VISetSCL(1);
if (wait4ClkHigh() == 0) {
return 0;
}
__VISetSCL(0);
data <<= 1;
}
__VISetSDA(1);
__VISetSCL(1);
if (wait4ClkHigh() == 0) {
return 0;
}
if (nBytes != 1 && __VIGetSDA() != 0) {
lastError = 1;
return 0;
}
__VISetSCL(0);
nBytes--;
}
__VISetSDA(0);
__VISetSCL(1);
__VISetSDA(1);
return 1;
}