I need help regarding the lwip stack with tiva c, I am building UDP to serial converter, the function is working correctly, but I have some concerns:
- When I receive the UDP packet buffer, I directly echoed it back if UART is busy sending the previous UDP, is this correct because I am not sure what is the type of the allocated buffer?
- When UART is not sending, I put the data on array and send it to the UART function, is there any other efficient way to perform this ? i.e using ring buffer or any other techniques?
the code for udp callback function for receiving packets:
void Udp_Receive(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port) {
uint8_t *pui8Data;
uint32_t ui32Idx;
//
// If packet more than 128 bytes drop it
//
if((p->len) > 128)
{
pbuf_free(p);
return;
}
//
// check UART and if it is sending just echo the UDP
//
if(uart_status == 1 )
{
//
// Send the echoed message.
//
udp_sendto(pcb, p, addr, 1234);
pbuf_free(p);
}
else
{
//
// Received Packet information and send it to serial
//
udp_length = (p->len);
pui8Data = p->payload;
udp_to_serial_counter++;
//
// clear the payload array to receive new one.
//
for(ui32Idx = 0; ui32Idx < udp_length ; ui32Idx++)
{
udp_payload[ui32Idx] = 0;
}
//
// fill the payload from buf into the array
//
for(ui32Idx = 0; ui32Idx < udp_length ; ui32Idx++)
{
udp_payload[ui32Idx] = pui8Data[ui32Idx];
}
pbuf_free(p);
//
// Allocate a new pbuf for sending the response.
//
p = pbuf_alloc(PBUF_TRANSPORT,udp_length, PBUF_RAM);
if(p == NULL)
{
return;
}
//
// Copy the the received packet payload into the udp_payload
//
p->payload = udp_payload ;
p->len = udp_length ;
//
// Send the echoed message.
//
udp_sendto(pcb, p, addr, 1234);
pbuf_free(p);
set_uart_send_status();
}
The uart send function is :
UARTSend( uint8_t *pui8Buffer, uint32_t ui32Count) { // Loop while there are more characters to send. while(ui32Count--) {
while(!(ROM_UARTSpaceAvail(UART6_BASE)));
ROM_UARTCharPutNonBlocking(UART6_BASE, *pui8Buffer++);
}
while((ROM_UARTBusy(UART6_BASE)));
}