comparison tests/cptest.c @ 1:f7c60e525801

cptest and cp_ping.py to test cmd_proto.c.
author Thinker K.F. Li <thinker@branda.to>
date Sat, 21 Feb 2009 20:20:06 +0800
parents
children e410832c3280
comparison
equal deleted inserted replaced
0:a0ce8ebf2f18 1:f7c60e525801
1 #include <stdio.h>
2 #include <string.h>
3 #include <util/delay.h>
4 #include "avriotools.h"
5 #include "cmd_proto.h"
6
7 #define BAUD_RATE 420000
8
9 void flash_init(void) {
10 pin_mode(&PORTB, PINB0, PM_OUTPUT);
11 pin_mode(&PORTB, PINB1, PM_OUTPUT);
12 pin_lo(PORTB, PINB0);
13 pin_lo(PORTB, PINB1);
14 }
15
16 void flash_led(void) {
17 pin_hi(PORTB, PINB0);
18 _delay_ms(50);
19 pin_lo(PORTB, PINB0);
20 }
21
22 int main(int argc, char * const argv[]) {
23 int c;
24 cmd_proto_t *cp;
25 cp_cmd_t *cmd;
26 char buf[255 + CP_CMD_OVERHEAD];
27 int sz;
28 int i;
29
30 flash_init();
31
32 uart_init(BAUD_RATE);
33
34 cp = cmd_proto_new();
35
36 while(1) {
37 uart_getc(c);
38 cmd = cmd_proto_rcv(cp, c);
39 if(cmd == NULL)
40 continue;
41
42 flash_led();
43
44 if(cmd == &BAD_CMD) {
45 memcpy(buf + CP_CMD_HEAD_SZ, "bad cmd", 7);
46 sz = cmd_proto_cmd_fill(buf, cmd->seq, CPCMD_NAK, 7);
47 for(i = 0; i < sz; i++)
48 uart_putc(buf[i]);
49 continue;
50 }
51
52 if(cmd == &CSUM_ERR_CMD) {
53 memcpy(buf + CP_CMD_HEAD_SZ, "csum err", 8);
54 sz = cmd_proto_cmd_fill(buf, cmd->seq, CPCMD_NAK, 8);
55 for(i = 0; i < sz; i++)
56 uart_putc(buf[i]);
57 continue;
58 }
59
60 switch(cmd->code) {
61 case CPCMD_PING:
62 memcpy(buf + CP_CMD_HEAD_SZ, cmd->data, cmd->data_sz);
63 sz = cmd_proto_cmd_fill(buf, cmd->seq, CPCMD_PONG, cmd->data_sz);
64 for(i = 0; i < sz; i++)
65 uart_putc(buf[i]);
66 break;
67
68 default:
69 memcpy(buf + CP_CMD_HEAD_SZ, cmd->data, cmd->data_sz);
70 sz = cmd_proto_cmd_fill(buf, cmd->seq, CPCMD_DATA, cmd->data_sz);
71 for(i = 0; i < sz; i++)
72 uart_putc(buf[i]);
73 }
74 }
75 }