CDC/ECM driver for mbed, based on USBDevice by mbed-official. Uses PicoTCP to access Ethernet USB device. License: GPLv2
Fork of USB_Ethernet by
pico_dev_mbed.cpp
00001 /********************************************************************* 00002 PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved. 00003 See LICENSE and COPYING for usage. 00004 Do not redistribute without a written permission by the Copyright 00005 holders. 00006 00007 Authors: Toon Peters, Maxime Vincent 00008 *********************************************************************/ 00009 #include "mbed.h" 00010 extern "C" { 00011 #include "pico_device.h" 00012 #include "pico_dev_mbed.h" 00013 #include "pico_stack.h" 00014 #include "ethernet_api.h" 00015 } 00016 00017 struct pico_device_mbed { 00018 struct pico_device dev; 00019 int bytes_left_in_frame; 00020 }; 00021 00022 #define ETH_MTU 1514 00023 uint8_t buf[ETH_MTU]; 00024 00025 Serial pc(p9, p10, "Serial port"); // tx, rx 00026 00027 extern "C" { 00028 00029 static int pico_mbed_send(struct pico_device *dev, void *buf, int len) 00030 { 00031 int ret, sent; 00032 struct pico_device_mbed *mb = (struct pico_device_mbed *) dev; 00033 00034 if (len > ETH_MTU) 00035 return -1; 00036 00037 /* Write buf content to dev and return amount written */ 00038 ret = ethernet_write((const char *)buf, len); 00039 sent = ethernet_send(); 00040 00041 pc.printf("ETH> sent %d bytes\r\n",ret); 00042 if (len != ret || sent != ret) 00043 return -1; 00044 else 00045 return ret; 00046 } 00047 00048 static int pico_mbed_poll(struct pico_device *dev, int loop_score) 00049 { 00050 int len; 00051 struct pico_device_mbed *mb = (struct pico_device_mbed *) dev; 00052 00053 while(loop_score > 0) 00054 { 00055 /* check for new frame(s) */ 00056 len = (int) ethernet_receive(); 00057 00058 /* return if no frame has arrived */ 00059 if (!len) 00060 return loop_score; 00061 00062 /* read and process frame */ 00063 len = ethernet_read((char*)buf, ETH_MTU); 00064 pc.printf("ETH> recv %d bytes: %x:%x\r\n", len, buf[0],buf[1]); 00065 pico_stack_recv(dev, buf, len); 00066 loop_score--; 00067 } 00068 return loop_score; 00069 } 00070 00071 /* Public interface: create/destroy. */ 00072 void pico_mbed_destroy(struct pico_device *dev) 00073 { 00074 ethernet_free(); 00075 pico_device_destroy(dev); 00076 } 00077 00078 struct pico_device *pico_mbed_create(char *name) 00079 { 00080 std::uint8_t mac[PICO_SIZE_ETH]; 00081 struct pico_device_mbed *mb = (struct pico_device_mbed*) pico_zalloc(sizeof(struct pico_device_mbed)); 00082 00083 if (!mb) 00084 return NULL; 00085 00086 ethernet_address((char *)mac); 00087 pc.printf("ETH> Set MAC address to: %x:%x:%x:%x:%x:%x\r\n", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); 00088 00089 if(0 != pico_device_init((struct pico_device *)mb, name, mac)) { 00090 pc.printf ("ETH> Loop init failed.\n"); 00091 //pico_loop_destroy(mb); 00092 return NULL; 00093 } 00094 00095 mb->dev.send = pico_mbed_send; 00096 mb->dev.poll = pico_mbed_poll; 00097 mb->dev.destroy = pico_mbed_destroy; 00098 mb->bytes_left_in_frame = 0; 00099 00100 if(0 != ethernet_init()) { 00101 pc.printf("ETH> Failed to initialize hardware.\r\n"); 00102 pico_device_destroy((struct pico_device *)mb); 00103 return NULL; 00104 } 00105 00106 // future work: make the mac address configurable 00107 00108 pc.printf("ETH> Device %s created.\r\n", mb->dev.name); 00109 00110 return (struct pico_device *)mb; 00111 } 00112 00113 void pico_mbed_get_address(char *mac) 00114 { 00115 ethernet_address(mac); 00116 } 00117 00118 }
Generated on Wed Jul 13 2022 02:20:45 by 1.7.2