static int m_spi_deselect(lua_State * L) { struct lspi_t * spi = luaL_checkudata(L, 1, MT_HARDWARE_SPI); spi_device_deselect(spi->dev); lua_settop(L, 1); return 1; }
static void ssd1309_write_command(struct fb_ssd1309_pdata_t * pdat, u8_t cmd) { spi_device_select(pdat->dev); gpio_set_value(pdat->cd, 0); spi_device_write_then_read(pdat->dev, &cmd, 1, 0, 0); gpio_set_value(pdat->cd, 1); spi_device_deselect(pdat->dev); }
void spi_device_init(struct spi_device *device) { pin_enable(device->chip_select); spi_device_deselect(device); pin_set_mode(device->chip_select, PIN_MODE_OUTPUT); pin_set_otype(device->chip_select, PIN_TYPE_PUSHPULL); pin_set_ospeed(device->chip_select, PIN_SPEED_2MHZ); }
/** * Perform a SPI read/write transfer using interrupts. */ ssize_t spi_transfer(struct spi_bus *bus, struct spi_device *dev, uint32_t timeout, const uint8_t *tx_buf, uint8_t *rx_buf, size_t len) { bool error = false; if (len == 0) /* ensure at least one byte of TX/RX */ return 0; if (xSemaphoreTake(bus->lock, timeout) == pdFALSE) return -1; spi_enable(bus, dev); spi_device_select(dev); bus->transfer.tx_buf = tx_buf; bus->transfer.rx_buf = rx_buf; bus->transfer.tx_len = len; bus->transfer.rx_len = len; interrupt_enable(bus->irq); spi_cr2_set(bus, SPI_CR2_TXEIE | SPI_CR2_ERRIE); /* XXX We may not be able to successfully communicate on this bus * after we time out like this without doing something special to * clear the error state. Most likely we're going to panic anyway, * though. */ if (!xSemaphoreTake(bus->complete, timeout)) error = true; spi_cr2_clear(bus, SPI_CR2_TXEIE | SPI_CR2_RXNEIE); interrupt_disable(bus->irq); /* According to the reference manual, we must wait until TXE=1 and * BSY=0 before disabling the SPI. Don't bother doing this if the * transfer has timed out. */ if (error) { BUSY_UNTIL_SET(bus->dev->SR, SPI_SR_TXE); BUSY_UNTIL_CLEAR(bus->dev->SR, SPI_SR_BSY); } spi_device_deselect(dev); spi_disable(bus); xSemaphoreGive(bus->lock); return error ? -1 : len - bus->transfer.rx_len; }
static void ssd1309_write_data(struct fb_ssd1309_pdata_t * pdat, u8_t dat) { spi_device_select(pdat->dev); spi_device_write_then_read(pdat->dev, &dat, 1, 0, 0); spi_device_deselect(pdat->dev); }