Ver código fonte

Add SD card configuration and update CMakeLists for fatfs support

dacowars 2 semanas atrás
pai
commit
a12efa27e1
3 arquivos alterados com 141 adições e 8 exclusões
  1. 2 1
      main/CMakeLists.txt
  2. 37 0
      main/Kconfig.projbuild
  3. 102 7
      main/main.c

+ 2 - 1
main/CMakeLists.txt

@@ -1,3 +1,4 @@
 idf_component_register(SRCS "main.c"
                     INCLUDE_DIRS "."
-                    REQUIRES driver dht22 esp_driver_uart GPS_parser)
+                    REQUIRES driver dht22 esp_driver_uart GPS_parser fatfs
+                    WHOLE_ARCHIVE)

+ 37 - 0
main/Kconfig.projbuild

@@ -0,0 +1,37 @@
+menu "SD SPI Example Configuration"
+
+    orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps"
+
+    config PIN_MISO
+        int "MISO pin configuration"
+        default 19
+        range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
+        help 
+            GPIO number where the SD card MISO pin is connected.
+            Make sure the pin supports input/output and has pull-up if needed.
+
+    config PIN_MOSI
+        int "MOSI pin configuration"
+        default 23
+        range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
+        help 
+            GPIO number where the SD card MOSI pin is connected.
+            Make sure the pin supports input/output and has pull-up if needed.
+    
+    config PIN_CLK
+        int "CLK pin configuration"
+        default 18
+        range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
+        help 
+            GPIO number where the SD card CLK pin is connected.
+            Make sure the pin supports input/output and has pull-up if needed.
+    
+    config PIN_CS
+        int "CS pin configuration"
+        default 5
+        range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
+        help 
+            GPIO number where the SD card CS pin is connected.
+            Make sure the pin supports input/output and has pull-up if needed.
+
+endmenu

+ 102 - 7
main/main.c

@@ -7,10 +7,22 @@
 #include "dht22.h"
 #include "GPS_parser.h"
 #include "driver/uart.h"
-
+#include <string.h>
+#include <sys/unistd.h>
+#include <sys/stat.h>
+#include "esp_vfs_fat.h"
+#include "sdmmc_cmd.h"
 
 const char *DHT_TAG = "DHT22";
 const char *GPS_TAG = "GPS_PARSER";
+const char *SD_TAG = "SD_CARD";
+
+#define MOUNT_POINT "/sdcard"
+
+#define PIN_NUM_MISO  CONFIG_PIN_MISO
+#define PIN_NUM_MOSI  CONFIG_PIN_MOSI
+#define PIN_NUM_CLK   CONFIG_PIN_CLK
+#define PIN_NUM_CS    CONFIG_PIN_CS
 
 const int uart_buffer_size = (1024 * 2);
 int length = 0;
@@ -19,10 +31,9 @@ uint8_t data;
 
 static gps_parser_t gps;
 
-void read_uart_char();
-
 void uart_configurator();
 
+static esp_err_t s_example_write_file(const char *path, char *data);
 
 
 void app_main(void)
@@ -48,6 +59,7 @@ void app_main(void)
     }
     */
     // Initialize GPS parser
+    /*
     uart_configurator();
     double latitude, longitude;
     gps_parser_init(&gps);
@@ -72,6 +84,78 @@ void app_main(void)
         }
         vTaskDelay(pdMS_TO_TICKS(2000)); // Wait for 1 second before the next reading
     }
+    */
+
+    // Initialize SD card
+    /*
+    esp_vfs_fat_sdmmc_mount_config_t mount_config = {
+        .format_if_mount_failed = false,
+        .max_files = 5,
+        .allocation_unit_size = 16 * 1024
+    };
+
+    sdmmc_card_t *card;
+
+    const char mount_point[] = MOUNT_POINT;
+
+    sdmmc_host_t host = SDSPI_HOST_DEFAULT();
+    host.max_freq_khz = 9000; //a mas freq no funciona, porque pipas
+
+    spi_bus_config_t bus_cfg = {
+        .mosi_io_num = PIN_NUM_MOSI,
+        .miso_io_num = PIN_NUM_MISO,
+        .sclk_io_num = PIN_NUM_CLK,
+        .quadwp_io_num = -1,
+        .quadhd_io_num = -1,
+        .max_transfer_sz = 4092,
+    };
+
+    esp_err_t ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);
+    if (ret != ESP_OK) {
+        ESP_LOGE(SD_TAG, "Failed to initialize bus.");
+        return;
+    }
+
+    sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
+    slot_config.gpio_cs = PIN_NUM_CS;
+    slot_config.host_id = host.slot;
+
+    ESP_LOGI(SD_TAG, "Mounting filesystem");
+    ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
+
+    if (ret != ESP_OK) {
+        if (ret == ESP_FAIL) {
+            ESP_LOGE(SD_TAG, "Failed to mount filesystem. "
+                     "If you want the card to be formatted, set the CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
+        } else {
+            ESP_LOGE(SD_TAG, "Failed to initialize the card (%s). "
+                     "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
+        }
+        return;
+    }
+    ESP_LOGI(SD_TAG, "Filesystem mounted");
+
+    // Card has been initialized, print its properties
+    sdmmc_card_print_info(stdout, card);
+
+    const char *file_foo = MOUNT_POINT"/foo.txt";
+
+    // Check if destination file exists before renaming
+    struct stat st;
+    if (stat(file_foo, &st) == 0) {
+        // Delete it if it exists
+        unlink(file_foo);
+    }
+
+    const char *file_nihao = MOUNT_POINT"/nihao.txt";
+
+    // All done, unmount partition and disable SPI peripheral
+    esp_vfs_fat_sdcard_unmount(mount_point, card);
+    ESP_LOGI(SD_TAG, "Card unmounted");
+
+    //deinitialize the bus after all devices are removed
+    spi_bus_free(host.slot);
+    */
 
 }
 
@@ -92,7 +176,18 @@ void uart_configurator() {
     ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, GPIO_NUM_17, GPIO_NUM_16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
 }
 
-void read_uart_char() {
-    ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM_2, (size_t*)&length));
-     // Read one byte from UART
-}
+static esp_err_t s_example_write_file(const char *path, char *data)
+{
+    ESP_LOGI(SD_TAG, "Opening file %s", path);
+    FILE *f = fopen(path, "w");
+    if (f == NULL) {
+        ESP_LOGE(SD_TAG, "Failed to open file for writing");
+        return ESP_FAIL;
+    }
+    fprintf(f, data);
+    fclose(f);
+    ESP_LOGI(SD_TAG, "File written");
+
+    return ESP_OK;
+}
+