---
title: "Setting Up UDP Multicast on W55RP20-EVB-PICO !"
url: "https://maker.wiznet.io/Grace_Koo/projects/setting-up-udp-multicast-on-w55rp20-evb-pico-/"
markdown_url: "https://maker.wiznet.io/Grace_Koo/projects/setting-up-udp-multicast-on-w55rp20-evb-pico-/md"
type: "WCC: WIZnet Created Content"
author: "Grace"
author_url: "https://maker.wiznet.io/Grace_Koo/"
original_author: "Grace Koo"
published: "2024-12-12"
language: "en"
tags: ["W55RP20", "C/C++"]
hardware: ["WIZnet W55RP20-EVB-Pico"]
likes: 0
views: 2837
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Setting Up UDP Multicast on W55RP20-EVB-PICO !

> Setting Up UDP Multicast on W55RP20-EVB-PICO

Original author: Grace Koo

## Components

- **WIZnet W55RP20-EVB-Pico** x 1 ([docs](https://wiznet.io/products/powered-by-raspberry-pi/w55rp20-evb-pico))

## Documents and links

- [UDP multicast receiver](https://github.com/WIZnet-ioNIC/WIZnet-PICO-C/tree/main/examples/udp_multicast/udp_multicast_receiver) (code)

## Article

UDP Multicast is a communication method that sends a single data packet to multiple receivers in a **multicast group**, using the network more efficiently than unicast or broadcast.

- **Key Features:**

1. Efficient data distribution to multiple receivers.

2. No acknowledgment or retransmission (best effort).

- **Common Use Cases:**

1. **Streaming Media:** Live video or audio broadcasting (e.g., IPTV).

2. **Gaming:** Real-time multiplayer game updates.

3. **Financial Systems:** Market data distribution.

4. **IoT Systems:** Device synchronization or firmware updates.

5. **Disaster Alert Systems:** Emergency notifications to multiple devices

- **IPv4 Multicast Address Range : 224.0.0.0 ~ 239.255.255.255**
  This range is reserved for multicast communication and can be divided as follows:

1. **224.0.0.0 ~ 224.0.0.255**: **Local Network Multicast**
   Limited to the same subnet and does not traverse routers.

2. **239.0.0.0 ~ 239.255.255.255**: **Organization-specific Multicast**
   Used within private networks and can be configured like unicast addresses.

#### Set up

In this WCC, the **W55RP20-EVB-Pico** will function as the UDP multicast sender, so several socket configurations will be made.

The receivers used include the **W5500-EVB-Pico**, **W5100S-EVB-Pico**, **W5500-EVB-Pico2**, and a **smartphone**.

Additionally, since the multicast will be performed within the local network, the **TTL** is set to 1,

```c
setSn_TTL(SOCKET_ID, 1);                // set TTL (1 : local)
```

and the address is configured in the **224.0.0.1** range.

```c
static uint8_t multicast_ip[4] = {224, 0, 0, 6}; // multicast ip address
static uint16_t multicast_port = 29999;          // multicast port
```

The multicast IP and port configured are set as the destination as follows.

```c
setSn_DIPR(SOCKET_ID, multicast_ip);          // destination IP 설정
setSn_DPORT(SOCKET_ID, multicast_port);  // destination port 설정
```

Multicast differs from standard ARP(Address Resolution Protocol) usage in that a specific IP address is shared by multiple receivers. Since multicast IP addresses are directly converted to MAC addresses, ARP is not required.

Therefore, the MAC address is converted as follows

```c
uint8_t multicast_mac[6] = {
        0x01, 0x00, 0x5e,         // First 3 bytes of the multicast MAC address
        multicast_ip[2] & 0x7f,   // Last 23 bits of the IP address(first 7 bits of 224~239 : 1)
        multicast_ip[3],          // Last 2 bytes of the IP address
        0x00                      // Remaining byte is set to 0
    };
```

and configured accordingly.

```c
// Set the MAC address (DHAR: Socket n Destination Hardware Address Register)
setSn_DHAR(SOCKET_ID, multicast_mac);

// Skip ARP (Address Resolution Protocol) and directly set the MAC address
setSn_CR(SOCKET_ID, Sn_CR_SEND_MAC);
```

The execution screen of the sender is as follows. When a rising edge is detected on a specific GPIO, the message is sent.

![](https://maker.wiznet.io/upload/ckeditor5/223655954%5F1734002233%2EPNG)

The UDP multicast receiver was implemented with reference to the code below, and the smartphone was connected to the same Wi-Fi network, using the tester app for testing.

> <https://github.com/WIZnet-ioNIC/WIZnet-PICO-C/tree/main/examples/udp_multicast/udp_multicast_receiver>

The following screen shows the receiver's display.

![](https://maker.wiznet.io/upload/ckeditor5/223655954%5F1734012425%2EPNG)

The video showing both the sender and receivers together is as follows.

[Embedded media](https://www.youtube.com/embed/PluWpFneIqU)

```c
// UDP multicast sender

#include <stdio.h>
#include "port_common.h"

#include "wizchip_conf.h"
#include "w5x00_spi.h"
#include "w5x00_gpio_irq.h"
#include "socket.h"
#include "loopback.h"
#include "multicast.h"
#include "timer.h"

/* Clock */
#define PLL_SYS_KHZ (133 * 1000)

/* Buffer */
#define SOCKET_ID 1                      // Socket number
#define ETHERNET_BUF_MAX_SIZE (1024 * 2) // Send and receive cache size

/* GPIO  */
static void gpio_callback(void);

/* Timer  */
static void repeating_timer_callback(void);
static time_t millis(void);
wiz_NetInfo net_info = {
    .mac = {0x00, 0x08, 0xdc, 0x16, 0xed, 0x2e}, // Define MAC variables
    .ip = {192, 168, 11, 21},                    // Define IP variables
    .sn = {255, 255, 255, 0},                    // Define subnet variables
    .gw = {192, 168, 11, 1},                     // Define gateway variables
    .dns = {168, 126, 63, 1},                    // Define DNS  variables
    .dhcp = NETINFO_STATIC};                     // Define the DNCP mode

static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
    0,
};
uint8_t data[] = "Warning! GPIO14 state is HIGH!";
static uint8_t multicast_ip[4] = {224, 0, 0, 6}; // multicast ip address
static uint16_t multicast_port = 29999;          // multicast port
int flag = 0;
volatile uint32_t prev_state = 0;  
volatile uint32_t current_state = 0;
uint32_t start_ms = 0;
static volatile uint32_t g_msec_cnt = 0;

static void set_clock_khz(void);

int main()
{
    uint16_t local_port = 5000;

	uint8_t multicast_mac[6] = {
        0x01, 0x00, 0x5e,         // First 3 bytes of the multicast MAC address
        multicast_ip[2] & 0x7f,   // Last 23 bits of the IP address(first 7 bits of 224~239 : 1)
        multicast_ip[3],          // Last 2 bytes of the IP address
        0x00                      // Remaining byte is set to 0
    };
    gpio_init(14);
    gpio_init(15);
    gpio_set_dir(14, false);
    gpio_set_dir(15, true);
    gpio_put(15, 1);

    set_clock_khz();

    /*mcu init*/
    stdio_init_all(); // Initialize the main control peripheral.
    wizchip_spi_initialize();
    wizchip_cris_initialize();
    wizchip_reset();
    wizchip_initialize(); // spi initialization
    wizchip_check();

    wizchip_1ms_timer_initialize(repeating_timer_callback);
    wizchip_gpio_interrupt_initialize(SOCKET_ID, gpio_callback);

    network_initialize(net_info);

    print_network_information(net_info); // Read back the configuration information and print it

    setSn_DIPR(SOCKET_ID, multicast_ip);    // Set destination IP
    setSn_DPORT(SOCKET_ID, multicast_port); // Set destination port
	setSn_DHAR(SOCKET_ID, multicast_mac); // Set the MAC address (DHAR: Socket n Destination Hardware Address Register)
	setSn_CR(SOCKET_ID, Sn_CR_SEND_MAC); // Skip ARP (Address Resolution Protocol) and directly set the MAC address
    setSn_TTL(SOCKET_ID, 1);                // Set TTL (1 : local)

    // 소켓 초기화
    if (socket(SOCKET_ID, Sn_MR_UDP, local_port, SF_MULTI_ENABLE) != SOCKET_ID)
    {
        printf("Failed to open socket\n");
    }
    printf("Sends a message when GPIO14 detects a rising edge!\n");

    while (1)
    { }
    }
}

static void set_clock_khz(void)
{
    // set a system clock frequency in khz
    set_sys_clock_khz(PLL_SYS_KHZ, true);

    // configure the specified clock
    clock_configure(
        clk_peri,
        0,                                                // No glitchless mux
        CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS, // System PLL on AUX mux
        PLL_SYS_KHZ * 1000,                               // Input frequency
        PLL_SYS_KHZ * 1000                                // Output (must be same as no divider)
    );
}

/* GPIO */
static void gpio_callback(void)
{
    current_state = millis();
    if((current_state - prev_state) > 500) {
        sendto(SOCKET_ID, data, sizeof(data), multicast_ip, multicast_port);
        printf("send!\n");
        prev_state = current_state;
    }
}
/* Timer */
static void repeating_timer_callback(void)
{
    g_msec_cnt++;
}

static time_t millis(void)
{
    return g_msec_cnt;
}
```

---

Source: https://maker.wiznet.io/Grace_Koo/projects/setting-up-udp-multicast-on-w55rp20-evb-pico-/
