Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for "calculated" MBFAN tacho signals. #59

Merged
merged 4 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)


project(fanpico
VERSION 1.5.1
VERSION 1.5.2
LANGUAGES C CXX ASM
)
set(CMAKE_C_STANDARD 11)
Expand Down
16 changes: 13 additions & 3 deletions commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,14 @@ CONF:MBFAN1:RPMF?
Configure source for the Tachometer (RPM) signal for a motheboard fan (output) port.

Source types:
* FAN (signal received from a fan)
* FIXED (static signal at given RPM)

Type|Parameters|Description|Example
----|----------|-----------|------
FAN|n|Return tachometer signal of a specified fan|FAN,1
FIXED|rpm|Return static tachometer signal at specified rpm|FIXED,1500
MIN|n1,n2,...|Return slowest FAN speed acros specified fans|MIN,2,7,8
MAX|n1,n2,...|Return fastest FAN speed acros specified fans|MAX,2,7,8
AVG|n1,n2,...|Return average FAN speed acros specified fans|AVG,2,7,8

Defaults:
MBFAN|SOURCE
Expand All @@ -582,6 +587,11 @@ Example: Set MBFAN 4 to see fixed 1500 RPM tachometer signal
CONF:MBFAN4:SOURCE FIXED,1500
```

Example: Set MBFAN 1 to return slowest fan speed from FAN1, FAN2, and FAN3
```
CONF:MBFAN1:SOURCE MIN,1,2,3
```

#### CONFigure:MBFANx:SOUrce?
Query current signal source for a motherboar fan (Tachometer output).

Expand Down Expand Up @@ -615,7 +625,7 @@ Example: Assuming we have configured FAN1 to only run above 30% PWM signal, but
motherboard sets alarm when it doesn't detect fan running, we can provide 'fake' 500RPM
speed to the motherboard until fan is actually running at least 500 RPM...
```
CONF:MBFAN1:PWMMAP 0,500,500,500,10000,10000
CONF:MBFAN1:RPMMAP 0,500,500,500,10000,10000
```

#### CONFigure:MBFANx:RPMMap?
Expand Down
84 changes: 67 additions & 17 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -936,42 +936,92 @@ int cmd_mbfan_rpm_map(const char *cmd, const char *args, int query, char *prev_c
int cmd_mbfan_source(const char *cmd, const char *args, int query, char *prev_cmd)
{
int fan;
int type, val, d_o, d_n;
int type, val, d_o, d_n, ocount;
char *tok, *saveptr, *param;
uint8_t new_sources[FAN_MAX_COUNT];
enum tacho_source_types s_type;
int ret = 0;

memset(new_sources, 0, sizeof(new_sources));

fan = atoi(&prev_cmd[5]) - 1;
if (fan < 0 || fan >= MBFAN_COUNT)
return 1;

s_type = conf->mbfans[fan].s_type;

if (query) {
val = conf->mbfans[fan].s_id;
if (conf->mbfans[fan].s_type != TACHO_FIXED)
val++;
printf("%s,%u\n",
tacho_source2str(conf->mbfans[fan].s_type),
val);
printf("%s,", tacho_source2str(s_type));
switch (s_type) {

case TACHO_FIXED:
case TACHO_FAN:
val = conf->mbfans[fan].s_id;
if (s_type != TACHO_FIXED)
val++;
printf("%u", val);
break;

case TACHO_MIN:
case TACHO_MAX:
case TACHO_AVG:
ocount = 0;
for (int i = 0; i < FAN_COUNT; i++) {
if (conf->mbfans[fan].sources[i]) {
if (ocount > 0)
printf(",");
printf("%u", i + 1);
ocount++;
}
}
break;
}
printf("\n");
} else {
param = strdup(args);
if ((tok = strtok_r(param, ",", &saveptr)) != NULL) {
type = str2tacho_source(tok);
d_n = (type != TACHO_FIXED ? 1 : 0);
if ((tok = strtok_r(NULL, ",", &saveptr)) != NULL) {
while ((tok = strtok_r(NULL, ",", &saveptr)) != NULL) {
val = atoi(tok) - d_n;
if (valid_tacho_source_ref(type, val)) {
d_o = (conf->mbfans[fan].s_type != TACHO_FIXED ? 1 : 0);
log_msg(LOG_NOTICE, "mbfan%d: change source %s,%u --> %s,%u",
fan + 1,
tacho_source2str(conf->mbfans[fan].s_type),
conf->mbfans[fan].s_id + d_o,
tacho_source2str(type),
val + d_n);
conf->mbfans[fan].s_type = type;
conf->mbfans[fan].s_id = val;
if (type == TACHO_FIXED || type == TACHO_FAN) {
d_o = (conf->mbfans[fan].s_type != TACHO_FIXED ? 1 : 0);
log_msg(LOG_NOTICE, "mbfan%d: change source %s,%u --> %s,%u",
fan + 1,
tacho_source2str(conf->mbfans[fan].s_type),
conf->mbfans[fan].s_id + d_o,
tacho_source2str(type),
val + d_n);
conf->mbfans[fan].s_type = type;
conf->mbfans[fan].s_id = val;
break;
}
new_sources[val] = 1;
} else {
log_msg(LOG_WARNING, "mbfan%d: invalid source: %s",
fan + 1, args);
ret = 2;
break;
}
}

if (type == TACHO_MIN || type == TACHO_MAX || type == TACHO_AVG) {
int scount = 0;
for (int i = 0; i < FAN_COUNT; i++) {
if (new_sources[i])
scount++;
}
if (scount >= 2) {
log_msg(LOG_NOTICE, "mbfan%d: new source %s", fan + 1, args);
conf->mbfans[fan].s_type = type;
conf->mbfans[fan].s_id = 0;
memcpy(conf->mbfans[fan].sources, new_sources,
sizeof(conf->mbfans[fan].sources));
} else {
log_msg(LOG_WARNING, "mbfan%d: too few parameters: %s",
fan + 1, args);
ret = 2;
}
}
}
Expand Down
52 changes: 51 additions & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ int str2tacho_source(const char *s)
ret = TACHO_FIXED;
else if (!strncasecmp(s, "fan", 3))
ret = TACHO_FAN;
else if (!strncasecmp(s, "min", 3))
ret = TACHO_MIN;
else if (!strncasecmp(s, "max", 3))
ret = TACHO_MAX;
else if (!strncasecmp(s, "avg", 3))
ret = TACHO_AVG;
}

return ret;
Expand All @@ -152,6 +158,12 @@ const char* tacho_source2str(enum tacho_source_types source)
{
if (source == TACHO_FAN)
return "fan";
else if (source == TACHO_MIN)
return "min";
else if (source == TACHO_MAX)
return "max";
else if (source == TACHO_AVG)
return "avg";

return "fixed";
}
Expand All @@ -167,6 +179,9 @@ int valid_tacho_source_ref(enum tacho_source_types source, uint16_t s_id)
break;
;;
case TACHO_FAN:
case TACHO_MIN:
case TACHO_MAX:
case TACHO_AVG:
ret = (s_id >= 0 && s_id < FAN_MAX_COUNT ? 1 : 0);
break;
}
Expand Down Expand Up @@ -253,7 +268,6 @@ void json2tacho_map(cJSON *item, struct tacho_map *map)
map->points = c;
}


cJSON* tacho_map2json(const struct tacho_map *map)
{
int i;
Expand All @@ -272,6 +286,36 @@ cJSON* tacho_map2json(const struct tacho_map *map)
return o;
}

void json2tacho_sources(cJSON *item, uint8_t *sources)
{
int i;
cJSON *o;

for (i = 0; i < FAN_MAX_COUNT; i++)
sources[i] = 0;

cJSON_ArrayForEach(o, item) {
i = cJSON_GetNumberValue(o) - 1;
if (i >= 0 && i < FAN_MAX_COUNT)
sources[i] = 1;
}
}

cJSON* tacho_sources2json(const uint8_t *sources)
{
int i;
cJSON *o;

if ((o = cJSON_CreateArray()) == NULL)
return NULL;

for (i = 0; i < FAN_COUNT; i++) {
if (sources[i])
cJSON_AddItemToArray(o, cJSON_CreateNumber(i + 1));
}

return o;
}

void json2temp_map(cJSON *item, struct temp_map *map)
{
Expand Down Expand Up @@ -418,6 +462,8 @@ void clear_config(struct fanpico_config *cfg)
m->map.points = 0;
m->filter = FILTER_NONE;
m->filter_ctx = NULL;
for (j = 0; j < FAN_MAX_COUNT; j++)
m->sources[j] = 0;
}

cfg->local_echo = false;
Expand Down Expand Up @@ -545,6 +591,8 @@ cJSON *config_to_json(const struct fanpico_config *cfg)
cJSON_AddItemToObject(o, "rpm_factor", cJSON_CreateNumber(m->rpm_factor));
cJSON_AddItemToObject(o, "source_type", cJSON_CreateString(tacho_source2str(m->s_type)));
cJSON_AddItemToObject(o, "source_id", cJSON_CreateNumber(m->s_id));
if (m->s_type == TACHO_MIN || m->s_type == TACHO_MAX || m->s_type == TACHO_AVG)
cJSON_AddItemToObject(o, "sources", tacho_sources2json(m->sources));
cJSON_AddItemToObject(o, "rpm_map", tacho_map2json(&m->map));
cJSON_AddItemToObject(o, "filter", filter2json(m->filter, m->filter_ctx));
cJSON_AddItemToArray(mbfans, o);
Expand Down Expand Up @@ -753,6 +801,8 @@ int json_to_config(cJSON *config, struct fanpico_config *cfg)
m->s_type = str2tacho_source(cJSON_GetStringValue(
cJSON_GetObjectItem(item, "source_type")));
m->s_id = cJSON_GetNumberValue(cJSON_GetObjectItem(item, "source_id"));
if ((r = cJSON_GetObjectItem(item, "sources")))
json2tacho_sources(r, m->sources);
if ((r = cJSON_GetObjectItem(item, "rpm_map")))
json2tacho_map(r, &m->map);
if ((r = cJSON_GetObjectItem(item, "filter")))
Expand Down
4 changes: 4 additions & 0 deletions src/fanpico.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ enum signal_filter_types {
enum tacho_source_types {
TACHO_FIXED = 0, /* Fixed speed set by s_id */
TACHO_FAN = 1, /* Fan tacho signal */
TACHO_MIN = 2, /* Slowest tacho signal from a group of fans. */
TACHO_MAX = 3, /* Fastest tacho signal from a group of fans. */
TACHO_AVG = 4, /* Average tacho signal from a group of fans. */
};
#define TACHO_ENUM_MAX 1

Expand Down Expand Up @@ -141,6 +144,7 @@ struct mb_input {
uint8_t rpm_factor;
enum tacho_source_types s_type;
uint16_t s_id;
uint8_t sources[FAN_MAX_COUNT];
struct tacho_map map;

/* input PWM signal settings */
Expand Down
36 changes: 35 additions & 1 deletion src/tacho.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* tacho.c
Copyright (C) 2021-2022 Timo Kokkonen <[email protected]>
Copyright (C) 2021-2023 Timo Kokkonen <[email protected]>

SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -362,7 +362,9 @@ double tacho_map(const struct tacho_map *map, double val)
double calculate_tacho_freq(struct fanpico_state *state, const struct fanpico_config *config, int i)
{
const struct mb_input *mbfan;
int count = 0;
double val = 0;
double sum = 0;

mbfan = &config->mbfans[i];

Expand All @@ -373,6 +375,38 @@ double calculate_tacho_freq(struct fanpico_state *state, const struct fanpico_co
case TACHO_FAN:
val = state->fan_freq[mbfan->s_id] * 60.0 / config->fans[mbfan->s_id].rpm_factor;
break;
case TACHO_MIN:
case TACHO_MAX:
case TACHO_AVG:
for (int i = 0; i < FAN_COUNT; i++) {
if (mbfan->sources[i]) {
val = state->fan_freq[i] * 60.0 / config->fans[i].rpm_factor;
if (count == 0) {
sum = val;
} else {
if (mbfan->s_type == TACHO_MIN) {
if (val < sum)
sum = val;
}
else if (mbfan->s_type == TACHO_MAX) {
if (val > sum)
sum = val;
}
else { /* average */
sum += val;
}
}
count++;
}
}
if (count >= 1) {
if (mbfan->s_type == TACHO_AVG) {
val = sum / count;
} else {
val = sum;
}
}
break;
}


Expand Down