Author Topic: who can explain me how the network sample program works on a CH32V board?  (Read 1113 times)

0 Members and 1 Guest are viewing this topic.

Offline RISCVfanTopic starter

  • Newbie
  • Posts: 2
  • Country: be
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
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 10150
  • Country: nz
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
« Last Edit: July 08, 2024, 09:58:56 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 
The following users thanked this post: RISCVfan

Offline RISCVfanTopic starter

  • Newbie
  • Posts: 2
  • Country: be
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.c



but that looks like a generic check for link ?

 

Online Psi

  • Super Contributor
  • ***
  • Posts: 10150
  • Country: nz
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?
Na, 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.

In the main loop, there's  "WCHNET_MainTask();"  , that seems to be defined in eth_driver.c

WCHNET_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.

I'm still bit puzzled how/when that "WCHNET_CreateUdpSocket" gets executed.
Not sure what you mean by that
WCHNET_CreateUdpSocket() gets executed by you in your main.c function just before the while(1) loop to setup a UDP socket.

« Last Edit: July 12, 2024, 01:33:34 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf