//******************************************************************************
// compactFlashInit
//
// Initializes the Interface - sets input/outputs/registers/etc.
//******************************************************************************
void compactFlashInit() // TODO: get the buffersize automatically
{
trisa = 00000101b;
trisb = 0;
trise = 0;
adcon1 = 0x06;
adcon0 = 0;
option_reg = 0xA0;
CF_CONTROL = 00011000b;
CF_ADDR = 0;
}
//******************************************************************************
// compactFlashWrite
//
// Writes a char to a register of the card
// addr: Address of the register (0-7)
// data: Data to write to the register
//******************************************************************************
void compactFlashWrite(char addr, char data)
{
CF_ADDR = addr;
CF_DATA = data;
compactFlashWait();
nop();
clear_bit(CF_CONTROL, WE);
nop();
nop();
nop();
set_bit(CF_CONTROL, WE);
}
//******************************************************************************
// compactFlashRead
//
// Reads a register of the card
// addr: Address of the register (0-7)
//******************************************************************************
char compactFlashRead(char addr)
{
char data;
trisb = 11111111b;
CF_ADDR = addr;
CF_DATA = 0;
compactFlashWait();
nop();
clear_bit(CF_CONTROL, OE);
nop();
nop();
nop();
data = CF_DATA;
nop();
set_bit(CF_CONTROL, OE);
trisb = 00000000b;
return data;
}
//******************************************************************************
// compactFlashWriteBuffer
//
// Writes the local to the buffer of the card
// offset: offset of the first byte to write (in bytes)(in the card buffer)
// length: how many bytes to write
//******************************************************************************
void compactFlashWriteBuffer(char offset, char length) // TODO: offset and length as long
{
char i;
for(i = compactFlashPosition; i < offset; i++)
{
compactFlashPosition++;
compactFlashWrite(DATA_REG, 0); // TODO: This erases all data in the offset range!
}
for(i = 0; i < length; i++) // i += 2 and i + 2 doesn't work! Could be a bug!
{
compactFlashPosition++;
compactFlashWrite(DATA_REG, compactFlashBuffer[i + 1]);
compactFlashPosition++;
compactFlashWrite(DATA_REG, compactFlashBuffer[i]);
i++;
}
}
//******************************************************************************
// compactFlashReadBuffer
//
// Reads the buffer of the card and stores it to the local buffer
// offset: offset of the first byte to read (in bytes)
// length: how many bytes to read
//******************************************************************************
void compactFlashReadBuffer(char offset, char length) // TODO: offset and length as long
{
char i;
for(i = compactFlashPosition; i < offset; i++)
{
compactFlashPosition++;
compactFlashRead(DATA_REG);
}
for(i = 0; i < length; i++) // i += 2 and i + 2 doesn't work! Could be a bug!
{
compactFlashPosition++;
compactFlashBuffer[i + 1] = compactFlashRead(DATA_REG);
compactFlashPosition++;
compactFlashBuffer[i] = compactFlashRead(DATA_REG);
i++;
}
}
//******************************************************************************
// compactFlashFillBuffer
//
// Fills up the COmpactFLASH buffer
//******************************************************************************
void compactFlashFillBuffer()
{
char a, b, c;
for(a = 0; a < compactFlashBufferSize; a++)
{
for(b = 0; b < 2; b++)
{
for(c = compactFlashPosition; c < 255; c++)
{
compactFlashWrite(DATA_REG, 0); // TODO: This erases all data after the offset!
}
compactFlashPosition = 0;
}
}
}
//******************************************************************************
// compactFlashClearBuffer
//
// Clears the local buffer
//******************************************************************************
void compactFlashClearBuffer()
{
char i;
for(i = 0; i < 64; i++)
{
compactFlashBuffer[i] = 0;
}
}
//******************************************************************************
// compactFlashPresent
//
// Checks if a card is inserted
// returns a boolean
//******************************************************************************
char compactFlashPresent()
{
return((CF_CONTROL & 1 << CD2) == 0);
}
//******************************************************************************
// compactFlashFlashWait
//
// Waits for the card to finish it's operation
//******************************************************************************
void compactFlashWait()
{
while((CF_CONTROL & 1 << RDY) == 0);
}
//******************************************************************************
// compactFlashReset
//
// Resets the card
//******************************************************************************
void compactFlashReset()
{
set_bit(CF_CONTROL, RESET);
nop();
nop();
clear_bit(CF_CONTROL, RESET);
delay_ms(250);
delay_ms(250);
}
//******************************************************************************
// compactFlashIdentify
//
// Sends the Indentify Drive command to the card
//******************************************************************************
void compactFlashIdentify()
{
compactFlashPosition = 0;
compactFlashWrite(COMMAND_REG, IDENTIFY);
}
//******************************************************************************
// compactFlashDiagnostic
//
// Sends the Drive Diagnostic command to the card
// returns the error number
//******************************************************************************
char compactFlashDiagnostic()
{
compactFlashWrite(COMMAND_REG, DIAGNOSTIC);
return compactFlashRead(ERROR_REG);
}
//******************************************************************************
// compactFlashReadBlock
//
// Sends the Read Sector command to the card, operates in locical block mode
// blockHigh: first byte of the 3 block address bytes
// blockMiddle: middle byte...
// blockLow: last byte...
//******************************************************************************
void compactFlashReadBlock(char blockHigh, char blockMiddle, char blockLow)
{
compactFlashPosition = 0;
compactFlashWrite(HEAD_REG, 0xE0);
compactFlashWrite(BLOCKHIGH_REG, blockHigh);
compactFlashWrite(BLOCKMIDDLE_REG, blockMiddle);
compactFlashWrite(BLOCKLOW_REG, blockLow);
compactFlashWrite(BLOCKCOUNT_REG, 1); // TODO: block count is always 1 because it makes no sense to read multible blocks, now!
compactFlashWrite(COMMAND_REG, READ_BLOCK);
}
//******************************************************************************
// compactFlashWriteBlock
//
// Sends the Write Sector command to the card, operates in locical block mode
// blockHigh: first byte of the 3 block address bytes
// blockMiddle: middle byte...
// blockLow: last byte...
//******************************************************************************
void compactFlashWriteBlock(char blockHigh, char blockMiddle, char blockLow)
{
compactFlashPosition = 0;
compactFlashWrite(HEAD_REG, 0xE0);
compactFlashWrite(BLOCKHIGH_REG, blockHigh);
compactFlashWrite(BLOCKMIDDLE_REG, blockMiddle);
compactFlashWrite(BLOCKLOW_REG, blockLow);
compactFlashWrite(BLOCKCOUNT_REG, 1); // TODO: block count is always 1 because it makes no sense to read multible blocks, now!
compactFlashWrite(COMMAND_REG, WRITE_BLOCK);
}
Copyright © 2002-2006 SourceBoost Technologies