Mercurial > ~darius > hgwebdir.cgi > stm32temp
comparison main.c @ 46:5b7d21698a80
When writing to stdout/stderr translate \n to \r\n.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Wed, 03 Apr 2013 23:29:16 +1030 |
parents | 6466e8720e04 |
children | ace431a0d0f5 |
comparison
equal
deleted
inserted
replaced
45:6466e8720e04 | 46:5b7d21698a80 |
---|---|
64 c = USART_ReceiveData(USART1); | 64 c = USART_ReceiveData(USART1); |
65 | 65 |
66 /* End of line? */ | 66 /* End of line? */ |
67 if (c == '\n' || c == '\r') { | 67 if (c == '\n' || c == '\r') { |
68 cmd.buf[cmd.state] = '\0'; | 68 cmd.buf[cmd.state] = '\0'; |
69 fputs("\r\n", stdout); | 69 fputs("\n", stdout); |
70 cmd.len = cmd.state; | 70 cmd.len = cmd.state; |
71 cmd.state = 255; | 71 cmd.state = 255; |
72 continue; | 72 continue; |
73 } | 73 } |
74 | 74 |
99 comm_put(cmd.buf[cmd.state]); | 99 comm_put(cmd.buf[cmd.state]); |
100 | 100 |
101 cmd.state++; | 101 cmd.state++; |
102 /* Over flow? */ | 102 /* Over flow? */ |
103 if (cmd.state == LINEBUF - 1) { | 103 if (cmd.state == LINEBUF - 1) { |
104 fputs("\r\nLine too long", stdout); | 104 fputs("\nLine too long", stdout); |
105 cmd.state = 0; | 105 cmd.state = 0; |
106 continue; | 106 continue; |
107 } | 107 } |
108 } | 108 } |
109 } | 109 } |
127 NVIC_Configuration(); | 127 NVIC_Configuration(); |
128 | 128 |
129 /* Setup SysTick Timer rate, also enables Systick and Systick-Interrupt */ | 129 /* Setup SysTick Timer rate, also enables Systick and Systick-Interrupt */ |
130 if (SysTick_Config(SystemCoreClock / TICK_FREQ)) { | 130 if (SysTick_Config(SystemCoreClock / TICK_FREQ)) { |
131 /* Capture error */ | 131 /* Capture error */ |
132 comm_puts("Can't setup SysTick\r\n"); | 132 comm_puts("Can't setup SysTick\n"); |
133 while (1) | 133 while (1) |
134 ; | 134 ; |
135 } | 135 } |
136 | 136 |
137 /* Set stdout to unbuffered */ | 137 /* Set stdout to unbuffered */ |
138 setvbuf(stdout, NULL, _IONBF, 0); | 138 setvbuf(stdout, NULL, _IONBF, 0); |
139 | 139 |
140 /* Say hello */ | 140 /* Say hello */ |
141 fputs("\r\n\r\n\r\nHello world\r\n", stdout); | 141 fputs("\n\n\nHello world\n", stdout); |
142 | 142 |
143 lcd_stripes(); | 143 lcd_stripes(); |
144 | 144 |
145 lcd_circle(20, 20, 20, 1, LCD_RED); /* Bottom left */ | 145 lcd_circle(20, 20, 20, 1, LCD_RED); /* Bottom left */ |
146 lcd_circle(300, 220, 20, 1, LCD_WHITE); /* Top right */ | 146 lcd_circle(300, 220, 20, 1, LCD_WHITE); /* Top right */ |
187 | 187 |
188 if (!strcmp("gc", argv[0])) { | 188 if (!strcmp("gc", argv[0])) { |
189 now = time(NULL); | 189 now = time(NULL); |
190 gmtime_r(&now, &nowtm); | 190 gmtime_r(&now, &nowtm); |
191 strftime(buf, sizeof(buf) - 1, "%Y/%m/%d %H:%M:%S UTC", &nowtm); | 191 strftime(buf, sizeof(buf) - 1, "%Y/%m/%d %H:%M:%S UTC", &nowtm); |
192 printf("Time is %s (%d)\r\n", buf, (int)now); | 192 printf("Time is %s (%d)\n", buf, (int)now); |
193 } else if (!strcmp("sc", argv[0])) { | 193 } else if (!strcmp("sc", argv[0])) { |
194 struct timeval tv; | 194 struct timeval tv; |
195 if (argc != 2) { | 195 if (argc != 2) { |
196 fputs("Incorrect number of arguments\r\n", stdout); | 196 fputs("Incorrect number of arguments\n", stdout); |
197 goto out; | 197 goto out; |
198 } | 198 } |
199 | 199 |
200 tv.tv_sec = atoi(argv[1]); | 200 tv.tv_sec = atoi(argv[1]); |
201 tv.tv_usec = 0; | 201 tv.tv_usec = 0; |
202 settimeofday(&tv, NULL); | 202 settimeofday(&tv, NULL); |
203 } else if (!strcmp("read", argv[0])) { | 203 } else if (!strcmp("read", argv[0])) { |
204 printf("PB5 = %d\r\n", GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)); | 204 printf("PB5 = %d\n", GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)); |
205 } else if (!strcmp("touch", argv[0])) { | 205 } else if (!strcmp("touch", argv[0])) { |
206 for (int i = 0; i < 10; i++) { | 206 for (int i = 0; i < 10; i++) { |
207 tp_getcoords(&x, &y, &z1, &z2, &t, &t2); | 207 tp_getcoords(&x, &y, &z1, &z2, &t, &t2); |
208 printf("X = %5d Y = %5d Z1 = %5d Z2 = %5d T = %7.2f T2 = %7.2f\r\n", x, y, z1, z2, t, t2); | 208 printf("X = %5d Y = %5d Z1 = %5d Z2 = %5d T = %7.2f T2 = %7.2f\n", x, y, z1, z2, t, t2); |
209 } | 209 } |
210 } else if (!strcmp("fl", argv[0])) { | 210 } else if (!strcmp("fl", argv[0])) { |
211 flashcmd(argc - 1, argv + 1); | 211 flashcmd(argc - 1, argv + 1); |
212 } else if (!strcmp("tc", argv[0])) { | 212 } else if (!strcmp("tc", argv[0])) { |
213 tempctrl_cmd(argc - 1, argv + 1); | 213 tempctrl_cmd(argc - 1, argv + 1); |
214 } else if (!strcmp("pwm", argv[0])) { | 214 } else if (!strcmp("pwm", argv[0])) { |
215 lcd_setpwm(atoi(argv[1])); | 215 lcd_setpwm(atoi(argv[1])); |
216 } else if (!strcmp("circ", argv[0])) { | 216 } else if (!strcmp("circ", argv[0])) { |
217 if (argc != 5) { | 217 if (argc != 5) { |
218 fputs("Unable to parse circ args\r\n", stdout); | 218 fputs("Unable to parse circ args\n", stdout); |
219 goto out; | 219 goto out; |
220 } | 220 } |
221 x = atoi(argv[1]); | 221 x = atoi(argv[1]); |
222 y = atoi(argv[2]); | 222 y = atoi(argv[2]); |
223 r = atoi(argv[3]); | 223 r = atoi(argv[3]); |
224 c = lcd_parsecol(argv[4][0]); | 224 c = lcd_parsecol(argv[4][0]); |
225 lcd_circle(x, y, r, 0, c); | 225 lcd_circle(x, y, r, 0, c); |
226 } else if (!strncmp("ellip", cmd.buf, 6)) { | 226 } else if (!strncmp("ellip", cmd.buf, 6)) { |
227 if (argc != 5) { | 227 if (argc != 5) { |
228 fputs("Unable to parse ellip args\r\n", stdout); | 228 fputs("Unable to parse ellip args\n", stdout); |
229 goto out; | 229 goto out; |
230 } | 230 } |
231 | 231 |
232 x = atoi(argv[1]); | 232 x = atoi(argv[1]); |
233 y = atoi(argv[2]); | 233 y = atoi(argv[2]); |
236 c = lcd_parsecol(argv[5][0]); | 236 c = lcd_parsecol(argv[5][0]); |
237 | 237 |
238 lcd_ellipse(x, y, rx, ry, 0, c); | 238 lcd_ellipse(x, y, rx, ry, 0, c); |
239 } else if (!strncmp("line", cmd.buf, 5)) { | 239 } else if (!strncmp("line", cmd.buf, 5)) { |
240 if (argc != 5) { | 240 if (argc != 5) { |
241 fputs("Unable to parse line args\r\n", stdout); | 241 fputs("Unable to parse line args\n", stdout); |
242 goto out; | 242 goto out; |
243 } | 243 } |
244 | 244 |
245 x = atoi(argv[1]); | 245 x = atoi(argv[1]); |
246 y = atoi(argv[2]); | 246 y = atoi(argv[2]); |
256 GPIO_ResetBits(GPIOE, GPIO_Pin_2); | 256 GPIO_ResetBits(GPIOE, GPIO_Pin_2); |
257 delay(60); | 257 delay(60); |
258 } | 258 } |
259 } else if (!strcmp("cyc", argv[0])) { | 259 } else if (!strcmp("cyc", argv[0])) { |
260 if (argc != 2) { | 260 if (argc != 2) { |
261 fputs("Incorrect number of arguments\r\n", stdout); | 261 fputs("Incorrect number of arguments\n", stdout); |
262 goto out; | 262 goto out; |
263 } | 263 } |
264 | 264 |
265 GPIO_SetBits(GPIOE, GPIO_Pin_2); | 265 GPIO_SetBits(GPIOE, GPIO_Pin_2); |
266 delay(atoi(argv[1])); | 266 delay(atoi(argv[1])); |
267 GPIO_ResetBits(GPIOE, GPIO_Pin_2); | 267 GPIO_ResetBits(GPIOE, GPIO_Pin_2); |
268 | 268 |
269 fputs("Done\r\n", stdout); | 269 fputs("Done\n", stdout); |
270 } else if (!strcmp("rs", argv[0])) { | 270 } else if (!strcmp("rs", argv[0])) { |
271 printf("Reset got %d\r\n", OWTouchReset()); | 271 printf("Reset got %d\n", OWTouchReset()); |
272 } else if (!strcmp("sr", argv[0])) { | 272 } else if (!strcmp("sr", argv[0])) { |
273 uint8_t ROM[8]; | 273 uint8_t ROM[8]; |
274 int8_t i; | 274 int8_t i; |
275 | 275 |
276 memset(ROM, 0, 8); | 276 memset(ROM, 0, 8); |
284 | 284 |
285 case OW_BADWIRE: | 285 case OW_BADWIRE: |
286 case OW_NOPRESENCE: | 286 case OW_NOPRESENCE: |
287 case OW_BADCRC: | 287 case OW_BADCRC: |
288 default: | 288 default: |
289 printf("Err %d\r\n", i); | 289 printf("Err %d\n", i); |
290 break; | 290 break; |
291 } | 291 } |
292 | 292 |
293 if (i != OW_FOUND) | 293 if (i != OW_FOUND) |
294 break; | 294 break; |
295 | 295 |
296 for (i = 0; i < 8; i++) | 296 for (i = 0; i < 8; i++) |
297 printf("%02x%s", ROM[i], i == 7 ? "\r\n" : ":"); | 297 printf("%02x%s", ROM[i], i == 7 ? "\n" : ":"); |
298 | 298 |
299 i = OWNext(ROM, 1, 0); | 299 i = OWNext(ROM, 1, 0); |
300 } while (1); | 300 } while (1); |
301 } else if (!strcmp("rb", argv[0])) { | 301 } else if (!strcmp("rb", argv[0])) { |
302 printf("Read bit returned %d\r\n", OWReadBit()); | 302 printf("Read bit returned %d\n", OWReadBit()); |
303 } else if (!strcmp("wb", argv[0])) { | 303 } else if (!strcmp("wb", argv[0])) { |
304 if (argc != 2) { | 304 if (argc != 2) { |
305 fputs("Incorrect number of arguments\r\n", stdout); | 305 fputs("Incorrect number of arguments\n", stdout); |
306 goto out; | 306 goto out; |
307 } | 307 } |
308 | 308 |
309 x = atoi(argv[1]); | 309 x = atoi(argv[1]); |
310 OWWriteBit(x); | 310 OWWriteBit(x); |
311 printf("Wrote %d\r\n", x); | 311 printf("Wrote %d\n", x); |
312 } else if (!strcmp("te", argv[0])) { | 312 } else if (!strcmp("te", argv[0])) { |
313 uint8_t ROM[8]; | 313 uint8_t ROM[8]; |
314 int16_t res; | 314 int16_t res; |
315 | 315 |
316 if (sscanf(argv[1], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | 316 if (sscanf(argv[1], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
317 &ROM[0], &ROM[1], &ROM[2], &ROM[3], | 317 &ROM[0], &ROM[1], &ROM[2], &ROM[3], |
318 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) { | 318 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) { |
319 fputs("Unable to parse ROM ID\r\n", stdout); | 319 fputs("Unable to parse ROM ID\n", stdout); |
320 goto out; | 320 goto out; |
321 } | 321 } |
322 | 322 |
323 res = OWGetTemp(ROM); | 323 res = OWGetTemp(ROM); |
324 switch (res) { | 324 switch (res) { |
325 case OW_TEMP_WRONG_FAM: | 325 case OW_TEMP_WRONG_FAM: |
326 printf("ROM specified isn't a temperature sensor\r\n"); | 326 printf("ROM specified isn't a temperature sensor\n"); |
327 break; | 327 break; |
328 | 328 |
329 case OW_TEMP_CRC_ERR: | 329 case OW_TEMP_CRC_ERR: |
330 printf("CRC mismatch\r\n"); | 330 printf("CRC mismatch\n"); |
331 break; | 331 break; |
332 | 332 |
333 case OW_TEMP_NO_ROM: | 333 case OW_TEMP_NO_ROM: |
334 printf("No ROM found\r\n"); | 334 printf("No ROM found\n"); |
335 break; | 335 break; |
336 | 336 |
337 default: | 337 default: |
338 printf("%hd.%02hd\r\n", GETWHOLE(res), GETFRAC(res)); | 338 printf("%hd.%02hd\n", GETWHOLE(res), GETFRAC(res)); |
339 break; | 339 break; |
340 } | 340 } |
341 } else if (!strcmp("rtc", argv[0])) { | 341 } else if (!strcmp("rtc", argv[0])) { |
342 float f, err, maxerr; | 342 float f, err, maxerr; |
343 uint32_t d, i; | 343 uint32_t d, i; |
345 maxerr = 0; | 345 maxerr = 0; |
346 for (i = 0; i < 32768; i++) { | 346 for (i = 0; i < 32768; i++) { |
347 d = RTC_PS2USEC(32768 - i); | 347 d = RTC_PS2USEC(32768 - i); |
348 f = ((float)i * 1e6) / (float)RTC_PRESCALE; | 348 f = ((float)i * 1e6) / (float)RTC_PRESCALE; |
349 err = fabs(d - f); | 349 err = fabs(d - f); |
350 //rtcprintf("i = %d, d = %d, f = %.3f, err = %.3f\r\n", i, d, f, err); | 350 //rtcprintf("i = %d, d = %d, f = %.3f, err = %.3f\n", i, d, f, err); |
351 if (err > maxerr) | 351 if (err > maxerr) |
352 maxerr = err; | 352 maxerr = err; |
353 } | 353 } |
354 printf("Max err = %.3f\r\n", maxerr); | 354 printf("Max err = %.3f\n", maxerr); |
355 } else if (!strcmp("assert", argv[0])) { | 355 } else if (!strcmp("assert", argv[0])) { |
356 assert(0 == 1); | 356 assert(0 == 1); |
357 } else if (!strcmp("zz", argv[0])) { | 357 } else if (!strcmp("zz", argv[0])) { |
358 NVIC_SystemReset(); | 358 NVIC_SystemReset(); |
359 } else { | 359 } else { |
360 printf("Unknown command\r\n"); | 360 printf("Unknown command\n"); |
361 } | 361 } |
362 out: | 362 out: |
363 cmd.state = 0; | 363 cmd.state = 0; |
364 } | 364 } |
365 } | 365 } |