Commit ee006822 authored by Theodor-Adrian Stana's avatar Theodor-Adrian Stana

gps-tracking: Implemented mutex sync for SD card access

parent 8e8fcdc1
......@@ -32,7 +32,12 @@
#include "msddmedia.h"
#include "em_usb.h"
#include <semphr.h>
static bool init_ok;
static int mutexours = 0;
extern xSemaphoreHandle mutexSdCardAccess;
static void usb_ms_redraw(struct ui_widget *w)
{
......@@ -40,11 +45,13 @@ static void usb_ms_redraw(struct ui_widget *w)
gfx_centered_text(&w->dc, &font_helv17b, 30, "USB mass storage", 1);
if(init_ok) {
if (mutexours && init_ok) {
gfx_centered_text(&w->dc, &font_helv17b, 30 + 18, "enabled", 1);
gfx_centered_text(&w->dc, &font_helv11, 80, "unmount the device", 1);
gfx_centered_text(&w->dc, &font_helv11, 80 + 12, "before unplugging", 1);
} else if (!mutexours) {
gfx_centered_text(&w->dc, &font_helv17b, 80, "media in use", 1);
} else {
gfx_centered_text(&w->dc, &font_helv17b, 80, "media error", 1);
}
......@@ -71,20 +78,27 @@ void usb_ms_main(void* params) {
ui_init_widget(&status_bar);
ui_add_widget(&status_bar);
init_ok = MSDDMEDIA_Init();
if (xSemaphoreTake(mutexSdCardAccess, 0)) {
mutexours = 1;
init_ok = MSDDMEDIA_Init();
if(init_ok) {
MSDD_Init(-1, -1);
}
}
ui_update(NULL);
if(init_ok) {
MSDD_Init(-1, -1);
}
while(1) {
if(xQueueReceive(appQueue, &evt, 1)) {
switch(evt.type) {
case BUTTON_PRESSED:
if(evt.data.button == BUT_TR) {
USBD_Stop();
if (mutexours) {
mutexours = 0;
USBD_Stop();
xSemaphoreGive(mutexSdCardAccess);
}
return; // go back to the main menu
}
break;
......@@ -94,9 +108,7 @@ void usb_ms_main(void* params) {
ui_update(&evt);
break;
}
}
else
{
} else if (mutexours) {
MSDD_Handler();
}
}
......
......@@ -33,7 +33,6 @@
#ifdef DEBUG
#include <usbdbg.h>
#include <stdio.h>
#endif
#include "application.h"
......@@ -43,6 +42,9 @@
#include <microsd.h>
#include <diskio.h>
#include <stdio.h>
#include <string.h>
#define GPSBKGND_TIMER_PERIOD (1000 / portTICK_RATE_MS)
static xTimerHandle timerGps;
......@@ -50,8 +52,7 @@ extern xSemaphoreHandle mutexSdCardAccess;
static int firstrun, firstfix;
static int cgpson, pgpson;
static int runcnt = 0;
static int ctrack, ptrack;
static FIL f;
static FATFS fatfs;
......@@ -68,7 +69,7 @@ static void gpsbkgnd_task(void *params)
struct tm time;
struct gps_utc gpstime;
/* Previous and current state of GPS on setting at timer tick */
/* Previous and current state of settings */
pgpson = cgpson;
cgpson = setting_get(&setting_gps_on);
......@@ -108,30 +109,33 @@ static void gpsbkgnd_task(void *params)
}
/* Track GPS position if setting tells us to */
if (setting_get(&setting_tracking) &&
xSemaphoreTake(mutexSdCardAccess, 0) ) {
ptrack = ctrack;
ctrack = setting_get(&setting_tracking);
if (ctrack) {
if (firstrun) {
/* Init stuff, open file & take semaphore so that only we write to SD */
if (firstrun || !ptrack) {
xSemaphoreTake(mutexSdCardAccess, 0);
MICROSD_Init();
disk_initialize(0);
f_mount(0, &fatfs);
f_open(&f, "hello", FA_CREATE_ALWAYS | FA_WRITE);
f_open(&f, "yellow", FA_CREATE_ALWAYS | FA_WRITE);
f_lseek(&f, 0);
int i;
for (i = 0; i < 10000; i++)
;
}
char buf[32];
int len;
UINT read;
sprintf(buf, "hello, world!\n");
len = strlen(buf);
f_write(&f, buf, len, &read);
if (++runcnt == 4) {
xSemaphoreGive(mutexSdCardAccess);
f_close(&f);
/* Write to file if gps is fixed */
if (gps_fixed()) {
f_printf(&f, "%d\n", gps_fixed());
}
} else if (!ctrack && ptrack) {
/*
* Turned off tracking setting => close file, deinit microsd driver and
* give mutex so that the USB mass storage device can take it
*/
f_close(&f);
MICROSD_Deinit();
xSemaphoreGive(mutexSdCardAccess);
}
if (firstrun)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment