처음으로 SD-Card 를 납땜을 해서, 동작이 잘 되는지 확인을 해 봤습니다.


테스트 방법은, uSD-Card 를 마운트 하고 디렉토리 스캔을 해서 USB_CDC 로 터미널에 뿌려서
안에 넣어둔 파일이 제대로 보이는지 확인하면 되겠습니다.

다음은 보드에 uSD-Card 를 납땜한 모습입니다.




uSD-Card 회로도는 다음과 같습니다. STM32F4xx 에 SDIO-4bit 방식으로 연결했습니다.




다음은 ST 사의 CUBEMX 툴로 SDIO-4BIT 를 추가한 핀맵입니다.




다음은 디렉토리 스캔 함수 인데, 출처는 기억이 안납니다. main.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
void f_opendir_scan(void)
{
    #if _USE_LFN
    TCHAR lfn[_MAX_LFN + 1];
    fno.lfname = lfn;
    fno.lfsize = sizeof lfn;
    #endif
    TCHAR path[200= "";
    
    res = f_mount(&fs32,SD_Path,1);
    printf("SD Mount : res f_mount : %02X\n\r",res);
    
    if (res == FR_OK)
    {
    res = f_opendir(&dir,path);
        printf("res f_open : %02X\n\r",res);
        
        if (res == FR_OK)
        {
        while(1)
        {
            char *fn;
             
            res = f_readdir(&dir, &fno);
             
            if (res != FR_OK)
                printf("res = %d f_readdir\n\r", res);
 
            if ((res != FR_OK) || (fno.fname[0== 0))
                break;
 
#if _USE_LFN
      fn = *fno.lfname ? fno.lfname : fno.fname;
#else
      fn = fno.fname;
#endif
            printf("%c%c%c%c ",
                ((fno.fattrib & AM_DIR) ? 'D' : '-'),
                ((fno.fattrib & AM_RDO) ? 'R' : '-'),
                ((fno.fattrib & AM_SYS) ? 'S' : '-'),
                ((fno.fattrib & AM_HID) ? 'H' : '-') );
 
            printf("%10d ", fno.fsize);
             
            printf("%s/%s\n\r", path, fn);
        }        
        }
 
        res = f_mount(0,SD_Path,0);
        printf("SD Unmount : res f_mount : %02X\n\r",res);
    }
}
 
cs

다음은 main() 함수에서, 파일 변수들 정의해 봏고, 디렉토리 스캔함수를 호출해 주면 됩니다.
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
/* Private variables ---------------------------------------------------------*/
HAL_SD_ErrorTypedef res_sd;
 
FRESULT res;
FILINFO fno;
FIL fil;
DIR dir;
FATFS fs32;
 
uint32_t byteswritten, bytesread; /* File write/read counts */
 
 
int main(void)
{
  /* Reset of all peripherals, 
  Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_TIM2_Init();
  MX_USB_DEVICE_Init();
  MX_SDIO_SD_Init();
  MX_FATFS_Init();
 
  /* USER CODE BEGIN 2 */
  // HAL_GPIO_WritePin(SDPWR_GPIO_Port, SDPWR_Pin, GPIO_PIN_RESET);
  HAL_Delay(500);
  HAL_TIM_Base_Start_IT(&htim2);
    
  f_opendir_scan();
 
  while (1)
  {
 
  }
}
cs

그리고 나서 프로그램을 다운로드 하고 실행을 하니, uSD-Card 의 내용이 보입니다.
납땜이 제대로 잘 됐군요.^^




이번에는 u-Sdcard FTP Server 를 만들기 전에 u-SD Card가 제대로 동작하는지를 보기 위해,

Sd-card 예제(SD-esp8266->ReadWrite)를 테스트해 봤습니다.




연결은 ESP8266 의 HSPI 포트에 연결을 하고, /CS는 일반 GPIO 핀인 GPIO04 에 연결했습니다.
즉, ESP8266 의 다음과 같은 핀을 사용해서 SPI 방식으로 u-SD Card를 제어합니다.
sd-card는 예전에 샀던 u-sdcard 소켙 보드를 이용해서 esp8266과 붙였습니다.
1. SCK (HSCLK:GPIO14)
2. MISO (HMISO:GPIO12)
3. MOSI (HMOSI:GPIO13)
4. /CS (GPIO04)




NODE MCU V3 보드의 핀헤더에 u-Sdcard 와 SPI로 연결되는 핀을 다시 자세히 표시해 봤습니다.




이렇게 연결한 실제 보드의 모습은 다음과 같습니다.




이렇게 연결하고 아두이노 IDE 환경에서 프로그램을 컴파일하고 프로그램을 업로드하면 
test.txt 파일이 Sd-card에 만들어 지고, Esp-8266을 리셋 할 때마다 test.txt 에 testing 1, 2, 3, 이 계속 추가됩니다.

SD-Card를 리더에 의해 읽어 보니 위에서 말한 동작이 잘 수행됐음을 알 수 있습니다.





이렇게 SD-Card가 ESP-8266 에 연결이 잘 되었고, 동작도 잘 되었음을 알 수 있었습니다.



2일을 개고생해서 겨우 SD-Card FTP Server를 만들었습니다.

SPIFFS(ESP-8266 외부 Nor Flash memory)로 동작하는 예제는 이미 있는데,
이것을 SD-card로 동작이 되도록 고쳤습니다.

이전 글에서 정말 쉽게 되는 것인 줄 알았는데, 생각처럼 쉬운 것이 아니었습니다.

쓸데없이 수정한 부분도 있는데, 이건 제가 영어를 잘 몰라서, 해야하는 작업인 줄 알고, 코드를 수정해 버린 부분이었습니다.
File이라는  File pointer 가 SPIFFS 와 SD-Card를 동시에 그냥 사용하면 서로 겹쳐서 에러가 나게 되는데 이것을 방지하는 
방법이 있었습니다.

저는 SPIFFS와 SD 기능 2개를 동시에 쓰지 않는데도 그냥 따라해서 고생을 사서 했습니다.
이 2가지를 같이 쓰는 방법은 다음 링크를 참조해 주시기 바랍니다.
file.cpp , SD.h , SD.cpp 를 수정하도록 되어 있습니다. 

필요하신 분은 보고 따라하시고, 이렇게 수정해 버리면 File 로 변수를 선언하는 대신, Sd::File 로 변수를 선언해야 합니다.
이것이 필요 없으면 위의 파일들을 수정하지 않고, FS.H 를 include 하지 않고 나중에 File 로 변수를 선언하면 됩니다.

https://github.com/esp8266/Arduino/issues/1723 의 링크를 타고 Cosmicboris 란 사람의 글을 참고하시면 됩니다.
boris가 다시 다음의 내용을 참고하라고 링크를 알려 주는데, 좀 더 자세한 수정 방법이 나와있습니다.

이제 SPIFFS FTP Server 를 제가 이전에 쓴 글을 보고 라이브러리에 포함시킨 후, 예제를 엽니다.
아두이노 IDE에서 보이는 코드를 스케치 파일이라 하는데, 여기에서 고칠 부분은 간단하다.
1
2
3
4
5
6
7
8
9
10
  if (SD.begin(4)) {
      Serial.println("SD opened!");
      ftpSrv.begin("esp8266","esp8266");    //username, password for ftp.  set ports in ESP8266FtpServer.h  (default 21, 50009 for PASV)
  }    
  /*
  if (SPIFFS.begin()) {
      Serial.println("SPIFFS opened!");
      ftpSrv.begin("esp8266","esp8266");    //username, password for ftp.  set ports in ESP8266FtpServer.h  (default 21, 50009 for PASV)
  }    */
 
cs

위와같이 SPIFFS.begin() 을 SD.begin(4)로 고치면 된다.
그런데, 주의할 점은  SD.begin(4) 처럼 파라메터로 4를 넣어야 한다. 안 넣으면 동작되지 않는다.

그 다음에, 수정할 부분은 FTP Server 라이브러리에 복사해 놓은 파일들 중에서, 
ESP8266FtpServer.cpp 와 ESP8266FtpServer.h 를 수정해야 한다.

ESP8266FtpServer.cpp 에서 수정된 부분은 다음과 같이 다른 색으로 표시해 놨다.
이 수정된 부분은 SPIFFS 용 함수를 SD Card용 함수로 다 바꿔야 하는 부분이다.
그리고 SD-Card 함수에는 rename 함수가 없어서, 수정한  FTP Server 프로그램은 Rename 명령이 안먹는다.

먼저 ESP8266FtpServer.cpp 에서 SPIFFS 용 헤더 파일을 뺀다.
1
2
3
4
5
6
#include "ESP8266FtpServer.h"
 
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//#include <FS.h>
cs

ESP8266FtpServer.cpp 에서 다른 함수들은 그대로 두고, 
boolean FtpServer::processCommand() 함수 부분만 수정하면 됩니다.
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
boolean FtpServer::processCommand()
{
 
  ///////////////////////////////////////
  //                                   //
  //      ACCESS CONTROL COMMANDS      //
  //                                   //
  ///////////////////////////////////////
 
  //
  //  CDUP - Change to Parent Directory 
  //
  if! strcmp( command, "CDUP" ))
  {
      client.println("250 Ok. Current directory is " + String(cwdName));
  }
 
  //
  //  CWD - Change Working Directory
  //
  else if! strcmp( command, "CWD" ))
  {
    char path[ FTP_CWD_SIZE ];
 
    if( strcmp( parameters, "." ) == 0 )  // 'CWD .' is the same as PWD command
      client.println( "257 \"" + String(cwdName) + "\" is your current directory");
    else 
      {       
        client.println( "250 Ok. Current directory is " + String(cwdName) );
      }
  }
 
  //
  //  PWD - Print Directory
  //
  else if! strcmp( command, "PWD" ))
    client.println( "257 \"" + String(cwdName) + "\" is your current directory");
  //
  //  QUIT
  //
  else if! strcmp( command, "QUIT" ))
  {
    disconnectClient();
    return false;
  }
 
 
  ///////////////////////////////////////
  //                                   //
  //    TRANSFER PARAMETER COMMANDS    //
  //                                   //
  ///////////////////////////////////////
 
  //
  //  MODE - Transfer Mode 
  //
  else if! strcmp( command, "MODE" ))
  {
    if! strcmp( parameters, "S" ))
      client.println( "200 S Ok");
    // else if( ! strcmp( parameters, "B" ))
    //  client.println( "200 B Ok\r\n";
    else
      client.println( "504 Only S(tream) is suported");
  }
 
  //
  //  PASV - Passive Connection management
  //
  else if! strcmp( command, "PASV" ))
  {
    if (data.connected()) data.stop();
    //dataServer.begin();
     //dataIp = Ethernet.localIP();    
    dataIp = WiFi.localIP();    
    dataPort = FTP_DATA_PORT_PASV;
    //data.connect( dataIp, dataPort );
    //data = dataServer.available();
 
    #ifdef FTP_DEBUG
    Serial.println("Connection management set to passive");
      Serial.println( "Data port set to " + String(dataPort));
    #endif
   client.println( "227 Entering Passive Mode ("+ String(dataIp[0]) + "," + String(dataIp[1])+","+ String(dataIp[2])+","+ String(dataIp[3])+","+String( dataPort >> 8 ) +","+String ( dataPort & 255 )+").");
   dataPassiveConn = true;
  }
 
  //
  //  PORT - Data Port
  //
  else if! strcmp( command, "PORT" ))
  {
    if (data) data.stop();
 
    // get IP of data client
    dataIp[ 0 ] = atoi( parameters );
    char * p = strchr( parameters, ',' );
 
    for( uint8_t i = 1; i < 4; i ++ )
    {
      dataIp[ i ] = atoi( ++ p );
      p = strchr( p, ',' );
    }
 
    // get port of data client
    dataPort = 256 * atoi( ++ p );
    p = strchr( p, ',' );
    dataPort += atoi( ++ p );
 
    if( p == NULL )
      client.println( "501 Can't interpret parameters");
    else
    {
        client.println("200 PORT command successful");
      dataPassiveConn = false;
    }
  }
 
  //
  //  STRU - File Structure
  //
  else if! strcmp( command, "STRU" ))
  {
    if! strcmp( parameters, "F" ))
      client.println( "200 F Ok");
    // else if( ! strcmp( parameters, "R" ))
    //  client.println( "200 B Ok\r\n";
    else
      client.println( "504 Only F(ile) is suported");
  }
 
  //
  //  TYPE - Data Type
  //
  else if! strcmp( command, "TYPE" ))
  {
    if! strcmp( parameters, "A" ))
      client.println( "200 TYPE is now ASII");
    else if! strcmp( parameters, "I" ))
      client.println( "200 TYPE is now 8-bit binary");
    else
      client.println( "504 Unknow TYPE");
  }
 
 
  ///////////////////////////////////////
  //                                   //
  //        FTP SERVICE COMMANDS       //
  //                                   //
  ///////////////////////////////////////
 
  //
  //  ABOR - Abort
  //
  else if! strcmp( command, "ABOR" ))
  {
    abortTransfer();
    client.println( "226 Data connection closed");
  }
 
  //
  //  DELE - Delete a File 
  //
  else if! strcmp( command, "DELE" ))
  {
    char path[ FTP_CWD_SIZE ];
 
    if( strlen( parameters ) == 0 )
      client.println( "501 No file name");
    else if( makePath( path ))
    {
      //try.. if( ! SPIFFS.exists( path ))
      if! SD.exists( path ))
        client.println( "550 File " + String(parameters) + " not found");
      else
      {
        //try.. if( SPIFFS.remove( path ))
        if( SD.remove( path ))
          client.println( "250 Deleted " + String(parameters) );
        else
          client.println( "450 Can't delete " + String(parameters));
      }
    }
  }
 
  //
  //  LIST - List 
  //
  else if! strcmp( command, "LIST" ))
  {
    if! dataConnect())
      client.println( "425 No data connection");
    else
    {
      client.println( "150 Accepted data connection");
      uint16_t nm = 0;
      //try.. Dir dir=SPIFFS.openDir(cwdName);
      //try2.. Dir dir=SD.openDir(cwdName);
      //dir_t dir=SD.openDir(cwdName);
      sd::File dir=SD.open(cwdName);
         sd::File entry;
      //try.. if( !SPIFFS.exists(cwdName))
      // try.. if( !SD.exists(cwdName))
      if(!dir.isDirectory())
        client.println( "550 Can't open directory " + String(cwdName) );
      else
      {
        // try.. while( dir.next())
        while(1)
        {
            entry =  dir.openNextFile();
            if (!entry)
                break;
 
            String fn,fs;
 
            //try.. fn = dir.fileName();
            fn = entry.name();
            //fn.remove(0, 1);
            //try.. fs = String(dir.fileSize());
            fs = String(entry.size());
              data.println( "Type=file;Size=" + fs + ";"+"modify=20000101160656;" +" " + fn);
              nm ++;
        }
        client.println( "226 " + String(nm) + " matches total");
      }
      data.stop();
    }
  }
 
  //
  //  MLSD - Listing for Machine Processing (see RFC 3659)
  //
  else if! strcmp( command, "MLSD" ))
  {
 
    if! dataConnect())
      client.println( "425 No data connection MLSD");
    else
    {
      client.println( "150 Accepted data connection");
      uint16_t nm = 0;
 
      //try.. Dir dir= SPIFFS.openDir(cwdName);
      //try.. Dir dir= SD.openDir(cwdName);
     sd::File dir=SD.open(cwdName);
      sd::File entry;
 
      char dtStr[ 15 ];
 
    //  if(!SPIFFS.exists(cwdName))
    //    client.println( "550 Can't open directory " +String(parameters)+ );
    //  else
      {
        //try.. while( dir.next())
 
        while(1)
        {
            entry =  dir.openNextFile();
            if (!entry)
                break;
 
            String fn,fs;
            //try.. fn = dir.fileName();
            fn = entry.name();
           //fn.remove(0, 1);
            //try.. fs = String(dir.fileSize());
            fs = String(entry.size());
              data.println( "Type=file;Size=" + fs + ";"+"modify=20000101160656;" +" " + fn);
            //Serial.print("+r,s" + fs);
            //Serial.println( ",\t" + fn );
              nm ++;
        }
        client.println( "226-options: -a -l");
        client.println( "226 " + String(nm) + " matches total");
      }
      data.stop();
    }
  }
 
  //
  //  NLST - Name List 
  //
  else if! strcmp( command, "NLST" ))
  {
    if! dataConnect())
      client.println( "425 No data connection");
    else
    {
      client.println( "150 Accepted data connection");
      uint16_t nm = 0;
      //try.. Dir dir=SPIFFS.openDir(cwdName);
      //Dir dir=SD.openDir(cwdName);
      sd::File dir=SD.open(cwdName);
      //try.. if( !SPIFFS.exists( cwdName ))
      if( !SD.exists( cwdName ))
        client.println( "550 Can't open directory " + String(parameters));
      else
      {
        //try.. while( dir.next())
        while( dir.openNextFile())
        {
          //try.. data.println( dir.fileName());
          data.println( dir.name());
          nm ++;
        }
        client.println( "226 " + String(nm) + " matches total");
      }
      data.stop();
    }
  }
 
  //
  //  NOOP
  //
  else if! strcmp( command, "NOOP" ))
  {
    // dataPort = 0;
    client.println( "200 Zzz...");
  }
  //
  //  RETR - Retrieve
  //
 
  else if! strcmp( command, "RETR" ))
  {
    char path[ FTP_CWD_SIZE ];
    if( strlen( parameters ) == 0 )
      client.println( "501 No file name");
    else if( makePath( path ))
    {
        //try.. file = SPIFFS.open(path, "r");
        sd::File file = SD.open(path, FILE_READ);
      if!file)
        client.println( "550 File " +String(parameters)+ " not found");
      else if!file )
        client.println( "450 Can't open " +String(parameters));
      else if! dataConnect())
        client.println( "425 No data connection");
      else
      {
        #ifdef FTP_DEBUG
          Serial.println("Sending " + String(parameters));
        #endif
        client.println( "150-Connected to port "+ String(dataPort));
        client.println( "150 " + String(file.size()) + " bytes to download");
        millisBeginTrans = millis();
        bytesTransfered = 0;
        transferStatus = 1;
      }
    }
  }
 
  //
  //  STOR - Store
  //
 
  else if! strcmp( command, "STOR" ))
  {
    char path[ FTP_CWD_SIZE ];
 
    if( strlen( parameters ) == 0 )
      client.println( "501 No file name");
 
    else if( makePath( path ))
    {
        //file = SPIFFS.open(path, "w");
        //try.. file = SD.open(path, "w");
        sd::File file = SD.open(path, FILE_WRITE);
 
      if!file)
        client.println( "451 Can't open/create " +String(parameters) );
 
      else if! dataConnect())
      {
        client.println( "425 No data connection");
        file.close();
      }
      else
      {
        #ifdef FTP_DEBUG
          Serial.println( "Receiving " +String(parameters));
        #endif
        client.println( "150 Connected to port " + String(dataPort));
        millisBeginTrans = millis();
        bytesTransfered = 0;
        transferStatus = 2;
      }
    }
  }
 
  //
  //  MKD - Make Directory
  //
  else if! strcmp( command, "MKD" ))
  {
      client.println( "550 Can't create \"" + String(parameters));  //not support on espyet
  }

  //
  //  RMD - Remove a Directory 
  //
  else if! strcmp( command, "RMD" ))
  {
      client.println( "501 Can't delete \"" +String(parameters));
  }
 
  //
  //  RNFR - Rename From 
  //
  else if! strcmp( command, "RNFR" ))
  {
    buf[ 0 ] = 0;
 
    if( strlen( parameters ) == 0 )
      client.println( "501 No file name");
    else if( makePath( buf ))
    {
      //if( ! SPIFFS.exists( buf ))
      if! SD.exists( buf ))
        client.println( "550 File " +String(parameters)+ " not found");
      else
      {
        #ifdef FTP_DEBUG
          Serial.println("Renaming " + String(buf));
        #endif
        client.println( "350 RNFR accepted - file exists, ready for destination");     
        rnfrCmd = true;
      }
    }
  }
 
  //
  //  RNTO - Rename To 
  //
  else if! strcmp( command, "RNTO" ))
  {  
    char path[ FTP_CWD_SIZE ];
    char dir[ FTP_FIL_SIZE ];
 
    if( strlen( buf ) == 0 || ! rnfrCmd )
      client.println( "503 Need RNFR before RNTO");
    else if( strlen( parameters ) == 0 )
      client.println( "501 No file name");
    else if( makePath( path ))
    {
      //if( SPIFFS.exists( path ))
      if( SD.exists( path ))
        client.println( "553 " +String(parameters)+ " already exists");
      else
      {          
            #ifdef FTP_DEBUG
          Serial.println("Renaming " + String(buf) + " to " + String(path));
            #endif
            //try.. if( SPIFFS.rename( buf, path ))
      /*try..      if( SD.rename( buf, path ))
              client.println( "250 File successfully renamed or moved");
            else
                client.println( "451 Rename/move failure");*/
        client.println( "451 Rename/move failure");
      }
    }
    rnfrCmd = false;
  }
 
  ///////////////////////////////////////
  //                                   //
  //   EXTENSIONS COMMANDS (RFC 3659)  //
  //                                   //
  ///////////////////////////////////////
  //
  //  FEAT - New Features
  //
 
  else if! strcmp( command, "FEAT" ))
  {
    client.println( "211-Extensions suported:");
    client.println( " MLSD");
    client.println( "211 End.");
  }
 
  //
  //  MDTM - File Modification Time (see RFC 3659)
  //
  else if (!strcmp(command, "MDTM"))
  {
      client.println("550 Unable to retrieve time");
  }
 
  //
  //  SIZE - Size of the file
  //
  else if! strcmp( command, "SIZE" ))
  {
    char path[ FTP_CWD_SIZE ];
    if( strlen( parameters ) == 0 )
      client.println( "501 No file name");
    else if( makePath( path ))
    {
        //file = SPIFFS.open(path, "r");
        sd::File file = SD.open(path,FILE_READ);
      if(!file)
         client.println( "450 Can't open " +String(parameters) );
      else
      {
        client.println( "213 " + String(file.size()));
        file.close();
      }
    }
  }
 
  //
  //  SITE - System command
  //
  else if! strcmp( command, "SITE" ))
  {
      client.println( "500 Unknow SITE command " +String(parameters) );
  }
 
  //
  //  Unrecognized commands ...
  //
  else
    client.println( "500 Unknow command");
  return true;
cs


ESP8266FtpServer.h 파일에서 다음과 같이 수정. (FS.h 를 빼 버리고 SD.h 와 SPI.h 를 넣는다)
1
2
3
4
#include <SPI.h>
#include <SD.h>
//#include <FS.h>
#include <WiFiClient.h>
cs

이렇게 수정하고 혹시나 라이브러리가 중복된다는 메세지가 나온다면 겹치는 라이브러리를 지워버리거나 다른 장소로 이동하면 없어진다. 아두이노는 초보라서 라이브러리가 중복되는 것을 무식하게 잡았다. ㅜㅜ

그럼 테스트 과정이다.

먼저 공유기에 접속해야 한다. 아두이노 스케치 파일에 SSID,Password 를 적는 란이 있는데, 자신의 공유기의 SSID 와 Password를 적어 주면,
1
2
3
4
5
6
7
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266FtpServer.h>
 
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";
cs

 
ESP8266 이 공유기로부터  IP를 할당받아서 FTP 서버로 붙는다.




위의 SD opened! 밑 부분은 제가 시리얼로 실험하느라 출력해 본 내용인데, 위에 올린 코드에서는 지웠을 겁니다.(아니네요.. 그냥 남아 있네요.)

이제 PC용 FTP Client 접속 프로그램인 Filezilla 를 인터넷에서 다운받아서 다음과 같이 설정합니다.



일반 탭의 설정 사함은 다음과 같다.




고급탭의 설정은 다음과 같다.



이렇게 설정하고 연결을 누르면 ESP8266 FTP Server와 WiFi 로 접속이 되어 파일을 읽고 쓸 수 있다.
쓰는 것(ESP8266 -> PC 로 파일 전송)은 잘 되는데, 읽는 것(PC-> ESP8266 로 파일 전송)은 내용이 없이 이름만 생기네요. 원인을 찾아봐야 겠습니다. 이런, 보여 줄려고 해보니 이상한 점이 좀 많네요. 동작 안 될 때도 있고 .. 조금 더 손봐야 하겠습니다.





문제점 잡았습니다. 휴.. 아두이노도 잘 모르고 C++도 잘 모르는데, 2가지를 한꺼번에 하고 있으니 힘드네요. ㅜㅜ
ESP8266FtpServer.cpp 파일에서,
1
2
        //try.. file = SPIFFS.open(path, "r");
        sd::File file = SD.open(path, FILE_READ);
cs
로 잘못 바꿨던 부분을  다음처럼 수정하니까, ESP8266->PC 방향으로 FTP에 의해 데이터가 제대로 전송되는군요.
1
2
        //try.. file = SPIFFS.open(path, "r");
        file = SD.open(path, FILE_READ);
cs

그리고 또 하나 수정할 점입니다.
ESP8266FtpServer.h 파일에서 file 변수 선언을 다음처럼 수정했습니다.
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
using namespace sd;
using sd::File;
 
class FtpServer
{
public:
  void    begin(String uname, String pword);
  void    handleFTP();
 
private:
  void    iniVariables();
  void    clientConnected();
  void    disconnectClient();
  boolean userIdentity();
  boolean userPassword();
  boolean processCommand();
  boolean dataConnect();
  boolean doRetrieve();
  boolean doStore();
  void    closeTransfer();
  void    abortTransfer();
  boolean makePath( char * fullname );
  boolean makePath( char * fullName, char * param );
  uint8_t getDateTime( uint16_t * pyear, uint8_t * pmonth, uint8_t * pday,
                       uint8_t * phour, uint8_t * pminute, uint8_t * second );
  char *  makeDateTimeStr( char * tstr, uint16_t date, uint16_t time );
  int8_t  readChar();
 
  IPAddress      dataIp;              // IP address of client for data
  WiFiClient client;
  WiFiClient data;
  
  //SdFile file;
  File file;
  
  boolean  dataPassiveConn;
  uint16_t dataPort;
  char     buf[ FTP_BUF_SIZE ];       // data buffer for transfers
  char     cmdLine[ FTP_CMD_SIZE ];   // where to store incoming char from client
  char     cwdName[ FTP_CWD_SIZE ];   // name of current directory
  char     command[ 5 ];              // command sent by client
  boolean  rnfrCmd;                   // previous command was RNFR
  char *   parameters;                // point to begin of parameters sent by client
  uint16_t iCL;                       // pointer to cmdLine next incoming char
  int8_t   cmdStatus,                 // status of ftp command connexion
           transferStatus;            // status of ftp data transfer
  uint32_t millisTimeOut,             // disconnect after 5 min of inactivity
           millisDelay,
           millisEndConnection,       // 
           millisBeginTrans,          // store time of beginning of a transaction
           bytesTransfered;           //
  String   _FTP_USER;
  String   _FTP_PASS;
 
  
 
};
cs

바꾼게 많아서 수정한 파일들을 첨부해 놓겠습니다. 
아두이노는 서툴러서 zip으로 프로젝트만 뽑아서 압축하는 방법도 아직 잘 몰라서 또 무식하게 따로따로 파일을 각각 첨부하네요.
^^


아두이노 IDE 툴의 스케치 프로그램에서 SD-Card 설정은 다음의 함수로 설정 가능하다.


1
2
3
4
5
6
7
8
9
10
11
12
boolean SDClass::begin(uint8_t csPin, uint32_t speed) {
  /*
    Performs the initialisation required by the sdfatlib library.
    Return true if initialization succeeds, false otherwise.
   */
  return card.init(speed, csPin) &&
         volume.init(card) &&
         root.openRoot(volume);
}
cs

이전에 Ftp 서버 스케치 프로그램의 void setup(void) 함수 내에서, 다음과 같이 사용했다.

1
2
3
4
  if (SD.begin(4)) {
      Serial.println("SD opened!");
      ftpSrv.begin("esp8266","esp8266");    //username, password for ftp.  set ports in ESP8266FtpServer.h  (default 21, 50009 for PASV)
  }    
cs

즉 MISO,MOSI,SCLK 는 HSPI 포트들을 사용하므로 고정되어 있으므로, CS 핀만 따로 지정해 주면 된다.
현재 위의 소스코드에서 Clock Speed는 사용하지 않았는데, 파라메터를 넣지 않으면 디폴트값이 들어간다.
하지만, 디폴트 값이 얼만지는 잘 모르겠다.
GPIO 4번핀을 CS핀으로 사용하고, Clock speed는 기본이다.

가령, SD.begin(4,40000000) 라고 하면 GPIO 4번핀을 CS핀으로 사용하고, Clock speed는 40MHz 이다.
최대 클럭 스피드는 시스템 클럭의 /2 이다.


esp8266 FTP 서버의 전송 속도를 개선하는 방법을 여러가지 찾아보고 적용해 봤습니다.


전송 속도에 관련된 사항은 다음과 같습니다. 
1. WIFI FTP BUFFER 의 크기 : ESP8266FtpServer.h 파일에 FTP_BUF_SIZE 로 정의가 되어 있는데,
   크기가 기본으로 1024 // 512 로 설정되어 있는데 이 설정으로는 너무 느리다.
   512 로 9KB/s , 1024로 18KB/s 밖에 안나온다.
   하지만 이 크기를 1460 의 배수인 1460*2 로 바꾸면 460KB/s 의 속도가 나온다. 
   이 속도는 SD-Card에서 읽어서 PC로 전송하는 속도이고, SPI Single 방식의 SD-Card Write 시에는 약 86KB/s 가 나온다.
   즉, SD 카드를 읽는 데는 엄청 빠른데, 쓸 때는 SPI Single 에다가 Write 할 동안 기다려야 하므로 아주 느리다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <SPI.h>
#include <SD.h>
//#include <FS.h>
#include <WiFiClient.h>
 
#define FTP_SERVER_VERSION "FTP-2016-01-14"
 
#define FTP_CTRL_PORT    21       // Command port on wich server is listening  
#define FTP_DATA_PORT_PASV 50009  // Data port in passive mode
 
#define FTP_TIME_OUT  5      // Disconnect client after 5 minutes of inactivity
#define FTP_CMD_SIZE 255 + 8 // max size of a command
#define FTP_CWD_SIZE 255 + 8 // max size of a directory name
#define FTP_FIL_SIZE 255     // max size of a file name
//#define FTP_BUF_SIZE 1024 //512   // size of file buffer for read/write
#define FTP_BUF_SIZE 2*1460
cs

2. CPU Clock 속도 Up : 80MHz 에서 160MHz 로 올리면 조금 더 빨라진다. 
3. SPI CLK 속도 UP : 현재 24MHz 까지 올렸는데 50MHz 까지 올려 볼 예정이다.
  이 과정은 선 길이에 영향을 많이 받기 때문에, PCB가 나와봐야 알 수 있을 것이다.


버퍼 크기는 1460 배수가 아니면 속도 차가 읽기 모드에서 최대 51배나 차이가 납니다.
쓰실 때, 꼭 알아두세요. ^^

다음은 FTP 서버에서 PC로 전송시 속도를 동영상으로 찍어 봤습니다.


ESP8266 FTP 서버에서 파일을 전송을 하던지 PC에서 FTP 서버로 전송을 하면

ESP8266 의 디렉토리가 몇개씩 사라지는 현상이 있어서 원인을 한참 찾다가 고쳐졌습니다.

정확한 원인은 잘 모르겠고, SD Card 에서 디렉토리를 읽어서 보여줄 때,
함수를 1개(rewindDirectory) 더 추가해야 이 현상이 없어졌습니다.

어쩌면 SPIFFS 메모리에서는 이런 현상이 안나타날 지도 모르겠습니다.
제가 SPIFFS 메모리로 된 FTP 서버 프로그램을 SD 카드용으로 바꿔서 여러가지 오동작이 많이 발생하는 것 같습니다.

FTP 서버에서 디렉토리를 새로고치는 명령이 "MLSD" 이므로 
ESP8266FtpServer.ccp 의 processCommand 함수에서 "MLSD" 명령을 처리하는 부분을 수정하면 됩니다.
수정한 코드는 다음과 같습니다.
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
  //
  //  MLSD - Listing for Machine Processing (see RFC 3659)
  //
  else if! strcmp( command, "MLSD" ))
  {
    if! dataConnect())
      client.println( "425 No data connection MLSD");
    else
    {
      client.println( "150 Accepted data connection");
      uint16_t nm = 0;
 
      File dir=SD.open(cwdName);
      File entry;
 
      dir.rewindDirectory();
          
      char dtStr[ 15 ];
 
        while(1)
        {
            entry =  dir.openNextFile();
            if (!entry)
                break;
            String fn,fs;
            fn = entry.name();
            fs = String(entry.size());
              data.println( "Type=file;Size=" + fs + ";"+"modify=20000101160656;" +" " + fn);
              nm ++;
        }
        client.println( "226-options: -a -l");
        client.println( "226 " + String(nm) + " matches total");
      }
      data.stop();
      entry.close();
  }
cs

SD.open() 함수 다음에 rewindDirectory(); 함수를 사용하면 됩니다.


1. 오류 발생 : 파일을 전송하는데 처음에만 되고 그 이후로는 안되서 오류 분석 중. 추가로 파일 전송을 하고 난 후, 디렉토리가 없어짐.
2. 오류 원인 : 이름이 한글로된 파일이나 이름에 콤마가 2개 이상인 파일이 문제를 일으켜서 FTP 서버가 동작을 안하는 경우가 있었다.
3. 해결책 : 위의 종류의 파일을 취급하지 않거나, LFN(Long File Name)을 지원하거나 UNICODE(한글 지원)이 되도록 프로그램을 고쳐야 함. (디렉토리 해결책은 다음 글에 올려 놨음)


안녕하세요, 이번에는 Soft AP 를 이용한 8266 FTP Server를 만들어 봤습니다.


주변의 공유기에 붙여서 IP를 할당받아서 사용하는 방법이 아니라, 
8266 자체가 Ftp Server가 됩니다.

이것도 FTP Server가 공식적인 예제가 아니여서인지, 처음에는 Soft AP 는 잘 구현되어서 
SSID 와 Password를 치고 들어갔는데 Ftp Server 동작이 안됐었습니다.

많이 고생을 했는데, 알고 보니 SoftAPIP 주소가 아니라 localIP 주소에 의해 Ftp Server가 동작되도록 되어 있었습니다.
아직 Esp8266 과 네트웍 개념도 없고 c++ 문법도 잘 몰라서 SoftIP 주소에 의해 동작하도록 하는 방법으로 수정할 순 없겠고,
localIP 주소를 수정해서 동작하도록 만들었습니다.

예전의 SD 카드에 의해 동작하는 FTP 예제에서 아두이노 스케치 코드만 조금 고쳐주면 됩니다.

SoftAP 로 동작하는 데에는 독립적인 SSID 와 비밀번호가 필요하니 정의해 놓고,
1
2
3
const char* ssid = "FISI_Server";
const char* password = "0317358631";
 
cs

FTP 에서 사용할 IP 및 Gateway,subnetMask 주소를 정의했습니다.
1
2
3
4
5
// config static IP
IPAddress apIP(19216841);  
IPAddress gateway(19216841); // set gateway to match your network
IPAddress subnet(2552552550); // set subnet mask to match your network
 
cs

config 함수로 실제 local IP,Gateway,SubnetMask 주소를 세팅합니다.
그리고 아무 상관은 없지만 기분이 찝찝하니 SoftAP Ip,gateway,subnetmask 주소도 세팅해 주고,
WiFi.softAP 함수로 위에서 정의해 둔, SSID 와  비밀번호로 AP를 만듭니다.
1
2
3
4
5
6
7
8
9
10
  WiFi.config(apIP, gateway, subnet);
  Serial.println();
  Serial.print("Configuring access point...");
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.mode(WIFI_AP);
  WiFi.disconnect();
  delay(100);
  WiFi.softAPConfig(apIP, gateway, subnet);
  WiFi.softAP(ssid, password);
 
cs


위의 소스코드에서 쓸데없이 들어있는 것도 있을텐데, 또 할일들이 많아서 더 테스트는 못했습니다.

전체 아두이노 IDE의 스케치 파일의 소스 코드는 다음과 같습니다.
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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266FtpServer.h>
 
const char* ssid = "FISI_Server";
const char* password = "0317358631";
 
// config static IP
IPAddress apIP(19216841);  
IPAddress gateway(19216841); // set gateway to match your network
IPAddress subnet(2552552550); // set subnet mask to match your network
 
 
ESP8266WebServer server(80);
FtpServer ftpSrv;   //set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp verbose on serial
 
 
 
void handleRoot() {
  server.send(200"text/plain""hello from esp8266!");
 
}
 
void handleNotFound(){
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404"text/plain", message);
}
 
void setup(void){
  delay(1000);
  Serial.begin(115200);
 
  Serial.println("");
  Serial.println(WiFi.SSID());
  Serial.println(WiFi.psk());
  WiFi.config(apIP, gateway, subnet);
 
  Serial.println();
  Serial.print("Configuring access point...");
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.mode(WIFI_AP);
  WiFi.disconnect();
  delay(100);
  WiFi.softAPConfig(apIP, gateway, subnet);
  WiFi.softAP(ssid, password);
 
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
 
  Serial.println("");
  Serial.println("");
 
  WiFi.printDiag(Serial);
  
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
  server.on("/", handleRoot);
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
 
  /////FTP Setup, ensure SPIFFS is started before ftp;  /////////
  
  if (SD.begin(4)) {
      Serial.println("SD opened!");
      ftpSrv.begin("esp8266","esp8266");    //username, password for ftp.  set ports in ESP8266FtpServer.h  (default 21, 50009 for PASV)
  }    
      Serial.println("FTP server On!");
  /*
  if (SPIFFS.begin()) {
      Serial.println("SPIFFS opened!");
      ftpSrv.begin("esp8266","esp8266");    //username, password for ftp.  set ports in ESP8266FtpServer.h  (default 21, 50009 for PASV)
  }    */
}
 
void loop(void){
  ftpSrv.handleFTP();        //make sure in loop you call handleFTP()!!  
  server.handleClient();
 
}
cs

다 준비가 됐으니, Wifi 로 미리 설정해 둔 SSID 와 비번으로 Esp8266 에 접속합니다.



접속이 됐으면 이번에는 랜드폰 앱으로 FTP Client 앱을 받아서 설치합니다.
여러가지가 있는데 저는 터보 클라이언트라는 앱을 사용했습니다.


FTP 프로그램을 실행해서 설정을 다음과 같이 했습니다.



그리고 FTP 서버에 접속하니 SD 카드에 들어있는 내용이 나왔습니다. SOFT AP 도 완료. ~` ^^;




안녕하세요, 

이 글은 예정에는 없었지만, 카페에 올린줄 알았는데 없어서 글을 쓰게 되었습니다.

PSOC5에서 micro SD-Card 를 사용하는 방법인데, PSOC5은 SDIO 가 없어서 SPI 방식만 지원합니다.
속도는 24Mhz 입니다.
제가 보기에는 현재까지 PSOC5가 MCU 중에서 가장 사용자 친화적으로 잘 만들어 진 것 같은데,
패키지가 다양하지 않고 후속 모델(PSOC6)이 안나오며, PSOC4만 계속 손데고 있어서 매우 불만이 많습니다.
가격은 요새 좀 내려간 듯 한데, STM32F4xx 에는 가성비가 못미치네요.
PSOC5 이대로 죽는겐가? ㅜㅜ

아.. 헛소리를 많이 했군요. 
그럼 다시 PSOC5에서 SPI 방식으로 SD Card 제어하는 방법을 알아보겠습니다.
PSOC Creator 의 TopDesign.cysch 에 emFile SPI Mode[V1.20] 콤포넌트를 배치하고 
emfile 콤포넌트의 Datasheet를 보면 여기서 설명하려는 내용이 영문으로 나와 있습니다.


1. PSOC Creator 내부에 File System 라이브러리가 없기 때문에 먼저 File System 라이브러리를 다운로드 받아야 합니다.

여기(링크)에서 다운로드 받으시면 됩니다.





2. 받은 파일을 압축을 풀어서 적당한 위치에 놓으십시요. (저는 프로젝트 폴더 옆에 놓았습니다)




3. 콤포넌트 창에서 Communication->FileSystem->emFile 을 TopDesign.cysch 으로 끌고 와서 배치합니다.




4. 왼쪽 Workspace Explorer 의 Project 를 우클릭하여 Build Setting 을 클릭합니다.




5. Include Directory 를 추가




6. 라이브러리 이름 추가. 직접 타이핑해 줍니다.
emf32noslfn 를 타이핑 하고, 이 의미는 No OS , Long File Name 의 의미입니다.




7. 다음은 라이브러리가 있는 디렉토리를 추가합니다.




8. 라이브러리 파일을 추가합니다.




9. 회로를 보고 핀을 배치합니다. (PSOC5는 이것이 장점 : 핀을 맘대로 옮길 수 있습니다.)
디버깅용으로 USB Serial 포트도 하나 넣었습니다.




10. micro-SD 카드 회로도는 다음과 같습니다. 신호에 굳이 풀업저항은 필요 없었습니다.
혹시나 해서 테스트 해 봤는데 잘 되네요.




11. SD 카트에서 데이터를 읽어서 뿌려볼라고 하는데, 구글링해서 "영문 텍스트 소설" 검색하니 해리포터 불의잔이 있네요. ^^
다운받아서 SD Card에 넣습니다.




12. 제작한 기판에 SD Card를 끼웁니다. 회사에서 만든 기판은 다음과 같습니다.





이제 프로그램만 짜면 되는데 불의잔을 읽어서 USB Serial로 터미날에 5000 바이트만 뿌려 보겠습니다.
먼저 결과를 올려 보겠습니다.

잘 읽히네요. 불의 잔. ^^

소스코드는 main.c 에만 코드를 추가했고, SD Card 읽기만 테스트 해 봤습니다.
나머지는 현재까지 읽기 이외의 작업이 필요없었던 관계로 회원 여러분이 찾아서 해 보십시요. ^^
프로젝트 압축해서 첨부합니다. 그런데 emfile 라이브러리는 PSOC Creator에서 한번에 압축을 안해 주네요.
아마도 4~8 과정을 회원분들께서 다시 해 주셔야 될 것 같습니다.

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
#include "project.h"
#include <stdio.h>
#include <FS.h>
 
#define BUFFER_LEN  64u
 
struct tagFILE
{
    uint8 accmd;
    uint32 sz;
    FS_FILE* fp;
    char tFileName[64];
};
 
static struct tagFILE   tFILE;
char8 *parity[] = { "None""Odd""Even""Mark""Space" };
char8 *stop[] = { "1""1.5""2" };
 
void ready_USB_UART_tx(void);
 
int main(void)
{
    uint8_t buffer[5000];
    uint16_t i;
    char usb_tx[BUFFER_LEN];
    
    
    FS_Init();
    FS_Mount(0);
    FS_FAT_SupportLFN();
    
    CyGlobalIntEnable; /* Enable global interrupts. */
 
    /* Place your initialization/startup code here (e.g. MyInst_Start()) */
    
    CyDelay(500);
    
    FS_DIR*     pDir;
    USBUART_1_Start(0u, USBUART_1_3V_OPERATION);
    ready_USB_UART_tx();
 
    pDir = FS_OpenDir("");
    sprintf(tFILE.tFileName,"Harry_potter_Goblet_of_Fire.txt");
    tFILE.fp = FS_FOpen(tFILE.tFileName, "rb");
    tFILE.sz = FS_GetFileSize (tFILE.fp);
 
    sprintf(usb_tx,"File size : %5lu\n\r",tFILE.sz);
    while(USBUART_1_CDCIsReady() == 0u);    /* Wait till component is ready to send more data to the PC */ 
    USBUART_1_PutString(usb_tx);       /* Send data back to PC */
    
    FS_FSeek(tFILE.fp,0,0);
    FS_Read(tFILE.fp, buffer, 5000);
    for (i=0;i<5000;i++)
    {
        while(USBUART_1_CDCIsReady() == 0u);    /* Wait till component is ready to send more data to the PC */ 
        USBUART_1_PutChar(buffer[i]);       /* Send data back to PC */
    }
    //proc_USB_UART();
 
    for(;;)
    {
        /* Place your application code here. */
    }
}
 
void ready_USB_UART_tx(void)
{
    uint16 count;
    uint8 state,led_st=0,cnt_USB_UART_con=0;;
    uint8 buffer[BUFFER_LEN];
    char tx_buffer[BUFFER_LEN];
   /* Main Loop: */
    for(;;)
    {
        if(USBUART_1_IsConfigurationChanged() != 0u) /* Host could send double SET_INTERFACE request */
        {
            if(USBUART_1_GetConfiguration() != 0u)   /* Init IN endpoints when device configured */
            {
               /* Enumeration is done, enable OUT endpoint for receive data from Host */
                USBUART_1_CDC_Init();
            }
        }         
        if(USBUART_1_GetConfiguration() != 0u)    /* Service USB CDC when device configured */
        {
            if(USBUART_1_DataIsReady() != 0u)               /* Check for input data from PC */
            {   
                count = USBUART_1_GetAll(buffer);           /* Read received data and re-enable OUT endpoint */
                if(count != 0u)
                {
                    while(USBUART_1_CDCIsReady() == 0u);    /* Wait till component is ready to send more data to the PC */ 
                    USBUART_1_PutData(buffer, count);       /* Send data back to PC */
                    /* If the last sent packet is exactly maximum packet size, 
                    *  it shall be followed by a zero-length packet to assure the
                    *  end of segment is properly identified by the terminal.
                    */
                    if(count == BUFFER_LEN)
                    {
                        while(USBUART_1_CDCIsReady() == 0u); /* Wait till component is ready to send more data to the PC */ 
                        USBUART_1_PutData(NULL, 0u);         /* Send zero-length packet to PC */
                    }
                }
            }  
            
            state = USBUART_1_IsLineChanged();              /* Check for Line settings changed */
            if(state != 0u)
            {  
                if(state & USBUART_1_LINE_CODING_CHANGED)   /* Show new settings */
                {
                    sprintf(tx_buffer,"BR:%4ld,DB:%d\n\r",USBUART_1_GetDTERate(),(uint16)USBUART_1_GetDataBits());
                    sprintf(tx_buffer,"SB:%s,Parity:%s\n\r", stop[(uint16)USBUART_1_GetCharFormat()], \
                                                         parity[(uint16)USBUART_1_GetParityType()]);
                }
 
                if(state & USBUART_1_LINE_CONTROL_CHANGED)  /* Show new settings */
                {   
                    state = USBUART_1_GetLineControl();
                    sprintf(tx_buffer,"DTR:%s,RTS:%s\n\r",  (state & USBUART_1_LINE_CONTROL_DTR) ? "ON" : "OFF", \
                                                        (state & USBUART_1_LINE_CONTROL_RTS) ? "ON" : "OFF");
                    cnt_USB_UART_con++;
                    
                    if (cnt_USB_UART_con > 1)
                        break;
                }
            }
        }
    }   
}
 
cs


emFile_V322c.zip

sd_card_test.zip


휴, 안도의 한숨이 나옵니다.

일주일 가량의 시간이 걸려 SDIO(4Bit Bus Width, DMA) 로 micro-SD Card 읽기를 성공했네요.

속도는 약 8.23 MByte / s  정도 나오네요. 아마도 오실로 스코프 상에서는 좀 더 빨라서 9 MBte /s 에 더 가까울 것 같습니다.
DMA를 사용하지 않았을 때는 1.2 MByte / s 정도 나왔으니 8배 정도 빠를 것 같습니다.



검색해 보니, 현재 SD 카드가 엄청 느린 녀석이었는데 좀 더 좋은 것을 쓰면 속도가 더 나올 수 있는 것 같습니다.
어떤 사람은 19 MByte/s 까지 나온다고 합니다. (링크)



DMA 의 개념은, 보통 메모리에 있는 데이터를 읽어서 SPI/UART/SDIO 통신으로 내보내는 경우에 메모리에서 데이터를 읽는 시간이 상당이 걸리는데 그 시간을 줄이는 목적이 있습니다. 
다른 좋은 장점이 또 있을 것 같은데요, 일단은 제가 실감한 것이 그렇습니다.

예를 들자면 다음과 같은 그림으로 잘 설명이 될 것 같습니다.



실제로 제가 측정한 내용은 위와같이, 데이터를 읽는 시간이 SDIO로 보내는 시간 보다 엄청 걸렸습니다. 
위의 그림과 예전의 테스트 상황이 99.9%(?) 일치 했습니다. ^^

그림으로 보니 엄청 빨라질 것 같죠? (DMA를 쓰면 빨라 집니다. 확실히~)

DMA를 사용하려다가 몇가지 복병을 만났었는데, 소개해 볼 까 합니다.
(참고로 저는 Receive 동작만 테스트 했습니다)
1. DMA 인터럽트 우선순위를 설정하지 않아서 프로그램이 멈추는 문제 발생
   - DMA RX 인터럽트 추가한 후, 인터럽트 번호를 잘 설정해 줄 것.
2. DMA 로 f_read 함수를 실행 시, BLOCK_SIZE(512 Byte) 이상을 읽으면 Error 리턴.
   - Err 리턴을 무시할 것. (현재 프로그램을 정리하다 보니 이 현상이 없어 졌는데, 조금 더 두고 봐야 겠습니다)
3. 4gb 이상의 u-SD 카드 동작시 SD_read() 함수 입력 변수 계산 오류
   - SD_read() 함수 내의 BSP_SD_ReadBlocks() 함수의 입력 변수 계산 코드 수정.
     (아직 테스트는 못 해봄. 4gb 이상 sd-card 없음)

위 내용의 해결 과정에 앞서서, 일반 polling 방식을 DMA 방식으로 바꾸는 방법을 알아보겠습니다.
저는 거의 모든 STM32 시리즈의 SDIO,u-SD Card 문서 및 웹사이트를 다 돌아 다니면서 코드를 분석도 해 봤는데 별로 건지지 못했고, 노가다 테스트로 해결했습니다.

결론은.. 진짜, 쉽습니다. 

Cube 툴로 생성된 Keil 소스코드에서, Middlewares/FatFs 폴더의 sd_diskio.c 를 열어서 SD_read()함수의 내부 코드를 바꾸면 됩니다.
SD_read() 함수 내의 폴링 방식의 Read 함수인 BSP_SD_ReadBlocks 를 BSP_SD_ReadBlocks_DMA 로,
   단지 이름만 바꿔 주면 됩니다.





/**
  * @brief  Reads Sector(s)
  * @param  lun : not used
  * @param  *buff: Data buffer to store read data
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to read (1..128)
  * @retval DRESULT: Operation result
  */
DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
{
  DRESULT res = RES_OK;
  /*
  if(BSP_SD_ReadBlocks((uint32_t*)buff, 
                       (uint64_t) (sector * BLOCK_SIZE), 
                       BLOCK_SIZE, 
                       count) != MSD_OK)*/
/*
  if(BSP_SD_ReadBlocks_DMA((uint32_t*)buff, 
                       (uint64_t) (sector * BLOCK_SIZE), 
                       BLOCK_SIZE, 
                       count) != MSD_OK)*/
  if(BSP_SD_ReadBlocks_DMA((uint32_t*)buff, 
                       ((uint64_t)sector * BLOCK_SIZE)
                       BLOCK_SIZE, 
                       count) != MSD_OK)
  {
    res = RES_ERROR;
  }
  
  return res;
}

그럼 다음으로 제가 만난 복병과 그 해결 방법을 설명 드리겟습니다.
먼저 위에 그림에서 추가로 4gb이상의 sd Card를 읽을 때, 수정해야 하는 내용이 먼저 나와서,
4g sd-card가 없어서 아직 테스트는 못해 봤지만 코드의 수정 내용을 알려 드립니다.

[4gb 이상의 SD Card Access 시 수정할 사항]
현재 sd_diskio.c 파일 내에 있는 SD_read 함수 내부의 코드에서 
BSD_SD_ReadBlocks/BSD_SD_ReadBlocks_DMA 함수의 2번째 파라 메터의 계산 식이 틀려서 4gb 이상의 Sd card가 Access 되지 않는다고 합니다.
 (uint64_t) (sector * BLOCK_SIZE) 을 ((uint64_t) sector * BLOCK_SIZE)로 수정하면 된다고 하네요.

다음은 4gb sd card 이상의 동작에서 오류 수정을 당부하는 내용의 원문입니다. [링크]



[DMA로 수정한 상태에서 f_read 동작이 안되는 경우]
제가 처음에 SD_read 함수에서 BSP_SD_ReadBlocks() 함수를 BSP_SD_ReadBlocks_DMA()함수로 바꿨는데 프로그램이 멈춰버리더군요. 원인은 우연히 찾았는데, DMA 인터럽트 우선순위를 CUBE 툴에서 설정하지 않아서 우선순위가 0으로 되어 있었던 것을 Rx는 1, Tx는 2 로 수정을 했더니 프로그램이 정상 동작을 했습니다.
CUBE 툴에서 인터럽트 우선 순위는 따로 사용자가 결정을 해 줘야 하는 것 같습니다.

FRESULT 에러 리스트 (ff.h)
typedef enum {
FR_OK = 0, /* (0) Succeeded */
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
FR_INT_ERR, /* (2) Assertion failed */
FR_NOT_READY, /* (3) The physical drive cannot work */
FR_NO_FILE, /* (4) Could not find the file */
FR_NO_PATH, /* (5) Could not find the path */
FR_INVALID_NAME, /* (6) The path name format is invalid */
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
FR_EXIST, /* (8) Access denied due to prohibited access */
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
FR_NOT_ENABLED, /* (12) The volume has no work area */
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */
FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_SHARE */
FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
} FRESULT;



다시 2달이 지난 현재(2016.11.07) STM32F446 에서 테스트를 진행 하고 있는데, 
u-SD CARD 를 mount 할 경우에 FR_NO_FILESYSTEM 에러(13)가 나더니, 예전에 사용한 방식(DMA 인터럽트 우선순위를 바꾸는) 을 사용해도 에러가 났고, UART2 인터럽트 priority 를 6으로 했더니 에러가 없어졌습니다.
(그냥 복불복 입니다. 됭 때도 있고 안될 때도 있는....)

하아~ 그런데 에러가 없어 진 다음에 다시 DMA interrupt priority 를 rx,tx 각각 0으로 돌리고, UART interrupt priority도 0으로
복구했는데도 정상 동작을 합니다. 그리고 나서 전원을 껐다 키니까 처음에 FR_NO_FILESYSTEM 에러가 나네요.
ㅎ~~ 원인은 접촉 불량 내지 선이 너무 길어서 노이즈가 탄 것 같습니다. 현재 u-SD Card 와 Nucleo 보드간 케이블 길이가
약 10cm 정도 되서 노이즈가 좀 심하리라 생각이 드네요. 
따라서 인터럽트를 바꿔서 정상동작을 하고 안하고의 문제나 512 Byte 읽을 때 Error 가 발생하는 원인도 접촉 불량이나 노이즈에 의한 문제로 보입니다. 

아.. 정말정말 정확한 이유는 모르겠지만, 인터럽트 우선순위를 변경하는 것과 u-SD-Card 인식은 관계가 없는 것은 확실합니다.
됐다 안됐다 하는 문제는 접촉 불량과 노이즈인 것 같습니다. 

선을 반으로 줄여 봤어도, 에러가 자꾸 났는데 납땜 문제로 노이즈가 더 심해 졌을 지도 모르겠고..
다시 u-SD-Card 소켙을 바꾸고 긴 케이블을 연결하니 잘 되는 것을 보니 u-SD-Card 소켙의 접촉 불량이나
선 불량 으로 보이고,  통신 속도를 늦춰도 똑같은 현상이 나타나는 것을 보아 고속 통신으로 인한 데이터의 지연 문제는 아닌 것 같습니다.

그냥 아트웍할 때, 신경써서 하면 잘 될 것 같네요.

되긴 하나, 왠지 찝찝 하여.. 다시 껐다 켜고를 반복 하니 또 에러가 났습니다.
u-SD Card 쪽 핀헤더에 연결한 점퍼 와이어를 꾹 눌러주면 되고 띠면 안되는 것으로 보아,
콘넥터 접촉불량으로 결론을 냈습니다. 제가 혹시 혼란을 가중 시켰나 모르겠습니다.

[결론] 

u-SD Card 인터페이스 시, 접촉 불량 안나도록 배선을 잘하라. ^^





[DMA로 수정한 상태에서 BLOCK_SIZE(512 Byte) 이상을 읽으면, f_read() 함수의 return 값이 Error을 리턴한다]
위의 소제목과 같이 Error를 리턴 합니다만, 그냥 무시하고 쓰면 됩니다. 
원인을 찾아서 고치면 좋겠는데, 시간과 능력이 부족하여 저는 그냥 쓰기로 했습니다.
이상하게, 에러는 발생하지만 동작에는 문제가 없었습니다.
(현재 프로그램을 정리하다 보니 이 현상이 없어 졌는데, 조금 더 두고 봐야 겠습니다)

참고로, 인터넷에서 알랑방구 좀 뀌는 외국인의 글을 소개하자면..
u-SD Card 속도를 올리기 위해서는 다음같은 사항을 점검하라고 하네요.
1. 데이터 버스폭은 4Bit 로 했는지 검토.
2. 코드를 DMA로 바꿨는지 검토.
3. 클럭 스피드 검토(Cube 툴에서 기본 설정: 24MHz, 최대 48MHz)

^^   저는 이 분이, sd 카드 DMA에 대해서 알려 주는 것도 같지만.. 지가 한 내용을 가지고 속도 빠르다고 자랑하는 것 같아 보였어요. 소스 코드도 옛날 것이고 받은 코드도 파일이 좀 빠져 있고, 결국엔 아무 도움도 안됐네요.

다만, DMA를 사용하면 빨라진다는 정도만 소득으로 얻었습니다.(링크 주소)



마지막으로 제가 첨부하는 예제의 설명 입니다.
여러가지 테스트 함수들이 들어 있는데, 소스 코드 보시고 참고하시고.. 실제로 실행되는 함수는 read_BMP_file_DMA() 함수입니다.
이 함수 내부의 GPIO ON/OFF 코드는 오실로 스코프로 DMA 시간 테스트를 위해서 만들어 놓은 것이니 괜히 고민하지 마시길 바랍니다. 또한 __nop() 함수는 MCU가 100MHz 동작하니까, 오실로 스코프로 봐도 GPIO 변환 지점이 잘 안보여서 딜레이 대용으로 넣은 것이니 신경 쓰지 않으셔도 됩니다.

예제 코드에서는 64x64 BMP 파일을 DMA 읽어서 UART2 포트로 전부 뿌리도록 되어 있습니다.
UART 출력 결과는 다음과 같습니다.



아가~~ ㄱ ^^ 
정말로 중요한 내용이 빠졌네요.
f_read 에서 한 번에 많은 데이터를 읽을 때, 데이터 버퍼의 크기에 맞게 스택 사이즈 늘려주셔야 합니다.
이전에 다룬 내용을 참고 하시기 바랍니다 




제가 테스트한 보드에서 사용된 MCU의 핀맵입니다.



소스코드 첨부합니다.

sd_LEDSigne_v100_2.zip


STM32F4xx 의 SDIO 통신방식으로 u-SD Card 를 다뤄 보겠습니다.


여러가지 예제를 보고 그대로 했는데도 자꾸 실수를 하는 바람에,
동작 시키기까지 하루 종일 걸렸네요. ^^

SD 카드 모듈은 Waveshare 에서 만든 그냥 u-SD Card 모듈인데, 엘레파츠에서 샀습니다.



Nucleo 보드와 SD Card 모듈의 H/W 연결은 다음과 같습니다.



실제 연결한 그림입니다. 사진을 잘 못 찍어서 이렇네요. ^^



SDIO의 큐브에서 설정은 다음과 같습니다. SDIO 를 넣었더니 clock 설정에서 APB1 peripheral clocks(MHz) 가 42MHz 이하로 줄여야 한다고 에러가 나서 42MHz로 맞추다 보니 시스템 클럭은 84MHz로 줄어 버렸습니다.



SDIO 4Bit 핀아웃은 다음과 같습니다. 데이터버스 4개,CLK,CMD 로 총 6개의 I/O 가 사용되고 경우에 따라서 CD(Card Detect) 입력을 1개 쓸 수도 있습니다. 일단은 테스트니까, CD는 사용 안했습니다.



프로그램은 그냥 Polling으로 처리했습니다. 나중에 DMA를 사용하면 아주 빠르다고 하네요.
프로그램은 
1. SD card 초기화하고, 
2. 텍스트 파일을 1개 만들어서 , 
3. 파일에 스트링을 몇자 쓰고 ,
4. 파일을 닫아서,
5. PC에서 SD Card를 읽어서 확인해 봤습니다.

다른 것은 별 어려움이 없었는데, 4Bit Wire로 바꾸는 것이 좀 어려웠습니다.
어딘가에서 바뀔 수도 있는지 모르겠는데, BSP_SD_Init(); 함수를 실행해야 4비트 방식으로 바뀌는 것인데..
프로그램 어디에도 BSP_SD_Init();함수를 실행하지를 않아서 SD Card 초기화 하고 나서 main()함수안에 BSP_SD_Init(); 함수를 실행하도록 수정했습니다.

int main(void)
{

  /* USER CODE BEGIN 1 */
    uint8_t i;
    char tx_buf[100];
    uint8_t file_path[13] = "testfile.txt";
    file_path[12] = '\0';
    uint8_t testBuffer[16] = "SD write success";
    UINT testBytes;
  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_TIM3_Init();
  MX_USART2_UART_Init();
  MX_I2C1_Init();
  MX_SDIO_SD_Init();
  MX_FATFS_Init();

  /* Initialize interrupts */
  MX_NVIC_Init();

  /* USER CODE BEGIN 2 */
for (i=0;i<10;i++)
{
sprintf(tx_buf,"UART Test ... [%3d] \n\r",i);
HAL_UART_Transmit(&huart2,(uint8_t *)tx_buf,cus_Size_of(tx_buf),3);
}
HAL_TIM_Base_Start_IT(&htim3);

BSP_SD_Init();

   :
   :
   :
}




다음은 이 프로그램에 의해 UART로 response 를 출력해 본 내용입니다. (제가 디버그용으로 UART를 주로 쓰는 편이라..)
response 가 0 이면 정상입니다.



다음은 SD card 내용과 그안에 만들어진 텍스트 파일의 내용입니다.





소스파일 첨부 합니다.


+ Recent posts