avr/main.c

Go to the documentation of this file.
00001 
00012 /*      file: main.c
00013      version: sync with EtherProgs
00014        state: coding 
00015   programmer: Sirpdc (Sir Pinto de Castro, sirpdc@gmail.com)
00016  description: firmware version of etherprogs to implement NE2000 
00017               device driver and the TCP/IP stack(using uIP)
00018 
00019 - changed BAUD to 56700 bps
00020 - put ASSERT on debug.h file (because eclipse was messing with code completion
00021 when arriving on assert definition)
00022 - updated util library to v0.04(added TxHex function)
00023  */
00024 
00025 /********************************************************************
00026 *   ATENÇÃO AO TAMANHO OCUPADO NA RAM!
00027 *
00028 *  - Quando o micro começa a fazer reset's é porque a stack está corrompida
00029 *                 => no nosso caso, ter atenção ao acesso aos vectores
00030 *                                 => ocupação da RAM!!!!!!!!!!!
00031 *
00032 
00033 ********************************************************************/
00034 #define INFO "Isa ethernet board firmware, version 0071"
00035 
00036 #include <config.h>
00037 #include "NE2000.h"
00038 #include "oscp.h"
00039 
00040 //******************************************************************
00041 #include <avr/io.h>
00042 #include "util.h"
00043 #include "debug.h"
00044 #include "isa_bus.h"
00045 #include "uip.h"
00046 #include "uip_arp.h"
00047 #include "config.h"
00048 
00049 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
00050 
00051 
00052 #if UIP_UDP
00053 void uip_udp_listen(u16_t port)
00054 {
00055   u8_t c;         
00056   for(c = 0; c < UIP_UDP_CONNS; ++c) {
00057     if(0==uip_udp_conns[c].lport){
00058         uip_udp_conns[c].lport = port;
00059     }
00060   }
00061 }
00062 #endif /* UIP_UDP */
00063 
00064 #define FRAME_HEADER ((struct frame_header *)&uip_appdata[0])
00065 #define FRAME_BUFFER ((unsigned char *)&uip_appdata[sizeof(struct frame_header)])
00066 #define FRAME_CHKSUM *((unsigned char *)&uip_appdata[sizeof(struct frame_header)+FRAME_HEADER->n])
00067 
00068 struct frame_header * m_frame_hdr;
00069 struct frame rxframe;   
00070 
00071 
00072  
00082 void udp_app(){
00083                 
00084         if( uip_newdata() ){
00085                 // this should go to initudp_app
00086                 m_frame_hdr = FRAME_HEADER;
00087                 
00088                 // copy header received 
00089                 memcpy( &rxframe,m_frame_hdr, sizeof(struct frame_header));             
00090                 // update data pointer
00091                 rxframe.data = FRAME_BUFFER;
00092                 // update checksum
00093                 rxframe.checksum = FRAME_CHKSUM;
00094                 
00095                 TxFrame(&rxframe);              // tX to the aquisition module
00096                 
00097                 // we should insert a timeout if the aquisition module, 
00098                 // did not responde
00099                 RxFrame(&rxframe);              // rx the response 
00100                 
00101                 // do the inverse
00102                 memcpy( m_frame_hdr, &rxframe, sizeof(struct frame_header));                            
00103                 FRAME_CHKSUM=rxframe.checksum;
00104                 
00105                 uip_udp_send(sizeof(struct frame_header)+m_frame_hdr->n+1 );    // 1 is the checksum
00106         }
00107         
00108 }
00120 int main(void)
00121 {
00122 
00123         // configure serial port for debug
00124         // data size=8, parity even and 1 stop bits
00125         USART_Init(BAUD,DATA_SIZE8|PARITY_DISABLE|STOP_BITS1);
00126 
00127         // send info
00128         TxEol();
00129         TxStr_P(INFO);
00130         TxEol();
00131 
00132         rtl8019SetupPorts();
00133 
00134         //Reset bus
00135         SetBits(CNTRL_PORT, RESET_PIN); // reset = 1
00136         delay(15);                                              // wait 15 ms
00137         RstBits(CNTRL_PORT, RESET_PIN); // reset = 0
00138 
00139         uint16_t i;
00140         
00141 #ifdef NE_PROBE 
00142         if( !ne_probe() ){
00143                 TxStr_P("Error detecting card!");
00144                 while(1) ;
00145         }
00146 #endif
00147 #ifdef NE_LOOPBACK      
00148     if(!loopback_test()){
00149         TxStr_P("Error on Loopback!");
00150                 while(1) ;
00151     }
00152 #endif
00153 
00154     TxStr_P("Initiating NE2000:...");
00155     ei_init();
00156     TxStr_P("Ok!");TxEol();
00157         
00158     TxStr_P("Testing memory.");TxEol();
00159     nic_mem_test();
00160     TxEol();
00161 
00162 
00163         /* Initialize the uIP TCP/IP stack. */
00164         uip_init();
00165         
00166         /* Initiate ARP module*/
00167         uip_arp_init();
00168   
00169         uip_udp_listen(HTONS(486));  
00170         
00171         /* This is where the dispacher start's!!!*/
00172         while(1) {
00173         /* Let the tapdev network device driver read an entire IP packet
00174            into the uip_buf. If it must wait for more than 0.5 seconds, it
00175            will return with the return value 0. If so, we know that it is
00176            time to call upon the uip_periodic(). Otherwise, the tapdev has
00177            received an IP packet that is to be processed by uIP. */
00178         uip_len = ei_poll();
00179         if(uip_len == 0) {
00180                 // sleep 100ms
00181                 #ifdef LINUX
00182                         usleep(100000); 
00183                 #else
00184                         delay(100);
00185                 #endif
00186                 /*
00187                 for(i = 0; i < UIP_CONNS; i++) {
00188                                 uip_periodic(i);
00189                                 // If the above function invocation resulted in data that
00190                                 // should be sent out on the network, the global variable
00191                                 // uip_len is set to a value > 0. 
00192                                 if(uip_len > 0) {
00193                                         uip_arp_out();
00194                                         tapdev_send();
00195                                 }
00196                 }      
00197                 
00198                 // Call the ARP timer function every 10 seconds. 
00199                 if(++arptimer == 20) {  
00200                                 uip_arp_timer();
00201                                 arptimer = 0;
00202                 }      
00203                 */
00204         } else {
00205                 if(BUF->type == htons(UIP_ETHTYPE_IP)) {
00206                                 uip_arp_ipin();
00207                                 uip_input();
00208                                 /* If the above function invocation resulted in data that
00209                                    should be sent out on the network, the global variable
00210                                    uip_len is set to a value > 0. */
00211                                 if(uip_len > 0) {
00212                                         uip_arp_out();
00213                                         ei_transmit(uip_buf,uip_len);
00214                                 }
00215                 }else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
00216                                 uip_arp_arpin();
00217                                 /* If the above function invocation resulted in data that
00218                                 should be sent out on the network, the global variable
00219                                 uip_len is set to a value > 0. */       
00220                                 if(uip_len > 0) {       
00221                                         ei_transmit(uip_buf,uip_len);
00222                                 }
00223                 }
00224         }    
00225   }
00226 
00227 
00228 
00229 }

Generated on Fri Jan 6 22:23:16 2006 for EtherProgs by  doxygen 1.4.5