Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.82 KB | None | 0 0
  1.  
  2. // Located in: microblaze_0/include/xparameters.h
  3. #include "xuartlite.h"
  4. #include "xparameters.h"
  5. #include "xintc.h"
  6. #include "xtmrctr_l.h"
  7. #include "stdio.h"
  8. #include "stdlib.h"
  9. #include "xutil.h"
  10. #include "base64decode.h"
  11.  
  12. // System Ace
  13. #include "xsysace.h"
  14. #include "sysace_stdio.h"
  15.  
  16. /* lwIP Includes */
  17. #include "netif/xemacif.h"
  18. #include "lwip/tcp.h"
  19. #include "lwip/memp.h"
  20. #include "lwip/mem.h"
  21. #include "lwip/pbuf.h"
  22. #include "netif/etharp.h"
  23. #include "lwipopts.h"
  24.  
  25. //====================================================
  26.  
  27. #define BUFF_SIZE 1024
  28.  
  29. /**         variables Golobal           **/
  30. int     portHost = 9010;
  31. struct  tcp_pcb *tx_pcb;
  32. char    ipLocal[4]  = {132,207,89,157};     // l adresse ip local
  33. char    ipHost[4]   = {132,207,89,02};
  34. char    subnet[4]   = {255,255,255,0};
  35. char    gateway[4]  = {132,207,89,0};
  36. char    fullmac[6]  = {0x00, 0x00, 0x0B, 0x35,0x22,0x89};
  37. char    buffRecv[BUFF_SIZE];            // tampon des donnees recus
  38. int     buffSizeRecv = 0;
  39. struct  netif *default_netif;
  40.  
  41.  
  42. /******     variables d'etat    *******/
  43. int     isConnected = 0;
  44. int     isDataSent = 0;
  45. int     isDataReceived = 0;
  46. int     isStreaming = 0;
  47. int     waiting_for_timer = 1;
  48.  
  49. /* defined in EDK generated xemacif_g.c file */
  50. extern XEmacIf_Config XEmacIf_ConfigTable[];
  51.  
  52.  
  53.  
  54.  
  55. /**
  56.     CallBack function
  57. **/
  58. void mytimer_int_handler (void* baseaddr_p) {
  59.     //xil_printf("Timer Handler ...\n\t");
  60.     int baseaddr = *(int *)baseaddr_p;
  61.     unsigned int csr;
  62.     //   unsigned int gpio_data;
  63.  
  64.     /* Read timer 0 CSR to see if it raised the interrupt */
  65.     csr = XTmrCtr_mGetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0);
  66.  
  67.     if (csr & XTC_CSR_INT_OCCURED_MASK) {
  68.            
  69.          waiting_for_timer =0;
  70.        
  71.         /* Clear the timer interrupt */
  72.         XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0, csr);
  73.     }
  74. }
  75.  
  76.  
  77.  
  78. /*---------------------------------------------------------------------------*/
  79. // show dotted decimal prints a dotted decimal address to the UART           */
  80. /*---------------------------------------------------------------------------*/
  81. static void show_dotted_decimal( unsigned char *address_array)
  82. {
  83.     int i;
  84.     unsigned char temp;
  85.  
  86.     for(i=0;i<4;i++)
  87.     {
  88.         temp = address_array[i];
  89.         if(i!=0) xil_printf(".");
  90.         xil_printf("%d", (int) temp);      
  91.     }
  92. }
  93.  
  94. /*---------------------------------------------------------------------------*/
  95. /* show dashed hex prints a dashed hex address to the UART                   */
  96. /*---------------------------------------------------------------------------*/
  97. static void show_dashed_hex( int bytes, char *address_array)
  98. {
  99.     //Assumes the caller passes the correct number of bytes
  100.     int bb;
  101.  
  102.     for(bb=0;bb<bytes;bb++)
  103.     {
  104.         if(bb !=0) xil_printf("-");
  105.         xil_printf("%02X", (int) address_array[bb]);
  106.     }
  107. }
  108.  
  109. /*---------------------------------------------------------------------------*/
  110. /* new_my_tmr - Called Periodically to dispatch TCP and ARP timers              
  111. */
  112. /*---------------------------------------------------------------------------*/
  113. void new_my_tmr(void)
  114. {
  115.     tcp_tmr();
  116.     etharp_tmr();
  117. }
  118.  
  119. void exc_illegal_op(void* arg) {
  120.     print ("Illegal Opcode Exception\r\n");
  121. }
  122.  
  123. void exc_iopb(void* arg) {
  124.     print ("IOPB Exception \r\n");
  125. }
  126.  
  127. void exc_dopb(void* arg) {
  128.     print ("DOPB Exception \r\n");
  129. }
  130.  
  131. void _stack_overflow_exit() {
  132.     print ("Stack Overflow Error \r\n");
  133. }
  134.  
  135. /*---------------------------------------------------------------------------*/
  136. static void
  137. TxTest_err(void *arg, err_t err)
  138. {
  139.     xil_printf("TxTest_err\r\n");    
  140. }
  141. /*---------------------------------------------------------------------------*/
  142. static void
  143. close_conn(struct tcp_pcb *pcb)
  144. {
  145.     //tcp_arg(pcb, NULL);
  146.     tcp_sent(pcb, NULL);  
  147.     tcp_recv(pcb, NULL);
  148.    
  149.     if (tcp_close(pcb) != ERR_OK)
  150.     print("[TxTest could not close] \r\n");
  151.     else
  152.     print("[TxTest_closed] \r\n");
  153. }
  154.  
  155. /*---------------------------------------------------------------------------*/
  156. static err_t
  157. TxTest_sent(void *arg, struct tcp_pcb *pcb, int len)
  158. {
  159.    
  160.     xil_printf("[TxTest_sent] ,  len: %d",  len);
  161.     isDataSent = 1;
  162.    
  163.    
  164.     return ERR_OK;
  165.  
  166. }  
  167.  
  168.  
  169. static err_t
  170. TxTest_received(void * arg, struct tcp_pcb * tpcb,
  171.                               struct pbuf * p, err_t err)
  172. {
  173.     /**** sauvgarde des donnees dans notre buffer principal ****/
  174.     /*  p est soius forme d'une list chainer : p->p1->p2->p3    */
  175.     int totalSize = p->tot_len;
  176.     int sum = 0;
  177.     /*while(p != NULL)
  178.     {
  179.         char *buffer = (char *)p->payload;
  180.         strncpy(buffRecv + sum, buffer, p->len);
  181.         sum += p->len;
  182.         p = p->next;
  183.     }*/
  184.     char *buffer = (char *)p->payload;
  185.     strncpy(buffRecv  , buffer, p->len);
  186.     buffSizeRecv = totalSize;
  187.     //buffRecv[buffSizeRecv] = '\0';
  188.    
  189.     tcp_recved(tpcb, totalSize);
  190.  
  191.     //xil_printf("\r [TxTest_received] ,  donner recu : %s, len : %d, len total : %d \r", buffRecv, buffSizeRecv, totalSize );
  192.    
  193.     /*if(isStreaming)
  194.     {   char *bufferDecoded;
  195.         int BufferDecodedSize = ap_base64decode(bufferDecoded,buffRecv,buffSizeRecv);
  196.         xil_printf("\r  taille decode :::  %d \r",  BufferDecodedSize );
  197.         isStreaming = 0;
  198.     }*/
  199.     isDataReceived = 1;
  200.      
  201.     return ERR_OK;
  202. }
  203.  
  204. /*---------------------------------------------------------------------------*/
  205. static err_t
  206. TxTest_connected(void *arg, struct tcp_pcb *pcb, err_t error)
  207. {
  208.     char *buf_p;
  209.     int i, len;
  210.     int stat, buflen;
  211.  
  212.     xil_printf("[TxTest_connected]\r\n");
  213.     tcp_setprio(pcb, TCP_PRIO_MIN);
  214.  
  215.     /* Tell TCP that this is the structure we wish to be passed for our
  216.     callbacks. */
  217.     //tcp_arg(pcb, ps);
  218.  
  219.     tcp_err(pcb, TxTest_err);
  220.  
  221.     tcp_sent(pcb, TxTest_sent);
  222.    
  223.     tcp_recv(pcb, TxTest_received);
  224.    
  225.     isConnected = 1;
  226.  
  227.  
  228.     return ERR_OK;
  229. }
  230. /*---------------------------------------------------------------------------*/
  231. void
  232. TxTest_init(void)
  233. {
  234.     struct ip_addr ipaddr;
  235.     struct ip_addr ipaddrLocal;
  236.     int stat;
  237.    
  238.     xil_printf("[TxTest_init]\r\n");
  239.     IP4_ADDR(&ipaddr, ipHost[0],ipHost[1],ipHost[2],ipHost[3]);
  240.     IP4_ADDR(&ipaddrLocal, ipLocal[0],ipLocal[1],ipLocal[2],ipLocal[3]);
  241.    
  242.     tx_pcb = tcp_new();
  243.     if ( !tx_pcb){
  244.         xil_printf("tcp_new fails\r\n");
  245.         return;
  246.     }
  247.     stat = tcp_bind(tx_pcb, &ipaddrLocal, 4097);
  248.     if (stat == ERR_MEM)
  249.     print("[TxTest_connect] memory error \r\n");
  250.     stat = tcp_connect(tx_pcb, &ipaddr, portHost, TxTest_connected);
  251.     if (stat == ERR_MEM)
  252.     print("[TxTest_connect] memory error \r\n");
  253.     //tcp_abort(tx_pcb);
  254. }
  255. void wait_for(int*  boolean)
  256. {
  257.     //xil_printf("Avant boucle!\r");
  258.     while(!(*boolean))
  259.     {
  260.         while (waiting_for_timer) {
  261.            // Call to check if there are any packets
  262.            xemacif_input(default_netif);
  263.          }
  264.          new_my_tmr();
  265.          waiting_for_timer = 1;
  266.     }
  267.     //xil_printf("Apres boucle!\r");
  268.  
  269. }
  270.  
  271. void send_msg(char* msg)
  272. {
  273.    isDataSent = 0;
  274.     size_t len = strlen(msg);
  275.     tcp_write(tx_pcb, msg, (u16_t)len, 1);
  276.     xil_printf("attendre la l'envoie des donnees ...\r");
  277.     wait_for(&isDataSent);
  278.     isDataSent = 0;
  279.     xil_printf(" donnees tres bien envoyer \r");
  280. }
  281. /*---------------------------------------------------------------------------*/
  282. /* main function                                                             */
  283. /*---------------------------------------------------------------------------*/
  284.  
  285.  
  286. int main (void) {
  287.     print("-- debut2 --\r");
  288.    
  289.     struct ip_addr ipaddr, netmask, gw;
  290.     XEmacIf_Config *xemacif_ptr = &XEmacIf_ConfigTable[0];
  291.  
  292.     /***     Enable and initialize cache    ***/
  293.     microblaze_init_icache_range(0, XPAR_MICROBLAZE_0_CACHE_BYTE_SIZE);
  294.     microblaze_enable_icache();
  295.     microblaze_init_dcache_range(0, XPAR_MICROBLAZE_0_DCACHE_BYTE_SIZE);
  296.     microblaze_enable_dcache();
  297.  
  298.     /***    Enable microblaze interrupts  ****/
  299.     microblaze_enable_interrupts();
  300.    
  301.     /*-------------------------------------------------------------------------
  302.     * Timer and Intc Init
  303.     *
  304.     * Set the Timer to interrupt for every 100ms
  305.     *------------------------------------------------------------------------*/
  306.     /* set the number of cycles the timer counts before interrupting */
  307.     /* 100 Mhz clock => .01us for 1 clk tick. For 100ms, 10000000 clk ticks need
  308. to elapse  */
  309.     XTmrCtr_mSetLoadReg(XPAR_OPB_TIMER_0_BASEADDR, 0, TCP_TMR_INTERVAL*100000);
  310.  
  311.     /* reset the timers, and clear interrupts */
  312.     XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0, XTC_CSR_INT_OCCURED_MASK | XTC_CSR_LOAD_MASK );
  313.  
  314.     /* start the timers */
  315.     XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0,
  316.     XTC_CSR_ENABLE_TMR_MASK | XTC_CSR_ENABLE_INT_MASK | XTC_CSR_AUTO_RELOAD_MASK |
  317.     XTC_CSR_DOWN_COUNT_MASK);
  318.  
  319.  
  320.     xil_printf("Starting Up...\r\n");
  321.    
  322.    
  323.     /*-----------------------------------------------------------------------*/
  324.     /* Do LWIP System Inits                                                  */
  325.     /*-----------------------------------------------------------------------*/
  326. #ifdef STATS
  327.     stats_init();
  328. #endif /* STATS */
  329.     xil_printf("Initializing Memory Structures.");
  330.     sys_init();
  331.     mem_init();
  332.     xil_printf(".");
  333.     memp_init();
  334.     xil_printf(".");
  335.     pbuf_init();
  336.     xil_printf(" done.\r\n");
  337.  
  338.  
  339.     /*-----------------------------------------------------------------------*/
  340.     /* Set host addresses                                                    */
  341.     /*-----------------------------------------------------------------------*/
  342.     xemacif_setmac(0, (u8_t *) fullmac);                        //Set MAC
  343.     IP4_ADDR(&gw, gateway[0],gateway[1],gateway[2],gateway[3]); //Set gateway
  344.     IP4_ADDR(&ipaddr, ipLocal[0],ipLocal[1],ipLocal[2],ipLocal[3]);                 //Set ip
  345.     IP4_ADDR(&netmask,subnet[0],subnet[1],subnet[2],subnet[3]); //Set subnet msk
  346.  
  347.     /*-----------------------------------------------------------------------*/
  348.     /* Show some host boot stuff and parameters                              */
  349.     /*-----------------------------------------------------------------------*/
  350.     xil_printf("\r\nStarting Network Interface...\r\n");
  351.     xil_printf("  MAC Address: "); show_dashed_hex(6, fullmac);  xil_printf("\r\n");
  352.     xil_printf("  IP Address: ");  show_dotted_decimal(ipLocal);      xil_printf("\r\n");
  353.     xil_printf("  Subnet Mask: "); show_dotted_decimal(subnet);  xil_printf("\r\n");
  354.     xil_printf("  Gateway IP: ");  show_dotted_decimal(gateway); xil_printf("\r\n");
  355.     xil_printf("  TxTest PortHost: %d  \r\n", portHost);
  356.  
  357.     /*-----------------------------------------------------------------------*/
  358.     /* Initialize netif                                                      */
  359.     /*-----------------------------------------------------------------------*/
  360.     netif_init();
  361.  
  362.     /*-----------------------------------------------------------------------*/
  363.     /* Initialize TCP Stack                                                  */
  364.     /*-----------------------------------------------------------------------*/
  365.     tcp_init();
  366.  
  367.     /*-----------------------------------------------------------------------*/
  368.     /* Set up the lwIP network interface...                                  */
  369.     /*-----------------------------------------------------------------------*/
  370.     /* allocate netif structure */  
  371.     default_netif = mem_malloc(sizeof(struct netif));
  372.  
  373.     if (default_netif == NULL) {
  374.         print("netif_add(): out of memory for default_netif\n");
  375.         return 1;
  376.     }
  377.     default_netif = netif_add( default_netif,
  378.     &ipaddr,
  379.     &netmask,
  380.     &gw,
  381.     &XEmacIf_ConfigTable[0],
  382.     xemacif_init,
  383.     ip_input
  384.     );
  385.  
  386.     netif_set_default(default_netif);
  387.  
  388.  
  389.     /* Register XEmacHandler with interrupt controller and enable interrupts */
  390.     XIntc_RegisterHandler(XPAR_OPB_INTC_0_BASEADDR,
  391.     XPAR_OPB_INTC_0_ETHERNET_MAC_IP2INTC_IRPT_INTR,
  392.     (XInterruptHandler)XEmac_IntrHandlerFifo,
  393.     xemacif_ptr->instance_ptr);
  394.    
  395.     XIntc_RegisterHandler(XPAR_OPB_INTC_0_BASEADDR,
  396.     XPAR_OPB_INTC_0_OPB_TIMER_0_INTERRUPT_INTR,
  397.     (XInterruptHandler)mytimer_int_handler,
  398.     xemacif_ptr->instance_ptr);
  399.  
  400.  
  401.     /* Start the interrupt controller */
  402.     XIntc_mMasterEnable(XPAR_OPB_INTC_0_BASEADDR);
  403.  
  404.     /* Enable timer and EMAC interrupts in the interrupt controller */
  405.     XIntc_mEnableIntr(XPAR_OPB_INTC_0_BASEADDR,
  406.     XPAR_ETHERNET_MAC_IP2INTC_IRPT_MASK | XPAR_OPB_TIMER_0_INTERRUPT_MASK);
  407.  
  408.     /*-----------------------------------------------------------------------*/
  409.     /* create new tcp pcb and start applications                             */
  410.     /*-----------------------------------------------------------------------*/
  411.  
  412.     // Start the Client
  413.     xil_printf("TxTest Client Running ... "); xil_printf("\r\n");
  414.     TxTest_init();
  415.  
  416.  
  417.     xil_printf("attendre la connexion...\r");
  418.     wait_for(&isConnected);
  419.     xil_printf("\rconnexion etablie \r");
  420.    
  421.     // envoyer le numero du fichier a telecharger
  422.     send_msg("2");
  423.    
  424.     /*********  recevoir la dimension du fichier (decode)  *********/
  425.     wait_for(&isDataReceived);
  426.     isDataReceived = 0;
  427.     int tailleFichierDecoded = atoi(buffRecv);
  428.     xil_printf("\rtaille fichier decode : %d\r\r\r", tailleFichierDecoded);
  429.    
  430.     send_msg("ok");
  431.  
  432.     /*********  recevoir la dimension du fichier a recedoir (encode)  *********/
  433.     wait_for(&isDataReceived);
  434.     isDataReceived = 0;
  435.     int tailleFichierCoded = atoi(buffRecv);
  436.     xil_printf("\rtaille fichier  code : %d\r\r\r", tailleFichierCoded);
  437.    
  438.     send_msg("ok");
  439.    
  440.     //void* output_file = sysace_fopen ("chanson.mp3", "w");
  441.    
  442.     int tailleRest = tailleFichierCoded;
  443.     while( tailleRest > 0 )
  444.     {
  445.         /*********  recevoir la taille de la premiere portion du fichier  *********/
  446.         isStreaming = 0;
  447.  
  448.         // recevoir la taille de la portion
  449.         wait_for(&isDataReceived);
  450.         isDataReceived = 0;
  451.         isStreaming = 1;
  452.        
  453.         send_msg("ok");
  454.        
  455.    
  456.         /*********  recevoir le stream, il faut maintenant le décoder *********/
  457.        
  458.  
  459.         // recevoir les donnees
  460.         wait_for(&isDataReceived);
  461.        
  462.         // decoder les donnees et l'ecrire sur la compact flash
  463.         char *bufferDecoded;
  464.         int BufferDecodedSize = ap_base64decode(bufferDecoded,buffRecv,buffSizeRecv);
  465.     //  sysace_fwrite (bufferDecoded, 1, BufferDecodedSize, output_file);
  466.        
  467.         isDataReceived = 0;
  468.         isStreaming = 0;
  469.         tailleRest -= buffSizeRecv;
  470.         xil_printf(" --------- tailleRest :  %d ----------\r", tailleRest);
  471.        
  472.         send_msg("ok");
  473.        
  474.     }
  475.    
  476.     //sysace_fclose (output_file);
  477.     close_conn(tx_pcb );
  478.    
  479.     return 0;
  480.  
  481. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement