3c1bd557f7bc30665534dafabef48040848119a6
[nixiedisp] / fw / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <avr/io.h>
5 #include <avr/interrupt.h>
6 #include <util/delay.h>
7 #include "serial.h"
8
9 /* TODO before board arrive:
10  * - USB comms
11  * - RTC time/data setting
12  * - hack the digit drivers with 7seg?
13  */
14
15 /* pin assignments
16  * B[0,2]: serial clock for the 3 shift registers
17  * B4: SPI MISO connected to RTC I/O pin
18  * B5: SPI SCK connected to RTC serial clock
19  *
20  * C0: serial data for the 3 shift registers
21  * C1: hour separator LEDs
22  * C2: RTC chip select (active high)
23  *
24  * D[2,7]: nixie dots
25  */
26 #define PB_CK1          0x01
27 #define PB_CK2          0x02
28 #define PB_CK3          0x04
29 #define PB_RTC_DATA     0x10
30 #define PB_RTC_CK       0x20
31 #define PC_SDATA        0x01
32 #define PC_HRSEP        0x02
33 #define PC_RTC_EN       0x04
34 #define PD_ADOT         0x04
35 #define PD_BDOT         0x08
36 #define PD_CDOT         0x10
37 #define PD_DDOT         0x20
38 #define PD_EDOT         0x40
39 #define PD_FDOT         0x80
40
41 static void proc_cmd(char *input);
42
43 static int echo;
44
45 static char input[128];
46 static unsigned char inp_cidx;
47
48
49 int main(void)
50 {
51         /* SPI (SS/MOSI/SCK) are outputs */
52         DDRB = ~PB_RTC_DATA;    /* port B all outputs except the RTC data line */
53         PORTB = 0;
54         DDRC = 0xff;                    /* port C all outputs */
55         PORTC = 0;
56         DDRD = 0xff;                    /* port D all outputs */
57         PORTD = 0;
58
59         /* init the serial port we use to talk to the host */
60         init_serial(38400);
61         sei();
62
63         for(;;) {
64                 if(have_input()) {
65                         int c = getchar();
66                         if(echo) {
67                                 putchar(c);
68                         }
69
70                         if(c == '\r' || c == '\n') {
71                                 input[inp_cidx] = 0;
72                                 proc_cmd(input);
73                                 inp_cidx = 0;
74                         } else if(inp_cidx < sizeof input - 1) {
75                                 input[inp_cidx++] = c;
76                         }
77                 }
78         }
79         return 0;
80 }
81
82 static void proc_cmd(char *input)
83 {
84         switch(input[0]) {
85         case 'e':
86                 echo = input[1] == '1' ? 1 : 0;
87                 printf("OK echo %s\n", echo ? "on" : "off");
88                 break;
89
90         case '?':
91                 puts("OK command help");
92                 puts(" e 0|1: turn echo on/off");
93                 break;
94
95         default:
96                 printf("ERR unknown command: '%c'\n", input[0]);
97         }
98 }