./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
19 / 29
65.5%
Branches:
4 / 6
66.7%
Functions:
4 / 7
57.1%

k-fsw/app/src/parameters/board_table.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdint.h>
3 #include <string.h>
4
5 #include <zephyr/sys/util.h>
6
7 #include <kfsw/platform/hardware.h>
8 #include <kfsw/platform/reset.h>
9 #include <kfsw/services/boot.h>
10 #include <kfsw/services/parameter.h>
11
12 #include "tables.h"
13
14 #if CONFIG_KFSW_CSP
15 #include <kfsw/comms/csp.h>
16 #if CONFIG_KFSW_CSP_CAN
17 #include <kfsw/comms/can.h>
18 #endif
19 #define KFSW_BOARD_NODE_ID_DEFAULT CONFIG_KFSW_CSP_ADDRESS
20 #else
21 #define KFSW_BOARD_NODE_ID_DEFAULT 0U
22 #endif
23
24 /* Identity is read from the running CSP configuration. The switches are
25 * read-only because the lower layers can't apply a new value.
26 */
27 /* Sized so the identities in use fit without truncation. */
28 #define KFSW_BOARD_UID_SIZE 32U
29 #define KFSW_BOARD_MODEL_SIZE 32U
30 #define KFSW_BOARD_REVISION_SIZE 40U
31
32 static char board_uid[KFSW_BOARD_UID_SIZE];
33 static char board_hardware_id[KFSW_HARDWARE_ID_TEXT_SIZE];
34 static char board_model[KFSW_BOARD_MODEL_SIZE];
35 static char board_revision[KFSW_BOARD_REVISION_SIZE];
36 static uint16_t board_node_id = KFSW_BOARD_NODE_ID_DEFAULT;
37 static uint32_t board_reset_cause;
38 static uint8_t board_csp_enabled = IS_ENABLED(CONFIG_KFSW_CSP);
39 static uint8_t board_kiss_enabled = IS_ENABLED(CONFIG_KFSW_CSP_KISS_UART);
40 static uint8_t board_shell_enabled = IS_ENABLED(CONFIG_SHELL);
41 static uint8_t board_storage_enabled = IS_ENABLED(CONFIG_KFSW_STORAGE);
42 static uint8_t board_can_enabled = IS_ENABLED(CONFIG_KFSW_CSP_CAN);
43 #if CONFIG_KFSW_CSP_CAN
44 static uint32_t board_can_speed = CONFIG_KFSW_CSP_CAN_BITRATE;
45 #endif
46
47 /* Copied from the running CSP identity. */
48 4 static void sample_identity(char *destination, size_t size, const char *source)
49 {
50 4 size_t length = 0U;
51
52
4/6
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 4 times.
52 while ((length + 1U < size) && (source != NULL) && (source[length] != '\0')) {
53 48 destination[length] = source[length];
54 48 length++;
55 }
56 4 destination[length] = '\0';
57 4 }
58
59 2 static void sample_uid(void *value)
60 {
61 #if CONFIG_KFSW_CSP
62 struct kfsw_csp_info info;
63
64 2 kfsw_csp_get_info(&info);
65 2 sample_identity(value, KFSW_BOARD_UID_SIZE, info.hostname);
66 #else
67 ARG_UNUSED(value);
68 #endif
69 2 }
70
71 /* The chip ID, from the boot service like the reset cause. */
72 ✗ static void sample_hardware_id(void *value)
73 {
74 ✗ sample_identity(value, KFSW_HARDWARE_ID_TEXT_SIZE, kfsw_boot_get_hardware_id());
75 ✗ }
76
77 ✗ static void sample_model(void *value)
78 {
79 #if CONFIG_KFSW_CSP
80 struct kfsw_csp_info info;
81
82 ✗ kfsw_csp_get_info(&info);
83 ✗ sample_identity(value, KFSW_BOARD_MODEL_SIZE, info.model);
84 #else
85 ARG_UNUSED(value);
86 #endif
87 ✗ }
88
89 2 static void sample_revision(void *value)
90 {
91 #if CONFIG_KFSW_CSP
92 struct kfsw_csp_info info;
93
94 2 kfsw_csp_get_info(&info);
95 2 sample_identity(value, KFSW_BOARD_REVISION_SIZE, info.revision);
96 #else
97 ARG_UNUSED(value);
98 #endif
99 2 }
100
101 2 static void sample_node_id(void *value)
102 {
103 #if CONFIG_KFSW_CSP
104 struct kfsw_csp_info info;
105
106 /* From the running configuration, not the build option. */
107 2 kfsw_csp_get_info(&info);
108 2 *(uint16_t *)value = info.address;
109 #else
110 ARG_UNUSED(value);
111 #endif
112 2 }
113
114 ✗ static void sample_reset_cause(void *value)
115 {
116 /* From the boot service: reading the platform again returns an empty register. */
117 ✗ *(uint32_t *)value = kfsw_boot_get_reset_cause();
118 ✗ }
119
120 #if CONFIG_KFSW_CSP_CAN
121 static void sample_can_speed(void *value)
122 {
123 struct kfsw_can_info info;
124
125 kfsw_can_get_info(&info);
126 *(uint32_t *)value = info.bitrate;
127 }
128
129 /* Checked in the validator, because the change callback runs after the value
130 * is stored.
131 */
132 static int validate_can_speed(const union kfsw_param_scalar *value)
133 {
134 return kfsw_can_bitrate_supported(value->u32) ? 0 : -EINVAL;
135 }
136
137 /* Every node on the bus needs the same bitrate, so changing it over CAN
138 * cuts the link until the other nodes follow.
139 */
140 static void changed_can_speed(const union kfsw_param_scalar *value)
141 {
142 (void)kfsw_can_set_bitrate(value->u32);
143 }
144 #endif
145
146 static const struct kfsw_param_definition board_param_definitions[] = {
147 {
148 .offset = 0x00U,
149 .type = KFSW_PARAM_U16,
150 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO,
151 .name = "node_id",
152 .description = "CSP address this node is running as",
153 .value = &board_node_id,
154 .default_value = {.u16 = KFSW_BOARD_NODE_ID_DEFAULT},
155 .sample = sample_node_id,
156 },
157 {
158 .offset = 0x10U,
159 .type = KFSW_PARAM_STRING,
160 .capacity = KFSW_BOARD_UID_SIZE,
161 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO,
162 .name = "uid",
163 .description = "Unit identity, as reported by csp ident",
164 .value = board_uid,
165 .sample = sample_uid,
166 },
167 {
168 .offset = 0x40U,
169 .type = KFSW_PARAM_STRING,
170 .capacity = KFSW_HARDWARE_ID_TEXT_SIZE,
171 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO,
172 .name = "hw_id",
173 .description = "Identifier this unit's silicon was manufactured with",
174 .value = board_hardware_id,
175 .sample = sample_hardware_id,
176 },
177 {
178 .offset = 0x20U,
179 .type = KFSW_PARAM_STRING,
180 .capacity = KFSW_BOARD_MODEL_SIZE,
181 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO,
182 .name = "model",
183 .description = "Composition this build identifies as",
184 .value = board_model,
185 .sample = sample_model,
186 },
187 {
188 .offset = 0x30U,
189 .type = KFSW_PARAM_STRING,
190 .capacity = KFSW_BOARD_REVISION_SIZE,
191 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO,
192 .name = "revision",
193 .description = "Build revision, as reported by csp ident",
194 .value = board_revision,
195 .sample = sample_revision,
196 },
197 {
198 .offset = 0x02U,
199 .type = KFSW_PARAM_U8,
200 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO,
201 .name = "csp_enabled",
202 .description = "Whether CSP is composed into this build",
203 .value = &board_csp_enabled,
204 .default_value = {.u8 = IS_ENABLED(CONFIG_KFSW_CSP)},
205 },
206 {
207 .offset = 0x05U,
208 .type = KFSW_PARAM_U8,
209 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO,
210 .name = "can_enabled",
211 .description = "Whether the CAN interface is composed",
212 .value = &board_can_enabled,
213 .default_value = {.u8 = IS_ENABLED(CONFIG_KFSW_CSP_CAN)},
214 },
215 #if CONFIG_KFSW_CSP_CAN
216 {
217 .offset = 0x0cU,
218 .type = KFSW_PARAM_U32,
219 .name = "can_speed",
220 .description = "CAN bitrate: 125000, 250000, 500000, 800000 or 1000000",
221 .value = &board_can_speed,
222 .default_value = {.u32 = CONFIG_KFSW_CSP_CAN_BITRATE},
223 .sample = sample_can_speed,
224 .validate = validate_can_speed,
225 .changed = changed_can_speed,
226 },
227 #endif
228 {
229 .offset = 0x03U,
230 .type = KFSW_PARAM_U8,
231 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO,
232 .name = "kiss_enabled",
233 .description = "Whether the KISS interface is composed",
234 .value = &board_kiss_enabled,
235 .default_value = {.u8 = IS_ENABLED(CONFIG_KFSW_CSP_KISS_UART)},
236 },
237 {
238 .offset = 0x04U,
239 .type = KFSW_PARAM_X32,
240 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO,
241 .name = "reset_cause",
242 .description = "Reset cause latched at boot",
243 .value = &board_reset_cause,
244 .default_value = {.u32 = 0U},
245 .sample = sample_reset_cause,
246 },
247 {
248 .offset = 0x08U,
249 .type = KFSW_PARAM_U8,
250 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO,
251 .name = "shell_enabled",
252 .description = "Whether the debug shell is composed",
253 .value = &board_shell_enabled,
254 .default_value = {.u8 = IS_ENABLED(CONFIG_SHELL)},
255 },
256 {
257 .offset = 0x09U,
258 .type = KFSW_PARAM_U8,
259 .flags = KFSW_PARAM_FLAG_READ_ONLY | KFSW_PARAM_FLAG_SYSTEM_INFO,
260 .name = "storage_enabled",
261 .description = "Whether persistent storage is composed",
262 .value = &board_storage_enabled,
263 .default_value = {.u8 = IS_ENABLED(CONFIG_KFSW_STORAGE)},
264 },
265 };
266
267 KFSW_PARAM_TABLE_FITS_THE_LINK(board_param_definitions);
268
269 const struct kfsw_param_definition_set kfsw_board_param_definitions = {
270 .table = KFSW_PARAM_TABLE_BOARD,
271 .name = KFSW_PARAM_TABLE_BOARD_NAME,
272 .description = "Node identity and what the board carries",
273 .definitions = board_param_definitions,
274 .count = ARRAY_SIZE(board_param_definitions),
275 };
276