====== SD-card ====== //Necessary knowledge: [HW] [[en:hardware:homelab:controller]]// This section of library contains functions for using HomeLab SD-card. ===== Data types ===== * **//static FATFS FATFS_Obj//** - FATFS_Obj is FATFS data structure instance. Consists of drive identificators. * **//FIL fil_obj//** - File system data structure where to store file identificators. * **//DSTATUS//** - Return codes for drive related functions. * //RES_OK// - (0) Procedure successful. * //RES_ERROR// - (1) R/W error. * //RES_WRPRT// - (2) Write protected. * //RES_NOTRDY// - (3) Drive not ready. * //RES_PARERR// - (4) Some parameters are false. * **//FRESULT//** - Most important codes that are returned from file related functions. * //FR_OK// - (0) Procedure successful. * //FR_DISK_ERR// - (1) Drive I/O layer error * //FR_INT_ERR// - (2) General drive error * //FR_NOT_READY// - (3) Drive not ready for procedure. * //FR_NO_FILE// - (4) File not found. * //FR_NO_PATH// - (5) Address not found. * //FR_INVALID_NAME// - (6) Wrong file name format. * //FR_DENIED// - (7) Drive is full. * //FR_EXIST// - (8) Access is denied. * //FR_INVALID_OBJECT// - (9) File or folder object error. * //FR_WRITE_PROTECTED// - (10) Drive is write protected. * //FR_INVALID_DRIVE// - (11) Drive number is false. * //FR_NO_FILESYSTEM// - (13) Supported FAT file system not found. * //FR_TIMEOUT// - (15) Connection with drive failed. * //FR_INVALID_PARAMETER// - (19) Some parameters are false. * **//ModeFlags//** - Possible modes to open files. * //FA_READ// - File open in read mode. * //FA_WRITE// - File open in write mode. * //FA_OPEN_EXISTING// - Opens file. If it does not exists then returns error code. * //FA_OPEN_ALWAYS// - Opens file. If it does not exists then creates it. * //FA_CREATE_NEW// - Creates new file. If files is already present, error code will be returned. * //FA_CREATE_ALWAYS// - Creates new file. If files is already present then overwrites it. ===== Functions ===== * **//DSTATUS disk_initialize (BYTE drv)//** \\ Initializes drive. Parameters: * //drv// - Drive number. In most cases 0. * Returns DSTATUS code. * **//DSTATUS disk_status (BYTE drv)//** \\ Displays drive status. Parameters: * //drv// - Drive number. In most cases 0. * Returns DSTATUS code. * **//FRESULT f_mount (BYTE Drive, FATFS* FileSystemObject)//** \\ Initializes file system. Parameters: * //Drive// - Drive number. In most cases 0. * //FileSystemObject// - File system object pointer. * Returns FRESULT code. * **//FRESULT f_open (FIL* FileObject, const TCHAR* FileName, BYTE ModeFlags)//** \\ Opens or creates file. Parameters: * //FileObject// - File system object pointer. * //FileName// - File name. * //ModeFlags// - Open mode flag. * Returns FRESULT code. * **//FRESULT f_close (FIL* FileObject)//** \\ Closes file. Parameters: * //FileObject// - File system object pointer. * Returns FRESULT code. * **//FRESULT f_read (FIL* FileObject, void* Buffer, UINT ByteToRead, UINT* ByteRead)//** \\ Reads required amount of bytes from file. Parameters: * //FileObject// - File object pointer. * //Buffer// - Buffer pointer where to store data. * //ByteToRead// - Number of files to be read. * //ByteRead// - Variable pointer where read bytes are returned. * Returns FRESULT code. * **//FRESULT f_write (FIL* FileObject, const void* Buffer, UINT ByteToWrite, UINT* ByteWritten)//** \\ Writes required amount of bytes to file. Parameters: * //FileObject// - Point to file object. * //Buffer// - Point to data being written into file. * //ByteToWrite// - Number of bytes to be written. * //ByteWritten// - Point to variable where written bytes are returned. * Returns FRESULT code. * **//FRESULT f_sync (FIL* FileObject)//** \\ Writes data from buffers to a file to save them from power loss. Used in applications where a file is open for a long time. Parameters: * //FileObject// - Point to file object. * Returns FRESULT code. * **//FRESULT f_mkdir (const TCHAR* DirName)//** \\ Creates folder. Parameters: * //DirName// - Folder name. * Returns FRESULT code. * **//FRESULT f_unlink (const TCHAR* FileName)//** \\ Deletes file or folder. Parameters: * //FileName// - Object name. * Returns FRESULT code. * **//FRESULT f_rename (const TCHAR* OldName, const TCHAR* NewName)//** \\ Rename file or folder. Parameters: * //OldName// - Object old name. * //NewName// - Object new name. * Returns FRESULT code. * **//TCHAR* f_gets (TCHAR* Str, int Size, FIL* FileObject)//** \\ Reads string with given length from file. Parameters: * //Str// - Buffer where to save read data. * //Size// - Length of string. * //FileObject// - Point to file object. * Returns Str buffer if successful. * **//int f_putc (TCHAR Chr, FIL* FileObject)//** \\ Adds a char to file. Parameters: * //Chr// - Char to be added into file. * //FileObject// - File object pointer . * Returns 1 if successful. * **//int f_printf (FIL* FileObject, const TCHAR* Foramt, ...)//** \\ Adds a formated string to file. Parameters: * //FileObject// - File object pointer. * //Foramt// - String format. * ... * Returns the number of written chars if successful. * **//DWORD f_tell (FIL* FileObject)//** \\ Cursor position in file. Parameters: * //FileObject// - File object pointer. * Returns read/write cursor position in file as a pointer. * **//FRESULT f_lseek (FIL* FileObject,DWORD Offset)//** \\ Move cursor to desired position in file. Parameters: * //FileObject// - File object pointer. * //Offset// - Desired position in bytes from the beginning of the file. * Returns FRESULT code. ===== Example ===== #include #include #include int main (void) { static FATFS FATFS_Obj; FIL fil_obj; // SD card initialization. disk_initialize(0); // File system initialization. f_mount(0, &FATFS_Obj); // Create text file "file.txt". f_open(&fil_obj, "/file.txt", FA_CREATE_NEW); // File to be open for writing. f_open(&fil_obj, "/file.txt", FA_WRITE); // Writing into file. f_printf(&fil_obj, "SD Card test"); // Close file. f_close(&fil_obj); }