diff --git a/P8000_WDC_Emulator/P8000_WDC_Emulator/Debug/Makefile b/P8000_WDC_Emulator/P8000_WDC_Emulator/Debug/Makefile index bc0c925..8a8e8ce 100755 --- a/P8000_WDC_Emulator/P8000_WDC_Emulator/Debug/Makefile +++ b/P8000_WDC_Emulator/P8000_WDC_Emulator/Debug/Makefile @@ -117,7 +117,7 @@ OUTPUT_FILE_DEP:= ./makedep.mk ./%.o: .././%.c @echo Building file: $< @echo Invoking: AVR/GNU C Compiler : (AVR_8_bit_GNU_Toolchain_3.4.0_663) 4.6.2 - $(QUOTE)C:\Program Files (x86)\Atmel\Atmel Studio 6.0\extensions\Atmel\AVRGCC\3.4.0.65\AVRToolchain\bin\avr-gcc.exe$(QUOTE) -funsigned-char -funsigned-bitfields -O1 -ffunction-sections -fpack-struct -fshort-enums -Wall -Wundef -Werror -pedantic -pedantic-errors -c -std=gnu99 -MD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -MT"$(@:%.o=%.o)" -mmcu=atmega1284p -o"$@" "$<" + $(QUOTE)C:\Program Files (x86)\Atmel\Atmel Studio 6.0\extensions\Atmel\AVRGCC\3.4.0.65\AVRToolchain\bin\avr-gcc.exe$(QUOTE) -funsigned-char -funsigned-bitfields -O1 -ffunction-sections -fpack-struct -fshort-enums -Wall -Wundef -Werror -pedantic -pedantic-errors -c -std=gnu99 -save-temps -MD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -MT"$(@:%.o=%.o)" -mmcu=atmega1284p -o"$@" "$<" @echo Finished building: $< diff --git a/P8000_WDC_Emulator/P8000_WDC_Emulator/P8000_WDC_Emulator.cproj b/P8000_WDC_Emulator/P8000_WDC_Emulator/P8000_WDC_Emulator.cproj index a619f77..e2be29c 100644 --- a/P8000_WDC_Emulator/P8000_WDC_Emulator/P8000_WDC_Emulator.cproj +++ b/P8000_WDC_Emulator/P8000_WDC_Emulator/P8000_WDC_Emulator.cproj @@ -45,28 +45,29 @@ - True - True - True - True - True - Optimize (-O1) - True - True - True - True - True - True - True - True - - - m - - - True - True - + True + True + True + True + True + Optimize (-O1) + True + True + True + True + True + True + True + True + True + + + m + + + True + True + diff --git a/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_drv_mmc.c b/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_drv_mmc.c index 0d45f7d..300d4f7 100644 --- a/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_drv_mmc.c +++ b/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_drv_mmc.c @@ -1,7 +1,7 @@ /* * P8000 WDC Emulator * - * $Id: wdc_drv_mmc.c,v 1.6 2012/06/05 19:46:26 olivleh1 Exp $ + * $Id: wdc_drv_mmc.c,v 1.7 2012/06/07 00:34:36 olivleh1 Exp $ * */ @@ -36,6 +36,16 @@ uint8_t mmc_read_block ( uint8_t *, uint8_t *, uint16_t ); uint8_t mmc_cmd ( uint8_t * ); +typedef union { + uint32_t value32; + struct { + uint8_t ll; + uint8_t lh; + uint8_t hl; + uint8_t hh; + } value8; +} sint32; + #ifdef SPI_CRC const uint16_t crc16_table[256] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, @@ -226,8 +236,10 @@ uint8_t mmc_cmd ( uint8_t *cmd ) uint8_t mmc_write_sector ( uint32_t addr, uint8_t *Buffer ) { + sint32 x; uint8_t tmp; uint8_t cmd[] = {CMD24, 0x00, 0x00, 0x00, 0x00, 0xFF}; + uint16_t i; #ifdef SPI_CRC uint16_t crc; uint8_t crcl, crch; @@ -236,11 +248,11 @@ uint8_t mmc_write_sector ( uint32_t addr, uint8_t *Buffer ) MMC_Enable(); // convert blocks to bytes - addr = addr << 9; //addr = addr * 512 - - cmd[1] = ( ( addr & 0xFF000000 ) >> 24 ); - cmd[2] = ( ( addr & 0x00FF0000 ) >> 16 ); - cmd[3] = ( ( addr & 0x0000FF00 ) >> 8 ); + addr = addr * MMC_BLOCKLEN; + x.value32 = addr; + cmd[1] = x.value8.hh; + cmd[2] = x.value8.hl; + cmd[3] = x.value8.lh; /* * send CMD24 @@ -255,13 +267,13 @@ uint8_t mmc_write_sector ( uint32_t addr, uint8_t *Buffer ) xmit_byte ( SB_START ); #ifdef SPI_CRC - crc = crc16 ( Buffer, 512 ); + crc = crc16 ( Buffer, MMC_BLOCKLEN ); crch = crc >> 8; crcl = crc & 0xff; #endif //write a single block - for ( uint16_t a = 0; a < 512; a++ ) { + for ( i = MMC_BLOCKLEN ; i ; i-- ) { uint8_t data = *Buffer; Buffer++; wait_till_send_done(); @@ -296,6 +308,7 @@ uint8_t mmc_write_sector ( uint32_t addr, uint8_t *Buffer ) uint8_t mmc_read_block ( uint8_t *cmd, uint8_t *Buffer, uint16_t Bytes ) { + uint16_t i = Bytes; #ifdef SPI_CRC uint16_t crc; #endif @@ -320,12 +333,13 @@ uint8_t mmc_read_block ( uint8_t *cmd, uint8_t *Buffer, uint16_t Bytes ) *Buffer = recv_byte(); //read the remaining 511 bytes - for ( uint16_t a = 0; a < Bytes - 1; a++ ) { + do { xmit_byte ( 0xff ); // send dummy byte Buffer++; + i--; wait_till_send_done(); *Buffer = recv_byte(); - } + } while ( i ); Buffer = Buffer - ( Bytes - 1 ); @@ -352,25 +366,35 @@ uint8_t mmc_read_block ( uint8_t *cmd, uint8_t *Buffer, uint16_t Bytes ) uint8_t mmc_read_sector ( uint32_t addr, uint8_t *Buffer ) { + sint32 x; /* * send CMD17 */ uint8_t cmd[] = {CMD17, 0x00, 0x00, 0x00, 0x00, 0xFF}; // convert blocks to bytes - addr = addr << 9; //addr = addr * 512 - cmd[1] = ( ( addr & 0xFF000000 ) >> 24 ); - cmd[2] = ( ( addr & 0x00FF0000 ) >> 16 ); - cmd[3] = ( ( addr & 0x0000FF00 ) >> 8 ); + addr = addr * MMC_BLOCKLEN; + x.value32 = addr; + cmd[1] = x.value8.hh; + cmd[2] = x.value8.hl; + cmd[3] = x.value8.lh; - return mmc_read_block ( cmd, Buffer, 512 ); + /* + cmd[1] = ( ( addr & 0xFF000000 ) >> 24 ); + cmd[2] = ( ( addr & 0x00FF0000 ) >> 16 ); + cmd[3] = ( ( addr & 0x0000FF00 ) >> 8 ); + */ + return mmc_read_block ( cmd, Buffer, MMC_BLOCKLEN ); } uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks ) { + sint32 x; uint8_t resp; uint8_t cmd[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF }; + uint8_t n = numblocks; + uint16_t i; #ifdef SPI_CRC uint16_t crc; #endif @@ -397,12 +421,13 @@ uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks #endif // convert blocks to bytes - addr = addr << 9; //addr = addr * 512 + addr = addr * MMC_BLOCKLEN; + x.value32 = addr; cmd[0] = CMD18; - cmd[1] = ( ( addr & 0xFF000000 ) >> 24 ); - cmd[2] = ( ( addr & 0x00FF0000 ) >> 16 ); - cmd[3] = ( ( addr & 0x0000FF00 ) >> 8 ); - cmd[4] = ( addr & 0x000000FF ); + cmd[1] = x.value8.hh; + cmd[2] = x.value8.hl; + cmd[3] = x.value8.lh; + cmd[4] = 0; wait_till_card_ready(); @@ -415,7 +440,9 @@ uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks return ( 1 ); } - for ( uint8_t n = numblocks; n > 0; n-- ) { + do { + n--; + i = MMC_BLOCKLEN - 1; // wait for startbyte while ( 1 ) { send_dummy_byte(); @@ -426,31 +453,32 @@ uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks send_dummy_byte(); *Buffer = recv_byte(); // receive the block - for ( uint16_t a = 0; a < 512 - 1; a++ ) { + do { xmit_byte ( 0xff ); Buffer++; + i--; wait_till_send_done(); *Buffer = recv_byte(); - } + } while ( i ); // handle CRC #ifdef SPI_CRC - Buffer = Buffer - ( 512 - 1 ); + Buffer = Buffer - ( MMC_BLOCKLEN - 1 ); send_dummy_byte(); crc = recv_byte() << 8; send_dummy_byte(); crc |= recv_byte(); - if ( crc != crc16 ( Buffer, 512 ) ) { + if ( crc != crc16 ( Buffer, MMC_BLOCKLEN ) ) { MMC_Disable(); return ( 1 ); } - Buffer = Buffer + 512; + Buffer = Buffer + MMC_BLOCKLEN; #else send_dummy_byte(); send_dummy_byte(); Buffer++; #endif - } + } while ( n ); wait_till_card_ready(); @@ -478,8 +506,11 @@ uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks ) { + sint32 x; uint8_t resp; uint8_t cmd[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF }; + uint8_t n = numblocks; + uint16_t i; #ifdef SPI_CRC uint16_t crc; uint8_t crcl, crch; @@ -507,11 +538,12 @@ uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks #endif // convert blocks to bytes - addr = addr << 9; + addr = addr * MMC_BLOCKLEN; + x.value32 = addr; cmd[0] = CMD25; - cmd[1] = ( ( addr & 0xFF000000 ) >> 24 ); - cmd[2] = ( ( addr & 0x00FF0000 ) >> 16 ); - cmd[3] = ( ( addr & 0x0000FF00 ) >> 8 ); + cmd[1] = x.value8.hh; + cmd[2] = x.value8.hl; + cmd[3] = x.value8.lh; cmd[4] = 0; /* @@ -523,9 +555,9 @@ uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks return ( 1 ); } - for ( uint8_t n = numblocks; n > 0; n-- ) { + do { #ifdef SPI_CRC - crc = crc16 ( Buffer, 512 ); + crc = crc16 ( Buffer, MMC_BLOCKLEN ); crch = crc >> 8; crcl = crc & 0xff; #endif @@ -535,13 +567,13 @@ uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks xmit_byte ( MB_START ); //actually transfer the data - for ( uint16_t i = 512; i; i-- ) { + for ( i = MMC_BLOCKLEN; i; i-- ) { uint8_t data = *Buffer; Buffer++; wait_till_send_done(); xmit_byte ( data ); } - + n--; wait_till_send_done(); // handle CRC #ifdef SPI_CRC @@ -568,7 +600,7 @@ uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks MMC_Disable(); return ( 2 ); } - } + } while ( n ); wait_till_card_ready(); diff --git a/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_drv_mmc.h b/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_drv_mmc.h index af24e5b..4e53937 100644 --- a/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_drv_mmc.h +++ b/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_drv_mmc.h @@ -1,7 +1,7 @@ /* * P8000 WDC Emulator * - * $Id: wdc_drv_mmc.h,v 1.4 2012/06/05 19:46:26 olivleh1 Exp $ + * $Id: wdc_drv_mmc.h,v 1.5 2012/06/07 00:34:36 olivleh1 Exp $ * */ @@ -16,6 +16,7 @@ extern uint8_t mmc_write_sector ( uint32_t addr, uint8_t *Buffer ); extern uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks ); extern uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *Buffer, uint8_t numblocks ); +#define MMC_BLOCKLEN 512 #endif /* MMC_H_ */ diff --git a/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_if_disk.h b/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_if_disk.h index 4f9311c..2dc677b 100644 --- a/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_if_disk.h +++ b/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_if_disk.h @@ -1,7 +1,7 @@ /* * P8000 WDC Emulator * - * $Id: wdc_if_disk.h,v 1.4 2012/06/05 18:21:48 olivleh1 Exp $ + * $Id: wdc_if_disk.h,v 1.5 2012/06/07 00:34:36 olivleh1 Exp $ * */ @@ -15,4 +15,7 @@ extern uint8_t wdc_write_sector ( uint32_t addr, uint8_t *sector ); extern uint8_t wdc_read_sector ( uint32_t addr, uint8_t *sector ); extern uint8_t wdc_read_multiblock ( uint32_t addr, uint8_t *sector, uint8_t numblocks ); extern uint8_t wdc_write_multiblock ( uint32_t addr, uint8_t *sector, uint8_t numblocks ); + +#define WDC_BLOCKLEN 512 + #endif /* WDC_IF_SDCARD_H_ */ \ No newline at end of file diff --git a/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_main.c b/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_main.c index de3053c..e4e6aec 100644 --- a/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_main.c +++ b/P8000_WDC_Emulator/P8000_WDC_Emulator/wdc_main.c @@ -1,7 +1,7 @@ /* * P8000 WDC Emulator * - * $Id: wdc_main.c,v 1.14 2012/06/06 23:10:31 olivleh1 Exp $ + * $Id: wdc_main.c,v 1.15 2012/06/07 00:34:36 olivleh1 Exp $ * * TODO: - Different Errorcodes in the MMC layer (use defines) * - errorchecking in several places @@ -54,7 +54,6 @@ main ( void ) uint32_t blockno; uint8_t errorcode; uint8_t i8; - uint16_t i16; #endif atmega_setup(); @@ -66,7 +65,7 @@ main ( void ) /* load Parameter Table into RAM if valid */ blockno = 0; - data_counter = 512; + data_counter = WDC_BLOCKLEN; while ( wdc_read_sector ( blockno, data_buffer ) ) { #if DEBUG >= 1 uart_puts_p ( PSTR ( "Block 0 of SD-Card not readable" ) ); @@ -114,18 +113,12 @@ main ( void ) , data_counter ); errorcode = 1; - if ( data_counter / 512 == 1 ) { + if ( data_counter == WDC_BLOCKLEN ) { errorcode = wdc_write_sector ( blockno, data_buffer ); } else { - errorcode = wdc_write_multiblock ( blockno, data_buffer, data_counter / 512 ); + errorcode = wdc_write_multiblock ( blockno, data_buffer, data_counter / WDC_BLOCKLEN ); } - /* check if this really works - this must be used if the card does not support multiblock-access (there are some) - for ( i16 = 0; i16 < data_counter; i16 += 512 ) { - errorcode = wdc_write_sector ( blockno, &data_buffer[i16] ); - if ( errorcode ) break; - blockno++; - } - */ + if ( errorcode ) wdc_send_error(); break; @@ -136,18 +129,12 @@ main ( void ) blockno = wdc_p8kblock2sdblock ( ( ( uint32_t ) cmd_buffer[5] << 24 ) | ( ( uint32_t ) cmd_buffer[4] << 16 ) | ( ( uint16_t ) cmd_buffer[3] << 8 ) | cmd_buffer[2] ); errorcode = 1; - if ( data_counter / 512 == 1 ) { + if ( data_counter == WDC_BLOCKLEN ) { errorcode = wdc_read_sector ( blockno, data_buffer ); } else { - errorcode = wdc_read_multiblock ( blockno, data_buffer, data_counter / 512 ); + errorcode = wdc_read_multiblock ( blockno, data_buffer, data_counter / WDC_BLOCKLEN ); } - /* check if this really works - this must be used if the card does not support multiblock-access (there are some) - for ( i16 = 0; i16 < data_counter; i16 += 512 ) { - errorcode = wdc_read_sector ( blockno, &data_buffer[i16] ); - if ( errorcode ) break; - blockno++; - } - */ + if ( errorcode ) { wdc_send_error(); } else { @@ -232,7 +219,7 @@ main ( void ) break; case CMD_FMTBTT_TRACK: - data_counter = 512; + data_counter = WDC_BLOCKLEN; memset ( &data_buffer[0], 0x00, data_counter ); blockno = wdc_sector2sdblock ( cmd_buffer[2] | ( cmd_buffer[3] << 8 ) @@ -263,7 +250,7 @@ main ( void ) break; case CMD_ST_PARBTT: - data_counter = 512; + data_counter = WDC_BLOCKLEN; blockno = 0; wdc_read_par_table ( data_buffer @@ -284,7 +271,7 @@ main ( void ) break; case CMD_VER_TRACK: - data_counter = 512; + data_counter = WDC_BLOCKLEN; blockno = wdc_sector2sdblock ( cmd_buffer[2] | ( cmd_buffer[3] << 8 ) , cmd_buffer[4] @@ -308,11 +295,13 @@ main ( void ) , cmd_buffer[5] ); errorcode = 1; - for ( i16 = 0; i16 < data_counter; i16 += 512 ) { - errorcode = wdc_read_sector ( blockno, &data_buffer[i16] ); - if ( errorcode ) break; - blockno++; + + if ( data_counter == WDC_BLOCKLEN ) { + errorcode = wdc_write_sector ( blockno, data_buffer ); + } else { + errorcode = wdc_write_multiblock ( blockno, data_buffer, data_counter / WDC_BLOCKLEN ); } + if ( errorcode ) wdc_send_error(); break; @@ -372,7 +361,7 @@ void measure_performance() uart_puts_p ( PSTR ( "Timer is initialized with a Prescaler 8" ) ); uart_putc ( '\n' ); - uart_puts_p ( PSTR ( "Test Read 5000 512B-Blocks with Multiblock-Read (4KB Blocksize):" ) ); + uart_puts_p ( PSTR ( "Test Read 5000 WDC_BLOCKLENB-Blocks with Multiblock-Read (4KB Blocksize):" ) ); uart_putc ( '\n' ); for ( i8 = 0; i8 < nr_of_tests; i8++ ) { @@ -389,9 +378,9 @@ void measure_performance() uart_putc ( '\n' ); } - uart_puts_p ( PSTR ( "Test Write 5000 512B-Blocks with Multiblock-Write (4KB Blocksize):" ) ); + uart_puts_p ( PSTR ( "Test Write 5000 WDC_BLOCKLENB-Blocks with Multiblock-Write (4KB Blocksize):" ) ); uart_putc ( '\n' ); - memset ( &data_buffer[0], 0xCC, 512 * numblocks ); + memset ( &data_buffer[0], 0xCC, WDC_BLOCKLEN * numblocks ); for ( i8 = 0; i8 < nr_of_tests; i8++ ) { blockno = BLOCKNO; for ( starttime = overflow; blockno < ( BLOCKNO + 5000 ); blockno += numblocks ) { @@ -406,7 +395,7 @@ void measure_performance() uart_putc ( '\n' ); } - uart_puts_p ( PSTR ( "Test Read 5000 512B-Blocks with Singleblock-Read (512B Blocksize):" ) ); + uart_puts_p ( PSTR ( "Test Read 5000 WDC_BLOCKLENB-Blocks with Singleblock-Read (WDC_BLOCKLENB Blocksize):" ) ); uart_putc ( '\n' ); for ( i8 = 0; i8 < nr_of_tests; i8++ ) { @@ -423,9 +412,9 @@ void measure_performance() uart_putc ( '\n' ); } - uart_puts_p ( PSTR ( "Test Write 5000 512B-Blocks with Singleblock-Write (512B Blocksize):" ) ); + uart_puts_p ( PSTR ( "Test Write 5000 WDC_BLOCKLENB-Blocks with Singleblock-Write (WDC_BLOCKLENB Blocksize):" ) ); uart_putc ( '\n' ); - memset ( &data_buffer[0], 0xCC, 512 * numblocks ); + memset ( &data_buffer[0], 0xCC, WDC_BLOCKLEN * numblocks ); for ( i8 = 0; i8 < nr_of_tests; i8++ ) { blockno = BLOCKNO; for ( starttime = overflow; blockno < ( BLOCKNO + 5000 ); blockno++ ) {