Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
#include <string.h>
Private define
#define BITMAP_BUFFER_SIZE
#define BITMAP_HEADER_SIZE
#define MIN
Private variables
aBuffer
MyFileInfo
MyDirectory
MyFile
fs
BytesWritten
BytesRead
Storage_OpenReadFile(uint8_t, uint16_t, const char *)
Storage_CopyFile(const char *, const char *)
Storage_CheckBitmapFile(const char *, uint32_t *)
Storage_GetDirectoryBitmapFiles(const char *, char **)
Buffercmp(uint8_t *, uint8_t *, uint16_t)
Files
loading...
CodeScopeSTM32 Libraries and SamplesDemonstrationsSrc/fatfs_storage.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file Demonstrations/Src/fatfs_storage.c * @author MCD Application Team * @brief This file includes the Storage (FatFs) driver ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include <string.h> Includes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define BITMAP_BUFFER_SIZE 1*512 /* You can adapt this size depending on the amount of RAM available */ #define BITMAP_HEADER_SIZE sizeof(BmpHeader) /* Bitmap specificity */ #define MIN(a,b) (((a)<(b))?(a):(b)) Private define/* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ uint8_t aBuffer[BITMAP_HEADER_SIZE + BITMAP_BUFFER_SIZE]; FILINFO MyFileInfo; DIR MyDirectory; FIL MyFile; FATFS fs; UINT BytesWritten, BytesRead; Private variables /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /** * @brief Open a file and display it on lcd * @param DirName: the Directory name to open * @param FileName: the file name to open * @param BufferAddress: A pointer to a buffer to copy the file to * @param FileLen: the File length * @retval err: Error status (0=> success, 1=> fail) *//* ... */ uint32_t Storage_OpenReadFile(uint8_t Xpoz, uint16_t Ypoz, const char *BmpName) { uint32_t size = 0; FIL bmpfile; uint32_t nbline; BmpHeader* pbmpheader = (BmpHeader*)aBuffer; /* Close a bmp file */ f_open(&bmpfile, BmpName, FA_READ); /* Read the constant part of the header from the file and store it at the top of aBuffer*/ f_read(&bmpfile, &aBuffer, BITMAP_HEADER_SIZE, &BytesRead); /* Get the size of the data stored inside the file */ size = pbmpheader->fsize - pbmpheader->offset; /* Start reading at the top of the file */ f_lseek(&bmpfile, 0); /* Read the entire header from the file and store it at the top of aBuffer */ f_read(&bmpfile, &aBuffer, pbmpheader->offset, &BytesRead); /* Compute the number of entire lines which can be stored inside the buffer */ nbline = (BITMAP_BUFFER_SIZE - pbmpheader->offset + BITMAP_HEADER_SIZE)/(pbmpheader->w * 2); /* As long as the entire bitmap file as not been displayed */ do { uint32_t nbbytetoread; /* Get the number of bytes which can be stored inside the buffer */ nbbytetoread = MIN(size,nbline*pbmpheader->w*2); /* Adapt the total size of the bitmap, stored inside the header, to this chunk */ pbmpheader->fsize = pbmpheader->offset + nbbytetoread; /* Adapt the number of line, stored inside the header, to this chunk */ pbmpheader->h = nbbytetoread/(pbmpheader->w*2); /* Start reading at the end of the file */ f_lseek(&bmpfile, pbmpheader->offset + size - nbbytetoread); /* Store this chunk (or the entire part if possible) of the file inside a buffer */ f_read(&bmpfile, aBuffer + pbmpheader->offset, nbbytetoread, &BytesRead); /* Draw the bitmap */ BSP_LCD_DrawBitmap(Xpoz, Ypoz, aBuffer); /* Update the remaining number of bytes to read */ size -= nbbytetoread; /* Change the display position of the next bitmap */ Ypoz += nbline; ...}while (size > 0); /* Close the bmp file */ f_close(&bmpfile); return 0; }{ ... } /** * @brief Copy file BmpName1 to BmpName2 * @param BmpName1: the source file name * @param BmpName2: the destination file name * @retval err: Error status (0=> success, 1=> fail) *//* ... */ uint32_t Storage_CopyFile(const char* BmpName1, const char* BmpName2) { uint32_t index = 0; FIL file1, file2; /* Open an Existent BMP file system */ f_open(&file1, BmpName1, FA_READ); /* Create a new BMP file system */ f_open(&file2, BmpName2, FA_CREATE_ALWAYS | FA_WRITE); do { f_read(&file1, aBuffer, _MAX_SS, &BytesRead); f_write(&file2, aBuffer, _MAX_SS, &BytesWritten); index+= _MAX_SS; ...} while(index < f_size(&file1)); f_close(&file1); f_close(&file2); return 1; }{ ... } /** * @brief Opens a file and copies its content to a buffer. * @param DirName: the Directory name to open * @param FileName: the file name to open * @param BufferAddress: A pointer to a buffer to copy the file to * @param FileLen: File length * @retval err: Error status (0=> success, 1=> fail) *//* ... */ uint32_t Storage_CheckBitmapFile(const char* BmpName, uint32_t *FileLen) { uint32_t err = 0; if(f_open(&MyFile, BmpName, FA_READ) != FR_OK) { err = 1; }if (f_open(&MyFile, BmpName, FA_READ) != FR_OK) { ... } else { if(f_close(&MyFile) != FR_OK) { err = 1; }if (f_close(&MyFile) != FR_OK) { ... } }else { ... } return err; }{ ... } /** * @brief List up to 25 file on the root directory with extension .BMP * @param DirName: Directory name * @param Files: Buffer to contain read files * @retval The number of the found files *//* ... */ uint32_t Storage_GetDirectoryBitmapFiles(const char* DirName, char* Files[]) { FRESULT res; uint32_t index = 0; /* Open filesystem */ if(f_mount(&fs, (TCHAR const*)"",0) != FR_OK) { return 0; }if (f_mount(&fs, (TCHAR const*)"",0) != FR_OK) { ... } /* Start to search for wave files */ res = f_findfirst(&MyDirectory, &MyFileInfo, DirName, "*.bmp"); /* Repeat while an item is found */ while (MyFileInfo.fname[0]) { if(res == FR_OK) { if(index < MAX_BMP_FILES) { sprintf (Files[index++], "%s", MyFileInfo.fname); }if (index < MAX_BMP_FILES) { ... } /* Search for next item */ res = f_findnext(&MyDirectory, &MyFileInfo); }if (res == FR_OK) { ... } else { index = 0; break; }else { ... } }while (MyFileInfo.fname[0]) { ... } f_closedir(&MyDirectory); return index; }{ ... } /** * @brief Compares two buffers. * @param pBuffer1, pBuffer2: buffers to be compared * @param BufferLength: buffer's length * @retval 0: pBuffer1 identical to pBuffer2 * 1: pBuffer1 differs from pBuffer2 *//* ... */ uint8_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength) { uint8_t ret = 1; while (BufferLength--) { if(*pBuffer1 != *pBuffer2) { ret = 0; }if (*pBuffer1 != *pBuffer2) { ... } pBuffer1++; pBuffer2++; }while (BufferLength--) { ... } return ret; }{ ... }