|
|
View previous topic :: View next topic |
Author |
Message |
young
Joined: 24 Jun 2004 Posts: 285
|
Flash card read and write. |
Posted: Tue Feb 15, 2005 1:14 pm |
|
|
I am going to develop a device to write and read data to and from a flash card. any idea how it works it? thanks in advance! |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Tue Feb 15, 2005 1:25 pm |
|
|
Try searching this site for the words "flash card" (set to search for all terms) and you will find a lot about it including code. There is also code in the Code Library.
Last edited by dyeatman on Tue Feb 15, 2005 9:28 pm; edited 1 time in total |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Feb 15, 2005 9:27 pm |
|
|
Oh my goodness! Sorry, I just had to get that out. |
|
|
lucky
Joined: 12 Sep 2003 Posts: 46 Location: South Coast - England
|
CF Read code |
Posted: Wed Feb 16, 2005 9:12 am |
|
|
http://www.mpic3.com
Check the Compact Flash Fat16 code in the downloads area. (reads only). _________________ Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Thu Mar 17, 2005 7:42 am |
|
|
Hi Lucky:
Sorry I was distracted from this project for a while, and I am coming back to work on it.
here are some concepts that I want to clearify.
1. In your cf.c code, what does CF_SetAddress(BYTE address) means
Code: | CF_SetAddress(BYTE address){
output_bit(CF_ADDRESS_0,bit_test(address,0));
output_bit(CF_ADDRESS_1,bit_test(address,1));
output_bit(CF_ADDRESS_2,bit_test(address,2));
}
|
CF_ADDRESS_0, CF_ADDRESS_1, CF_ADDRESS_2, should be defined as
Code: |
#define CF_ADDRESS_0 PIN_A0
#define CF_ADDRESS_1 PIN_A1
#define CF_ADDRESS_2 PIN_A2
| or some other pins of microcontroller, right?
and other CF_READ, CF_WRITE etc. should be defined to pins connect to flashcart pin oe,we,etc. right?
2. the variable BYTE address in the CF_SetAddress(BYTE address) definition is corredponding to the eight control registers, right?
3. CF is read and write at a 512 bytes per time, right? how do you setup the right sectors to w/r the data, is it like this,
for read,
a. setup control register to 0x03 using, 03 is sector number
CF_SetAddress(0x03)
b, enable oe line, which is defined by CF_READ
output_low(CF_READ)
c, put data to portd, which is data line the sector number, for example, if I want to go to sector 200 then output_d(200)
d, and then go the data reading set control register as CF_SetAddress(0x00)
e. enable oe output_low(CF_READ)
f, read data data=portd
clearify me where I am wrong? |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Thu Mar 17, 2005 7:46 am |
|
|
Just to correct a typo
Quote: | for read,
a. setup control register to 0x03 using, 03 is sector number
CF_SetAddress(0x03)
b, enable oe line, which is defined by CF_READ
output_low(CF_READ)
c, put data to portd, which is data line the sector number, for example, if I want to go to sector 200 then output_d(200)
d, and then go the data reading set control register as CF_SetAddress(0x00)
e. enable oe output_low(CF_READ)
f, read data data=portd
|
a. 03 is sector number should be 03 is control register for sector number[/code] |
|
|
lucky
Joined: 12 Sep 2003 Posts: 46 Location: South Coast - England
|
CF Read code |
Posted: Thu Mar 17, 2005 7:55 am |
|
|
Hi young,
Download the MPic3 player code zip and the MPic3 player diagram and this will answer all of your questions. You are correct in what you have put and CF_SetAddress(BYTE address) is simply used to select the correct address on the CF address lines. Goto www.mpic3.com/downloads you should also try www.mpic3.com/forum for advice from the other users of this code. _________________ Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Thu Mar 17, 2005 8:50 am |
|
|
THANK YOU LUCKY:
I already download the zip code, and had been studing your code. and I went to the forum, I did not find what I want to know, by the way, it is a great website
so far for the pins definition is clearfied by you, however, I need more detailed step by step understanding of how CF w/r works, please help me out of this.
1. I did not see how you use register to go to a sector, is my discription in the last post correct.
2. in your code, there is a int32 compactFlastPosition variable, and it is used in Code: |
void compactFlashReadBuffer(int32 offset, int32 length){
int32 i;
for(i=compactFlashPosition;i<offset;i++){
compactFlashPosition++;
CF_ReadData(0);
}
for(i=0;i<length;i++){
compactFlashPosition++;
buffer[i]=CF_ReadData(0);
}
} |
what is exactly compactFlastPosition means, it is a address? how do you go to the compactFlashPosition? for example, if I want to go to 33550000
location (32M=33554432), and offset 0, and length 10, I have to let
Code: | compactFlastPosition=33550000; |
and then call the function compactFlashReadBuffer(0,10). I believe it is wrong, would you please explain how compactFlashReadBuffer() works?
Thanks again |
|
|
lucky
Joined: 12 Sep 2003 Posts: 46 Location: South Coast - England
|
CF Read code |
Posted: Thu Mar 17, 2005 9:12 am |
|
|
Hi young,
1) get_sector() is used to skip to the required location on the CF. This can be seen in FAT16.c when find_file() is used to read the mp3. get_sector() then writes all of the required data (head, cylinder, mode etc) to the CF then calls compactFlashReadBuffer() to get 512 bytes of data (does not have to be 512 bytes).
2) compactFlastPosition is just used to keep track of how far in to the CF you have read as each read will incrament the counter inside of the CF.
To read sector 128 you could simply call get_sector(128); then the first 512 bytes of sector 128 would be in buffer[0] - buffer[511]. _________________ Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Thu Mar 17, 2005 9:45 am |
|
|
Thnak you:
You are leading me to solutions------>
in your get_sector function, why you define sectors=32, int heads=16, and what is the a means here and why there is a for loop to increase what is the k means?
Code: |
a=heads*sectors;
for(sector=sector;sector>=a;sector=sector-a) k++;
|
|
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Thu Mar 17, 2005 10:09 am |
|
|
I found out that 1 track=32cluster, 1 clusterr=16sectors a should be the total sectors per track, right? |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Mar 17, 2005 10:11 am |
|
|
Microsoft has FAT specifications. Take a look at them and most of this will make sense to you. |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Thu Mar 17, 2005 10:14 am |
|
|
Thank you, by the calculation I now understand how your get_sectors works, thank for being with me.I start to write a write_sector function according to your code, please help me. I will post the code soon for you to check! |
|
|
lucky
Joined: 12 Sep 2003 Posts: 46 Location: South Coast - England
|
CF Read code |
Posted: Thu Mar 17, 2005 10:18 am |
|
|
young,
This is the calcualtions to find the correct head and cylinder for the requested sector.
sectors and heads should be read from the CF and populated in read_boot_sector() (the commented out bit) but this was work in progress and I never got this working so I set sectors and heads manuly.
This code could probably use some optimisation and clean-up but it does work. I am continuing the MPic3 player project in a few weeks time when I get my first proper PCB's in. I will then re-work this code to improve it and make it more readable and add comments etc. It has been a long time since I have worked on it and it was all left in a bit of a mess with work still in progress.
Please sign up to the MPic3 forum. _________________ Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop |
|
|
young
Joined: 24 Jun 2004 Posts: 285
|
|
Posted: Thu Mar 17, 2005 10:26 am |
|
|
HI lucky:
Please help me to the code for writing data
here is a function corresponding to compactFlashWriteBuffer.
Code: |
void compactFlashWriteBuffer(int32 offset, int32 length){
int32 i;
for(i=compactFlashPosition;i<offset;i++){
compactFlashPosition++;
CF_ReadData(0);
}
for(i=0;i<length;i++){
compactFlashPosition++;
WRITE_CF_DATA(0,buffer[i]);
}
} |
but I do not know if this part is necessary or not
Code: |
for(i=compactFlashPosition;i<offset;i++){
compactFlashPosition++;
CF_ReadData(0);
}
|
here is the corresponding for get_sector
Code: |
void write_sector(int32 sector){
int32 i;
int32 k=0;
int32 a=0;
int32 sectors=32;
int32 heads=16;
compactFlashPosition=0;
a=heads*sectors;
for(sector=sector;sector>=a;sector=sector-a) k++;
WRITE_CF_DATA(5,k>>8); // write cylinder
WRITE_CF_DATA(4,k);
k=0;
for(sector=sector;sector>=sectors;sector=sector-sectors) k++;
WRITE_CF_DATA(3,sector+1); // write sector
WRITE_CF_DATA(6,k+0xA0); // write head
WRITE_CF_DATA(2,1);
WRITE_CF_DATA(7,0x20);
delay_us(200); // Wait For Data
compactFlashWriteBuffer(0,512);
} |
please light me up with your genius thought |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|