Mercurial > ~darius > hgwebdir.cgi > stm32temp
comparison main.c @ 21:bd8e2cf04034
- Add flash erase, write & read commands (needs more work).
- Split the buffer into argv/argc to make sub commands simpler.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Thu, 15 Nov 2012 23:40:51 +1030 |
parents | d078651f5353 |
children | 198ac9d95770 |
comparison
equal
deleted
inserted
replaced
20:35cf31794a42 | 21:bd8e2cf04034 |
---|---|
16 #include "hw.h" | 16 #include "hw.h" |
17 #include "lcd.h" | 17 #include "lcd.h" |
18 #include "main.h" | 18 #include "main.h" |
19 #include "touch.h" | 19 #include "touch.h" |
20 | 20 |
21 #define MAXARGS 10 | |
22 #define LINEBUF 40 | |
21 typedef struct { | 23 typedef struct { |
22 char buf[40]; | 24 char *buf; |
23 volatile uint8_t state; | 25 volatile uint8_t state; |
24 uint8_t len; | 26 uint8_t len; |
25 } consbuf_t; | 27 } consbuf_t; |
26 | 28 |
27 void NVIC_Configuration(void); | 29 void NVIC_Configuration(void); |
91 /* Echo back to the user */ | 93 /* Echo back to the user */ |
92 comm_put(cmd.buf[cmd.state]); | 94 comm_put(cmd.buf[cmd.state]); |
93 | 95 |
94 cmd.state++; | 96 cmd.state++; |
95 /* Over flow? */ | 97 /* Over flow? */ |
96 if (cmd.state == ((sizeof(cmd.buf) / sizeof(cmd.buf[0])) - 1)) { | 98 if (cmd.state == LINEBUF - 1) { |
97 fputs("\r\nLine too long", stdout); | 99 fputs("\r\nLine too long", stdout); |
98 cmd.state = 0; | 100 cmd.state = 0; |
99 continue; | 101 continue; |
100 } | 102 } |
101 } | 103 } |
102 } | 104 } |
103 | 105 |
104 | |
105 int | 106 int |
106 main(void) { | 107 main(void) { |
107 char buf[40]; | 108 char buf[40], *argv[MAXARGS], **ap, *tmp; |
109 int argc; | |
108 struct tm nowtm; | 110 struct tm nowtm; |
109 time_t now; | 111 time_t now; |
110 uint16_t x, y, x1, y1, z1, z2, r, c, rx, ry; | 112 uint16_t x, y, x1, y1, z1, z2, r, c, rx, ry; |
111 float t, t2; | 113 float t, t2; |
112 char col; | |
113 | 114 |
114 cmd.state = cmd.len = 0; | 115 cmd.state = cmd.len = 0; |
116 cmd.buf = malloc(LINEBUF); | |
115 | 117 |
116 /* Init hardware - configure IO ports and external peripherals */ | 118 /* Init hardware - configure IO ports and external peripherals */ |
117 hw_init(); | 119 hw_init(); |
118 | 120 |
119 /* NVIC configuration */ | 121 /* NVIC configuration */ |
152 fputs("> ", stdout); | 154 fputs("> ", stdout); |
153 | 155 |
154 while (cmd.state != 255) | 156 while (cmd.state != 255) |
155 ; | 157 ; |
156 | 158 |
157 if (cmd.len > 0) { | 159 if (cmd.len < 1) |
158 if (!strncmp("gc", cmd.buf, 2)) { | 160 goto out; |
159 now = time(NULL); | 161 |
160 gmtime_r(&now, &nowtm); | 162 /* Split command string on space/tab boundaries into argv/argc */ |
161 strftime(buf, sizeof(buf) - 1, "%Y/%m/%d %H:%M:%S UTC", &nowtm); | 163 argc = 0; |
162 printf("Time is %s (%d)\r\n", buf, (int)now); | 164 tmp = cmd.buf; |
163 } else if (!strncmp("sc ", cmd.buf, 3)) { | 165 for (ap = argv; (*ap = strsep(&cmd.buf, " \t")) != NULL;) { |
164 struct timeval tv; | 166 if (**ap != '\0') { |
165 tv.tv_sec = atoi(cmd.buf + 3); | 167 argc++; |
166 tv.tv_usec = 0; | 168 if (++ap >= &argv[MAXARGS]) |
167 settimeofday(&tv, NULL); | 169 break; |
168 } else if (!strncmp("read", cmd.buf, 4)) { | 170 } |
169 printf("PB5 = %d\r\n", GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)); | 171 } |
170 } else if (!strncmp("touch", cmd.buf, 5)) { | 172 /* Reset the buffer pointer after strsep() has mangled it */ |
171 for (int i = 0; i < 10; i++) { | 173 cmd.buf = tmp; |
172 tp_getcoords(&x, &y, &z1, &z2, &t, &t2); | 174 |
173 printf("X = %5d Y = %5d Z1 = %5d Z2 = %5d T = %7.2f T2 = %7.2f\r\n", x, y, z1, z2, t, t2); | 175 if (!strcmp("gc", argv[0])) { |
174 } | 176 now = time(NULL); |
175 } else if (!strncmp("fl", cmd.buf, 2)) { | 177 gmtime_r(&now, &nowtm); |
176 uint8_t status; | 178 strftime(buf, sizeof(buf) - 1, "%Y/%m/%d %H:%M:%S UTC", &nowtm); |
177 char *flstattbl[] = { | 179 printf("Time is %s (%d)\r\n", buf, (int)now); |
178 "BUSY", | 180 } else if (!strcmp("sc", argv[0])) { |
179 "WEL", | 181 struct timeval tv; |
180 "BP0", | 182 if (argc != 2) { |
181 "BP1", | 183 fputs("Incorrect number of arguments\r\n", stdout); |
182 "BP2", | 184 goto out; |
183 "BP3", | 185 } |
184 "AAI", | 186 |
185 "BPL" | 187 tv.tv_sec = atoi(argv[1]); |
186 }; | 188 tv.tv_usec = 0; |
189 settimeofday(&tv, NULL); | |
190 } else if (!strcmp("read", argv[0])) { | |
191 printf("PB5 = %d\r\n", GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)); | |
192 } else if (!strcmp("touch", argv[0])) { | |
193 for (int i = 0; i < 10; i++) { | |
194 tp_getcoords(&x, &y, &z1, &z2, &t, &t2); | |
195 printf("X = %5d Y = %5d Z1 = %5d Z2 = %5d T = %7.2f T2 = %7.2f\r\n", x, y, z1, z2, t, t2); | |
196 } | |
197 } else if (!strcmp("fl", argv[0])) { | |
198 flashcmd(argv + 1, argc - 1); | |
199 } else if (!strcmp("pwm", argv[0])) { | |
200 lcd_setpwm(atoi(argv[1])); | |
201 } else if (!strcmp("timing", argv[0])) { | |
202 fputs("Timing..\r\n", stdout); | |
203 delay(10000); | |
204 fputs("Done\r\n", stdout); | |
205 } else if (!strcmp("circ", argv[0])) { | |
206 if (argc != 5) { | |
207 fputs("Unable to parse circ args\r\n", stdout); | |
208 goto out; | |
209 } | |
210 x = atoi(argv[1]); | |
211 y = atoi(argv[2]); | |
212 r = atoi(argv[3]); | |
213 c = lcd_parsecol(argv[4][0]); | |
214 lcd_circle(x, y, r, 0, c); | |
215 } else if (!strncmp("ellip", cmd.buf, 6)) { | |
216 if (argc != 5) { | |
217 fputs("Unable to parse ellip args\r\n", stdout); | |
218 goto out; | |
219 } | |
220 | |
221 x = atoi(argv[1]); | |
222 y = atoi(argv[2]); | |
223 rx = atoi(argv[3]); | |
224 ry = atoi(argv[4]); | |
225 c = lcd_parsecol(argv[5][0]); | |
226 | |
227 lcd_ellipse(x, y, rx, ry, 0, c); | |
228 } else if (!strncmp("line", cmd.buf, 5)) { | |
229 if (argc != 5) { | |
230 fputs("Unable to parse line args\r\n", stdout); | |
231 goto out; | |
232 } | |
233 | |
234 x = atoi(argv[1]); | |
235 y = atoi(argv[2]); | |
236 x1 = atoi(argv[3]); | |
237 y1 = atoi(argv[4]); | |
238 c = lcd_parsecol(argv[5][0]); | |
239 | |
240 lcd_line(x, y, x1, y1, c); | |
241 } else if (!strcmp("delay", argv[0])) { | |
242 GPIO_InitTypeDef GPIO_InitStructure; | |
187 | 243 |
188 printf("Flash ID = 0x%04hx (expect 0xbf41)\r\n", flashreadid()); | 244 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; |
189 status = flashreadstatus(); | 245 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
190 | 246 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; |
191 fputs("Status = ", stdout); | 247 GPIO_Init(GPIOE, &GPIO_InitStructure); |
192 for (unsigned int i = 0; i < sizeof(flstattbl) / sizeof(flstattbl[0]); i++) | 248 |
193 if (status & 1 << i) { | 249 for (x = 0; x < 100; x++) { |
194 fputs(flstattbl[i], stdout); | 250 GPIO_SetBits(GPIOE, GPIO_Pin_2); |
195 fputs(" ", stdout); | 251 delay(30); |
196 } | 252 GPIO_ResetBits(GPIOE, GPIO_Pin_2); |
197 printf("(0x%02x)\r\n", status); | 253 delay(60); |
198 | 254 } |
199 flashwritestatus(0x00); | 255 } else if (!strcmp("rs", argv[0])) { |
200 | 256 printf("Reset got %d\r\n", OWTouchReset()); |
201 status = flashreadstatus(); | 257 } else if (!strcmp("sr", argv[0])) { |
202 | 258 uint8_t ROM[8]; |
203 fputs("Status = ", stdout); | 259 int8_t i; |
204 for (unsigned int i = 0; i < sizeof(flstattbl) / sizeof(flstattbl[0]); i++) | 260 |
205 if (status & 1 << i) { | 261 memset(ROM, 0, 8); |
206 fputs(flstattbl[i], stdout); | 262 |
207 fputs(" ", stdout); | 263 i = OWFirst(ROM, 1, 0); |
208 } | 264 do { |
209 printf("(0x%02x)\r\n", status); | 265 switch (i) { |
210 } else if (!strncmp("pwm ", cmd.buf, 4)) { | 266 case OW_NOMODULES: |
211 lcd_setpwm(atoi(cmd.buf + 4)); | 267 case OW_FOUND: |
212 } else if (!strncmp("timing", cmd.buf, 6)) { | 268 break; |
213 fputs("Timing..\r\n", stdout); | |
214 delay(10000); | |
215 fputs("Done\r\n", stdout); | |
216 } else if (!strncmp("circ ", cmd.buf, 5)) { | |
217 if (sscanf(cmd.buf, "circ %hu %hu %hu %c", &x, &y, &r, &col) != 4) { | |
218 printf("Unable to parse circ args\r\n"); | |
219 goto out; | |
220 } | |
221 | |
222 c = lcd_parsecol(col); | |
223 lcd_circle(x, y, r, 0, c); | |
224 } else if (!strncmp("ellip ", cmd.buf, 6)) { | |
225 if (sscanf(cmd.buf, "ellip %hu %hu %hu %hu %c", &x, &y, &rx, &ry, &col) != 5) { | |
226 printf("Unable to parse circ args\r\n"); | |
227 goto out; | |
228 } | |
229 | |
230 c = lcd_parsecol(col); | |
231 lcd_ellipse(x, y, rx, ry, 0, c); | |
232 } else if (!strncmp("line ", cmd.buf, 5)) { | |
233 if (sscanf(cmd.buf, "line %hu %hu %hu %hu %c", &x, &y, &x1, &y1, &col) != 5) { | |
234 printf("Unable to parse line args\r\n"); | |
235 goto out; | |
236 } | |
237 | |
238 c = lcd_parsecol(col); | |
239 lcd_line(x, y, x1, y1, c); | |
240 } else if (!strncmp("delay", cmd.buf, 5)) { | |
241 GPIO_InitTypeDef GPIO_InitStructure; | |
242 | |
243 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; | |
244 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | |
245 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; | |
246 GPIO_Init(GPIOE, &GPIO_InitStructure); | |
247 | |
248 for (x = 0; x < 100; x++) { | |
249 GPIO_SetBits(GPIOE, GPIO_Pin_2); | |
250 delay(30); | |
251 GPIO_ResetBits(GPIOE, GPIO_Pin_2); | |
252 delay(60); | |
253 } | |
254 } else if (!strncmp("rs", cmd.buf, 2)) { | |
255 printf("Reset got %d\r\n", OWTouchReset()); | |
256 } else if (!strncmp("sr", cmd.buf, 2)) { | |
257 uint8_t ROM[8]; | |
258 int8_t i; | |
259 | |
260 memset(ROM, 0, 8); | |
261 | |
262 i = OWFirst(ROM, 1, 0); | |
263 do { | |
264 switch (i) { | |
265 case OW_NOMODULES: | |
266 case OW_FOUND: | |
267 break; | |
268 | 269 |
269 case OW_BADWIRE: | 270 case OW_BADWIRE: |
270 case OW_NOPRESENCE: | 271 case OW_NOPRESENCE: |
271 case OW_BADCRC: | 272 case OW_BADCRC: |
272 default: | |
273 printf("Err %d\r\n", i); | |
274 break; | |
275 } | |
276 | |
277 if (i != OW_FOUND) | |
278 break; | |
279 | |
280 for (i = 0; i < 8; i++) | |
281 printf("%02x%s", ROM[i], i == 7 ? "\r\n" : ":"); | |
282 | |
283 i = OWNext(ROM, 1, 0); | |
284 } while (1); | |
285 } else if (!strncmp("rb", cmd.buf, 2)) { | |
286 printf("Read bit returned %d\r\n", OWReadBit()); | |
287 } else if (!strncmp("wb ", cmd.buf, 3)) { | |
288 x = atoi(cmd.buf + 3); | |
289 OWWriteBit(x); | |
290 printf("Wrote %d\r\n", x); | |
291 } else if (!strncasecmp(cmd.buf, "te ", 3)) { | |
292 uint8_t ROM[8]; | |
293 int16_t res; | |
294 | |
295 if (sscanf(cmd.buf, "te %hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
296 &ROM[0], &ROM[1], &ROM[2], &ROM[3], | |
297 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) { | |
298 printf("Unable to parse ROM ID\r\n"); | |
299 goto out; | |
300 } | |
301 | |
302 res = OWGetTemp(ROM); | |
303 switch (res) { | |
304 case OW_TEMP_WRONG_FAM: | |
305 printf("ROM specified isn't a temperature sensor\r\n"); | |
306 break; | |
307 | |
308 case OW_TEMP_CRC_ERR: | |
309 printf("CRC mismatch\r\n"); | |
310 break; | |
311 | |
312 case OW_TEMP_NO_ROM: | |
313 printf("No ROM found\r\n"); | |
314 break; | |
315 | |
316 default: | 273 default: |
317 printf("%hd.%02hd\r\n", GETWHOLE(res), GETFRAC(res)); | 274 printf("Err %d\r\n", i); |
318 break; | 275 break; |
319 } | 276 } |
320 } else if (!strncmp("zz", cmd.buf, 2)) { | 277 |
321 NVIC_SystemReset(); | 278 if (i != OW_FOUND) |
322 } else { | 279 break; |
323 printf("Unknown command\r\n"); | 280 |
324 } | 281 for (i = 0; i < 8; i++) |
282 printf("%02x%s", ROM[i], i == 7 ? "\r\n" : ":"); | |
283 | |
284 i = OWNext(ROM, 1, 0); | |
285 } while (1); | |
286 } else if (!strcmp("rb", argv[0])) { | |
287 printf("Read bit returned %d\r\n", OWReadBit()); | |
288 } else if (!strcmp("wb", argv[0])) { | |
289 if (argc != 2) { | |
290 fputs("Incorrect number of arguments\r\n", stdout); | |
291 goto out; | |
292 } | |
293 | |
294 x = atoi(argv[1]); | |
295 OWWriteBit(x); | |
296 printf("Wrote %d\r\n", x); | |
297 } else if (!strcmp("te", argv[0])) { | |
298 uint8_t ROM[8]; | |
299 int16_t res; | |
300 | |
301 if (sscanf(argv[1], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
302 &ROM[0], &ROM[1], &ROM[2], &ROM[3], | |
303 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) { | |
304 fputs("Unable to parse ROM ID\r\n", stdout); | |
305 goto out; | |
306 } | |
307 | |
308 res = OWGetTemp(ROM); | |
309 switch (res) { | |
310 case OW_TEMP_WRONG_FAM: | |
311 printf("ROM specified isn't a temperature sensor\r\n"); | |
312 break; | |
313 | |
314 case OW_TEMP_CRC_ERR: | |
315 printf("CRC mismatch\r\n"); | |
316 break; | |
317 | |
318 case OW_TEMP_NO_ROM: | |
319 printf("No ROM found\r\n"); | |
320 break; | |
321 | |
322 default: | |
323 printf("%hd.%02hd\r\n", GETWHOLE(res), GETFRAC(res)); | |
324 break; | |
325 } | |
326 } else if (!strcmp("zz", argv[0])) { | |
327 NVIC_SystemReset(); | |
328 } else { | |
329 printf("Unknown command\r\n"); | |
325 } | 330 } |
326 out: | 331 out: |
327 cmd.state = 0; | 332 cmd.state = 0; |
328 } | 333 } |
329 } | 334 } |