zyh_126

stm32扩展网口芯片CS8900a移植lwip

0
阅读(4005)
上次在博客中贴出了stm32扩展网口芯片CS8900a的硬件电路,通过这段时间的学习,修改了stm32f107的lwip的官网程序,移植到stm32f103ZET6上了。有关信号量,消息机制的函数直接用的stm32f107官网lwip移植工程中的函数,它和stm32f103ZET6兼容,只是修改了下与硬件有关的底层函数。具体的细节如下
static void
low_level_init(struct netif *netif)
{
  SYS_ARCH_DECL_PROTECT(sr);
    
  /* set MAC hardware address length */
  netif->hwaddr_len = ETHARP_HWADDR_LEN;

  /* set MAC hardware address */
  netif->hwaddr[0] =  MACaddr[0];
  netif->hwaddr[1] =  MACaddr[1];
  netif->hwaddr[2] =  MACaddr[2];
  netif->hwaddr[3] =  MACaddr[3];
  netif->hwaddr[4] =  MACaddr[4];
  netif->hwaddr[5] =  MACaddr[5];

  /* maximum transfer unit */
  netif->mtu = 1500;

  /* device capabilities */
  /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;

  SYS_ARCH_PROTECT(sr);
  CS8900_Reset(0);
  //BSP_IntVectSet(BSP_INT_ID_ETH, LwIP_Pkt_Handle);
  //BSP_IntEn(BSP_INT_ID_ETH);
  


  SYS_ARCH_UNPROTECT(sr);
  

}

/**
 * This function should do the actual transmission of the packet. The packet is
 * contained in the pbuf that is passed to the function. This pbuf
 * might be chained.
 *
 * @param netif the lwip network interface structure for this ethernetif
 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
 * @return ERR_OK if the packet could be sent
 *         an err_t value if the packet couldn't be sent
 *
 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
 *       strange results. You might consider waiting for space in the DMA queue
 *       to become availale since the stack doesn't retry to send a packet
 *       dropped because of memory failure (except for the TCP timers).
 */

static err_t
low_level_output(struct netif *netif, struct pbuf *p)
{
  struct pbuf *q;
  u16 *ptr;
  u16 tries,i;
  SYS_ARCH_DECL_PROTECT(sr);
  /* Interrupts are disabled through this whole thing to support multi-threading
	   transmit calls. Also this function might be called from an ISR. */
  SYS_ARCH_PROTECT(sr);
  while ( ((get_reg (PP_BusST)& READY_FOR_TX_NOW) == 0)&&tries++>500); 
  CS8900_TxCMD = TX_START_ALL_BYTES;
  CS8900_TxLEN = q->len;
  if((get_reg (PP_BusST)& READY_FOR_TX_NOW) != 0)
{
  for(q = p; q != NULL; q = q->next) 
  {
    ptr=q->payload;
    for(i=0;i<q->len;i+=2)
    {
       CS8900_RTDATA = *ptr++;
    }

  }
}

  SYS_ARCH_UNPROTECT(sr);

  return ERR_OK;
}

/**
 * Should allocate a pbuf and transfer the bytes of the incoming
 * packet from the interface into the pbuf.
 *
 * @param netif the lwip network interface structure for this ethernetif
 * @return a pbuf filled with the received packet (including MAC header)
 *         NULL on memory error
 */
static struct pbuf *
low_level_input(struct netif *netif)
{
  struct pbuf *p, *q;
  u16_t len,status,i;
  u16 *ptr;
  
  p = NULL;
  status = CS8900_RTDATA;	
  len = CS8900_RTDATA;		/* len */
  
  /* We allocate a pbuf chain of pbufs from the pool. */
  p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);

  if (p != NULL)
  {
    for (q = p; q != NULL; q = q->next)
    {
      ptr=q->payload;
      for(i=0;i<(len+1)/2;i++)
        *ptr++=CS8900_RTDATA;
    }    
  }

  return p;
}