Skip to content

Commit

Permalink
more robust file writing
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Mar 25, 2020
1 parent e5cb8f4 commit 8f5f1ca
Show file tree
Hide file tree
Showing 18 changed files with 451 additions and 256 deletions.
5 changes: 4 additions & 1 deletion src/eez/gui/action_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,10 @@ void action_user_switch_clicked() {

case persist_conf::USER_SWITCH_ACTION_SCREENSHOT:
using namespace scpi;
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_MESSAGE(SCPI_QUEUE_MESSAGE_TARGET_NONE, SCPI_QUEUE_MESSAGE_SCREENSHOT, 0), osWaitForever);
if (!g_screenshotGenerating) {
g_screenshotGenerating = true;
osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_MESSAGE(SCPI_QUEUE_MESSAGE_TARGET_NONE, SCPI_QUEUE_MESSAGE_SCREENSHOT, 0), osWaitForever);
}
break;

case persist_conf::USER_SWITCH_ACTION_MANUAL_TRIGGER:
Expand Down
2 changes: 1 addition & 1 deletion src/eez/libs/sd_fat/sd_fat.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class File {
bool open(const char *path, uint8_t mode = FILE_READ);

~File();
void close();
bool close();

bool isOpen();

Expand Down
6 changes: 4 additions & 2 deletions src/eez/libs/sd_fat/simulator/sd_fat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,14 @@ File::~File() {
close();
}

void File::close() {
bool File::close() {
int result = 0;
if (m_fp) {
fclose(m_fp);
result = fclose(m_fp);
m_fp = NULL;
}
m_isOpen = false;
return !result;
}

bool File::isOpen() {
Expand Down
95 changes: 70 additions & 25 deletions src/eez/libs/sd_fat/stm32/sd_fat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@

#include <scpi/scpi.h>

#include <eez/debug.h>
#include <eez/util.h>
#include <eez/libs/sd_fat/sd_fat.h>

#define CHECK_ERROR(desc, err) (void)(desc); (void)(err);
//#define CHECK_ERROR(desc, err) if (err != FR_OK) DebugTrace("%s: %d\n", desc, (int)err)

namespace eez {

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -33,7 +37,9 @@ FileInfo::FileInfo() {
}

SdFatResult FileInfo::fstat(const char *filePath) {
return (SdFatResult)f_stat(filePath, &m_fno);
auto result = f_stat(filePath, &m_fno);
CHECK_ERROR("FileInfo::fstat", result);
return (SdFatResult)result;
}

FileInfo::operator bool() {
Expand Down Expand Up @@ -106,19 +112,24 @@ Directory::~Directory() {
}

void Directory::close() {
f_closedir(&m_dj);
auto result = f_closedir(&m_dj);
CHECK_ERROR("Directory::close", result);
}

SdFatResult Directory::findFirst(const char *path, const char *pattern, FileInfo &fileInfo) {
return (SdFatResult)f_findfirst(&m_dj, &fileInfo.m_fno, path, pattern ? pattern : "*");
auto result = f_findfirst(&m_dj, &fileInfo.m_fno, path, pattern ? pattern : "*");
CHECK_ERROR("Directory::findFirst", result);
return (SdFatResult)result;
}

SdFatResult Directory::findFirst(const char *path, FileInfo &fileInfo) {
return findFirst(path, nullptr, fileInfo);
}

SdFatResult Directory::findNext(FileInfo &fileInfo) {
return (SdFatResult)f_findnext(&m_dj, &fileInfo.m_fno);
auto result = f_findnext(&m_dj, &fileInfo.m_fno);
CHECK_ERROR("Directory::findNext", result);
return (SdFatResult)result;
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -127,25 +138,32 @@ File::File() : m_isOpen(false) {
}

bool File::open(const char *path, uint8_t mode) {
FRESULT result = f_open(&m_file, path, mode);
auto result = f_open(&m_file, path, mode);
CHECK_ERROR("File::open", result);
m_isOpen = result == FR_OK;
return m_isOpen;
}

File::~File() {
}

void File::close() {
f_close(&m_file);
bool File::close() {
auto result = f_close(&m_file);
CHECK_ERROR("File::close", result);
m_isOpen = false;
return result == FR_OK;
}

bool File::isOpen() {
return m_isOpen;
}

bool File::truncate(uint32_t length) {
return f_lseek(&m_file, length) == FR_OK && f_truncate(&m_file) == FR_OK;
auto result1 = f_lseek(&m_file, length);
CHECK_ERROR("File::truncate 1", result1);
auto result2 = f_truncate(&m_file);
CHECK_ERROR("File::truncate 2", result2);
return result1 == FR_OK && result2 == FR_OK;
}

size_t File::size() {
Expand All @@ -157,24 +175,30 @@ bool File::available() {
}

bool File::seek(uint32_t pos) {
return f_lseek(&m_file, pos) == FR_OK;
auto result = f_lseek(&m_file, pos);
CHECK_ERROR("File::seek", result);
return result == FR_OK;
}

size_t File::tell() {
return f_tell(&m_file);
auto result = f_tell(&m_file);
CHECK_ERROR("File::tell", result);
return result;
}

int File::peek() {
auto pos = f_tell(&m_file);
int result = read();
f_lseek(&m_file, pos);
return result;
int ch = read();
auto result = f_lseek(&m_file, pos);
CHECK_ERROR("File::peek", result);
return ch;
}

int File::read() {
uint8_t value;
UINT br;
auto result = f_read(&m_file, &value, 1, &br);
CHECK_ERROR("File::read", result);
return result != FR_OK || br != 1 ? EOF : (int)value;
}

Expand All @@ -189,6 +213,7 @@ size_t File::read(void *buf, uint32_t size) {
uint8_t unalignedBuffer[4] __attribute__((aligned));
UINT br;
auto result = f_read(&m_file, unalignedBuffer, unalignedLength, &br);
CHECK_ERROR("File::read 1", result);
if (result != FR_OK) {
return 0;
}
Expand All @@ -209,6 +234,7 @@ size_t File::read(void *buf, uint32_t size) {

UINT br;
auto result = f_read(&m_file, (uint8_t *)buf + brTotal, btr, &br);
CHECK_ERROR("File::read 2", result);
if (result != FR_OK) {
return brTotal;
}
Expand Down Expand Up @@ -238,6 +264,7 @@ size_t File::write(const void *buf, size_t size) {

UINT bw;
auto result = f_write(&m_file, unalignedBuffer, unalignedLength, &bw);
CHECK_ERROR("File::write 1", result);
if (result != FR_OK) {
return 0;
}
Expand All @@ -254,6 +281,7 @@ size_t File::write(const void *buf, size_t size) {

UINT bw;
auto result = f_write(&m_file, (const uint8_t *)buf + bwTotal, btw, &bw);
CHECK_ERROR("File::write 2", result);
if (result != FR_OK) {
return bwTotal;
}
Expand All @@ -269,7 +297,9 @@ size_t File::write(const void *buf, size_t size) {
}

bool File::sync() {
return f_sync(&m_file) == FR_OK;
auto result = f_sync(&m_file);
CHECK_ERROR("File::sync", result);
return result == FR_OK;
}

void File::print(float value, int numDecimalDigits) {
Expand All @@ -279,15 +309,17 @@ void File::print(float value, int numDecimalDigits) {
}

void File::print(char value) {
f_printf(&m_file, "%c", value);
auto result = f_printf(&m_file, "%c", value);
CHECK_ERROR("File:print", result);
}

////////////////////////////////////////////////////////////////////////////////

bool SdFat::mount(int *err) {
auto res = f_mount(&SDFatFS, SDPath, 1);
if (res != FR_OK) {
if (res == FR_NO_FILESYSTEM) {
auto result = f_mount(&SDFatFS, SDPath, 1);
CHECK_ERROR("SdFat::mount", result);
if (result != FR_OK) {
if (result == FR_NO_FILESYSTEM) {
*err = SCPI_ERROR_MASS_MEDIA_NO_FILESYSTEM;
} else {
*err = SCPI_ERROR_MASS_STORAGE_ERROR;
Expand All @@ -300,7 +332,8 @@ bool SdFat::mount(int *err) {
}

void SdFat::unmount() {
f_mount(0, "", 0);
auto result = f_mount(0, "", 0);
CHECK_ERROR("SdFat::unmount", result);
memset(&SDFatFS, 0, sizeof(SDFatFS));
}

Expand All @@ -309,29 +342,41 @@ bool SdFat::exists(const char *path) {
return true;
}
FILINFO fno;
return f_stat(path, &fno) == FR_OK;
auto result = f_stat(path, &fno);
CHECK_ERROR("SdFat::exists", result);
return result == FR_OK;
}

bool SdFat::rename(const char *sourcePath, const char *destinationPath) {
return f_rename(sourcePath, destinationPath) == FR_OK;
auto result = f_rename(sourcePath, destinationPath);
CHECK_ERROR("SdFat::rename", result);
return result == FR_OK;
}

bool SdFat::remove(const char *path) {
return f_unlink(path) == FR_OK;
auto result = f_unlink(path);
CHECK_ERROR("SdFat::remove", result);
return result == FR_OK;
}

bool SdFat::mkdir(const char *path) {
return f_mkdir(path) == FR_OK;
auto result = f_mkdir(path);
CHECK_ERROR("SdFat::mkdir", result);
return result == FR_OK;
}

bool SdFat::rmdir(const char *path) {
return f_unlink(path) == FR_OK;
auto result = f_unlink(path);
CHECK_ERROR("SdFat::rmdir", result);
return result == FR_OK;
}

bool SdFat::getInfo(uint64_t &usedSpace, uint64_t &freeSpace) {
DWORD freeClusters;
FATFS *fs;
if (f_getfree(SDPath, &freeClusters, &fs) != FR_OK) {
auto result = f_getfree(SDPath, &freeClusters, &fs);
CHECK_ERROR("SdFat::getInfo", result);
if (result != FR_OK) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions src/eez/modules/psu/channel_dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,12 +550,12 @@ void setVoltage(Channel &channel, float voltage) {
}

void setVoltageStep(Channel &channel, float voltageStep) {
voltageStep = roundTrackingValuePrecision(UNIT_VOLT, voltageStep);

if (channel.channelIndex < 2 && (g_couplingType == COUPLING_TYPE_SERIES || g_couplingType == COUPLING_TYPE_PARALLEL)) {
Channel::get(0).u.step = voltageStep;
Channel::get(1).u.step = voltageStep;
} else if (channel.flags.trackingEnabled) {
voltageStep = roundTrackingValuePrecision(UNIT_VOLT, voltageStep);

for (int i = 0; i < CH_NUM; ++i) {
Channel &trackingChannel = Channel::get(i);
if (trackingChannel.flags.trackingEnabled) {
Expand Down Expand Up @@ -850,12 +850,12 @@ void setCurrent(Channel &channel, float current) {
}

void setCurrentStep(Channel &channel, float currentStep) {
currentStep = roundTrackingValuePrecision(UNIT_AMPER, currentStep);

if (channel.channelIndex < 2 && (g_couplingType == COUPLING_TYPE_SERIES || g_couplingType == COUPLING_TYPE_PARALLEL)) {
Channel::get(0).i.step = currentStep;
Channel::get(1).i.step = currentStep;
} else if (channel.flags.trackingEnabled) {
currentStep = roundTrackingValuePrecision(UNIT_AMPER, currentStep);

for (int i = 0; i < CH_NUM; ++i) {
Channel &trackingChannel = Channel::get(i);
if (trackingChannel.flags.trackingEnabled) {
Expand Down
Loading

0 comments on commit 8f5f1ca

Please sign in to comment.