Part Number: HERCULES_SAFETY_MCU_DEMOS
Hello all,
I'm using the LwIP stack to implement UDP communication using the Raw API and no OS. What is the best practice to extract and and handle received UDP data? Below is a short code snippet that initializes the UDP socket, and sets the reception callback. The callback copies the data from the pbuf to a local buffer, and then the idea is to process the data from the buffer from a loop called in main by getting a pointer to the rxData buffer. I am worried that:
A. The data may not be processed fast enough before new data arrives
B. Once new data arrives, it will overwrite the buffer before it's fully processed, this will cause my data to be all mixed up (I can put a mutex on the receive buffer, but I'm worried I'll be loosing some received data)
In general, it is a good idea to process the data outside of the callback, correct? Since it's technically part of the ISR... correct me if I am wrong, otherwise I can do all I need to do inside the callback
How's this typically handled? Is there a better way of doing what I'm trying to do? Any examples?
static uint16_t rxLength = 0; static uint8_t rxData[4096]; void EMAC_LwIP_Main (uint8_t * macAddress) { unsigned int ipAddr; /* Enable the interrupt generation in CPSR register */ IntMasterIRQEnable(); _enable_FIQ(); /* Initialze the lwIP library, using static address.*/ uint8 ip_addr[4] = { 192, 168, 2, 44 }; uint8 netmask[4] = { 255, 255, 255, 0 }; uint8 gateway[4] = { 192, 168, 2, 254 }; ipAddr = lwIPInit(0, macAddress, *((uint32_t *)ip_addr), *((uint32_t *)netmask), *((uint32_t *)gateway), IPADDR_USE_STATIC); struct udp_pcb * mypcb; mypcb = udp_new(); if (udp_bind(mypcb, IP_ADDR_ANY, 8760) == ERR_OK) { /* Set a receive callback for the pcb */ udp_recv(mypcb, udp_receive_callback, NULL); } else { return; } /* Loop forever. All the work is done in interrupt handlers. */ while(1) { ProcessRxData(); } } void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port) { /* Copy data to local buffer */ if (p != NULL && p->tot_len != 0) { rxLength = pbuf_copy_partial(p, rxData, p->tot_len, 0); } } void ProcessRxData(void) { // Process RX data here... may be a long routine }