{"id":5384,"date":"2017-06-08T01:40:09","date_gmt":"2017-06-07T16:40:09","guid":{"rendered":"https:\/\/blog.eldhrimnir.com\/?p=5384"},"modified":"2017-06-08T01:40:09","modified_gmt":"2017-06-07T16:40:09","slug":"esp-wroom-32%e9%96%8b%e7%99%ba%e3%83%9c%e3%83%bc%e3%83%89microsd%e3%81%a7sd%e3%82%ab%e3%83%bc%e3%83%89%e3%82%92%e8%aa%ad%e3%82%80","status":"publish","type":"post","link":"https:\/\/blog.eldhrimnir.com\/?p=5384","title":{"rendered":"ESP-WROOM-32\u958b\u767a\u30dc\u30fc\u30c9+microSD\u3067SD\u30ab\u30fc\u30c9\u3092\u8aad\u3080"},"content":{"rendered":"<p>\u304a\u554f\u3044\u5408\u308f\u305b\u3092\u3044\u305f\u3060\u3044\u305f\u308a\u3057\u307e\u3057\u305f\u306e\u3067\u3001SD\u30ab\u30fc\u30c9\u306e\u8aad\u307f\u53d6\u308a\u306b\u3064\u3044\u3066\u66f8\u3044\u3066\u304a\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002\u3055\u3066\u3055\u3066\u300cESP-WROOM-32\u958b\u767a\u30dc\u30fc\u30c9+microSD\u300d\u3067\u306fSD\u30ab\u30fc\u30c9\u30926bit\u306eHS2\u3092\u4f7f\u3063\u3066\u63a5\u7d9a\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002\u305d\u3057\u3066\u3001SD\u30ab\u30fc\u30c9\u306e\u8a8d\u8b58\u3068\u3044\u3046\u304b\u4ed5\u69d8\u306b\u306f2\u7a2e\u985e\u3042\u308a\u307e\u3057\u3066Arduino\u3067\u4e00\u822c\u7684\u306b\u4f7f\u308f\u308c\u3066\u3044\u308bSPI\u30e2\u30fc\u30c9\u3068\u306f\u63a5\u7d9a\u65b9\u6cd5\u304c\u9055\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/blog.eldhrimnir.com\/?attachment_id=5390\" rel=\"attachment wp-att-5390\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-5390\" src=\"https:\/\/blog.eldhrimnir.com\/wp-content\/uploads\/2017\/06\/IMG_2996-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\" srcset=\"https:\/\/blog.eldhrimnir.com\/wp-content\/uploads\/2017\/06\/IMG_2996-300x225.jpg 300w, https:\/\/blog.eldhrimnir.com\/wp-content\/uploads\/2017\/06\/IMG_2996-768x576.jpg 768w, https:\/\/blog.eldhrimnir.com\/wp-content\/uploads\/2017\/06\/IMG_2996-1024x768.jpg 1024w, https:\/\/blog.eldhrimnir.com\/wp-content\/uploads\/2017\/06\/IMG_2996.jpg 1280w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p style=\"padding-left: 30px;\">SPI\u30e2\u30fc\u30c9<br \/>\n* DATA3\/SS<br \/>\n* CMD\/MOSI<br \/>\n* CLK\/SCK<br \/>\n* DATA0\/MISO<\/p>\n<p style=\"padding-left: 30px;\">\u2605HS\u30e2\u30fc\u30c9\uff08ESP-WROOM-32\u958b\u767a\u30dc\u30fc\u30c9+microSD\u3067\u4f7f\u7528\uff09<br \/>\n* DATA0\/D2<br \/>\n* DATA1\/D4<br \/>\n* DATA2\/D12<br \/>\n* DATA3\/D13<br \/>\n* CMD\/D15<br \/>\n* CLK\/D14<\/p>\n<p>\u25a0\u8a8d\u8b58\u6642\u306e\u30b3\u30f3\u30bd\u30fc\u30eb\u753b\u9762<\/p>\n<p><a href=\"https:\/\/blog.eldhrimnir.com\/?attachment_id=5160\" rel=\"attachment wp-att-5160\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-5160\" src=\"https:\/\/blog.eldhrimnir.com\/wp-content\/uploads\/2017\/03\/52f5be27636826c10b1102844b928434-271x300.png\" alt=\"\" width=\"271\" height=\"300\" srcset=\"https:\/\/blog.eldhrimnir.com\/wp-content\/uploads\/2017\/03\/52f5be27636826c10b1102844b928434-271x300.png 271w, https:\/\/blog.eldhrimnir.com\/wp-content\/uploads\/2017\/03\/52f5be27636826c10b1102844b928434-768x851.png 768w, https:\/\/blog.eldhrimnir.com\/wp-content\/uploads\/2017\/03\/52f5be27636826c10b1102844b928434-924x1024.png 924w, https:\/\/blog.eldhrimnir.com\/wp-content\/uploads\/2017\/03\/52f5be27636826c10b1102844b928434.png 962w\" sizes=\"(max-width: 271px) 100vw, 271px\" \/><\/a><!--more--><\/p>\n<p>\u25a0\u4f7f\u7528\u3059\u308b\u30b3\u30fc\u30c9(SDMMC_Test)<\/p>\n<p>[cpp]<\/p>\n<p>\/*<br \/>\n* Connect the SD card to the following pins:<br \/>\n*<br \/>\n* SD Card | ESP32<br \/>\n* D2 12<br \/>\n* D3 13<br \/>\n* CMD 15<br \/>\n* VSS GND<br \/>\n* VDD 3.3V<br \/>\n* CLK 14<br \/>\n* VSS GND<br \/>\n* D0 2 (add 1K pull up after flashing)<br \/>\n* D1 4<br \/>\n*\/<\/p>\n<p>#include &quot;FS.h&quot;<br \/>\n#include &quot;SD_MMC.h&quot;<\/p>\n<p>void listDir(fs::FS &amp;fs, const char * dirname, uint8_t levels){<br \/>\nSerial.printf(&quot;Listing directory: %s\\n&quot;, dirname);<\/p>\n<p>File root = fs.open(dirname);<br \/>\nif(!root){<br \/>\nSerial.println(&quot;Failed to open directory&quot;);<br \/>\nreturn;<br \/>\n}<br \/>\nif(!root.isDirectory()){<br \/>\nSerial.println(&quot;Not a directory&quot;);<br \/>\nreturn;<br \/>\n}<\/p>\n<p>File file = root.openNextFile();<br \/>\nwhile(file){<br \/>\nif(file.isDirectory()){<br \/>\nSerial.print(&quot; DIR : &quot;);<br \/>\nSerial.println(file.name());<br \/>\nif(levels){<br \/>\nlistDir(fs, file.name(), levels -1);<br \/>\n}<br \/>\n} else {<br \/>\nSerial.print(&quot; FILE: &quot;);<br \/>\nSerial.print(file.name());<br \/>\nSerial.print(&quot; SIZE: &quot;);<br \/>\nSerial.println(file.size());<br \/>\n}<br \/>\nfile = root.openNextFile();<br \/>\n}<br \/>\n}<\/p>\n<p>void createDir(fs::FS &amp;fs, const char * path){<br \/>\nSerial.printf(&quot;Creating Dir: %s\\n&quot;, path);<br \/>\nif(fs.mkdir(path)){<br \/>\nSerial.println(&quot;Dir created&quot;);<br \/>\n} else {<br \/>\nSerial.println(&quot;mkdir failed&quot;);<br \/>\n}<br \/>\n}<\/p>\n<p>void removeDir(fs::FS &amp;fs, const char * path){<br \/>\nSerial.printf(&quot;Removing Dir: %s\\n&quot;, path);<br \/>\nif(fs.rmdir(path)){<br \/>\nSerial.println(&quot;Dir removed&quot;);<br \/>\n} else {<br \/>\nSerial.println(&quot;rmdir failed&quot;);<br \/>\n}<br \/>\n}<\/p>\n<p>void readFile(fs::FS &amp;fs, const char * path){<br \/>\nSerial.printf(&quot;Reading file: %s\\n&quot;, path);<\/p>\n<p>File file = fs.open(path);<br \/>\nif(!file){<br \/>\nSerial.println(&quot;Failed to open file for reading&quot;);<br \/>\nreturn;<br \/>\n}<\/p>\n<p>Serial.print(&quot;Read from file: &quot;);<br \/>\nwhile(file.available()){<br \/>\nSerial.write(file.read());<br \/>\n}<br \/>\n}<\/p>\n<p>void writeFile(fs::FS &amp;fs, const char * path, const char * message){<br \/>\nSerial.printf(&quot;Writing file: %s\\n&quot;, path);<\/p>\n<p>File file = fs.open(path, FILE_WRITE);<br \/>\nif(!file){<br \/>\nSerial.println(&quot;Failed to open file for writing&quot;);<br \/>\nreturn;<br \/>\n}<br \/>\nif(file.print(message)){<br \/>\nSerial.println(&quot;File written&quot;);<br \/>\n} else {<br \/>\nSerial.println(&quot;Write failed&quot;);<br \/>\n}<br \/>\n}<\/p>\n<p>void appendFile(fs::FS &amp;fs, const char * path, const char * message){<br \/>\nSerial.printf(&quot;Appending to file: %s\\n&quot;, path);<\/p>\n<p>File file = fs.open(path, FILE_APPEND);<br \/>\nif(!file){<br \/>\nSerial.println(&quot;Failed to open file for appending&quot;);<br \/>\nreturn;<br \/>\n}<br \/>\nif(file.print(message)){<br \/>\nSerial.println(&quot;Message appended&quot;);<br \/>\n} else {<br \/>\nSerial.println(&quot;Append failed&quot;);<br \/>\n}<br \/>\n}<\/p>\n<p>void renameFile(fs::FS &amp;fs, const char * path1, const char * path2){<br \/>\nSerial.printf(&quot;Renaming file %s to %s\\n&quot;, path1, path2);<br \/>\nif (fs.rename(path1, path2)) {<br \/>\nSerial.println(&quot;File renamed&quot;);<br \/>\n} else {<br \/>\nSerial.println(&quot;Rename failed&quot;);<br \/>\n}<br \/>\n}<\/p>\n<p>void deleteFile(fs::FS &amp;fs, const char * path){<br \/>\nSerial.printf(&quot;Deleting file: %s\\n&quot;, path);<br \/>\nif(fs.remove(path)){<br \/>\nSerial.println(&quot;File deleted&quot;);<br \/>\n} else {<br \/>\nSerial.println(&quot;Delete failed&quot;);<br \/>\n}<br \/>\n}<\/p>\n<p>void testFileIO(fs::FS &amp;fs, const char * path){<br \/>\nFile file = fs.open(path);<br \/>\nstatic uint8_t buf[512];<br \/>\nsize_t len = 0;<br \/>\nuint32_t start = millis();<br \/>\nuint32_t end = start;<br \/>\nif(file){<br \/>\nlen = file.size();<br \/>\nsize_t flen = len;<br \/>\nstart = millis();<br \/>\nwhile(len){<br \/>\nsize_t toRead = len;<br \/>\nif(toRead &gt; 512){<br \/>\ntoRead = 512;<br \/>\n}<br \/>\nfile.read(buf, toRead);<br \/>\nlen -= toRead;<br \/>\n}<br \/>\nend = millis() &#8211; start;<br \/>\nSerial.printf(&quot;%u bytes read for %u ms\\n&quot;, flen, end);<br \/>\nfile.close();<br \/>\n} else {<br \/>\nSerial.println(&quot;Failed to open file for reading&quot;);<br \/>\n}<br \/>\nfile = fs.open(path, FILE_WRITE);<br \/>\nif(!file){<br \/>\nSerial.println(&quot;Failed to open file for writing&quot;);<br \/>\nreturn;<br \/>\n}<\/p>\n<p>size_t i;<br \/>\nstart = millis();<br \/>\nfor(i=0; i&lt;2048; i++){<br \/>\nfile.write(buf, 512);<br \/>\n}<br \/>\nend = millis() &#8211; start;<br \/>\nSerial.printf(&quot;%u bytes written for %u ms\\n&quot;, 2048 * 512, end);<br \/>\nfile.close();<br \/>\n}<\/p>\n<p>void setup(){<br \/>\nSerial.begin(115200);<br \/>\nif(!SD_MMC.begin()){<br \/>\nSerial.println(&quot;Card Mount Failed&quot;);<br \/>\nreturn;<br \/>\n}<br \/>\nuint8_t cardType = SD_MMC.cardType();<\/p>\n<p>if(cardType == CARD_NONE){<br \/>\nSerial.println(&quot;No SD_MMC card attached&quot;);<br \/>\nreturn;<br \/>\n}<\/p>\n<p>Serial.print(&quot;SD_MMC Card Type: &quot;);<br \/>\nif(cardType == CARD_MMC){<br \/>\nSerial.println(&quot;MMC&quot;);<br \/>\n} else if(cardType == CARD_SD){<br \/>\nSerial.println(&quot;SDSC&quot;);<br \/>\n} else if(cardType == CARD_SDHC){<br \/>\nSerial.println(&quot;SDHC&quot;);<br \/>\n} else {<br \/>\nSerial.println(&quot;UNKNOWN&quot;);<br \/>\n}<\/p>\n<p>uint64_t cardSize = SD_MMC.cardSize() \/ (1024 * 1024);<br \/>\nSerial.printf(&quot;SD_MMC Card Size: %lluMB\\n&quot;, cardSize);<\/p>\n<p>listDir(SD_MMC, &quot;\/&quot;, 0);<br \/>\ncreateDir(SD_MMC, &quot;\/mydir&quot;);<br \/>\nlistDir(SD_MMC, &quot;\/&quot;, 0);<br \/>\nremoveDir(SD_MMC, &quot;\/mydir&quot;);<br \/>\nlistDir(SD_MMC, &quot;\/&quot;, 2);<br \/>\nwriteFile(SD_MMC, &quot;\/hello.txt&quot;, &quot;Hello &quot;);<br \/>\nappendFile(SD_MMC, &quot;\/hello.txt&quot;, &quot;World!\\n&quot;);<br \/>\nreadFile(SD_MMC, &quot;\/hello.txt&quot;);<br \/>\ndeleteFile(SD_MMC, &quot;\/foo.txt&quot;);<br \/>\nrenameFile(SD_MMC, &quot;\/hello.txt&quot;, &quot;\/foo.txt&quot;);<br \/>\nreadFile(SD_MMC, &quot;\/foo.txt&quot;);<br \/>\ntestFileIO(SD_MMC, &quot;\/test.txt&quot;);<br \/>\n}<\/p>\n<p>void loop(){<\/p>\n<p>}<\/p>\n<p>[\/cpp]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u304a\u554f\u3044\u5408\u308f\u305b\u3092\u3044\u305f\u3060\u3044\u305f\u308a\u3057\u307e\u3057\u305f\u306e\u3067\u3001SD\u30ab\u30fc\u30c9\u306e\u8aad\u307f\u53d6\u308a\u306b\u3064\u3044\u3066\u66f8\u3044\u3066\u304a\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002\u3055\u3066\u3055\u3066\u300cESP-WROOM-32\u958b\u767a\u30dc\u30fc\u30c9+microSD\u300d\u3067\u306fSD\u30ab\u30fc\u30c9\u30926bit\u306eHS2\u3092\u4f7f\u3063\u3066\u63a5\u7d9a\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002\u305d\u3057\u3066 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,16],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.eldhrimnir.com\/index.php?rest_route=\/wp\/v2\/posts\/5384"}],"collection":[{"href":"https:\/\/blog.eldhrimnir.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.eldhrimnir.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.eldhrimnir.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.eldhrimnir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5384"}],"version-history":[{"count":0,"href":"https:\/\/blog.eldhrimnir.com\/index.php?rest_route=\/wp\/v2\/posts\/5384\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.eldhrimnir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.eldhrimnir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.eldhrimnir.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}