CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Read a HDD or Compact Flash

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lucky



Joined: 12 Sep 2003
Posts: 46
Location: South Coast - England

View user's profile Send private message Visit poster's website

Read a HDD or Compact Flash
PostPosted: Thu Nov 27, 2003 2:52 am     Reply with quote

Hi all,
I want to read a HDD or Compact Flash with a PIC (18F452). I have looked at all sources I have found but have had trouble understanding them as they are very complex & have not been written in CCS C. Just a step by step of what I need to do in text would be great.

i.e:
1) set CS high
2) set read pin low
3) . . . . .

Thanks in advance for any help. :grin
_________________
Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Nov 27, 2003 9:30 am     Reply with quote

Are you planning on using the HDD as just a raw medium or the way a PC uses it with a file system? Something like FAT16 or FAT32?
lucky



Joined: 12 Sep 2003
Posts: 46
Location: South Coast - England

View user's profile Send private message Visit poster's website

Read a HDD or Compact Flash
PostPosted: Thu Nov 27, 2003 9:50 am     Reply with quote

Hello Mark,

To be able to use Fat16 would be nice but for now I just want to control the HDD / CF itself.

If Im not using FAT I would NEED to be able to write as well as read though.
_________________
Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop
fpgeh



Joined: 07 Sep 2003
Posts: 19
Location: Vancouver, BC

View user's profile Send private message

PostPosted: Thu Nov 27, 2003 2:42 pm     Reply with quote

I don't have any experience with interfacing to a HD but I have used CompactFlash and I found this article:
http://www.walrus.com/~raphael/pdf/compactflashOnPicArticle.pdf
to be a good start. Also the CompactFlash specification is a free download from http://www.compactflash.org and contains easy to read timing diagrams.
dvsoft
Guest







compact flash 18f452
PostPosted: Fri Nov 28, 2003 5:51 am     Reply with quote

hello

i write this source code for test on CF

#include "C:\YA1-LOGGER\YL\Prototype\Main-Board-Test\CompactFlash\Test_cf.h"
#include <stdlib.h>
#include <input.c>

//-----------------------------------------------------------------------------
// Compact flash card Address A0 to A1, WR an RD signals
//
#define CF_A0 PIN_A0
#define CF_A1 PIN_A1
#define CF_A2 PIN_A2
#define CF_RD PIN_E0
#define CF_WR PIN_E1

//-----------------------------------------------------------------------------
// Compact Data bus
//
#byte CF_DATA = 0xF83

//-----------------------------------------------------------------------------
// Compact flash tris data
//
#define CF_DATA_TRIS_IN 0xFF
#define CF_DATA_TRIS_OUT 0x00
#define CF_ADDRESS_TRIS 0x00
#define CF_SIGNAL_TRIS 0x00

//-----------------------------------------------------------------------------
// Use fast io port D,A,E
//
#use fast_io(A)
#use fast_io(D)
#use fast_io(E)

unsigned int16 CF_Signature;
unsigned int16 CF_Maxsector;
unsigned int16 CF_Maxhead;
unsigned int16 CF_Maxcyl;

//-----------------------------------------------------------------------------
void InitPic(void)
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
}
//-----------------------------------------------------------------------------
void CF_SetAddress(BYTE address)
{
output_bit(CF_A0, bit_test(address, 0));
output_bit(CF_A1, bit_test(address, 1));
output_bit(CF_A2, bit_test(address, 2));
}
//-----------------------------------------------------------------------------
void CF_Init(void)
{
//--- Set data tris
set_tris_d(CF_DATA_TRIS_IN);
//--- Set Address tris
set_tris_a(CF_ADDRESS_TRIS);
//--- Set signal tris
set_tris_e(CF_SIGNAL_TRIS);
}
//-----------------------------------------------------------------------------
int CF_Read()
{
unsigned int Data;
set_tris_d(CF_DATA_TRIS_IN);
//--- strobe read signal and get data
output_low(CF_RD);
Data = CF_DATA;
output_high(CF_RD);
return Data;
}
//-----------------------------------------------------------------------------
void CF_Write(BYTE Data)
{
// set data
set_tris_d(CF_DATA_TRIS_OUT);
CF_DATA = Data;
// Strobe write signal
output_low(CF_WR);
output_high(CF_WR);
}
//-----------------------------------------------------------------------------
void CF_WriteData(BYTE Address,BYTE Data)
{
//--- Set Address
CF_SetAddress(Address);
//--- Write data
CF_Write(Data);
}
//-----------------------------------------------------------------------------
void CF_SkipByte(Byte ByteCount)
{
Byte ByteCnt;
ByteCnt = ByteCount;
do {
output_low(CF_RD);
output_high(CF_RD);
}while (--ByteCount);
}
//-----------------------------------------------------------------------------
BYTE CF_ReadData(BYTE Address)
{
//--- Set Address
CF_SetAddress(Address);
//--- Read Data
return CF_Read();
}
//-----------------------------------------------------------------------------
BOOLEAN CF_Reset(void)
{
BYTE i;
//--- Reset no interrupt
CF_WriteData(6,0x06);
//--- Wait 1s
delay_ms(1000);
//--- End Reset, no interrupt
CF_WriteData(6,0x02);
//--- CF Status register address
CF_SetAddress(7);
//--- Loop wait ready
i = 10;
do {
//--- CF not Busy ? (D7)
if (!bit_test(CF_Read(),7))
//--- Reset Ok
return TRUE;
delay_ms(10);
}while(--i);
return FALSE;
}
//-----------------------------------------------------------------------------
BOOLEAN CF_Indentify(void)
{
//--- Send drive No
CF_WriteData(6,0xA0);
//--- Send Read Status Command
CF_WriteData(7,0xEC);
//--- Wait Busy D7
while(bit_test(CF_Read(),7));
//--- Set Address buffer 0
CF_SetAddress(0x00);
//--- General configuration - signature for the CompactFlash Storage Card
CF_Signature = CF_Read() + (256*CF_Read());
//--- Default number of cylinders
CF_Maxcyl = CF_Read() + (256*CF_Read());
//--- Skip 2 bytes
CF_SkipByte(2);
//--- Default number of heads
CF_Maxhead = CF_Read() + (256*CF_Read());
//--- Skip 4 bytes
CF_SkipByte(4);
//--- Default number of sectors per track
CF_Maxsector = CF_Read() + (256*CF_Read());
//--- Success if dataflash storage signature = 0x848A
return (CF_Signature == 0x848A);
}
//-----------------------------------------------------------------------------
void main()
{
unsigned int8 Add,Value;
//--- Initialisation
InitPic();
//--- Power up delays
delay_ms(200);
//--- Message
printf("Compact flash test\r\n");
CF_Init();
//--- Main loop
while(1) {
if (kbhit()) {
switch (getc()) {
case '0':// Test signal RD = 0
printf("RD = 0\r\n");
output_low(CF_RD);
break;
case '1':// Test signal RD = 1
printf("RD = 1\r\n");
output_high(CF_RD);
break;
case '2':// Test signal wr = 0
printf("WD = 0\r\n");
output_low(CF_WR);
break;
case '3':// Test signal wr = 1
printf("WD = 1\r\n");
output_high(CF_WR);
break;
case 'w':// Test write
printf("Address :");
Add = get_int();
printf(" Value :");
Value = get_int();
CF_SetAddress(Add);
CF_Write(Value);
printf(" OK\r\n");
break;
case 'r':// Test read
printf("Address :");
Add = get_int();
CF_SetAddress(Add);
printf("Read %x\r\n",CF_Read());
break;
case 'a':// Test Set Adresse
printf("Address :");
Add = get_int();
CF_SetAddress(Add);
printf(" Ok\r\n");
break;
case 'd':// Dump after cmd a
printf("Read %x\r\n",CF_Read());
break;
case 'i':// Identify function (only after reset function
printf("Indentify..\r\n");
if (CF_Indentify()) {
printf("CF_Signature :%lu\r\n",CF_Signature);
printf("Nb Sector by track :%lu\r\n",CF_Maxsector);
printf("Nb Head :%lu\r\n",CF_Maxhead);
printf("Nb Cylinder :%lu\r\n",CF_Maxcyl);
}// End IF
else
printf("Compact not ready !!\r\n");
break;
case 'u':// Reset Test
printf("Reset....");
if (CF_Reset())
printf(" Ok\r\n");
else
printf(" No Ok\r\n");
break;
}// End Switch
}// End if
}// End while

}
//-----------------------------------------------------------------------------
lucky



Joined: 12 Sep 2003
Posts: 46
Location: South Coast - England

View user's profile Send private message Visit poster's website

Compact Flash 18f452
PostPosted: Fri Nov 28, 2003 6:45 am     Reply with quote

dvsoft,

Thank you, this is exactly what I wanted Very Happy . It all looks so simple too Shocked !!

Thanks.
_________________
Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop
lucky



Joined: 12 Sep 2003
Posts: 46
Location: South Coast - England

View user's profile Send private message Visit poster's website

CF Code
PostPosted: Thu Apr 22, 2004 5:59 am     Reply with quote

Responce to Private Message:

Hello ljbeng,

I havent had much time to work on this project but I am just getting back into it after a long break. I am able to ID the CF & write & read data to & from the card but I need to do a little tidying before I release the code.

At the moment I have no FAT implementation but I may try to run FAT16 soon.

I will put all of my code so far on my website in the next few days. Then you can go there & download it & all of my supporting documents.

The address is www.Mpic3.com.

Please bear in mind that the site is not yet complete!

_________________
Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop
SteveS



Joined: 27 Oct 2003
Posts: 126

View user's profile Send private message

PostPosted: Thu Apr 22, 2004 7:16 am     Reply with quote

This fellow has PIC code for FAT16 - geared towards reading MP3 files. I have modified the code to run on an 18F6620 and all the basics work.

http://www.walrus.com/~raphael/html/mp3.html
D. Collin



Joined: 16 Apr 2005
Posts: 7

View user's profile Send private message

Read a HDD or Compact Flash
PostPosted: Tue May 03, 2005 11:32 am     Reply with quote

Hi all,

Sorry if this is repeated question/answer:

Using the code provided here (highly appreciated) by dvsoft, I can communicate with the CF card, the Identify function returns correct data:

Signature: 33930
Sector by track: 32
Heads: 8
Cylinders: 978

Then I tried to use:

CF_WriteData(5,0); //Cylinder high
CF_WriteData(4,0); //Cylinder Low
CF_WriteData(3,1); //Select sector # 1
CF_WriteData(6,0xA0); //Select Card 0, Head 0
CF_WriteData(2,1); //1 sector to read
CF_WriteData(7,0x20); //Send Read Command

//delay for 1 ms for card to fill card buffer

And then call CF_ReadData(0) 512 times to fill PIC internal buffer with the sector data.

Shouldn’t this fill the buffer with the first sector (boot sector) which is on head 0, cylinder 0, and sector 1? It doesn’t when comparing the first sector with WinHex.

I feel I am doing something wrong here but I am using the exact code provided with the addition of the above code to read sector 0.

Thanx in advance.

D.Collin
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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