TriOS/lib/driver_enc28j60.spin

1 line
28 KiB
Plaintext
Raw Normal View History

{{ ENC28J60 Ethernet MAC / PHY Driver ---------------------------------- Copyright (c) 2006-2009 Harrison Pham <harrison@harrisonpham.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. The latest version of this software can be obtained from http://hdpham.com/PropTCP and http://obex.parallax.com/ Constant Names / Code Logic based on code from Microchip Technology, Inc.'s enc28j60.c / enc28j60.h source files}}CON version = 6 ' major version release = 0 ' minor versionCON' ***************************************' ** ENC28J60 SRAM Defines **' *************************************** ' ENC28J60 Frequency enc_freq = 25_000_000 ' ENC28J60 SRAM Usage Constants MAXFRAME = 1518 ' 6 (src addr) + 6 (dst addr) + 2 (type) + 1500 (data) + 4 (FCS CRC) = 1518 bytes TX_BUFFER_SIZE = 1518 TXSTART = 8192 - (TX_BUFFER_SIZE + 8) TXEND = TXSTART + (TX_BUFFER_SIZE + 8) RXSTART = $0000 RXSTOP = (TXSTART - 2) | $0001 ' must be odd (B5 Errata) RXSIZE = (RXSTOP - RXSTART + 1)DAT' ***************************************' ** MAC Address Vars / Defaults **' *************************************** ' ** This is the default MAC address used by this driver. The parent object ' can override this by passing a pointer to a new MAC address in the public ' start() method. It is recommend that this is done to provide a level of ' abstraction and makes tcp stack design easier. ' ** This is the ethernet MAC address, it is critical that you change this ' if you have more than one device using this code on a local network. ' ** If you plan on commercial deployment, you must purchase MAC address ' groups from IEEE or some other standards organization. eth_mac byte $02, $00, $00, $00, $00, $01' ***************************************' ** Global Variables **' *************************************** rxlen word 0 tx_end word 0 packetheader byte 0[6] 'packet byte 0[MAXFRAME]PUB start(_cs, _sck, _si, _so, xtalout, macptr)'' Starts the driver (uses 1 cog for spi engine) ' Since some people don't have 25mhz crystals, we use the cog counters ' to generate a 25mhz frequency for the ENC28J60 (I love the Propeller) ' Note: This requires a main crystal that is a multiple of 25mhz (5mhz works). spi_start(_cs, _sck, _so, _si, xtalout) ' If a MAC address pointer is provided (addr > -1) then copy it into ' the MAC address array (this kind of wastes space, but simplifies usage). if macptr > -1 bytemove(@eth_mac, macptr, 6) delay_ms(50) init_ENC28J60 ' return the chip silicon version banksel(EREVID) return rd_cntlreg(EREVID)PUB stop'' Stops the driver, frees 1 cog spi_stopPUB rd_macreg(address) : data'' Read MAC Control Register spi_out_cs(cRCR | address) spi_out_cs(0) ' transmit dummy byte data := spi_in ' get actual dataPUB rd_cntlreg(address) : data'' Read ETH Control Register spi_out_cs(cRCR | address) data := spi_inPUB wr_reg(address,