source: trunk/modules/sdmmc/caba/source/include/sdmmc.h @ 557

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

Switch to using a mealy function. This seems to be the only way to get output
signals in sync with the spi_clk in all cases.
Now this model works in cosimulation with the VHDL spi controller.

File size: 3.5 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 SD/MMC block device.
32//
33// This component can perform data transfers between one single file belonging
34// to the host system and a SPI controller.
35// The file name is an argument of the constructor.
36//
37
38#ifndef SOCLIB_SDMMC_H
39#define SOCLIB_SDMMC_H
40
41#include <stdint.h>
42#include <systemc>
43#include <unistd.h>
44#include "caba_base_module.h"
45
46namespace soclib {
47namespace caba {
48
49using namespace sc_core;
50
51class SdMMC
52        : public caba::BaseModule
53{
54private:
55
56    // Registers
57    int               spi_fsm;           // SPI state register
58    int               spi_shiftreg;     // data shift in/out
59    int               spi_bitcount;
60    int               spi_clk;
61
62    uint8_t           command;
63    uint32_t          args;
64    uint8_t           cmdcrc;
65    int               m_fd;              // File descriptor
66    uint64_t          m_device_size;     // Total number of blocks
67    const uint32_t    m_latency;         // device latency
68
69    uint8_t           m_databuf[1 /* reponse */ + 1 /* data tocken */ + 512 /* data block */ + 2 /* CRC */ ];
70    uint32_t          m_datalen_snd; // data size to be sent to host
71    uint32_t          m_datalen_rcv; // data size expected from host
72    uint32_t          m_data_idx;
73    bool              m_acmd; // next command will be acmd
74    int               m_sdstate; // sdcard internal state
75
76    // sd states
77    enum {
78        SD_IDLE = 0,
79        SD_READY = 1,
80    };
81
82    // methods
83    void genMealy();
84
85    void handle_sdmmc_cmd(uint8_t, uint32_t);
86    void handle_sdmmc_write(uint8_t, uint32_t);
87
88    //  Master FSM states
89    enum {
90    S_IDLE               = 0,
91    S_RECEIVE_CMD        = 1,
92    S_RECEIVE_ARGS_START = 2,
93    S_RECEIVE_ARGS       = 3,
94    S_RECEIVE_CRC        = 4,
95    S_RECEIVE_DATA_WAIT  = 5,
96    S_RECEIVE_DATA       = 6,
97    S_SEND_DATA          = 7,
98    };
99
100protected:
101
102    SC_HAS_PROCESS(SdMMC);
103
104public:
105
106    // ports
107    sc_in<bool>                                               p_clk;
108    sc_in<bool>                                               p_resetn;
109    sc_in<bool>                                               p_spi_ss;
110    sc_in<bool>                                               p_spi_clk;
111    sc_in<bool>                                               p_spi_mosi;
112    sc_out<bool>                                              p_spi_miso;
113
114    void print_trace();
115
116    // Constructor   
117    SdMMC(
118        sc_module_name                      name,
119        const std::string                   &filename,
120        const uint32_t                      latency = 0);
121
122    ~SdMMC();
123
124};
125
126}}
127
128#endif /* SOCLIB_SDMMC_H */
129
130// Local Variables:
131// tab-width: 4
132// c-basic-offset: 4
133// c-file-offsets:((innamespace . 0)(inline-open . 0))
134// indent-tabs-mode: nil
135// End:
136
137// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
Note: See TracBrowser for help on using the repository browser.