Solaris Software
Loading...
Searching...
No Matches
storagePort.c
Go to the documentation of this file.
1/**
2 * @file storagePort.c
3 * @brief ESP32-S3 SD card storage port implementation.
4 */
5
6/* ----------------------------------------------------------------
7 * INCLUDES
8 * ---------------------------------------------------------------- */
9#include "storagePort.h"
10
11#include "macrosEsp32.h"
12#include "../spi/spiPort.h"
13
14#include "driver/spi_common.h"
15#include "driver/gpio.h"
16#include "driver/sdspi_host.h"
17#include "sdmmc_cmd.h"
18
19#include "freertos/FreeRTOS.h"
20#include "freertos/task.h"
21
22/* ----------------------------------------------------------------
23 * VARIABLES
24 * ---------------------------------------------------------------- */
25static sdspi_dev_handle_t sdHandle;
26static sdmmc_card_t sdCard;
28
29/* ----------------------------------------------------------------
30 * PUBLIC FUNCTIONS
31 * ---------------------------------------------------------------- */
32
34{
35 esp_err_t ret;
36
38 {
39 return K_SPP_ERROR;
40 }
41
42 sdspi_device_config_t sdCfg = SDSPI_DEVICE_CONFIG_DEFAULT();
43 sdCfg.host_id = K_ESP32_SPI_HOST;
44 sdCfg.gpio_cs = K_ESP32_PIN_CS_SDC;
45
46 (void)sdspi_host_deinit();
47
48 gpio_set_direction(K_ESP32_PIN_CS_BMP, GPIO_MODE_OUTPUT);
49 gpio_set_direction(K_ESP32_PIN_CS_ICM, GPIO_MODE_OUTPUT);
50 gpio_set_direction(K_ESP32_PIN_CS_SX1262, GPIO_MODE_OUTPUT);
51 gpio_set_level(K_ESP32_PIN_CS_BMP, 1);
52 gpio_set_level(K_ESP32_PIN_CS_ICM, 1);
53 gpio_set_level(K_ESP32_PIN_CS_SX1262, 1);
54
55 for (int attempt = 0; attempt < 5; attempt++)
56 {
57 ret = sdspi_host_init();
58 if (ret != ESP_OK)
59 {
60 continue;
61 }
62
63 ret = sdspi_host_init_device(&sdCfg, &sdHandle);
64 if (ret != ESP_OK)
65 {
66 (void)sdspi_host_deinit();
67 continue;
68 }
69
70 sdmmc_host_t host = SDSPI_HOST_DEFAULT();
71 host.slot = sdHandle;
72
73 vTaskDelay(pdMS_TO_TICKS(10));
74
75 ret = sdmmc_card_init(&host, &sdCard);
76 if (ret == ESP_OK)
77 {
79 return K_SPP_OK;
80 }
81
82 (void)sdspi_host_remove_device(sdHandle);
83 (void)sdspi_host_deinit();
84 vTaskDelay(pdMS_TO_TICKS(50));
85 }
86
87 return K_SPP_ERROR;
88}
89
91{
92 esp_err_t ret;
93
94 if (s_storageInitialized == false || count == 0)
95 {
96 return K_SPP_ERROR;
97 }
98
99 if (p_buffer == NULL)
100 {
102 }
103
104 if (sdCard.csd.capacity <= first_block || (sdCard.csd.capacity - first_block) < count)
105 {
106 return K_SPP_ERROR;
107 }
108
109 ret = sdmmc_write_sectors(&sdCard, p_buffer, first_block, count);
110 if (ret != ESP_OK)
111 {
112 return K_SPP_ERROR;
113 }
114
115 return K_SPP_OK;
116}
ESP32 hardware pin definitions and SPI constants.
#define K_ESP32_SPI_HOST
SPI peripheral to use.
Definition macrosEsp32.h:25
#define K_ESP32_PIN_CS_BMP
BMP390 barometer chip-select GPIO.
Definition macrosEsp32.h:32
#define K_ESP32_PIN_CS_ICM
ICM20948 IMU chip-select GPIO.
Definition macrosEsp32.h:35
#define K_ESP32_PIN_CS_SX1262
SX1262 chip-select GPIO.
Definition macrosEsp32.h:41
#define K_ESP32_PIN_CS_SDC
SD card chip-select GPIO.
Definition macrosEsp32.h:38
SPP_RetVal_t
Unified return type for all SPP API functions.
Definition returnTypes.h:24
@ K_SPP_OK
Definition returnTypes.h:25
@ K_SPP_ERROR
Definition returnTypes.h:26
@ K_SPP_ERROR_NULL_POINTER
Definition returnTypes.h:30
spp_bool_t SPP_PORTS_HAL_ESP32_spiIsBusInitialized(void)
Check whether the SPI bus has been initialised.
Definition spiPort.c:218
ESP32-S3 SPI port implementation.
static sdmmc_card_t sdCard
Definition storagePort.c:26
SPP_RetVal_t SPP_PORTS_HAL_ESP32_storageWrite(const void *p_buffer, spp_uint32_t first_block, spp_uint16_t count)
Writes raw 512-byte sectors directly to the SD card.
Definition storagePort.c:90
SPP_RetVal_t SPP_PORTS_HAL_ESP32_storageInit(void)
Initializes the SD card using SDSPI.
Definition storagePort.c:33
static spp_bool_t s_storageInitialized
Definition storagePort.c:27
static sdspi_dev_handle_t sdHandle
Definition storagePort.c:25
ESP32-S3 SD card storage port implementation.
uint16_t spp_uint16_t
Definition types.h:29
uint32_t spp_uint32_t
Definition types.h:31
bool spp_bool_t
Definition types.h:36