예제 #1
0
파일: SPIDevice.go 프로젝트: xqbumu/goPi
// Sends bytes over SPI channel and returns []byte response
func (spi *SPIDevice) Send(bytes_to_send [3]byte) ([]byte, error) {
	wBuffer := bytes_to_send
	rBuffer := [3]byte{}

	// generates message
	transfer := SPI_IOC_TRANSFER{}
	transfer.txBuf = uint64(uintptr(unsafe.Pointer(&wBuffer)))
	transfer.rxBuf = uint64(uintptr(unsafe.Pointer(&rBuffer)))
	transfer.length = uint32(unsafe.Sizeof(wBuffer))
	transfer.delayUsecs = SPI_DELAY
	transfer.bitsPerWord = spi.bpw
	transfer.speedHz = spi.speed

	// sends message over SPI
	err := ioctl.IOCTL(spi.file.Fd(), SPI_IOC_MESSAGE(1), uintptr(unsafe.Pointer(&transfer)))
	if err != nil {
		return nil, fmt.Errorf("Error on sending: %s\n", err)
	}

	// generates a valid response
	ret := make([]byte, unsafe.Sizeof(rBuffer))
	for i := range ret {
		ret[i] = rBuffer[i]
	}

	return ret, nil
}
예제 #2
0
파일: SPIDevice.go 프로젝트: xqbumu/goPi
func (spi *SPIDevice) SetMode(mode uint8) error {
	spi.mode = mode
	err := ioctl.IOCTL(spi.file.Fd(), SPI_IOC_WR_MODE(), uintptr(unsafe.Pointer(&mode)))
	if err != nil {
		return fmt.Errorf("Error setting mode: %s\n", err)
	}
	return nil
}
예제 #3
0
파일: SPIDevice.go 프로젝트: xqbumu/goPi
func (spi *SPIDevice) SetSpeed(speed uint32) error {
	spi.speed = speed
	err := ioctl.IOCTL(spi.file.Fd(), SPI_IOC_WR_MAX_SPEED_HZ(), uintptr(unsafe.Pointer(&speed)))
	if err != nil {
		return fmt.Errorf("Error setting speed: %s\n", err)
	}
	return nil
}
예제 #4
0
파일: SPIDevice.go 프로젝트: xqbumu/goPi
func (spi *SPIDevice) SetBitsPerWord(bpw uint8) error {
	spi.bpw = bpw
	err := ioctl.IOCTL(spi.file.Fd(), SPI_IOC_WR_BITS_PER_WORD(), uintptr(unsafe.Pointer(&bpw)))
	if err != nil {
		return fmt.Errorf("Error setting bits per word: %s\n", err)
	}
	return nil
}