source: branches/v4/softs/tsar_boot/src/spi.c @ 919

Last change on this file since 919 was 292, checked in by cfuguet, 12 years ago

Changing directory structure of the TSAR boot loader.
A README.txt file has been included to explain the new structure
and the MAKEFILE parameters.

Erasing the heap segment for the boot elf loader. All the work space
is allocated in the stack.

The stack size is defined in the include/defs.h.

Important modification in the reset.S file. The non-boot
processors (processor id != 0) wait in a low comsumption energy
mode to be wake up by processor 0 using an IPI. Each processor
has a private mailbox in the local XICU. The value written in
the mailbox will be used as address to jump by the processors.

The waking up of non-boot processors is not done in this boot loader
so it must be done in the application loaded.

The boot_loader_elf function loads into memory an executable .elf file
which must be placed in the BOOT_LOADER_LBA block of the disk. This
constant can be defined in the include/defs.h file.

File size: 2.7 KB
Line 
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 */
15static 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 */
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        asm volatile("sync");
62    }
63    _spi_wait_if_busy(spi);
64}
65
66volatile unsigned char spi_get_rx(struct spi_dev * spi, int index)
67{
68    return ioread8(&spi->rx_tx[index % 4]);
69}
70
71void 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
78void 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
85void 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 */
Note: See TracBrowser for help on using the repository browser.