OSDN Git Service

Update lejos_osek to nxtOSEK_v205b0.zip
[nxt-jsp/etrobo-atk.git] / nxtOSEK / samples_c / usbtest / usbhost / main_usbhost.c
1 /**
2  * Main program code for USB host sample.
3  *
4  * Copyright 2008 Takashi Chikamasa <takashic@cybernet.co.jp>
5  * Based on runc by Lawrie Griffiths <lawrie.griffiths@ntlworld.com>
6  * and fwflash by David Anderson <david.anderson@calixo.net>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "error.h"
29 #include "lowlevel.h"
30 #include "samba.h"
31 #include "firmware.h"
32
33 #define NXT_HANDLE_ERR(expr, nxt, msg)     \
34   do {                                     \
35     nxt_error_t nxt__err_temp = (expr);    \
36     if (nxt__err_temp)                     \
37       return handle_error(nxt, msg, nxt__err_temp);  \
38   } while(0)
39
40 static int handle_error(nxt_t *nxt, char *msg, nxt_error_t err)
41 {
42   printf("%s: %s\n", msg, nxt_str_error(err));
43   if (nxt != NULL)
44     /* nxt_close0 should be used for LEJOS OSEK BIOS(uses leJOS I/O driver) */
45     nxt_close0(nxt);
46   exit(err);
47 }
48
49 int main(int argc, char *argv[])
50 {
51   nxt_t *nxt;
52   nxt_error_t err;
53
54   int ch;
55   
56   printf("#========================================================#\n");
57   printf("#                 Simple USB host sample                 #\n");
58   printf("#                                                        #\n");
59   printf("# - Run usbtest program in the NXT before starting this  #\n");
60   printf("#   program.                                             #\n");
61   printf("# - Type characters and press Enter in the keyboard,     #\n");
62   printf("#   NXT returns the same characters and displayed        #\n");
63   printf("#   in the PC console.                                   #\n");
64   printf("# - To stop this program, press Ctrl+D (EOF).            #\n");
65   printf("#========================================================#\n\n");
66
67   /* init USB */
68   NXT_HANDLE_ERR(nxt_init(&nxt), NULL,
69                  "Error during library initialization");
70   err = nxt_find(nxt);
71   if (err)
72   {
73       if (err == NXT_NOT_PRESENT)
74         printf("NXT not found. Is it properly plugged in via USB?\n");
75       else
76         NXT_HANDLE_ERR(0, NULL, "Error while scanning for NXT");
77       exit(1);
78   }
79
80   /* start accessing NXT */
81   if (nxt_in_reset_mode(nxt))
82   {
83       printf("NXT found, but running in reset mode.\n");
84       exit(2);
85   }
86
87   /* nxt_open0 should be used for LEJOS OSEK USB application */
88   NXT_HANDLE_ERR(nxt_open0(nxt), NULL, "Error while connecting to NXT");
89
90   /* send 1 character to NXT and display echo data */
91   while((ch = getchar()) != EOF)
92   {
93         NXT_HANDLE_ERR(nxt_send_buf(nxt, (unsigned char *)&ch, 1), NULL, "Error Sending data");
94         NXT_HANDLE_ERR(nxt_recv_buf(nxt, (unsigned char *)&ch, 1), NULL, "Error Receiving data");
95     putchar(ch);
96   }
97   
98   /* nxt_close0 should be used for LEJOS OSEK application */
99   NXT_HANDLE_ERR(nxt_close0(nxt), NULL,
100                  "Error while closing connection to NXT");
101   return 0;
102 }