Parcourir la source

Part ID recognition

Implemented part ID recongnition - at this moment, it is just for testing purposes.
Mateusz Bugdalski il y a 13 ans
Parent
commit
1da69880a5
2 fichiers modifiés avec 45 ajouts et 2 suppressions
  1. 44 1
      lpc.c
  2. 1 1
      main.c

+ 44 - 1
lpc.c

@@ -1,6 +1,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <stdarg.h>
 #include <serial.h>
 #include <lpc.h>
 
@@ -42,6 +43,8 @@ const struct sprog_family lpc_family = {
 
 void lpc_ispmode(struct lpc_device *dev, int state);
 void lpc_reset(struct lpc_device *dev);
+int lpc_command(char *text, ...);
+int lpc_read_partid(struct lpc_device *dev);
 
 struct lpc_device *lpc_setup(struct serial_device *port) {
   struct lpc_device *dev;
@@ -60,8 +63,48 @@ void lpc_init(struct lpc_device *dev) {
   fgets(buf, sizeof(buf), stdin);
   if(strcmp(buf, "Synchronized\r\n")==0)
     sprog_error("Synchronization successful\n");
+  printf("Synchronized\r\n");
+  fgets(buf, sizeof(buf), stdin);
+  if(strcmp(buf, "OK\r\n")!=0)
+    sprog_error("Expected OK, received '%s'\n", buf);
+  printf("12000\r\n");
+  fgets(buf, sizeof(buf), stdin); /* deny echoed line */
+  fgets(buf, sizeof(buf), stdin);
+  if(strcmp(buf, "OK\r\n")!=0)
+    sprog_error("Expected OK, received '%s'\n", buf);
+  if(lpc_read_partid(dev)==0)
+    sprog_error("Found device: %s, %dkB Flash, %dkB RAM\n", dev->part->name, dev->part->flash, dev->part->ram);
+}
+
+int lpc_command(char *text, ...) {
+  char buf[4096];
+  int res;
+  va_list l;
+  va_start(l, text);
+  vprintf(text, l);
+  fgets(buf, sizeof(buf), stdin); /* deny echoed line */
+  if(scanf("%d\r\n", &res)<1)
+    return -1;
+  va_end(l);
+  return res;
+}
+
+int lpc_read_partid(struct lpc_device *dev) {
+  int i;
+  int res;
+  unsigned int partid;
+  res = lpc_command("J\r\n");
+  if(res!=0)
+    return res;
+  scanf("%u\r\n", &partid);
+  for(i=0; lpc_parts[i].name; i++)
+    if(lpc_parts[i].part_id==partid)
+      break;
+  if(lpc_parts[i].name)
+    dev->part = &lpc_parts[i];
   else
-    sprog_error("Synchronization error - received '%s'\n", buf);
+    return -1;
+  return 0;
 }
 
 void lpc_ispmode(struct lpc_device *dev, int state) {

+ 1 - 1
main.c

@@ -80,7 +80,7 @@ void usage(const char *name) {
   printf("%s -p <port> -f <family> [options]\n", name);
   printf("Following options are supported:\n");
   printf("  -p <port>, --port <port>		specify the serial port, eg. /dev/ttyS0\n");
-  printf("  -f <family>, --family <family>	specify the serial port, eg. /dev/ttyS0\n");
+  printf("  -f <family>, --family <family>	specify the microcontroller family, eg. lpc\n");
   printf("  -b <baud>, --baud <baud>		specify the baud rate\n");
   printf("  -h, --help				display this help\n");
   printf("  --version				print version and exit\n");