[292] | 1 | /* |
---|
| 2 | * \file spi.c |
---|
| 3 | * \data 31 August 2012 |
---|
| 4 | * \author Cesar Fuguet <cesar.fuguet-tortolero@lip6.fr> |
---|
| 5 | */ |
---|
| 6 | #include <spi.h> |
---|
| 7 | |
---|
| 8 | /** |
---|
| 9 | * \param spi : Initialized pointer to the SPI controller |
---|
| 10 | * |
---|
| 11 | * \brief Wait until the SPI controller has finished a transfer |
---|
| 12 | * |
---|
| 13 | * Wait until the GO_BUSY bit of the SPI controller be deasserted |
---|
| 14 | */ |
---|
| 15 | static void _spi_wait_if_busy(struct spi_dev * spi) |
---|
| 16 | { |
---|
| 17 | volatile register int delay; |
---|
| 18 | |
---|
| 19 | while(SPI_IS_BUSY(spi)) |
---|
| 20 | { |
---|
| 21 | for (delay = 0; delay < 10000; delay++); |
---|
| 22 | } |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * \param spi : Initialized pointer to the SPI controller |
---|
| 27 | * |
---|
| 28 | * \return void |
---|
| 29 | * |
---|
| 30 | * \brief Init transfer of the tx registers to the selected slaves |
---|
| 31 | */ |
---|
| 32 | static void _spi_init_transfer(struct spi_dev * spi) |
---|
| 33 | { |
---|
| 34 | unsigned int spi_ctrl = ioread32(&spi->ctrl); |
---|
| 35 | |
---|
| 36 | iowrite32(&spi->ctrl, spi_ctrl | SPI_CTRL_GO_BSY); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * \param spi_freq : Desired frequency for the generated clock from the SPI |
---|
| 41 | * controller |
---|
| 42 | * \param sys_freq : System clock frequency |
---|
| 43 | * |
---|
| 44 | * \brief Calculated the value for the divider register in order to obtain the SPI |
---|
| 45 | * desired clock frequency |
---|
| 46 | */ |
---|
| 47 | static unsigned int _spi_calc_divider_value ( |
---|
| 48 | unsigned int spi_freq , |
---|
| 49 | unsigned int sys_freq ) |
---|
| 50 | { |
---|
| 51 | return ((sys_freq / (spi_freq * 2)) - 1); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void spi_put_tx(struct spi_dev * spi, unsigned char byte, int index) |
---|
| 55 | { |
---|
| 56 | _spi_wait_if_busy(spi); |
---|
| 57 | { |
---|
| 58 | iowrite8(&spi->rx_tx[index % 4], byte); |
---|
| 59 | _spi_init_transfer(spi); |
---|
| 60 | |
---|
| 61 | asm volatile("sync"); |
---|
| 62 | } |
---|
| 63 | _spi_wait_if_busy(spi); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | volatile unsigned char spi_get_rx(struct spi_dev * spi, int index) |
---|
| 67 | { |
---|
| 68 | return ioread8(&spi->rx_tx[index % 4]); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void spi_ss_assert(struct spi_dev * spi, int index) |
---|
| 72 | { |
---|
| 73 | unsigned int spi_ss = ioread32(&spi->ss); |
---|
| 74 | |
---|
| 75 | iowrite32(&spi->ss, spi_ss | (1 << index)); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | void spi_ss_deassert(struct spi_dev * spi, int index) |
---|
| 79 | { |
---|
| 80 | unsigned int spi_ss = ioread32(&spi->ss); |
---|
| 81 | |
---|
| 82 | iowrite32(&spi->ss, spi_ss & ~(1 << index)); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | void spi_dev_config ( |
---|
| 86 | struct spi_dev * spi, |
---|
| 87 | int spi_freq , |
---|
| 88 | int sys_freq , |
---|
| 89 | int char_len , |
---|
| 90 | int tx_edge , |
---|
| 91 | int rx_edge ) |
---|
| 92 | { |
---|
| 93 | unsigned int spi_ctrl = ioread32(&spi->ctrl); |
---|
| 94 | |
---|
| 95 | if ( tx_edge == 0 ) spi_ctrl |= SPI_CTRL_TXN_EN; |
---|
| 96 | else if ( tx_edge == 1 ) spi_ctrl &= ~SPI_CTRL_TXN_EN; |
---|
| 97 | if ( rx_edge == 0 ) spi_ctrl |= SPI_CTRL_RXN_EN; |
---|
| 98 | else if ( rx_edge == 1 ) spi_ctrl &= ~SPI_CTRL_RXN_EN; |
---|
| 99 | if ( char_len > 0 ) spi_ctrl = (spi_ctrl & ~SPI_CTRL_CHAR_LEN_MASK) | |
---|
| 100 | (char_len & SPI_CTRL_CHAR_LEN_MASK); |
---|
| 101 | |
---|
| 102 | iowrite32(&spi->ctrl, spi_ctrl); |
---|
| 103 | |
---|
| 104 | if (spi_freq > 0 && sys_freq > 0) |
---|
| 105 | iowrite32(&spi->divider, _spi_calc_divider_value(spi_freq, sys_freq)); |
---|
| 106 | |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /* |
---|
| 110 | * vim: tabstop=4 : shiftwidth=4 : expandtab : softtabstop=4 |
---|
| 111 | */ |
---|