黑金开发板ENC28J60驱动

  • a0_259231
    了解作者
  • 6.7KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-15 01:36
    上传日期
nios开发下利用ENC28J60作为网络接口的驱动程序
7_lan.zip
  • inc
  • enc28j60.h
    8.5KB
  • sopc.h
    2.9KB
  • main
  • main.c
    1021B
  • driver
  • enc28j60.c
    12.9KB
内容介绍
/* * ===================================================================================== * * Filename: enc28j60.c * * Description: * * Version: 1.0.0 * Created: 2010.4.16 * Revision: none * Compiler: Nios II 9.0 IDE * * Author: 马瑞 (AVIC) * Email: avic633@gmail.com * QQ: 984597569 * URL: http://kingst.cnblogs.com * * ===================================================================================== */ /*----------------------------------------------------------------------------- * Include *-----------------------------------------------------------------------------*/ #include "../inc/enc28j60.h" #include "../inc/sopc.h" #include <stdio.h> /*----------------------------------------------------------------------------- * Function Prototype *-----------------------------------------------------------------------------*/ static unsigned char enc28j60_read_control_register(unsigned char address); static void enc28j60_initialize(void); static void enc28j60_packet_send(unsigned short len,unsigned char * packet); static unsigned int enc28j60_packet_receive(unsigned short maxlen,unsigned char * packet); /*----------------------------------------------------------------------------- * Variable *-----------------------------------------------------------------------------*/ ENC28J60 enc28j60={ .read_control_register = enc28j60_read_control_register, .initialize = enc28j60_initialize, .packet_send = enc28j60_packet_send, .packet_receive = enc28j60_packet_receive }; static unsigned char enc28j60_bank = 1; static unsigned short next_packet_pointer; /* * === FUNCTION ====================================================================== * Name: set_cs * Description: * ===================================================================================== */ static void set_cs(unsigned char level) { if(level) LAN_CS->DATA = 1; else LAN_CS->DATA = 0; } /* * === FUNCTION ====================================================================== * Name: enc28j60_write_operation * Description: * ===================================================================================== */ static void enc28j60_write_operation(unsigned char op, unsigned char address, unsigned char data) { set_cs(0); LAN->TXDATA = (op | (address & 0x1F)); // write command while(!(LAN->STATUS.BITS.TMT)); LAN->TXDATA = data; // write data while(!(LAN->STATUS.BITS.TMT)); set_cs(1); } /* * === FUNCTION ====================================================================== * Name: enc28j60_read_operation * Description: * ===================================================================================== */ static unsigned char enc28j60_read_operation(unsigned char op,unsigned char address) { unsigned char data; set_cs(0); LAN->TXDATA = op|(address&0x1f); while(!(LAN->STATUS.BITS.TMT)); LAN->TXDATA = 0x00; //0x00 is random number ,to enable clock while(!(LAN->STATUS.BITS.TMT)); if(address&0x80){ LAN->TXDATA = 0x00; while(!(LAN->STATUS.BITS.TMT)); //The first byte that MAC and MII registers read is invalid,so they need to read twice } data = LAN->RXDATA; set_cs(1); return data; } /* * === FUNCTION ====================================================================== * Name: enc28j60_write_operation * Description: * ===================================================================================== */ static void enc28j60_set_bank(unsigned char address) { if((address & BANK_MASK) != enc28j60_bank){ enc28j60_write_operation(ENC28J60_BIT_FIELD_CLR,ECON1,(ECON1_BSEL1|ECON1_BSEL0)); //clear BSEL1,BSEL0 enc28j60_write_operation(ENC28J60_BIT_FIELD_SET,ECON1,(address&BANK_MASK)>>5); //set bit 0x00:bank0;0x01:bank1;0x10:bank2;0x11:bank3 enc28j60_bank=(address&BANK_MASK); } } /* * === FUNCTION ====================================================================== * Name: enc28j60_read_buffer * Description: * ===================================================================================== */ static void enc28j60_read_buffer(unsigned short len,unsigned char * data) { set_cs(0); LAN->TXDATA = ENC28J60_READ_BUF_MEM; while(!(LAN->STATUS.BITS.TMT)); while(len--){ LAN->TXDATA = 0x00; while(!(LAN->STATUS.BITS.TMT)); while(!(LAN->STATUS.BITS.RRDY)); *data++=LAN->RXDATA; } set_cs(1); } /* * === FUNCTION ====================================================================== * Name: enc28j60_write_buffer * Description: * ===================================================================================== */ static void enc28j60_write_buffer(unsigned short len,unsigned char * data) { set_cs(0); LAN->TXDATA = ENC28J60_WRITE_BUF_MEM; while(!(LAN->STATUS.BITS.TMT)); while(len--){ LAN->TXDATA = *data++; while(!(LAN->STATUS.BITS.TMT)); } set_cs(1); } /* * === FUNCTION ====================================================================== * Name: enc28j60_read_control_register * Description: * ===================================================================================== */ static unsigned char enc28j60_read_control_register(unsigned char address) { enc28j60_set_bank(address); return enc28j60_read_operation(ENC28J60_READ_CTRL_REG,address); } /* * === FUNCTION ====================================================================== * Name: enc28j60_write_control_register * Description: * ===================================================================================== */ static void enc28j60_write_control_register(unsigned char address,unsigned char data) { enc28j60_set_bank(address); enc28j60_write_operation(ENC28J60_WRITE_CTRL_REG,address,data); } /* * === FUNCTION ====================================================================== * Name: enc28j60_phy_write * Description: * ===================================================================================== */ static void enc28j60_phy_write(unsigned char address,unsigned short data) { enc28j60_write_control_register(MIREGADR,address);//MIREGADR:0x14 enc28j60_write_control_register(MIWRL,data); enc28j60_write_control_register(MIWRH,data>>8); //wait until the PHY write completes while(enc28j60_read_control_register(MISTAT)&MISTAT_BUSY);//MISTAT:0xa0 } /* * === FUNCTION ====================================================================== * Name: enc28j60_initialize * Description: * ===================================================================================== */ static void enc28j60_initialize(void) { unsigned int a; unsigned int timeout; enc28j60_write_operation(ENC28J60_SOFT_RESET, 0, ENC28J60_SOFT_RESET); do{ timeout = 0; // perform system reset enc28j60_write_operation(ENC28J60_SOFT_RESET, 0, ENC28J60_SOFT_RESET); // check CLKRDY bit to see if reset is complete for(a=0;a<100000;a++); while(!(enc28j60_read_control_register(ESTAT) & ESTAT_CLKRDY)){ timeout++; if (timeout > 100000) break; } }while(timeout > 100000); next_packet_pointer=RXSTART_INIT; //set receive buffer start address enc28j60_write_control_register(ERXSTL,RXSTART_INIT&0xff); enc28j60_write_control_register(ERXSTH,RXSTART_INIT>>8); //set receive pointer address enc28j60_write_control_register(ERXRDPTL,RXSTART_INIT&0xff); enc28j60_write_control_register(ERXRDPTH,RXSTART_INIT>>8); //set receive buffer end //ERXND defaults to 0x1FF
评论
    相关推荐
    • ios
      适用于iOS的Currency Converter应用程序
    • ReplayKit iOS
      iOS视频录制 绝对好东西
    • ios HotelApp
      ios 完整的HotelApp 欢迎一起学习和探讨
    • iOS开发
      iOS开发
    • iOS 播放器
      iOS 播放器
    • ios Drawing
      iOS’s drawing routines are primarily sourced from the UIKit and QuartzCore frameworks.
    • ios Apprentice
      ios app 书籍 ios Apprentice v5.0 一到四章节pdf code
    • iOS内存
      iOS内存 自定义按钮 UIcolorConvertor->将十六进制转换为UIColor
    • ios
      ios
    • IOS programming
      最新版的 ios12 programming;这个是我 最近找的;希望对大家有用