Hello and welcome to controllers tech. This is yet another video covering the Riverdi
display, and today we will send the data from UART to the UI. We will simply receive something from the
UART, and then display it on the GUI. I will continue where I left the previous
project, so the LED functionality will still be present in the tutorial. Here is the previous project we worked on. Let’s add another textArea to it. I am using the same large typography, and
the name should be text area UART. The wildcard range is already set for it. So this is it for the configuration, let’s
generate the project and open it in the cube IDE. In the meantime, let’s see the datasheet
again. Here you can see the board has a RS485 interface,
and a RS232 interface. Along with these, the expansion connector
also has the u sart 1 pins, and one UART8 TX pin for the modbus. For convenience, you can use the RS232, but
since I don’t have a converter for this, I am going to use the RS485. All of them work on the UART, so using either
is fine. You can check out my previous video explaining
the RS485 communication. Here we need only 2 pins, pin A and pin B. I am using a USB to RS485 converter module
with the computer. The pin A from the module connects to the
pin A of the board, and the pin B connects with each other. The board already has a RS485 to UART converter,
so we don’t need to worry about the signal level. Let’s see the project now. This is the same project we made for LED control,
and I will continue with the same. Here in the main file, you can see the u sart
header file is already included. In the initialization section, the UART 1
is being initialized, along with UART 4, and UART 8. Let’s see the code for it. This section initializes the UART 4, and if
you note here, there is a function to initialize the RS485. This means that the UART 4 is connected to
RS485, and we will work with it. If you know about the RS485 module, there
is a driver enable pin. This pin can be used to set the module in
the receive mode, or the transmit mode. Here the DE pin’s polarity is set to high,
which means that the pin is active high. So to pull the signal high, we pull the pin
high, and to pull the signal low, we pull the pin low. The UART 8 has no initialization here. The UART 1 is initialized normally. In the initialization of UART 2, the hardware
flow control is enabled. So this must be connected to the RS232 port. As I mentioned, I am going to use the RS485
port, so I will use the UART 4. Irrespective of whatever UART you use, the
code will remain the same. All you need to do is change the UART instance. Let’s quickly see the MSP initialization
for the UART 4. Here you can see the DE pin is actually the
pin P A 15. Keep this in mind, as we would need it to
put the port in the receive mode. The UART 4 initialization is already called,
so we are good here. We don’t need anything in the main file,
let’s head over to myfile.c, which we created in the last video. Here we already have the LED related code
from the previous video, so I will write the UART code separately. Most of the things will remain the same, so
let’s copy this complete code and paste in the UART section. Now we will change the word LED to the UART. Alright, let’s understand things. First we define the task handle, and the task
entry function. Then we have the attributes for the UART task. The stack size is 512 words, and Priority
is set to normal. Next define the attributes for the UART queue,
which will be used to send the data to the UI. Then we write the functions to initialize
the UART task, and UART queue. And finally we have the task function, which
we will write from scratch. Since we will be sending the data in the string
format, we need to create a structured queue. I have covered this in my touch GFX video,
you can see on the top right corner. Here the structure consists of 2 elements,
a character array to store the data, and an integral variable to store the length of the
data. Let’s define the structure uartdata_s, which
will be used to store the information. The Queue will only have 1 element, and the
size of this element will be the size of the structure. We also need to define the UART handle, so
copy it from the uart.c file, and define it as an external variable. Now I could simply use the basic UART functions
to receive the data here, but let’s say that I want to use the idle line interrupt
to do so. Let’s also assume that I don't know how
to initialize the interrupt for the UART. So far we have used the cube MX to initialize
the interrupts for us, but we know that we can’t use cube MX with the Riverdi display. So here is what we will do. Copy the project to another location. Now open the IOC file in cube MX. Let’s see the UART 4. Here you can see the pins being used for the
UART 4. I want to initialize the interrupt for the
UART 4, so let’s enable it. Don’t make any unnecessary changes in the
file. Generate the code now. Don’t open the project, instead open the
folder. Now go to the CM7 -> core -> source and open
the uart.c file. Here you can see the interrupt has been initialized,
so let’s copy this. You can put it in the UART source file itself,
but since it's initialized after the UART, I am just putting it here in the task function. Another addition that would have happened
is in the interrupt file. Let’s copy the UART handle and paste it
in the interrupt file. The UART 4 interrupt handler is also created,
so we will copy it in our interrupt file. That’s all the changes happened when we
added the interrupt. Let’s write the rest of the function now. First we will set the DE pin low, so that
the RS485 can be put in the receiver mode. Now we will receive data in the receive to
idle interrupt mode. The data will be stored in the RX buffer,
and the maximum we can receive 64 bytes at once. Let’s define this RX buffer. Since we don’t need anything else from this
task, let’s give a bigger delay here. Now when the UART has received 64 bytes, or
it detects an idle line before that, an interrupt will trigger. This will call the RX event callback function. Here we will first copy the data from the
RX buffer into our structure. I am setting the last data byte as the string
terminator (‘\0’), and updating the length parameter with the new size. Now we will send the structure to the queue. So first we will check if there is some space
in the queue. If there is, then put the message. The priority and timeout, both are set to
0 here. And finally call the receive function again. So that is all we need to do for sending the
data, let’s build the code now. The string header file needs to be included
here. Now define these newly created functions in
the my file header, so that we can call them in the main file. Alright now we need to receive this data in
the model, and finally display it on the UI. So let’s start with the model first. Let’s define the UART queue handle as the
external variable, and also define a new structure to save the received data. The model tick function is called every time
the frame is refreshed on the UI, so we will write our code inside it. We will first check if there is some data
available in the queue. If there is, then we will get the data, and
store it in the structure we defined. Now copy the data from the structure, and
store it in an array. We need to define the character array in the
model header file. Once the data has been stored, call the function
UART display in the model listener. We need to define this function in the model
listener. Make sure the function has an empty implementation. Since the function has an empty implementation,
it will look for the same function in the presenter now. So we will define the same function in the
presenter header file, and write the code for it in the source file. Here we will call the same function in the
view. So now define it in the view header file. Finally we will write the code in the view
source file. Here we will use the unicode string copy function
to copy the data into the textAreaUART buffer. I guess I forgot to implement the wildcard
for the text area. Let me do this quickly. The buffer should have 1 higher byte than
the maximum data you are displaying on it. Regenerate the code. Let’s refresh the project once. It is still not showing in the auto complete,
so let me copy it from the screen view base file. After copying the data into the buffer, we
will invalidate the textArea, so the changes can take effect. The string copy inclusion is missing, so let’s
include the <cstring>. Alright the code builds fine. Go to touch GFX, and flash the code to the
board. I have connected the RS485 module to the computer,
and it is connected to com port 7. You can see the UART data is being displayed
on the text area. Here the data has exceeded the space available
for the text area. This is because I forgot to include the word
wrap in the code. Here in the view file, set the wide text action
to word wrap. This will fix that issue, and the text will
automatically display on the next line. The LED is working fine, and now we have the
UART also. So this completes today’s video about sending
the data from UART to the UI. I hope you understood the procedure. I am still working on the ADC, and once I
interface it successfully, I will make a video on it. The link to download the code is in the description
below. Leave comments in case of any doubt. Keep watching, and have a nice day ahead.