2025-01-01 01:52 AM
Hello,
I am currently working with the STM32H7S78-DK board, using STM32CubeIDE for programming. My goal is to configure the Ethernet peripheral and send/receive data via UDP. As a reference, I am following the example project provided at the following link:
stm32-upd-client
Here is my implementation,
void udp_receive_callback_client(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
static void udpClient_send(void);
struct udp_pcb *upcb;
char buffer[100];
int counter = 0;
extern TIM_HandleTypeDef htim1;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
printf("HAL_TIM_PeriodElapsedCallback\n\r");
udpClient_send();
}
/* IMPLEMENTATION FOR UDP CLIENT : source:https://www.geeksforgeeks.org/udp-server-client-implementation-c/
1. Create UDP socket.
2. Send message to server.
3. Wait until response from server is received.
4. Process reply and go back to step 2, if necessary.
5. Close socket descriptor and exit.
*/
void udpClient_connect(void)
{
printf("udpClient_connect\n\r");
err_t err;
/* 1. Create a new UDP control block */
upcb = udp_new();
/* Bind the block to module's IP and port */
ip_addr_t myIPaddr;
IP_ADDR4(&myIPaddr, 192, 168, 15, 100);
err = udp_bind(upcb, &myIPaddr, 5000);
if (err == ERR_OK)
{
printf("udp_bind successfully \n\r");
printf("Bound to IP: %s, Port: %d\n\r", ip4addr_ntoa(&upcb->local_ip), upcb->local_port);
}
/* configure destination IP address and port */
ip_addr_t DestIPaddr;
IP_ADDR4(&DestIPaddr, 192, 168, 15, 13);
err = udp_connect(upcb, &DestIPaddr, 8);
if (err == ERR_OK)
{
printf("udpClient_send\n\r");
/* 2. Send message to server */
udpClient_send ();
/* 3. Set a receive callback for the upcb */
udp_recv(upcb, udp_receive_callback_client, NULL);
}
}
static void udpClient_send(void)
{
struct pbuf *txBuf;
char data[100];
err_t err;
int len = sprintf(data, "sending UDP client message %d", counter);
/* allocate pbuf from pool*/
txBuf = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
if (txBuf != NULL)
{
/* copy data to pbuf */
pbuf_take(txBuf, data, len);
/* send udp data */
err = udp_send(upcb, txBuf);
if(err != ERR_OK)
{
printf("udp_send fails with err : %d\n\r",err);
}
else
{
printf("udpClient_send (%s)\n\r",(char *)data);
}
/* free pbuf */
pbuf_free(txBuf);
}
}
void udp_receive_callback_client(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
printf("udp_receive_client_callback_\n\r");
/* Copy the data from the pbuf */
strncpy (buffer, (char *)p->payload, p->len);
/*increment message count */
counter++;
/* Free receive pbuf */
pbuf_free(p);
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* Enable the CPU Cache */
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();
/* MCU Configuration--------------------------------------------------------*/
/* Update SystemCoreClock variable according to RCC registers values. */
SystemCoreClockUpdate();
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_FLASH_Init();
MX_UART4_Init();
MX_LWIP_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim1);
/* USER CODE BEGIN 2 */
UART_Print("Hello from STM32 from UART4!\n\r");
printf("Hello_STM32 from printf function!\n\r");
udpClient_connect();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
MX_LWIP_Process();
}
/* USER CODE END 3 */
}
However, I am facing an issue:
- When I first call the udpClient_connect function from the main function and attempt to send data using the udp_send function, it returns an error code -4.
- After the first attempt, subsequent calls to udp_send do not return errors, and the function executes successfully.
- I am sending data at 1-second intervals, but I am unable to see the data on the Hercules tool.
Here is screenshot of the debug log printed on Docklight.
I have attached my project for your reference.
Could you please help me understand why the initial error occurs and why data is not visible in the Hercules tool?
Thank you for your assistance.
Best regards,
Mehul
Solved! Go to Solution.
2025-01-03 08:20 PM
Hi @STea ,
Thank you for your support! I have resolved the issue. The problem was on the hardware side of the STM32H7S78-DK Discovery board. To enable Ethernet functionality, modifications were required in the default solder bridge configuration:
These adjustments resolved the issue successfully.
Best regards,
Mehul
2025-01-01 06:24 AM
While waiting for more helpful responses, check here if some local expert can assist you.
2025-01-03 06:37 AM
Hello @Mehulrana511 ,
I'll be checking your code and I'll get back to you as soon as possible.
Regards
2025-01-03 08:20 PM
Hi @STea ,
Thank you for your support! I have resolved the issue. The problem was on the hardware side of the STM32H7S78-DK Discovery board. To enable Ethernet functionality, modifications were required in the default solder bridge configuration:
These adjustments resolved the issue successfully.
Best regards,
Mehul