Remove toml. Write calibration to dat files. Implement first draft for reading function

This commit is contained in:
Mario Hüttel 2020-08-16 19:37:41 +02:00
parent a5402d3f04
commit e06c9f7ddc
9 changed files with 120 additions and 2339 deletions

View File

@ -49,8 +49,6 @@ CFILES += settings/settings.c settings/settings-sd-card.c
CFILES += safety/safety-adc.c safety/safety-controller.c safety/watchdog.c
CFILES += toml/toml.c
DEBUG_DEFINES = -DDEBUGBUILD
RELEASE_DEFINES =

View File

@ -710,21 +710,16 @@ DRESULT sdio_disk_write(const BYTE *buff, DWORD sector, UINT count)
addr = (card_info.type == SD_V2_HC ? (sector) : (sector * 512));
while (count) {
do {
ret = sdio_check_status_register_cmd13(card_info.rca, &status.value);
} while (status.statusstruct.CURRENT_STATE == CURRENT_STATE_PRG ||
status.statusstruct.CURRENT_STATE == CURRENT_STATE_RCV ||
ret);
if (ret)
return RES_ERROR;
if (status.statusstruct.CURRENT_STATE == CURRENT_STATE_STBY) {
if (sdio_send_select_card_cmd7(card_info.rca))
return RES_ERROR;
}
do {
sdio_check_status_register_cmd13(card_info.rca, &status.value);
} while (status.statusstruct.READY_FOR_DATA != 1);
ret = sdio_send_write_block_cmd24(addr);
if (ret) {
return RES_ERROR;

View File

@ -12,9 +12,9 @@
//4 bit: 4
#define BUSWIDTH 4 //4
//Initial Transfer CLK (ca. 400kHz)
#define INITCLK 130 //120
#define INITCLK 140 //120
//Working CLK (Maximum)
#define WORKCLK 50 //0
#define WORKCLK 45 //0
//Data Timeout in CLK Cycles
#define DTIMEOUT 0x3000 //150
//DMA Stream used for TX and RX DMA2 Stream 3 or 6 possible

View File

@ -23,8 +23,16 @@
#include <stdbool.h>
#define CALIBRATION_FILE_NAME "cal.toml"
#define CALIBRATION_FILE_NAME "settings.ini"
int sd_card_settings_save_calibration(float sens_deviation, float offset, bool active);
/**
* @brief Try and load calibration from SD card
* @param sens_deviation
* @param offset
* @return 0 if files found -1 if files errorneous, -2 if no files found
*/
int sd_card_settings_try_load_calibration(float *sens_deviation, float *offset);
#endif /* __SETTINGS_SETTINGS_SD_CARD_H__ */

View File

@ -32,4 +32,6 @@
*/
int settings_save_calibration(float sens_deviation, float offset, bool active);
int settings_load_calibration(float *sens_dev, float *offset);
#endif /* __SETTINGS_SETTINGS_H__ */

View File

@ -1,150 +0,0 @@
/*
MIT License
Copyright (c) 2017 - 2019 CK Tan
https://github.com/cktan/tomlc99
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef TOML_H
#define TOML_H
#include <stdio.h>
#include <stdint.h>
#ifdef __cplusplus
#define TOML_EXTERN extern "C"
#else
#define TOML_EXTERN extern
#endif
typedef struct toml_table_t toml_table_t;
typedef struct toml_array_t toml_array_t;
/* A raw value, must be processed by toml_rto* before using. */
typedef const char* toml_raw_t;
/* Parse a file. Return a table on success, or 0 otherwise.
* Caller must toml_free(the-return-value) after use.
*/
TOML_EXTERN toml_table_t* toml_parse_file(FILE* fp,
char* errbuf,
int errbufsz);
/* Parse a string containing the full config.
* Return a table on success, or 0 otherwise.
* Caller must toml_free(the-return-value) after use.
*/
TOML_EXTERN toml_table_t* toml_parse(char* conf, /* NUL terminated, please. */
char* errbuf,
int errbufsz);
/* Free the table returned by toml_parse() or toml_parse_file(). */
TOML_EXTERN void toml_free(toml_table_t* tab);
/* Retrieve the key in table at keyidx. Return 0 if out of range. */
TOML_EXTERN const char* toml_key_in(const toml_table_t* tab, int keyidx);
/* Lookup table by key. Return the element or 0 if not found. */
TOML_EXTERN toml_raw_t toml_raw_in(const toml_table_t* tab, const char* key);
TOML_EXTERN toml_array_t* toml_array_in(const toml_table_t* tab,
const char* key);
TOML_EXTERN toml_table_t* toml_table_in(const toml_table_t* tab,
const char* key);
/* Return the array kind: 't'able, 'a'rray, 'v'alue */
TOML_EXTERN char toml_array_kind(const toml_array_t* arr);
/* For array kind 'v'alue, return the type of values
i:int, d:double, b:bool, s:string, t:time, D:date, T:timestamp
0 if unknown
*/
TOML_EXTERN char toml_array_type(const toml_array_t* arr);
/* Return the number of elements in the array */
TOML_EXTERN int toml_array_nelem(const toml_array_t* arr);
/* Return the key of an array */
TOML_EXTERN const char* toml_array_key(const toml_array_t* arr);
/* Return the number of key-values in a table */
TOML_EXTERN int toml_table_nkval(const toml_table_t* tab);
/* Return the number of arrays in a table */
TOML_EXTERN int toml_table_narr(const toml_table_t* tab);
/* Return the number of sub-tables in a table */
TOML_EXTERN int toml_table_ntab(const toml_table_t* tab);
/* Return the key of a table*/
TOML_EXTERN const char* toml_table_key(const toml_table_t* tab);
/* Deref array by index. Return the element at idx or 0 if out of range. */
TOML_EXTERN toml_raw_t toml_raw_at(const toml_array_t* arr, int idx);
TOML_EXTERN toml_array_t* toml_array_at(const toml_array_t* arr, int idx);
TOML_EXTERN toml_table_t* toml_table_at(const toml_array_t* arr, int idx);
/* Raw to String. Caller must call free(ret) after use.
* Return 0 on success, -1 otherwise.
*/
TOML_EXTERN int toml_rtos(toml_raw_t s, char** ret);
/* Raw to Boolean. Return 0 on success, -1 otherwise. */
TOML_EXTERN int toml_rtob(toml_raw_t s, int* ret);
/* Raw to Integer. Return 0 on success, -1 otherwise. */
TOML_EXTERN int toml_rtoi(toml_raw_t s, int64_t* ret);
/* Raw to Double. Return 0 on success, -1 otherwise. */
TOML_EXTERN int toml_rtod(toml_raw_t s, double* ret);
/* Same as toml_rtod, but return the sanitized double in string form as well */
TOML_EXTERN int toml_rtod_ex(toml_raw_t s, double* ret, char* buf, int buflen);
/* Timestamp types. The year, month, day, hour, minute, second, z
* fields may be NULL if they are not relevant. e.g. In a DATE
* type, the hour, minute, second and z fields will be NULLs.
*/
typedef struct toml_timestamp_t toml_timestamp_t;
struct toml_timestamp_t {
struct { /* internal. do not use. */
int year, month, day;
int hour, minute, second, millisec;
char z[10];
} __buffer;
int *year, *month, *day;
int *hour, *minute, *second, *millisec;
char* z;
};
/* Raw to Timestamp. Return 0 on success, -1 otherwise. */
TOML_EXTERN int toml_rtots(toml_raw_t s, toml_timestamp_t* ret);
/* misc */
TOML_EXTERN int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret);
TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]);
TOML_EXTERN void toml_set_memutil(void* (*xxmalloc)(size_t),
void (*xxfree)(void*),
void* (*xxcalloc)(size_t, size_t),
void* (*xxrealloc)(void*, size_t));
#endif /* TOML_H */

View File

@ -20,8 +20,11 @@
#include <reflow-controller/settings/settings-sd-card.h>
#include <stm-periph/unique-id.h>
#include <toml/toml.h>
#include <fatfs/ff.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void get_controller_folder_path(char *path, size_t size)
{
@ -38,6 +41,14 @@ static void get_controller_folder_path(char *path, size_t size)
(unsigned int)high, (unsigned int)mid, (unsigned int)low);
}
static void get_controller_settings_path(char *path, size_t size, const char *setting)
{
char folder[48];
get_controller_folder_path(folder, sizeof(folder));
snprintf(path, size, "%s/%s.dat", folder, setting);
}
/**
* @brief Open or create the controller folder on the SD Card.
* @param[in,out] controller_folder
@ -67,50 +78,108 @@ static int create_controller_folder(void)
}
}
if (ret >= 0)
f_chdir(foldername);
return ret;
}
static int read_settings_file_float(const char *path, float *value)
{
FRESULT res;
FIL file;
int ret = 0;
char buff[32];
UINT read_count;
if (!value)
return -1002;
res = f_open(&file, path, FA_READ);
if (res == FR_OK) {
memset(buff, 0, sizeof(buff));
res = f_read(&file, buff, sizeof(buff)-1, &read_count);
if (res != FR_OK) {
ret = -1;
goto close_file;
}
*value = strtof(buff, NULL);
close_file:
f_close(&file);
} else {
ret = -2;
}
return ret;
}
int sd_card_settings_save_calibration(float sens_deviation, float offset, bool active)
{
FIL cal_file;
int status;
FRESULT res;
char path[128];
char buff[64];
UINT bw;
FIL file;
FRESULT res;
int ret = 0;
char buff[256];
status = create_controller_folder();
if (status < 0)
if (status)
return -2;
/* Create new calibration file */
res = f_open(&cal_file, CALIBRATION_FILE_NAME, FA_CREATE_ALWAYS | FA_WRITE);
if (res == FR_OK) {
status = snprintf(buff, sizeof(buff), "[cal]\noffset = %f\nsensdev = %f\nactive = %s\n",
offset, sens_deviation, (active ? "true" : "false"));
if ((unsigned int)status < sizeof(buff) && status >= 0) {
f_write(&cal_file, buff, status, &bw);
if (bw == (unsigned int)status)
ret = 0;
else
ret = -3;
} else {
ret = -4;
get_controller_settings_path(path, sizeof(path), "offset");
if (active) {
res = f_open(&file, path, FA_CREATE_ALWAYS | FA_WRITE);
if (res != FR_OK) {
ret = -2;
goto exit_offset;
}
f_close(&cal_file);
status = snprintf(buff, sizeof(buff), "%f\n", offset);
f_write(&file, buff, status, &bw);
f_close(&file);
} else {
ret = -1;
f_unlink(path);
}
exit_offset:
get_controller_settings_path(path, sizeof(path), "sens");
if (active) {
res = f_open(&file, path, FA_CREATE_ALWAYS | FA_WRITE);
if (res != FR_OK) {
ret = -2;
goto exit_sens;
}
f_chdir("/");
status = snprintf(buff, sizeof(buff), "%f\n", sens_deviation);
f_write(&file, buff, status, &bw);
f_close(&file);
} else {
f_unlink(path);
}
exit_sens:
return ret;
}
int sd_card_settings_try_load_calibration(float *sens_deviation, float *offset)
{
char path[128];
int status;
int ret = -2;
if (!sens_deviation || !offset)
return -1000;
get_controller_settings_path(path, sizeof(path), "offset");
status = read_settings_file_float(path, offset);
if (status) {
ret = status;
goto exit;
}
get_controller_settings_path(path, sizeof(path), "sens");
status = read_settings_file_float(path, sens_deviation);
if (status) {
ret = status;
goto exit;
}
exit:
return ret;
}

View File

@ -21,8 +21,13 @@
#include <reflow-controller/settings/settings.h>
#include <reflow-controller/settings/settings-sd-card.h>
settings_save_calibration(float sens_deviation, float offset, bool active)
int settings_save_calibration(float sens_deviation, float offset, bool active)
{
/* There is no other configuration location besides the SD card (yet) */
return sd_card_settings_save_calibration(sens_deviation, offset, active);
}
int settings_load_calibration(float *sens_dev, float *offset)
{
return sd_card_settings_try_load_calibration(sens_dev, offset);
}

File diff suppressed because it is too large Load Diff