Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Located in: microblaze_0/include/xparameters.h
- #include "xuartlite.h"
- #include "xparameters.h"
- #include "xintc.h"
- #include "xtmrctr_l.h"
- #include "stdio.h"
- #include "stdlib.h"
- #include "xutil.h"
- #include "base64decode.h"
- // System Ace
- #include "xsysace.h"
- #include "sysace_stdio.h"
- /* lwIP Includes */
- #include "netif/xemacif.h"
- #include "lwip/tcp.h"
- #include "lwip/memp.h"
- #include "lwip/mem.h"
- #include "lwip/pbuf.h"
- #include "netif/etharp.h"
- #include "lwipopts.h"
- //====================================================
- #define BUFF_SIZE 1024
- /** variables Golobal **/
- int portHost = 9010;
- struct tcp_pcb *tx_pcb;
- char ipLocal[4] = {132,207,89,157}; // l adresse ip local
- char ipHost[4] = {132,207,89,02};
- char subnet[4] = {255,255,255,0};
- char gateway[4] = {132,207,89,0};
- char fullmac[6] = {0x00, 0x00, 0x0B, 0x35,0x22,0x89};
- char buffRecv[BUFF_SIZE]; // tampon des donnees recus
- int buffSizeRecv = 0;
- struct netif *default_netif;
- /****** variables d'etat *******/
- int isConnected = 0;
- int isDataSent = 0;
- int isDataReceived = 0;
- int isStreaming = 0;
- int waiting_for_timer = 1;
- /* defined in EDK generated xemacif_g.c file */
- extern XEmacIf_Config XEmacIf_ConfigTable[];
- /**
- CallBack function
- **/
- void mytimer_int_handler (void* baseaddr_p) {
- //xil_printf("Timer Handler ...\n\t");
- int baseaddr = *(int *)baseaddr_p;
- unsigned int csr;
- // unsigned int gpio_data;
- /* Read timer 0 CSR to see if it raised the interrupt */
- csr = XTmrCtr_mGetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0);
- if (csr & XTC_CSR_INT_OCCURED_MASK) {
- waiting_for_timer =0;
- /* Clear the timer interrupt */
- XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0, csr);
- }
- }
- /*---------------------------------------------------------------------------*/
- // show dotted decimal prints a dotted decimal address to the UART */
- /*---------------------------------------------------------------------------*/
- static void show_dotted_decimal( unsigned char *address_array)
- {
- int i;
- unsigned char temp;
- for(i=0;i<4;i++)
- {
- temp = address_array[i];
- if(i!=0) xil_printf(".");
- xil_printf("%d", (int) temp);
- }
- }
- /*---------------------------------------------------------------------------*/
- /* show dashed hex prints a dashed hex address to the UART */
- /*---------------------------------------------------------------------------*/
- static void show_dashed_hex( int bytes, char *address_array)
- {
- //Assumes the caller passes the correct number of bytes
- int bb;
- for(bb=0;bb<bytes;bb++)
- {
- if(bb !=0) xil_printf("-");
- xil_printf("%02X", (int) address_array[bb]);
- }
- }
- /*---------------------------------------------------------------------------*/
- /* new_my_tmr - Called Periodically to dispatch TCP and ARP timers
- */
- /*---------------------------------------------------------------------------*/
- void new_my_tmr(void)
- {
- tcp_tmr();
- etharp_tmr();
- }
- void exc_illegal_op(void* arg) {
- print ("Illegal Opcode Exception\r\n");
- }
- void exc_iopb(void* arg) {
- print ("IOPB Exception \r\n");
- }
- void exc_dopb(void* arg) {
- print ("DOPB Exception \r\n");
- }
- void _stack_overflow_exit() {
- print ("Stack Overflow Error \r\n");
- }
- /*---------------------------------------------------------------------------*/
- static void
- TxTest_err(void *arg, err_t err)
- {
- xil_printf("TxTest_err\r\n");
- }
- /*---------------------------------------------------------------------------*/
- static void
- close_conn(struct tcp_pcb *pcb)
- {
- //tcp_arg(pcb, NULL);
- tcp_sent(pcb, NULL);
- tcp_recv(pcb, NULL);
- if (tcp_close(pcb) != ERR_OK)
- print("[TxTest could not close] \r\n");
- else
- print("[TxTest_closed] \r\n");
- }
- /*---------------------------------------------------------------------------*/
- static err_t
- TxTest_sent(void *arg, struct tcp_pcb *pcb, int len)
- {
- xil_printf("[TxTest_sent] , len: %d", len);
- isDataSent = 1;
- return ERR_OK;
- }
- static err_t
- TxTest_received(void * arg, struct tcp_pcb * tpcb,
- struct pbuf * p, err_t err)
- {
- /**** sauvgarde des donnees dans notre buffer principal ****/
- /* p est soius forme d'une list chainer : p->p1->p2->p3 */
- int totalSize = p->tot_len;
- int sum = 0;
- /*while(p != NULL)
- {
- char *buffer = (char *)p->payload;
- strncpy(buffRecv + sum, buffer, p->len);
- sum += p->len;
- p = p->next;
- }*/
- char *buffer = (char *)p->payload;
- strncpy(buffRecv , buffer, p->len);
- buffSizeRecv = totalSize;
- //buffRecv[buffSizeRecv] = '\0';
- tcp_recved(tpcb, totalSize);
- //xil_printf("\r [TxTest_received] , donner recu : %s, len : %d, len total : %d \r", buffRecv, buffSizeRecv, totalSize );
- /*if(isStreaming)
- { char *bufferDecoded;
- int BufferDecodedSize = ap_base64decode(bufferDecoded,buffRecv,buffSizeRecv);
- xil_printf("\r taille decode ::: %d \r", BufferDecodedSize );
- isStreaming = 0;
- }*/
- isDataReceived = 1;
- return ERR_OK;
- }
- /*---------------------------------------------------------------------------*/
- static err_t
- TxTest_connected(void *arg, struct tcp_pcb *pcb, err_t error)
- {
- char *buf_p;
- int i, len;
- int stat, buflen;
- xil_printf("[TxTest_connected]\r\n");
- tcp_setprio(pcb, TCP_PRIO_MIN);
- /* Tell TCP that this is the structure we wish to be passed for our
- callbacks. */
- //tcp_arg(pcb, ps);
- tcp_err(pcb, TxTest_err);
- tcp_sent(pcb, TxTest_sent);
- tcp_recv(pcb, TxTest_received);
- isConnected = 1;
- return ERR_OK;
- }
- /*---------------------------------------------------------------------------*/
- void
- TxTest_init(void)
- {
- struct ip_addr ipaddr;
- struct ip_addr ipaddrLocal;
- int stat;
- xil_printf("[TxTest_init]\r\n");
- IP4_ADDR(&ipaddr, ipHost[0],ipHost[1],ipHost[2],ipHost[3]);
- IP4_ADDR(&ipaddrLocal, ipLocal[0],ipLocal[1],ipLocal[2],ipLocal[3]);
- tx_pcb = tcp_new();
- if ( !tx_pcb){
- xil_printf("tcp_new fails\r\n");
- return;
- }
- stat = tcp_bind(tx_pcb, &ipaddrLocal, 4097);
- if (stat == ERR_MEM)
- print("[TxTest_connect] memory error \r\n");
- stat = tcp_connect(tx_pcb, &ipaddr, portHost, TxTest_connected);
- if (stat == ERR_MEM)
- print("[TxTest_connect] memory error \r\n");
- //tcp_abort(tx_pcb);
- }
- void wait_for(int* boolean)
- {
- //xil_printf("Avant boucle!\r");
- while(!(*boolean))
- {
- while (waiting_for_timer) {
- // Call to check if there are any packets
- xemacif_input(default_netif);
- }
- new_my_tmr();
- waiting_for_timer = 1;
- }
- //xil_printf("Apres boucle!\r");
- }
- void send_msg(char* msg)
- {
- isDataSent = 0;
- size_t len = strlen(msg);
- tcp_write(tx_pcb, msg, (u16_t)len, 1);
- xil_printf("attendre la l'envoie des donnees ...\r");
- wait_for(&isDataSent);
- isDataSent = 0;
- xil_printf(" donnees tres bien envoyer \r");
- }
- /*---------------------------------------------------------------------------*/
- /* main function */
- /*---------------------------------------------------------------------------*/
- int main (void) {
- print("-- debut2 --\r");
- struct ip_addr ipaddr, netmask, gw;
- XEmacIf_Config *xemacif_ptr = &XEmacIf_ConfigTable[0];
- /*** Enable and initialize cache ***/
- microblaze_init_icache_range(0, XPAR_MICROBLAZE_0_CACHE_BYTE_SIZE);
- microblaze_enable_icache();
- microblaze_init_dcache_range(0, XPAR_MICROBLAZE_0_DCACHE_BYTE_SIZE);
- microblaze_enable_dcache();
- /*** Enable microblaze interrupts ****/
- microblaze_enable_interrupts();
- /*-------------------------------------------------------------------------
- * Timer and Intc Init
- *
- * Set the Timer to interrupt for every 100ms
- *------------------------------------------------------------------------*/
- /* set the number of cycles the timer counts before interrupting */
- /* 100 Mhz clock => .01us for 1 clk tick. For 100ms, 10000000 clk ticks need
- to elapse */
- XTmrCtr_mSetLoadReg(XPAR_OPB_TIMER_0_BASEADDR, 0, TCP_TMR_INTERVAL*100000);
- /* reset the timers, and clear interrupts */
- XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0, XTC_CSR_INT_OCCURED_MASK | XTC_CSR_LOAD_MASK );
- /* start the timers */
- XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0,
- XTC_CSR_ENABLE_TMR_MASK | XTC_CSR_ENABLE_INT_MASK | XTC_CSR_AUTO_RELOAD_MASK |
- XTC_CSR_DOWN_COUNT_MASK);
- xil_printf("Starting Up...\r\n");
- /*-----------------------------------------------------------------------*/
- /* Do LWIP System Inits */
- /*-----------------------------------------------------------------------*/
- #ifdef STATS
- stats_init();
- #endif /* STATS */
- xil_printf("Initializing Memory Structures.");
- sys_init();
- mem_init();
- xil_printf(".");
- memp_init();
- xil_printf(".");
- pbuf_init();
- xil_printf(" done.\r\n");
- /*-----------------------------------------------------------------------*/
- /* Set host addresses */
- /*-----------------------------------------------------------------------*/
- xemacif_setmac(0, (u8_t *) fullmac); //Set MAC
- IP4_ADDR(&gw, gateway[0],gateway[1],gateway[2],gateway[3]); //Set gateway
- IP4_ADDR(&ipaddr, ipLocal[0],ipLocal[1],ipLocal[2],ipLocal[3]); //Set ip
- IP4_ADDR(&netmask,subnet[0],subnet[1],subnet[2],subnet[3]); //Set subnet msk
- /*-----------------------------------------------------------------------*/
- /* Show some host boot stuff and parameters */
- /*-----------------------------------------------------------------------*/
- xil_printf("\r\nStarting Network Interface...\r\n");
- xil_printf(" MAC Address: "); show_dashed_hex(6, fullmac); xil_printf("\r\n");
- xil_printf(" IP Address: "); show_dotted_decimal(ipLocal); xil_printf("\r\n");
- xil_printf(" Subnet Mask: "); show_dotted_decimal(subnet); xil_printf("\r\n");
- xil_printf(" Gateway IP: "); show_dotted_decimal(gateway); xil_printf("\r\n");
- xil_printf(" TxTest PortHost: %d \r\n", portHost);
- /*-----------------------------------------------------------------------*/
- /* Initialize netif */
- /*-----------------------------------------------------------------------*/
- netif_init();
- /*-----------------------------------------------------------------------*/
- /* Initialize TCP Stack */
- /*-----------------------------------------------------------------------*/
- tcp_init();
- /*-----------------------------------------------------------------------*/
- /* Set up the lwIP network interface... */
- /*-----------------------------------------------------------------------*/
- /* allocate netif structure */
- default_netif = mem_malloc(sizeof(struct netif));
- if (default_netif == NULL) {
- print("netif_add(): out of memory for default_netif\n");
- return 1;
- }
- default_netif = netif_add( default_netif,
- &ipaddr,
- &netmask,
- &gw,
- &XEmacIf_ConfigTable[0],
- xemacif_init,
- ip_input
- );
- netif_set_default(default_netif);
- /* Register XEmacHandler with interrupt controller and enable interrupts */
- XIntc_RegisterHandler(XPAR_OPB_INTC_0_BASEADDR,
- XPAR_OPB_INTC_0_ETHERNET_MAC_IP2INTC_IRPT_INTR,
- (XInterruptHandler)XEmac_IntrHandlerFifo,
- xemacif_ptr->instance_ptr);
- XIntc_RegisterHandler(XPAR_OPB_INTC_0_BASEADDR,
- XPAR_OPB_INTC_0_OPB_TIMER_0_INTERRUPT_INTR,
- (XInterruptHandler)mytimer_int_handler,
- xemacif_ptr->instance_ptr);
- /* Start the interrupt controller */
- XIntc_mMasterEnable(XPAR_OPB_INTC_0_BASEADDR);
- /* Enable timer and EMAC interrupts in the interrupt controller */
- XIntc_mEnableIntr(XPAR_OPB_INTC_0_BASEADDR,
- XPAR_ETHERNET_MAC_IP2INTC_IRPT_MASK | XPAR_OPB_TIMER_0_INTERRUPT_MASK);
- /*-----------------------------------------------------------------------*/
- /* create new tcp pcb and start applications */
- /*-----------------------------------------------------------------------*/
- // Start the Client
- xil_printf("TxTest Client Running ... "); xil_printf("\r\n");
- TxTest_init();
- xil_printf("attendre la connexion...\r");
- wait_for(&isConnected);
- xil_printf("\rconnexion etablie \r");
- // envoyer le numero du fichier a telecharger
- send_msg("2");
- /********* recevoir la dimension du fichier (decode) *********/
- wait_for(&isDataReceived);
- isDataReceived = 0;
- int tailleFichierDecoded = atoi(buffRecv);
- xil_printf("\rtaille fichier decode : %d\r\r\r", tailleFichierDecoded);
- send_msg("ok");
- /********* recevoir la dimension du fichier a recedoir (encode) *********/
- wait_for(&isDataReceived);
- isDataReceived = 0;
- int tailleFichierCoded = atoi(buffRecv);
- xil_printf("\rtaille fichier code : %d\r\r\r", tailleFichierCoded);
- send_msg("ok");
- //void* output_file = sysace_fopen ("chanson.mp3", "w");
- int tailleRest = tailleFichierCoded;
- while( tailleRest > 0 )
- {
- /********* recevoir la taille de la premiere portion du fichier *********/
- isStreaming = 0;
- // recevoir la taille de la portion
- wait_for(&isDataReceived);
- isDataReceived = 0;
- isStreaming = 1;
- send_msg("ok");
- /********* recevoir le stream, il faut maintenant le décoder *********/
- // recevoir les donnees
- wait_for(&isDataReceived);
- // decoder les donnees et l'ecrire sur la compact flash
- char *bufferDecoded;
- int BufferDecodedSize = ap_base64decode(bufferDecoded,buffRecv,buffSizeRecv);
- // sysace_fwrite (bufferDecoded, 1, BufferDecodedSize, output_file);
- isDataReceived = 0;
- isStreaming = 0;
- tailleRest -= buffSizeRecv;
- xil_printf(" --------- tailleRest : %d ----------\r", tailleRest);
- send_msg("ok");
- }
- //sysace_fclose (output_file);
- close_conn(tx_pcb );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement