def spisend(bytes_to_send): """Sends bytes via the SPI bus""" global spidev_fd if spidev_fd is None: raise InitError("Before spisend(), call init().") # make some buffer space to store reading/writing write_bytes = bytes(bytes_to_send) wbuffer = ctypes.create_string_buffer(write_bytes, len(write_bytes)) rbuffer = ctypes.create_string_buffer(len(bytes_to_send)) # create the spi transfer struct transfer = _spi_ioc_transfer( tx_buf=ctypes.addressof(wbuffer), rx_buf=ctypes.addressof(rbuffer), len=ctypes.sizeof(wbuffer)) # send the spi command (with a little help from asm-generic iomsg = _IOW(SPI_IOC_MAGIC, 0, ctypes.c_char*ctypes.sizeof(transfer)) ioctl(spidev_fd, iomsg, ctypes.addressof(transfer)) return ctypes.string_at(rbuffer, ctypes.sizeof(rbuffer))
def SPI_IOC_MESSAGE(N): return _IOW(SPI_IOC_MAGIC, 0, c_char * SPI_MSGSIZE(N))
def SPI_IOC_MESSAGE(N): return _IOW(SPI_IOC_MAGIC, 0, c_char*SPI_MSGSIZE(N))
__slots__ = [name for name, type in _fields_] # not all platforms use <asm-generic/ioctl.h> or _IOC_TYPECHECK() ... def SPI_MSGSIZE(N): if ((N) * (sizeof(spi_ioc_transfer))) < (1 << _IOC_SIZEBITS): return (N) * (sizeof(spi_ioc_transfer)) else: return 0 def SPI_IOC_MESSAGE(N): return _IOW(SPI_IOC_MAGIC, 0, c_char * SPI_MSGSIZE(N)) # Read / Write of SPI mode (SPI_MODE_0..SPI_MODE_3) SPI_IOC_RD_MODE = _IOR(SPI_IOC_MAGIC, 1, c_uint8) SPI_IOC_WR_MODE = _IOW(SPI_IOC_MAGIC, 1, c_uint8) # Read / Write SPI bit justification SPI_IOC_RD_LSB_FIRST = _IOR(SPI_IOC_MAGIC, 2, c_uint8) SPI_IOC_WR_LSB_FIRST = _IOW(SPI_IOC_MAGIC, 2, c_uint8) # Read / Write SPI device word length (1..N) SPI_IOC_RD_BITS_PER_WORD = _IOR(SPI_IOC_MAGIC, 3, c_uint8) SPI_IOC_WR_BITS_PER_WORD = _IOW(SPI_IOC_MAGIC, 3, c_uint8) # Read / Write SPI device default max speed hz SPI_IOC_RD_MAX_SPEED_HZ = _IOR(SPI_IOC_MAGIC, 4, c_uint32) SPI_IOC_WR_MAX_SPEED_HZ = _IOW(SPI_IOC_MAGIC, 4, c_uint32)
__slots__ = [name for name,type in _fields_] # not all platforms use <asm-generic/ioctl.h> or _IOC_TYPECHECK() ... def SPI_MSGSIZE(N): if ((N)*(sizeof(spi_ioc_transfer))) < (1 << _IOC_SIZEBITS): return (N)*(sizeof(spi_ioc_transfer)) else: return 0 def SPI_IOC_MESSAGE(N): return _IOW(SPI_IOC_MAGIC, 0, c_char*SPI_MSGSIZE(N)) # Read / Write of SPI mode (SPI_MODE_0..SPI_MODE_3) SPI_IOC_RD_MODE = _IOR(SPI_IOC_MAGIC, 1, c_uint8) SPI_IOC_WR_MODE = _IOW(SPI_IOC_MAGIC, 1, c_uint8) # Read / Write SPI bit justification SPI_IOC_RD_LSB_FIRST = _IOR(SPI_IOC_MAGIC, 2, c_uint8) SPI_IOC_WR_LSB_FIRST = _IOW(SPI_IOC_MAGIC, 2, c_uint8) # Read / Write SPI device word length (1..N) SPI_IOC_RD_BITS_PER_WORD = _IOR(SPI_IOC_MAGIC, 3, c_uint8) SPI_IOC_WR_BITS_PER_WORD = _IOW(SPI_IOC_MAGIC, 3, c_uint8) # Read / Write SPI device default max speed hz SPI_IOC_RD_MAX_SPEED_HZ = _IOR(SPI_IOC_MAGIC, 4, c_uint32) SPI_IOC_WR_MAX_SPEED_HZ = _IOW(SPI_IOC_MAGIC, 4, c_uint32)