Phoenard
Classes | Macros | Functions | Variables
PHNSDMinimal.h File Reference

A minimal Micro-SD library implementation that focuses on minimal code size. More...

#include <string.h>
#include <avr/pgmspace.h>
#include "PHNSDMinimal_fat.h"
#include "PHNCore.h"

Go to the source code of this file.

Classes

struct  FilePtr
 Stores the block and index of a file dir entry. More...
 
struct  CardCommand
 Stores all arguments to complete a full card command, order matters! More...
 
struct  CardVolume
 Stores all information about a loaded volume. More...
 

Macros

#define SDMIN_FILE_READ   0
 
#define SDMIN_FILE_WRITE   (FILE_CREATE | FILE_WIPE)
 
#define SDMIN_FILE_CREATE   1
 
#define SDMIN_FILE_WIPE   2
 
#define SCK_SPEED   1
 

Functions

uint8_t card_command (uint8_t cmd, uint32_t arg, uint8_t crc)
 Writes out a command to the card.
 
uint8_t card_waitForData (uint8_t data_state)
 Waits to receive a given token from the card.
 
void card_setEnabled (uint8_t enabled)
 Turns the card chip-select on or off.
 
void volume_writeCache (void)
 Writes out the current cached block.
 
void volume_writeCache (uint32_t block)
 Writes out the current cached block to the block specified.
 
uint32_t volume_firstClusterBlock (uint32_t cluster)
 Gets the first block of a cluster.
 
uint8_t * volume_cacheCurrentBlock (uint8_t writeCluster)
 Caches the current block to be read/written.
 
void volume_readCache (uint32_t blockNumber)
 Reads in the cache at the block specified.
 
uint8_t volume_fatGet (uint32_t cluster, uint32_t *value)
 Gets the next cluster in the FAT cluster chain.
 
void volume_fatPut (uint32_t cluster, uint32_t value)
 Puts the next cluster in the FAT cluster chain.
 
uint8_t volume_init (uint8_t resetPosition=1)
 Initializes the volume.
 
uint8_t file_open (const char *filename, const char *ext, uint8_t mode)
 Initializes the card, then attempts to find and open the file. More...
 
void file_truncate ()
 Deletes all contents of a file (must call after opening file)
 
void file_flush (void)
 Flushes any pending block writes to the card. More...
 
SDMINFAT::dir_tfile_readCacheDir (void)
 Reads the current directory entry information into the cache.
 
void file_read_hex_line (char *buff)
 Reads a single full HEX line (intel HEX format)
 
void file_append_hex_line (char *buff, uint8_t len, uint16_t address, unsigned char recordType)
 Writes a single full HEX line (intel HEX format)
 
char * file_read (uint16_t nByteIncrement)
 Reads a next block of data, size being fixed increments of 1/2/4/8/16/32/64/128/256/512.
 
void file_write (const char *data, uint16_t nBytes)
 Writes a new block of data, size being fixed increments of 1/2/4/8/16/32/64/128/256/512.
 
char file_read_byte (void)
 Reads a single byte.
 
void file_write_byte (char b)
 Writes a single byte.
 
void file_append_byte (char b)
 Writes a single byte at the end of the file.
 
void file_delete (void)
 Deletes the currently opened file.
 
void file_save (char filename[8])
 Saves the currently opened file under a new name, extension is preserved. More...
 

Variables

uint8_t card_notSDHCBlockShift
 
union SDMINFAT::cache_t volume_cacheBuffer
 
uint32_t volume_cacheBlockNumber
 
uint8_t volume_cacheDirty
 
uint8_t volume_cacheFATMirror
 
CardVolume volume
 
uint8_t file_isroot16dir
 
uint32_t file_curCluster
 
uint32_t file_position
 
FilePtr file_curDir
 
uint32_t file_size
 

Detailed Description

A minimal Micro-SD library implementation that focuses on minimal code size.

Also used by the bootloader, this minimalistic library has the least of the least. It is intended to be used for just reading files, writing files and listing files. File names are all in the 8.3 format and no fancy checks are performed. If something goes wrong, the library is locked down to prevent damages - simple as that.

If you want to do complex things like traversing directories - it is possible - but at a cost. There will not be a fancy class like the SD File to do it, and you will need to store the pointers in memory yourself.

That said, this minimalistic library is truly intended for unique applications. For one, the sketch list has to be minimal size to boost the loading times, so it uses the minimal SD library instead.

If you wish to dive deep into the FAT16 and FAT32 file systems, then this is your chance. You are not prohibited to cross the lines...but be aware that mistakes will corrupt your micro-sd card requiring it to be re-formatted. Please don't 'play' around with this library on a card containing information you don't want to lose.

Remember: it takes a single incorrect block write to permanently corrupt an entire directory.

Function Documentation

void file_flush ( void  )

Flushes any pending block writes to the card.

The flush() call causes all modified data and directory fields to be written to the storage device.

uint8_t file_open ( const char *  filename,
const char *  ext,
uint8_t  mode 
)

Initializes the card, then attempts to find and open the file.

The filename must be 8 characters long, padded with spaces at the end if needed. The extension must be 3 characters long, padded with spaces at the end if needed. The mode specified the way to open the file, where SDMIN_FILE_READ, SDMIN_FILE_WRITE and SDMIN_FILE_CREATE are possible

void file_save ( char  filename[8])

Saves the currently opened file under a new name, extension is preserved.

The flush() call causes all modified data and directory fields to be written to the storage device. With save, the file name to write to can be specified as well