-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
329 lines (274 loc) · 7.95 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Water Tower Monitor
*
* Copyright (c) 2015, longfeng.xiao <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*----------------------------------------------------------------------------
* CMSIS-RTOS 'main' function template
*---------------------------------------------------------------------------*/
#define osObjectsPublic // define objects in main module
#include "osObjects.h" // RTOS object definitions
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "misc.h"
#include "common.h"
#define SYS_CBSIZE 128
#define SYS_MAX_ARGS 4
static const char *sysPrompt = "STM32# ";
static char consoleBuffer[SYS_CBSIZE];
static void rccInit(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
}
static void nvicInit(void)
{
NVIC_InitTypeDef nvicInitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
nvicInitStruct.NVIC_IRQChannel = SPI1_IRQn;
nvicInitStruct.NVIC_IRQChannelPreemptionPriority = 2;
nvicInitStruct.NVIC_IRQChannelSubPriority = 0;
nvicInitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicInitStruct);
nvicInitStruct.NVIC_IRQChannel = USART1_IRQn;
nvicInitStruct.NVIC_IRQChannelPreemptionPriority = 2;
nvicInitStruct.NVIC_IRQChannelSubPriority = 1;
nvicInitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicInitStruct);
nvicInitStruct.NVIC_IRQChannel = USART2_IRQn;
nvicInitStruct.NVIC_IRQChannelPreemptionPriority = 2;
nvicInitStruct.NVIC_IRQChannelSubPriority = 2;
nvicInitStruct.NVIC_IRQChannelCmd = ENABLE;
nvicInitStruct.NVIC_IRQChannel = TIM5_IRQn;
nvicInitStruct.NVIC_IRQChannelPreemptionPriority = 1;
nvicInitStruct.NVIC_IRQChannelSubPriority = 0;
nvicInitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicInitStruct);
}
struct CmdTableS
{
const char *name;
int maxargs;
int (*cmd)(struct CmdTableS *, int, char *[]);
const char *usage;
};
typedef struct CmdTableS CmdTableT;
int do_help(CmdTableT *cmdtp, int argc, char *argv[]);
int do_ur(CmdTableT *cmdtp, int argc, char *argv[]);
int do_ra(CmdTableT *cmdtp, int argc, char *argv[]);
static const CmdTableT cmdTable[] = {
{"?", SYS_MAX_ARGS, do_help, "alias for 'help'"},
{"help", SYS_MAX_ARGS, do_help, "print online help"},
{"ur", SYS_MAX_ARGS, do_ur, "ultrasonic ranging"},
{"ra", SYS_MAX_ARGS, do_ra, "radio controller"},
};
#define CMD_ITEMS (sizeof (cmdTable) / sizeof (cmdTable[0]))
static CmdTableT *findCmd(char *cmd)
{
const char *p;
int len;
int i;
len = ((p = strchr(cmd, '.')) ==NULL) ? strlen(cmd) : (p - cmd);
for (i = 0; i < CMD_ITEMS; i++) {
if (strncmp(cmd, cmdTable[i].name, len) == 0) {
if (len == strlen(cmdTable[i].name))
return (CmdTableT *)&cmdTable[i];
}
}
return 0;
}
static void cmdUsage(CmdTableT *cmdtp)
{
printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
}
int do_help(CmdTableT *cmdtp, int argc, char *argv[])
{
int i;
if (argc == 1) {
for (i = 0; i < CMD_ITEMS; i++) {
printf("%*s- %s\n", 8, cmdTable[i].name, cmdTable[i].usage);
}
} else {
for (i = 1; i < argc; i++) {
if ((cmdtp = findCmd(argv[i])) != NULL) {
cmdUsage(cmdtp);
} else {
printf("Unknown comand '%s' - try help;"
" without arguments for list of all"
" known commands\n\n", argv[i]);
}
}
}
return 0;
}
int do_ur(CmdTableT *cmdtp, int argc, char *argv[])
{
uint32_t sample0 = getUltrasonicRangingSample(0);
uint32_t sample1 = getUltrasonicRangingSample(1);
uint32_t distance0 = sample0 * 340 / 2 / 10000;
uint32_t distance1 = sample1 * 340 / 2 / 10000;
printf("Ultrasonic ranging sample[0]: %d cm <%d usec>, sample[1]: %d cm <%d usec>\n",
distance0, sample0, distance1, sample1);
return 0;
}
int do_ra(CmdTableT *cmdtp, int argc, char *argv[])
{
int len;
switch (argc) {
case 2:
len = strlen(argv[1]);
if (strncmp(argv[1], "reset", len) == 0) {
printf("Reset radio\n");
radioReset();
}
if (strncmp(argv[1], "dump", len) == 0) {
radioDumpReg();
}
break;
case 3:
len = strlen(argv[1]);
if (strncmp(argv[1], "read", len) == 0) {
uint8_t reg = strtoul(argv[2], NULL, 0);
printf("Read radio reg %x: %x\n", reg, radioReadReg(reg));
}
break;
case 4:
len = strlen(argv[1]);
if (strncmp(argv[1], "read", len) == 0) {
uint8_t reg = strtoul(argv[2], NULL, 0);
uint8_t val = strtoul(argv[3], NULL, 0);
radioWriteReg(reg, val);
printf("Write radio reg %x: %x [%x]\n", reg, val, radioReadReg(reg));
}
break;
default:
cmdUsage(cmdtp);
break;
}
return 0;
}
static int readline(void)
{
int len, ch;
char *p = consoleBuffer;
printf(sysPrompt);
len = 0;
while (1) {
ch = getchar();
if (ch < 0) {
osDelay(100);
continue;
}
switch (ch) {
case '\r':
case '\n':
*p = '\0';
putchar('\n');
return len;
case '\0':
continue;
case 0x03: /* ^C - break */
consoleBuffer[0] = '\0';
return -1;
default:
if (len < SYS_CBSIZE - 2) {
len++;
*p++ = ch;
putchar(ch);
} else {
putchar('\a');
}
}
}
}
static int parseLine(char *line, char *argv[])
{
int nargs = 0;
while (nargs < SYS_MAX_ARGS) {
while ((*line == ' ') || (*line == '\t')) {
line++;
}
if (*line == '\0') {
argv[nargs] = NULL;
return nargs;
}
argv[nargs++] = line;
while (*line && (*line != ' ') && (*line != '\t')) {
++line;
}
if (*line == '\0') {
argv[nargs] = NULL;
return nargs;
}
*line++ = '\0';
}
printf(" ** Too many args (max. %d) **\n", SYS_MAX_ARGS);
return nargs;
}
static int runCommand(void)
{
char *cmdBuffer = consoleBuffer;
int len = strlen(cmdBuffer);
char *argv[SYS_MAX_ARGS];
int argc;
CmdTableT *cmdtp;
if (len > SYS_CBSIZE) {
puts("## Command too long!");
return -1;
}
if ((argc = parseLine(cmdBuffer, argv)) == 0) {
puts("## No command at all");
return -1;
}
if ((cmdtp = findCmd(argv[0])) == NULL) {
printf("Unknown command '%s' - try 'help'\n", argv[0]);
return -1;
}
if (argc > cmdtp->maxargs) {
cmdUsage(cmdtp);
return -1;
}
return (cmdtp->cmd)(cmdtp, argc, argv);
}
/*
* main: initialize and start the system
*/
int main (void)
{
rccInit();
nvicInit();
stdioInit();
printf("\nWater Tower Monitor\n");
blinkyInit();
multiPointComInit();
ultrasonicRangingInit();
while (1) {
int len = readline();
if (len > 0) {
runCommand();
} else if (len < 0) {
puts("<INTERRUPT>");
}
}
}