Skip to content

Commit 750b400

Browse files
authored
Merge pull request #16 from scientisst/dev_kiko
v3.2.0: SDCard fully functional
2 parents f268807 + 2e3bb06 commit 750b400

File tree

14 files changed

+633
-392
lines changed

14 files changed

+633
-392
lines changed

main/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
set(srcs "scientisst.c" "bt.c" "timer.c" "adc.c" "gpio.c" "com.c" "spi.c" "wifi.c" "wifi_rest_server.c" "tcp.c" "udp.c" "uart.c" "ws.c" "ble.c" "sd_card.c")
2-
set(requires esp_adc_cal nvs_flash json mdns bt esp_http_server fatfs spiffs esp_https_server)
2+
set(requires esp_adc_cal nvs_flash json mdns bt esp_http_server fatfs spiffs esp_https_server driver sdmmc)
33

44
set(SRCS_PATH "scientisst-sense-firmware/main")
55

@@ -14,7 +14,7 @@ idf_component_register(SRCS ${srcs}
1414
INCLUDE_DIRS "." "../../"
1515
REQUIRES ${requires}
1616
EMBED_TXTFILES "${CMAKE_CURRENT_LIST_DIR}/certs/cacert.pem" "${CMAKE_CURRENT_LIST_DIR}/certs/prvtkey.pem"
17-
)
17+
)
1818

1919

2020
set(WEB_SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/../www")

main/adc.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,6 @@ void IRAM_ATTR acquireChannelsScientisst(uint8_t* frame) {
243243
#if _ADC_EXT_ != NO_EXT_ADC
244244
if (num_extern_active_chs > 0) {
245245
// Get raw values from AX1 & AX2 (A6 and A7), store them in the frame
246-
if (sim_flag) {
247-
for (i = 0; i < num_extern_active_chs; i++)
248-
adc_external_res[i] = sin10Hz[sin_i % 100];
249-
} else {
250-
for (i = 0; i < num_extern_active_chs; i++)
251-
adc_external_res[i] = adc_ext_samples[i];
252-
}
253246

254247
mcpReadADCValues(REG_ADCDATA, 4 * num_extern_active_chs);
255248

main/adc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#if HW_VERSION == HW_VERSION_CORE
4242
#define ABAT_ADC_CH ADC2_CHANNEL_5
4343
#elif HW_VERSION == HW_VERSION_CARDIO
44-
#define ABAT_ADC_CH ADC1_CHANNEL_5
44+
#define ABAT_ADC_CH ADC2_CHANNEL_5
4545
#elif HW_VERSION == HW_VERSION_NANO
4646
#define ABAT_ADC_CH ADC1_CHANNEL_2 // GPIO38
4747
#endif

main/com.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void selectChsFromMaskScientisstJson(uint8_t* buff) {
219219
}
220220

221221
DEBUG_PRINT_W("selectChsFromMask", "Channel A%d added",
222-
channel_number - 1);
222+
channel_number);
223223
}
224224
channel_number--;
225225
}

main/config.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,26 @@
2424
// Possible values for _ADC_EXT_:
2525
// - NO_EXT_ADC (No external adc) [Default]
2626
// - ADC_MCP (Enable external adc)
27-
#define _ADC_EXT_ NO_EXT_ADC
27+
#define _ADC_EXT_ ADC_MCP
2828

2929
// Possible values for _TIMESTAMP_:
3030
// - TIMESTAMP_DISABLED (Disable timestamp) [Default]
3131
// - TIMESTAMP_ENABLED (Enable timestamp on AX1 and AX2 channels)
32-
#define _TIMESTAMP_ TIMESTAMP_DISABLED
32+
#define _TIMESTAMP_ TIMESTAMP_ENABLED
3333

3434
/************************
3535
* SDCARD CONFIGURATION *
3636
************************/
3737
// Possible values for _SD_CARD_ENABLED_:
3838
// - SD_CARD_DISABLED (Disable sd card) [Default]
3939
// - SD_CARD_ENABLED (Enable sd card)
40-
#define _SD_CARD_ENABLED_ SD_CARD_DISABLED
40+
#define _SD_CARD_ENABLED_ SD_CARD_ENABLED
4141

4242
// Possible values for FORMAT_SDCARD_IF_MOUNT_FAILED:
4343
// - DO_NOT_FORMAT_SDCARD (Do not format sd card if mount failed) [Default]
4444
// - FORMAT_SDCARD (Format sd card if mount failed) (BE CAREFUL, THIS WILL
4545
// ERASE ALL DATA ON THE SD CARD)
46-
#define FORMAT_SDCARD_IF_MOUNT_FAILED DO_NOT_FORMAT_SDCARD
46+
#define FORMAT_SDCARD_IF_MOUNT_FAILED FORMAT_SDCARD
4747

4848
// Possible values for CONVERSION_MODE:
4949
// - RAW_ONLY (Only raw data)
@@ -86,13 +86,15 @@
8686

8787
// Check if the configuration is valid
8888
// Timestamp requires that no external adc is enabled
89-
#if _TIMESTAMP_ == TIMESTAMP_ENABLED && _ADC_EXT_ != NO_EXT_ADC
89+
#if _TIMESTAMP_ == TIMESTAMP_ENABLED && \
90+
(_ADC_EXT_ != NO_EXT_ADC && _SD_CARD_ENABLED_ == SD_CARD_DISABLED)
9091
#error timestamp requires that no external adc is enabled, check config.h
9192
#endif
9293

9394
// SD card requires that no external adc is enabled
9495
#if _SD_CARD_ENABLED_ == SD_CARD_ENABLED && _ADC_EXT_ != NO_EXT_ADC
95-
#error sd card requires that no external adc is enabled, check config.h
96+
#define MULTIPLE_SPI_DEVICES 1
97+
// #error sd card requires that no external adc is enabled, check config.h
9698
#endif
9799

98100
#endif

main/scientisst.c

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
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
5659
SemaphoreHandle_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
5984
char device_name[17] = BT_DEFAULT_DEVICE_NAME;
@@ -116,9 +141,6 @@ uint8_t active_internal_chs[DEFAULT_ADC_CHANNELS] = {
116141
///< 3, 2, 1, 0}
117142
uint8_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
122144
uint8_t num_intern_active_chs = 0;
123145
uint8_t num_extern_active_chs = 0;
124146
uint8_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
151173
op_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
170192
uint8_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-
328360
void 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,

main/scientisst.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "freertos/task.h"
2727
#include "nvs.h"
2828
#include "nvs_flash.h"
29+
#include "sdmmc_cmd.h"
2930
#include "sys/time.h"
3031
#include "time.h"
3132
#include "version.h"
@@ -58,12 +59,13 @@ extern esp_adc_cal_characteristics_t adc2_chars;
5859
extern char device_name[];
5960
extern uint8_t send_busy;
6061
extern SemaphoreHandle_t bt_buffs_to_send_mutex;
62+
extern SemaphoreHandle_t spi_mutex;
63+
extern sdmmc_host_t sd_spi_host;
6164
extern uint16_t send_threshold;
6265
extern uint8_t bt_curr_buff;
6366
extern uint8_t acq_curr_buff;
6467
extern Api_Config api_config;
6568
extern uint8_t active_ext_chs[];
66-
extern uint32_t adc_ext_samples[];
6769
extern uint8_t num_extern_active_chs;
6870
extern cJSON* json;
6971
extern DRAM_ATTR const uint8_t sin10Hz[];

0 commit comments

Comments
 (0)