source: trunk/softs/tsar_boot/src/spi.c @ 395

Last change on this file since 395 was 388, checked in by cfuguet, 11 years ago

Modifications in tsar/trunk/softs/tsar_boot:

  • Improving the boot_ioc_read when using a SD card in FPGA platform.
  • Adding some instrumentation on the SD card driver (under preprocessor conditional directives).
  • Including Doxyfile for generate documentation using doxygen.
  • Improving the Makefile to include doc generation.
File size: 2.7 KB
Line 
1/**
2 * \file    spi.c
3 * \date    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 */
15static void _spi_wait_if_busy(struct spi_dev * spi)
16{
17    register int delay;
18
19    while(SPI_IS_BUSY(spi))
20    {
21        for (delay = 0; delay < 100; 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 */
32static 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 */
47static 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
54void 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    _spi_wait_if_busy(spi);
62}
63
64volatile unsigned char spi_get_rx(struct spi_dev * spi, int index)
65{
66    return ioread8(&spi->rx_tx[index % 4]);
67}
68
69void spi_ss_assert(struct spi_dev * spi, int index)
70{
71    unsigned int spi_ss = ioread32(&spi->ss);
72
73    iowrite32(&spi->ss, spi_ss | (1 << index));
74}
75
76void spi_ss_deassert(struct spi_dev * spi, int index)
77{
78    unsigned int spi_ss = ioread32(&spi->ss);
79
80    iowrite32(&spi->ss, spi_ss & ~(1 << index));
81}
82
83void spi_dev_config (
84    struct spi_dev * spi,
85    int spi_freq        ,
86    int sys_freq        ,
87    int char_len                ,
88    int tx_edge                 ,
89    int rx_edge                 )
90{
91    unsigned int spi_ctrl = ioread32(&spi->ctrl);
92
93    if      ( tx_edge == 0 ) spi_ctrl |=  SPI_CTRL_TXN_EN;
94    else if ( tx_edge == 1 ) spi_ctrl &= ~SPI_CTRL_TXN_EN;
95    if      ( rx_edge == 0 ) spi_ctrl |=  SPI_CTRL_RXN_EN;
96    else if ( rx_edge == 1 ) spi_ctrl &= ~SPI_CTRL_RXN_EN;
97    if      ( char_len > 0 ) spi_ctrl  = (spi_ctrl & ~SPI_CTRL_CHAR_LEN_MASK) |
98                                         (char_len &  SPI_CTRL_CHAR_LEN_MASK);
99
100    iowrite32(&spi->ctrl, spi_ctrl);
101
102    if (spi_freq > 0 && sys_freq > 0)
103        iowrite32(&spi->divider, _spi_calc_divider_value(spi_freq, sys_freq));
104}
105
106/*
107 * vim: tabstop=4 : shiftwidth=4 : expandtab : softtabstop=4
108 */
Note: See TracBrowser for help on using the repository browser.