Changeset 556 for trunk/modules/sdmmc


Ignore:
Timestamp:
Oct 22, 2013, 11:22:44 PM (10 years ago)
Author:
bouyer
Message:

Convert all sc_signal to plain integer types. Tracking edges of the spi_clock
from the main clock can lead to a 180deg phase shift otherwise when
spi_clock is just the main clock divided by 2.

Location:
trunk/modules/sdmmc/caba/source
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/modules/sdmmc/caba/source/include/sdmmc.h

    r555 r556  
    5555
    5656    // 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;
     57    int               spi_fsm;           // SPI state register
     58    int               spi_shiftreg;     // data shift in/out
     59    int               spi_bitcount;
     60    int               spi_clk;
    6161
    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
     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
    6868
    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
     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
    7575
    7676    // sd states
  • trunk/modules/sdmmc/caba/source/src/sdmmc.cpp

    r555 r556  
    4444    if(p_resetn.read() == false)
    4545    {
    46         r_spi_fsm  = S_IDLE;
     46        spi_fsm  = S_IDLE;
    4747        m_acmd     = false;     
    4848        m_sdstate  = SD_IDLE;
     
    5050    }
    5151    if (p_spi_ss.read()) {
    52         if (r_spi_fsm != S_IDLE) {
     52        if (spi_fsm != S_IDLE) {
    5353                std::cerr << name() << " deselect but not idle, state "
    54                 << std::dec << r_spi_fsm << " last cmd " << (int)r_command
    55                 << " args " << std::hex << r_args << std::dec
    56                 << " bitcount " << (int)r_spi_bitcount.read()
     54                << std::dec << spi_fsm << " last cmd " << (int)command
     55                << " args " << std::hex << args << std::dec
     56                << " bitcount " << (int)spi_bitcount
    5757                << " idx " << m_data_idx << " len_snd " << m_datalen_snd
    5858                << " len_rcv " << m_datalen_rcv << std::endl;
    5959        }
    60         r_spi_fsm  = S_IDLE;
     60        spi_fsm  = S_IDLE;
     61        spi_clk = p_spi_clk;
    6162        return;
    6263    }
    6364
    64     r_spi_clk = p_spi_clk;
    65 
    66     switch(r_spi_fsm) {
     65    switch(spi_fsm) {
    6766    case S_IDLE:
    68         if (p_spi_clk.read() == 1 && r_spi_clk.read() == 0) {
     67        if (p_spi_clk.read() == 1 && spi_clk == 0) {
    6968                // rising edge
    70                 r_command = (r_command << 1) | p_spi_mosi;
    71                 r_spi_bitcount = 6;
    72                 r_spi_fsm = S_RECEIVE_CMD;
     69                command = (command << 1) | p_spi_mosi;
     70                spi_bitcount = 6;
     71                spi_fsm = S_RECEIVE_CMD;
    7372        }
    7473        break;
    7574    case S_RECEIVE_CMD:
    76         if (p_spi_clk.read() == 1 && r_spi_clk.read() == 0) {
     75        if (p_spi_clk.read() == 1 && spi_clk == 0) {
    7776                // rising edge
    78                 r_command = (r_command << 1) | p_spi_mosi;
    79                 r_spi_bitcount = r_spi_bitcount - 1;
    80                 if (r_spi_bitcount == 0) {
    81                         if (((r_command << 1) & 0x80) == 0) {
    82                                 r_spi_fsm = S_RECEIVE_ARGS_START;
     77                command = (command << 1) | p_spi_mosi;
     78                if (spi_bitcount == 0) {
     79                        if ((command & 0x80) == 0) {
     80                                spi_fsm = S_RECEIVE_ARGS_START;
    8381                        } else {
    8482#ifdef SOCLIB_MODULE_DEBUG0
    85                                 std::cout << name() << " S_RECEIVE_CMD " << std::hex << ((int)((r_command << 1) | p_spi_mosi) & 0xff) << std::endl;
    86 #endif
    87                                 r_spi_fsm = S_IDLE;
     83                                std::cout << name() << " S_RECEIVE_CMD " << std::hex << (int)command << std::endl;
     84#endif
     85                                spi_fsm = S_IDLE;
    8886                        }
     87                } else {
     88                    spi_bitcount = spi_bitcount - 1;
    8989                }
    9090        }
    9191        break;
    9292    case S_RECEIVE_ARGS_START:
    93         if (p_spi_clk.read() == 1 && r_spi_clk.read() == 0) {
     93        if (p_spi_clk.read() == 1 && spi_clk == 0) {
    9494                // rising edge
    95                 r_args = (r_args << 1) | p_spi_mosi;
    96                 r_spi_bitcount = 30;
    97                 r_spi_fsm = S_RECEIVE_ARGS;
     95                args = (args << 1) | p_spi_mosi;
     96                spi_bitcount = 30;
     97                spi_fsm = S_RECEIVE_ARGS;
    9898        }
    9999        break;
    100100    case S_RECEIVE_ARGS:
    101         if (p_spi_clk.read() == 1 && r_spi_clk.read() == 0) {
     101        if (p_spi_clk.read() == 1 && spi_clk == 0) {
    102102                // rising edge
    103                 r_args = (r_args << 1) | p_spi_mosi;
    104                 r_spi_bitcount = r_spi_bitcount - 1;
    105                 if (r_spi_bitcount == 0) {
    106                         r_spi_bitcount = 7;
    107                         r_spi_fsm = S_RECEIVE_CRC;
     103                args = (args << 1) | p_spi_mosi;
     104                if (spi_bitcount == 0) {
     105                        spi_bitcount = 7;
     106                        spi_fsm = S_RECEIVE_CRC;
     107                } else {
     108                    spi_bitcount = spi_bitcount - 1;
    108109                }
    109110        }
    110111        break;
    111112    case S_RECEIVE_CRC:
    112         if (p_spi_clk.read() == 1 && r_spi_clk.read() == 0) {
     113        if (p_spi_clk.read() == 1 && spi_clk == 0) {
    113114                // rising edge
    114                 uint8_t crc = (r_cmdcrc << 1) | p_spi_mosi;
    115                 r_cmdcrc = crc;
    116                 if (r_spi_bitcount == 0) {
    117                         handle_sdmmc_cmd(r_command.read(), r_args.read());
    118                         r_spi_bitcount = 0; // SEND_DATA will reset it
    119                         r_spi_fsm = S_SEND_DATA;
     115                cmdcrc = (cmdcrc << 1) | p_spi_mosi;
     116                if (spi_bitcount == 0) {
     117                        handle_sdmmc_cmd(command, args);
     118                        spi_bitcount = 0; // SEND_DATA will reset it
     119                        spi_fsm = S_SEND_DATA;
    120120                        m_data_idx = 0;
    121121                } else {
    122                         r_spi_bitcount = r_spi_bitcount - 1;
     122                        spi_bitcount = spi_bitcount - 1;
    123123                }
    124124        }
     
    126126       
    127127    case S_SEND_DATA:
    128         if (p_spi_clk.read() == 0 && r_spi_clk.read() == 1) {
     128        if (p_spi_clk.read() == 0 && spi_clk == 1) {
    129129                // falling edge
    130                 if (r_spi_bitcount == 0) {
     130                if (spi_bitcount == 0) {
    131131                        if (m_data_idx != m_datalen_snd) {     
    132                                 r_spi_shiftreg = m_databuf[m_data_idx];
    133                                 r_spi_bitcount = 7;
    134                                 r_spi_fsm = S_SEND_DATA;
     132                                spi_shiftreg = m_databuf[m_data_idx];
     133                                spi_bitcount = 7;
     134                                spi_fsm = S_SEND_DATA;
    135135                                m_data_idx++;
    136136#ifdef SOCLIB_MODULE_DEBUG0
     
    138138#endif
    139139                        } else if (m_datalen_rcv != 0) {
    140                                 r_spi_fsm = S_RECEIVE_DATA_WAIT;
    141                                 r_spi_bitcount = 7;
     140                                spi_fsm = S_RECEIVE_DATA_WAIT;
     141                                spi_bitcount = 7;
    142142                                m_data_idx = 0;
    143143                        } else {
    144                                 r_spi_fsm = S_IDLE;
     144                                spi_fsm = S_IDLE;
    145145                        }
    146146                } else {
    147                         r_spi_bitcount = r_spi_bitcount - 1;
    148                         r_spi_shiftreg = r_spi_shiftreg << 1;
     147                        spi_bitcount = spi_bitcount - 1;
     148                        spi_shiftreg = spi_shiftreg << 1;
    149149                }
    150150        }
    151151        break;
    152152    case S_RECEIVE_DATA_WAIT:
    153         if (p_spi_clk.read() == 1 && r_spi_clk.read() == 0) {
     153        if (p_spi_clk.read() == 1 && spi_clk == 0) {
     154            // rising edge
    154155            uint8_t s_data;
    155             // rising edge
    156156            s_data = (m_databuf[0] << 1) | p_spi_mosi;
    157157            m_databuf[0] = s_data;
    158             r_spi_bitcount = r_spi_bitcount - 1;
    159             if (r_spi_bitcount == 0) {
     158            if (spi_bitcount == 0) {
    160159#ifdef SOCLIB_MODULE_DEBUG
    161160        std::cout << name() << " S_RECEIVE_DATA_WAIT " << std::dec << (int)s_data << std::endl;
    162161#endif
    163                     r_spi_bitcount = 7;
     162                    spi_bitcount = 7;
    164163                    if (s_data == 0xfe) { // data start token
    165                         r_spi_fsm = S_RECEIVE_DATA;
     164                        spi_fsm = S_RECEIVE_DATA;
    166165                        m_data_idx = 1;
    167166                    } else {
     
    169168                        std::cout << name() << " S_RECEIVE_DATA_WAIT " << std::hex << (int)s_data << std::endl;
    170169#endif
    171                         r_spi_fsm = S_RECEIVE_DATA_WAIT;
    172                 }
     170                        spi_fsm = S_RECEIVE_DATA_WAIT;
     171                }
     172            } else {
     173                spi_bitcount = spi_bitcount - 1;
    173174            }
    174175        }
    175176        break;
    176177        case S_RECEIVE_DATA:
    177             if (p_spi_clk.read() == 1 && r_spi_clk.read() == 0) {
     178            if (p_spi_clk.read() == 1 && spi_clk == 0) {
    178179                // rising edge
    179180                m_databuf[m_data_idx] = (m_databuf[m_data_idx] << 1) | p_spi_mosi;
    180                 if (r_spi_bitcount == 0) {
     181                if (spi_bitcount == 0) {
    181182                    m_data_idx++;
    182183                    if (m_data_idx != m_datalen_rcv) {
    183                         r_spi_fsm = S_RECEIVE_DATA;
    184                         r_spi_bitcount = 7;
     184                        spi_fsm = S_RECEIVE_DATA;
     185                        spi_bitcount = 7;
    185186                    } else {
    186                         handle_sdmmc_write(r_command.read(), r_args.read());
     187                        handle_sdmmc_write(command, args);
    187188                        if (m_datalen_snd > 0) {
    188                             r_spi_bitcount = 0; // SEND_DATA will reset it
    189                             r_spi_fsm = S_SEND_DATA;
     189                            spi_bitcount = 0; // SEND_DATA will reset it
     190                            spi_fsm = S_SEND_DATA;
    190191                            m_data_idx = 0;
    191192                        } else {
    192                             r_spi_fsm = S_IDLE;
     193                            spi_fsm = S_IDLE;
    193194                        }
    194195                    }
    195196                } else {
    196                         r_spi_bitcount = r_spi_bitcount - 1;
     197                        spi_bitcount = spi_bitcount - 1;
    197198                }
    198199            }
    199200            break;
    200201    }
     202    spi_clk = p_spi_clk.read();
    201203}  // end transition
    202204
     
    204206void SdMMC::genMoore()
    205207{
    206     switch(r_spi_fsm) {
     208    switch(spi_fsm) {
    207209    case S_IDLE:
    208210        p_spi_miso = !p_spi_ss.read();
    209211        break;
    210212    case S_SEND_DATA:
    211         p_spi_miso = (r_spi_shiftreg & 0x80) != 0;
     213        p_spi_miso = (spi_shiftreg & 0x80) != 0;
    212214        break;
    213215    default:
     
    235237        if (m_acmd) {
    236238#ifdef SOCLIB_MODULE_DEBUG0
    237         std::cout << name() << " new acmd " << std::dec << (int)cmd << " args " << std::hex << data << " crc " << (int)r_cmdcrc << std::endl;
     239        std::cout << name() << " new acmd " << std::dec << (int)cmd << " args " << std::hex << data << " crc " << (int)cmdcrc << std::endl;
    238240#endif
    239241            m_acmd = false;
     
    267269        } else {
    268270#ifdef SOCLIB_MODULE_DEBUG0
    269         std::cout << name() << " new cmd " << std::dec << (int)cmd << " args " << std::hex << data << " crc " << (int)r_cmdcrc << std::endl;
     271        std::cout << name() << " new cmd " << std::dec << (int)cmd << " args " << std::hex << data << " crc " << (int)cmdcrc << std::endl;
    270272#endif
    271273            switch (cmd) {
     
    524526                "S_NOP",
    525527        };
    526         if (r_spi_clk != p_spi_clk) {
    527         std::cout << name() << " SPI_FSM : " << spi_str[r_spi_fsm]
     528        if (spi_clk != p_spi_clk.read()) {
     529        std::cout << name() << " SPI_FSM : " << spi_str[spi_fsm]
    528530            << std::dec
    529             << " clk " << r_spi_clk << "->" << p_spi_clk << " ss " << p_spi_ss
     531            << " clk " << spi_clk << "->" << p_spi_clk << " ss " << p_spi_ss
    530532            << " mosi " << p_spi_mosi << " miso " << p_spi_miso
    531533            << std::endl;
    532         std::cout << "         r_spi_shiftreg: " << std::hex << (int)r_spi_shiftreg
    533             << " r_spi_bitcount: " << (int)r_spi_bitcount
     534        std::cout << "         spi_shiftreg: " << std::hex << (int)spi_shiftreg
     535            << " spi_bitcount: " << (int)spi_bitcount
    534536            << std::endl;
    535537        }
Note: See TracChangeset for help on using the changeset viewer.