FITkit
Fakulta informačních technologií

enc28j60_spi_rx.c

Zobrazit dokumentaci tohoto souboru.
00001 /*******************************************************************************
00002    enc28j60_spi_rx.c: Knihovna funkcí pro práci s přijímacím bufferem
00003    Copyright (C) 2010 Brno University of Technology,
00004                       Faculty of Information Technology
00005    Author(s): Martin Musil <xmusil34 AT fit.vutbr.cz>
00006 
00007    LICENSE TERMS
00008 
00009    Redistribution and use in source and binary forms, with or without
00010    modification, are permitted provided that the following conditions
00011    are met:
00012    1. Redistributions of source code must retain the above copyright
00013       notice, this list of conditions and the following disclaimer.
00014    2. Redistributions in binary form must reproduce the above copyright
00015       notice, this list of conditions and the following disclaimer in
00016       the documentation and/or other materials provided with the
00017       distribution.
00018    3. All advertising materials mentioning features or use of this software
00019       or firmware must display the following acknowledgement:
00020 
00021         This product includes software developed by the University of
00022         Technology, Faculty of Information Technology, Brno and its
00023         contributors.
00024 
00025    4. Neither the name of the Company nor the names of its contributors
00026       may be used to endorse or promote products derived from this
00027       software without specific prior written permission.
00028 
00029    This software or firmware is provided ``as is'', and any express or implied
00030    warranties, including, but not limited to, the implied warranties of
00031    merchantability and fitness for a particular purpose are disclaimed.
00032    In no event shall the company or contributors be liable for any
00033    direct, indirect, incidental, special, exemplary, or consequential
00034    damages (including, but not limited to, procurement of substitute
00035    goods or services; loss of use, data, or profits; or business
00036    interruption) however caused and on any theory of liability, whether
00037    in contract, strict liability, or tort (including negligence or
00038    otherwise) arising in any way out of the use of this software, even
00039    if advised of the possibility of such damage.
00040 
00041    $Id$
00042 
00043 *******************************************************************************/ 
00044 
00045 #include "enc28j60_spi_rx.h"
00046 #include "enc28j60_spi.h"
00047 
00048 
00049 // pomocne ukazatele 
00050 unsigned int rx_next_pkt;                                               //ukazatele na dalsi paket
00051 unsigned int rx_pkt_len;                                                //zbyvajici delka paketu
00052 unsigned char receive_status[STATUS_LEN];               //status vektor
00053 unsigned char rx_initialized;                                   //zamek bufferu 
00054 
00055 //***************************************
00056 //prijimaci buffer
00057 //***************************************
00058 
00062 char rx_init(){
00063 
00064         select_bank(1);
00065         
00066         if((read_reg8(EPKTCNT,NO_DUMMY) > 0) & (rx_initialized == 0)){                                  //pocet paketu
00067                 rx_initialized = 1;
00068                 select_bank(0);
00069 
00070                 spi_read((char*) &rx_next_pkt, PTR_LEN);                //zapamatovani next_packet pointeru;
00071                 spi_read(&receive_status,STATUS_LEN);                   //precteni status-vektoru
00072 
00073                 unsigned int rec_len = *receive_status;
00074 
00075                 int ERDPT_tmp = read_reg16(ERDPT,NO_DUMMY);
00076 
00077                 if(ERDPT_tmp > rx_next_pkt){                                                                                            //paket je na "hrane" bufferu (cast na konci pameti,cast na zacatku)
00078                         rx_pkt_len = RX_END - ERDPT_tmp + rx_next_pkt - RX_START;                                       //vypocet delky paketu
00079                 }
00080                 else{
00081                         rx_pkt_len = rx_next_pkt - ERDPT_tmp;
00082                 }
00083 
00084                 if((rec_len & 0x0001) > 0)              //licha delka prijateho ramce - konec ramce je doplnen, aby zacatek noveho ramce lezel na sude adrese
00085                         rx_pkt_len -= 5;        // od delky odecteme 4 bajty CRC a bajtovou mezeru mezi pakety
00086                 else    
00087                         rx_pkt_len -= 4;                                                                                
00088         }
00089         else return -1;
00090 
00091 return 0;
00092 }
00093 
00097 char rx_free(){
00098         
00099         if(rx_initialized == 1){
00100                 select_bank(0);
00101 
00102                 write_reg16(ERDPT,rx_next_pkt);//nastaveni ERDPT na next_packet adresu;
00103 
00104                 write_reg16(ERXRDPT,rx_next_pkt);//posunuti ERXRDPT --> uvolneni pameti
00105 
00106                 bf_set(ECON2,ECON2_PKTDEC);
00107                 rx_initialized = 0;
00108                 return 0;
00109         }
00110 return -1;
00111 }
00112 
00116 inline unsigned int rx_getc(){
00117            
00118         if(rx_pkt_len--)
00119                 return (unsigned int) spi_getc();//kontrola mezi paketu;
00120         else
00121                 return -1;
00122 };
00123 
00127 unsigned int rx_read(void* ptr, int length){
00128 
00129         if(rx_pkt_len == 0)
00130                 return 0;
00131         else if(length >= rx_pkt_len){
00132                 int bytes_readed = rx_pkt_len;
00133                 spi_read(ptr, rx_pkt_len);      
00134                 rx_pkt_len = 0;
00135                 return bytes_readed;
00136         }
00137         else{
00138                 spi_read(ptr, length);
00139                 rx_pkt_len -= length;
00140                 return length;
00141         }
00142 }
00143 
00147 unsigned int rx_getline(void* ptr, int max_length){
00148 
00149         max_length--;
00150 
00151         if(rx_pkt_len == 0){
00152                 ((char*)ptr)[0] = 0;    
00153                 return 0;
00154         }
00155         else if(max_length >= rx_pkt_len){
00156                 int bytes_readed = spi_getline(ptr, rx_pkt_len);
00157                 ((char*)ptr)[bytes_readed] = 0; 
00158                 rx_pkt_len -= bytes_readed;
00159                 return bytes_readed;
00160         }
00161         else{
00162                 int bytes_readed = spi_getline(ptr, max_length);
00163                 ((char*)ptr)[bytes_readed] = 0;
00164                 rx_pkt_len -= bytes_readed;
00165                 return bytes_readed;
00166         }
00167 }
00168 
00172 inline int rx_left(){
00173 
00174 return rx_pkt_len;
00175 }
00176 
00180 inline unsigned char rx_isempty(){
00181 
00182 return (rx_pkt_len > 0) ? 0 : 1;
00183 }       
00184 
00188 inline void rx_cut_padding(unsigned char length){
00189         
00190         rx_pkt_len -= length;
00191 return;
00192 }