2020-04-26 20:21:13 +02:00
|
|
|
/* Reflow Oven Controller
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
|
|
|
|
*
|
|
|
|
* This file is part of the Reflow Oven Controller Project.
|
|
|
|
*
|
|
|
|
* The reflow oven controller is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with the reflow oven controller project.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <reflow-controller/settings/settings-sd-card.h>
|
2020-08-16 01:24:20 +02:00
|
|
|
#include <stm-periph/unique-id.h>
|
|
|
|
#include <toml/toml.h>
|
|
|
|
#include <fatfs/ff.h>
|
|
|
|
|
|
|
|
static void get_controller_folder_path(char *path, size_t size)
|
|
|
|
{
|
|
|
|
uint32_t high;
|
|
|
|
uint32_t mid;
|
|
|
|
uint32_t low;
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unique_id_get(&high, &mid, &low);
|
|
|
|
|
|
|
|
snprintf(path, size, "/%08X-%08X-%08X",
|
|
|
|
(unsigned int)high, (unsigned int)mid, (unsigned int)low);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Open or create the controller folder on the SD Card.
|
|
|
|
* @param[in,out] controller_folder
|
|
|
|
* @return 0 if opened, 1 if created and opened, -1 if error.
|
|
|
|
*/
|
|
|
|
static int open_or_create_controller_folder(DIR *controller_folder)
|
|
|
|
{
|
|
|
|
char foldername[48];
|
|
|
|
int ret = -1;
|
|
|
|
FRESULT filesystem_result;
|
|
|
|
|
|
|
|
if (!controller_folder)
|
|
|
|
return -1001;
|
|
|
|
|
|
|
|
get_controller_folder_path(foldername, sizeof(foldername));
|
|
|
|
|
|
|
|
/* Check if folder is present */
|
|
|
|
filesystem_result = f_opendir(controller_folder, foldername);
|
|
|
|
if (filesystem_result == FR_OK) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
filesystem_result = f_mkdir(foldername);
|
|
|
|
if (filesystem_result == FR_OK) {
|
|
|
|
filesystem_result = f_opendir(controller_folder, foldername);
|
|
|
|
if (filesystem_result == FR_OK)
|
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = -1;
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
f_chdir(foldername);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sd_card_settings_save_calibration(float sens_deviation, float offset)
|
|
|
|
{
|
|
|
|
DIR folder;
|
|
|
|
FIL cal_file;
|
|
|
|
int status;
|
|
|
|
FRESULT res;
|
|
|
|
UINT bw;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
status = open_or_create_controller_folder(&folder);
|
|
|
|
f_closedir(&folder);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
/* Create new calibration file */
|
|
|
|
res = f_open(&cal_file, CALIBRATION_FILE_NAME, FA_CREATE_ALWAYS | FA_WRITE);
|
|
|
|
if (res == FR_OK) {
|
|
|
|
f_write(&cal_file, "Test", 4U, &bw);
|
|
|
|
f_close(&cal_file);
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
f_chdir("/");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|