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

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

Implement single block write

File size: 3.7 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    sc_signal<int>                    r_spi_fsm;         // SPI state register
58    sc_signal<uint8_t>                r_spi_shiftreg;   // data shift in/out
59    sc_signal<uint8_t>                r_spi_bitcount;
60    sc_signal<bool>                   r_spi_clk;
61
62    sc_signal<uint8_t>                r_command;
63    sc_signal<uint32_t>               r_args;
64    sc_signal<uint8_t>                r_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 transition();
84    void genMoore();
85
86    void handle_sdmmc_cmd(uint8_t, uint32_t);
87    void handle_sdmmc_write(uint8_t, uint32_t);
88
89    //  Master FSM states
90    enum {
91    S_IDLE               = 0,
92    S_RECEIVE_CMD        = 1,
93    S_RECEIVE_ARGS_START = 2,
94    S_RECEIVE_ARGS       = 3,
95    S_RECEIVE_CRC        = 4,
96    S_RECEIVE_DATA_WAIT  = 5,
97    S_RECEIVE_DATA       = 6,
98    S_SEND_DATA          = 7,
99    };
100
101protected:
102
103    SC_HAS_PROCESS(SdMMC);
104
105public:
106
107    // ports
108    sc_in<bool>                                               p_clk;
109    sc_in<bool>                                               p_resetn;
110    sc_in<bool>                                               p_spi_ss;
111    sc_in<bool>                                               p_spi_clk;
112    sc_in<bool>                                               p_spi_mosi;
113    sc_out<bool>                                              p_spi_miso;
114
115    void print_trace();
116
117    // Constructor   
118    SdMMC(
119        sc_module_name                      name,
120        const std::string                   &filename,
121        const uint32_t                      latency = 0);
122
123    ~SdMMC();
124
125};
126
127}}
128
129#endif /* SOCLIB_SDMMC_H */
130
131// Local Variables:
132// tab-width: 4
133// c-basic-offset: 4
134// c-file-offsets:((innamespace . 0)(inline-open . 0))
135// indent-tabs-mode: nil
136// End:
137
138// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
Note: See TracBrowser for help on using the repository browser.