kfsw-modules/temperature-sensor-example/parameters/temperature_sensor_example_table.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | |||
| 4 | #include <zephyr/sys/util.h> | ||
| 5 | |||
| 6 | #include <kfsw/modules/temperature_sensor_example.h> | ||
| 7 | #include <kfsw/services/parameter.h> | ||
| 8 | |||
| 9 | /* | ||
| 10 | * Die temperature in milli-degrees, with the valid flag and the failure count, | ||
| 11 | * and one operator setting that survives a reset. | ||
| 12 | * | ||
| 13 | * TODO(template): one definition per value the ground needs. Telemetry is | ||
| 14 | * read-only and refreshed by a sample callback from the module's snapshot; | ||
| 15 | * settings validate, then call the module's setter from a changed callback. | ||
| 16 | */ | ||
| 17 | |||
| 18 | static int32_t temp_milli_c = KFSW_TEMP_EXAMPLE_INVALID_MILLI_C; | ||
| 19 | static uint32_t temp_samples; | ||
| 20 | static uint32_t temp_failures; | ||
| 21 | static uint32_t temp_last_uptime_ms; | ||
| 22 | static uint8_t temp_valid; | ||
| 23 | static uint32_t temp_alarms; | ||
| 24 | static int32_t temp_alarm_mc = CONFIG_KFSW_TEMP_EXAMPLE_ALARM_MILLI_C; | ||
| 25 | |||
| 26 | 38 | static void sample_reading(void) | |
| 27 | { | ||
| 28 | struct kfsw_temp_example_reading reading; | ||
| 29 | |||
| 30 | 38 | if (kfsw_temp_example_get(&reading) != 0) { | |
| 31 | ✗ | return; | |
| 32 | } | ||
| 33 | 38 | temp_milli_c = reading.milli_c; | |
| 34 | 38 | temp_samples = reading.samples; | |
| 35 | 38 | temp_failures = reading.failures; | |
| 36 | 38 | temp_last_uptime_ms = reading.last_uptime_ms; | |
| 37 | 38 | temp_valid = reading.valid ? 1U : 0U; | |
| 38 | 38 | temp_alarms = reading.alarms; | |
| 39 | } | ||
| 40 | |||
| 41 | #define TEMP_SAMPLE(field, type) \ | ||
| 42 | static void sample_##field(void *value) \ | ||
| 43 | { \ | ||
| 44 | sample_reading(); \ | ||
| 45 | *(type *)value = temp_##field; \ | ||
| 46 | } | ||
| 47 | |||
| 48 | 8 | TEMP_SAMPLE(milli_c, int32_t) | |
| 49 | 6 | TEMP_SAMPLE(samples, uint32_t) | |
| 50 | 10 | TEMP_SAMPLE(failures, uint32_t) | |
| 51 | 2 | TEMP_SAMPLE(last_uptime_ms, uint32_t) | |
| 52 | 6 | TEMP_SAMPLE(valid, uint8_t) | |
| 53 | 6 | TEMP_SAMPLE(alarms, uint32_t) | |
| 54 | |||
| 55 | 8 | static int validate_alarm(const union kfsw_param_scalar *value) | |
| 56 | { | ||
| 57 | 8 | if ((value->i32 < KFSW_TEMP_EXAMPLE_ALARM_MIN_MILLI_C) || | |
| 58 | 8 | (value->i32 > KFSW_TEMP_EXAMPLE_ALARM_MAX_MILLI_C)) { | |
| 59 | 2 | return -ERANGE; | |
| 60 | } | ||
| 61 | 6 | return 0; | |
| 62 | } | ||
| 63 | |||
| 64 | /* A changed callback, not apply: the snapshot restore refuses a persistent | ||
| 65 | * parameter with an apply callback, and runs changed for each restored value. | ||
| 66 | */ | ||
| 67 | 6 | static void change_alarm(const union kfsw_param_scalar *value) | |
| 68 | { | ||
| 69 | 6 | (void)kfsw_temp_example_set_alarm(value->i32); | |
| 70 | 6 | } | |
| 71 | |||
| 72 | static const struct kfsw_param_definition temp_example_param_definitions[] = { | ||
| 73 | { | ||
| 74 | .offset = 0x00U, | ||
| 75 | .type = KFSW_PARAM_I32, | ||
| 76 | .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_LIVE, | ||
| 77 | .name = "temp_mcu_mc", | ||
| 78 | .unit = "mC", | ||
| 79 | .description = "Die temperature in thousandths of a degree Celsius", | ||
| 80 | .value = &temp_milli_c, | ||
| 81 | .sample = sample_milli_c, | ||
| 82 | }, | ||
| 83 | { | ||
| 84 | .offset = 0x04U, | ||
| 85 | .type = KFSW_PARAM_U32, | ||
| 86 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 87 | .name = "temp_samples", | ||
| 88 | .description = "Successful sensor reads since boot", | ||
| 89 | .value = &temp_samples, | ||
| 90 | .sample = sample_samples, | ||
| 91 | }, | ||
| 92 | { | ||
| 93 | .offset = 0x08U, | ||
| 94 | .type = KFSW_PARAM_U32, | ||
| 95 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 96 | .name = "temp_failures", | ||
| 97 | .description = "Sensor reads that returned an error", | ||
| 98 | .value = &temp_failures, | ||
| 99 | .sample = sample_failures, | ||
| 100 | }, | ||
| 101 | { | ||
| 102 | .offset = 0x0cU, | ||
| 103 | .type = KFSW_PARAM_U32, | ||
| 104 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 105 | .name = "temp_last_ms", | ||
| 106 | .unit = "ms", | ||
| 107 | .description = "Uptime of the latest successful read", | ||
| 108 | .value = &temp_last_uptime_ms, | ||
| 109 | .sample = sample_last_uptime_ms, | ||
| 110 | }, | ||
| 111 | { | ||
| 112 | .offset = 0x10U, | ||
| 113 | .type = KFSW_PARAM_U8, | ||
| 114 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 115 | .name = "temp_valid", | ||
| 116 | .description = "Whether temp_mcu_mc is a reading or the reserved value", | ||
| 117 | .value = &temp_valid, | ||
| 118 | .sample = sample_valid, | ||
| 119 | }, | ||
| 120 | { | ||
| 121 | .offset = 0x14U, | ||
| 122 | .type = KFSW_PARAM_U32, | ||
| 123 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 124 | .name = "temp_alarms", | ||
| 125 | .description = "Readings that crossed the alarm threshold upwards", | ||
| 126 | .value = &temp_alarms, | ||
| 127 | .sample = sample_alarms, | ||
| 128 | }, | ||
| 129 | { | ||
| 130 | .offset = 0x18U, | ||
| 131 | .type = KFSW_PARAM_I32, | ||
| 132 | .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_PERSISTENT, | ||
| 133 | .name = "temp_alarm_mc", | ||
| 134 | .unit = "mC", | ||
| 135 | .description = "Alarm threshold, -40000 to 125000", | ||
| 136 | .value = &temp_alarm_mc, | ||
| 137 | .default_value = {.i32 = CONFIG_KFSW_TEMP_EXAMPLE_ALARM_MILLI_C}, | ||
| 138 | .validate = validate_alarm, | ||
| 139 | .changed = change_alarm, | ||
| 140 | }, | ||
| 141 | }; | ||
| 142 | |||
| 143 | KFSW_PARAM_TABLE_FITS_THE_LINK(temp_example_param_definitions); | ||
| 144 | |||
| 145 | const struct kfsw_param_definition_set kfsw_temp_example_param_definitions = { | ||
| 146 | .table = KFSW_TEMP_EXAMPLE_PARAM_TABLE_ID, | ||
| 147 | .name = KFSW_TEMP_EXAMPLE_PARAM_TABLE_NAME, | ||
| 148 | .description = "Die temperature readings and alarm", | ||
| 149 | .definitions = temp_example_param_definitions, | ||
| 150 | .count = ARRAY_SIZE(temp_example_param_definitions), | ||
| 151 | }; | ||
| 152 |