1515#define FILENAME_SIZE (SFN_MAX + SFN_SUFFIX_LEN + 1)
1616#define BUF_MAX FF_MIN_SS
1717#define SDCARD_DRIVE "0:"
18+ #define CONFIG_FILENAME "PSLAB.CFG"
1819
20+ static bool s_mounted = false;
1921static TCHAR s_sector_buf [BUF_MAX ];
2022_Static_assert (
2123 sizeof (s_sector_buf ) == 512 ,
@@ -141,3 +143,51 @@ response_t SDCARD_get_file_info(void)
141143
142144 return SUCCESS ;
143145}
146+
147+ /**
148+ * @brief Read the configuration file from the SD card.
149+ * @return true if the config file was read successfully, false otherwise or if the config structure is invalid.
150+ */
151+ static bool read_config_file (void ) {
152+ FIL file ;
153+ if (f_open (& file , CONFIG_FILENAME , FA_READ ) != FR_OK ) {
154+ return false;
155+ }
156+
157+ TCHAR buf [6 ] = {0 }; // Config file should have a magic header of 5 bytes
158+ UINT bytes_read = 0 ;
159+ if (f_read (& file , buf , sizeof buf - 1 , & bytes_read ) != FR_OK
160+ || bytes_read != sizeof buf - 1 ) {
161+ f_close (& file );
162+ return false;
163+ }
164+ f_close (& file );
165+
166+ return true;
167+ }
168+
169+ response_t SDCARD_standalone_check (void ) {
170+ if (!SD_SPI_IsMediaPresent ()) {
171+ return FAILED ;
172+ }
173+
174+ FATFS drive ;
175+ if (f_mount (& drive , SDCARD_DRIVE , 1 ) != FR_OK ) {
176+ return FAILED ;
177+ }
178+ s_mounted = true;
179+
180+ if (!read_config_file ()) {
181+ SDCARD_standalone_unmount ();
182+ return FAILED ;
183+ }
184+
185+ return SUCCESS ;
186+ }
187+
188+ void SDCARD_standalone_unmount (void ) {
189+ if (s_mounted ) {
190+ f_mount (0 , SDCARD_DRIVE , 0 );
191+ s_mounted = false;
192+ }
193+ }
0 commit comments