Phoenard
PHNSDMinimal.h
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 This file is part of the Phoenard Arduino library
5 Copyright (c) 2014 Phoenard
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25 
51 #ifndef SDMINIMAL_H_
52 #define SDMINIMAL_H_
53 
54 #include <string.h>
55 #include <avr/pgmspace.h>
56 #include "PHNSDMinimal_fat.h"
57 #include "PHNCore.h"
58 
59 #define SDMIN_FILE_READ 0
60 #define SDMIN_FILE_WRITE (FILE_CREATE | FILE_WIPE)
61 #define SDMIN_FILE_CREATE 1
62 #define SDMIN_FILE_WIPE 2
63 
64 /* SCK Speed defines */
65 /* Speed number, 0 is full speed, 6 is slowest speed */
66 #define SCK_SPEED 1
67 
68 /* ============================================================================== */
69 
71 typedef struct {
72  uint32_t block;
73  uint8_t index;
74 } FilePtr;
75 
77 typedef struct {
78  uint8_t crc;
79  uint32_t arg;
80  uint8_t cmd;
81  uint32_t flush;
82 } CardCommand;
83 
85 typedef struct {
86  uint8_t isInitialized; /* Indicates whether the volume is successfully initialized. Reset when errors occur */
87  uint8_t isMultiFat; /* More than one FAT on the volume */
88  uint8_t isfat16; /* volume type is FAT16, FAT32 otherwise */
89  uint8_t blocksPerCluster; /* cluster size in blocks */
90  uint32_t blocksPerFat; /* FAT size in blocks */
91  uint32_t clusterLast; /* the index of the first cluster that is out of range */
92  uint32_t rootCluster; /* First cluster of a FAT32 root directory, for FAT16: root offset */
93  uint32_t rootSize; /* Total size of a FAT16 root directory */
94  uint32_t dataStartBlock; /* first data block number */
95  uint32_t fatStartBlock; /* start block for first FAT */
96 } CardVolume;
97 
98 /* ============================================================================== */
99 
100 extern uint8_t card_notSDHCBlockShift; /* Card is SD1 or SD2, and NOT SDHC. In that case this value is 9, 0 otherwise */
101 
102 extern union SDMINFAT::cache_t volume_cacheBuffer; /* 512 byte cache for device blocks */
103 extern uint32_t volume_cacheBlockNumber; /* Logical number of block in the cache */
104 extern uint8_t volume_cacheDirty; /* readCache() will write current block first if true */
105 extern uint8_t volume_cacheFATMirror; /* current block in cache is a mirrored FAT block */
106 
107 extern CardVolume volume; /* stores all current volume information */
108 
109 extern uint8_t file_isroot16dir; /* file is a FAT16 root directory */
110 extern uint32_t file_curCluster; /* cluster for current file position */
111 extern uint32_t file_position; /* current file position in bytes from beginning */
112 extern FilePtr file_curDir; /* directory currently selected */
113 extern uint32_t file_size; /* total size of the currently opened file */
114 
115 /* ============================================================================== */
116 
118 uint8_t card_command(uint8_t cmd, uint32_t arg, uint8_t crc);
120 uint8_t card_waitForData(uint8_t data_state);
122 void card_setEnabled(uint8_t enabled);
123 
125 void volume_writeCache(void);
127 void volume_writeCache(uint32_t block);
129 inline uint32_t volume_firstClusterBlock(uint32_t cluster) {
130  return volume.dataStartBlock + ((cluster - 2) * volume.blocksPerCluster);
131 }
133 uint8_t* volume_cacheCurrentBlock(uint8_t writeCluster);
135 void volume_readCache(uint32_t blockNumber);
137 uint8_t volume_fatGet(uint32_t cluster, uint32_t* value);
139 void volume_fatPut(uint32_t cluster, uint32_t value);
141 uint8_t volume_init(uint8_t resetPosition = 1);
142 
151 uint8_t file_open(const char* filename, const char* ext, uint8_t mode);
153 void file_truncate();
155 void file_flush(void);
159 void file_read_hex_line(char* buff);
161 void file_append_hex_line(char* buff, uint8_t len, uint16_t address, unsigned char recordType);
163 char* file_read(uint16_t nByteIncrement);
165 void file_write(const char* data, uint16_t nBytes);
167 char file_read_byte(void);
169 void file_write_byte(char b);
171 void file_append_byte(char b);
172 
174 void file_delete(void);
176 void file_save(char filename[8]);
177 
178 /* ============================================================================== */
179 
180 #endif /* SDMINIMAL_H_ */
void file_save(char filename[8])
Saves the currently opened file under a new name, extension is preserved.
Definition: PHNSDMinimal.cpp:313
void file_write_byte(char b)
Writes a single byte.
Definition: PHNSDMinimal.cpp:724
void volume_readCache(uint32_t blockNumber)
Reads in the cache at the block specified.
Definition: PHNSDMinimal.cpp:151
uint8_t volume_fatGet(uint32_t cluster, uint32_t *value)
Gets the next cluster in the FAT cluster chain.
Definition: PHNSDMinimal.cpp:258
void file_read_hex_line(char *buff)
Reads a single full HEX line (intel HEX format)
Definition: PHNSDMinimal.cpp:758
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.
Definition: PHNSDMinimal.cpp:712
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)
Definition: PHNSDMinimal.cpp:793
FAT short directory entry.
Definition: PHNSDMinimal_fat.h:362
void file_delete(void)
Deletes the currently opened file.
Definition: PHNSDMinimal.cpp:326
Definition: PHNSDMinimal_fat.h:430
uint8_t * volume_cacheCurrentBlock(uint8_t writeCluster)
Caches the current block to be read/written.
Definition: PHNSDMinimal.cpp:618
uint8_t card_command(uint8_t cmd, uint32_t arg, uint8_t crc)
Writes out a command to the card.
Definition: PHNSDMinimal.cpp:63
void file_append_byte(char b)
Writes a single byte at the end of the file.
Definition: PHNSDMinimal.cpp:748
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.
Definition: PHNSDMinimal.cpp:703
void volume_fatPut(uint32_t cluster, uint32_t value)
Puts the next cluster in the FAT cluster chain.
Definition: PHNSDMinimal.cpp:275
uint8_t card_waitForData(uint8_t data_state)
Waits to receive a given token from the card.
Definition: PHNSDMinimal.cpp:98
uint32_t volume_firstClusterBlock(uint32_t cluster)
Gets the first block of a cluster.
Definition: PHNSDMinimal.h:129
Stores all information about a loaded volume.
Definition: PHNSDMinimal.h:85
Pinout information for the Phoenard hardware components.
uint8_t volume_init(uint8_t resetPosition=1)
Initializes the volume.
Definition: PHNSDMinimal.cpp:294
void volume_writeCache(void)
Writes out the current cached block.
Definition: PHNSDMinimal.cpp:182
uint8_t file_open(const char *filename, const char *ext, uint8_t mode)
Initializes the card, then attempts to find and open the file.
Definition: PHNSDMinimal.cpp:350
Stores all arguments to complete a full card command, order matters!
Definition: PHNSDMinimal.h:77
Stores the block and index of a file dir entry.
Definition: PHNSDMinimal.h:71
void card_setEnabled(uint8_t enabled)
Turns the card chip-select on or off.
Definition: PHNSDMinimal.cpp:107
SDMINFAT::dir_t * file_readCacheDir(void)
Reads the current directory entry information into the cache.
Definition: PHNSDMinimal.cpp:125
void file_truncate()
Deletes all contents of a file (must call after opening file)
Definition: PHNSDMinimal.cpp:598
char file_read_byte(void)
Reads a single byte.
Definition: PHNSDMinimal.cpp:738
void file_flush(void)
Flushes any pending block writes to the card.
Definition: PHNSDMinimal.cpp:340