2009-12-02

Bringing up the UART

I spent a moment with the mega8515 again. This time I'm using a small dev board I made. It contains a level shifter and a ready header you can plug an old PC serial port to. I read a bit on how avr-libc does stdio and with a few lines, the cylon light now prints out the state in the main loop. You could easily compare the state to a previous one or some flag and just print the value when it changes but the point was mostly just to get some text out. Another exercise might be to use backspacing to make a character dance on the terminal like the LED does on the board. My current goal is some minimal command line I can use to fiddle with the system without having to constantly do the write-compile-flash-test loop. It goes something like this:

   printf_P(PSTR("Command prompt test\n"));
   printf_P(PSTR("p x: print x"));
   printf_P(PSTR("l:   show state of port A"));
   while(1) {
       printf_P(PSTR("cmd> ")); // prompt

       if (fgets(buf, sizeof buf - 1, stdin) == NULL) // ^C, maybe EOF
           break;
       if (tolower(buf[0]) == 'q') // q)uit
           break;

       switch (tolower(buf[0])) {
           default:
               printf("Unknown command: %s\n", buf);
               break;

           case 'p':
               if (sscanf(buf, "%*s %s", s) > 0)
                   printf("Got %s\n", s);
               else
                   printf("scanf failed\n");
               break;

           case 'l':
               printf("Porta: 0x%02X\n", (uint8_t) ~PORTA);
               break;
       }
   }
   printf("Bye!\n");

It's kind of neat how the familiar Unixy string stuff is largely there.