Free (GPLv2) TCP/IP stack developed by TASS Belgium

Fork of PicoTCP by Daniele Lacamera

modules/pico_dev_mbed_usb.cpp

Committer:
daniele
Date:
2013-08-03
Revision:
51:18637a3d071f
Parent:
50:c3b337c38feb

File content as of revision 51:18637a3d071f:

/*********************************************************************
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
Do not redistribute without a written permission by the Copyright
holders.

Authors: Daniele Lacamera, Julien Duraj
*********************************************************************/
#include "mbed.h"
#include "USBCDC_ECM.h"
#include "ethernet_api.h"
#include "pico_dev_mbed_usb.h"

extern "C" {
#include "pico_device.h"
#include "pico_stack.h"
}

struct pico_device_mbed_usb {
  struct pico_device dev;
  int bytes_left_in_frame;
  USBCDC_ECM *ecm;
};

#define ETH_MTU 1514
static uint8_t buf[ETH_MTU];

Serial __pc(p9, p10, "Serial port"); // tx, rx

extern "C" {

static int pico_mbed_usb_send(struct pico_device *dev, void *buffer, int len)
{
  int ret, sent;
  struct pico_device_mbed_usb *mb = (struct pico_device_mbed_usb *) dev;

  if (len > ETH_MTU)
    return -1;
  ret = mb->ecm->send((uint8_t *)buffer, len);

  if (!ret)
    return -1;
  return len;
}

static int pico_mbed_usb_poll(struct pico_device *dev, int loop_score)
{
  int size;
  struct pico_device_mbed_usb *mb = (struct pico_device_mbed_usb *) dev;
  
  while(loop_score > 0)
  {
    bool ret;
    /* check for new frame(s) */
    ret = mb->ecm->readEP_NB(buf, (uint32_t *)&size);
    
    /* return if no frame has arrived */
    if (!ret)
      return loop_score;
  
    /* read and process frame */
    printf("ETH> recv %d bytes: %x:%x\r\n", size, buf[0],buf[1]);
    pico_stack_recv(dev, buf, size);
    loop_score--;
  }
  return loop_score;
}

/* Public interface: create/destroy. */
void pico_mbed_usb_destroy(struct pico_device *dev)
{
  pico_device_destroy(dev);
}

struct pico_device *pico_mbed_usb_create(char *name, USBCDC_ECM *ecm)
{
  std::uint8_t mac[PICO_SIZE_ETH];
  struct pico_device_mbed_usb *mb = (struct pico_device_mbed_usb*) pico_zalloc(sizeof(struct pico_device_mbed_usb));

  if (!mb)
    return NULL;

  if(0 != pico_device_init((struct pico_device *)mb, name, mac)) {
    __pc.printf ("ETH> Loop init failed.\n");
    //pico_loop_destroy(mb);
    return NULL;
  }
  mb->ecm = ecm;
  mb->dev.send = pico_mbed_usb_send;
  mb->dev.poll = pico_mbed_usb_poll;
  mb->dev.destroy = pico_mbed_usb_destroy;
  mb->bytes_left_in_frame = 0;

  if(0 != ethernet_init()) {
    __pc.printf("ETH> Failed to initialize hardware.\r\n");
    pico_device_destroy((struct pico_device *)mb);
    return NULL;
  }

  __pc.printf("ETH> Device %s created.\r\n", mb->dev.name);

  return (struct pico_device *)mb;
}

void pico_mbed_usb_get_address(char *mac)
{
  /* TODO */
}

}