void xs_i2c_readBlock(xsMachine *the) { FskErr err; xsI2C i2c = xsGetHostData(xsThis); int argc = xsToInteger(xsArgc), i; int format = 2; SInt32 dataSize = xsToInteger(xsArg(0)), readCount; UInt8 data[32]; xsThrowIfNULL(i2c); DBG_I2C("xs_i2c_readBlock\n"); if ((dataSize > 32) || (dataSize <= 0)) xsThrowDiagnosticIfFskErr(kFskErrInvalidParameter, "I2C readBlock invalid size %d. %s", (int)dataSize, i2c->diagnosticID); FskPinI2CSetAddress(i2c->pin, i2c->address); err = FskPinI2CReadBytes(i2c->pin, dataSize, &readCount, data); if (err) { xsTraceDiagnostic("I2C readBlock failed with error %s %s.", FskInstrumentationGetErrorString(err), i2c->diagnosticID); goto bail; } if (argc > 1) { int t = xsTypeOf(xsArg(1)); if ((xsNumberType == t) || (t == xsIntegerType)) format = xsToInteger(xsArg(1)); else { char *formatString = xsToString(xsArg(1)); if (0 == FskStrCompare(formatString, "Buffer")) format = 2; else if (0 == FskStrCompare(formatString, "Chunk")) format = 0; else if (0 == FskStrCompare(formatString, "Array")) format = 1; } } if (2 == format) { xsResult = xsArrayBuffer(data, readCount); } else if (0 == format) { xsResult = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(readCount)); FskMemMove(xsGetHostData(xsResult), data, readCount); } else if (1 == format) { xsResult = xsNew1(xsGlobal, xsID("Array"), xsInteger(readCount)); for (i = 0; i < readCount; i++) xsSet(xsResult, i, xsInteger(data[i])); } bail: if (err) xsError(err); }
void xs_i2c_writeByte(xsMachine *the) { xsI2C i2c = xsGetHostData(xsThis); FskErr err; UInt8 byte = (UInt8)xsToInteger(xsArg(0)); DBG_I2C("xs_i2c_writeByte: %#x\n", byte); xsThrowIfNULL(i2c); FskPinI2CSetAddress(i2c->pin, i2c->address); err = FskPinI2CWriteByte(i2c->pin, byte); xsThrowDiagnosticIfFskErr(err, "I2C writeByte failed with error %s %s.", FskInstrumentationGetErrorString(err), i2c->diagnosticID); }
void xs_i2c_writeWordDataSMB(xsMachine *the) { xsI2C i2c = xsGetHostData(xsThis); FskErr err; UInt8 command = (UInt8)xsToInteger(xsArg(0)); UInt16 word = (uint16_t)xsToInteger(xsArg(1)); DBG_I2C("xs_i2c_writeWordDataSMB write word %u from %u\n", word, command); xsThrowIfNULL(i2c); FskPinI2CSetAddress(i2c->pin, i2c->address); err = FskPinI2CWriteDataWord(i2c->pin, command, word); xsThrowDiagnosticIfFskErr(err, "I2C writeWordDataSMB register %d failed with error %s %s.", (int)command, FskInstrumentationGetErrorString(err), i2c->diagnosticID); }
void xs_i2c_writeQuickSMB(xsMachine *the) { xsI2C i2c = xsGetHostData(xsThis); FskErr err; uint8_t byte = (uint8_t)xsToInteger(xsArg(0)); DBG_I2C("xs_i2c_writeQuickSMB Writing quick byte SMB %u\n", byte); xsThrowIfNULL(i2c); FskPinI2CSetAddress(i2c->pin, i2c->address); //@@ err = FskI2CWriteQuickSMB(i2c->bus, byte); err = kFskErrOperationFailed; //@@ xsThrowDiagnosticIfFskErr(err, "I2C writeQuickSMB failed with error %s %s.", FskInstrumentationGetErrorString(err), i2c->diagnosticID); }
void xs_i2c_readByte(xsMachine *the) { xsI2C i2c = xsGetHostData(xsThis); FskErr err; UInt8 byte; DBG_I2C("xs_i2c_readByte\n"); xsThrowIfNULL(i2c); FskPinI2CSetAddress(i2c->pin, i2c->address); err = FskPinI2CReadByte(i2c->pin, &byte); xsThrowDiagnosticIfFskErr(err, "I2C readByte failed %s.", i2c->diagnosticID); xsResult = xsInteger(byte); }
FskErr createFrontAnalogRead(FskPinAnalog pin, double *value) { createFrontAnalog cfa = (createFrontAnalog)pin; FskErr err; UInt16 readValue; err = FskPinI2CSetAddress(cfa->i2c, cfa->address); if (err) return err; err = FskPinI2CReadDataWord(cfa->i2c, a2dpins[cfa->pin], &readValue); if (err) return err; *value = ((double)readValue) / 1023.0; return kFskErrNone; }
void xs_i2c_readByteDataSMB(xsMachine *the) { xsI2C i2c = xsGetHostData(xsThis); FskErr err; UInt8 command = (UInt8)xsToInteger(xsArg(0)); UInt8 byte; DBG_I2C("xs_i2c_readByteDataSMB Reading byte from %u\n", reg); xsThrowIfNULL(i2c); FskPinI2CSetAddress(i2c->pin, i2c->address); err = FskPinI2CReadDataByte(i2c->pin, command, &byte); xsThrowDiagnosticIfFskErr(err, "I2C readByteDataSMB register %d failed %s.", (int)command, i2c->diagnosticID); xsResult = xsInteger(byte); }
void xs_i2c_writeBlock(xsMachine* the) { FskErr err; xsI2C i2c = xsGetHostData(xsThis); int argc = xsToInteger(xsArgc), i; UInt8 buffer[32], *bufPtr = buffer; xsThrowIfNULL(i2c); DBG_I2C("xs_i2c_writeBlock\n"); for (i = 0; i < argc; i++) bufPtr = writeOne(the, i2c, &xsArg(i), bufPtr, buffer + sizeof(buffer)); FskPinI2CSetAddress(i2c->pin, i2c->address); err = FskPinI2CWriteBytes(i2c->pin, bufPtr - buffer, buffer); xsThrowDiagnosticIfFskErr(err, "I2C FskI2CWriteBlock failed with error %s %s.", FskInstrumentationGetErrorString(err), i2c->diagnosticID); }
void xs_i2c_readWordDataSMB(xsMachine *the) { xsI2C i2c = xsGetHostData(xsThis); UInt8 command = (UInt8)xsToInteger(xsArg(0)); FskErr err; UInt16 val; DBG_I2C("xs_i2c_readWordDataSMB Call SMB to register %d\n", command); xsThrowIfNULL(i2c); FskPinI2CSetAddress(i2c->pin, i2c->address); err = FskPinI2CReadDataWord(i2c->pin, command, &val); //@@ if (! (i2c->address == 0x36) ) xsTraceDiagnostic("I2C readWordDataSMB register %d failed %s.", (int)reg, i2c->diagnosticID); xsThrowDiagnosticIfFskErr(err, "I2C readWordDataSMB register %d failed %s.", (int)command, i2c->diagnosticID); xsResult = xsInteger(val); }
void xs_i2c_processCallSMB(xsMachine *the) { xsI2C i2c = xsGetHostData(xsThis); FskErr err; UInt8 command = (UInt8)xsToInteger(xsArg(0)); UInt16 value = (UInt16)xsToInteger(xsArg(1)); UInt16 result; DBG_I2C("xs_i2c_processCallSMB Process Call SMB to register %d, value %d\n", command, value); xsThrowIfNULL(i2c); FskPinI2CSetAddress(i2c->pin, i2c->address); err = FskPinI2CProcessCall(i2c->pin, command, value, &result); xsThrowDiagnosticIfFskErr(err, "I2C processCallSMB register %d failed %s.", (int)command, i2c->diagnosticID); xsResult = xsInteger(result); }
void xs_i2c_writeBlockDataSMB(xsMachine* the) { FskErr err; xsI2C i2c = xsGetHostData(xsThis); int argc = xsToInteger(xsArgc), i; UInt8 command = (UInt8)xsToInteger(xsArg(0)); unsigned char buffer[32], *bufPtr = buffer; xsThrowIfNULL(i2c); DBG_I2C("xs_i2c_writeBlockDataSMB\n"); FskPinI2CSetAddress(i2c->pin, i2c->address); for (i = 1; i < argc; i++) bufPtr = writeOne(the, i2c, &xsArg(i), bufPtr, buffer + sizeof(buffer)); err = FskPinI2CWriteDataBytes(i2c->pin, command, (SInt32)(bufPtr - buffer), buffer); xsThrowDiagnosticIfFskErr(err, "I2C writeBlockDataSMB register %d failed with error %s %s.", (int)command, FskInstrumentationGetErrorString(err), i2c->diagnosticID); }
void xs_i2c_readBlockDataSMB(xsMachine* the) { xsI2C i2c = xsGetHostData(xsThis); FskErr err; char* formatString; int format = 2, i; SInt32 dataSize = 0; UInt8 command = (UInt8)xsToInteger(xsArg(0)); UInt8 data[34]; //needs to be 34 because we're using I2C_SMBUS_I2C_BLOCK_BROKEN in i2cdev.c //@@ WTF - not at this layer, at least SInt32 length = (SInt32)xsToInteger(xsArg(1)); DBG_I2C("xs_i2c_readBlockDataSMB\n"); xsThrowIfNULL(i2c); if ((length < 0) || (length > 32)) xsThrowDiagnosticIfFskErr(kFskErrInvalidParameter, "I2C readBlockDataSMB bad length %d %s.", length, i2c->diagnosticID); FskPinI2CSetAddress(i2c->pin, i2c->address); formatString = xsToString(xsArg(2)); if (!FskStrCompare(formatString, "Buffer")) format = 2; else if (!FskStrCompare(formatString, "Chunk")) format = 0; else if (!FskStrCompare(formatString, "Array")) format = 1; err = FskPinI2CReadDataBytes(i2c->pin, command, length, &dataSize, data); xsThrowDiagnosticIfFskErr(err, "I2C readBlockDataSMB register %d failed %s.", (int)command, i2c->diagnosticID); if (2 == format) { xsResult = xsArrayBuffer(data, dataSize); } else if (0 == format) { xsResult = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(dataSize)); FskMemMove(xsGetHostData(xsResult), data, dataSize); } else if (1 == format) { xsResult = xsNew1(xsGlobal, xsID("Array"), xsInteger(dataSize)); for (i = 0; i < dataSize; i++) xsSet(xsResult, i, xsInteger(data[i])); } }