aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/EFL/shcfg.c
diff options
context:
space:
mode:
authorAkshay <[email protected]>2022-04-10 12:13:40 +0100
committerAkshay <[email protected]>2022-04-10 12:13:40 +0100
commitdc90387ce7d8ba7b607d9c48540bf6d8b560f14d (patch)
tree4ccb8fa5886b66fa9d480edef74236c27f035e16 /lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/EFL/shcfg.c
Diffstat (limited to 'lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/EFL/shcfg.c')
-rw-r--r--lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/EFL/shcfg.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/EFL/shcfg.c b/lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/EFL/shcfg.c
new file mode 100644
index 000000000..865b9e60b
--- /dev/null
+++ b/lib/chibios-contrib/testhal/NUMICRO/NUC123/NUTINY-SDK-NUC123-V2.0/EFL/shcfg.c
@@ -0,0 +1,112 @@
1/*
2 Copyright (C) 2021 Alex Lewontin
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * @file shcfg.c
19 * @brief Shell config.
20 *
21 * @addtogroup Shell
22 * @{
23 */
24#include "hal.h"
25#include "shcfg.h"
26#include "chprintf.h"
27
28#include <string.h>
29#include <stdlib.h>
30
31MFSDriver mfsd;
32
33const MFSConfig mfsd_config = {.flashp = (BaseFlash *)&EFLD1,
34 .erased = 0xFFFFFFFF,
35 .bank0_sectors = 4,
36 .bank0_start = 0,
37 .bank1_sectors = 4,
38 .bank1_start = 4,
39 .bank_size = 2048};
40
41void sh_kvs_put(BaseSequentialStream *chp, int argc, char *argv[]) {
42 if (argc < 2) {
43 chprintf(chp, "Format: kvs_put key [value]\nAt this time, key must be numeric.\n");
44 return;
45 }
46 mfs_id_t rid = atoi(argv[0]);
47 if (rid < 1 || MFS_CFG_MAX_RECORDS < rid) {
48 chprintf(chp, "key must be [%d, %d].\n", 1, MFS_CFG_MAX_RECORDS);
49 return;
50 }
51 mfsWriteRecord(&mfsd, rid, strlen(argv[1]), (uint8_t *)argv[1]);
52}
53
54void sh_kvs_get(BaseSequentialStream *chp, int argc, char *argv[]) {
55 if (argc < 1) {
56 chprintf(chp,
57 "Format: kvs_get key\nAt this time, key must be "
58 "numeric.\n");
59 return;
60 }
61 mfs_id_t rid = atoi(argv[0]);
62 if (rid < 1 || MFS_CFG_MAX_RECORDS < rid) {
63 chprintf(chp, "key must be [%d, %d].\n", 1, MFS_CFG_MAX_RECORDS);
64 return;
65 }
66
67 uint8_t buf[128];
68 size_t n = 128;
69 mfs_error_t err = mfsReadRecord(&mfsd, rid, &n, buf);
70 switch (err) {
71 case MFS_WARN_GC:
72 case MFS_WARN_REPAIR:
73 case MFS_NO_ERROR:
74 chprintf(chp, "%.*s\n", n, buf);
75 break;
76 case MFS_ERR_NOT_FOUND:
77 chprintf(chp, "Record not found\n");
78 break;
79 default:
80 chprintf(chp, "Unknown error reading record: %d\n", err);
81 }
82}
83
84const char all_flag[] = "--all";
85void sh_kvs_erase(BaseSequentialStream *chp, int argc, char *argv[]) {
86
87 if (argc < 1) {
88 chprintf(chp,
89 "Format: kvs_erase [%s] key\nAt this time, key must be"
90 "numeric.\n", all_flag);
91 return;
92 }
93
94 if (strcmp(all_flag, argv[0]) == 0) {
95 mfsErase(&mfsd);
96 } else {
97 mfs_id_t rid = atoi(argv[0]);
98 if (rid < 1 || MFS_CFG_MAX_RECORDS < rid) {
99 chprintf(chp, "key must be [%d, %d].\n", 1, MFS_CFG_MAX_RECORDS);
100 return;
101 }
102 mfsEraseRecord(&mfsd, rid);
103 }
104
105}
106
107const ShellCommand commands[] = {{"kvs_put", sh_kvs_put},
108 {"kvs_get", sh_kvs_get},
109 {"kvs_erase", sh_kvs_erase},
110 {NULL, NULL}};
111
112const ShellConfig shell_cfg = {(BaseSequentialStream *)&SHELL_SERIAL_DRIVER, commands};