Changeset 556 for trunk/modules
- Timestamp:
- Oct 22, 2013, 11:22:44 PM (11 years ago)
- Location:
- trunk/modules/sdmmc/caba/source
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/sdmmc/caba/source/include/sdmmc.h
r555 r556 55 55 56 56 // Registers 57 sc_signal<int> r_spi_fsm; // SPI state register58 sc_signal<uint8_t> r_spi_shiftreg; // data shift in/out59 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; 61 61 62 sc_signal<uint8_t> r_command;63 sc_signal<uint32_t> r_args;64 sc_signal<uint8_t> r_cmdcrc;65 int 66 uint64_t 67 const uint32_t 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 68 69 uint8_t 70 uint32_t 71 uint32_t 72 uint32_t 73 bool 74 int 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 75 76 76 // sd states -
trunk/modules/sdmmc/caba/source/src/sdmmc.cpp
r555 r556 44 44 if(p_resetn.read() == false) 45 45 { 46 r_spi_fsm = S_IDLE;46 spi_fsm = S_IDLE; 47 47 m_acmd = false; 48 48 m_sdstate = SD_IDLE; … … 50 50 } 51 51 if (p_spi_ss.read()) { 52 if ( r_spi_fsm != S_IDLE) {52 if (spi_fsm != S_IDLE) { 53 53 std::cerr << name() << " deselect but not idle, state " 54 << std::dec << r_spi_fsm << " last cmd " << (int)r_command55 << " args " << std::hex << r_args << std::dec56 << " 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 57 57 << " idx " << m_data_idx << " len_snd " << m_datalen_snd 58 58 << " len_rcv " << m_datalen_rcv << std::endl; 59 59 } 60 r_spi_fsm = S_IDLE; 60 spi_fsm = S_IDLE; 61 spi_clk = p_spi_clk; 61 62 return; 62 63 } 63 64 64 r_spi_clk = p_spi_clk; 65 66 switch(r_spi_fsm) { 65 switch(spi_fsm) { 67 66 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) { 69 68 // 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; 73 72 } 74 73 break; 75 74 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) { 77 76 // 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; 83 81 } else { 84 82 #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; 88 86 } 87 } else { 88 spi_bitcount = spi_bitcount - 1; 89 89 } 90 90 } 91 91 break; 92 92 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) { 94 94 // 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; 98 98 } 99 99 break; 100 100 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) { 102 102 // 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; 108 109 } 109 110 } 110 111 break; 111 112 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) { 113 114 // 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; 120 120 m_data_idx = 0; 121 121 } else { 122 r_spi_bitcount = r_spi_bitcount - 1;122 spi_bitcount = spi_bitcount - 1; 123 123 } 124 124 } … … 126 126 127 127 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) { 129 129 // falling edge 130 if ( r_spi_bitcount == 0) {130 if (spi_bitcount == 0) { 131 131 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; 135 135 m_data_idx++; 136 136 #ifdef SOCLIB_MODULE_DEBUG0 … … 138 138 #endif 139 139 } 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; 142 142 m_data_idx = 0; 143 143 } else { 144 r_spi_fsm = S_IDLE;144 spi_fsm = S_IDLE; 145 145 } 146 146 } 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; 149 149 } 150 150 } 151 151 break; 152 152 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 154 155 uint8_t s_data; 155 // rising edge156 156 s_data = (m_databuf[0] << 1) | p_spi_mosi; 157 157 m_databuf[0] = s_data; 158 r_spi_bitcount = r_spi_bitcount - 1; 159 if (r_spi_bitcount == 0) { 158 if (spi_bitcount == 0) { 160 159 #ifdef SOCLIB_MODULE_DEBUG 161 160 std::cout << name() << " S_RECEIVE_DATA_WAIT " << std::dec << (int)s_data << std::endl; 162 161 #endif 163 r_spi_bitcount = 7;162 spi_bitcount = 7; 164 163 if (s_data == 0xfe) { // data start token 165 r_spi_fsm = S_RECEIVE_DATA;164 spi_fsm = S_RECEIVE_DATA; 166 165 m_data_idx = 1; 167 166 } else { … … 169 168 std::cout << name() << " S_RECEIVE_DATA_WAIT " << std::hex << (int)s_data << std::endl; 170 169 #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; 173 174 } 174 175 } 175 176 break; 176 177 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) { 178 179 // rising edge 179 180 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) { 181 182 m_data_idx++; 182 183 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; 185 186 } else { 186 handle_sdmmc_write( r_command.read(), r_args.read());187 handle_sdmmc_write(command, args); 187 188 if (m_datalen_snd > 0) { 188 r_spi_bitcount = 0; // SEND_DATA will reset it189 r_spi_fsm = S_SEND_DATA;189 spi_bitcount = 0; // SEND_DATA will reset it 190 spi_fsm = S_SEND_DATA; 190 191 m_data_idx = 0; 191 192 } else { 192 r_spi_fsm = S_IDLE;193 spi_fsm = S_IDLE; 193 194 } 194 195 } 195 196 } else { 196 r_spi_bitcount = r_spi_bitcount - 1;197 spi_bitcount = spi_bitcount - 1; 197 198 } 198 199 } 199 200 break; 200 201 } 202 spi_clk = p_spi_clk.read(); 201 203 } // end transition 202 204 … … 204 206 void SdMMC::genMoore() 205 207 { 206 switch( r_spi_fsm) {208 switch(spi_fsm) { 207 209 case S_IDLE: 208 210 p_spi_miso = !p_spi_ss.read(); 209 211 break; 210 212 case S_SEND_DATA: 211 p_spi_miso = ( r_spi_shiftreg & 0x80) != 0;213 p_spi_miso = (spi_shiftreg & 0x80) != 0; 212 214 break; 213 215 default: … … 235 237 if (m_acmd) { 236 238 #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; 238 240 #endif 239 241 m_acmd = false; … … 267 269 } else { 268 270 #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; 270 272 #endif 271 273 switch (cmd) { … … 524 526 "S_NOP", 525 527 }; 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] 528 530 << std::dec 529 << " clk " << r_spi_clk << "->" << p_spi_clk << " ss " << p_spi_ss531 << " clk " << spi_clk << "->" << p_spi_clk << " ss " << p_spi_ss 530 532 << " mosi " << p_spi_mosi << " miso " << p_spi_miso 531 533 << std::endl; 532 std::cout << " r_spi_shiftreg: " << std::hex << (int)r_spi_shiftreg533 << " r_spi_bitcount: " << (int)r_spi_bitcount534 std::cout << " spi_shiftreg: " << std::hex << (int)spi_shiftreg 535 << " spi_bitcount: " << (int)spi_bitcount 534 536 << std::endl; 535 537 }
Note: See TracChangeset
for help on using the changeset viewer.