cdc_eem.c
00001 #include "cdc_eem.h"
00002 #include "contiki.h"
00003 #include "usb_drv.h"
00004 #include "usb_descriptors.h"
00005 #include "usb_specific_request.h"
00006 #include "rndis/rndis_task.h"
00007 #include "rndis/rndis_protocol.h"
00008 #include "uip.h"
00009 #include "sicslow_ethernet.h"
00010 #include <stdio.h>
00011
00012 #include <avr/pgmspace.h>
00013 #include <util/delay.h>
00014
00015 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
00016 #define PRINTF printf
00017 #define PRINTF_P printf_P
00018
00019 extern uint8_t usb_eth_data_buffer[64];
00020
00021
00022
00023
00024
00025 #define EEMCMD_ECHO 0x00
00026 #define EEMCMD_ECHO_RESPONSE 0x01
00027 #define EEMCMD_SUSPEND_HINT 0x02
00028 #define EEMCMD_RESPONSE_HINT 0x03
00029 #define EEMCMD_RESPONSE_COMPLETE_HINT 0x04
00030 #define EEMCMD_TICKLE 0x05
00031
00032
00033 void cdc_eem_configure_endpoints() {
00034 usb_configure_endpoint(TX_EP, \
00035 TYPE_BULK, \
00036 DIRECTION_IN, \
00037 SIZE_64, \
00038 TWO_BANKS, \
00039 NYET_ENABLED);
00040
00041 usb_configure_endpoint(RX_EP, \
00042 TYPE_BULK, \
00043 DIRECTION_OUT, \
00044 SIZE_64, \
00045 TWO_BANKS, \
00046 NYET_ENABLED);
00047 Usb_reset_endpoint(TX_EP);
00048 Usb_reset_endpoint(RX_EP);
00049 }
00050
00051 void cdc_eem_process(void) {
00052 uint16_t datalength;
00053 uint8_t bytecounter, headercounter;
00054 uint16_t i;
00055
00056 #ifdef USB_ETH_HOOK_INIT
00057 static uint8_t doInit = 1;
00058 if (doInit) {
00059 USB_ETH_HOOK_INIT();
00060 doInit = 0;
00061 }
00062 #endif
00063
00064
00065 Led0_on();
00066
00067 Usb_select_endpoint(RX_EP);
00068
00069
00070 if(Is_usb_receive_out() && (uip_len == 0)) {
00071
00072
00073 bytecounter = Usb_byte_counter_8();
00074
00075
00076 headercounter = 2;
00077
00078 uint8_t fail = 0;
00079
00080
00081 if (bytecounter < headercounter) {
00082 Usb_ack_receive_out();
00083
00084 }
00085
00086
00087 i = 0;
00088 while (headercounter) {
00089 usb_eth_data_buffer[i] = Usb_read_byte();
00090 bytecounter--;
00091 headercounter--;
00092 i++;
00093 }
00094
00095
00096
00097 if (usb_eth_data_buffer[1] & 0x80) {
00098
00099 datalength = 0;
00100 } else {
00101
00102
00103 datalength = usb_eth_data_buffer[0] | ((usb_eth_data_buffer[1] & 0x3F) << 8);
00104 }
00105
00106
00107 if ((datalength == 0) && (fail == 0))
00108 {
00109 uint8_t command;
00110 uint16_t echoLength;
00111
00112
00113 command = usb_eth_data_buffer[1] & 0x38;
00114 command = command >> 3;
00115
00116
00117 switch (command)
00118 {
00119
00120 case EEMCMD_ECHO:
00121
00122
00123 echoLength = (usb_eth_data_buffer[1] & 0x07) << 8;
00124 echoLength |= usb_eth_data_buffer[0];
00125
00126
00127
00128 break;
00129
00130
00131 case EEMCMD_ECHO_RESPONSE:
00132 case EEMCMD_SUSPEND_HINT:
00133 case EEMCMD_RESPONSE_HINT:
00134 case EEMCMD_RESPONSE_COMPLETE_HINT:
00135 case EEMCMD_TICKLE:
00136 break;
00137
00138 default: break;
00139 }
00140 }
00141
00142 else if (datalength && (fail == 0))
00143 {
00144
00145 #ifdef USB_ETH_HOOK_RX_START
00146 USB_ETH_HOOK_RX_START();
00147 #endif
00148
00149 uint16_t bytes_received = 0;
00150 uint16_t dataleft = datalength;
00151 U8 * buffer = uip_buf;
00152
00153 while(dataleft)
00154 {
00155 *buffer++ = Usb_read_byte();
00156
00157 dataleft--;
00158 bytecounter--;
00159 bytes_received++;
00160
00161
00162 if ((bytecounter == 0) && (dataleft))
00163 {
00164
00165 Usb_ack_receive_out();
00166
00167
00168 while (!Is_usb_receive_out());
00169
00170
00171 bytecounter = Usb_byte_counter_8();
00172
00173
00174 if (bytecounter == 0)
00175 {
00176
00177 break;
00178 }
00179 }
00180 }
00181
00182
00183 Usb_ack_receive_out();
00184
00185 #ifdef USB_ETH_HOOK_RX_END
00186 USB_ETH_HOOK_RX_END();
00187 #endif
00188
00189
00190 datalength -= 4;
00191
00192
00193 if(datalength <= USB_ETH_MTU) {
00194 USB_ETH_HOOK_HANDLE_INBOUND_PACKET(uip_buf,datalength);
00195 } else {
00196 USB_ETH_HOOK_RX_ERROR("Oversized packet");
00197 }
00198 }
00199 }
00200 }
00201
00202
00203
00204
00205
00206 uint8_t eem_send(uint8_t * senddata, uint16_t sendlen, uint8_t led)
00207 {
00208
00209 uint8_t header[2];
00210
00211
00212 sendlen += 4;
00213 header[0] = (sendlen >> 8) & 0x3f;
00214 header[1] = sendlen & 0xff;
00215
00216
00217 sendlen -= 4;
00218
00219
00220 Usb_select_endpoint(TX_EP);
00221
00222
00223
00224 if(usb_endpoint_wait_for_write_enabled()!=0) {
00225 USB_ETH_HOOK_TX_ERROR("Timeout: write enabled");
00226 return 0;
00227 }
00228
00229 #ifdef USB_ETH_HOOK_TX_START
00230 USB_ETH_HOOK_TX_START();
00231 #endif
00232
00233
00234 Usb_write_byte(header[1]);
00235 Usb_write_byte(header[0]);
00236
00237
00238 while(sendlen) {
00239 Usb_write_byte(*senddata);
00240 senddata++;
00241 sendlen--;
00242
00243
00244
00245 if (!Is_usb_write_enabled()) {
00246 Usb_send_in();
00247
00248 if(usb_endpoint_wait_for_write_enabled()!=0) {
00249 USB_ETH_HOOK_TX_ERROR("Timeout: write enabled");
00250 return 0;
00251 }
00252 }
00253
00254 }
00255
00256
00257
00258
00259
00260 uint8_t crc[4] = {0xde, 0xad, 0xbe, 0xef};
00261
00262 sendlen = 4;
00263 uint8_t i = 0;
00264
00265
00266 while(sendlen) {
00267 Usb_write_byte(crc[i]);
00268 i++;
00269 sendlen--;
00270
00271
00272
00273 if (!Is_usb_write_enabled()) {
00274 Usb_send_in();
00275 if(usb_endpoint_wait_for_write_enabled()!=0) {
00276 USB_ETH_HOOK_TX_ERROR("Timeout: write enabled");
00277 return 0;
00278 }
00279 }
00280 }
00281
00282
00283 Usb_send_in();
00284
00285 #ifdef USB_ETH_HOOK_TX_END
00286 USB_ETH_HOOK_TX_END();
00287 #endif
00288
00289
00290 if(usb_endpoint_wait_for_IN_ready()!=0) {
00291 USB_ETH_HOOK_TX_ERROR("Timeout: IN ready");
00292 return 0;
00293 }
00294
00295 return 1;
00296 }