Improvements

This commit is contained in:
Candas1 2021-01-06 12:54:08 +01:00
parent 7a6dc017a6
commit cd40425e99
2 changed files with 150 additions and 97 deletions

View File

@ -70,6 +70,7 @@ void handle_input(uint8_t *userCommand, uint32_t len);
typedef struct command_entry_struct command_entry; typedef struct command_entry_struct command_entry;
struct command_entry_struct { struct command_entry_struct {
const uint8_t type;
const char *name; const char *name;
int8_t (*callback_function0)(); int8_t (*callback_function0)();
int8_t (*callback_function1)(uint8_t index); int8_t (*callback_function1)(uint8_t index);
@ -84,7 +85,7 @@ struct parameter_entry_struct {
const uint8_t datatype; const uint8_t datatype;
void *valueL; void *valueL;
void *valueR; void *valueR;
const int32_t addr; const uint16_t addr;
const int32_t init; const int32_t init;
const int32_t min; const int32_t min;
const int32_t max; const int32_t max;
@ -92,7 +93,6 @@ struct parameter_entry_struct {
const uint8_t mul; const uint8_t mul;
const uint8_t fix; const uint8_t fix;
void (*callback_function)(); void (*callback_function)();
uint8_t watch;
const char *help; const char *help;
}; };

View File

@ -19,6 +19,8 @@
// Includes // Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "stm32f1xx_hal.h" #include "stm32f1xx_hal.h"
#include "config.h" #include "config.h"
@ -38,65 +40,89 @@ extern ExtY rtY_Right; /* External outputs */
extern ExtU rtU_Right; /* External inputs */ extern ExtU rtU_Right; /* External inputs */
extern P rtP_Right; extern P rtP_Right;
extern InputStruct input1[]; // input structure
extern InputStruct input2[]; // input structure
extern uint16_t VirtAddVarTab[NB_OF_VAR]; extern uint16_t VirtAddVarTab[NB_OF_VAR];
extern int16_t speedAvg; // average measured speed extern int16_t speedAvg; // average measured speed
extern int16_t speedAvgAbs; // average measured speed in absolute extern int16_t speedAvgAbs; // average measured speed in absolute
extern uint8_t ctrlModReqRaw; extern uint8_t ctrlModReqRaw;
extern adc_buf_t adc_buffer;
extern int16_t board_temp_deg_c;
enum commandTypes {READ,WRITE};
// Function0 - Function with 0 parameter // Function0 - Function with 0 parameter
// Function1 - Function with 1 parameter (e.g. GET PARAM) // Function1 - Function with 1 parameter (e.g. GET PARAM)
// Function2 - Function with 2 parameter (e.g. SET PARAM XXXX) // Function2 - Function with 2 parameter (e.g. SET PARAM XXXX)
command_entry commands[] = { const command_entry commands[] = {
// Name Function0 Function1 Function2 Help // Type ,Name ,Function0 ,Function1 ,Function2 ,Help
{"GET" ,printAllParamDef ,printParamDef ,NULL ,"Get Parameter/Variable Values"}, {READ ,"GET" ,printAllParamDef ,printParamDef ,NULL ,"Get Parameter/Variable"},
{"SET" ,NULL ,NULL ,setParamValExt ,"Set Parameter with Value"}, {READ ,"HELP" ,printAllParamHelp ,printParamHelp ,NULL ,"Command/Parameter/Variable Help"},
{"INIT" ,NULL ,initParamVal ,NULL ,"Init Parameter with Value from EEPROM or CONFIG.H"}, {READ ,"WATCH" ,NULL ,watchParamVal ,NULL ,"Toggle Parameter/Variable Watch"},
{"SAVE" ,NULL ,saveParamVal ,NULL ,"Save Parameter Value to EEPROM"}, {WRITE ,"SET" ,NULL ,NULL ,setParamValExt ,"Set Parameter"},
{"HELP" ,printAllParamHelp ,printParamHelp ,NULL ,"Show Help"}, {WRITE ,"INIT" ,NULL ,initParamVal ,NULL ,"Init Parameter from EEPROM or CONFIG.H"},
{"WATCH" ,NULL ,watchParamVal ,NULL ,"Enable/Disable Watch for Parameter/Variable"}, {WRITE ,"SAVE" ,NULL ,saveParamVal ,NULL ,"Save Parameter to EEPROM"},
}; };
enum paramTypes {PARAMETER,VARIABLE}; enum paramTypes {PARAMETER,VARIABLE};
// Keywords to match with param index const parameter_entry params[] = {
enum parameters {PCTRL_MOD_REQ, // CONTROL PARAMETERS
PCTRL_TYP_SEL, // Type ,Name ,Datatype ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text
PI_MOT_MAX, {PARAMETER ,"CTRL_MOD" ,ADD_PARAM(ctrlModReqRaw) ,NULL ,0 ,CTRL_MOD_REQ ,1 ,3 ,0 ,0 ,0 ,NULL ,"Ctrl mode 1:VLT 2:SPD 3:TRQ"},
PN_MOT_MAX, {PARAMETER ,"CTRL_TYP" ,ADD_PARAM(rtP_Left.z_ctrlTypSel) ,&rtP_Right.z_ctrlTypSel ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,0 ,NULL ,"Ctrl type 0:COM 1:SIN 2:FOC"},
PFIELD_WEAK_ENA, {PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max) ,&rtP_Right.i_max ,1 ,I_MOT_MAX ,1 ,40 ,A2BIT_CONV ,0 ,4 ,NULL ,"Max phase current A"},
PFIELD_WEAK_HI, {PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max) ,&rtP_Right.n_max ,2 ,N_MOT_MAX ,10 ,2000 ,0 ,0 ,4 ,NULL ,"Max motor RPM"},
PFIELD_WEAK_LO, {PARAMETER ,"FI_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna) ,&rtP_Right.b_fieldWeakEna ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,0 ,NULL ,"Enable field weak"},
PFIELD_WEAK_MAX, {PARAMETER ,"FI_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi) ,&rtP_Right.r_fieldWeakHi ,0 ,FIELD_WEAK_HI ,0 ,1500 ,0 ,0 ,4 ,Input_Lim_Init ,"Field weak high RPM"},
PPHASE_ADV_MAX, {PARAMETER ,"FI_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo) ,&rtP_Right.r_fieldWeakLo ,0 ,FIELD_WEAK_LO ,0 ,1000 ,0 ,0 ,4 ,Input_Lim_Init ,"Field weak low RPM"},
VI_DC_LINK, {PARAMETER ,"FI_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax) ,&rtP_Right.id_fieldWeakMax,0 ,FIELD_WEAK_MAX ,0 ,20 ,A2BIT_CONV ,0 ,4 ,NULL ,"Field weak max current A(FOC)"},
VSPEED_AVG, {PARAMETER ,"PHA_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax) ,&rtP_Right.a_phaAdvMax ,0 ,PHASE_ADV_MAX ,0 ,55 ,0 ,0 ,4 ,NULL ,"Max Phase Adv angle Deg(SIN)"},
VRATE, // INPUT PARAMETERS
VSPEED_COEF, // Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text
VSTEER_COEF, {PARAMETER ,"PRI_IN1_TYP" ,ADD_PARAM(input1[0].typ) ,NULL ,3 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Input1 type"},
}; {PARAMETER ,"PRI_IN1_MIN" ,ADD_PARAM(input1[0].min) ,NULL ,4 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 min"},
{PARAMETER ,"PRI_IN1_MID" ,ADD_PARAM(input1[0].mid) ,NULL ,5 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 mid"},
parameter_entry params[] = { {PARAMETER ,"PRI_IN1_MAX" ,ADD_PARAM(input1[0].max) ,NULL ,6 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 max"},
// Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Watch ,Help text {PARAMETER ,"PRI_IN2_TYP" ,ADD_PARAM(input2[0].typ) ,NULL ,7 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Input2 type"},
{PARAMETER ,"CTRL_MOD_REQ" ,ADD_PARAM(ctrlModReqRaw) ,NULL ,0 ,CTRL_MOD_REQ ,1 ,3 ,0 ,0 ,0 ,NULL ,0 ,"Ctrl mode [1] voltage [2] Speed [3] Torque"}, {PARAMETER ,"PRI_IN2_MIN" ,ADD_PARAM(input2[0].min) ,NULL ,8 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 min"},
{PARAMETER ,"CTRL_TYP_SEL" ,ADD_PARAM(rtP_Left.z_ctrlTypSel) ,&rtP_Right.z_ctrlTypSel ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,0 ,NULL ,0 ,"Ctrl type [0] Commutation [1] Sinusoidal [2] FOC"}, {PARAMETER ,"PRI_IN2_MID" ,ADD_PARAM(input2[0].mid) ,NULL ,9 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 mid"},
{PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max) ,&rtP_Right.i_max ,1 ,I_MOT_MAX ,1 ,40 ,A2BIT_CONV ,0 ,4 ,NULL ,0 ,"Maximum phase current [A]"}, {PARAMETER ,"PRI_IN2_MAX" ,ADD_PARAM(input2[0].max) ,NULL ,10 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 max"},
{PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max) ,&rtP_Right.n_max ,2 ,N_MOT_MAX ,10 ,2000 ,0 ,0 ,4 ,NULL ,0 ,"Maximum motor [RPM]"}, {VARIABLE ,"PRI_IN1_RAW" ,ADD_PARAM(input1[0].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 raw"},
{PARAMETER ,"FIELD_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna) ,&rtP_Right.b_fieldWeakEna ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,0 ,NULL ,0 ,"Enable field weakening"}, {VARIABLE ,"PRI_IN2_RAW" ,ADD_PARAM(input2[0].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 raw"},
{PARAMETER ,"FIELD_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi) ,&rtP_Right.r_fieldWeakHi ,0 ,FIELD_WEAK_HI ,0 ,1500 ,0 ,0 ,4 ,Input_Lim_Init ,0 ,"Field weak high [RPM]"}, {VARIABLE ,"PRI_IN1_CMD" ,ADD_PARAM(input1[0].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 cmd"},
{PARAMETER ,"FIELD_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo) ,&rtP_Right.r_fieldWeakLo ,0 ,FIELD_WEAK_LO ,0 ,1000 ,0 ,0 ,4 ,Input_Lim_Init ,0 ,"Field weak low [RPM)"}, {VARIABLE ,"PRI_IN2_CMD" ,ADD_PARAM(input2[0].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 cmd"},
{PARAMETER ,"FIELD_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax) ,&rtP_Right.id_fieldWeakMax,0 ,FIELD_WEAK_MAX ,0 ,20 ,A2BIT_CONV ,0 ,4 ,NULL ,0 ,"Field weak max current [A](only for FOC)"}, #if defined(PRI_INPUT1) && defined(PRI_INPUT2) && defined(AUX_INPUT1) && defined(AUX_INPUT2)
{PARAMETER ,"PHASE_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax) ,&rtP_Right.a_phaAdvMax ,0 ,PHASE_ADV_MAX ,0 ,55 ,0 ,0 ,4 ,NULL ,0 ,"Maximum Phase Advance angle [Deg](only for SIN)"}, {PARAMETER ,"AUX_IN1_TYP" ,ADD_PARAM(input1[1].typ) ,NULL ,11 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Aux. input1 type"},
{VARIABLE ,"I_DC_LINK" ,ADD_PARAM(rtU_Left.i_DCLink) ,&rtU_Right.i_DCLink ,0 ,0 ,0 ,0 ,A2BIT_CONV ,0 ,0 ,NULL ,0 ,"DC Link current [A]"}, {PARAMETER ,"AUX_IN1_MIN" ,ADD_PARAM(input1[1].min) ,NULL ,12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 min"},
{VARIABLE ,"SPEED_AVG" ,ADD_PARAM(speedAvg) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Motor Measured Average Speed [RPM]"}, {PARAMETER ,"AUX_IN1_MID" ,ADD_PARAM(input1[1].mid) ,NULL ,13 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 mid"},
{VARIABLE ,"SPEEDL" ,ADD_PARAM(rtY_Left.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Left Motor Measured Speed [RPM]"}, {PARAMETER ,"AUX_IN1_MAX" ,ADD_PARAM(input1[1].max) ,NULL ,14 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 max"},
{VARIABLE ,"SPEEDR" ,ADD_PARAM(rtY_Right.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Right Motor Measured Speed [RPM]"}, {PARAMETER ,"AUX_IN2_TYP" ,ADD_PARAM(input2[1].typ) ,NULL ,15 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Aux. input2 type"},
{VARIABLE ,"RATE" ,0 , NULL ,NULL ,0 ,RATE ,0 ,0 ,0 ,0 ,4 ,NULL ,0 ,"Rate *10"}, {PARAMETER ,"AUX_IN2_MIN" ,ADD_PARAM(input2[1].min) ,NULL ,16 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 min"},
{VARIABLE ,"SPEED_COEF" ,0 , NULL ,NULL ,0 ,SPEED_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,0 ,"Speed Coefficient *10"}, {PARAMETER ,"AUX_IN2_MID" ,ADD_PARAM(input2[1].mid) ,NULL ,17 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 mid"},
{VARIABLE ,"STEER_COEF" ,0 , NULL ,NULL ,0 ,STEER_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,0 ,"Steer Coefficient *10"}, {PARAMETER ,"AUX_IN2_MAX" ,ADD_PARAM(input2[1].max) ,NULL ,18 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 max"},
//{VARIABLE ,"BATV" ,ADD_PARAM(adc_buffer.batt1) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Battery voltage [V]*100"}, {VARIABLE ,"AUX_IN1_RAW" ,ADD_PARAM(input1[1].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 raw"},
//{VARIABLE ,"TEMP" ,ADD_PARAM(board_temp_deg_c) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Temperature [°C]*10"}, {VARIABLE ,"AUX_IN2_RAW" ,ADD_PARAM(input2[1].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 raw"},
{VARIABLE ,"AUX_IN1_CMD" ,ADD_PARAM(input1[1].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 cmd"},
{VARIABLE ,"AUX_IN2_CMD" ,ADD_PARAM(input2[1].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 cmd"},
#endif
// FEEDBACK
// Type ,Name ,Datatype, ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text
{VARIABLE ,"I_DC_LINK" ,ADD_PARAM(rtU_Left.i_DCLink) ,&rtU_Right.i_DCLink ,0 ,0 ,0 ,0 ,A2BIT_CONV ,0 ,0 ,NULL ,"DC Link current A"},
{VARIABLE ,"SPD_AVG" ,ADD_PARAM(speedAvg) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Motor Measured Avg RPM"},
{VARIABLE ,"SPDL" ,ADD_PARAM(rtY_Left.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Left Motor Measured RPM"},
{VARIABLE ,"SPDR" ,ADD_PARAM(rtY_Right.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Right Motor Measured RPM"},
{VARIABLE ,"RATE" ,0 , NULL ,NULL ,0 ,RATE ,0 ,0 ,0 ,0 ,4 ,NULL ,"Rate *10"},
{VARIABLE ,"SPD_COEF" ,0 , NULL ,NULL ,0 ,SPEED_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,"Speed Coefficient *10"},
{VARIABLE ,"STR_COEF" ,0 , NULL ,NULL ,0 ,STEER_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,"Steer Coefficient *10"},
{VARIABLE ,"BATV" ,ADD_PARAM(adc_buffer.batt1) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Battery voltage *100"},
//{VARIABLE ,"TEMP" ,ADD_PARAM(board_temp_deg_c) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Temperature °C *10"},
}; };
uint8_t * watchParamList;
// Translate from External format to Internal Format // Translate from External format to Internal Format
int32_t ExtToInt(uint8_t index,int32_t value){ int32_t ExtToInt(uint8_t index,int32_t value){
// Multiply to translate to internal format // Multiply to translate to internal format
@ -104,23 +130,17 @@ int32_t ExtToInt(uint8_t index,int32_t value){
// Shift to translate to internal format // Shift to translate to internal format
if (params[index].fix) value <<= params[index].fix; if (params[index].fix) value <<= params[index].fix;
// Divide for small number // Divide for small number
if(params[index].mul)value /= params[index].mul; if(params[index].mul) value /= params[index].mul;
return value; return value;
} }
// Set Param with Value from external format // Set Param with Value from external format
int8_t setParamValExt(uint8_t index, int32_t value) { int8_t setParamValExt(uint8_t index, int32_t value) {
// Only Parameters can be set
if (params[index].type == VARIABLE){
printf("! A variable cannot be SET\r\n");
return 0;
}
// check min and max before conversion to internal values // check min and max before conversion to internal values
if (IN_RANGE(value,params[index].min,params[index].max)){ if (IN_RANGE(value,params[index].min,params[index].max)){
return setParamValInt(index,ExtToInt(index,value)); return setParamValInt(index,ExtToInt(index,value));
}else{ }else{
printf("! Value %li not in range [min:%li max:%li]\r\n",value,params[index].min,params[index].max); printf("! Value %li out of range [min:%li max:%li]",value,params[index].min,params[index].max);
return 0; return 0;
} }
} }
@ -172,7 +192,7 @@ int32_t getParamValExt(uint8_t index) {
int32_t getParamValInt(uint8_t index) { int32_t getParamValInt(uint8_t index) {
int32_t value = 0; int32_t value = 0;
int countVar = 0; int8_t countVar = 0;
if (params[index].valueL != NULL) countVar++; if (params[index].valueL != NULL) countVar++;
if (params[index].valueR != NULL) countVar++; if (params[index].valueR != NULL) countVar++;
@ -222,20 +242,31 @@ int32_t getParamValInt(uint8_t index) {
// Set watch flag for parameter // Set watch flag for parameter
int8_t watchParamVal(uint8_t index){ int8_t watchParamVal(uint8_t index){
params[index].watch = (params[index].watch==0); boolean_T found = 0;
uint8_t size = sizeof(watchParamList);
for(int i=0;i<size;i++){
if (watchParamList[i] == index) found = 1;
if ( found && i < size - 1 ) watchParamList[i] = watchParamList[i+1];
}
if (found){size--;}else{size++;}
if (size == 0){
free(watchParamList);
}else{
watchParamList = (uint8_t*) realloc(watchParamList, size * sizeof(uint8_t));
}
if (!found && watchParamList != NULL) watchParamList[size-1] = index;
return 1; return 1;
} }
// Print value for all parameters with watch flag // Print value for all parameters with watch flag
int8_t printParamVal(){ int8_t printParamVal(){
int count = 0; for(int i=0;i<sizeof(watchParamList);i++){
for(int i=0;i<PARAM_SIZE(params);i++){ printf("%s:%li ",params[watchParamList[i]].name,getParamValExt(i));
if (params[i].watch){
printf("%s:%li ",params[i].name,getParamValExt(i));
count++;
}
} }
if (count) printf("\r\n"); if (sizeof(watchParamList)>0) printf("\r\n");
return 1; return 1;
} }
@ -255,15 +286,23 @@ int8_t printParamHelp(uint8_t index){
// Print help for all parameters // Print help for all parameters
int8_t printAllParamHelp(){ int8_t printAllParamHelp(){
printf("? Commands:\r\n"); printf("? Commands\r\n");
for(int i=0;i<COMMAND_SIZE(commands);i++) for(int i=0;i<COMMAND_SIZE(commands);i++)
printCommandHelp(i); printCommandHelp(i);
printf("\r\n"); printf("\r\n");
printf("? Parameters/Variable:\r\n"); printf("? Parameters\r\n");
for(int i=0;i<PARAM_SIZE(params);i++) for(int i=0;i<PARAM_SIZE(params);i++){
printParamHelp(i); if (params[i].type == PARAMETER) printParamHelp(i);
}
printf("\r\n");
printf("? Variables\r\n");
for(int i=0;i<PARAM_SIZE(params);i++){
if (params[i].type == VARIABLE) printParamHelp(i);
}
printf("\r\n");
return 1; return 1;
} }
@ -280,15 +319,13 @@ int8_t printParamDef(uint8_t index){
// Print definition for all parameters // Print definition for all parameters
int8_t printAllParamDef(){ int8_t printAllParamDef(){
for(int i=0;i<PARAM_SIZE(params);i++) for(int i=0;i<PARAM_SIZE(params);i++) printParamDef(i);
printParamDef(i);
return 1; return 1;
} }
int8_t incrParamVal(uint8_t index) {
// Only Parameters can be set
if (params[index].type == VARIABLE) return 0;
// Get Parameter in External format, check max value, increment, set Parameter
int8_t incrParamVal(uint8_t index) {
uint32_t value = getParamValExt(index); uint32_t value = getParamValExt(index);
if (value < params[index].max){ if (value < params[index].max){
return setParamValExt(index,value + 1); return setParamValExt(index,value + 1);
@ -299,16 +336,24 @@ int8_t incrParamVal(uint8_t index) {
// Get internal Parameter value and save it to EEprom if address is assigned // Get internal Parameter value and save it to EEprom if address is assigned
int8_t saveParamVal(uint8_t index) { int8_t saveParamVal(uint8_t index) {
HAL_FLASH_Unlock(); // Only Parameters with eeprom address can be saved
int found = 0; if (params[index].addr){
for(int i=0;i<PARAM_SIZE(params);i++){ uint16_t writeCheck;
if (params[i].type != VARIABLE && params[i].addr){
EE_WriteVariable(VirtAddVarTab[params[i].addr] , (uint16_t)getParamValInt(i)); HAL_FLASH_Unlock();
found = 1; EE_ReadVariable(VirtAddVarTab[0], &writeCheck);
if (writeCheck != FLASH_WRITE_KEY){
// Flash Write Key is different, EEPROM should be wiped out
for(int i=0;i< sizeof(VirtAddVarTab)/sizeof(uint16_t);i++)
EE_WriteVariable(VirtAddVarTab[i] , 0);
EE_WriteVariable(VirtAddVarTab[0] , (uint16_t)FLASH_WRITE_KEY);
} }
EE_WriteVariable(VirtAddVarTab[params[index].addr] , (uint16_t)getParamValInt(index));
HAL_FLASH_Lock();
return 1;
}else{
return 0;
} }
HAL_FLASH_Lock();
return found;
} }
int32_t IntToExt(uint8_t index,int32_t value){ int32_t IntToExt(uint8_t index,int32_t value){
@ -322,22 +367,26 @@ int32_t IntToExt(uint8_t index,int32_t value){
} }
int32_t getParamInitExt(uint8_t index) { int32_t getParamInitExt(uint8_t index) {
int32_t value = 0; return IntToExt(index,getParamInitInt(index));
value = IntToExt(index,getParamInitInt(index));
return value;
} }
// Get Parameter value with EEprom data if address is avalaible, init value otherwise // Get Parameter value with EEprom data if address is avalaible, init value otherwise
int16_t getParamInitInt(uint8_t index) { int16_t getParamInitInt(uint8_t index){
if (params[index].addr){ if (params[index].addr){
// if EEPROM address is specified, init from EEPROM address // if EEPROM address is specified, init from EEPROM address
uint16_t readEEPROMVal; uint16_t writeCheck, readVal;
HAL_FLASH_Unlock(); HAL_FLASH_Unlock();
EE_ReadVariable(VirtAddVarTab[params[index].addr] , &readEEPROMVal); EE_ReadVariable(VirtAddVarTab[0], &writeCheck);
EE_ReadVariable(VirtAddVarTab[params[index].addr] , &readVal);
HAL_FLASH_Lock(); HAL_FLASH_Lock();
return readEEPROMVal;
if (writeCheck == FLASH_WRITE_KEY){
return readVal;
}else{
return params[index].init;
}
}else{ }else{
// Initialize from param array
return params[index].init; return params[index].init;
} }
} }
@ -345,16 +394,13 @@ int16_t getParamInitInt(uint8_t index) {
// initialize Parameter value with EEprom data if address is avalaible, init value otherwise // initialize Parameter value with EEprom data if address is avalaible, init value otherwise
int8_t initParamVal(uint8_t index) { int8_t initParamVal(uint8_t index) {
// Only Parameters can be loaded from EEPROM
if (params[index].type == VARIABLE) return 0;
return setParamValInt(index,(int32_t) getParamInitInt(index)); return setParamValInt(index,(int32_t) getParamInitInt(index));
} }
// Find parameter in params array and return index // Find parameter in params array and return index
int8_t findParam(uint8_t *userCommand, uint32_t len){ int8_t findParam(uint8_t *userCommand, uint32_t len){
for(int index=0;index<PARAM_SIZE(params);index++){ for(int index=0;index<PARAM_SIZE(params);index++){
int param_len = strlen(params[index].name); uint8_t param_len = strlen(params[index].name);
if (param_len < len){ if (param_len < len){
if (memcmp(userCommand,params[index].name,param_len)==0){ if (memcmp(userCommand,params[index].name,param_len)==0){
return index; return index;
@ -367,7 +413,7 @@ int8_t findParam(uint8_t *userCommand, uint32_t len){
// Find command in commands array and return index // Find command in commands array and return index
int8_t findCommand(uint8_t *userCommand, uint32_t len){ int8_t findCommand(uint8_t *userCommand, uint32_t len){
for(int index=0;index<COMMAND_SIZE(commands);index++){ for(int index=0;index<COMMAND_SIZE(commands);index++){
int command_len = strlen(commands[index].name); uint8_t command_len = strlen(commands[index].name);
if (command_len < len){ if (command_len < len){
if (memcmp(userCommand,commands[index].name,command_len)==0){ if (memcmp(userCommand,commands[index].name,command_len)==0){
return index; return index;
@ -377,8 +423,10 @@ int8_t findCommand(uint8_t *userCommand, uint32_t len){
return -1; // Not found return -1; // Not found
} }
void handle_input(uint8_t *userCommand, uint32_t len) void handle_input(uint8_t *userCommand, uint32_t len)
{ {
int8_t cindex = -1; int8_t cindex = -1;
int8_t pindex = -1; int8_t pindex = -1;
uint8_t size = 0; uint8_t size = 0;
@ -412,6 +460,11 @@ void handle_input(uint8_t *userCommand, uint32_t len)
return; return;
} }
if (commands[cindex].type == WRITE && params[pindex].type == VARIABLE){
printf("! This command cannot be used with a Variable\r\n");
return;
}
if (commands[cindex].callback_function1 != NULL){ if (commands[cindex].callback_function1 != NULL){
// This function needs only a parameter // This function needs only a parameter
ret = (*commands[cindex].callback_function1)(pindex); ret = (*commands[cindex].callback_function1)(pindex);