| 1 | /* |
|---|
| 2 | @file w5100.c |
|---|
| 3 | */ |
|---|
| 4 | |
|---|
| 5 | #include <stdio.h> |
|---|
| 6 | #include <string.h> |
|---|
| 7 | |
|---|
| 8 | #include <avr/interrupt.h> |
|---|
| 9 | // #include <avr/io.h> |
|---|
| 10 | |
|---|
| 11 | #include "types.h" |
|---|
| 12 | |
|---|
| 13 | #include "w5100.h" |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | #ifdef __DEF_IINCHIP_PPP__ |
|---|
| 18 | #include "md5.h" |
|---|
| 19 | #include "delay.h" // for wait function |
|---|
| 20 | #endif |
|---|
| 21 | |
|---|
| 22 | static uint8 I_STATUS[MAX_SOCK_NUM]; |
|---|
| 23 | static uint16 SMASK[MAX_SOCK_NUM]; /**< Variable for Tx buffer MASK in each channel */ |
|---|
| 24 | static uint16 RMASK[MAX_SOCK_NUM]; /**< Variable for Rx buffer MASK in each channel */ |
|---|
| 25 | static uint16 SSIZE[MAX_SOCK_NUM]; /**< Max Tx buffer size by each channel */ |
|---|
| 26 | static uint16 RSIZE[MAX_SOCK_NUM]; /**< Max Rx buffer size by each channel */ |
|---|
| 27 | static uint16 SBUFBASEADDRESS[MAX_SOCK_NUM]; /**< Tx buffer base address by each channel */ |
|---|
| 28 | static uint16 RBUFBASEADDRESS[MAX_SOCK_NUM]; /**< Rx buffer base address by each channel */ |
|---|
| 29 | |
|---|
| 30 | uint8 getISR(uint8 s) |
|---|
| 31 | { |
|---|
| 32 | return I_STATUS[s]; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | void putISR(uint8 s, uint8 val) |
|---|
| 36 | { |
|---|
| 37 | I_STATUS[s] = val; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | uint16 getIINCHIP_RxMAX(uint8 s) |
|---|
| 41 | { |
|---|
| 42 | return RSIZE[s]; |
|---|
| 43 | } |
|---|
| 44 | uint16 getIINCHIP_TxMAX(uint8 s) |
|---|
| 45 | { |
|---|
| 46 | return SSIZE[s]; |
|---|
| 47 | } |
|---|
| 48 | uint16 getIINCHIP_RxMASK(uint8 s) |
|---|
| 49 | { |
|---|
| 50 | return RMASK[s]; |
|---|
| 51 | } |
|---|
| 52 | uint16 getIINCHIP_TxMASK(uint8 s) |
|---|
| 53 | { |
|---|
| 54 | return SMASK[s]; |
|---|
| 55 | } |
|---|
| 56 | uint16 getIINCHIP_RxBASE(uint8 s) |
|---|
| 57 | { |
|---|
| 58 | return RBUFBASEADDRESS[s]; |
|---|
| 59 | } |
|---|
| 60 | uint16 getIINCHIP_TxBASE(uint8 s) |
|---|
| 61 | { |
|---|
| 62 | return SBUFBASEADDRESS[s]; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | @brief This function writes the data into W5100 registers. |
|---|
| 67 | */ |
|---|
| 68 | uint8 IINCHIP_WRITE(uint16 addr,uint8 data) |
|---|
| 69 | { |
|---|
| 70 | // DIRECT MODE I/F |
|---|
| 71 | #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__) |
|---|
| 72 | IINCHIP_ISR_DISABLE(); |
|---|
| 73 | *((vuint8*)(addr)) = data; |
|---|
| 74 | IINCHIP_ISR_ENABLE(); |
|---|
| 75 | #elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) /* INDIRECT MODE I/F */ |
|---|
| 76 | IINCHIP_ISR_DISABLE(); |
|---|
| 77 | *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8); |
|---|
| 78 | *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF); |
|---|
| 79 | *((vuint8*)IDM_DR) = data; |
|---|
| 80 | IINCHIP_ISR_ENABLE(); |
|---|
| 81 | #elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__) |
|---|
| 82 | //SPI MODE I/F |
|---|
| 83 | IINCHIP_ISR_DISABLE(); |
|---|
| 84 | #ifndef __ARDUINO__ |
|---|
| 85 | DDRB = 0x07; // MISO=input, etc.=output |
|---|
| 86 | // PB3(MISO), PB2(MOSI), PB1(SCK), PB0(/SS) |
|---|
| 87 | PORTB = 0x01; // CS=1, waiting for SPI start |
|---|
| 88 | SPCR = 0x50; // SPI mode 0, 4MHz |
|---|
| 89 | SPSR = 0x01; // SPI2X=0 |
|---|
| 90 | |
|---|
| 91 | PORTB = 0x00; // CS=0, SPI start |
|---|
| 92 | #else |
|---|
| 93 | PORTB = PORTB & ~CS_PIN; // CS=0, SPI start |
|---|
| 94 | #endif // ifndef __ARDUINO__ |
|---|
| 95 | SPDR = 0xF0; |
|---|
| 96 | while((SPSR&0x80)==0x00); |
|---|
| 97 | SPDR = (uint8)((addr & 0xFF00) >> 8); |
|---|
| 98 | while((SPSR&0x80)==0x00); |
|---|
| 99 | SPDR = (uint8)(addr & 0x00FF); |
|---|
| 100 | while((SPSR&0x80)==0x00); |
|---|
| 101 | SPDR = data; |
|---|
| 102 | while((SPSR&0x80)==0x00); |
|---|
| 103 | #ifndef __ARDUINO__ |
|---|
| 104 | PORTB = 0x01; // SPI end |
|---|
| 105 | // CS=1, waiting for SPI start |
|---|
| 106 | #else |
|---|
| 107 | PORTB = PORTB | CS_PIN; // SPI end |
|---|
| 108 | #endif // ifndef __ARDUINO__ |
|---|
| 109 | IINCHIP_ISR_ENABLE(); |
|---|
| 110 | #else |
|---|
| 111 | #error "unknown bus type" |
|---|
| 112 | #endif |
|---|
| 113 | return 1; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | @brief This function reads the value from W5100 registers. |
|---|
| 119 | */ |
|---|
| 120 | uint8 IINCHIP_READ(uint16 addr) |
|---|
| 121 | { |
|---|
| 122 | uint8 data; |
|---|
| 123 | |
|---|
| 124 | // DIRECT MODE I/F |
|---|
| 125 | |
|---|
| 126 | #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__) |
|---|
| 127 | IINCHIP_ISR_DISABLE(); |
|---|
| 128 | data = *((vuint8*)(addr)); |
|---|
| 129 | IINCHIP_ISR_ENABLE(); |
|---|
| 130 | #elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) |
|---|
| 131 | IINCHIP_ISR_DISABLE(); |
|---|
| 132 | *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8); |
|---|
| 133 | *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF); |
|---|
| 134 | data = *((vuint8*)IDM_DR); |
|---|
| 135 | IINCHIP_ISR_ENABLE(); |
|---|
| 136 | |
|---|
| 137 | #elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__) |
|---|
| 138 | //SPI MODE I/F |
|---|
| 139 | IINCHIP_ISR_DISABLE(); |
|---|
| 140 | #ifndef __ARDUINO__ |
|---|
| 141 | DDRB = 0x07; // MISO=input, etc.=output |
|---|
| 142 | // PB3(MISO), PB2(MOSI), PB1(SCK), PB0(/SS) |
|---|
| 143 | PORTB = 0x01; // CS=1, waiting for SPI start |
|---|
| 144 | SPCR = 0x50; // SPI mode 0, 4MHz |
|---|
| 145 | SPSR = 0x01; // SPI2X=0 |
|---|
| 146 | |
|---|
| 147 | PORTB = 0x00; // CS=0, SPI start |
|---|
| 148 | #else |
|---|
| 149 | PORTB = PORTB & ~CS_PIN; // CS=0, SPI start |
|---|
| 150 | #endif // ifndef __ARDUINO__ |
|---|
| 151 | SPDR = 0x0F; |
|---|
| 152 | while((SPSR&0x80)==0x00); |
|---|
| 153 | SPDR = (uint8)((addr & 0xFF00) >> 8); |
|---|
| 154 | while((SPSR&0x80)==0x00); |
|---|
| 155 | SPDR = (uint8)(addr & 0x00FF); |
|---|
| 156 | while((SPSR&0x80)==0x00); |
|---|
| 157 | SPDR = 0x00; // write dummy data |
|---|
| 158 | while((SPSR&0x80)==0x00); |
|---|
| 159 | data = SPDR; // read data |
|---|
| 160 | #ifndef __ARDUINO__ |
|---|
| 161 | PORTB = 0x01; // SPI end |
|---|
| 162 | // CS=1, waiting for SPI start |
|---|
| 163 | #else |
|---|
| 164 | PORTB = PORTB | CS_PIN; // SPI end |
|---|
| 165 | #endif // ifndef __ARDUINO__ |
|---|
| 166 | IINCHIP_ISR_ENABLE(); |
|---|
| 167 | #else |
|---|
| 168 | #error "unknown bus type" |
|---|
| 169 | #endif |
|---|
| 170 | return data; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | /** |
|---|
| 175 | @brief This function writes into W5100 memory(Buffer) |
|---|
| 176 | */ |
|---|
| 177 | uint16 wiz_write_buf(uint16 addr,uint8* buf,uint16 len) |
|---|
| 178 | { |
|---|
| 179 | #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__) |
|---|
| 180 | IINCHIP_ISR_DISABLE(); |
|---|
| 181 | memcpy((uint8 *)addr, buf, len); |
|---|
| 182 | IINCHIP_ISR_ENABLE(); |
|---|
| 183 | #elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) |
|---|
| 184 | uint16 idx = 0; |
|---|
| 185 | IINCHIP_ISR_DISABLE(); |
|---|
| 186 | *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8); |
|---|
| 187 | *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF); |
|---|
| 188 | for (idx = 0; idx < len ; idx++) *((vuint8*)IDM_DR) = buf[idx]; |
|---|
| 189 | IINCHIP_ISR_ENABLE(); |
|---|
| 190 | #elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__) |
|---|
| 191 | //SPI MODE I/F |
|---|
| 192 | IINCHIP_ISR_DISABLE(); |
|---|
| 193 | uint16 ii=0; |
|---|
| 194 | |
|---|
| 195 | for(ii=0;ii<len;ii++) |
|---|
| 196 | { |
|---|
| 197 | #ifndef __ARDUINO__ |
|---|
| 198 | DDRB = 0x07; // MISO=input, etc.=output |
|---|
| 199 | // PB3(MISO), PB2(MOSI), PB1(SCK), PB0(/SS) |
|---|
| 200 | PORTB = 0x01; // CS=1, waiting for SPI start |
|---|
| 201 | SPCR = 0x50; // SPI mode 0, 4MHz |
|---|
| 202 | SPSR = 0x01; // SPI2X=0 |
|---|
| 203 | |
|---|
| 204 | PORTB = 0x00; // CS=0, SPI start |
|---|
| 205 | #else |
|---|
| 206 | PORTB = PORTB & ~CS_PIN; // CS=0, SPI start |
|---|
| 207 | #endif // ifndef __ARDUINO__ |
|---|
| 208 | SPDR = 0xF0; |
|---|
| 209 | while((SPSR&0x80)==0x00); |
|---|
| 210 | SPDR = (uint8)(((addr+ii) & 0xFF00) >> 8); |
|---|
| 211 | while((SPSR&0x80)==0x00); |
|---|
| 212 | SPDR = (uint8)((addr+ii) & 0x00FF); |
|---|
| 213 | while((SPSR&0x80)==0x00); |
|---|
| 214 | SPDR = buf[ii]; |
|---|
| 215 | while((SPSR&0x80)==0x00); |
|---|
| 216 | #ifndef __ARDUINO__ |
|---|
| 217 | PORTB = 0x01; // SPI end |
|---|
| 218 | // CS=1, waiting for SPI start |
|---|
| 219 | #else |
|---|
| 220 | PORTB = PORTB | CS_PIN; // SPI end |
|---|
| 221 | #endif // ifndef __ARDUINO__ |
|---|
| 222 | } |
|---|
| 223 | IINCHIP_ISR_ENABLE(); |
|---|
| 224 | #else |
|---|
| 225 | #error "unknown bus type" |
|---|
| 226 | #endif |
|---|
| 227 | return len; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | /** |
|---|
| 232 | @brief This function reads into W5100 memory(Buffer) |
|---|
| 233 | */ |
|---|
| 234 | uint16 wiz_read_buf(uint16 addr, uint8* buf,uint16 len) |
|---|
| 235 | { |
|---|
| 236 | #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__) |
|---|
| 237 | IINCHIP_ISR_DISABLE(); |
|---|
| 238 | memcpy(buf, (uint8 *)addr, len); |
|---|
| 239 | IINCHIP_ISR_ENABLE(); |
|---|
| 240 | #elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) |
|---|
| 241 | uint16 idx = 0; |
|---|
| 242 | IINCHIP_ISR_DISABLE(); |
|---|
| 243 | *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8); |
|---|
| 244 | *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF); |
|---|
| 245 | for (idx = 0; idx < len ; idx++) buf[idx] = *((vuint8*)IDM_DR); |
|---|
| 246 | IINCHIP_ISR_ENABLE(); |
|---|
| 247 | #elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__) |
|---|
| 248 | //SPI MODE I/F |
|---|
| 249 | IINCHIP_ISR_DISABLE(); |
|---|
| 250 | uint16 iii=0; |
|---|
| 251 | for (iii=0; iii<len; iii++) |
|---|
| 252 | { |
|---|
| 253 | #ifndef __ARDUINO__ |
|---|
| 254 | DDRB = 0x07; // MISO=input, etc.=output |
|---|
| 255 | // PB3(MISO), PB2(MOSI), PB1(SCK), PB0(/SS) |
|---|
| 256 | PORTB = 0x01; // CS=1, waiting for SPI start |
|---|
| 257 | SPCR = 0x50; // SPI mode 0, 4MHz |
|---|
| 258 | SPSR = 0x01; // SPI2X=0 |
|---|
| 259 | |
|---|
| 260 | PORTB = 0x00; // CS=0, SPI start |
|---|
| 261 | #else |
|---|
| 262 | PORTB = PORTB & ~CS_PIN; // CS=0, SPI start |
|---|
| 263 | #endif // ifndef __ARDUINO__ |
|---|
| 264 | SPDR = 0x0F; |
|---|
| 265 | while((SPSR&0x80)==0x00); |
|---|
| 266 | SPDR = (uint8)(((addr+iii) & 0xFF00) >> 8); |
|---|
| 267 | while((SPSR&0x80)==0x00); |
|---|
| 268 | SPDR = (uint8)((addr+iii) & 0x00FF); |
|---|
| 269 | while((SPSR&0x80)==0x00); |
|---|
| 270 | //for (iii=0; iii<len; iii++) |
|---|
| 271 | //{ |
|---|
| 272 | SPDR = 0x00; // write dummy data |
|---|
| 273 | while((SPSR&0x80)==0x00); |
|---|
| 274 | buf[iii]=SPDR; // read data |
|---|
| 275 | //} |
|---|
| 276 | #ifndef __ARDUINO__ |
|---|
| 277 | PORTB = 0x01; // SPI end |
|---|
| 278 | // CS=1, waiting for SPI start |
|---|
| 279 | #else |
|---|
| 280 | PORTB = PORTB | CS_PIN; // SPI end |
|---|
| 281 | #endif // ifndef __ARDUINO__ |
|---|
| 282 | } |
|---|
| 283 | IINCHIP_ISR_ENABLE(); |
|---|
| 284 | #else |
|---|
| 285 | #error "unknown bus type" |
|---|
| 286 | #endif |
|---|
| 287 | return len; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | #ifndef __ARDUINO__ |
|---|
| 291 | #if (__COMPILER_VERSION__ == __WINAVR_20050214__) |
|---|
| 292 | static void iinchip_irq(void); |
|---|
| 293 | |
|---|
| 294 | void SIG_INTERRUPT4( void ) __attribute__ ((signal)); |
|---|
| 295 | void SIG_INTERRUPT4( void ) |
|---|
| 296 | { |
|---|
| 297 | iinchip_irq(); |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | /** |
|---|
| 302 | @brief Socket interrupt routine |
|---|
| 303 | */ |
|---|
| 304 | static void iinchip_irq(void) |
|---|
| 305 | { |
|---|
| 306 | #ifdef __DEF_IINCHIP_INT__ |
|---|
| 307 | uint8 int_val; |
|---|
| 308 | IINCHIP_ISR_DISABLE(); |
|---|
| 309 | int_val = IINCHIP_READ(IR); |
|---|
| 310 | |
|---|
| 311 | if (int_val & IR_CONFLICT) |
|---|
| 312 | { |
|---|
| 313 | printf("IP conflict : %.2x\r\n", int_val); |
|---|
| 314 | } |
|---|
| 315 | if (int_val & IR_UNREACH) |
|---|
| 316 | { |
|---|
| 317 | printf("INT Port Unreachable : %.2x\r\n", int_val); |
|---|
| 318 | printf("UIPR0 : %d.%d.%d.%d\r\n", IINCHIP_READ(UIPR0), IINCHIP_READ(UIPR0+1), IINCHIP_READ(UIPR0+2), IINCHIP_READ(UIPR0+3)); |
|---|
| 319 | printf("UPORT0 : %.2x %.2x\r\n", IINCHIP_READ(UPORT0), IINCHIP_READ(UPORT0+1)); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | if (int_val & IR_SOCK(0)) |
|---|
| 323 | { |
|---|
| 324 | I_STATUS[0] = IINCHIP_READ(Sn_IR(0)); |
|---|
| 325 | IINCHIP_WRITE(Sn_IR(0), I_STATUS[0] & 0xf); |
|---|
| 326 | } |
|---|
| 327 | if (int_val & IR_SOCK(1)) |
|---|
| 328 | { |
|---|
| 329 | I_STATUS[1] = IINCHIP_READ(Sn_IR(1)); |
|---|
| 330 | IINCHIP_WRITE(Sn_IR(1), I_STATUS[1] & 0xf); |
|---|
| 331 | } |
|---|
| 332 | if (int_val & IR_SOCK(2)) |
|---|
| 333 | { |
|---|
| 334 | I_STATUS[2] = IINCHIP_READ(Sn_IR(2)); |
|---|
| 335 | IINCHIP_WRITE(Sn_IR(2), I_STATUS[2] & 0xf); |
|---|
| 336 | } |
|---|
| 337 | if (int_val & IR_SOCK(3)) |
|---|
| 338 | { |
|---|
| 339 | I_STATUS[3] = IINCHIP_READ(Sn_IR(3)); |
|---|
| 340 | IINCHIP_WRITE(Sn_IR(3), I_STATUS[3] & 0xf); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | IINCHIP_WRITE(IR, int_val); |
|---|
| 344 | |
|---|
| 345 | IINCHIP_ISR_ENABLE(); |
|---|
| 346 | |
|---|
| 347 | #endif |
|---|
| 348 | } |
|---|
| 349 | #else |
|---|
| 350 | /** |
|---|
| 351 | @brief Socket interrupt routine |
|---|
| 352 | */ |
|---|
| 353 | ISR(INT4_vect) |
|---|
| 354 | { |
|---|
| 355 | #ifdef __DEF_IINCHIP_INT__ |
|---|
| 356 | uint8 int_val; |
|---|
| 357 | IINCHIP_ISR_DISABLE(); |
|---|
| 358 | int_val = IINCHIP_READ(IR); |
|---|
| 359 | |
|---|
| 360 | if (int_val & IR_CONFLICT) |
|---|
| 361 | { |
|---|
| 362 | printf("IP conflict : %.2x\r\n", int_val); |
|---|
| 363 | } |
|---|
| 364 | if (int_val & IR_UNREACH) |
|---|
| 365 | { |
|---|
| 366 | printf("INT Port Unreachable : %.2x\r\n", int_val); |
|---|
| 367 | printf("UIPR0 : %d.%d.%d.%d\r\n", IINCHIP_READ(UIPR0), IINCHIP_READ(UIPR0+1), IINCHIP_READ(UIPR0+2), IINCHIP_READ(UIPR0+3)); |
|---|
| 368 | printf("UPORT0 : %.2x %.2x\r\n", IINCHIP_READ(UPORT0), IINCHIP_READ(UPORT0+1)); |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | if (int_val & IR_SOCK(0)) |
|---|
| 372 | { |
|---|
| 373 | I_STATUS[0] = IINCHIP_READ(Sn_IR(0)); |
|---|
| 374 | IINCHIP_WRITE(Sn_IR(0), I_STATUS[0] & 0xf); |
|---|
| 375 | } |
|---|
| 376 | if (int_val & IR_SOCK(1)) |
|---|
| 377 | { |
|---|
| 378 | I_STATUS[1] = IINCHIP_READ(Sn_IR(1)); |
|---|
| 379 | IINCHIP_WRITE(Sn_IR(1), I_STATUS[1] & 0xf); |
|---|
| 380 | } |
|---|
| 381 | if (int_val & IR_SOCK(2)) |
|---|
| 382 | { |
|---|
| 383 | I_STATUS[2] = IINCHIP_READ(Sn_IR(2)); |
|---|
| 384 | IINCHIP_WRITE(Sn_IR(2), I_STATUS[2] & 0xf); |
|---|
| 385 | } |
|---|
| 386 | if (int_val & IR_SOCK(3)) |
|---|
| 387 | { |
|---|
| 388 | I_STATUS[3] = IINCHIP_READ(Sn_IR(3)); |
|---|
| 389 | IINCHIP_WRITE(Sn_IR(3), I_STATUS[3] & 0xf); |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | IINCHIP_WRITE(IR, int_val); |
|---|
| 393 | |
|---|
| 394 | IINCHIP_ISR_ENABLE(); |
|---|
| 395 | |
|---|
| 396 | #endif |
|---|
| 397 | } |
|---|
| 398 | #endif |
|---|
| 399 | #endif // ifndef __ARDUINO__ |
|---|
| 400 | |
|---|
| 401 | /** |
|---|
| 402 | @brief This function is for resetting of the iinchip. Initializes the iinchip to work in whether DIRECT or INDIRECT mode |
|---|
| 403 | */ |
|---|
| 404 | void iinchip_init(void) |
|---|
| 405 | { |
|---|
| 406 | setMR(MR_RST); |
|---|
| 407 | |
|---|
| 408 | #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) |
|---|
| 409 | setMR(MR_IND | MR_AI); |
|---|
| 410 | #ifdef __DEF_IINCHIP_DBG__ |
|---|
| 411 | printf("MR value is %d \r\n",IINCHIP_READ(MR)); |
|---|
| 412 | #endif |
|---|
| 413 | #endif |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | /** |
|---|
| 418 | @brief This function set the transmit & receive buffer size as per the channels is used |
|---|
| 419 | |
|---|
| 420 | Note for TMSR and RMSR bits are as follows\n |
|---|
| 421 | bit 1-0 : memory size of channel #0 \n |
|---|
| 422 | bit 3-2 : memory size of channel #1 \n |
|---|
| 423 | bit 5-4 : memory size of channel #2 \n |
|---|
| 424 | bit 7-6 : memory size of channel #3 \n\n |
|---|
| 425 | Maximum memory size for Tx, Rx in the W5100 is 8K Bytes,\n |
|---|
| 426 | In the range of 8KBytes, the memory size could be allocated dynamically by each channel.\n |
|---|
| 427 | Be attentive to sum of memory size shouldn't exceed 8Kbytes\n |
|---|
| 428 | and to data transmission and receiption from non-allocated channel may cause some problems.\n |
|---|
| 429 | If the 8KBytes memory is already assigned to centain channel, \n |
|---|
| 430 | other 3 channels couldn't be used, for there's no available memory.\n |
|---|
| 431 | If two 4KBytes memory are assigned to two each channels, \n |
|---|
| 432 | other 2 channels couldn't be used, for there's no available memory.\n |
|---|
| 433 | */ |
|---|
| 434 | void sysinit( |
|---|
| 435 | uint8 tx_size, /**< tx_size Tx memory size (00 - 1KByte, 01- 2KBtye, 10 - 4KByte, 11 - 8KByte) */ |
|---|
| 436 | uint8 rx_size /**< rx_size Rx memory size (00 - 1KByte, 01- 2KBtye, 10 - 4KByte, 11 - 8KByte) */ |
|---|
| 437 | ) |
|---|
| 438 | { |
|---|
| 439 | int16 i; |
|---|
| 440 | int16 ssum,rsum; |
|---|
| 441 | |
|---|
| 442 | #ifdef __DEF_IINCHIP_DBG__ |
|---|
| 443 | printf("sysinit()\r\n"); |
|---|
| 444 | #endif |
|---|
| 445 | |
|---|
| 446 | ssum = 0; |
|---|
| 447 | rsum = 0; |
|---|
| 448 | |
|---|
| 449 | IINCHIP_WRITE(TMSR,tx_size); /* Set Tx memory size for each channel */ |
|---|
| 450 | IINCHIP_WRITE(RMSR,rx_size); /* Set Rx memory size for each channel */ |
|---|
| 451 | |
|---|
| 452 | SBUFBASEADDRESS[0] = (uint16)(__DEF_IINCHIP_MAP_TXBUF__); /* Set base address of Tx memory for channel #0 */ |
|---|
| 453 | RBUFBASEADDRESS[0] = (uint16)(__DEF_IINCHIP_MAP_RXBUF__); /* Set base address of Rx memory for channel #0 */ |
|---|
| 454 | |
|---|
| 455 | #ifdef __DEF_IINCHIP_DBG__ |
|---|
| 456 | printf("Channel : SEND MEM SIZE : RECV MEM SIZE\r\n"); |
|---|
| 457 | #endif |
|---|
| 458 | |
|---|
| 459 | for (i = 0 ; i < MAX_SOCK_NUM; i++) // Set the size, masking and base address of Tx & Rx memory by each channel |
|---|
| 460 | { |
|---|
| 461 | SSIZE[i] = (int16)(0); |
|---|
| 462 | RSIZE[i] = (int16)(0); |
|---|
| 463 | if (ssum < 8192) |
|---|
| 464 | { |
|---|
| 465 | switch((tx_size >> i*2) & 0x03) // Set Tx memory size |
|---|
| 466 | { |
|---|
| 467 | case 0: |
|---|
| 468 | SSIZE[i] = (int16)(1024); |
|---|
| 469 | SMASK[i] = (uint16)(0x03FF); |
|---|
| 470 | break; |
|---|
| 471 | case 1: |
|---|
| 472 | SSIZE[i] = (int16)(2048); |
|---|
| 473 | SMASK[i] = (uint16)(0x07FF); |
|---|
| 474 | break; |
|---|
| 475 | case 2: |
|---|
| 476 | SSIZE[i] = (int16)(4096); |
|---|
| 477 | SMASK[i] = (uint16)(0x0FFF); |
|---|
| 478 | break; |
|---|
| 479 | case 3: |
|---|
| 480 | SSIZE[i] = (int16)(8192); |
|---|
| 481 | SMASK[i] = (uint16)(0x1FFF); |
|---|
| 482 | break; |
|---|
| 483 | } |
|---|
| 484 | } |
|---|
| 485 | if (rsum < 8192) |
|---|
| 486 | { |
|---|
| 487 | switch((rx_size >> i*2) & 0x03) // Set Rx memory size |
|---|
| 488 | { |
|---|
| 489 | case 0: |
|---|
| 490 | RSIZE[i] = (int16)(1024); |
|---|
| 491 | RMASK[i] = (uint16)(0x03FF); |
|---|
| 492 | break; |
|---|
| 493 | case 1: |
|---|
| 494 | RSIZE[i] = (int16)(2048); |
|---|
| 495 | RMASK[i] = (uint16)(0x07FF); |
|---|
| 496 | break; |
|---|
| 497 | case 2: |
|---|
| 498 | RSIZE[i] = (int16)(4096); |
|---|
| 499 | RMASK[i] = (uint16)(0x0FFF); |
|---|
| 500 | break; |
|---|
| 501 | case 3: |
|---|
| 502 | RSIZE[i] = (int16)(8192); |
|---|
| 503 | RMASK[i] = (uint16)(0x1FFF); |
|---|
| 504 | break; |
|---|
| 505 | } |
|---|
| 506 | } |
|---|
| 507 | ssum += SSIZE[i]; |
|---|
| 508 | rsum += RSIZE[i]; |
|---|
| 509 | |
|---|
| 510 | if (i != 0) // Sets base address of Tx and Rx memory for channel #1,#2,#3 |
|---|
| 511 | { |
|---|
| 512 | SBUFBASEADDRESS[i] = SBUFBASEADDRESS[i-1] + SSIZE[i-1]; |
|---|
| 513 | RBUFBASEADDRESS[i] = RBUFBASEADDRESS[i-1] + RSIZE[i-1]; |
|---|
| 514 | } |
|---|
| 515 | #ifdef __DEF_IINCHIP_DBG__ |
|---|
| 516 | printf("%d : %.4x : %.4x : %.4x : %.4x\r\n", i, (uint16)SBUFBASEADDRESS[i], (uint16)RBUFBASEADDRESS[i], SSIZE[i], RSIZE[i]); |
|---|
| 517 | #endif |
|---|
| 518 | } |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | void setMR(uint8 val) |
|---|
| 522 | { |
|---|
| 523 | *((volatile uint8*)(MR)) = val; |
|---|
| 524 | } |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | /** |
|---|
| 528 | @brief This function sets up gateway IP address. |
|---|
| 529 | */ |
|---|
| 530 | void setGAR( |
|---|
| 531 | uint8 * addr /**< a pointer to a 4 -byte array responsible to set the Gateway IP address. */ |
|---|
| 532 | ) |
|---|
| 533 | { |
|---|
| 534 | IINCHIP_WRITE((GAR0 + 0),addr[0]); |
|---|
| 535 | IINCHIP_WRITE((GAR0 + 1),addr[1]); |
|---|
| 536 | IINCHIP_WRITE((GAR0 + 2),addr[2]); |
|---|
| 537 | IINCHIP_WRITE((GAR0 + 3),addr[3]); |
|---|
| 538 | } |
|---|
| 539 | void getGWIP(uint8 * addr) |
|---|
| 540 | { |
|---|
| 541 | addr[0] = IINCHIP_READ((GAR0 + 0)); |
|---|
| 542 | addr[1] = IINCHIP_READ((GAR0 + 1)); |
|---|
| 543 | addr[2] = IINCHIP_READ((GAR0 + 2)); |
|---|
| 544 | addr[3] = IINCHIP_READ((GAR0 + 3)); |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | |
|---|
| 548 | /** |
|---|
| 549 | @brief It sets up SubnetMask address |
|---|
| 550 | */ |
|---|
| 551 | void setSUBR( |
|---|
| 552 | uint8 * addr /**< a pointer to a 4 -byte array responsible to set the SubnetMask address */ |
|---|
| 553 | ) |
|---|
| 554 | { |
|---|
| 555 | IINCHIP_WRITE((SUBR0 + 0),addr[0]); |
|---|
| 556 | IINCHIP_WRITE((SUBR0 + 1),addr[1]); |
|---|
| 557 | IINCHIP_WRITE((SUBR0 + 2),addr[2]); |
|---|
| 558 | IINCHIP_WRITE((SUBR0 + 3),addr[3]); |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | |
|---|
| 562 | /** |
|---|
| 563 | @brief This function sets up MAC address. |
|---|
| 564 | */ |
|---|
| 565 | void setSHAR( |
|---|
| 566 | uint8 * addr /**< a pointer to a 6 -byte array responsible to set the MAC address. */ |
|---|
| 567 | ) |
|---|
| 568 | { |
|---|
| 569 | IINCHIP_WRITE((SHAR0 + 0),addr[0]); |
|---|
| 570 | IINCHIP_WRITE((SHAR0 + 1),addr[1]); |
|---|
| 571 | IINCHIP_WRITE((SHAR0 + 2),addr[2]); |
|---|
| 572 | IINCHIP_WRITE((SHAR0 + 3),addr[3]); |
|---|
| 573 | IINCHIP_WRITE((SHAR0 + 4),addr[4]); |
|---|
| 574 | IINCHIP_WRITE((SHAR0 + 5),addr[5]); |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | |
|---|
| 578 | /** |
|---|
| 579 | @brief This function sets up Source IP address. |
|---|
| 580 | */ |
|---|
| 581 | void setSIPR( |
|---|
| 582 | uint8 * addr /**< a pointer to a 4 -byte array responsible to set the Source IP address. */ |
|---|
| 583 | ) |
|---|
| 584 | { |
|---|
| 585 | IINCHIP_WRITE((SIPR0 + 0),addr[0]); |
|---|
| 586 | IINCHIP_WRITE((SIPR0 + 1),addr[1]); |
|---|
| 587 | IINCHIP_WRITE((SIPR0 + 2),addr[2]); |
|---|
| 588 | IINCHIP_WRITE((SIPR0 + 3),addr[3]); |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | |
|---|
| 592 | /** |
|---|
| 593 | @brief This function gets Interrupt register in common register. |
|---|
| 594 | */ |
|---|
| 595 | uint8 getIR( void ) |
|---|
| 596 | { |
|---|
| 597 | return IINCHIP_READ(IR); |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | /** |
|---|
| 603 | @brief This function sets up Retransmission time. |
|---|
| 604 | |
|---|
| 605 | If there is no response from the peer or delay in response then retransmission |
|---|
| 606 | will be there as per RTR (Retry Time-value Register)setting |
|---|
| 607 | */ |
|---|
| 608 | void setRTR(uint16 timeout) |
|---|
| 609 | { |
|---|
| 610 | IINCHIP_WRITE(RTR0,(uint8)((timeout & 0xff00) >> 8)); |
|---|
| 611 | IINCHIP_WRITE((RTR0 + 1),(uint8)(timeout & 0x00ff)); |
|---|
| 612 | } |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | /** |
|---|
| 616 | @brief This function set the number of Retransmission. |
|---|
| 617 | |
|---|
| 618 | If there is no response from the peer or delay in response then recorded time |
|---|
| 619 | as per RTR & RCR register seeting then time out will occur. |
|---|
| 620 | */ |
|---|
| 621 | void setRCR(uint8 retry) |
|---|
| 622 | { |
|---|
| 623 | IINCHIP_WRITE(RCR,retry); |
|---|
| 624 | } |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | /** |
|---|
| 628 | @brief This function set the interrupt mask Enable/Disable appropriate Interrupt. ('1' : interrupt enable) |
|---|
| 629 | |
|---|
| 630 | If any bit in IMR is set as '0' then there is not interrupt signal though the bit is |
|---|
| 631 | set in IR register. |
|---|
| 632 | */ |
|---|
| 633 | void setIMR(uint8 mask) |
|---|
| 634 | { |
|---|
| 635 | IINCHIP_WRITE(IMR,mask); // must be setted 0x10. |
|---|
| 636 | } |
|---|
| 637 | |
|---|
| 638 | |
|---|
| 639 | /** |
|---|
| 640 | @brief These below functions are used to get the Gateway, SubnetMask |
|---|
| 641 | and Source Hardware Address (MAC Address) and Source IP address |
|---|
| 642 | */ |
|---|
| 643 | void getGAR(uint8 * addr) |
|---|
| 644 | { |
|---|
| 645 | addr[0] = IINCHIP_READ(GAR0); |
|---|
| 646 | addr[1] = IINCHIP_READ(GAR0+1); |
|---|
| 647 | addr[2] = IINCHIP_READ(GAR0+2); |
|---|
| 648 | addr[3] = IINCHIP_READ(GAR0+3); |
|---|
| 649 | } |
|---|
| 650 | void getSUBR(uint8 * addr) |
|---|
| 651 | { |
|---|
| 652 | addr[0] = IINCHIP_READ(SUBR0); |
|---|
| 653 | addr[1] = IINCHIP_READ(SUBR0+1); |
|---|
| 654 | addr[2] = IINCHIP_READ(SUBR0+2); |
|---|
| 655 | addr[3] = IINCHIP_READ(SUBR0+3); |
|---|
| 656 | } |
|---|
| 657 | void getSHAR(uint8 * addr) |
|---|
| 658 | { |
|---|
| 659 | addr[0] = IINCHIP_READ(SHAR0); |
|---|
| 660 | addr[1] = IINCHIP_READ(SHAR0+1); |
|---|
| 661 | addr[2] = IINCHIP_READ(SHAR0+2); |
|---|
| 662 | addr[3] = IINCHIP_READ(SHAR0+3); |
|---|
| 663 | addr[4] = IINCHIP_READ(SHAR0+4); |
|---|
| 664 | addr[5] = IINCHIP_READ(SHAR0+5); |
|---|
| 665 | } |
|---|
| 666 | void getSIPR(uint8 * addr) |
|---|
| 667 | { |
|---|
| 668 | addr[0] = IINCHIP_READ(SIPR0); |
|---|
| 669 | addr[1] = IINCHIP_READ(SIPR0+1); |
|---|
| 670 | addr[2] = IINCHIP_READ(SIPR0+2); |
|---|
| 671 | addr[3] = IINCHIP_READ(SIPR0+3); |
|---|
| 672 | } |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | /** |
|---|
| 676 | @brief These below functions are used to get the Destination Hardware Address (MAC Address), Destination IP address and Destination Port. |
|---|
| 677 | */ |
|---|
| 678 | void getSn_DHAR(SOCKET s, uint8 * addr) |
|---|
| 679 | { |
|---|
| 680 | addr[0] = IINCHIP_READ(Sn_DHAR0(s)); |
|---|
| 681 | addr[1] = IINCHIP_READ(Sn_DHAR0(s)+1); |
|---|
| 682 | addr[2] = IINCHIP_READ(Sn_DHAR0(s)+2); |
|---|
| 683 | addr[3] = IINCHIP_READ(Sn_DHAR0(s)+3); |
|---|
| 684 | addr[4] = IINCHIP_READ(Sn_DHAR0(s)+4); |
|---|
| 685 | addr[5] = IINCHIP_READ(Sn_DHAR0(s)+5); |
|---|
| 686 | } |
|---|
| 687 | void setSn_DHAR(SOCKET s, uint8 * addr) |
|---|
| 688 | { |
|---|
| 689 | IINCHIP_WRITE((Sn_DHAR0(s) + 0),addr[0]); |
|---|
| 690 | IINCHIP_WRITE((Sn_DHAR0(s) + 1),addr[1]); |
|---|
| 691 | IINCHIP_WRITE((Sn_DHAR0(s) + 2),addr[2]); |
|---|
| 692 | IINCHIP_WRITE((Sn_DHAR0(s) + 3),addr[3]); |
|---|
| 693 | IINCHIP_WRITE((Sn_DHAR0(s) + 4),addr[4]); |
|---|
| 694 | IINCHIP_WRITE((Sn_DHAR0(s) + 5),addr[5]); |
|---|
| 695 | } |
|---|
| 696 | void getSn_DIPR(SOCKET s, uint8 * addr) |
|---|
| 697 | { |
|---|
| 698 | addr[0] = IINCHIP_READ(Sn_DIPR0(s)); |
|---|
| 699 | addr[1] = IINCHIP_READ(Sn_DIPR0(s)+1); |
|---|
| 700 | addr[2] = IINCHIP_READ(Sn_DIPR0(s)+2); |
|---|
| 701 | addr[3] = IINCHIP_READ(Sn_DIPR0(s)+3); |
|---|
| 702 | } |
|---|
| 703 | void setSn_DIPR(SOCKET s, uint8 * addr) |
|---|
| 704 | { |
|---|
| 705 | IINCHIP_WRITE((Sn_DIPR0(s) + 0),addr[0]); |
|---|
| 706 | IINCHIP_WRITE((Sn_DIPR0(s) + 1),addr[1]); |
|---|
| 707 | IINCHIP_WRITE((Sn_DIPR0(s) + 2),addr[2]); |
|---|
| 708 | IINCHIP_WRITE((Sn_DIPR0(s) + 3),addr[3]); |
|---|
| 709 | } |
|---|
| 710 | void getSn_DPORT(SOCKET s, uint8 * addr) |
|---|
| 711 | { |
|---|
| 712 | addr[0] = IINCHIP_READ(Sn_DPORT0(s)); |
|---|
| 713 | addr[1] = IINCHIP_READ(Sn_DPORT0(s)+1); |
|---|
| 714 | } |
|---|
| 715 | void setSn_DPORT(SOCKET s, uint8 * addr) |
|---|
| 716 | { |
|---|
| 717 | IINCHIP_WRITE((Sn_DPORT0(s) + 0),addr[0]); |
|---|
| 718 | IINCHIP_WRITE((Sn_DPORT0(s) + 1),addr[1]); |
|---|
| 719 | } |
|---|
| 720 | |
|---|
| 721 | |
|---|
| 722 | /** |
|---|
| 723 | @brief This sets the maximum segment size of TCP in Active Mode), while in Passive Mode this is set by peer |
|---|
| 724 | */ |
|---|
| 725 | void setSn_MSS(SOCKET s, uint16 Sn_MSSR0) |
|---|
| 726 | { |
|---|
| 727 | IINCHIP_WRITE(Sn_MSSR0(s),(uint8)((Sn_MSSR0 & 0xff00) >> 8)); |
|---|
| 728 | IINCHIP_WRITE((Sn_MSSR0(s) + 1),(uint8)(Sn_MSSR0 & 0x00ff)); |
|---|
| 729 | } |
|---|
| 730 | |
|---|
| 731 | void setSn_TTL(SOCKET s, uint8 ttl) |
|---|
| 732 | { |
|---|
| 733 | IINCHIP_WRITE(Sn_TTL(s), ttl);< |
|---|