1 /***************************************************************************
2 * Copyright (C) 2011 by Martin Schmoelzer *
3 * <martin.schmoelzer@student.tuwien.ac.at> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
28 #include "reg_ezusb.h"
31 * @file Implementation of the OpenULINK communication protocol.
33 * The OpenULINK protocol uses one OUT and one IN endpoint. These two endpoints
34 * are configured to use the maximum packet size for full-speed transfers,
35 * 64 bytes. Commands always start with a command ID (see msgtypes.h for
36 * command ID definitions) and contain zero or more payload data bytes in both
37 * transfer directions (IN and OUT). The payload
39 * Almost all commands contain a fixed number of payload data bytes. The number
40 * of payload data bytes for the IN and OUT direction does not need to be the
43 * Multiple commands may be sent in one EP2 Bulk-OUT packet. Because the
44 * OpenULINK firmware does not perform bounds checking for EP2 Bulk-IN packets,
45 * the host MUST ensure that the commands sent in the OUT packet require a
46 * maximum of 64 bytes of IN data.
49 /** Index in EP2 Bulk-OUT data buffer that contains the current command ID */
50 volatile u8 cmd_id_index;
52 /** Number of data bytes already in EP2 Bulk-IN buffer */
53 volatile u8 payload_index_in;
56 * Execute a SET_LEDS command.
58 void execute_set_led_command(void)
60 u8 led_state = OUT2BUF[cmd_id_index + 1];
62 if (led_state & RUN_LED_ON) {
66 if (led_state & COM_LED_ON) {
70 if (led_state & RUN_LED_OFF) {
74 if (led_state & COM_LED_OFF) {
80 * Executes one command and updates global command indexes.
82 * @param index the index of the Bulk EP2-OUT data buffer at which the
83 * command ID is stored.
84 * @return true if this command was the last command.
85 * @return false if there are more commands within the current contents of the
86 * Bulk EP2-OUT data buffer.
88 bool execute_command(void)
90 u8 usb_out_bytecount, usb_in_bytecount;
94 /* Most commands do not transfer IN data. To save code space, we write 0 to
95 * usb_in_bytecount here, then modify it in the switch statement below where
99 switch (OUT2BUF[cmd_id_index] /* Command ID */) {
101 usb_out_bytecount = 5;
102 usb_in_bytecount = OUT2BUF[cmd_id_index + 1];
103 jtag_scan_in(cmd_id_index + 1, payload_index_in);
106 usb_out_bytecount = OUT2BUF[cmd_id_index + 1] + 5;
107 jtag_scan_out(cmd_id_index + 1);
110 usb_in_bytecount = OUT2BUF[cmd_id_index + 1];
111 usb_out_bytecount = usb_in_bytecount + 5;
112 jtag_scan_io(cmd_id_index + 1, payload_index_in);
115 usb_out_bytecount = 2;
116 jtag_clock_tms(OUT2BUF[cmd_id_index + 1], OUT2BUF[cmd_id_index + 2]);
119 usb_out_bytecount = 2;
120 count = (u16)OUT2BUF[cmd_id_index + 1];
121 count |= ((u16)OUT2BUF[cmd_id_index + 2]) << 8;
122 jtag_clock_tck(count);
125 usb_out_bytecount = 2;
126 count = (u16)OUT2BUF[cmd_id_index + 1];
127 count |= ((u16)OUT2BUF[cmd_id_index + 2]) << 8;
131 usb_out_bytecount = 2;
132 count = (u16)OUT2BUF[cmd_id_index + 1];
133 count |= ((u16)OUT2BUF[cmd_id_index + 2]) << 8;
136 case CMD_GET_SIGNALS:
137 usb_out_bytecount = 0;
138 usb_in_bytecount = 2;
139 signal_state = jtag_get_signals();
140 IN2BUF[payload_index_in] = (signal_state >> 8) & 0x00FF;
141 IN2BUF[payload_index_in + 1] = signal_state & 0x00FF;
143 case CMD_SET_SIGNALS:
144 usb_out_bytecount = 2;
145 jtag_set_signals(OUT2BUF[cmd_id_index + 1], OUT2BUF[cmd_id_index + 2]);
148 usb_out_bytecount = 1;
149 execute_set_led_command();
152 usb_out_bytecount = 1;
153 /* Do nothing... This command is only used to test if the device is ready
154 * to accept new commands */
157 /* Should never be reached */
158 usb_out_bytecount = 0;
162 /* Update EP2 Bulk-IN data byte count */
163 payload_index_in += usb_in_bytecount;
165 /* Determine if this was the last command */
166 if ((cmd_id_index + usb_out_bytecount + 1) >= OUT2BC) {
170 /* Not the last command, update cmd_id_index */
171 cmd_id_index += (usb_out_bytecount + 1);
177 * Forever wait for commands and execute them as they arrive.
179 void command_loop(void)
185 payload_index_in = 0;
187 /* Wait until host sends EP2 Bulk-OUT packet */
191 /* Turn on COM LED to indicate command execution */
194 /* Execute the commands */
195 last_command = false;
196 while (last_command == false) {
197 last_command = execute_command();
202 /* Send back EP2 Bulk-IN packet if required */
203 if (payload_index_in > 0) {
204 IN2BC = payload_index_in;
209 /* Re-arm EP2-OUT after command execution */