2 * Main program code for USB host sample.
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>
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.
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.
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
33 #define NXT_HANDLE_ERR(expr, nxt, msg) \
35 nxt_error_t nxt__err_temp = (expr); \
37 return handle_error(nxt, msg, nxt__err_temp); \
40 static int handle_error(nxt_t *nxt, char *msg, nxt_error_t err)
42 printf("%s: %s\n", msg, nxt_str_error(err));
44 /* nxt_close0 should be used for LEJOS OSEK BIOS(uses leJOS I/O driver) */
49 int main(int argc, char *argv[])
56 printf("#========================================================#\n");
57 printf("# Simple USB host sample #\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");
68 NXT_HANDLE_ERR(nxt_init(&nxt), NULL,
69 "Error during library initialization");
73 if (err == NXT_NOT_PRESENT)
74 printf("NXT not found. Is it properly plugged in via USB?\n");
76 NXT_HANDLE_ERR(0, NULL, "Error while scanning for NXT");
80 /* start accessing NXT */
81 if (nxt_in_reset_mode(nxt))
83 printf("NXT found, but running in reset mode.\n");
87 /* nxt_open0 should be used for LEJOS OSEK USB application */
88 NXT_HANDLE_ERR(nxt_open0(nxt), NULL, "Error while connecting to NXT");
90 /* send 1 character to NXT and display echo data */
91 while((ch = getchar()) != EOF)
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");
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");