2 ******************************************************************************
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Thorsten Klose (tk@midibox.org)
7 * @brief Sets up basic system hardware, functions are called from Main.
8 * @see The GNU Public License (GPL) Version 3
9 * @defgroup PIOS_SDCARD SDCard Functions
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 /* Project Includes */
33 #if defined(PIOS_INCLUDE_SDCARD)
37 * Initialises SPI pins and peripheral to access MMC/SD Card
38 * \param[in] mode currently only mode 0 supported
39 * \return < 0 if initialisation failed
41 int32_t PIOS_SDCARD_Init(uint32_t spi_id
)
50 * \return < 0 if initialisation sequence failed
52 int32_t PIOS_SDCARD_PowerOn(void)
54 return 0; /* Status should be 0 if nothing went wrong! */
59 * Disconnects from SD Card
60 * \return < 0 on errors
62 int32_t PIOS_SDCARD_PowerOff(void)
69 * If SD card was previously available: Checks if the SD Card is still
70 * available by sending the STATUS command.<BR>
71 * This takes ca. 10 uS
73 * If SD card was previously not available: Checks if the SD Card is
74 * available by sending the IDLE command at low speed.<BR>
75 * This takes ca. 500 uS!<BR>
76 * Once we got a positive response, SDCARD_PowerOn() will be
77 * called by this function to initialize the card completely.
79 * Example for Connection/Disconnection detection:
81 * // this function is called each second from a low-priority task
82 * // If multiple tasks are accessing the SD card, add a semaphore/mutex
83 * // to avoid IO access collisions with other tasks!
84 * u8 sdcard_available;
85 * int32_t CheckSDCard(void)
87 * // check if SD card is available
88 * // High speed access if the card was previously available
89 * u8 prev_sdcard_available = sdcard_available;
90 * sdcard_available = PIOS_SDCARD_CheckAvailable(prev_sdcard_available);
92 * if(sdcard_available && !prev_sdcard_available) {
93 * // SD Card has been connected
95 * // now it's possible to read/write sectors
97 * } else if( !sdcard_available && prev_sdcard_available ) {
98 * // SD Card has been disconnected
100 * // here you can notify your application about this state
103 * return 0; // no error
106 * \param[in] was_available should only be set if the SD card was previously available
107 * \return 0 if no response from SD Card
108 * \return 1 if SD card is accessible
110 int32_t PIOS_SDCARD_CheckAvailable(uint8_t was_available
)
112 return 1; /* 1 = available, 0 = not available. */
117 * Sends command to SD card
118 * \param[in] cmd SD card command
119 * \param[in] addr 32bit address
120 * \param[in] crc precalculated CRC
121 * \return >= 0x00 if command has been sent successfully (contains received byte)
122 * \return -1 if no response from SD Card (timeout)
124 int32_t PIOS_SDCARD_SendSDCCmd(uint8_t cmd
, uint32_t addr
, uint8_t crc
)
131 * Reads 512 bytes from selected sector
132 * \param[in] sector 32bit sector
133 * \param[in] *buffer pointer to 512 byte buffer
134 * \return 0 if whole sector has been successfully read
135 * \return -error if error occured during read operation:<BR>
137 * <LI>Bit 0 - In idle state if 1
138 * <LI>Bit 1 - Erase Reset if 1
139 * <LI>Bit 2 - Illgal Command if 1
140 * <LI>Bit 3 - Com CRC Error if 1
141 * <LI>Bit 4 - Erase Sequence Error if 1
142 * <LI>Bit 5 - Address Error if 1
143 * <LI>Bit 6 - Parameter Error if 1
144 * <LI>Bit 7 - Not used, always '0'
146 * \return -256 if timeout during command has been sent
147 * \return -257 if timeout while waiting for start token
149 int32_t PIOS_SDCARD_SectorRead(uint32_t sector
, uint8_t *buffer
)
156 * Writes 512 bytes into selected sector
157 * \param[in] sector 32bit sector
158 * \param[in] *buffer pointer to 512 byte buffer
159 * \return 0 if whole sector has been successfully read
160 * \return -error if error occured during read operation:<BR>
162 * <LI>Bit 0 - In idle state if 1
163 * <LI>Bit 1 - Erase Reset if 1
164 * <LI>Bit 2 - Illgal Command if 1
165 * <LI>Bit 3 - Com CRC Error if 1
166 * <LI>Bit 4 - Erase Sequence Error if 1
167 * <LI>Bit 5 - Address Error if 1
168 * <LI>Bit 6 - Parameter Error if 1
169 * <LI>Bit 7 - Not used, always '0'
171 * \return -256 if timeout during command has been sent
172 * \return -257 if write operation not accepted
173 * \return -258 if timeout during write operation
175 int32_t PIOS_SDCARD_SectorWrite(uint32_t sector
, uint8_t *buffer
)
182 * Reads the CID informations from SD Card
183 * \param[in] *cid pointer to buffer which holds the CID informations
184 * \return 0 if the informations haven been successfully read
185 * \return -error if error occured during read operation
186 * \return -256 if timeout during command has been sent
187 * \return -257 if timeout while waiting for start token
189 int32_t PIOS_SDCARD_CIDRead(SDCARDCidTypeDef
*cid
)
195 * Reads the CSD informations from SD Card
196 * \param[in] *csd pointer to buffer which holds the CSD informations
197 * \return 0 if the informations haven been successfully read
198 * \return -error if error occured during read operation
199 * \return -256 if timeout during command has been sent
200 * \return -257 if timeout while waiting for start token
202 int32_t PIOS_SDCARD_CSDRead(SDCARDCsdTypeDef
*csd
)
208 * Attempts to write a startup log to the SDCard
210 * return -1 Error deleting file
211 * return -2 Error opening file
212 * return -3 Error writing file
214 int32_t PIOS_SDCARD_StartupLog(void)
220 * Check if the SD card has been mounted
224 int32_t PIOS_SDCARD_IsMounted()
230 * Mounts the file system
231 * param[in] CreateStartupLog 1 = True, 0 = False
233 * return -1 SDCard not available
234 * return -2 Cannot find first partition
235 * return -3 No volume information
236 * return -4 Error writing startup log file
238 int32_t PIOS_SDCARD_MountFS(uint32_t CreateStartupLog
)
244 * Mounts the file system
245 * return Amount of free bytes
247 int32_t PIOS_SDCARD_GetFree(void)
255 * return -1 DFS_ReadFile failed
256 * return -2 Less bytes read than expected
258 // int32_t PIOS_SDCARD_ReadBuffer(PFILEINFO fileinfo, uint8_t *buffer, uint32_t len)
265 * Read a line from file
266 * returns Number of bytes read
268 // int32_t PIOS_SDCARD_ReadLine(PFILEINFO fileinfo, uint8_t *buffer, uint32_t max_len)
275 * WARNING: This will overwrite the destination file even if it exists
276 * param[in] *Source Path to file to copy
277 * param[in] *Destination Path to destination file
279 * return -1 Source file doesn't exist
280 * return -2 Failed to create destination file
281 * return -3 DFS_ReadFile failed
282 * return -4 DFS_WriteFile failed
284 int32_t PIOS_SDCARD_FileCopy(char *Source
, char *Destination
)
291 * param[in] *Filename File to delete
293 * return -1 Error deleting file
295 int32_t PIOS_SDCARD_FileDelete(char *Filename
)
300 #endif /* if defined(PIOS_INCLUDE_SDCARD) */