who can explain me how the network sample program works on a CH32V board? - Page 1
Forum section: electronics / general

author: RISCVfan
author_rank: Newbie
timestamp: July 08, 2024, 09:33:56 am
content: hi, I'm using a WCH development board an Embeetle IDE.I want to make a program that sends a UDP message on the network whenever a button is pressed (GPIO pin)I thought I'd start by taking the example file "UdpServer" that comes with the WCHNET sample files.It works great: when running it; you can send an UDP message to the board and it replies back with the same message.But ... looking at this example file, I have no idea how it actually works.In the main.c file, it's basically an endless loop ...I was expecting to find some code that says: read into bufffer, send from buffer... but can't find it.anybody that can help me understand this specific example? many thanks in advance

author: Psi
author_rank: Super Contributor
timestamp: July 08, 2024, 09:39:52 am
content: It uses callbacks from this WCHNET ethernet library that does all the work for you.when you create the socket in WCHNET_CreateUdpSocket you pass in the TmpSocketInf struct.That struct includes a "function pointer" to a function of type (struct _SOCK_INF *socinf, u32 ipaddr, u16 port, u8 *buf, u32 len)This line here gives that function to the library so it can use it.TmpSocketInf.AppCallBack = WCHNET_UdpServerRecv;eg, the second part of that line is the name of the function above.Function pointers can be a bit odd if you've never used/seen them before. but they are very powerful and can make things cleaner.So you make your own local function with all those function parameters the same and pass a pointer to that function into the library and the library will "callback" your function when it has some data for you to handle. Along with that callback is a pointer to a buffer that's located inside the library where you can grab the data from. (*buf)In this case the callback function is taking that buf pointer containing the data and gives it to the function used to Send UDP WCHNET_SocketUdpSendTo(socinf->SockIndex, buf, &len, ip_addr, port);So it just sends what it receives.Note that the length (len) is passed along with it so you know how far you can safely read into that buffer (buf) or how much data is to be read/written.Normally you might have a local buffer variable and do a memcpy() to make a local copy of the data in buf.But since this is just sending what it receives there was no point

author: RISCVfan
author_rank: Newbie
timestamp: July 08, 2024, 10:03:13 am
content: thanks!I'm sorry, I'm not all that familiar with callbacks... I'll dig into it. Is it comparable to what would be called an interrupt routine?I'm still bit puzzled how/when that "WCHNET_CreateUdpSocket" gets executed.In the main loop, there's "WCHNET_MainTask();" , that seems to be defined in eth_driver.cbut that looks like a generic check for link ?

author: Psi
author_rank: Super Contributor
timestamp: July 12, 2024, 12:52:03 pm
content: Quote from: RISCVfan on July 08, 2024, 10:03:13 amNa, it's not like interrupts, it's just like a function being called, like if you made a function yourself and then called it.Except in this case the thing calling that function you made is not in your program it's in a different C file (WCHNET library) that's written by someone else.But the person who wrote WCHNET does not know if you, a user of that library, need to use a function for this task or not.They cannot hard code a call to an external function if they don't know if you want one or not. So instead they give you a way to tell the library "hey, I want to you call my function X when Y occurs, and here's the memory address of the function".Normally this is called registering the callback. You are passing a pointer (that points to your function) into the library's code so it can call that function when it needs to.Quote from: RISCVfan on July 08, 2024, 10:03:13 amWCHNET_MainTask() gives the WCHNET library it's ability to run it's own code by inserting itself into the main program loop.Practically everything Ethernet related happens inside that function or its sub functions.Quote from: RISCVfan on July 08, 2024, 10:03:13 amNot sure what you mean by thatWCHNET_CreateUdpSocket() gets executed by you in your main.c function just before the while(1) loop to setup a UDP socket.