Application_FlashCard.doc

(249 KB) Pobierz
After the development my winter garden controlling system, I wanted collect the climatic data, such as outside and interior temperature, sunshine intensity and rain in order to be able to evaluate it later

Accessing a Compact Flash Card from BASCOM

 

After the development of my glass house (Wintergarten) controlling system, I wanted collect the climatic data, such as outside and interior temperature, sunshine intensity and rain in order to be able to evaluate it later, building some statistisc, diagrams and so on.

First I send the data online to a terminal program on a PC and stored it there in a text-file. But this solution have the disadvantage, that I need always a PC (or Laptop) on the embedded system. Next I thougth about to store the data in a comprimized form in the EEPROM on the ATMega103, but even the 4 kByte EEPROM can hold only the data of few days.

So I searched the Internet for other solution for long time logging on an embedded system.

I found a very interesting article for embedded systems on

http://www.circuitcellar.com/echips-pdfs/0201/c0201mspdf.pdf about to store data in a compact flash card.

With a CompactFlash Card you have a huge amount of memory to a low price.

The Compactflash Memory Card can be used in three mode:

·         Memory Mode

·         I/O Mode

·         IDE Mode

I decided to use it in Memory Mode to keep things simple and use lowest count of pins of the controller to attach the flashcard.

The 17 pins are 8 for data, 3 for register addressing and 6 for controlling.



You can see in the schematic, that the connection is very simple, only one pullup resistor is needed.

I used a ATMEGA103 Test-Board from RIBU Electronic Austria. But it should be no problem to use any other developing system. This boards provides VCC and Gnd on every port, but extra lines for VCC and Gnd will do it as well.

 

I transfered the PIC-Routines from the above mentioned article to AVR and build it modular, so they can be used in a easy way from BASCOM.

 

The routines are:

 

·         ReadSector: Read one or more Sectors (512 Bytes) from the CF-Card to SRAM

·         WriteSector: Write one or more Sectors (512 Bytes) from SRAM to the CF-Card

·         GetDriveIdentity: Read CF-Card Identity Information to SRAM

·         DriveInit: Set Input and Out-Ports in Controller and resets the CF-Card

·         DriveReset: Resets the CF-Card

·         DriveCheck: Checks, if CF-Card is plugged in

 

I wrote the routines in ASM to save code-space and execution time.

This functions are located in FlashCardDrive.Bas. There are the ASM-routines and the BASCOM-Interface routines, which allows to call the ASM-routine from BASCOM as normal Sub of Function.

 

Now the Usage of the Functions/Subs:

 

Sub ReadSector ( SRAMPointer, SectorNumber, SectorCount)

 

SRAMPointer (Type Word): Pointer to memorylocation to which the transfer from the CF-Card is written

SectorNumber (Type Long): Sectornumber on the CF-Card

SectorCount (Type Byte): Count of sectors (each 512 bytes) to read from the CF-Card (hightest value is 127, which can be handled by the routine)

 

Reads 1 to 127 Sectors from the CF-Card and stores it in the SRAM starting at a desired address.

 

Example:

 

$Include "FlashCardDrive.bas"

Dim TransferBuffer(512) as Byte         ' Hold Sector to and from CF-Card
Dim wSRAMPointer as Word                ' Address-Pointer for read and write
Dim lSectorNumber as Long               ' Sector Number
 

DriveInit                            ' Set pins to CF-Card and reset card, only needed at start


' give Address of first Byte of the 512 Byte Buffer to Word-Variable
wSRAMPointer = VarPtr(TransferBuffer(1))

' Set Sectornumber, sector 32 normally holds the Boot record sector of first

' partition
lSectorNumber = 32

' Now read in sector 32 (1 Sector) from CF-Card
ReadSector wSRAMPointer , lSectorNumber , 1
' Now Sector number 32 is in Byte-Array TransferBuffer
 

 

 

Sub WriteSector ( SRAMPointer, SectorNumber, SectorCount)

 

SRAMPointer (Type Word): Pointer to memorylocation from which the transfer to the CF-Card is written

SectorNumber (Type Long): Sectornumber on the CF-Card

SectorCount (Type Byte): Count of sectors (at 512 bytes) to read from the CF-Card (Hightest value is 127, which can be handled by the routine)

 

Writes 1 to 127 Sectors to the CF-Card from the SRAM starting at a desired address.

 

Example:

 

$Include "FlashCardDrive.bas"

Dim TransferBuffer(512) as Byte         ' Hold Sector to and from CF-Card
Dim wSRAMPointer as Word                ' Address-Pointer for read and write
Dim lSectorNumber as Long               ' Sector Number
 

DriveInit                            ' Set pins to CF-Card and reset card, only neede ar start


' give Address of first Byte of the 512 Byte Buffer to Word-Variable
wSRAMPointer = VarPtr(TransferBuffer(1))

' Set Sectornumber to 2

lSectorNumber = 2

' Now write Content of Byte-Array TransferBuffer to CF-Card at Sector 2
ReadSector wSRAMPointer , lSectorNumber , 1
 

 

 

Sub GetDriveIdentity (SRAMPointer)

 

SRAMPointer (Type Word): Pointer to memorylocation to which the transfer from the CF-Card is written

Store the special Card Information (512 Bytes) into SRAM starting at a desired address. Check Compact-FlashCard Specification for meaning of this information.

 

 

Example:

 

$Include "FlashCardDrive.bas"

Dim TransferBuffer(512) as Byte         ' Hold Sector to and from CF-Card
Dim wSRAMPointer as Word                ' Address-Pointer for read and write
Dim lSectorNumber as Long               ' Sector Number
 

DriveInit                            ' Set pins to CF-Card and reset card, only needed at start


' give Address of first Byte of the 512 Byte Buffer to Word-Variable
wSRAMPointer = VarPtr(TransferBuffer(1))

' Now read in Card Identity Information
GetCardIdentity wSRAMPointer
' Now 512 Byte of Card Identity Inforation is in Byte-Array TransferBuffer
 

 

 

Sub DriveInit

 

Inits the Input and Output – Ports of the Controller and resets the CF-Card

 

 

 

Sub DriveReset

 

Resets the CF-Card (Hardware-Reset)

 

 

Function DriveCheck() as Byte

 

Checks whether the CF-Card is plugged in (Pin CD1 is Low). It return 1, it CD1 is Low, otherwise it return 0

 

$Include "FlashCardDrive.bas"

if DriveCheck() = 1 then
   DriveInit
else
    print "Card not inserted, check Card!"
end if

 

A compact flash provide a huge array of 512-Byte Sectors. With the above mentioned routines reading and writing of every sectors is possible. A small card with 16 MB have for example approxemitely 31.250 Sectors.

I you want to use the previous described functions in a application, you have to $Include "FlashCardDrive.bas"

 

 

Testing enviroment

 



To test the Sub/Functions and get first experiences with the Compact-Flash Card i wrote an additional tiny interpreter, so the user can read and write sectors of the Card with short comands in a terminal program on a PC connected to the controller via a RS232. Preparing a test-sector in SRAM is also possible.

 

The Commands are:

 

CFI

[Compact Flash Identity]: Reads the Card Identity Information and dumps it to the terminal. It shows also the available Number of Sectors on the Card and the Memory in Bytes

 

CFR

[Compact Flash Reset]: Resets the Compactflash-Card

 

MBR

[Master Boot Record]: Reads the first sector (sector 0) of the Card (Master Boot Record) and dumps it to the terminal. It also reads the partition table located in this sector and shows installed partitions with first sector, last sector and number of sectors in a partition and the code for the installed file system.

 

SD <SectorNumber>

[Sector Dump]: Read <Sectornumber> from Compactflash Card and dumps it to Terminal

 

SD <SectorNumberStart> <SectorNumberEnd>

[Sector Dump]: Dumps Sectors from <SectornumberStart> to <SectorNumbersEnd> to Terminal

 

MD [<SRAMStart>] [<SRAMEnd>]

[Memory Dump]: Dumps the SRAM Buffer to the Terminal. In the first 512 Bytes of the defined SRAM buffer(with 1024 Bytes to work with 2 sectos too) for the interpreter all readings from the CF are stored. With parameter <SRAMStart> every desired SRAM area can be dumped. With <SRAMEnd> it dumps to this address, otherwise it dumps 512 Bytes (1 Sector). The address shown at the beginning of the line is always relatively to the starting address.

 

SW <SectorNumberStart> <SectorCount> [<SRAMPointer>]

[Sector Write]:Write <SectorCount> Sectors from SRAM to the Compactflash Card starting at <SectorNumberStart>. Without parameter <SRAMPointer> the defined SRAM-TransferBuffer is used, with the parameter <SRAMPointer> you can write from any SRAM memory-location.

 

MB <Byte1> [<Byte2>] [<Byte2>] ...... [<Byte8>]

[Memory Byte]: Write single Bytes to the Transfer Buffer. For <Byte> the ASCII – Code must be typed. For exampe 65 or $41 for Letter ‚A‘. Writing to Transfer Buffer starts at address of MemroyPointer. Up to 8 Bytes are possible with one MB command.

 

MF <Byte> [<BufferStart>] [<BufferEnd>]

[Memory Byte]: Fill the Transfer Buffer with <Byte> from <BufferStart> to <BufferEnd>. For <Byte> the ASCII – Code must be typed. For exampe 65 or $41 for Letter ‚A‘. Without <BufferStart> and <BufferEnd> the buffer from the MemoryPointer in the Transfer Buffer to End of the Buffer is filled. You can start also filling at <BufferStart> and end at <BufferEnd>.

 

MT <Text>

[Memory Text]: Fill the SRAM-Transfer Buffer with <Text> starting at the position of the memorypointer.

 

MP <MemoryPointer>

[Memory Pointer]: Adjust the SRAM-Transfer Buffer Pointer for next writing with MB, MT or MF

The actual Memory-Pointer is shown with the Prompt.

 

All values can written in decimal or hex. If hex is used the value must be preceeded by a $-sign like $3B.

...

Zgłoś jeśli naruszono regulamin