List of commits:
Subject Hash Author Date (UTC)
add sd_card demo 3d8cac0ad026a435cd03daa783978d94b6f98c1c zhanzr 2018-08-14 17:53:37
simple wifi demo 3b49d1710d647028a95144d22eaf92ead994cd4f zhanzr 2018-08-04 18:33:49
maintaince eab2557c1c0fbfb1df5b8b38af229b810be691fc zhanzr 2018-08-04 18:08:39
maintaince 9735c9025f877485b7fba7d68cee37342eb2bc41 zhanzr 2018-08-04 18:07:52
maintaince 1045165526407dec48797d66751a06dbe94257fb zhanzr 2018-08-04 18:06:59
hellow word, printf from serial port. 6818f08e563aefc70c9bc9ef18852149e080d237 zhanzr 2018-08-04 18:03:27
blink demo. bc35d066c34d62eb527f29af81a9907a960e23bd zhanzr 2018-08-04 17:34:03
Commit 3d8cac0ad026a435cd03daa783978d94b6f98c1c - add sd_card demo
Author: zhanzr
Author date (UTC): 2018-08-14 17:53
Committer name: zhanzr
Committer date (UTC): 2018-08-14 17:53
Parent(s): e341a5df3bf07be9a19d6fcdb00cf093a35159af
Signing key:
Tree: a51f0595c8540476f570075407b4b57d358928b4
File Lines added Lines deleted
sd_card/Makefile 1 1
sd_card/README.md 102 0
sd_card/main/component.mk 0 0
sd_card/main/sd_card_example_main.c 155 0
sd_card/sdkconfig 0 0
sd_card/sdkconfig.old 0 5
File sd_card/Makefile copied from file blink/Makefile (similarity 86%) (mode: 100644) (index 6f8b9fa..512e40f)
3 3 # project subdirectory. # project subdirectory.
4 4 # #
5 5
6 PROJECT_NAME := blink
6 PROJECT_NAME := sd_card
7 7
8 8 include $(IDF_PATH)/make/project.mk include $(IDF_PATH)/make/project.mk
9 9
File sd_card/README.md added (mode: 100644) (index 0000000..697baed)
1 # SD Card example
2
3 This example demonstrates how to use an SD card with ESP32. Example does the following steps:
4
5 1. Use an "all-in-one" `esp_vfs_fat_sdmmc_mount` function to:
6 - initialize SDMMC peripheral,
7 - probe and initialize the card connected to SD/MMC slot 1 (HS2_CMD, HS2_CLK, HS2_D0, HS2_D1, HS2_D2, HS2_D3 lines),
8 - mount FAT filesystem using FATFS library (and format card, if the filesystem can not be mounted),
9 - register FAT filesystem in VFS, enabling C standard library and POSIX functions to be used.
10 2. Print information about the card, such as name, type, capacity, and maximum supported frequency.
11 3. Create a file using `fopen` and write to it using `fprintf`.
12 4. Rename the file. Before renaming, check if destination file already exists using `stat` function, and remove it using `unlink` function.
13 5. Open renamed file for reading, read back the line, and print it to the terminal.
14
15 *Note:* despite the name, `sdmmc` component doesn't support MMC/eMMC cards yet. It is also possible to extend `sdmmc` component to support SPI mode with SD cards via SPI peripheral.
16
17 ## Hardware
18
19 To run this example, ESP32 development board needs to be connected to SD card as follows:
20
21 ESP32 pin | SD card pin | SPI pin | Notes
22 --------------|-------------|---------|------------
23 GPIO14 (MTMS) | CLK | SCK | 10k pullup in SD mode
24 GPIO15 (MTDO) | CMD | MOSI | 10k pullup, both in SD and SPI modes
25 GPIO2 | D0 | MISO | 10k pullup in SD mode, pull low to go into download mode (see note below!)
26 GPIO4 | D1 | N/C | not used in 1-line SD mode; 10k pullup in 4-line SD mode
27 GPIO12 (MTDI) | D2 | N/C | not used in 1-line SD mode; 10k pullup in 4-line SD mode (see note below!)
28 GPIO13 (MTCK) | D3 | CS | not used in 1-line SD mode, but card's D3 pin must have a 10k pullup
29 N/C | CD | | optional, not used in the example
30 N/C | WP | | optional, not used in the example
31
32 This example doesn't utilize card detect (CD) and write protect (WP) signals from SD card slot.
33
34 With the given pinout for SPI mode, same connections between the SD card and ESP32 can be used to test both SD and SPI modes, provided that the appropriate pullups are in place.
35 See document `sd_pullup_requirements.rst` in `docs/en/api-reference/peripherals/` for more details about pullup support and compatiblities about modules and devkits.
36
37 In SPI mode, pins can be customized. See the initialization of ``sdspi_slot_config_t`` structure in the example code.
38
39 ### Note about GPIO2
40
41 GPIO2 pin is used as a bootstrapping pin, and should be low to enter UART download mode. One way to do this is to connect GPIO0 and GPIO2 using a jumper, and then the auto-reset circuit on most development boards will pull GPIO2 low along with GPIO0, when entering download mode.
42
43 - Some boards have pulldown and/or LED on GPIO2. LED is usually ok, but pulldown will interfere with D0 signals and must be removed. Check the schematic of your development board for anything connected to GPIO2.
44
45 ### Note about GPIO12
46
47 GPIO12 is used as a bootstrapping pin to select output voltage of an internal regulator which powers the flash chip (VDD_SDIO). This pin has an internal pulldown so if left unconnected it will read low at reset (selecting default 3.3V operation). When adding a pullup to this pin for SD card operation, consider the following:
48
49 - For boards which don't use the internal regulator (VDD_SDIO) to power the flash, GPIO12 can be pulled high.
50 - For boards which use 1.8V flash chip, GPIO12 needs to be pulled high at reset. This is fully compatible with SD card operation.
51 - On boards which use the internal regulator and a 3.3V flash chip, GPIO12 must be low at reset. This is incompatible with SD card operation.
52 * In most cases, external pullup can be omitted and an internal pullup can be enabled using a `gpio_pullup_en(GPIO_NUM_12);` call. Most SD cards work fine when an internal pullup on GPIO12 line is enabled. Note that if ESP32 experiences a power-on reset while the SD card is sending data, high level on GPIO12 can be latched into the bootstrapping register, and ESP32 will enter a boot loop until external reset with correct GPIO12 level is applied.
53 * Another option is to burn the flash voltage selection efuses. This will permanently select 3.3V output voltage for the internal regulator, and GPIO12 will not be used as a bootstrapping pin. Then it is safe to connect a pullup resistor to GPIO12. This option is suggested for production use.
54
55 The following command can be used to program flash voltage selection efuses **to 3.3V**:
56
57 ```sh
58 components/esptool_py/esptool/espefuse.py set_flash_voltage 3.3V
59 ```
60
61 This command will burn the `XPD_SDIO_TIEH`, `XPD_SDIO_FORCE`, and `XPD_SDIO_REG` efuses. With all three burned to value 1, the internal VDD_SDIO flash voltage regulator is permanently enabled at 3.3V. See the technical reference manual for more details.
62
63 `espefuse.py` has a `--do-not-confirm` option if running from an automated flashing script.
64
65 ## 4-line and 1-line modes
66
67 By default, example code uses the following initializer for SDMMC host peripheral configuration:
68
69 ```c++
70 sdmmc_host_t host = SDMMC_HOST_DEFAULT();
71 ```
72
73 Among other things, this sets `host.flags` to `SDMMC_HOST_FLAG_4BIT`, which means that SD/MMC driver will switch to 4-line mode when initializing the card (initial communication always happens in 1-line mode). If some of the card's D1, D2, D3 pins are not connected to the ESP32, set `host.flags` to `SDMMC_HOST_FLAG_1BIT` — then the SD/MMC driver will not attempt to switch to 4-line mode.
74 Note that even if card's D3 line is not connected to the ESP32, it still has to be pulled up, otherwise the card will go into SPI protocol mode.
75
76 ## SPI mode
77
78 By default, the example uses SDMMC Host peripheral to access SD card. To use SPI peripheral instead, uncomment ``#define USE_SPI_MODE`` in the example code.
79
80 ## Example output
81
82 Here is an example console output. In this case a 128MB SDSC card was connected, and `format_if_mount_failed` parameter was set to `true` in the source code. Card was unformatted, so the initial mount has failed. Card was then partitioned, formatted, and mounted again.
83
84 ```
85 I (1776) example: Initializing SD card
86 W (1856) vfs_fat_sdmmc: failed to mount card (13)
87 W (1856) vfs_fat_sdmmc: partitioning card
88 W (1856) vfs_fat_sdmmc: formatting card
89 W (2726) vfs_fat_sdmmc: mounting again
90 I (2736) example: Card info:
91 I (2736) example: Name: SU128
92 I (2736) example: Type: SDSC
93 I (2736) example: Capacity: 120 MB
94 I (2736) example: Max clock speed: 25 MHz
95 I (2736) example: Opening file
96 I (2756) example: File written
97 I (2756) example: Renaming file
98 I (2756) example: Reading file
99 I (2756) example: Read from file: 'Hello SU128!'
100 I (2756) example: Card unmounted
101 ```
102
File sd_card/main/component.mk copied from file blink/main/component.mk (similarity 100%)
File sd_card/main/sd_card_example_main.c added (mode: 100644) (index 0000000..ed35b64)
1 /* SD card and FAT filesystem example.
2 This example code is in the Public Domain (or CC0 licensed, at your option.)
3
4 Unless required by applicable law or agreed to in writing, this
5 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6 CONDITIONS OF ANY KIND, either express or implied.
7 */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/unistd.h>
12 #include <sys/stat.h>
13 #include "esp_err.h"
14 #include "esp_log.h"
15 #include "esp_vfs_fat.h"
16 #include "driver/sdmmc_host.h"
17 #include "driver/sdspi_host.h"
18 #include "sdmmc_cmd.h"
19
20 static const char *TAG = "example";
21
22 // This example can use SDMMC and SPI peripherals to communicate with SD card.
23 // By default, SDMMC peripheral is used.
24 // To enable SPI mode, uncomment the following line:
25
26 // #define USE_SPI_MODE
27
28 // When testing SD and SPI modes, keep in mind that once the card has been
29 // initialized in SPI mode, it can not be reinitialized in SD mode without
30 // toggling power to the card.
31
32 #ifdef USE_SPI_MODE
33 // Pin mapping when using SPI mode.
34 // With this mapping, SD card can be used both in SPI and 1-line SD mode.
35 // Note that a pull-up on CS line is required in SD mode.
36 #define PIN_NUM_MISO 2
37 #define PIN_NUM_MOSI 15
38 #define PIN_NUM_CLK 14
39 #define PIN_NUM_CS 13
40 #endif //USE_SPI_MODE
41
42 void app_main(void)
43 {
44 ESP_LOGI(TAG, "Initializing SD card");
45
46 #ifndef USE_SPI_MODE
47 ESP_LOGI(TAG, "Using SDMMC peripheral");
48 sdmmc_host_t host = SDMMC_HOST_DEFAULT();
49
50 // To use 1-line SD mode, uncomment the following line:
51 // host.flags = SDMMC_HOST_FLAG_1BIT;
52
53 // This initializes the slot without card detect (CD) and write protect (WP) signals.
54 // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
55 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
56
57 // GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
58 // Internal pull-ups are not sufficient. However, enabling internal pull-ups
59 // does make a difference some boards, so we do that here.
60 gpio_set_pull_mode(15, GPIO_PULLUP_ONLY); // CMD, needed in 4- and 1- line modes
61 gpio_set_pull_mode(2, GPIO_PULLUP_ONLY); // D0, needed in 4- and 1-line modes
62 gpio_set_pull_mode(4, GPIO_PULLUP_ONLY); // D1, needed in 4-line mode only
63 gpio_set_pull_mode(12, GPIO_PULLUP_ONLY); // D2, needed in 4-line mode only
64 gpio_set_pull_mode(13, GPIO_PULLUP_ONLY); // D3, needed in 4- and 1-line modes
65
66 #else
67 ESP_LOGI(TAG, "Using SPI peripheral");
68
69 sdmmc_host_t host = SDSPI_HOST_DEFAULT();
70 sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
71 slot_config.gpio_miso = PIN_NUM_MISO;
72 slot_config.gpio_mosi = PIN_NUM_MOSI;
73 slot_config.gpio_sck = PIN_NUM_CLK;
74 slot_config.gpio_cs = PIN_NUM_CS;
75 // This initializes the slot without card detect (CD) and write protect (WP) signals.
76 // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
77 #endif //USE_SPI_MODE
78
79 // Options for mounting the filesystem.
80 // If format_if_mount_failed is set to true, SD card will be partitioned and
81 // formatted in case when mounting fails.
82 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
83 .format_if_mount_failed = false,
84 .max_files = 5,
85 .allocation_unit_size = 16 * 1024
86 };
87
88 // Use settings defined above to initialize SD card and mount FAT filesystem.
89 // Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
90 // Please check its source code and implement error recovery when developing
91 // production applications.
92 sdmmc_card_t* card;
93 esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
94
95 if (ret != ESP_OK) {
96 if (ret == ESP_FAIL) {
97 ESP_LOGE(TAG, "Failed to mount filesystem. "
98 "If you want the card to be formatted, set format_if_mount_failed = true.");
99 } else {
100 ESP_LOGE(TAG, "Failed to initialize the card (%s). "
101 "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
102 }
103 return;
104 }
105
106 // Card has been initialized, print its properties
107 sdmmc_card_print_info(stdout, card);
108
109 // Use POSIX and C standard library functions to work with files.
110 // First create a file.
111 ESP_LOGI(TAG, "Opening file");
112 FILE* f = fopen("/sdcard/hello.txt", "w");
113 if (f == NULL) {
114 ESP_LOGE(TAG, "Failed to open file for writing");
115 return;
116 }
117 fprintf(f, "Hello %s!\n", card->cid.name);
118 fclose(f);
119 ESP_LOGI(TAG, "File written");
120
121 // Check if destination file exists before renaming
122 struct stat st;
123 if (stat("/sdcard/foo.txt", &st) == 0) {
124 // Delete it if it exists
125 unlink("/sdcard/foo.txt");
126 }
127
128 // Rename original file
129 ESP_LOGI(TAG, "Renaming file");
130 if (rename("/sdcard/hello.txt", "/sdcard/foo.txt") != 0) {
131 ESP_LOGE(TAG, "Rename failed");
132 return;
133 }
134
135 // Open renamed file for reading
136 ESP_LOGI(TAG, "Reading file");
137 f = fopen("/sdcard/foo.txt", "r");
138 if (f == NULL) {
139 ESP_LOGE(TAG, "Failed to open file for reading");
140 return;
141 }
142 char line[64];
143 fgets(line, sizeof(line), f);
144 fclose(f);
145 // strip newline
146 char* pos = strchr(line, '\n');
147 if (pos) {
148 *pos = '\0';
149 }
150 ESP_LOGI(TAG, "Read from file: '%s'", line);
151
152 // All done, unmount partition and disable SDMMC or SPI peripheral
153 esp_vfs_fat_sdmmc_unmount();
154 ESP_LOGI(TAG, "Card unmounted");
155 }
File sd_card/sdkconfig copied from file hello_world/sdkconfig (similarity 100%)
File sd_card/sdkconfig.old copied from file blink/sdkconfig.old (similarity 99%) (mode: 100644) (index 8b70102..3b8ebee)
... ... CONFIG_MONITOR_BAUD_OTHER=
76 76 CONFIG_MONITOR_BAUD_OTHER_VAL=115200 CONFIG_MONITOR_BAUD_OTHER_VAL=115200
77 77 CONFIG_MONITOR_BAUD=115200 CONFIG_MONITOR_BAUD=115200
78 78
79 #
80 # Example Configuration
81 #
82 CONFIG_BLINK_GPIO=5
83
84 79 # #
85 80 # Partition Table # Partition Table
86 81 # #
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/zhanzr/esp32_radio_demo

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/zhanzr/esp32_radio_demo

Clone this repository using git:
git clone git://git.rocketgit.com/user/zhanzr/esp32_radio_demo

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main