|
|
@ -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(); |
|
|
|
} |
|
|
|
|
|
|
|