/*----------------------------------------------------------------------------*/ static size_t interfaceWrite(void *object, const void *buffer, size_t length) { struct InterfaceWrapper * const interface = object; pinSet(interface->tx); return ifWrite(interface->pipe, buffer, length); }
bool RTIMUHal::HALWrite(unsigned char slaveAddr, unsigned char regAddr, unsigned char length, unsigned char const *data, const char *errorMsg) { int result; unsigned char txBuff[MAX_WRITE_LEN + 1]; char *ifType; if (m_busIsI2C) { if (!I2CSelectSlave(slaveAddr, errorMsg)) return false; ifType = (char *)"I2C"; } else { ifType = (char *)"SPI"; } if (length == 0) { result = ifWrite(®Addr, 1); if (result < 0) { if (strlen(errorMsg) > 0) HAL_ERROR2("%s write of regAddr failed - %s\n", ifType, errorMsg); return false; } else if (result != 1) { if (strlen(errorMsg) > 0) HAL_ERROR2("%s write of regAddr failed (nothing written) - %s\n", ifType, errorMsg); return false; } } else { txBuff[0] = regAddr; memcpy(txBuff + 1, data, length); result = ifWrite(txBuff, length + 1); if (result < 0) { if (strlen(errorMsg) > 0) HAL_ERROR3("%s data write of %d bytes failed - %s\n", ifType, length, errorMsg); return false; } else if (result < (int)length) { if (strlen(errorMsg) > 0) HAL_ERROR4("%s data write of %d bytes failed, only %d written - %s\n", ifType, length, result, errorMsg); return false; } } return true; }
/*----------------------------------------------------------------------------*/ int main(void) { static const char separator[] = "-----------------------\r\n"; struct Interface * const ow = init(OneWireSsp, &owConfig); assert(ow); struct Interface * const serial = init(Serial, &serialConfig); assert(serial); struct Timer * const timer = init(GpTimer, &timerConfig); assert(timer); timerSetOverflow(timer, 2000); bool busEvent = false; ifSetCallback(ow, onBusEvent, &busEvent); bool timerEvent = false; timerSetCallback(timer, onTimerOverflow, &timerEvent); timerEnable(timer); while (1) { while (!timerEvent) barrier(); timerEvent = false; enum Result res; res = ifSetParam(ow, IF_ONE_WIRE_START_SEARCH, 0); assert(res == E_OK); do { while (!busEvent) barrier(); busEvent = false; res = ifGetParam(ow, IF_STATUS, 0); if (res != E_OK) break; uint64_t address; res = ifGetParam(ow, IF_ADDRESS, &address); assert(res == E_OK); printAddress(serial, address); } while (ifSetParam(ow, IF_ONE_WIRE_FIND_NEXT, 0) == E_OK); ifWrite(serial, separator, sizeof(separator) - 1); } return 0; }
/*----------------------------------------------------------------------------*/ static void deviceWrite(struct DeviceDriver *device) { deviceConfigIO(device, false); device->change = !device->change; memcpy(device->buffer, &device->localAddress, MEMORY_ADDRESS_SIZE); for (size_t i = 0; i < sizeof(device->buffer) - MEMORY_ADDRESS_SIZE; ++i) device->buffer[i + MEMORY_ADDRESS_SIZE] = device->change ? ~i : i; pinSet(device->led); device->state = DEVICE_PH2_DATA; #ifdef TEST_ZEROCOPY ifWrite(device->interface, device->buffer, sizeof(device->buffer)); #else const size_t bytesWritten = ifWrite(device->interface, device->buffer, sizeof(device->buffer)); if (bytesWritten == sizeof(device->buffer)) pinReset(device->led); #endif }
/*----------------------------------------------------------------------------*/ static enum Result program(struct Interface *flash, const uint8_t *buffer, size_t length, size_t address) { enum Result res; if ((res = ifSetParam(flash, IF_POSITION, &address)) != E_OK) return res; if (ifWrite(flash, buffer, length) != length) return E_INTERFACE; return E_OK; }
/*----------------------------------------------------------------------------*/ static void deviceRead(struct DeviceDriver *device) { deviceConfigIO(device, true); pinSet(device->led); device->state = DEVICE_PH1_ADDRESS; #ifdef TEST_ZEROCOPY ifWrite(device->interface, &device->localAddress, MEMORY_ADDRESS_SIZE); #else const size_t bytesWritten = ifWrite(device->interface, &device->localAddress, MEMORY_ADDRESS_SIZE); if (bytesWritten == MEMORY_ADDRESS_SIZE) { const size_t bytesRead = ifRead(device->interface, device->buffer, sizeof(device->buffer) - MEMORY_ADDRESS_SIZE); if (bytesRead == sizeof(device->buffer) - MEMORY_ADDRESS_SIZE) pinReset(device->led); } #endif }
/*----------------------------------------------------------------------------*/ void usbTrace(const char *format, ...) { if (!traceInterface) return; va_list arguments; int length; va_start(arguments, format); length = vsnprintf(traceBuffer, CONFIG_TRACE_BUFFER_SIZE - 2, format, arguments); va_end(arguments); memcpy(traceBuffer + length, "\r\n", 2); ifWrite(traceInterface, traceBuffer, (size_t)(length + 2)); }
/*----------------------------------------------------------------------------*/ static void printAddress(struct Interface *serial, uint64_t address) { const uint8_t * const overlay = (const uint8_t *)&address; uint8_t serialized[25]; uint8_t *position = serialized; for (unsigned int i = 0; i < 8; ++i) { if (i > 0) *position++ = ' '; *position++ = binToHex(overlay[i] >> 4); *position++ = binToHex(overlay[i] & 0x0F); } *position++ = '\r'; *position = '\n'; ifWrite(serial, serialized, sizeof(serialized)); }