Compact Flash Library

The Compact Flash Library provides routines for accessing data on Compact Flash card (abbr. CF further in text). CF cards are widely used memory elements, commonly used with digital cameras. Great capacity and excellent access time of only a few microseconds make them very attractive for microcontroller applications.

In CF card, data is divided into sectors. One sector usually comprises 512 bytes. Routines for file handling, the Cf_Fat routines, are not performed directly but successively through 512B buffer.

  Important :

Library Dependency Tree

Compact Flash Library Dependency Tree

External dependencies of Compact Flash Library

Stellaris

The following variables must be defined in all projects using Compact Flash Library: Description : Example :
var CF_Data_Port : dword; sfr; external; Compact Flash Data Port. var CF_Data_Port : byte at GPIO_PORTE_DATA;
var CF_RDY : sbit; sfr; external; Ready signal line. var CF_RDY : sbit at GPIO_PORTD_DATA7_bit;
var CF_WE : sbit; sfr; external; Write Enable signal line. var CF_WE : sbit at GPIO_PORTD_DATA6_bit;
var CF_OE : sbit; sfr; external; Output Enable signal line. var CF_OE : sbit at GPIO_PORTD_DATA5_bit;
var CF_CD1 : sbit; sfr; external; Chip Detect signal line. var CF_CD1 : sbit at GPIO_PORTD_DATA4_bit;
var CF_CE1 : sbit; sfr; external; Chip Enable signal line. var CF_CE1 : sbit at GPIO_PORTD_DATA3_bit;
var CF_A2 : sbit; sfr; external; Address pin 2. var CF_A2 : sbit at GPIO_PORTD_DATA2_bit;
var CF_A1 : sbit; sfr; external; Address pin 1. var CF_A1 : sbit at GPIO_PORTD_DATA1_bit;
var CF_A0 : sbit; sfr; external; Address pin 0. var CF_A0 : sbit at GPIO_PORTD_DATA0_bit;
var CF_RDY_direction : sbit; sfr; external; Direction of the Ready pin. var CF_RDY_direction : sbit at GPIO_PORTD_DIR7_bit;
var CF_WE_direction : sbit; sfr; external; Direction of the Write Enable pin. var CF_WE_direction : sbit at GPIO_PORTD_DIR6_bit;
var CF_OE_direction : sbit; sfr; external; Direction of the Output Enable pin. var CF_OE_direction : sbit at GPIO_PORTD_DIR5_bit;
var CF_CD1_direction : sbit; sfr; external; Direction of the Chip Detect pin. var CF_CD1_direction : sbit at GPIO_PORTD_DIR4_bit;
var CF_CE1_direction : sbit; sfr; external; Direction of the Chip Enable pin. var CF_CE1_direction : sbit at GPIO_PORTD_DIR3_bit;
var CF_A2_direction : sbit; sfr; external; Direction of the Address 2 pin. var CF_A2_direction : sbit at GPIO_PORTD_DIR2_bit;
var CF_A1_direction : sbit; sfr; external; Direction of the Address 1 pin. var CF_A1_direction : sbit at GPIO_PORTD_DIR1_bit;
var CF_A0_direction : sbit; sfr; external; Direction of the Address 0 pin. var CF_A0_direction : sbit at GPIO_PORTD_DIR0_bit;

MSP432

The following variables must be defined in all projects using Compact Flash Library: Description : Example :
var Cf_Data_Port_Output : word; sfr; external; Compact Flash Data Port Output. var Cf_Data_Port_Output : word at DIO_P5OUT;
var Cf_Data_Port_Input : word; sfr; external; Compact Flash Data Port Input. var Cf_Data_Port_Input : word at DIO_P5IN;
var CF_RDY : sbit; sfr; external; Ready signal line. var CF_RDY : sbit at DIO_P6OUT.B0;
var CF_WE : sbit; sfr; external; Write Enable signal line. var CF_WE : sbit at DIO_P6OUT.B1;
var CF_OE : sbit; sfr; external; Output Enable signal line. var CF_OE : sbit at DIO_P6OUT.B2;
var CF_CD1 : sbit; sfr; external; Chip Detect signal line. var CF_CD1 : sbit at DIO_P6OUT.B3;
var CF_CE1 : sbit; sfr; external; Chip Enable signal line. var CF_CE1 : sbit at DIO_P6OUT.B4;
var CF_A2 : sbit; sfr; external; Address pin 2. var CF_A2 : sbit at DIO_P6OUT.B5;
var CF_A1 : sbit; sfr; external; Address pin 1. var CF_A1 : sbit at DIO_P6OUT.B6;
var CF_A0 : sbit; sfr; external; Address pin 0. var CF_A0 : sbit at DIO_P6OUT.B7;
var CF_RDY_direction : sbit; sfr; external; Direction of the Ready pin. var CF_RDY_direction : sbit at DIO_P6DIR.B0;
var CF_WE_direction : sbit; sfr; external; Direction of the Write Enable pin. var CF_WE_direction : sbit at DIO_P6DIR.B1;
var CF_OE_direction : sbit; sfr; external; Direction of the Output Enable pin. var CF_OE_direction : sbit at DIO_P6DIR.B2;
var CF_CD1_direction : sbit; sfr; external; Direction of the Chip Detect pin. var CF_CD1_direction : sbit at DIO_P6DIR.B3;
var CF_CE1_direction : sbit; sfr; external; Direction of the Chip Enable pin. var CF_CE1_direction : sbit at DIO_P6DIR.B4;
var CF_A2_direction : sbit; sfr; external; Direction of the Address 2 pin. var CF_A2_direction : sbit at DIO_P6DIR.B5;
var CF_A1_direction : sbit; sfr; external; Direction of the Address 1 pin. var CF_A1_direction : sbit at DIO_P6DIR.B6;
var CF_A0_direction : sbit; sfr; external; Direction of the Address 0 pin. var CF_A0_direction : sbit at DIO_P6DIR.B7;

STM32

The following variables must be defined in all projects using Compact Flash Library: Description : Example :
var Cf_Data_Port_Input : dword; sfr; external; Compact Flash Data Input Port. var Cf_Data_Port_Input : byte at GPIOD_IDR;
var Cf_Data_Port_Output : dword; sfr; external; Compact Flash Data Output Port. var Cf_Data_Port_Output : byte at GPIOD_ODR;
var CF_RDY : sbit; sfr; external; Ready signal line. var CF_RDY : sbit at GPIOD_IDR.B15;
var CF_WE : sbit; sfr; external; Write Enable signal line. var CF_WE : sbit at GPIOD_ODR.B14;
var CF_OE : sbit; sfr; external; Output Enable signal line. var CF_OE : sbit at GPIOD_ODR.B13;
var CF_CD1 : sbit; sfr; external; Chip Detect signal line. var CF_CD1 : sbit at GPIOD_IDR.B12;
var CF_CE1 : sbit; sfr; external; Chip Enable signal line. var CF_CE1 : sbit at GPIOD_ODR.B11;
var CF_A2 : sbit; sfr; external; Address pin 2. var CF_A2 : sbit at GPIOD_ODR.B10;
var CF_A1 : sbit; sfr; external; Address pin 1. var CF_A1 : sbit at GPIOD_ODR.B9;
var CF_A0 : sbit; sfr; external; Address pin 0. var CF_A0 : sbit at GPIOD_ODR.B8;

CEC1x02

The following variables must be defined in all projects using Compact Flash Library: Description : Example :
var Cf_Data_Port_Input : byte; sfr; external; Compact Flash Data Input Port. var Cf_Data_Port_Input : byte at GPIO_INPUT_010_017;
var Cf_Data_Port_Output : byte; sfr; external; Compact Flash Data Output Port. var Cf_Data_Port_Output : byte at GPIO_OUTPUT_010_017;
var CF_RDY : sbit; sfr; external; Ready signal line. var CF_RDY : sbit at GPIO_INPUT_PIN_001_bit;
var CF_WE : sbit; sfr; external; Write Enable signal line. var CF_WE : sbit at GPIO_INPUT_PIN_002_bit;
var CF_OE : sbit; sfr; external; Output Enable signal line. var CF_OE : sbit at GPIO_INPUT_PIN_003_bit;
var CF_CD1 : sbit; sfr; external; Chip Detect signal line. var CF_CD1 : sbit at GPIO_INPUT_PIN_004_bit;
var CF_CE1 : sbit; sfr; external; Chip Enable signal line. var CF_CE1 : sbit at GPIO_INPUT_PIN_005_bit;
var CF_A2 : sbit; sfr; external; Address pin 2. var CF_A2 : sbit at GPIO_INPUT_PIN_006_bit;
var CF_A1 : sbit; sfr; external; Address pin 1. var CF_A1 : sbit at GPIO_INPUT_PIN_007_bit;
var CF_A0 : sbit; sfr; external; Address pin 0. var CF_A0 : sbit at GPIO_INPUT_PIN_147_bit.B8;

Library Routines

Routines for file handling:

The following routine is for the internal use by compiler only:

Cf_Init

Prototype

procedure Cf_Init();

Description

Initializes ports appropriately for communication with CF card.

Parameters

None.

Returns

Nothing.

Requires

External dependencies of the library from the top of the page must be defined before using this function.

Example

Stellaris

// set compact flash pinout
var
  Cf_Data_Port : byte at GPIO_PORTE_DATA;

  CF_RDY : sbit at GPIO_PORTD_DATA7_bit;
  CF_WE  : sbit at GPIO_PORTD_DATA6_bit;
  CF_OE  : sbit at GPIO_PORTD_DATA5_bit;
  CF_CD1 : sbit at GPIO_PORTD_DATA4_bit;
  CF_CE1 : sbit at GPIO_PORTD_DATA3_bit;
  CF_A2  : sbit at GPIO_PORTD_DATA2_bit;
  CF_A1  : sbit at GPIO_PORTD_DATA1_bit;
  CF_A0  : sbit at GPIO_PORTD_DATA0_bit;

  CF_RDY_direction : sbit at GPIO_PORTD_DIR7_bit;
  CF_WE_direction  : sbit at GPIO_PORTD_DIR6_bit;
  CF_OE_direction  : sbit at GPIO_PORTD_DIR5_bit;
  CF_CD1_direction : sbit at GPIO_PORTD_DIR4_bit;
  CF_CE1_direction : sbit at GPIO_PORTD_DIR3_bit;
  CF_A2_direction  : sbit at GPIO_PORTD_DIR2_bit;
  CF_A1_direction  : sbit at GPIO_PORTD_DIR1_bit;
  CF_A0_direction  : sbit at GPIO_PORTD_DIR0_bit;
// end of compact flash pinout
...
Cf_Init();      // initialize CF 

MSP432

// set compact flash pinout
var
  Cf_Data_Port_Output : word at DIO_P5OUT;
  Cf_Data_Port_Input : word at DIO_P5IN;

  CF_RDY : sbit at DIO_P6OUT.B0;
  CF_WE  : sbit at DIO_P6OUT.B1;
  CF_OE  : sbit at DIO_P6OUT.B2;
  CF_CD1 : sbit at DIO_P6OUT.B3;
  CF_CE1 : sbit at DIO_P6OUT.B4;
  CF_A2  : sbit at DIO_P6OUT.B5;
  CF_A1  : sbit at DIO_P6OUT.B6;
  CF_A0  : sbit at DIO_P6OUT.B7;

  CF_RDY_direction : sbit at DIO_P6DIR.B0;
  CF_WE_direction  : sbit at DIO_P6DIR.B1;
  CF_OE_direction  : sbit at DIO_P6DIR.B2;
  CF_CD1_direction : sbit at DIO_P6DIR.B3;
  CF_CE1_direction : sbit at DIO_P6DIR.B4;
  CF_A2_direction  : sbit at DIO_P6DIR.B5;
  CF_A1_direction  : sbit at DIO_P6DIR.B6;
  CF_A0_direction  : sbit at DIO_P6DIR.B7;
// end of compact flash pinout
...
Cf_Init();      // initialize CF 

STM32

// set compact flash pinout
var
  Cf_Data_Port_Input:  dword at GPIOD_IDR;
  Cf_Data_Port_Output: dword at GPIOD_ODR;

  CF_RDY: sbit at GPIOD_IDR.B15;
  CF_WE:  sbit at GPIOD_ODR.B14;
  CF_OE:  sbit at GPIOD_ODR.B13;
  CF_CD1: sbit at GPIOD_IDR.B12;
  CF_CE1: sbit at GPIOD_ODR.B11;
  CF_A2:  sbit at GPIOD_ODR.B10;
  CF_A1:  sbit at GPIOD_ODR.B9;
  CF_A0:  sbit at GPIOD_ODR.B8;
// end of compact flash pinout
...
Cf_Init();      // initialize CF 

CEC1x02

// set compact flash pinout
var
  Cf_Data_Port_Input:  byte at GPIO_OUTPUT_010_017;
  Cf_Data_Port_Output: dword at GPIO_INPUT_010_017;

  CF_RDY: sbit at GPIO_INPUT_PIN_001_bit;
  CF_WE:  sbit at GPIO_OUTPUT_PIN_002_bit;
  CF_OE:  sbit at GPIO_OUTPUT_PIN_003_bit;
  CF_CD1: sbit at GPIO_INPUT_PIN_004_bit;
  CF_CE1: sbit at GPIO_OUTPUT_PIN_005_bit;
  CF_A2:  sbit at GPIO_OUTPUT_PIN_006_bit;
  CF_A1:  sbit at GPIO_OUTPUT_PIN_007_bit;
  CF_A0:  sbit at GPIO_OUTPUT_PIN_147_bit;
// end of compact flash pinout
...
Cf_Init();      // initialize CF 
Notes

None.

Cf_Detect

Prototype

function CF_Detect() : word ;

Description

Checks for presence of CF card by reading the chip detect pin.

Parameters

None.

Returns

  • 1 - if CF card was detected
  • 0 - otherwise

Requires

The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.

Example
// Wait until CF card is inserted:
while (Cf_Detect() = 0) do
  nop;
Notes

ARM family MCU and CF card voltage levels are different. The user must ensure that MCU's pin connected to CD line can read CF card Logical One correctly.

Cf_Enable

Prototype

procedure Cf_Enable();

Description

Enables the device. Routine needs to be called only if you have disabled the device by means of the Cf_Disable routine. These two routines in conjunction allow you to free/occupy data line when working with multiple devices.

Parameters

None.

Returns

Nothing.

Requires

The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.

Example
// enable compact flash
Cf_Enable();
Notes

None.

Cf_Disable

Prototype

procedure Cf_Disable();

Description

Routine disables the device and frees the data lines for other devices. To enable the device again, call Cf_Enable. These two routines in conjunction allow you to free/occupy data line when working with multiple devices.

Parameters

None.

Returns

Nothing.

Requires

The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.

Example
// disable compact flash
Cf_Disable();
Notes

None.

Cf_Read_Init

Prototype

procedure Cf_Read_Init(address : dword; sector_count : byte);

Description

Initializes CF card for reading.

Parameters
  • address: the first sector to be prepared for reading operation.
  • sector_count: number of sectors to be prepared for reading operation.
Returns

Nothing.

Requires

The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.

Example
// initialize compact flash for reading from sector 590
Cf_Read_Init(590, 1);
Notes

None.

Cf_Read_Byte

Prototype

function CF_Read_Byte() : byte;

Description

Reads one byte from Compact Flash sector buffer location currently pointed to by internal read pointers. These pointers will be autoicremented upon reading.

Parameters

None.

Returns

Returns a byte read from Compact Flash sector buffer.

Requires

The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.

CF card must be initialized for reading operation. See Cf_Read_Init.

Example
// Read a byte from compact flash:
var data_ as byte;
...
data_ := Cf_Read_Byte();
Notes Higher byte of the unsigned return value is cleared.

Cf_Write_Init

Prototype

procedure Cf_Write_Init(address : dword; sectcnt : byte);

Description

Initializes CF card for writing.

Parameters
  • address: the first sector to be prepared for writing operation.
  • sectcnt: number of sectors to be prepared for writing operation.
Returns

Nothing.

Requires

The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.

Example
// initialize compact flash for writing to sector 590
Cf_Write_Init(590, 1);
Notes

None.

Cf_Write_Byte

Prototype

procedure Cf_Write_Byte(data_ : byte) ;

Description

Writes a byte to Compact Flash sector buffer location currently pointed to by writing pointers. These pointers will be autoicremented upon reading. When sector buffer is full, its contents will be transfered to appropriate flash memory sector.

Parameters
  • data_: byte to be written.
Returns

Nothing.

Requires

The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.

CF card must be initialized for writing operation. See Cf_Write_Init.

Example
var data_ : byte;
...
data_ := 0xAA;
Cf_Write_Byte(data_);
Notes

None.

Cf_Read_Sector

Prototype

procedure Cf_Read_Sector(sector_number : dword; var buffer : array[512] of byte);

Description

Reads one sector (512 bytes). Read data is stored into buffer provided by the buffer parameter.

Parameters
  • sector_number: sector to be read.
  • buffer: data buffer of at least 512 bytes in length.
Returns

Nothing.

Requires

The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.

Example
// read sector 22
var data_ : array[512] of byte;
...
Cf_Read_Sector(22, data_);
Notes

None.

Cf_Write_Sector

Prototype

procedure Cf_Write_Sector(sector_number : dword; var buffer : array[512] of byte) ;

Description

Writes 512 bytes of data provided by the buffer parameter to one CF sector.

Parameters
  • sector_number: sector to be written to.
  • buffer: data buffer of 512 bytes in length.
Returns

Nothing.

Requires

The corresponding MCU ports must be appropriately initialized for CF card. See Cf_Init.

Example
// write to sector 22
var data_ : array[512] of byte;
...
Cf_Write_Sector(22, data_);
Notes

None.

Cf_Fat_Init

Prototype

function Cf_Fat_Init(): word;

Description

Initializes CF card, reads CF FAT16 boot sector and extracts necessary data needed by the library.

Parameters

None.

Returns
  • 0 - if CF card was detected and successfully initialized
  • 1 - if FAT16 boot sector was not found
  • 255 - if card was not detected
Requires

Nothing.

Example
//  init the FAT library
if (Cf_Fat_Init() = 0) then
  begin
  ...
  end
Notes

None.

Cf_Fat_QuickFormat

Prototype

function Cf_Fat_QuickFormat(var cf_fat_label : string[11]) : word;

Description

Formats to FAT16 and initializes CF card.

Parameters
  • cf_fat_label: volume label (11 characters in length). If less than 11 characters are provided, the label will be padded with spaces. If null string is passed, the volume will not be labeled.
Returns
  • 0 - if CF card was detected, successfully formated and initialized
  • 1 - if FAT16 format was unsuccessful
  • 255 - if card was not detected
Requires

Nothing.

Example
// format and initialize the FAT library
if (Cf_Fat_QuickFormat('mikroE') = 0) then
begin
  ...
end;
Notes
  • This routine can be used instead or in conjunction with Cf_Fat_Init routine.
  • If CF card already contains a valid boot sector, it will remain unchanged (except volume label field) and only FAT and ROOT tables will be erased. Also, the new volume label will be set.

Cf_Fat_Assign

Prototype

function Cf_Fat_Assign(var filename: array[12] of char; file_cre_attr: byte): word;

Description

Assigns file for file operations (read, write, delete...). All subsequent file operations will be applied over the assigned file.

Parameters
  • filename: name of the file that should be assigned for file operations. The file name should be in DOS 8.3 (file_name.extension) format. The file name and extension will be automatically padded with spaces by the library if they have less than length required (i.e. "mikro.tx" -> "mikro .tx "), so the user does not have to take care of that. The file name and extension are case insensitive. The library will convert them to proper case automatically, so the user does not have to take care of that.

    Also, in order to keep backward compatibility with the first version of this library, file names can be entered as UPPERCASE string of 11 bytes in length with no dot character between the file name and extension (i.e. "MIKROELETXT" -> MIKROELE.TXT). In this case the last 3 characters of the string are considered to be file extension.

  • file_cre_attr: file creation and attributes flags. Each bit corresponds to the appropriate file attribute:
  • Bit Mask Description
    0 0x01 Read Only
    1 0x02 Hidden
    2 0x04 System
    3 0x08 Volume Label
    4 0x10 Subdirectory
    5 0x20 Archive
    6 0x40 Device (internal use only, never found on disk)
    7 0x80 File creation flag. If the file does not exist and this flag is set, a new file with specified name will be created.
Returns
  • 0 if file does not exist and no new file is created.
  • 1 if file already exists or file does not exist but a new file is created.
Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

Example
// create file with archive attribut if it does not already exist
Cf_Fat_Assign('MIKRO007.TXT',0xA0);
Notes Long File Names (LFN) are not supported.

Cf_Fat_Reset

Prototype

procedure Cf_Fat_Reset(var size: dword);

Description

Opens currently assigned file for reading.

Parameters
  • size: buffer to store file size to. After file has been open for reading its size is returned through this parameter.
Returns

Nothing.

Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

File must be previously assigned. See Cf_Fat_Assign.

Example
var size : dword;
...
Cf_Fat_Reset(size);
Notes

None.

Cf_Fat_Read

Prototype

procedure Cf_Fat_Read(var bdata: byte);

Description

Reads a byte from currently assigned file opened for reading. Upon function execution file pointers will be set to the next character in the file.

Parameters
  • bdata: buffer to store read byte to. Upon this function execution read byte is returned through this parameter.
Returns

Nothing.

Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

File must be previously assigned. See Cf_Fat_Assign.

File must be open for reading. See Cf_Fat_Reset.

Example
var bdata : byte;
...
Cf_Fat_Read(bdata);
Notes

None.

Cf_Fat_Rewrite

Prototype

procedure Cf_Fat_Rewrite();

Description

Opens currently assigned file for writing. If the file is not empty its content will be erased.

Parameters

None.

Returns

Nothing.

Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

The file must be previously assigned. See Cf_Fat_Assign.

Example
// open file for writing
Cf_Fat_Rewrite();
Notes

None.

Cf_Fat_Append

Prototype

procedure Cf_Fat_Append();

Description

Opens currently assigned file for appending. Upon this function execution file pointers will be positioned after the last byte in the file, so any subsequent file writing operation will start from there.

Parameters

None.

Returns

Nothing.

Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

File must be previously assigned. See Cf_Fat_Assign.

Example
// open file for appending
Cf_Fat_Append();
Notes

None.

Cf_Fat_Delete

Prototype

procedure Cf_Fat_Delete();

Description

Deletes currently assigned file from CF card.

Parameters

None.

Returns

Nothing.

Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

File must be previously assigned. See Cf_Fat_Assign.

Example
// delete current file
Cf_Fat_Delete();
Notes

None.

Cf_Fat_Write

Prototype

procedure Cf_Fat_Write(var fdata: array[512] of byte; data_len: word);

Description

Writes requested number of bytes to currently assigned file opened for writing.

Parameters
  • fdata: data to be written.
  • data_len: number of bytes to be written.
Returns

Nothing.

Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

File must be previously assigned. See Cf_Fat_Assign.

File must be open for writing. See Cf_Fat_Rewrite or Cf_Fat_Append.

Example
var file_contents : array[42] of byte;
...
Cf_Fat_Write(file_contents, 42); // write data to the assigned file
Notes

None.

Cf_Fat_Set_File_Date

Prototype

procedure Cf_Fat_Set_File_Date(year: word; month: byte; day: byte; hours: byte; mins: byte; seconds: byte);

Description

Sets the date/time stamp. Any subsequent file writing operation will write this stamp to currently assigned file's time/date attributes.

Parameters
  • year: year attribute. Valid values: 1980-2107
  • month: month attribute. Valid values: 1-12
  • day: day attribute. Valid values: 1-31
  • hours: hours attribute. Valid values: 0-23
  • mins: minutes attribute. Valid values: 0-59
  • seconds: seconds attribute. Valid values: 0-59
Returns

Nothing.

Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

File must be previously assigned. See Cf_Fat_Assign.

File must be open for writing. See Cf_Fat_Rewrite or Cf_Fat_Append.

Example
Cf_Fat_Set_File_Date(2005,9,30,17,41,0);
Notes

None.

Cf_Fat_Get_File_Date

Prototype

procedure Cf_Fat_Get_File_Date(var year: word; var month: byte; var day: byte; var hours: byte; var mins: byte);

Description

Reads time/date attributes of currently assigned file.

Parameters
  • year: buffer to store year attribute to. Upon function execution year attribute is returned through this parameter.
  • month: buffer to store month attribute to. Upon function execution month attribute is returned through this parameter.
  • day: buffer to store day attribute to. Upon function execution day attribute is returned through this parameter.
  • hours: buffer to store hours attribute to. Upon function execution hours attribute is returned through this parameter.
  • mins: buffer to store minutes attribute to. Upon function execution minutes attribute is returned through this parameter.
Returns

Nothing.

Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

File must be previously assigned. See Cf_Fat_Assign.

Example
var year : word;
    month, day, hours, mins : byte;
...
Cf_Fat_Get_File_Date(year, month, day, hours, mins);
Notes

None.

Cf_Fat_Get_File_Date_Modified

Prototype

procedure Cf_Fat_Get_File_Date_Modified(var year: word; var month: byte; var day: byte; var hours: byte; var mins: byte);

Description

Retrieves the last modification date/time of the currently assigned file.

Parameters
  • year: buffer to store year of modification attribute to. Upon function execution year of modification attribute is returned through this parameter.
  • month: buffer to store month of modification attribute to. Upon function execution month of modification attribute is returned through this parameter.
  • day: buffer to store day of modification attribute to. Upon function execution day of modification attribute is returned through this parameter.
  • hours: buffer to store hours of modification attribute to. Upon function execution hours of modification attribute is returned through this parameter.
  • mins: buffer to store minutes of modification attribute to. Upon function execution minutes of modification attribute is returned through this parameter.
Returns

Nothing.

Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

File must be previously assigned. See Cf_Fat_Assign.

Example
var year : word;
    month, day, hours, mins : byte;
...
Cf_Fat_Get_File_Date_Modified(year, month, day, hours, mins);
Notes

None.

Cf_Fat_Get_File_Size

Prototype

function Cf_Fat_Get_File_Size(): dword;

Description

This function reads size of currently assigned file in bytes.

Parameters

None.

Returns

Size of the currently assigned file in bytes.

Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

File must be previously assigned. See Cf_Fat_Assign.

Example
var my_file_size : dword;
...
my_file_size := Cf_Fat_Get_File_Size();
Notes

None.

Cf_Fat_Get_Swap_File

Prototype

function Cf_Fat_Get_Swap_File(sectors_cnt: dword; var filename : string[11]; file_attr : byte): dword;

Description

This function is used to create a swap file of predefined name and size on the CF media. If a file with specified name already exists on the media, search for consecutive sectors will ignore sectors occupied by this file. Therefore, it is recommended to erase such file if it exists before calling this function. If it is not erased and there is still enough space for a new swap file, this function will delete it after allocating new memory space for a new swap file.

The purpose of the swap file is to make reading and writing to CF media as fast as possible, by using the Cf_Read_Sector() and Cf_Write_Sector() functions directly, without potentially damaging the FAT system. Swap file can be considered as a "window" on the media where the user can freely write/read data. It's main purpose in the this library is to be used for fast data acquisition; when the time-critical acquisition has finished, the data can be re-written into a "normal" file, and formatted in the most suitable way.

Parameters
  • sectors_cnt: number of consecutive sectors that user wants the swap file to have.
  • filename: name of the file that should be assigned for file operations. The file name should be in DOS 8.3 (file_name.extension) format. The file name and extension will be automatically padded with spaces by the library if they have less than length required (i.e. "mikro.tx" -> "mikro .tx "), so the user does not have to take care of that. The file name and extension are case insensitive. The library will convert them to proper case automatically, so the user does not have to take care of that.

    Also, in order to keep backward compatibility with the first version of this library, file names can be entered as UPPERCASE string of 11 bytes in length with no dot character between the file name and extension (i.e. "MIKROELETXT" -> MIKROELE.TXT). In this case the last 3 characters of the string are considered to be file extension.

  • file_attr: file creation and attributes flags. Each bit corresponds to the appropriate file attribute:
  • Bit Mask Description
    0 0x01 Read Only
    1 0x02 Hidden
    2 0x04 System
    3 0x08 Volume Label
    4 0x10 Subdirectory
    5 0x20 Archive
    6 0x40 Device (internal use only, never found on disk)
    7 0x80 Not used

Returns

  • Number of the start sector for the newly created swap file, if there was enough free space on CF card to create file of required size.
  • 0 - otherwise.

Requires

CF card and CF library must be initialized for file operations. See Cf_Fat_Init.

Example
//  Try to create a swap file with archive atribute, whose size will be at least 1000 sectors.
//               If it succeeds, it sends the No. of start sector over UART
var size : dword;
...
size := Cf_Fat_Get_Swap_File(1000, 'mikroE.txt', 0x20);
if (size <> 0) then
begin
  UART1_Write(0xAA);
  UART1_Write(Lo(size));
  UART1_Write(Hi(size));
  UART1_Write(Higher(size));
  UART1_Write(Highest(size));
  UART1_Write(0xAA);
end;
Notes Long File Names (LFN) are not supported.