/* * Transfer (i.e. exchange) one "word" with selected SPI device. * Typically one word is 8-bits (an octet), but it does not need to be, * this function is word-width agnostic - when using the SSC. * However, for PIO bit-banging, then one word is explicitly * unconditionally assumed to be exactly 8-bits in length. * * input: "out" is word to be send to slave SPI device * returns: one word read from the slave SPI device * * It is the caller's responsibility to ensure that the * chip select (SPI_NOTCS) is correctly asserted. */ static unsigned int spi_xfer_one_word(const unsigned int out) { unsigned int in = 0; #if defined(CONFIG_SOFT_SPI) /* Use PIO Bit-Banging for SPI */ signed int i; for(i=7; i>=0; i--) /* for each bit in turn ... */ { /* do 8 bits, msb first */ SPI_SCL(0); /* SPI_CLK = low */ SPI_SDA(out & (1u<<i)); /* output next bit on SPI_DOUT */ SPI_DELAY; /* clock low cycle */ SPI_SCL(1); /* SPI_CLK = high */ SPI_DELAY; /* sample on RISING clock edge */ in <<= 1; /* shift */ in |= SPI_READ; /* get next bit from SPI_DIN */ } #else /* Use SSC for SPI */ /* write out data 'out' */ ssc_write(SSC_TBUF, out); /* wait for Receive Buffer Full flag to be asserted */ while ((ssc_read(SSC_STAT) & SSC_STAT_RIR) == 0) { ; /* busy poll - do nothing */ } /* read in data */ in = ssc_read(SSC_RBUF); #endif /* CONFIG_SOFT_SPI */ /* return exchanged data */ return in; }
static void spi_write(unsigned char b) { for(int i = 7; i >= 0; i--) { SPI_SCL(false); spi_delay(); SPI_SDO((b >> i) & 1); spi_delay(); SPI_SCL(true); spi_delay(); } }
void spi_cs_activate(struct spi_slave *slave) { gpio_set_value(CONFIG_SOFT_SPI_GPIO_CS, !(CONFIG_SOFT_SPI_MODE & SPI_CS_HIGH)); SPI_SCL(1); gpio_set_value(CONFIG_SOFT_SPI_GPIO_CS, CONFIG_SOFT_SPI_MODE & SPI_CS_HIGH); }