|
| 1 | +// dpgeorge: this file taken from w5500/w5500.c and adapted to W5200 |
| 2 | + |
| 3 | +//***************************************************************************** |
| 4 | +// |
| 5 | +//! \file w5500.c |
| 6 | +//! \brief W5500 HAL Interface. |
| 7 | +//! \version 1.0.1 |
| 8 | +//! \date 2013/10/21 |
| 9 | +//! \par Revision history |
| 10 | +//! <2014/05/01> V1.0.2 |
| 11 | +//! 1. Implicit type casting -> Explicit type casting. Refer to M20140501 |
| 12 | +//! Fixed the problem on porting into under 32bit MCU |
| 13 | +//! Issued by Mathias ClauBen, wizwiki forum ID Think01 and bobh |
| 14 | +//! Thank for your interesting and serious advices. |
| 15 | +//! <2013/10/21> 1st Release |
| 16 | +//! <2013/12/20> V1.0.1 |
| 17 | +//! 1. Remove warning |
| 18 | +//! 2. WIZCHIP_READ_BUF WIZCHIP_WRITE_BUF in case _WIZCHIP_IO_MODE_SPI_FDM_ |
| 19 | +//! for loop optimized(removed). refer to M20131220 |
| 20 | +//! \author MidnightCow |
| 21 | +//! \copyright |
| 22 | +//! |
| 23 | +//! Copyright (c) 2013, WIZnet Co., LTD. |
| 24 | +//! All rights reserved. |
| 25 | +//! |
| 26 | +//! Redistribution and use in source and binary forms, with or without |
| 27 | +//! modification, are permitted provided that the following conditions |
| 28 | +//! are met: |
| 29 | +//! |
| 30 | +//! * Redistributions of source code must retain the above copyright |
| 31 | +//! notice, this list of conditions and the following disclaimer. |
| 32 | +//! * Redistributions in binary form must reproduce the above copyright |
| 33 | +//! notice, this list of conditions and the following disclaimer in the |
| 34 | +//! documentation and/or other materials provided with the distribution. |
| 35 | +//! * Neither the name of the <ORGANIZATION> nor the names of its |
| 36 | +//! contributors may be used to endorse or promote products derived |
| 37 | +//! from this software without specific prior written permission. |
| 38 | +//! |
| 39 | +//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 40 | +//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 41 | +//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 42 | +//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 43 | +//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 44 | +//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 45 | +//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 46 | +//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 47 | +//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 48 | +//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 49 | +//! THE POSSIBILITY OF SUCH DAMAGE. |
| 50 | +// |
| 51 | +//***************************************************************************** |
| 52 | + |
| 53 | +#include "w5200.h" |
| 54 | + |
| 55 | +#define SMASK (0x7ff) /* tx buffer mask */ |
| 56 | +#define RMASK (0x7ff) /* rx buffer mask */ |
| 57 | +#define SSIZE (2048) /* max tx buffer size */ |
| 58 | +#define RSIZE (2048) /* max rx buffer size */ |
| 59 | + |
| 60 | +#define TXBUF_BASE (0x8000) |
| 61 | +#define RXBUF_BASE (0xc000) |
| 62 | +#define SBASE(sn) (TXBUF_BASE + SSIZE * (sn)) /* tx buffer base for socket sn */ |
| 63 | +#define RBASE(sn) (RXBUF_BASE + RSIZE * (sn)) /* rx buffer base for socket sn */ |
| 64 | + |
| 65 | +uint8_t WIZCHIP_READ(uint32_t AddrSel) { |
| 66 | + WIZCHIP_CRITICAL_ENTER(); |
| 67 | + WIZCHIP.CS._select(); |
| 68 | + |
| 69 | + uint8_t spi_data[4] = { |
| 70 | + AddrSel >> 8, |
| 71 | + AddrSel, |
| 72 | + 0x00, |
| 73 | + 0x01, |
| 74 | + }; |
| 75 | + WIZCHIP.IF.SPI._write_bytes(spi_data, 4); |
| 76 | + uint8_t ret; |
| 77 | + WIZCHIP.IF.SPI._read_bytes(&ret, 1); |
| 78 | + |
| 79 | + WIZCHIP.CS._deselect(); |
| 80 | + WIZCHIP_CRITICAL_EXIT(); |
| 81 | + |
| 82 | + return ret; |
| 83 | +} |
| 84 | + |
| 85 | +void WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb) { |
| 86 | + WIZCHIP_CRITICAL_ENTER(); |
| 87 | + WIZCHIP.CS._select(); |
| 88 | + |
| 89 | + uint8_t spi_data[5] = { |
| 90 | + AddrSel >> 8, |
| 91 | + AddrSel, |
| 92 | + 0x80, |
| 93 | + 0x01, |
| 94 | + wb, |
| 95 | + }; |
| 96 | + WIZCHIP.IF.SPI._write_bytes(spi_data, 5); |
| 97 | + |
| 98 | + WIZCHIP.CS._deselect(); |
| 99 | + WIZCHIP_CRITICAL_EXIT(); |
| 100 | +} |
| 101 | + |
| 102 | +void WIZCHIP_READ_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len) { |
| 103 | + WIZCHIP_CRITICAL_ENTER(); |
| 104 | + WIZCHIP.CS._select(); |
| 105 | + |
| 106 | + uint8_t spi_data[4] = { |
| 107 | + AddrSel >> 8, |
| 108 | + AddrSel, |
| 109 | + 0x00 | ((len >> 8) & 0x7f), |
| 110 | + len & 0xff, |
| 111 | + }; |
| 112 | + WIZCHIP.IF.SPI._write_bytes(spi_data, 4); |
| 113 | + WIZCHIP.IF.SPI._read_bytes(pBuf, len); |
| 114 | + |
| 115 | + WIZCHIP.CS._deselect(); |
| 116 | + WIZCHIP_CRITICAL_EXIT(); |
| 117 | +} |
| 118 | + |
| 119 | +void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len) { |
| 120 | + WIZCHIP_CRITICAL_ENTER(); |
| 121 | + WIZCHIP.CS._select(); |
| 122 | + |
| 123 | + uint8_t spi_data[4] = { |
| 124 | + AddrSel >> 8, |
| 125 | + AddrSel, |
| 126 | + 0x80 | ((len >> 8) & 0x7f), |
| 127 | + len & 0xff, |
| 128 | + }; |
| 129 | + WIZCHIP.IF.SPI._write_bytes(spi_data, 4); |
| 130 | + WIZCHIP.IF.SPI._write_bytes(pBuf, len); |
| 131 | + |
| 132 | + WIZCHIP.CS._deselect(); |
| 133 | + WIZCHIP_CRITICAL_EXIT(); |
| 134 | +} |
| 135 | + |
| 136 | +uint16_t getSn_TX_FSR(uint8_t sn) { |
| 137 | + uint16_t val = 0, val1 = 0; |
| 138 | + do { |
| 139 | + val1 = (WIZCHIP_READ(Sn_TX_FSR(sn)) << 8) | WIZCHIP_READ(Sn_TX_FSR(sn) + 1); |
| 140 | + if (val1 != 0) { |
| 141 | + val = (WIZCHIP_READ(Sn_TX_FSR(sn)) << 8) | WIZCHIP_READ(Sn_TX_FSR(sn) + 1); |
| 142 | + } |
| 143 | + } while (val != val1); |
| 144 | + return val; |
| 145 | +} |
| 146 | + |
| 147 | +uint16_t getSn_RX_RSR(uint8_t sn) { |
| 148 | + uint16_t val = 0, val1 = 0; |
| 149 | + do { |
| 150 | + val1 = (WIZCHIP_READ(Sn_RX_RSR(sn)) << 8) | WIZCHIP_READ(Sn_RX_RSR(sn) + 1); |
| 151 | + if (val1 != 0) { |
| 152 | + val = (WIZCHIP_READ(Sn_RX_RSR(sn)) << 8) | WIZCHIP_READ(Sn_RX_RSR(sn) + 1); |
| 153 | + } |
| 154 | + } while (val != val1); |
| 155 | + return val; |
| 156 | +} |
| 157 | + |
| 158 | +void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len) { |
| 159 | + if (len == 0) { |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + uint16_t ptr = getSn_TX_WR(sn); |
| 164 | + uint16_t offset = ptr & SMASK; |
| 165 | + uint32_t addr = offset + SBASE(sn); |
| 166 | + |
| 167 | + if (offset + len > SSIZE) { |
| 168 | + // implement wrap-around circular buffer |
| 169 | + uint16_t size = SSIZE - offset; |
| 170 | + WIZCHIP_WRITE_BUF(addr, wizdata, size); |
| 171 | + WIZCHIP_WRITE_BUF(SBASE(sn), wizdata + size, len - size); |
| 172 | + } else { |
| 173 | + WIZCHIP_WRITE_BUF(addr, wizdata, len); |
| 174 | + } |
| 175 | + |
| 176 | + ptr += len; |
| 177 | + setSn_TX_WR(sn, ptr); |
| 178 | +} |
| 179 | + |
| 180 | +void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len) { |
| 181 | + if (len == 0) { |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + uint16_t ptr = getSn_RX_RD(sn); |
| 186 | + uint16_t offset = ptr & RMASK; |
| 187 | + uint16_t addr = RBASE(sn) + offset; |
| 188 | + |
| 189 | + if (offset + len > RSIZE) { |
| 190 | + // implement wrap-around circular buffer |
| 191 | + uint16_t size = RSIZE - offset; |
| 192 | + WIZCHIP_READ_BUF(addr, wizdata, size); |
| 193 | + WIZCHIP_READ_BUF(RBASE(sn), wizdata + size, len - size); |
| 194 | + } else { |
| 195 | + WIZCHIP_READ_BUF(addr, wizdata, len); |
| 196 | + } |
| 197 | + |
| 198 | + ptr += len; |
| 199 | + setSn_RX_RD(sn, ptr); |
| 200 | +} |
| 201 | + |
| 202 | +void wiz_recv_ignore(uint8_t sn, uint16_t len) { |
| 203 | + uint16_t ptr = getSn_RX_RD(sn); |
| 204 | + ptr += len; |
| 205 | + setSn_RX_RD(sn, ptr); |
| 206 | +} |
0 commit comments