Example #1
0
int
SPI::transfer(uint8_t *send, uint8_t *recv, unsigned len)
{
	int result;

	if ((send == nullptr) && (recv == nullptr))
		return -EINVAL;

	LockMode mode = up_interrupt_context() ? LOCK_NONE : locking_mode;

	/* lock the bus as required */
	switch (mode) {
	default:
	case LOCK_PREEMPTION:
		{
			irqstate_t state = irqsave();
			result = _transfer(send, recv, len);
			irqrestore(state);
		}
		break;
	case LOCK_THREADS:
		SPI_LOCK(_dev, true);
		result = _transfer(send, recv, len);
		SPI_LOCK(_dev, false);
		break;
	case LOCK_NONE:
		result = _transfer(send, recv, len);
		break;
	}
	return result;
}
Example #2
0
bool KnobModulo::_init()
{
    uint8_t positionData[2];
    if (_transfer(FUNCTION_KNOB_GET_POSITION, 0, 0, positionData, 2)) {
        _position = positionData[0] | (positionData[1] << 8);
    }

    uint8_t buttonData[1];
    if (_transfer(FUNCTION_KNOB_GET_BUTTON, 0, 0, buttonData, 1)) {
        _buttonState = buttonData[0];
    }

    if (_positionChangeCallback) {
        _positionChangeCallback(*this);
    }
}
Example #3
0
bool DisplayModulo::isEmpty() {
    uint8_t empty = 0;
    if (_transfer(FUNCTION_IS_EMPTY, 0, 0, &empty, 1)) {
        return empty;
    }
    return true;
}
Example #4
0
int
MS5611_SPI::_measure(unsigned addr)
{
	uint8_t cmd = addr | DIR_WRITE;

	return _transfer(&cmd, nullptr, 1);
}
Example #5
0
int
MS5611_SPI::_reset()
{
	uint8_t cmd = ADDR_RESET_CMD | DIR_WRITE;

	return  _transfer(&cmd, nullptr, 1);
}
Example #6
0
bool DisplayModulo::isComplete() {
    uint8_t complete = 0;
    if (_transfer(FUNCTION_IS_COMPLETE, 0, 0, &complete, 1)) {
        return complete;
    }
    return true;
}
Example #7
0
uint16_t
MS5611_SPI::_reg16(unsigned reg)
{
	uint8_t cmd[3] = { (uint8_t)(reg | DIR_READ), 0, 0 };

	_transfer(cmd, cmd, sizeof(cmd));

	return (uint16_t)(cmd[1] << 8) | cmd[2];
}
Example #8
0
void DisplayModulo::_sendOp(uint8_t *data, uint8_t len) {
    while (_availableSpace < len) {
        uint8_t receiveData[2] = {0,0};
        if (_transfer(FUNCTION_GET_AVAILABLE_SPACE, 0, 0, receiveData, 2)) {
            _availableSpace = receiveData[0] | (receiveData[1] << 8);
        } else {
            return;
        }


        if (_availableSpace < len) {
            delay(5);
        }
    }

    _availableSpace -= len;

    _transfer(FUNCTION_APPEND_OP, data, len, 0, 0);
}
Example #9
0
void DisplayModulo::setCurrent(float current) {
    // Before changing contrast we must wait until no drawing operations are
    // still in progress.
    while (!isComplete()) {
        delay(5);
    }

    uint8_t sendData[] = {static_cast<uint8_t>(15*current)};
    _transfer(FUNCTION_SET_CURRENT, sendData, 1, 0, 0);
}
Example #10
0
void DisplayModulo::setContrast(float r, float g, float b) {
    // Before changing contrast we must wait until no drawing operations are
    // still in progress.
    while (!isComplete()) {
        delay(5);
    }

    uint8_t sendData[] = {static_cast<uint8_t>(255*r),
                          static_cast<uint8_t>(255*g),
                          static_cast<uint8_t>(255*b)
                         };
    _transfer(FUNCTION_SET_CONTRAST, sendData, 3, 0, 0);
}
void
transfer(char *entry, int howto, int od, int csc, char *lr, char *es)
{

	printf("Entry point: 0x%lx\n", (u_long)entry);

#ifdef EXEC_DEBUG
	printf("\n\nReturn to boot...\n");
	(void)getchar();
#endif

	_transfer(entry, howto, od, csc, lr, es);
}
Example #12
0
static int virtual_switch_get_status(HSB_STATUS_T *status)
{
	uint8_t wbuf[64], rbuf[64];
	int ret, len;

	VS_DEV_T *pdev = _find_dev_by_id(status->devid);
	if (!pdev)
		return HSB_E_ENTRY_NOT_FOUND;

	memset(wbuf, 0, sizeof(wbuf));
	len = 8;
	SET_CMD_FIELD(wbuf, 0, uint16_t, VS_CMD_GET_STATUS);
	SET_CMD_FIELD(wbuf, 2, uint16_t, len);
	SET_CMD_FIELD(wbuf, 4, uint32_t, 0xffffffff);

	ret = _transfer(&pdev->ip, wbuf, len, rbuf, sizeof(rbuf));

	if (ret < MIN_VS_CMD_LEN) {
		hsb_critical("get status: get err pkt, len=%d\n", ret);
		return HSB_E_INVALID_MSG;
	}

	uint16_t cmd = GET_CMD_FIELD(rbuf, 0, uint16_t);
	uint16_t rlen = GET_CMD_FIELD(rbuf, 2, uint16_t);

	if (cmd != VS_CMD_GET_STATUS_RESP || ret != rlen) {
		hsb_critical("get status: get err pkt, cmd=%x, len=%d\n", cmd, rlen);
		return HSB_E_INVALID_MSG;
	}

	int status_num = 0;
	uint16_t id, val;
	len = 4;

	while (len + 4 <= rlen) {
		status->id[status_num] = GET_CMD_FIELD(rbuf, len, uint16_t);
		status->val[status_num] = GET_CMD_FIELD(rbuf, len + 2, uint16_t);

		status_num ++;
		len += 4;

		if (status_num >= status->num)
			break;
	}

	status->num = status_num;

	return HSB_E_OK;
}
Example #13
0
void DisplayModulo::_rawWrite(bool dataMode, uint8_t *data, size_t len) {
    if (len > 30) {
        return;
    }

    // Before issuing a raw write we must wait until no drawing operations are
    // still in progress.
    while (!isComplete()) {
        delay(5);
    }

    uint8_t sendData[32] = {dataMode};
    for  (int i=0; i < len; i++) {
        sendData[i+1] = data[i];
    }
    _transfer(FUNCTION_RAW_WRITE, sendData, len+1, 0, 0);
}
Example #14
0
static int virtual_switch_set_status(const HSB_STATUS_T *status)
{
	int ret, len;
	uint8_t wbuf[64];
	uint8_t rbuf[64];
	HSB_STATUS_T *pstat = (HSB_STATUS_T *)status;

	VS_DEV_T *pdev = _find_dev_by_id(pstat->devid);
	if (!pdev)
		return HSB_E_ENTRY_NOT_FOUND;

	len = 4 + 4 * pstat->num;
	memset(wbuf, 0, sizeof(wbuf));
	SET_CMD_FIELD(wbuf, 0, uint16_t, VS_CMD_SET_STATUS);
	SET_CMD_FIELD(wbuf, 2, uint16_t, len);

	int id;
	for (id = 0; id < pstat->num; id++) {
		SET_CMD_FIELD(wbuf, 4 + id * 4, uint16_t, pstat->id[id]);
		SET_CMD_FIELD(wbuf, 6 + id * 4, uint16_t, pstat->val[id]);
	}

	ret = _transfer(&pdev->ip, wbuf, len, rbuf, sizeof(rbuf));

	if (ret < MIN_VS_CMD_LEN) {
		hsb_critical("set status: get err pkt, len=%d\n", ret);
		return HSB_E_INVALID_MSG;
	}

	uint16_t cmd = GET_CMD_FIELD(rbuf, 0, uint16_t);
	uint16_t rlen = GET_CMD_FIELD(rbuf, 2, uint16_t);
	uint16_t result = GET_CMD_FIELD(rbuf, 4, uint16_t);

	if (cmd != VS_CMD_RESULT || rlen != ret) {
		hsb_critical("set status: get err pkt, cmd=%x, len=%d\n", cmd, rlen);
		return HSB_E_INVALID_MSG;
	}

	if (!result) {
		hsb_debug("set status result=%d\n", result);
		return HSB_E_ACT_FAILED;
	}

	return HSB_E_OK;
}
Example #15
0
int i2c_write_bytes(i2c_t dev, uint8_t address, char *data, int length)
{
    I2C_TransferSeq_TypeDef transfer;

    transfer.addr = address;
    transfer.flags = I2C_FLAG_WRITE;
    transfer.buf[0].data = (uint8_t *) data;
    transfer.buf[0].len = length;

    /* start a transfer */
    _transfer(dev, &transfer);

    if (i2c_progress[dev] != i2cTransferDone) {
        return -2;
    }

    return length;
}
Example #16
0
int i2c_read_bytes(i2c_t dev, uint8_t address, void *data, int length)
{
    I2C_TransferSeq_TypeDef transfer;

    transfer.addr = (address << 1);
    transfer.flags = I2C_FLAG_READ;
    transfer.buf[0].data = (uint8_t *) data;
    transfer.buf[0].len = length;

    /* start a transfer */
    _transfer(dev, &transfer);

    if (i2c_progress[dev] != i2cTransferDone) {
        return -2;
    }

    return length;
}
Example #17
0
static int virtual_switch_set_action(const HSB_ACTION_T *act)
{
	int ret, len;
	uint8_t wbuf[64];
	uint8_t rbuf[64];

	VS_DEV_T *pdev = _find_dev_by_id(act->devid);
	if (!pdev)
		return HSB_E_ENTRY_NOT_FOUND;

	len = 12;
	memset(wbuf, 0, sizeof(wbuf));
	SET_CMD_FIELD(wbuf, 0, uint16_t, VS_CMD_DO_ACTION);
	SET_CMD_FIELD(wbuf, 2, uint16_t, len);
	SET_CMD_FIELD(wbuf, 4, uint16_t, act->id);
	SET_CMD_FIELD(wbuf, 6, uint16_t, act->param1);
	SET_CMD_FIELD(wbuf, 8, uint32_t, act->param2);

	ret = _transfer(&pdev->ip, wbuf, len, rbuf, sizeof(rbuf));

	if (ret < MIN_VS_CMD_LEN) {
		hsb_critical("set action: get err pkt, len=%d\n", ret);
		return HSB_E_INVALID_MSG;
	}

	uint16_t cmd = GET_CMD_FIELD(rbuf, 0, uint16_t);
	uint16_t rlen = GET_CMD_FIELD(rbuf, 2, uint16_t);
	uint16_t result = GET_CMD_FIELD(rbuf, 4, uint16_t);

	if (cmd != VS_CMD_RESULT || rlen != ret) {
		hsb_critical("set action: get err pkt, cmd=%x, len=%d\n", cmd, rlen);
		return HSB_E_INVALID_MSG;
	}

	if (!result) {
		hsb_debug("set action result=%d\n", result);
		return HSB_E_ACT_FAILED;
	}

	return HSB_E_OK;
}
Example #18
0
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg,
                   const void *data, int length)
{
    I2C_TransferSeq_TypeDef transfer;

    transfer.addr = (address << 1);
    transfer.flags = I2C_FLAG_WRITE_WRITE;
    transfer.buf[0].data = &reg;
    transfer.buf[0].len = 1;
    transfer.buf[1].data = (uint8_t *) data;
    transfer.buf[1].len = length;

    /* start a transfer */
    _transfer(dev, &transfer);

    if (i2c_progress[dev] != i2cTransferDone) {
        return -2;
    }

    return length;
}
/**
This method transfers a buffer of data on the bus.
\param[in] dataOut The data to send on the SPI bus. If the parameter is NULL, 0's will be sent
\param[in] datIn The data received on the SPI bus. If this parameter is NULL, data in will be ignored
\param[in] bufferBytes the size of each of the buffers
\return HRESULT success or error code.
*/
HRESULT BtSpiControllerClass::transferBuffer(PBYTE dataOut, PBYTE dataIn, size_t bufferBytes)
{
    ULONG tempDataIn = 0;
    HRESULT hr;
    for (size_t i = 0; i < bufferBytes; i++)
    {
        hr = _transfer(dataOut ? dataOut[i] : 0, tempDataIn, 8);

        if (FAILED(hr))
        {
            break;
        }

        if (dataIn)
        {
            dataIn[i] = (BYTE)tempDataIn;
        }
    }

    return hr;
}
Example #20
0
static int gridseed_pl2303_init(struct cgpu_info *gridseed, int interface)
{
	// Set Data Control
	transfer(gridseed, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL,
			 interface, C_SETDATA);

	if (gridseed->usbinfo.nodev)
		return -1;

	// Set Line Control
	uint32_t ica_data[2] = { PL2303_VALUE_LINE0, PL2303_VALUE_LINE1 };
	_transfer(gridseed, PL2303_CTRL_OUT, PL2303_REQUEST_LINE, PL2303_VALUE_LINE,
			 interface, &ica_data[0], PL2303_VALUE_LINE_SIZE, C_SETLINE);

	if (gridseed->usbinfo.nodev)
		return -1;

	// Vendor
	transfer(gridseed, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, PL2303_VALUE_VENDOR,
			 interface, C_VENDOR);

	return 0;
}
Example #21
0
static int gridseed_cp210x_init(struct cgpu_info *gridseed, int interface)
{
	// Enable the UART
	transfer(gridseed, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE, CP210X_VALUE_UART_ENABLE,
			interface, C_ENABLE_UART);

	if (gridseed->usbinfo.nodev)
		return -1;

	// Set data control
	transfer(gridseed, CP210X_TYPE_OUT, CP210X_REQUEST_DATA, CP210X_VALUE_DATA,
			interface, C_SETDATA);

	if (gridseed->usbinfo.nodev)
		return -1;

	// Set the baud
	uint32_t data = CP210X_DATA_BAUD;
	_transfer(gridseed, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD, 0,
			interface, &data, sizeof(data), C_SETBAUD);

	return 0;
}
Example #22
0
int
MS5611_SPI::read(unsigned offset, void *data, unsigned count)
{
	union _cvt {
		uint8_t	b[4];
		uint32_t w;
	} *cvt = (_cvt *)data;
	uint8_t buf[4] = { 0 | DIR_WRITE, 0, 0, 0 };

	/* read the most recent measurement */
	int ret = _transfer(&buf[0], &buf[0], sizeof(buf));

	if (ret == OK) {
		/* fetch the raw value */
		cvt->b[0] = buf[3];
		cvt->b[1] = buf[2];
		cvt->b[2] = buf[1];
		cvt->b[3] = 0;

		ret = count;
	}

	return ret;
}
Example #23
0
bool KnobModulo::setColor(float r, float g, float b) {
    uint8_t sendData[3] = {static_cast<uint8_t>(r*255),
        static_cast<uint8_t>(g*255),
        static_cast<uint8_t>(b*255)};
    return _transfer(FUNCTION_KNOB_SET_COLOR, sendData, 3, 0, 0);
}
uint32_t SPIDevice::getLastTransmissionResult()
{
    return _transfer(0x18, 0, 0);
}