def transfer_value(value): """Reads and writes one byte from and to the SPI. @var value: the byte to write @return: the byte that was read """ # clear the TX and RX fifos regs.SPI0CS |= 0x30 # set the TA flag regs.SPI0CS |= 0x80 # wait for TXD flag while regs.SPI0CS & 0x40000 == 0: utils.nanosleep(0, 5) # write the value to the TX fifo regs.SPI0FIFO = value & 0xFF # wait for DONE flag to be set while regs.SPI0CS & 0x10000 == 0: utils.nanosleep(0, 5) # read the received value value = regs.SPI0FIFO # clear the TA flag regs.SPI0CS &= 0xFFFFFF7F return value
def configure(gpio, pull): '''Configures the pull-up/down for the specified GPIO pin. @param gpio: the number of the GPIO pin @param pull: set to True to enable pull-up, False to enable pull-down or None to disable the pull function. ''' if gpio < 0 or gpio > funcs.MAX_GPIO: raise Exception('Invalid GPIO number') if pull: # pull-up regs.GPPUD = 2 elif pull is False: # pull-down regs.GPPUD = 1 else: # None - disable pull regs.GPPUD = 0 utils.nanosleep(0, 5) reg_no = gpio // 32 bit_no = gpio % 32 if reg_no == 0: regs.GPPUDCLK0 = 1 << bit_no else: regs.GPPUDCLK1 = 1 << bit_no utils.nanosleep(0, 5) regs.GPPUD = 0 utils.nanosleep(0, 5) if reg_no == 0: regs.GPPUDCLK0 = 0 else: regs.GPPUDCLK1 = 0 utils.nanosleep(0, 5)
def transfer_values(values): """Reads and writes more bytes from and to the SPI. @var values: the list of bytes to write @return: the bytes that were read """ # clear the TX and RX fifos regs.SPI0CS |= 0x30 # set the TA flag regs.SPI0CS |= 0x80 read_values = [] for value in values: # wait for TXD flag to be set while regs.SPI0CS & 0x40000 == 0: utils.nanosleep(0, 5) # write the value to the TX fifo regs.SPI0FIFO = value & 0xFF # wait for RXD flag to be set while regs.SPI0CS & 0x20000 == 0: utils.nanosleep(0, 5) # read the received value read_values.append(regs.SPI0FIFO) # wait for DONE flag to be set while regs.SPI0CS & 0x10000 == 0: utils.nanosleep(0, 5) # clear the TA flag regs.SPI0CS &= 0xFFFFFF7F return read_values