1212
1313#define SFN_MAX 8
1414#define SFN_SUFFIX_LEN 4 // Period + at most three chars.
15+ #define FILENAME_SIZE (SFN_MAX + SFN_SUFFIX_LEN + 1)
1516#define BUF_MAX FF_MIN_SS
1617#define SDCARD_DRIVE "0:"
1718
@@ -33,17 +34,13 @@ static void get_filename(TCHAR *const fn_buf, UINT const size)
3334 // manual check for null-termination.
3435}
3536
36- response_t SDCARD_write_file (void )
37- {
38- // +1 is null-terminator.
39- size_t const filename_size = SFN_MAX + SFN_SUFFIX_LEN + 1 ;
40- TCHAR filename [filename_size ];
41- get_filename (filename , filename_size );
37+ response_t SDCARD_write_file (void ) {
38+ TCHAR filename [FILENAME_SIZE ];
39+ get_filename (filename , FILENAME_SIZE );
4240 BYTE const mode = UART1_Read ();
4341
4442 if (!SD_SPI_IsMediaPresent ()) {
45- UART1_Write (FR_NOT_READY );
46- return DO_NOT_BOTHER ;
43+ return FR_NOT_READY ;
4744 }
4845 FATFS drive ;
4946 DEBUG_write_u8 (f_mount (& drive , SDCARD_DRIVE , 1 ));
@@ -102,12 +99,16 @@ response_t SDCARD_read_file(void)
10299 remaining -= block_size ) {
103100 WATCHDOG_TimerClear ();
104101 UINT read = 0 ;
105- f_read (& file , s_sector_buf , (UINT )block_size , & read );
102+ FRESULT read_result = f_read (& file , s_sector_buf , (UINT )block_size , & read );
106103 bytes_read += read ;
107104
108- for (UINT i = 0 ; i < block_size ; ++ i ) {
105+ for (UINT i = 0 ; i < read ; ++ i ) {
109106 UART1_Write (s_sector_buf [i ]);
110107 }
108+
109+ if (read_result != FR_OK || read < block_size ) {
110+ break ;
111+ }
111112 }
112113
113114 UART1_write_u32 (bytes_read );
@@ -123,8 +124,7 @@ response_t SDCARD_get_file_info(void)
123124 get_filename (filename , sizeof filename );
124125
125126 if (!SD_SPI_IsMediaPresent ()) {
126- UART1_Write (FR_NOT_READY );
127- return DO_NOT_BOTHER ;
127+ return FR_NOT_READY ;
128128 }
129129 FATFS drive ;
130130 DEBUG_write_u8 (f_mount (& drive , SDCARD_DRIVE , 1 ));
0 commit comments