6 #include <avr/interrupt.h>
7 #include <util/delay.h>
10 /* TODO before board arrive:
12 * - RTC time/data setting
13 * - hack the digit drivers with 7seg?
17 * B[0,2]: serial clock for the 3 shift registers
18 * B4: SPI MISO connected to RTC I/O pin
19 * B5: SPI SCK connected to RTC serial clock
21 * C0: serial data for the 3 shift registers
22 * C1: hour separator LEDs
23 * C2: RTC chip select (active high)
30 #define PB_RTC_DATA 0x10
31 #define PB_RTC_CK 0x20
34 #define PC_RTC_EN 0x04
42 static void proc_cmd(char *input);
43 static void update_display(void);
44 static void show_number(uint32_t xnum);
45 static void setclock(int hr, int min, int sec);
46 static void setdate(int day, int mon, int year);
48 enum { MODE_CLOCK, MODE_NUM };
51 static int echo, blank;
52 static uint32_t number;
54 static char input[128];
55 static unsigned char inp_cidx;
60 /* SPI (SS/MOSI/SCK) are outputs */
61 DDRB = ~PB_RTC_DATA; /* port B all outputs except the RTC data line */
63 DDRC = 0xff; /* port C all outputs */
65 DDRD = 0xff; /* port D all outputs */
68 /* init the serial port we use to talk to the host */
79 if(c == '\r' || c == '\n') {
83 } else if(inp_cidx < sizeof input - 1) {
84 input[inp_cidx++] = c;
91 static const char *helpstr =
93 " e 0|1: turn echo on/off\n"
94 " b 0|1: blank/unblank display\n"
95 " m n|c: change display mode (n: number, c: clock)\n"
96 " s <hr>:<min>.<sec>: set clock\n"
97 " d <day>/<mon>/<year>: set date\n"
98 " ?/h: print command help\n";
100 static void proc_cmd(char *input)
102 int cmd, hr, min, sec, day, mon, year;
105 while(*input && isspace(*input)) input++;
110 while(*args && isspace(*args)) args++;
115 printf("OK echo %s\n", echo ? "on" : "off");
119 printf("OK %sblanking display\n", blank ? "" : "un");
125 if(input[1] == 'c') {
126 printf("OK clock mode\n");
129 } else if(input[1] == 'n') {
130 printf("OK number mode\n");
134 printf("ERR invalid mode: '%s'\n", args);
140 if(sscanf(args, "%d:%d.%d", &hr, &min, &sec) < 2) {
141 printf("ERR invalid time string: \"%s\"\n", args);
144 setclock(hr, min, sec);
145 printf("OK clock set\n");
146 if(mode == MODE_CLOCK) {
152 if(sscanf(args, "%d/%d/%d", &day, &mon, &year) != 3 || day < 1 || day > 31 ||
153 mon < 1 || mon > 12 || year < 0) {
154 printf("ERR invalid date string: \"%s\"\n", args);
157 if(year < 100) year += 2000;
158 setdate(day, mon, year);
159 printf("OK date set\n");
166 long num = strtol(args, &endp, 16);
168 printf("ERR invalid hex number: \"%s\"\n", args);
172 if(mode == MODE_NUM) {
180 puts("OK command help");
185 if(isdigit(args[0])) {
186 int num = atoi(args);
187 number = (uint32_t)num << 16;
188 printf("OK number: %d (%08lxh)\n", num, number);
189 if(mode == MODE_NUM) {
193 printf("ERR unknown command: '%c'\n", cmd);
198 static void update_display(void)
202 static void show_number(uint32_t xnum)
206 static void setclock(int hr, int min, int sec)
210 static void setdate(int day, int mon, int year)