From 137c30be6e0c4d0ccc830aba76fb0197165de14d Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Fri, 21 Apr 2017 16:43:49 +0200 Subject: [PATCH] attempt to fix double task creation --- src/main.cpp | 9 +++++---- src/sdcard.cpp | 33 ++++++++++++++++++++++++++------- src/sound.cpp | 14 ++++++++++---- src/sound.h | 2 +- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0be0787..6ba66fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,7 +45,7 @@ void setup() static const char *bootSound = "/boot.wav"; - xTaskCreate(&(Sound::buffer_refill_task), "buffer_refill_task", 4096, (void*)bootSound, 5, &xTaskSound); + //xTaskCreate(&(Sound::buffer_refill_task), "buffer_refill_task", 3072, (void*)bootSound, 5, &xTaskSound); } void loop() @@ -56,9 +56,10 @@ void loop() touched = true; Serial.print("touch "); Serial.println(i, DEC); - if (sound.playing == false) { - Serial.println("new task"); - xTaskCreate(&(Sound::buffer_refill_task), "buffer_refill_task", 4096, (void*)soundFile[i/3][i%3], 5, &xTaskSound); + if (Sound::playing == false) { + Sound::playing == true; // ToDo: fix double task init + Serial.println("main loop touched==true: new task"); + xTaskCreate(&(Sound::buffer_refill_task), "buffer_refill_task", 3072, (void*)soundFile[i/3][i%3], 5, &xTaskSound); } } } diff --git a/src/sdcard.cpp b/src/sdcard.cpp index 6672e93..9d840b8 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -9,6 +9,7 @@ #include "FS.h" #include "SD.h" #include "SPI.h" +#include "freertos/semphr.h" #include "hardware.h" #include "sdcard.h" @@ -20,10 +21,12 @@ using namespace std; static const char* TAG = "SDCard"; unsigned char *DataSource::buffer = NULL; +SemaphoreHandle_t xSemaphore = NULL; unsigned int DataSource::buf_pos = 0; File SDCard::file; SDCard::SDCard() { + xSemaphore = xSemaphoreCreateMutex(); } void SDCard::mount() { @@ -62,17 +65,33 @@ void SDCard::umount() { } void SDCard::open(const char *path) { - DataSource::buffer = (unsigned char *)malloc(BUF_LENGTH); - Serial.print("Allocated buffer at: "); Serial.println((int)(DataSource::buffer),HEX); - SDCard::file = (SDCard::fs).open(path); - if(!SDCard::file){ - Serial.println("Failed to open file for reading"); - return; + if( xSemaphore != NULL ) { + if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE ) { + if (DataSource::buffer != NULL) Serial.println("double malloc for buffer?"); + DataSource::buffer = (unsigned char *)malloc(BUF_LENGTH); + Serial.print("Allocated buffer at: "); Serial.println((int)(DataSource::buffer),HEX); + SDCard::file = (SDCard::fs).open(path); + if(!SDCard::file){ + Serial.println("Failed to open file for reading"); + return; + } + } else { + /* xSemaphoreTake() != pdTRUE */ + /* could not obtain the semaphore */ + Serial.println("sdcard: open(): could not obtain semaphore"); + } } + + } void SDCard::close() { - free(DataSource::buffer); + if (DataSource::buffer != NULL) { // ToDo: really fix this + Serial.print("free(buffer) at: "); Serial.println((int)(DataSource::buffer),HEX); + free(DataSource::buffer); + DataSource::buffer = NULL; + xSemaphoreGive( xSemaphore ); + } SDCard::file.close(); } diff --git a/src/sound.cpp b/src/sound.cpp index 838119d..3aa935d 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -28,7 +28,7 @@ using namespace std; -bool Sound::playing = false; +volatile bool Sound::playing = false; Sound::Sound() { } @@ -82,13 +82,15 @@ void Sound::init() { } void Sound::play() { + Serial.println("Sound::play()"); i2s_start(i2s_num); - playing = true; + Sound::playing = true; } void Sound::stop() { + Serial.println("Sound::stop()"); i2s_stop(i2s_num); - playing = false; + Sound::playing = false; } void Sound::end() { @@ -96,22 +98,25 @@ void Sound::end() { } void Sound::buffer_refill_task(void *pvParameter) { +// Serial.print("initial free stack: "); Serial.println(uxTaskGetStackHighWaterMark( NULL ), DEC); // DEBUG char *path = (char *) pvParameter; Serial.println("sdcard: opening File"); SDCard::open(path); Sound::play(); while (SDCard::available()) { +// Serial.print("chunk free stack: "); Serial.println(uxTaskGetStackHighWaterMark( NULL ), DEC); // DEBUG // get chunk of data SDCard::read(); // overwrite buffer DataSource::buf_pos = 0; while (DataSource::buf_pos < BUF_LENGTH) { +// Serial.print("i2s_push free stack: "); Serial.println(uxTaskGetStackHighWaterMark( NULL ), DEC); // DEBUG uint16_t *buf = (uint16_t *) DataSource::buffer; int buf_pos = (DataSource::buf_pos)/4; // convert data //unsigned int sample = ((unsigned short) DataSource::buffer[DataSource::buf_pos] << 16 & 0xffff0000) | ((unsigned short) DataSource::buffer[DataSource::buf_pos]); - unsigned int sample = ((unsigned short) buf[buf_pos] << 16 & 0xffff0000) | ((unsigned short) DataSource::buffer[buf_pos]); + unsigned int sample = ((unsigned short) buf[buf_pos] << 16 & 0xffff0000) | ((unsigned short) buf[buf_pos]); // push samples int num_pushed_bytes = i2s_push_sample(Sound::i2s_num, (char *)&sample, 0); if (num_pushed_bytes == 0) { @@ -127,6 +132,7 @@ void Sound::buffer_refill_task(void *pvParameter) { Serial.println("sdcard: EOF"); SDCard::close(); Sound::stop(); + Serial.println("task exiting"); vTaskDelete( NULL ); } diff --git a/src/sound.h b/src/sound.h index 56b2237..e06b5f9 100644 --- a/src/sound.h +++ b/src/sound.h @@ -18,7 +18,7 @@ class Sound { static void stop(); int render_sample_block(uint16_t *sample_buf_left, uint16_t *sample_buf_right, int num_samples); static const i2s_port_t i2s_num = (i2s_port_t)I2S_NUM; - static bool playing; + static volatile bool playing; static void buffer_refill_task(void *pvParameter); private: TaskHandle_t xTaskRefillBuffer;