10 #include "sicslow_ethernet.h"
16 #include <avr/pgmspace.h>
17 #include <util/delay.h>
19 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
21 #define PRINTF_P printf_P
24 static U16 usb_ecm_packet_filter = 0;
26 #define PACKET_TYPE_PROMISCUOUS (1<<0)
27 #define PACKET_TYPE_ALL_MULTICAST (1<<1)
28 #define PACKET_TYPE_DIRECTED (1<<2)
29 #define PACKET_TYPE_BROADCAST (1<<3)
30 #define PACKET_TYPE_MULTICAST (1<<4)
32 #define Usb_write_word(x) Usb_write_byte((x)&0xFF),Usb_write_byte((x>>8)&0xFF)
33 #define Usb_write_long(x) Usb_write_word((x)&0xFFFF),Usb_write_word((x>>16)&0xFFFF)
35 #define Usb_read_word() ((U16)Usb_read_byte()+((U16)Usb_read_byte()<<8))
38 cdc_ecm_set_ethernet_packet_filter(
void) {
39 usb_ecm_packet_filter = Usb_read_word();
43 usb_endpoint_wait_for_read_control_enabled();
45 PRINTF_P(PSTR(
"cdc_ecm: Received SET_ETHERNET_PACKET_FILTER: (0x%04X) "),usb_ecm_packet_filter);
46 if(usb_ecm_packet_filter & PACKET_TYPE_PROMISCUOUS) {
47 PRINTF_P(PSTR(
"PROMISCUOUS "));
48 USB_ETH_HOOK_SET_PROMISCIOUS_MODE(
true);
50 USB_ETH_HOOK_SET_PROMISCIOUS_MODE(
false);
53 if(usb_ecm_packet_filter & PACKET_TYPE_ALL_MULTICAST)
54 PRINTF_P(PSTR(
"ALL_MULTICAST "));
55 if(usb_ecm_packet_filter & PACKET_TYPE_DIRECTED)
56 PRINTF_P(PSTR(
"DIRECTED "));
57 if(usb_ecm_packet_filter & PACKET_TYPE_BROADCAST)
58 PRINTF_P(PSTR(
"BROADCAST "));
59 if(usb_ecm_packet_filter & PACKET_TYPE_MULTICAST)
60 PRINTF_P(PSTR(
"MULTICAST "));
66 #define CDC_NOTIFY_NETWORK_CONNECTION (0x00)
67 #define CDC_NOTIFY_CONNECTION_SPEED_CHANGE (0x2A)
70 cdc_ecm_notify_network_connection(uint8_t value) {
71 #if CDC_ECM_USES_INTERRUPT_ENDPOINT
79 if(usb_endpoint_wait_for_IN_ready()!=0) {
90 Usb_write_word(ECM_INTERFACE0_NB);
91 Usb_write_word(0x0000);
94 PRINTF_P(PSTR(
"cdc_ecm: CDC_NOTIFY_NETWORK_CONNECTION %d\n"),value);
98 #define CDC_ECM_DATA_ENDPOINT_SIZE 64
100 void cdc_ecm_configure_endpoints() {
101 #if CDC_ECM_USES_INTERRUPT_ENDPOINT
102 usb_configure_endpoint(INT_EP, \
110 usb_configure_endpoint(TX_EP, \
117 usb_configure_endpoint(RX_EP, \
123 #if CDC_ECM_USES_INTERRUPT_ENDPOINT
128 usb_eth_is_active = 1;
133 cdc_ecm_notify_connection_speed_change(uint32_t upstream,uint32_t downstream) {
134 #if CDC_ECM_USES_INTERRUPT_ENDPOINT
140 if(usb_endpoint_wait_for_IN_ready()!=0)
147 Usb_write_word(0x0000);
148 Usb_write_word(ECM_INTERFACE0_NB);
149 Usb_write_word(0x0008);
153 if(usb_endpoint_wait_for_write_enabled()!=0)
156 Usb_write_long(upstream);
157 Usb_write_long(downstream);
160 PRINTF_P(PSTR(
"cdc_ecm: CDC_NOTIFY_CONNECTION_SPEED_CHANGE UP:%d DOWN:%d\n"),upstream,downstream);
164 void cdc_ecm_set_active(uint8_t value) {
165 if(value!=usb_eth_is_active) {
168 usb_eth_is_active = value;
169 cdc_ecm_notify_network_connection(value);
171 cdc_ecm_notify_connection_speed_change(250000,250000);
173 cdc_ecm_notify_connection_speed_change(0,0);
179 cdc_ecm_process(
void) {
180 static uint8_t doInit = 1;
189 #ifdef USB_ETH_HOOK_INIT
192 cdc_ecm_notify_network_connection(1);
193 cdc_ecm_notify_connection_speed_change(250000,250000);
195 if(usb_ecm_packet_filter & PACKET_TYPE_PROMISCUOUS) {
197 rf230_set_promiscuous_mode(
true);
207 if(!usb_eth_is_active) {
219 uint16_t bytecounter;
220 uint16_t bytes_received = 0;
221 U8 * buffer = uip_buf;
223 if(!usb_eth_ready_for_next_packet()) {
229 #ifdef USB_ETH_HOOK_RX_START
230 USB_ETH_HOOK_RX_START();
234 while((bytes_received<USB_ETH_MTU) && (bytecounter--)) {
238 bytes_received+=bytecounter+1;
244 if(usb_endpoint_wait_for_read_enabled()!=0) {
245 USB_ETH_HOOK_RX_ERROR(
"Timeout: read enabled");
250 while((bytes_received<USB_ETH_MTU) && (bytecounter--)) {
254 bytes_received+=bytecounter+1;
261 #ifdef USB_ETH_HOOK_RX_END
262 USB_ETH_HOOK_RX_END();
266 if(bytes_received<=USB_ETH_MTU) {
268 USB_ETH_HOOK_HANDLE_INBOUND_PACKET(uip_buf,bytes_received);
270 USB_ETH_HOOK_RX_ERROR(
"Oversized packet");
278 ecm_send(uint8_t * senddata, uint16_t sendlen, uint8_t led) {
279 U8 byte_in_packet = 0;
284 if(usb_endpoint_wait_for_write_enabled()!=0) {
285 USB_ETH_HOOK_TX_ERROR(
"Timeout: write enabled");
289 #ifdef USB_ETH_HOOK_TX_START
290 USB_ETH_HOOK_TX_START();
305 if(usb_endpoint_wait_for_write_enabled()!=0) {
306 USB_ETH_HOOK_TX_ERROR(
"Timeout: write enabled");
317 #ifdef USB_ETH_HOOK_TX_END
318 USB_ETH_HOOK_TX_END();
322 if(usb_endpoint_wait_for_IN_ready()!=0) {
323 USB_ETH_HOOK_TX_ERROR(
"Timeout: IN ready");