![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Control of mbed using OSC. Based on code from the Make Controller. Right now you can turn the onboard LEDs on/off and toggle 8 digital out pins. More I/O will be done in the future.
lwip/device.cpp@0:439354122597, 2010-03-17 (annotated)
- Committer:
- pehrhovey
- Date:
- Wed Mar 17 03:17:38 2010 +0000
- Revision:
- 0:439354122597
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pehrhovey | 0:439354122597 | 1 | #include "mbed.h" |
pehrhovey | 0:439354122597 | 2 | |
pehrhovey | 0:439354122597 | 3 | using namespace mbed; |
pehrhovey | 0:439354122597 | 4 | |
pehrhovey | 0:439354122597 | 5 | Ethernet *eth; |
pehrhovey | 0:439354122597 | 6 | #ifdef __cplusplus |
pehrhovey | 0:439354122597 | 7 | extern "C" { |
pehrhovey | 0:439354122597 | 8 | #endif |
pehrhovey | 0:439354122597 | 9 | |
pehrhovey | 0:439354122597 | 10 | #include "lwip/opt.h" |
pehrhovey | 0:439354122597 | 11 | |
pehrhovey | 0:439354122597 | 12 | #include "lwip/def.h" |
pehrhovey | 0:439354122597 | 13 | #include "lwip/pbuf.h" |
pehrhovey | 0:439354122597 | 14 | #include "lwip/sys.h" |
pehrhovey | 0:439354122597 | 15 | #include "lwip/stats.h" |
pehrhovey | 0:439354122597 | 16 | #include "netif/etharp.h" |
pehrhovey | 0:439354122597 | 17 | #include "string.h" |
pehrhovey | 0:439354122597 | 18 | |
pehrhovey | 0:439354122597 | 19 | #define IFNAME0 'E' |
pehrhovey | 0:439354122597 | 20 | #define IFNAME1 'X' |
pehrhovey | 0:439354122597 | 21 | |
pehrhovey | 0:439354122597 | 22 | #define min(x,y) (((x)<(y))?(x):(y)) |
pehrhovey | 0:439354122597 | 23 | |
pehrhovey | 0:439354122597 | 24 | struct netif *gnetif; |
pehrhovey | 0:439354122597 | 25 | |
pehrhovey | 0:439354122597 | 26 | static err_t device_output(struct netif *netif, struct pbuf *p) { |
pehrhovey | 0:439354122597 | 27 | #if ETH_PAD_SIZE |
pehrhovey | 0:439354122597 | 28 | pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ |
pehrhovey | 0:439354122597 | 29 | #endif |
pehrhovey | 0:439354122597 | 30 | |
pehrhovey | 0:439354122597 | 31 | do { |
pehrhovey | 0:439354122597 | 32 | eth->write((const char *)p->payload, p->len); |
pehrhovey | 0:439354122597 | 33 | } while((p = p->next)!=NULL); |
pehrhovey | 0:439354122597 | 34 | |
pehrhovey | 0:439354122597 | 35 | eth->send(); |
pehrhovey | 0:439354122597 | 36 | |
pehrhovey | 0:439354122597 | 37 | #if ETH_PAD_SIZE |
pehrhovey | 0:439354122597 | 38 | pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ |
pehrhovey | 0:439354122597 | 39 | #endif |
pehrhovey | 0:439354122597 | 40 | |
pehrhovey | 0:439354122597 | 41 | LINK_STATS_INC(link.xmit); |
pehrhovey | 0:439354122597 | 42 | return ERR_OK; |
pehrhovey | 0:439354122597 | 43 | } |
pehrhovey | 0:439354122597 | 44 | |
pehrhovey | 0:439354122597 | 45 | void device_poll() { |
pehrhovey | 0:439354122597 | 46 | struct eth_hdr *ethhdr; |
pehrhovey | 0:439354122597 | 47 | struct pbuf *frame, *p; |
pehrhovey | 0:439354122597 | 48 | int len, read; |
pehrhovey | 0:439354122597 | 49 | |
pehrhovey | 0:439354122597 | 50 | while((len = eth->receive()) != 0) { |
pehrhovey | 0:439354122597 | 51 | frame = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); |
pehrhovey | 0:439354122597 | 52 | if(frame == NULL) { |
pehrhovey | 0:439354122597 | 53 | return; |
pehrhovey | 0:439354122597 | 54 | } |
pehrhovey | 0:439354122597 | 55 | p = frame; |
pehrhovey | 0:439354122597 | 56 | do { |
pehrhovey | 0:439354122597 | 57 | read = eth->read((char *)p->payload, p->len); |
pehrhovey | 0:439354122597 | 58 | p = p->next; |
pehrhovey | 0:439354122597 | 59 | } while(p != NULL && read != 0); |
pehrhovey | 0:439354122597 | 60 | |
pehrhovey | 0:439354122597 | 61 | #if ETH_PAD_SIZE |
pehrhovey | 0:439354122597 | 62 | pbuf_header(p, ETH_PAD_SIZE); |
pehrhovey | 0:439354122597 | 63 | #endif |
pehrhovey | 0:439354122597 | 64 | |
pehrhovey | 0:439354122597 | 65 | ethhdr = (struct eth_hdr *)(frame->payload); |
pehrhovey | 0:439354122597 | 66 | |
pehrhovey | 0:439354122597 | 67 | switch(htons(ethhdr->type)) { |
pehrhovey | 0:439354122597 | 68 | |
pehrhovey | 0:439354122597 | 69 | case ETHTYPE_IP: |
pehrhovey | 0:439354122597 | 70 | etharp_ip_input(gnetif, frame); |
pehrhovey | 0:439354122597 | 71 | pbuf_header(frame, -((s16_t) sizeof(struct eth_hdr))); |
pehrhovey | 0:439354122597 | 72 | gnetif->input(frame, gnetif); |
pehrhovey | 0:439354122597 | 73 | break; |
pehrhovey | 0:439354122597 | 74 | |
pehrhovey | 0:439354122597 | 75 | case ETHTYPE_ARP: |
pehrhovey | 0:439354122597 | 76 | etharp_arp_input(gnetif, (struct eth_addr *)(gnetif->hwaddr), frame); |
pehrhovey | 0:439354122597 | 77 | break; |
pehrhovey | 0:439354122597 | 78 | |
pehrhovey | 0:439354122597 | 79 | default: |
pehrhovey | 0:439354122597 | 80 | break; |
pehrhovey | 0:439354122597 | 81 | } |
pehrhovey | 0:439354122597 | 82 | pbuf_free(frame); |
pehrhovey | 0:439354122597 | 83 | } |
pehrhovey | 0:439354122597 | 84 | } |
pehrhovey | 0:439354122597 | 85 | |
pehrhovey | 0:439354122597 | 86 | err_t device_init(struct netif *netif) { |
pehrhovey | 0:439354122597 | 87 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
pehrhovey | 0:439354122597 | 88 | |
pehrhovey | 0:439354122597 | 89 | NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 0x2EA); |
pehrhovey | 0:439354122597 | 90 | |
pehrhovey | 0:439354122597 | 91 | /* maximum transfer unit */ |
pehrhovey | 0:439354122597 | 92 | netif->mtu = 0x2EA; |
pehrhovey | 0:439354122597 | 93 | |
pehrhovey | 0:439354122597 | 94 | /* device capabilities */ |
pehrhovey | 0:439354122597 | 95 | /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ |
pehrhovey | 0:439354122597 | 96 | netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; |
pehrhovey | 0:439354122597 | 97 | |
pehrhovey | 0:439354122597 | 98 | netif->state = NULL; |
pehrhovey | 0:439354122597 | 99 | gnetif = netif; |
pehrhovey | 0:439354122597 | 100 | |
pehrhovey | 0:439354122597 | 101 | netif->name[0] = IFNAME0; |
pehrhovey | 0:439354122597 | 102 | netif->name[1] = IFNAME1; |
pehrhovey | 0:439354122597 | 103 | |
pehrhovey | 0:439354122597 | 104 | /* We directly use etharp_output() here to save a function call. |
pehrhovey | 0:439354122597 | 105 | * You can instead declare your own function an call etharp_output() |
pehrhovey | 0:439354122597 | 106 | * from it if you have to do some checks before sending (e.g. if link |
pehrhovey | 0:439354122597 | 107 | * is available...) */ |
pehrhovey | 0:439354122597 | 108 | netif->output = etharp_output; |
pehrhovey | 0:439354122597 | 109 | netif->linkoutput = device_output; |
pehrhovey | 0:439354122597 | 110 | |
pehrhovey | 0:439354122597 | 111 | eth = new Ethernet(); |
pehrhovey | 0:439354122597 | 112 | |
pehrhovey | 0:439354122597 | 113 | return ERR_OK; |
pehrhovey | 0:439354122597 | 114 | } |
pehrhovey | 0:439354122597 | 115 | |
pehrhovey | 0:439354122597 | 116 | void device_address(char *mac) { |
pehrhovey | 0:439354122597 | 117 | eth->address(mac); |
pehrhovey | 0:439354122597 | 118 | } |
pehrhovey | 0:439354122597 | 119 | |
pehrhovey | 0:439354122597 | 120 | #ifdef __cplusplus |
pehrhovey | 0:439354122597 | 121 | }; |
pehrhovey | 0:439354122597 | 122 | #endif |