Skip to content

Commit b72d809

Browse files
committed
fix_soc_sdmmc_and_add_soc_temperature_for_s3_support
1 parent c6a593b commit b72d809

File tree

3 files changed

+103
-13
lines changed

3 files changed

+103
-13
lines changed

code/components/jomjol_helper/Helper.cpp

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ extern "C"
3333
#include "esp_vfs_fat.h"
3434
#include "../sdmmc_common.h"
3535

36+
#ifdef CONFIG_SOC_TEMP_SENSOR_SUPPORTED
37+
#include "driver/temperature_sensor.h"
38+
#endif
39+
3640
static const char *TAG = "HELPER";
3741

3842
using namespace std;
@@ -613,11 +617,62 @@ string toLower(string in)
613617
return in;
614618
}
615619

616-
// CPU Temp
620+
// SOC temperature sensor
621+
#if defined(CONFIG_SOC_TEMP_SENSOR_SUPPORTED)
622+
static float socTemperature = -1;
623+
624+
void taskSocTemp(void *pvParameter)
625+
{
626+
temperature_sensor_handle_t socTempSensor = NULL;
627+
temperature_sensor_config_t socTempSensorConfig = TEMPERATURE_SENSOR_CONFIG_DEFAULT(20, 100);
628+
temperature_sensor_install(&socTempSensorConfig, &socTempSensor);
629+
temperature_sensor_enable(socTempSensor);
630+
631+
while (1) {
632+
if (temperature_sensor_get_celsius(socTempSensor, &socTemperature) != ESP_OK) {
633+
socTemperature = -1;
634+
}
635+
636+
vTaskDelay(pdMS_TO_TICKS(5000));
637+
}
638+
}
639+
640+
void initTemperatureSensor()
641+
{
642+
// Create a dedicated task to ensure access temperature ressource only from a single source
643+
BaseType_t xReturned = xTaskCreate(&taskSocTemp, "taskSocTemp", 2048, NULL, tskIDLE_PRIORITY + 1, NULL);
644+
645+
if (xReturned != pdPASS) {
646+
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to create taskSocTemp");
647+
}
648+
}
649+
650+
float temperatureRead()
651+
{
652+
return socTemperature;
653+
}
654+
655+
#elif defined(CONFIG_IDF_TARGET_ESP32) // Inofficial support of vanilla ESP32. Value might be unreliable
617656
extern "C" uint8_t temprature_sens_read();
657+
618658
float temperatureRead()
619659
{
620-
return (temprature_sens_read() - 32) / 1.8;
660+
return (temprature_sens_read() - 32) / 1.8;
661+
}
662+
663+
#else
664+
#warning "SOC temperature sensor not supported"
665+
float temperatureRead()
666+
{
667+
return -1.0;
668+
}
669+
#endif
670+
671+
std::string intToHexString(int _valueInt)
672+
{
673+
char valueHex[33];
674+
sprintf(valueHex, "0x%02x", _valueInt);
675+
return std::string(valueHex);
621676
}
622677

623678
time_t addDays(time_t startTime, int days)

code/components/jomjol_helper/Helper.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ int removeFolder(const char* folderPath, const char* logTag);
3838
string toLower(string in);
3939
string toUpper(string in);
4040

41+
#ifdef CONFIG_SOC_TEMP_SENSOR_SUPPORTED
42+
void initTemperatureSensor();
43+
#endif
44+
4145
float temperatureRead();
4246

47+
std::string intToHexString(int _valueInt);
4348
time_t addDays(time_t startTime, int days);
4449

4550
void memCopyGen(uint8_t* _source, uint8_t* _target, int _size);

code/main/main.cpp

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@
7979
static heap_trace_record_t trace_record[NUM_RECORDS]; // This buffer must be in internal RAM
8080
#endif
8181

82-
extern const char* GIT_TAG;
83-
extern const char* GIT_REV;
84-
extern const char* GIT_BRANCH;
85-
extern const char* BUILD_TIME;
82+
extern const char *GIT_TAG;
83+
extern const char *GIT_REV;
84+
extern const char *GIT_BRANCH;
85+
extern const char *BUILD_TIME;
8686

8787
extern std::string getFwVersion(void);
8888
extern std::string getHTMLversion(void);
@@ -109,27 +109,51 @@ bool Init_NVS_SDCard()
109109
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
110110
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
111111

112+
// For SoCs where the SD power can be supplied both via an internal or external (e.g. on-board LDO) power supply.
113+
// When using specific IO pins (which can be used for ultra high-speed SDMMC) to connect to the SD card
114+
// and the internal LDO power supply, we need to initialize the power supply first.
115+
#if SD_PWR_CTRL_LDO_INTERNAL_IO
116+
sd_pwr_ctrl_ldo_config_t ldo_config = {
117+
.ldo_chan_id = CONFIG_EXAMPLE_SD_PWR_CTRL_LDO_IO_ID,
118+
};
119+
sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL;
120+
121+
ret = sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle);
122+
if (ret != ESP_OK)
123+
{
124+
ESP_LOGE(TAG, "Failed to create a new on-chip LDO power control driver");
125+
return ret;
126+
}
127+
host.pwr_ctrl_handle = pwr_ctrl_handle;
128+
#endif
129+
112130
// This initializes the slot without card detect (CD) and write protect (WP) signals.
113131
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
132+
#ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
133+
sdmmc_slot_config_t slot_config = {
134+
.cd = SDMMC_SLOT_NO_CD,
135+
.wp = SDMMC_SLOT_NO_WP,
136+
};
137+
#else
114138
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
139+
#endif
115140

116141
// Set bus width to use:
117142
#ifdef __SD_USE_ONE_LINE_MODE__
118143
slot_config.width = 1;
119-
#ifdef SOC_SDMMC_USE_GPIO_MATRIX
144+
#ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
120145
slot_config.clk = GPIO_SDCARD_CLK;
121146
slot_config.cmd = GPIO_SDCARD_CMD;
122147
slot_config.d0 = GPIO_SDCARD_D0;
123-
#endif
124-
125-
#else
148+
#endif // end CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
149+
#else // else __SD_USE_ONE_LINE_MODE__
126150
slot_config.width = 4;
127-
#ifdef SOC_SDMMC_USE_GPIO_MATRIX
151+
#ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
128152
slot_config.d1 = GPIO_SDCARD_D1;
129153
slot_config.d2 = GPIO_SDCARD_D2;
130154
slot_config.d3 = GPIO_SDCARD_D3;
131-
#endif
132-
#endif
155+
#endif // end CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
156+
#endif // end __SD_USE_ONE_LINE_MODE__
133157

134158
// Enable internal pullups on enabled pins. The internal pullups
135159
// are insufficient however, please make sure 10k external pullups are
@@ -350,6 +374,12 @@ extern "C" void app_main(void)
350374
// ********************************************
351375
setupTime(); // NTP time service: Status of time synchronization will be checked after every round (server_tflite.cpp)
352376

377+
// Init SOC temperature sensor (if supported by hardware)
378+
// ********************************************
379+
#if defined(CONFIG_SOC_TEMP_SENSOR_SUPPORTED)
380+
initTemperatureSensor();
381+
#endif
382+
353383
// Set CPU Frequency
354384
// ********************************************
355385
setCpuFrequency();

0 commit comments

Comments
 (0)