Receiving UDP broadcast on mbed LPC1768/Mbed OS5

  • Hardware - mbed LPC1768
  • OS - Mbed OS 5
  • Separate node broadcasting UDP packet to MY_PORT

Information: - LPC176X target code sets flags for broadcast in default. So we can receive broadcast packets without setting additional flag(for LPC1768 register RxFilterCtrl, bit AcceptBroadcastEn)

mbed-os/targets/TARGET_NXP/TARGET_LPC176X/ethenet_api.c ethenet_init()

int ethernet_init() {
...
  LPC_EMAC->RxFilterCtrl = RFC_UCAST_EN | RFC_MCAST_EN | RFC_BCAST_EN | RFC_PERFECT_EN;
                                                      /* Receive Broadcast, Perfect Match Packets */
...
}

code example here:

Import programreceive_udp_broadcast_os5

this code demonstrate how to receive broadcast packet with mbed-os (Mbed OS 5). (tested on LPC1768)

you may need this python snippet on your pc to broadcast test message

import sys
import time
import socket

import netifaces as net

BROADCAST_INTERVAL_SEC = 5

HOST = ''
PORT = 58083
interfaces = []
for name in net.interfaces():
        data = net.ifaddresses(name)
        d = data.get(net.AF_INET)
        if d is not None:
                interfaces.append(d[0])

address = None
broadcast_addresses = []
for each in interfaces:
        if 'broadcast' in each.keys():
                broadcast_addresses.append(each['broadcast'])
                address = each['addr']

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.bind((HOST, PORT))

for each in broadcast_addresses:
        msg =  "broadcast from " + address
        sock.sendto(bytes(msg.encode()), (each, PORT))


Please log in to post comments.