Čeština / English
Login

SVN Repository / Prohlížení

Aktuální adresář: FITkit / trunk / mcu / libs / enc28j60 /

enc28j60_spi_rx.c

   1  /*******************************************************************************
   2  
   3     Copyright (C) 2010 Brno University of Technology,
   4                        Faculty of Information Technology
   5     Author(s): Martin Musil <xmusil34 AT fit.vutbr.cz>
   6  
   7     LICENSE TERMS
   8  
   9     Redistribution and use in source and binary forms, with or without
  10     modification, are permitted provided that the following conditions
  11     are met:
  12     1. Redistributions of source code must retain the above copyright
  13        notice, this list of conditions and the following disclaimer.
  14     2. Redistributions in binary form must reproduce the above copyright
  15        notice, this list of conditions and the following disclaimer in
  16        the documentation and/or other materials provided with the
  17        distribution.
  18     3. All advertising materials mentioning features or use of this software
  19        or firmware must display the following acknowledgement:
  20  
  21          This product includes software developed by the University of
  22          Technology, Faculty of Information Technology, Brno and its
  23          contributors.
  24  
  25     4. Neither the name of the Company nor the names of its contributors
  26        may be used to endorse or promote products derived from this
  27        software without specific prior written permission.
  28  
  29     This software or firmware is provided ``as is'', and any express or implied
  30     warranties, including, but not limited to, the implied warranties of
  31     merchantability and fitness for a particular purpose are disclaimed.
  32     In no event shall the company or contributors be liable for any
  33     direct, indirect, incidental, special, exemplary, or consequential
  34     damages (including, but not limited to, procurement of substitute
  35     goods or services; loss of use, data, or profits; or business
  36     interruption) however caused and on any theory of liability, whether
  37     in contract, strict liability, or tort (including negligence or
  38     otherwise) arising in any way out of the use of this software, even
  39     if advised of the possibility of such damage.
  40  
  41     $Id$
  42  
  43  *******************************************************************************/
  44  
  45  #include "enc28j60_spi_rx.h"
  46  #include "enc28j60_spi.h"
  47  
  48  
  49  // pomocne ukazatele
  50  unsigned int rx_next_pkt;						//ukazatele na dalsi paket
  51  unsigned int rx_pkt_len;						//zbyvajici delka paketu
  52  unsigned char receive_status[STATUS_LEN];		//status vektor
  53  unsigned char rx_initialized;					//zamek bufferu
  54  
  55  //***************************************
  56  //prijimaci buffer
  57  //***************************************
  58  
  59  /**
  60  
  61   **/
  62  char rx_init(){
  63  
  64  	select_bank(1);
  65  
  66  	if((read_reg8(EPKTCNT,NO_DUMMY) > 0) & (rx_initialized == 0)){					//pocet paketu
  67  		rx_initialized = 1;
  68  		select_bank(0);
  69  
  70  		spi_read((char*) &rx_next_pkt, PTR_LEN);		//zapamatovani next_packet pointeru;
  71  		spi_read(&receive_status,STATUS_LEN);			//precteni status-vektoru
  72  
  73  		unsigned int rec_len = *receive_status;
  74  
  75  		int ERDPT_tmp = read_reg16(ERDPT,NO_DUMMY);
  76  
  77  		if(ERDPT_tmp > rx_next_pkt){												//paket je na "hrane" bufferu (cast na konci pameti,cast na zacatku)
  78  			rx_pkt_len = RX_END - ERDPT_tmp + rx_next_pkt - RX_START;					//vypocet delky paketu
  79  		}
  80  		else{
  81  			rx_pkt_len = rx_next_pkt - ERDPT_tmp;
  82  		}
  83  
  84  		if((rec_len & 0x0001) > 0)		//licha delka prijateho ramce - konec ramce je doplnen, aby zacatek noveho ramce lezel na sude adrese
  85  			rx_pkt_len -= 5;	// od delky odecteme 4 bajty CRC a bajtovou mezeru mezi pakety
  86  		else
  87  			rx_pkt_len -= 4;
  88  	}
  89  	else return -1;
  90  
  91  return 0;
  92  }
  93  
  94  /**
  95  
  96   **/
  97  char rx_free(){
  98  
  99  	if(rx_initialized == 1){
 100  		select_bank(0);
 101  
 102  		write_reg16(ERDPT,rx_next_pkt);//nastaveni ERDPT na next_packet adresu;
 103  
 104  		write_reg16(ERXRDPT,rx_next_pkt);//posunuti ERXRDPT --> uvolneni pameti
 105  
 106  		bf_set(ECON2,ECON2_PKTDEC);
 107  		rx_initialized = 0;
 108  		return 0;
 109  	}
 110  return -1;
 111  }
 112  
 113  /**
 114  
 115   **/
 116  inline unsigned int rx_getc(){
 117  
 118  	if(rx_pkt_len--)
 119  		return (unsigned int) spi_getc();//kontrola mezi paketu;
 120  	else
 121  		return -1;
 122  };
 123  
 124  /**
 125  
 126   **/
 127  unsigned int rx_read(void* ptr, int length){
 128  
 129  	if(rx_pkt_len == 0)
 130  		return 0;
 131  	else if(length >= rx_pkt_len){
 132  		int bytes_readed = rx_pkt_len;
 133  		spi_read(ptr, rx_pkt_len);
 134  		rx_pkt_len = 0;
 135  		return bytes_readed;
 136  	}
 137  	else{
 138  		spi_read(ptr, length);
 139  		rx_pkt_len -= length;
 140  		return length;
 141  	}
 142  }
 143  
 144  /**
 145  
 146   **/
 147  unsigned int rx_getline(void* ptr, int max_length){
 148  
 149  	max_length--;
 150  
 151  	if(rx_pkt_len == 0){
 152  		((char*)ptr)[0] = 0;
 153  		return 0;
 154  	}
 155  	else if(max_length >= rx_pkt_len){
 156  		int bytes_readed = spi_getline(ptr, rx_pkt_len);
 157  		((char*)ptr)[bytes_readed] = 0;
 158  		rx_pkt_len -= bytes_readed;
 159  		return bytes_readed;
 160  	}
 161  	else{
 162  		int bytes_readed = spi_getline(ptr, max_length);
 163  		((char*)ptr)[bytes_readed] = 0;
 164  		rx_pkt_len -= bytes_readed;
 165  		return bytes_readed;
 166  	}
 167  }
 168  
 169  /**
 170  
 171   **/
 172  inline int rx_left(){
 173  
 174  return rx_pkt_len;
 175  }
 176  
 177  /**
 178  
 179   **/
 180  inline unsigned char rx_isempty(){
 181  
 182  return (rx_pkt_len > 0) ? 0 : 1;
 183  }
 184  
 185  /**
 186  
 187   **/
 188  inline void rx_cut_padding(unsigned char length){
 189  
 190  	rx_pkt_len -= length;
 191  return;
 192  }
 193  
Zobrazeno: 695375x Naposledy: 19.8.2022 13:09:15