0:09
hello and welcome to controllers tech
0:13
this is the ninth video in the stm
0:15
ethernet series and today we will see
0:18
how to use the sdm32 as a tcp server and
0:22
tcp client both using the lwip and
0:25
netcon using free rtos of course i have
0:30
already covered the udp server and
0:32
client in my previous videos and you can
0:35
check them out in the ethernet library
0:38
before i start the video there is
0:40
important information i want to share
0:44
the controllers tech website now has the
0:46
community feature where you can open a
0:49
topic about the relevant issue
0:51
to do so you have to go to the community
0:56
here i have added a few microcontrollers
0:59
so that you can create the topic in the
1:02
you have to first register on the forum
1:06
once the registration is successful you
1:08
can go to the respective mcu and create
1:13
if you want more threads to be added you
1:15
can send me a mail about it
1:18
if the registration option doesn't show
1:20
up in the menu you can directly go to
1:22
the register page and then register
1:35
the work is still in progress and
1:37
sometimes it is misbehaving as of now
1:41
anyway let's continue with the video and
1:44
we will create a new project in cube id
1:50
stm32f750 discovery board
1:54
give some name to the project and click
1:58
the cube mx configuration is similar to
2:01
what we have seen in the previous two
2:04
so if you have already seen the previous
2:06
two videos on the ethernet you can skip
2:11
let me clear the pin outs first
2:17
we will start with the clock
2:20
i have 25 megahertz crystal on board and
2:24
i am running it as 200 megahertz clock
2:34
all right now go to the ethernet and
2:37
enable the type of connection you have
2:40
as i mentioned earlier this board have
2:45
let's start with the parameter
2:49
since we are using the on-board
2:51
connector the phy address must be zero
2:55
also change the rx mode to the interrupt
3:00
when you do this you can see the
3:02
ethernet interrupt is enabled
3:05
finally do check the pin outs and match
3:08
it with the schematics of your board
3:11
also make sure that the speed is set to
3:24
next go to the free r t o s
3:27
i am enabling the version 2 of the c m
3:31
no changes are needed to be made in the
3:36
just leave everything to default
3:39
you can see there is a default task
3:41
created and later the lwip will use this
3:45
task so leave this too
3:48
i am enabling the new library re-entrant
3:50
as it gives the error while generating
4:01
here first of all we will disable the
4:03
dhcp and manually enter the ip address
4:16
you can see here the rtos usage is
4:20
enabled by default as we have enabled it
4:24
anyway leave everything else to default
4:26
in the general settings
4:29
in the key options the only change we
4:31
need to make is increase the heap memory
4:36
i am setting it to 10 kilobytes
4:39
this is it for the lwip configuration
4:42
leave everything else to default for now
4:47
last but not least since we are using
4:50
the rtos we have to use the time base as
4:53
anything other than systick
4:55
i am using the timer 6 for this purpose
4:59
all right if you are not using cortex m7
5:02
based mcu go ahead and generate the code
5:06
as i mentioned i am using the f750
5:09
discovery board and as st recommends we
5:12
must enable the instruction and data
5:14
cache for better processing
5:17
also this board has less flash memory
5:20
and it won't be able to store all the
5:22
variables in the flash
5:24
that is why i am going to use the
5:26
external flash memory and for this
5:28
purpose i must use the m-p-u
5:32
set the m-p-u to background region
5:34
privileged access only plus m-p-u will
5:37
be disabled during heart fault
5:40
now enable the mpu region
5:43
the base address will be the address of
5:45
the external flash that is the qspi
5:48
which is at 9 million hexa
5:51
the size will be 512 megabytes
5:55
and we will disable all the access in
6:02
as mentioned here in the memory
6:04
description the qspi is in the block 4
6:07
which is 512 megabytes
6:11
this is why i blocked access to 512
6:14
megabytes of memory so as to prevent the
6:16
speculative access to this region if you
6:20
don't understand this part watch the mpu
6:23
configuration series in the cortex m7
6:27
the link is in the description
6:30
the region 2 will start again at the 9
6:32
million hexa and the size will be 16
6:37
this is the actual size of the qspi
6:40
memory available on the board
6:43
here we will permit the access and we
6:45
will set the region to cacheable and
6:49
this was actually explained in my video
6:51
about the memory configuration in cortex
6:56
we are trying to set this region as the
6:58
normal memory region with the right back
7:03
this is as according to the st's
7:05
recommendation for the q-s-p-i memory
7:09
we will create one more memory region at
7:12
9 million hexa but this time we will
7:14
enable the instruction execution from
7:26
this is it for the mpu configuration for
7:31
click save to generate the code
7:39
you can see there is the default task
7:42
created and inside the default task the
7:46
lwip gets initialized
7:52
let's build the code once to check for
8:04
all right we have four errors let's
8:08
the first one is about the multiple
8:12
it is defined in the middleware third
8:15
party lwip system os sysarch file
8:21
let's open this file first
8:31
this must be where the redefinition is
8:34
let's comment out this line and rebuild
8:39
we still have some errors but the error
8:41
regarding redefinition is gone
8:44
now the issue is related to the flash
8:46
memory being overflowed
8:49
for this reason we have already set up
8:51
the mpu so that we can use the external
8:54
flash memory to store the data
8:57
let's go to the flash script file to do
9:02
we have to change the origin of the
9:03
flash to the q s p i memory
9:11
the address is 9 million hexa and the
9:27
save the file and generate the code
9:30
and all the errors are gone now
9:33
we have modified the flash script but
9:37
we still need to make some changes in
9:48
go to system init function and we will
9:53
first we will reset the configuration
9:55
register and then we must relocate the
9:57
vector table to the new flash base
10:00
this is all the setup needed for now we
10:04
will do the ping test first and for that
10:06
we don't need any functions
10:09
let's build the code and debug it
10:12
since i am using external flash i need
10:14
to use the external loader in the
10:16
debugger so here we will create a new
10:24
go to debugger tab check external loader
10:36
click apply to save the configuration
10:46
download verified successfully let's
10:48
reset the controller
10:50
let's put a break point in the error
10:52
handler to make sure we don't hit it
11:12
let's ping to the board
11:18
the ping test is successful and before
11:21
we go ahead i want to show you the
11:23
configuration for the ethernet
11:25
this time i have connected the
11:27
controller directly with the computer
11:29
without using any router
11:31
this is why i want to show the
11:33
configuration for the ethernet in the
11:37
here i have changed the ip assignment to
11:41
and you can see the rest of the
11:44
i will upload these images along with
11:46
the project so you can access them later
11:53
all right since the ping is successful
11:56
let's include the library files in our
12:00
here i have the files for tcp server and
12:03
client and we are going to cover both of
12:05
them in this video itself
12:08
put the source files in the source
12:10
directory and header files in the
12:14
let's take a look at the tcp server
12:18
here i have defined two netcon
12:22
one of them will be used as the
12:24
identifier for the server and another
12:26
one for the client which will connect
12:30
the buff is the pointer to the net buff
12:32
structure and as we already know the net
12:34
buff stores all the information about
12:36
the data and also about the sender like
12:39
the address and port of the sender
12:42
i have also defined the variables to
12:44
store the address and port in case you
12:47
need the same for the client
12:49
and here we have the message arrays
12:52
where we will store the message received
12:54
and the message to be transmitted
12:57
the initialization of the server starts
12:59
with the tcp server init function
13:03
here we create a new thread whose name
13:08
the entry function is also the tcp
13:10
thread the argument passed to this
13:14
the stack size is set to default which
13:19
and the priority is set to normal
13:22
let's see the tcp thread now
13:27
here we will first create a new
13:29
connection using the netcon new function
13:32
the argument netcon t-c-p creates a
13:37
then we will bind this connection to any
13:40
available i p address
13:42
it will automatically bind it to the i p
13:45
that we set in the cube m-x and this
13:47
will be the ip address of the server
13:50
also the port for the server is set to
13:54
now the server is ready and we will put
13:56
it into the listen mode so that it can
13:58
listen for the new client
14:02
if a client tries to connect to this
14:04
server we will accept the connection and
14:07
the parameters of this connection will
14:09
be saved into the new con
14:11
the net concept takes two arguments
14:14
which netcon is listening and where the
14:16
connection will be stored
14:21
once we have accepted the connection
14:23
this while loop will run forever
14:26
here we will wait for the client to send
14:30
netcon receive takes 2 parameters again
14:34
the first is the connection from which
14:35
the data is to be received which in our
14:38
case is the new con the client
14:41
and the second is the pointer to the net
14:43
buff structure which we have defined as
14:47
if the data is received successfully we
14:50
will first extract the address and port
14:52
of the client just in case if you need
14:56
then the string copy function is used to
14:58
copy the payload from the net buff into
15:05
you can use this data sent by the client
15:07
in any way you want but here i am just
15:10
going to modify this data and send it
15:12
back to the client as a response from
15:16
netcon write is used to send the data to
15:21
the first parameter is the connection
15:23
where you want to send the data to then
15:26
we have the data pointer itself which in
15:28
our case is the s message array
15:31
then the length of the message
15:34
and at last we have the api flag which
15:39
these flags are defined to handle the
15:43
for example when the netcon copy flag is
15:46
passed the data is copied into internal
15:49
buffers which are allocated for the data
15:51
this allows the data to be modified
15:54
directly after the call
15:56
you can read more about these flags in
15:58
the sds documentation but in these
16:01
tutorials we will stick with the netcon
16:05
after sending the data we will clear the
16:08
and then we will delete the buffer also
16:11
so that we can receive new data from the
16:14
the server will again wait for the
16:16
client to send some data and this loop
16:21
so the steps were simple
16:23
first we create a new netcon identifier
16:26
then bind the identifier to the local ip
16:31
then we put the server in the listen
16:34
if some client tries to connect
16:36
the server will accept the new
16:39
then it will wait for the data to be
16:43
and finally the server will send the
16:45
response to that data
16:47
these are all the steps for the tcp
16:51
let's write the main file now
16:56
include the tcp server header file
17:03
in the default task we will just make a
17:05
call to the tcp server init function
17:08
which will eventually create the new
17:10
thread and everything will begin
17:13
let's build and debug the code so that
17:16
we can see the working in real time
17:22
i am using hercules and since stm32 is
17:25
the server hercules will act as the
17:31
here is the i p address and port of the
17:35
note that the i p address is the same as
17:37
we configured in the cube mx
17:48
let's connect to the server
17:51
here is the message that the connection
17:55
now we will send some data to the server
17:57
and you can see the server has sent the
18:03
the pink color data is sent by the
18:05
client and the black color is the
18:07
response sent by the server
18:12
you can see the server is responding
18:14
pretty well even for the larger data
18:23
so our tcp server was working well
18:26
now let's move to the tcp client
18:31
here stm 32 will act as the tcp client
18:34
which will send some periodic data to
18:36
the server along with a response for any
18:39
data sent by the server
18:47
this is the tcp client source file
18:51
just like the server here also we have a
18:54
pointer to the con structure and a
18:56
pointer to the net buff structure
18:59
there are variables for address and port
19:03
also the message and s message array to
19:07
all right everything starts at the tcp
19:09
client init function
19:14
here first of all we will create a new
19:18
the argument ecp sem is defined in the
19:24
the semaphore will be used to prevent
19:25
the simultaneous access to this tcp send
19:28
function which is used to actually send
19:31
the data to the server and is being
19:33
called by two different threads the tcp
19:35
send thread and the tcp init thread
19:39
we will use the sama4 to make sure that
19:41
both threads don't call the function at
19:45
the sys cemu takes two arguments the
19:48
first is the semaphore handler and the
19:51
second is the initial count
19:53
i am setting the initial count to zero
19:56
so we must first release the semaphore
19:58
before it can be acquired and we will
20:00
release it after the initialization of
20:02
the client is complete
20:06
then we will create two new threads one
20:08
for initializing the tcp client and
20:11
another for periodically sending the
20:15
the stack size is default one kilobytes
20:18
and priority is set to normal for both
20:21
let's see the initialization thread
20:27
here we will first create a new
20:29
connection identifier
20:31
the argument netcon tcp will create the
20:36
once the identifier is created we will
20:39
bind it to the available ip address and
20:43
the ip address will be the same as the
20:45
one we defined in the cube mx and this
20:48
will be the address of the client
20:50
now we will connect to the server
20:53
first we will convert the ip address of
20:56
the server into the integral format
20:58
this address will be the same as the ip
21:01
address of the computer which you can
21:03
find in windows by using the command ip
21:07
make sure you use the address of the
21:08
ethernet and not the wi-fi
21:14
the destination port is 10 and this is
21:16
the port of the server
21:18
now we will connect to the server using
21:21
the netcon connect function
21:23
here the first argument is the
21:25
connection identifier then we have the
21:27
pointer to the destination address and
21:29
at last the destination port
21:35
if the connection is successful we will
21:37
release the semaphore so that the tcp
21:40
send thread can send the data to the
21:42
server here we will wait for the server
21:45
to send some data to the client
21:48
once the data is received we will
21:50
extract the address and port of the
21:52
server just in case you need it
21:59
now copy the data sent by the server
22:01
into our message array
22:10
we will modify this message and send it
22:14
but before we can call the tcp send
22:17
function we would need to acquire the
22:20
this is to prevent the simultaneous
22:22
access to the tcp send function by this
22:25
thread and the tcp send thread
22:29
let's see the tcp send thread now
22:35
here we will convert the value of the
22:37
index variable into the string format
22:41
and then send this value to the server
22:43
but again before calling the tcp send we
22:47
must acquire the semaphore
22:49
this is a periodic task and it will run
22:52
every 500 milliseconds
22:56
in the tcp send function we will first
22:59
send the data to the server
23:02
net conright takes four arguments
23:05
the first parameter is the connection
23:08
where you want to send the data to
23:10
then we have the data pointer itself
23:16
then the length of the message
23:18
and at last we have the api flag which
23:23
these flags are defined to handle the
23:27
for example when the netcon copy flag is
23:30
passed the data is copied into internal
23:32
buffers which are allocated for the data
23:35
this allows the data to be modified
23:38
directly after the call
23:40
you can read more about these flags in
23:42
the sds documentation but in these
23:45
tutorials we will stick with the netcon
23:49
once the data is sent we will release
23:51
the semaphore implying that it's safe to
23:56
so the task waiting for the semaphore
23:58
can acquire it and call the tcp send
24:01
this is it for the explanation part now
24:04
let's write the main code
24:06
here instead of tcp server we will
24:09
initialize the tcp client
24:20
we also need to include the tcp client
24:24
let's build and debug the code so that
24:27
we can see the working of the client in
24:30
the stm32 is acting as the tcp client so
24:34
the hercules will act as the tcp
24:44
server this is the port of the server
24:47
and it should be the same as whatever
25:00
all right let's run the code now
25:03
put the server in the listen mode
25:06
the client is connected and it is
25:08
sending the index value every 500
25:12
you can see the confirmation in the
25:16
the value of the index variable started
25:18
at 6 because for the first 5 times it
25:22
was not able to acquire the semaphore as
25:24
the server wasn't connected
25:28
you can see the client is also
25:30
responding to the data sent by the
25:33
the index value is updating every 500
25:42
so everything is working as expected
25:46
we saw how to intimidate the sdm 32 as
25:48
the tcp server and tcp client and in
25:52
both cases our program worked as
25:56
the next few videos on ethernet will
25:58
cover the http server where we will do
26:00
one basic server and then implement some
26:03
additional details for example the use
26:08
this is it for today's video
26:10
i hope the things were clear
26:13
you can use the forum on the website to
26:15
open topics in case you have doubts
26:18
you can also leave a comment about your
26:21
thoughts on the video
26:23
the link to the code it is in the
26:26
keep watching and have a nice day ahead