Jump to content

1541 emulates by sd card


reboot
 Share

Recommended Posts

Hello,

my 2cents: can a PIC emulate an I2C eeprom? If so, it could be visible to the Core as a common (huge) BankStick and no modification to MIOS would be required. On the other hand, the emulator can implement all the SD/FAT stuff, or even gather data from anything else than an SD.

Link to comment
Share on other sites

  • Replies 53
  • Created
  • Last Reply

Top Posters In This Topic

I had the idea to use a PIC18F4550 to bridge the SD card to USB and IIC, but I skipped it because of the reduced flexibility, and the slow IIC transfer speed.

I also think, that getting such a system stable and foolproven would cost me even more effort.

Best Regards, Thorsten.

Link to comment
Share on other sites

I'd been thinking more SPI than I2C, and using a second processor to off load the file system from the main processor, though the filing I/O and display etc wouldn't mean that much of an off-load.

I still like the idea of a local processor to do the filing system 'primitives', given the cost of the hardware, but I'm not competent enough in PIC design to analyse the hardware/software trade off.

My thinking was coloured by my work with CF. IDE already does quite a lot of the filing primitive worK, SCSI does a bit more. It might be possible to have a similar thing for SD.

A point I'd forgotten: where are the life extending algorithms for the SD?, (the software that prevents spurious erase and write cycles, to optimise the cards working life) are they in the card, or do they need to be implemented outside?

Card would still be a very good idea though.

Best wishes

Mike

Link to comment
Share on other sites

A point I'd forgotten: where are the life extending algorithms for the SD?, (the software that prevents spurious erase and write cycles, to optimise the cards working life) are they in the card, or do they need to be implemented outside?

They are in the card itself.

Best regards, ilmenator

Link to comment
Share on other sites

In distance to IIC, SPI doesn't allow to stretch a data transfer request, accordingly the SPI slave has to handle the request immediately during a block transfer to avoid data loss. An handshaking to sync the master with the slave can only take place at the beginning of a block transfer. Since SD is interfaced via SPI as well, the resulting overall bandwidth will be (much) lower than the speed which could be achieved on a direct connection. And it would probably be lower than using the more reliable IIC protocol.

Only elegant solution I see for a multi-processor setup: searching for a ready-made open source solution which is running on a faster microcontroller. The microcontroller should be faster than the PIC to avoid performance loss. Once such a project has been found, let's compare the costs ;)

Best Regards, Thorsten.

Link to comment
Share on other sites

  • 3 weeks later...

Well, they've got a new firmware in progress at the moment, which increases speed, and introduces some new features, I suspect that IIC may be one of them... But it still works with SPI in the meantime.

While they're focussing on the new firmware, they aren't shipping to distributors, but sparkfun had these for $12, and I read a fairly recent forum post saying something about a $5 price (probably in large quantities).... They also have a note there saying that they will take inquiries about stock in the meantime. I suspect that a community like ours could arrange a decent size bulk buy of these, and maybe we could provide some useful assistance to them with testing the IIC interface? Could be a win-win situation....

Link to comment
Share on other sites

Yes, I saw that Sparkfun had the chips for US 13$ (and the complete module for US 45$ - a little steep for my taste).

A community bulk buy might actually be a good idea. Assuming that the bankstick memory was replaced in all applications with this module (or the SD would be available as an alternative to the bankstick memory), this could actually create the necessary demand. Let's see how the USB MIDI interface thingy develops and what experiences can be gained from that. The documentation looks as if the necessary hardware was rather easy to implement, and the ridiculous prices for (proper) card connectors should come down considerably for larger quantities.

Best regards, ilmenator

Link to comment
Share on other sites

I dunno, I got the best connector around for $5... They aren't that bad. I even offered freebies to everyone I saw at the time I was ordering, and noone wanted one!

Speaking of which... $5-13 for the chip... $5 for the socket... so, that sparkfunPCB is worth like 30 bucks. ouch! Good if you want it ready to go, but in our case, I'm sure we could make it as a small kit for a lot cheaper.

It's an interesting idea as to whether they could be used as a generic replacement for banksticks. Given that these are file-based, and BS are address-based, I don't know how it'll work. The datasheets will say a lot :)

Link to comment
Share on other sites

I dunno, I got the best connector around for $5...

Now, that sounds more reasonable than what even Reichelt have in their shop - usually they have rather good prices, so I might got confused.

And yes, the PCB should be cheaper than what Sparkfun has to offer. As for file-based vs. address-based: a simple conversion/naming scheme might already do the trick.

Best regards, ilmenator

Link to comment
Share on other sites

http://australia.rs-online.com/web/search/searchBrowseAction.html?method=getProduct&R=5025010

That's the Hirose DMB1 series, it's a springloaded push-in/push-out with a detector switch and all that, AUD$4.81 (about USD$5). This seems to be the standard 'nice' connector :) This is the DMB1-B* where the card goes into the slot pin-side-up, There is one that costs ~10c more, where the card is not upside-down, the DMB1-A*. Or, you can go with a slightly less-nice older model like this one for about $3 :)

Link to comment
Share on other sites

That kind of connector is ok if you never ever want to change the SD card, e.g. if it goes inside the box as a fixed memory extension. It is not so nice anymore (and probably rather error-prone) if you want to exchange the SD card on a regular basis. Though at the moment, I cannot think of any application scenario that would make replacing an SD card necessary - except for backups maybe, but then a file system is a must. dosonchip again...

Best regards, ilmenator

Link to comment
Share on other sites

This spaghetti code is mainly a "proof of concept" - it searches for a specific file (here: MBSID_P.V2) in the root directory, and returns the starting sector. It has a lot of room for improvements (e.g. I splitted the 512 byte sector into two pages, so that array accesses are faster in C) - but the code will be thrown away anyhow, since the final version will be in assembler.

So, next step is to write the algorithm in assembler, and the last step is a "file seek" function, which goes through the FAT to search for a specific file offset (easy!)


unsigned long partition_sector0;
unsigned char partition_cluster_size;
unsigned int  partition_res_sectors;
unsigned int  partition_fat_sectors;
unsigned int  partition_root_entries;
unsigned long partition_sector_fat;
unsigned long partition_sector_root;
unsigned char partition_root_sectors;
unsigned long partition_sector_cl2;
unsigned long partition_sector_file;
unsigned int  partition_file_cluster;

const unsigned char filename[8+3+1] = "MBSID_P V2 ";
#define EXPECTED_FILESIZE_B0 0x00
#define EXPECTED_FILESIZE_B1 0x00
#define EXPECTED_FILESIZE_B2 0x08
#define EXPECTED_FILESIZE_B3 0x00

#define SDCARD_ERROR_NONE                 0x00

#define SDCARD_ERROR_MBR_MISSING_MARKER   0x01
#define SDCARD_ERROR_MBR_WRONG_PARTI_TYPE 0x02

#define SDCARD_ERROR_BR_MISSING_MARKER    0x08
#define SDCARD_ERROR_BR_UNSUPP_SEC_SIZE   0x09

#define SDCARD_ERROR_FILE_NOT_FOUND       0x10
#define SDCARD_ERROR_FILE_WRONG_SIZE      0x11

// returns != 0 on errors
unsigned char ReadFile(void)
{
  unsigned char entry, dir_sec, offset, i;

  // read MBR
  SDCARD_SectorRead(0x00000000);

  // references for MBR structure:
  // http://mirror.href.com/thestarman/asm/mbr/PartTables.htm
  // http://home.teleport.com/~brainy/fat16.htm

  // check for boot marker
  if( sdcard_buffer_p1[0xfe] != 0x55 || sdcard_buffer_p1[0xff] != 0xaa )
    return SDCARD_ERROR_MBR_MISSING_MARKER;

  // check for type of partition (expecting FAT16 larger than 32MB)
  if( sdcard_buffer_p1[0xbe + 0x04] != 0x06 )
    return SDCARD_ERROR_MBR_WRONG_PARTI_TYPE;

  // get first sector of partition
  partition_sector0 = (sdcard_buffer_p1[0xbe + 0x09] << 8) | sdcard_buffer_p1[0xbe + 0x08];

  // read FAT16 boot record at sector 0 of partition
  SDCARD_SectorRead(partition_sector0);

  // check for boot marker
  if( sdcard_buffer_p1[0xfe] != 0x55 || sdcard_buffer_p1[0xff] != 0xaa )
    return SDCARD_ERROR_BR_MISSING_MARKER;

  // check that bytes per sectors is 512
  if( sdcard_buffer_p0[0x0b] != 0x00 || sdcard_buffer_p0[0x0c] != 0x02 )
    return SDCARD_ERROR_BR_UNSUPP_SEC_SIZE;

  // bytes per clusters
  partition_cluster_size = sdcard_buffer_p0[0x0d];

  // number of reserved sectors
  partition_res_sectors = (sdcard_buffer_p0[0x0f] << 8) | sdcard_buffer_p0[0x0e];

  // number of FAT sectors
  partition_fat_sectors = (sdcard_buffer_p0[0x17] << 8) | sdcard_buffer_p0[0x16];

  // calculate address to FAT
  partition_sector_fat = partition_sector0 + partition_res_sectors;

  // calculate address to root directory
  partition_sector_root = partition_sector_fat + 2*partition_fat_sectors;

  // max. number of root entries
  partition_root_entries = (sdcard_buffer_p0[0x12] << 8) | sdcard_buffer_p0[0x11];

  // number of directory sectors
  partition_root_sectors = (partition_root_entries >> 4);

  // calculate address to cluster 2 (start of data)
  partition_sector_cl2 = partition_sector_root + partition_root_sectors;

  // read root directory and search for filename
  // reference: http://home.teleport.com/~brainy/lfn.htm
  partition_file_cluster=0;
  for(dir_sec=0; (dir_sec<partition_root_sectors) && !partition_file_cluster; ++dir_sec) {
    SDCARD_SectorRead(partition_sector_root + dir_sec);

    // searching for file in lower and upper page
    for(entry=0, offset=0x00; (entry<8) && !partition_file_cluster; ++entry) {

      // lower page
      for(offset=entry<<5, i=0; i<8+3; ++i, ++offset) {
MIOS_MIDI_TxBufferPut(0xc0);
MIOS_MIDI_TxBufferPut(offset & 0x7f);
if( sdcard_buffer_p0[offset] != filename[i] )
  break;
      }

      if( i == 8+3 ) {
if( sdcard_buffer_p0[offset-8-3+28] != EXPECTED_FILESIZE_B0 ||
    sdcard_buffer_p0[offset-8-3+29] != EXPECTED_FILESIZE_B1 ||
    sdcard_buffer_p0[offset-8-3+30] != EXPECTED_FILESIZE_B2 ||
    sdcard_buffer_p0[offset-8-3+31] != EXPECTED_FILESIZE_B3 ) {
  return SDCARD_ERROR_FILE_WRONG_SIZE;
}
partition_file_cluster = (sdcard_buffer_p0[offset+-8-3+27] << 8) | sdcard_buffer_p0[offset+-8-3+26];
      } else {
// upper page
for(offset=entry<<5, i=0; i<8+3; ++i, ++offset) {
  if( sdcard_buffer_p1[offset] != filename[i] )
    break;
}

if( i == 8+3 ) {
  if( sdcard_buffer_p1[offset-8-3+28] != EXPECTED_FILESIZE_B0 ||
      sdcard_buffer_p1[offset-8-3+29] != EXPECTED_FILESIZE_B1 ||
      sdcard_buffer_p1[offset-8-3+30] != EXPECTED_FILESIZE_B2 ||
      sdcard_buffer_p1[offset-8-3+31] != EXPECTED_FILESIZE_B3 ) {
    return SDCARD_ERROR_FILE_WRONG_SIZE;
  }
  partition_file_cluster = (sdcard_buffer_p1[offset+-8-3+27] << 8) | sdcard_buffer_p1[offset+-8-3+26];
}
      }
    }
  }

  // exit if file hasn't been found in root directory
  if( !partition_file_cluster )
    return SDCARD_ERROR_FILE_NOT_FOUND;

  // read first sector of file
  partition_sector_file = partition_sector_cl2 + (partition_file_cluster-2) * partition_cluster_size;
  SDCARD_SectorRead(partition_sector_file);

  return SDCARD_ERROR_NONE;
}
[/code]

Best Regards, Thorsten.

Link to comment
Share on other sites

Sweet!! 8)

"Oops, TK did it again"  ;D

Last call for SD connectors, if you want one, PM me NOW! They'll be USD$5 plus postage from Australia to wherever you are.

Here's the link with datasheet:

http://australia.rs-online.com/web/search/searchBrowseAction.html?method=getProduct&R=5025010

For 30c more you can get the top-side-up version:

http://australia.rs-online.com/web/search/searchBrowseAction.html?method=getProduct&R=5025004

If you're stateside then you might like to look to sparkfun, but it seems that you euro guys have no cheap source, so here it is :) Last chance from me for a while!

Link to comment
Share on other sites

  • 5 weeks later...

Okay, I think I know how the schematic TK uses looks like. Apparently, judging from the photos he posted, it is very similar to this, which is basically the same as published by U. Radig here. All the sources I found on the net are extremely similar  :).

I'll see if I can find these components in my spare parts box  :D.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share


×
×
  • Create New...