1313#include "ble.h"
1414#include "bt.h"
1515#include "com.h"
16+ #include "config.h"
17+ #include "esp_vfs_fat.h"
1618#include "gpio.h"
1719#include "i2c.h"
1820#include "macros.h"
1921#include "macros_conf.h"
2022#include "sd_card.h"
2123#include "sdkconfig.h"
24+ #include "sdmmc_cmd.h"
2225#include "spi.h"
2326#include "tcp.h"
2427#include "timer.h"
@@ -54,6 +57,28 @@ TaskHandle_t acq_adc_ext_task;
5457
5558// Mutex for the buffers
5659SemaphoreHandle_t bt_buffs_to_send_mutex ;
60+ SemaphoreHandle_t spi_mutex ;
61+
62+ sdmmc_host_t sd_spi_host = {
63+ .flags = SDMMC_HOST_FLAG_SPI | SDMMC_HOST_FLAG_DEINIT_ARG ,
64+ .slot = SDSPI_DEFAULT_HOST , // TODO: SPI3_HOST
65+ #if _ADC_EXT_ != NO_EXT_ADC
66+ .max_freq_khz = (80 * 1000 ) / 64 ,
67+ #else
68+ .max_freq_khz = SDMMC_FREQ_DEFAULT * 2 ,
69+ #endif
70+ .io_voltage = 3.3f ,
71+ .init = & sdspi_host_init ,
72+ .set_bus_width = NULL ,
73+ .get_bus_width = NULL ,
74+ .set_bus_ddr_mode = NULL ,
75+ .set_card_clk = & sdspi_host_set_card_clk ,
76+ .do_transaction = & sdspi_host_do_transaction ,
77+ .deinit_p = & sdspi_host_remove_device ,
78+ .io_int_enable = & sdspi_host_io_int_enable ,
79+ .io_int_wait = & sdspi_host_io_int_wait ,
80+ .command_timeout_ms = 0 ,
81+ };
5782
5883// BT
5984char device_name [17 ] = BT_DEFAULT_DEVICE_NAME ;
@@ -116,9 +141,6 @@ uint8_t active_internal_chs[DEFAULT_ADC_CHANNELS] = {
116141 ///< 3, 2, 1, 0}
117142uint8_t active_ext_chs [EXT_ADC_CHANNELS ] = {
118143 0 , 0 }; ///< Active external channels | If all channels are active: = {7, 6}
119- uint32_t adc_ext_samples [EXT_ADC_CHANNELS ] = {
120- 0 , 0 }; ///< Samples from external adc get stored here before being
121- ///< incorporated into frame
122144uint8_t num_intern_active_chs = 0 ;
123145uint8_t num_extern_active_chs = 0 ;
124146uint8_t op_mode = OP_MODE_IDLE ; ///< Flag that indicastes if op mode is on
@@ -149,7 +171,7 @@ spi_transaction_t adc_ext_trans;
149171
150172// Op settings
151173op_settings_info_t op_settings = {
152- .com_mode = COM_MODE_BT ,
174+ .com_mode = COM_MODE_SD_CARD ,
153175};
154176/*{
155177 .com_mode = COM_MODE_TCP_STA,
@@ -168,6 +190,7 @@ uint8_t is_op_settings_valid = 0;
168190
169191// Firmware version
170192uint8_t first_failed_send = 0 ;
193+ uint8_t sd_card_present = 0 ; ///< Flag that indicates if SD card is present
171194
172195/**
173196 * \brief scientisst-firmware main function
@@ -186,6 +209,11 @@ void initScientisst(void) {
186209 abort ();
187210 }
188211
212+ if ((spi_mutex = xSemaphoreCreateMutex ()) == NULL ) {
213+ DEBUG_PRINT_E ("xSemaphoreCreateMutex" , "Mutex creation failed" );
214+ abort ();
215+ }
216+
189217 // Init nvs (Non volatile storage)
190218 esp_err_t ret = nvs_flash_init ();
191219 if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
@@ -232,8 +260,8 @@ void initScientisst(void) {
232260 }
233261 }
234262
235- xTaskCreatePinnedToCore (& sendTask , "sendTask" , 4096 , NULL , BT_SEND_PRIORITY ,
236- & send_task , 0 );
263+ xTaskCreatePinnedToCore (& sendTask , "sendTask" , 4096 * 4 , NULL ,
264+ BT_SEND_PRIORITY , & send_task , 0 );
237265
238266 // If op_mode is Wifi or Serial
239267 if (isComModeWifi () || !strcmp (op_settings .com_mode , COM_MODE_SERIAL )) {
@@ -263,9 +291,14 @@ void initScientisst(void) {
263291
264292 // Create the 1st task that will acquire data from adc. This task will be
265293 // responsible for acquiring the data from adc1
294+
295+ #if _SD_CARD_ENABLED_ == SD_CARD_ENABLED
296+ xTaskCreatePinnedToCore (& acquisitionSDCard , "acqSDCard" , 4096 * 2 , NULL , 24 ,
297+ & acq_adc1_task , 1 );
298+ #else
266299 xTaskCreatePinnedToCore (& acqAdc1Task , "acqAdc1Task" , 4096 , NULL ,
267300 ACQ_ADC1_PRIORITY , & acq_adc1_task , 1 );
268-
301+ #endif
269302 // xTaskCreatePinnedToCore(&acqAdcExtTask, "acqAdcExtTask", 2048, NULL,
270303 // ACQ_ADC_EXT_PRIORITY, &acq_adc_ext_task, 1);
271304
@@ -296,26 +329,26 @@ void IRAM_ATTR sendTask(void* not_used) {
296329#if _SD_CARD_ENABLED_ == SD_CARD_ENABLED
297330 else if (!strcmp (op_settings .com_mode , COM_MODE_SD_CARD )) {
298331 if (initSDCard () != ESP_OK ) {
332+ vTaskDelete (acq_adc1_task );
299333 DEBUG_PRINT_E ("SD_CARD" ,
300334 "Cannot init SD Card, changing to BT mode" );
335+ xTaskCreatePinnedToCore (& acqAdc1Task , "acqAdc1Task" , 4096 , NULL ,
336+ ACQ_ADC1_PRIORITY , & acq_adc1_task , 1 );
301337 initBt ();
302338 send_func = & esp_spp_write ;
303339 } else {
304- send_func = & saveToSDCardSend ;
340+ sd_card_present = 1 ;
305341 startAcquisitionSDCard ();
306342 }
307343 }
308344#endif
345+ #if _SD_CARD_ENABLED_ == SD_CARD_ENABLED
346+ if (sd_card_present ) vTaskDelete (NULL );
347+ #endif
309348
310349 while (1 ) {
311350 ulTaskNotifyTake (pdTRUE , portMAX_DELAY );
312- #if _SD_CARD_ENABLED_ == SD_CARD_ENABLED
313- openFile ();
314- #endif
315351 sendData ();
316- #if _SD_CARD_ENABLED_ == SD_CARD_ENABLED
317- closeSDCard ();
318- #endif
319352 }
320353}
321354
@@ -324,9 +357,9 @@ void IRAM_ATTR sendTask(void* not_used) {
324357 *
325358 * This task is responsible for receiving data from the client.
326359 */
327-
328360void rcvTask (void * not_used ) {
329361 while (1 ) {
362+ if (sd_card_present ) vTaskDelete (NULL );
330363 // TCP client
331364 if (!strcmp (op_settings .com_mode , COM_MODE_TCP_STA )) {
332365 while ((send_fd = initTcpClient (op_settings .host_ip ,
0 commit comments