0:09
hello and welcome to controllers Tech today in this video we will see how to
0:14
Port the uh G2 graphic library to the SDM 32 this is a very popular Graphics
0:21
library for embedded devices targeting all types of monochrome
0:26
displays we will see how to Port this library to s M32 on the SDM 32 Cube ID
0:33
and use the cube MX to generate the project we will also see the implementation on some of the displays
0:40
that includes 0.96 in Old 1.3 in SPI Old
0:45
St 7920 based graphic display and the hx1 1230 based
0:52
display this is why the video is going to be quite long here I have all the
0:58
displays that will be used in this video the purpose is to show the implementation using all the possible
1:05
methods which includes SBI i2c big banging and even the
1:10
dma we will start with the sh 1106 based 1.3 in SBI old
1:18
display let's check out the library first here you can see the GitHub page
1:24
of the u8 G2 these are all the devices that are supported and you can see the full list
1:31
here this Library only supports the monochrome displays for now so you can
1:36
search for the display controller on this page all right let's download this
1:42
entire library and extract it we will copy the required files from it later in
1:48
our project as I mentioned we will start with the sh16 based 1.3 in SBI old
1:57
first here is how the display is connected to the SDM 32446
2:04
re I have also created a project and we will configure it now let's start with
2:11
the clock setup first we will bypass the high-speed clock Source the board has 8
2:18
MHz Crystal on it and I want to run the system at maximum 180
2:24
MHz now we will configure the SBI peripheral for the display I am enabling the SBI in the
2:31
transmit only mode we do not need any input from the display so transmitting
2:37
only is enough for this application you can see the pins pa5 and
2:43
pa7 are configured as the clock and data pins here I have connected the mossy pin
2:49
to the data pin of the display the clock pin from the MCU is
2:54
connected to the clock pin of the display we need to configure three more pins pins one for the chip select one
3:02
for the data or command mode and one for the reset the Cs pin is connected to the d10
3:09
and the MCU here I have the pinout for the nucleo F
3:14
446 the d10 pin is actually the pin pb6 let's set the pin pv6 as output and
3:22
rename it to CS next we have the DC pin connected to D9 of the
3:28
MCU this D9 9 is actually the pin pc7 so let's set the pin pc7 as output
3:36
and rename it to DC at last we have the reset pin connected to d8 of the
3:42
MCU this is actually the pin pa9 of the MCU let's set the pin PA n's output and
3:50
rename it to reset the SBI is configured with 8bit data size and the MSB will be
3:57
sent first let's increase the prescaler here to reduce the SBI board rate to
4:03
around 2 mb/s the SPI clock polarity is set to
4:08
low and the clock phase is set to one Edge basically the SBI mode zero is
4:13
being used now go to the gpio configuration select all the pins and
4:20
set their output speed to very high or high whichever is available that is all
4:26
we need to configure click save to generate the project go to the uh hg2 folder we
4:33
downloaded earlier here the SE Source folder consists of all the necessary
4:38
files we need so we will copy this folder in our project I am copying it
4:44
inside the driver's folder now open the project properties go to the cc++ build
4:52
settings under the compiler setting open the include path here we will add the
4:58
path to the folder we just copied click on the workspace browse the folder and
5:04
save the configuration let's build the project
5:11
once to check for any errors all right there are no errors so let's proceed
5:16
ahead you can search for U hg2 port to find the porting guide here is the
5:23
official guide to Port the library to different MCU there are examples for SDM 32 as
5:30
well we will start from the beginning basically there are only two things we
5:36
need to take care of while porting it to different MCU one is the gpio And Delay call back
5:44
and another is the communication call back these are the two call back functions which we need to Define in our
5:51
code and that's it this is the Callback function type and it is the same for
5:57
both the call backs the delay and message call back handles the delay used
6:03
in the library is also used to handle the timing requirement for the i2c and
6:09
SBI in case the software implementation of these protocols is being used the GPO
6:16
messages consist of the setting and resetting of different pins used by the display for example the Cs pin DC pin
6:25
reset pin Etc the menu pins are used when agree menu is created on the
6:31
display we will not use the menu pins in this video here is the template for the
6:37
GP iio And Delay call back we do not need to use all the messages defined
6:44
here so let's just copy the initial part of this call back now we will only
6:49
Define the messages we
6:54
need we do not need this initialization as the h initializes everything for us
7:01
the Nano and micro delays are used during the software implementation of the SBI and i2c
7:08
protocols we are using the hardware SBI so we will skip these definitions as
7:13
well we need to define the millisecond DeLay So let's copy it in our function
7:20
the how delay also provides the delay in milliseconds so we will simply pass it
7:25
here the parameter argument in defines the number of mill seconds you can also
7:31
check these messages in the u8 by8 header file next we need to define the
7:37
gpio messages we are using the pins CS DC and reset so let's define
7:47
them here again the parameter argument in decides whether the pin should be set
7:52
or reset so we will simply pass the parameter to the gpio right function
7:59
similarly we need to Define the DC and reset pins as
8:12
well so our delay and GPI o message call back is ready next we will see the
8:19
communication call back there are some predefined callbacks
8:26
in case we are using the software implementation of SBI and i2c or if we are using the parallel mode in such a
8:34
case we need to define the clock and data pins in the gpio call back we will
8:40
see this case later in the video for now we will focus on Hardware
8:45
peripherals these are the communication messages we need to Define in the communication call
8:56
back let's copy the initial part of the function from the previous call back I
9:01
am calling it U 8x8 SBI now we will Define some new messages
9:07
inside this call back the first is the initialization message it can be used to
9:13
initialize the SBI or i2c peripherals in our case Hal initializes
9:19
everything so we do not need this one next is the message to control the DC
9:25
pin here we will set or reset the DC pin based on the parameters value next we
9:32
have the message to send the
9:40
data as mentioned here the data is located in the argument pointer and
9:46
argument in defines the number of bytes we will simply call the function H SBI
9:52
transmit to send the data via the SBI the data is the argument pointer and
9:58
size is the argument int let's typ cast it to the pointer to the unsigned
10:03
integer type next is the message to start the transfer process as mentioned
10:09
here this message can be used to enable the chip select
10:19
pin we will reset the Cs pin to enable the slave device this is the standard
10:29
next we have the message to end the data transfer and to do so we will set the Cs
10:40
pin that is all the communication messages we need to deal with here you
10:46
can see a template for the hardware SBI is provided based on the Arduino
10:51
functions you can also use this template just change the functions as per the SDM
10:57
32 Hal anyway we will continue with how we Define these call backs actually
11:04
these functions should be defined outside the main function or right now
11:09
we need to call the appropriate functions to initialize the display and print something on it here you can see
11:16
the startup sequence for the library so let's open it we need to First Define
11:22
au8 G2 structure in our code
11:31
I am calling it my display let's copy all these functions in our main file and
11:36
we will modify them according to our display the first function is used to
11:42
set up the display and it changes based on what display you are using I have the
11:47
sh 1106 based display and here you can see a lot of functions to set up this
11:53
display this display does not have any name so we will focus on the ones with
11:59
no known name there are three variants of each display type 1 2 and F these
12:06
numbers basically Define the memory used for the frame buffer one stands for
12:11
onepage memory two stands for two page memory and F stands for fullframe buffer
12:17
will be stored in the memory you can see more details about the memory Below in the setup reference for the display with
12:25
128x 32 resolution onepage buffer occupies 128 bytes in memory two-page
12:33
buffer occupies 256 bytes and fullframe buffer occupies 512
12:39
bytes these memory occupations change with the resolution of the display so do
12:45
check out these setup function references to know the exact size for your display the F 446 has enough memory
12:53
to store a fullframe buffer so I will use the F variant of the setup function
13:02
let's change the structure to my display the second parameter is the
13:08
rotation here you can see values for different rotations I will leave it to the default
13:15
one next is the communication call back which we have already
13:23
defined and at last we have the gpio And Delay call back which is also defined
13:29
above the next function initializes the display and the next one wakes It Up by
13:35
disabling the power save mode now our display is up and running so we need to
13:40
print and draw something on
13:50
it here you can see the u8 G2 reference manual let's open it we have the
13:57
references for different things used in the display we will only look at some basic ones for now we need to clear the
14:06
display let's also see this one for drawing a string after initializing the
14:12
display we will clear it
14:18
first here are different functions to print the string on the display and here
14:23
is one example for it let's copy this
14:31
we also need to send the buffer to the display and here is the function for the
14:46
same let's build the project once we have many errors here I forgot to
14:53
include the uhg 2 header file all right most errors are gone now but there is
14:59
still one here actually the functions I copied are for C++ but anyway I will
15:05
modify them for the C the first parameter of these functions will be the U hg2 structure we defined above you can
15:14
refer to the draw string and set font functions for the C in the function
15:23
reference all right all the errors are gone now so let's Flash the project to
15:34
you can see the string is printed on the display let me add another string below
15:39
this one let's build and Flash the project again another one is also
15:44
printed on the display let's draw a circle on this
15:52
display I will use this function to draw the circle here x0 and y0 is the center
15:59
of the circle and rad is the
16:04
radius let's build and Flash it
16:10
again you can see that we have got the required result on the display so the
16:16
library is working fine for sh 1106 based old display now we will try
16:22
another display the code will remain pretty much the same so I will make a
16:27
copy of these call backs we just created I will keep these call backs untouched
16:33
in case someone wants to use them as it
16:40
is now we will interface the St 7920 graphic LCD display here is how the
16:48
display is connected to the SDM 32446 this display doesn't have the
16:55
dedicated pins for the serial communication but we can use other available pins for this purpose the
17:02
display is powered with 5 volts from the MCU itself and a potentiometer can be
17:07
used on the vo pin to control the display contrast the data or instruction
17:13
pin acts as a chip select so it is connected to the Cs pin of the MCU the
17:19
read write pin acts as a data pin so it is connected to the mossy pin of the MCU
17:25
the enable pin acts as a clock pin so it is connected to the clock pin of the
17:30
MCU the cs1 pin or the PSB pin must be grounded to use the display in the
17:36
serial mode the reset pin is connected to the reset pin of the
17:42
MCU we are not connecting the DC pin here other than that the rest of the pins are the same as what we defined in
17:49
case of the sh 1106 all right let's see the changes we
17:54
need to make in our code for this display to work this display requires
17:59
some different configuration for the chip select pin so let's see it in its data
18:05
sheet here we have the data sheet of St 7920 as mentioned here the serial mode
18:13
can be selected by grounding the PSB pin we have already done that as explained
18:19
in the connection diagram let's see the details about the serial
18:25
interface as mentioned here the chip select pin must be high for the clock to be accepted by the display this is
18:33
unlike the SBI communication we are used to where the Cs pin must be low to
18:38
enable the slave device so we need to pull the Cs pin high before sending the
18:44
data to the display we don't need to make any changes in the gpio call back
18:50
in the communication call back we need to change the Cs pin polarity in the start and transfer end messages the rest
18:59
of the call back function will remain the same since the DC pin is not being
19:04
used we can comment out the messages regarding the DC pin now we will change
19:10
the setup function inside the
19:20
main let's search for the St 7920
19:26
here here we have the setup function for different resolutions but I am looking
19:31
for 128x 64 all right here it is I am going to
19:37
use the one with the fullframe buffer the rest of the code will remain the
19:43
same let's build and Flash the project to the board it did not work so we need
19:49
to make some changes in the code let's use this s variant setup function
19:59
let's build and Flash it again to the board we still did not get the display to work properly it might be happening
20:07
due to the high SBI clock we are using let's reduce the SBI clock to below 1
20:14
mahz all right let's build and Flash it
20:20
again we have got the display working now it looks weird because I did not
20:25
connect the potentiometer to control the contrast instead the vo pin is left not
20:30
connected let's change the rotation of this display to R three it did not flip
20:37
180° as expected let me change it to R2 all right the display
20:44
flipped 180° so we have successfully interfaced
20:49
the St 7920 by using this Library the next display we will check if
20:56
ssd1306 i2c old this is how the display is connected to the f44
21:03
6re the i2c clock pin is connected to the clock and the data pin is connected
21:09
to the data we don't have the i2c enabled so let's open the cube MX and
21:15
enable the i2c one select the fast mode to use the 400 khz i2c clock the display
21:23
is connected to the pins pb8 and pb9 so let's configure them as clock and data
21:29
pins all right that is all we need click save to generate the project let's
21:35
comment out the St 7920 related call backs the i2c old does not use the Cs DC
21:44
and reset pins so let's remove them from the gpio call back before we modify the
21:50
communication call back let's take a look at the porting guide again as I
21:55
mentioned earlier the guide provides the temp templates for the hardware SBI and i2c
22:02
Communications here is the template for the i2c communication but this is made keeping the Arduino functions in mind
22:10
although there is another template in case we only have one function for our 2C transfer stdm 32 h also has a single
22:18
function to transfer the data via the i2c so we will use this template for the
22:24
display this is the bite send message and here we arej just copying the data
22:29
into the buffer we don't need to initialize the communication as it is already initialized in the beginning
22:37
inside the stock transfer message we are resetting the buffer index finally inside the transfer end
22:44
message we will transmit the data via the i2c we will call the function i2c Master
22:50
transmit to transmit the data the slave address for the display is 78 hex data
22:57
is the buffer size is buffer index and let's set some time out for this function we have defined both the
23:04
callbacks let's search for the setup function now there are separate setup
23:09
functions for the ssd1306 i2c so make sure you choose the
23:15
correct function I am again using the F variant for the fullframe
23:27
buffer all right let's build and Flash the project to the
23:35
board you can see the display is working fine with this Library if you are seeing
23:41
some fluctuations it is only due to the camera the display does not fluctuate at
23:47
all let's change this string and Flash the project again to the board you can
23:53
see the updated string is printed on the display so the ssd1 1 306a display is
24:00
also working fine next we will see the last display I have with me the hx1
24:07
1230 let's see the connection between this display and nucleo F
24:12
446 this connection diagram is for Nokia 5,110 LCD but the same connection can
24:20
work for hx1 1232 The Only Exception is the DC pin in
24:25
Nokia 5,110 LC whereas hx1 1230 does not have
24:31
the DC pin the rest of the connection is the same the clock in is connected to
24:37
clock data is connected to data chip select is connected to Cs and the
24:42
display is powered with 5 Vols from the MCU itself let's open the cube MX again hx1
24:50
1230 supports board rate up to 4 mb/s so I will increase the SBI clock
24:58
that is all the change we need to make let's generate the project again let me
25:04
comment out the old related functions and we will Define separate call backs for the hx1
25:17
1230 there is no DC pin used here so let's remove the message related to the
25:23
DC pen let's also remove it from the communication call back
25:29
we need to reset the Cs pin when the transfer starts and set this pin when the transfer
25:35
ends now we will look for the hx1 1230 setup
25:45
function here I am again going to use the fullframe buffer I forgot to change
25:52
the communication call
25:57
back all right let's Flash the project to the board
26:05
now the display did not work at all upon further investigation I found something
26:12
unique about the datab bits used by this display let's take a look at its data
26:26
sheet as you can see here the display needs nine bits one for the control and
26:32
eight for the data the control bit decides whether the scent bite is a command or data stdm 32 is capable of
26:41
transmitting 8 Bits or 16 bits at once but it cannot transmit nine bits we can
26:48
still manage the nine bits by keeping track of how many bits have been sent and how many are remaining but this is a
26:55
good opportunity to demonstrate the software SBI implementation let's delete the
27:01
communication call back now we will add more messages in the GPO call back so to
27:08
implement the software SBI method we are going to need the delay in
27:14
nanoseconds don't worry we don't exactly need a delay of 1 nond just perform an
27:20
operation here which takes extremely less time to execute let's also Define
27:26
the 100 nond delay a here I will call the knop instruction in a for Loop now
27:33
we need to define the messages related to clock and data pins but before doing
27:39
that we need to Define these pins in our project let's disable the SBI in the
27:45
cube MX instead we will configure these pins as output and rename them to clock
27:52
and data now go to the gpio configuration
28:01
and set the speed of the pins to very high now Define the SBI data message
28:06
inside the call back here we will set or reset the data
28:13
pin based on the value of the parameter argument
28:18
int similarly we need to define the SBI clock message and set or reset the clock
28:24
pin based on the parameter
28:33
that is all we need to Define in the call back now we can use the default
28:38
software s call back available in the library we will use this three wire SPI
28:44
as it is used in case of 9bit communication where the DC pin is not
28:50
available that is it the rest of the things will remain as it is let's build
28:55
and Flash the project to the board again you can see the display is working fine
29:01
now do not worry too much about this delay time just set the knop instruction
29:07
here irrespective of what MCU you are using you can also check the SBI clock
29:12
timing on the scope if the clock is too fast insert a for Loop in the Nano delay
29:18
message let's change the rotation of the display to see if it accepts the command
29:24
it flipped 90° I will change it to R2 and see
29:32
again all right now flipped completely 180° so we saw how different displays
29:40
can be interfaced with the same U G2 Library some displays have unique
29:45
features so we need to make changes in the call back functions but most of the displays
29:52
either use SBI or i2c so we can interface them directly now I will
29:58
demonstrate the use of spma to interface the display I will use the sh 1106 for
30:05
this purpose let's uncomment the functions required for
30:11
h106 now we need to enable thebi again in the cube MX so let's disable these
30:18
clock and data output pins first and instead we will enable the SBI in the transmit only mode bring the SBI clock
30:26
down to around 3 mb/s now go to the dma section and
30:32
enable the dma for transmission the dma mode is set to normal mode and make sure
30:39
that the dma interrupt is enabled in the interrupt section that is all let's
30:44
generate the project again along with the dma we will also see how to use the
30:50
partial frame buffer to send onepage data to the display so let's comment out
30:56
this part and instead we will use another code to send one page data at a time first we set the current page
31:03
position to zero then update the buffer with the data we want to print on the display and call the next page function
31:12
this next page function will send the one page buffer to the display and set the position to the next page this way
31:19
the display will update one page at a time this process is slow but it
31:25
occupies less memory in the MCU we also need to change the display setup
31:31
function to use a onepage frame buffer let's build and debug the project to see
31:37
if it is working fine I have added a break point inside the Dual Loop to see
31:42
how the data is printed on the display we have hit the breakpoint and we are on
31:47
the first page right now let's resume the debugger we have hit it again now we
31:55
are on the next page and hence the previous page data has been sent to the display you can see it is printed on the
32:03
display we keep hitting the break point and with each hit you can see a new data
32:08
is printing on the display so this is how the partial frame buffer Works only one page is stored and
32:16
processed at a time and the loop exits when all the pages have been printed
32:21
let's see how the data is printed when we use a two-page frame buffer
32:30
this time two pages are printing at once and we got the data at a faster rate than before the two-page frame buffer
32:38
occupies twice the memory but it also prints at Double the speed now let's see
32:44
the usage of dma in transmitting the data we will get the template from the
32:49
porting guide itself here is one template for the SBI dma but it uses the
32:55
Arduino functions we will modify it as per the SDM 32
33:09
Hal inside the send bite message we need to wait for the dma completion we will
33:15
come back to this later let's first create a SBI dma transfer here the data
33:22
we want to transmit is in the dma buffer and the number of bytes is the argument int once the dma transfer is finished an
33:30
interrupt will trigger and the transmit complete call back will be called inside
33:36
this call back we will set the variable TX done note that I am initializing the
33:41
TX done with the value one that is because before even sending the first
33:47
bite we need to wait for the transfer completion if the variable is initialized with the value zero this
33:54
while loop will always wait here and the transmission will never begin we can set
33:59
the variable to zero after the previous check is passed so before sending the data via the dma the txd variable will
34:07
always be zero inside the initialization message we need to initialize SBI and
34:15
dma they are already initialized so we will skip it here the Cs pin is set so
34:22
we will do the same using theal function inside the set DC message
34:27
message we need to wait again for the dma transfer completion so let's wait for the TX data
34:34
variable to be set to one again we need to set or reset the DC
34:39
pin based on the value of the parameter argument int inside the start transfer
34:45
message we need to reset the Cs
34:53
pin let's remove this as this function is used for the nanc delay and we do not
34:59
need it in the hardware SBI let's set the variable TX done to
35:04
zero here now inside the end transfer message we need to again wait for the
35:10
dma completion so let's do that remove the delay function and set the Cs
35:18
pin that is all we need to modify the communication call back let's change the
35:24
setup function to use the full frame and here we will pass the new call back
35:29
function I am changing the string so it will be easier to identify if the code
35:35
worked or not all right let's build and Flash it to the board it did not work
35:41
let's debug the project to see the issue I will run the debugger once and then
35:47
pause it here it gets stuck at this particular Loop actually we do not need
35:53
to wait here since we are already waiting at the transfer end message message let's also comment out this part
36:01
because if this message is called first the variable will reset back to zero and
36:06
then this Loop will never exit let's build and debug the project again you
36:12
can see the display is working fine now so we saw how to use the uh hg2 library
36:20
to interface different monocrome graphic displays we also saw implementation of
36:26
hardware and software SBI and i2c protocols you might need to change the
36:32
protocol implementation based on which display you want to interface this is it for the
36:39
video I hope everything was clear you can download the project from the link
36:44
in the description leave comments in case of any doubt keep watching and have a nice