source: trunk/modules/vci_spi/caba/source/include/vci_spi.h @ 551

Last change on this file since 551 was 551, checked in by bouyer, 11 years ago

Add vci_spi, a SPI (Serial Peripheral Interface) controller. The initiator
port is unused for now, but a DMA feature will be added soon.

File size: 7.6 KB
Line 
1
2/* -*- c++ -*-
3 *
4 * SOCLIB_LGPL_HEADER_BEGIN
5 *
6 * This file is part of SoCLib, GNU LGPLv2.1.
7 *
8 * SoCLib is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; version 2.1 of the License.
11 *
12 * SoCLib is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with SoCLib; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 * SOCLIB_LGPL_HEADER_END
23 *
24 * Copyright (c) UPMC, Lip6, SoC
25 *         manuel.bouyer@lip6.fr october 2013
26 *
27 * Maintainers: bouyer
28 */
29
30//////////////////////////////////////////////////////////////////////////////////////
31// This component is a SPI controller with a VCI interface
32// It supports only 32 or 64 bits VCI DATA width, but all addressable registers
33// contain 32 bits words. It supports VCI addresss lartger than 32 bits.
34//
35// This component can perform data transfers between one single file belonging
36// to the host system and a buffer in the memory of the virtual prototype.
37// The file name is an argument of the constructor.
38// This component has a DMA capability, and is both a target and an initiator.
39// The burst size (bytes) must be power of 2.
40// The burst size is typically a cache line.
41// The memory buffer must be aligned to a a burst boundary.
42// Both read and write transfers are supported. An IRQ is optionally
43// asserted when the transfer is completed.
44//
45
46#ifndef SOCLIB_VCI_SPI_H
47#define SOCLIB_VCI_SPI_H
48
49#include <stdint.h>
50#include <systemc>
51#include <unistd.h>
52#include "caba_base_module.h"
53#include "mapping_table.h"
54#include "vci_initiator.h"
55#include "vci_target.h"
56
57namespace soclib {
58namespace caba {
59
60using namespace sc_core;
61
62template<typename vci_param>
63class VciSpi
64        : public caba::BaseModule
65{
66private:
67
68    // Registers
69    sc_signal<int>                     r_target_fsm;       // target fsm state register
70    sc_signal<int>                     r_initiator_fsm;    // initiator fsm state register
71    sc_signal<int>                     r_spi_fsm;          // spi engine state
72    sc_signal<uint64_t>                r_txrx[2];          // data in/out
73    sc_signal<uint32_t>                r_divider;          // SPI clk divider
74    sc_signal<uint8_t>                 r_ss;               // SPI slave select
75    sc_signal<bool>                    r_ctrl_cpol;     // clock polarity
76    sc_signal<bool>                    r_ctrl_cpha;     // clock phase
77    sc_signal<bool>                    r_ctrl_ass;      // auto slave select
78    sc_signal<bool>                    r_ctrl_ie;       // interrupt enable
79    sc_signal<bool>                    r_ctrl_go_bsy;
80    sc_signal<uint8_t>                 r_ctrl_char_len; // number of bits in xfer
81
82    sc_signal<uint8_t>                 r_txrx_addr;
83
84    sc_signal<uint32_t>                r_bit_count;
85    sc_signal<uint32_t>                r_clk_counter;
86    sc_signal<bool>                    r_spi_clk;
87    sc_signal<bool>                    r_spi_clk_previous;
88    sc_signal<bool>                    r_spi_clk_ignore;
89    sc_signal<bool>                    r_spi_out;
90    sc_signal<bool>                    r_irq;
91
92    sc_signal<bool>                    r_read;
93    sc_signal<uint32_t>                r_nblocks;          // number of blocks in transfer
94    sc_signal<uint64_t>                r_buf_address;      // memory buffer address
95    sc_signal<uint32_t>                r_index;            // word index in local buffer
96    sc_signal<uint32_t>                r_latency_count;    // latency counter
97    sc_signal<uint32_t>                r_words_count;      // word counter (in a burst)
98    sc_signal<uint32_t>                r_burst_count;      // burst counter (in a block)
99    sc_signal<uint32_t>                r_block_count;      // block counter (in a transfer)
100    sc_signal<uint32_t>                r_burst_offset;     // number of non aligned words
101    sc_signal<uint32_t>                r_burst_nwords;     // number of words in a burst
102    sc_signal<bool>                    r_go;               // command from T_FSM to M_FSM
103
104    sc_signal<typename vci_param::srcid_t >     r_srcid;   // save srcid
105    sc_signal<typename vci_param::trdid_t >     r_trdid;   // save trdid
106    sc_signal<typename vci_param::pktid_t >     r_pktid;   // save pktid
107    sc_signal<typename vci_param::data_t >      r_tdata;   // save wdata
108
109    uint32_t*                          r_local_buffer;     // capacity is one block
110
111    // structural parameters
112    std::list<soclib::common::Segment> m_seglist;
113    uint32_t                           m_srcid;            // initiator index
114    const uint32_t                     m_words_per_block;  // block size
115    const uint32_t                     m_words_per_burst;  // number of words in a burst
116    const uint32_t                     m_bursts_per_block; // number of bursts in a block
117
118    // methods
119    void transition();
120    void genMoore();
121
122    //  Master FSM states
123    enum {
124    M_IDLE              = 0,
125
126    M_READ_BLOCK        = 1,
127    M_READ_BURST        = 2,
128    M_READ_CMD          = 3,
129    M_READ_RSP          = 4,
130    M_READ_SUCCESS      = 5,
131    M_READ_ERROR        = 6,
132
133    M_WRITE_BURST       = 7,
134    M_WRITE_CMD         = 8,
135    M_WRITE_RSP         = 9,
136    M_WRITE_BLOCK       = 10,
137    M_WRITE_SUCCESS     = 11,
138    M_WRITE_ERROR       = 12,
139    };
140
141    // Target FSM states
142    enum {
143    T_IDLE              = 0,
144    T_WRITE_TXRX        = 1,
145    T_READ_TXRX         = 2,
146    T_WRITE_CTRL        = 3,
147    T_READ_CTRL         = 4,
148    T_WRITE_DIVIDER     = 5,
149    T_READ_DIVIDER      = 6,
150    T_WRITE_SS          = 7,
151    T_READ_SS           = 8,
152    T_WRITE_ERROR       = 9,
153    T_READ_ERROR        = 10,
154    };
155
156    // SPI FSM states
157    enum {
158    S_IDLE              = 0,
159    S_XMIT              = 1,
160    };
161
162    // Error codes values
163    enum {
164    VCI_READ_OK         = 0,
165    VCI_READ_ERROR      = 1,
166    VCI_WRITE_OK        = 2,
167    VCI_WRITE_ERROR     = 3,
168    };
169
170    /* transaction type, pktid field */
171    enum transaction_type_e
172    {
173      // b3 unused
174      // b2 READ / NOT READ
175      // Si READ
176      //  b1 DATA / INS
177      //  b0 UNC / MISS
178      // Si NOT READ
179      //  b1 accÚs table llsc type SW / other
180      //  b2 WRITE/CAS/LL/SC
181      TYPE_READ_DATA_UNC          = 0x0,
182      TYPE_READ_DATA_MISS         = 0x1,
183      TYPE_READ_INS_UNC           = 0x2,
184      TYPE_READ_INS_MISS          = 0x3,
185      TYPE_WRITE                  = 0x4,
186      TYPE_CAS                    = 0x5,
187      TYPE_LL                     = 0x6,
188      TYPE_SC                     = 0x7
189    };
190
191protected:
192
193    SC_HAS_PROCESS(VciSpi);
194
195public:
196
197    // ports
198    sc_in<bool>                                               p_clk;
199    sc_in<bool>                                               p_resetn;
200    sc_out<bool>                                              p_irq;
201    soclib::caba::VciInitiator<vci_param> p_vci_initiator;
202    soclib::caba::VciTarget<vci_param>    p_vci_target;
203    sc_out<bool>                                              p_spi_ss;
204    sc_out<bool>                                              p_spi_clk;
205    sc_out<bool>                                              p_spi_mosi;
206    sc_in<bool>                                               p_spi_miso;
207
208    void print_trace();
209
210    // Constructor   
211    VciSpi(
212        sc_module_name                      name,
213        const soclib::common::MappingTable  &mt,
214        const soclib::common::IntTab        &srcid,
215        const soclib::common::IntTab        &tgtid,
216        const uint32_t                      burst_size = 64);
217
218    ~VciSpi();
219
220};
221
222}}
223
224#endif /* SOCLIB_VCI_SPI_H */
225
226// Local Variables:
227// tab-width: 4
228// c-basic-offset: 4
229// c-file-offsets:((innamespace . 0)(inline-open . 0))
230// indent-tabs-mode: nil
231// End:
232
233// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
234
Note: See TracBrowser for help on using the repository browser.