with its two cores
operating at 480 and 240 megahertz
the arduino portenta h7 is the most
powerful arduino we've seen here so far
this model was recently created for use
in high-end
applications such as industrial
equipment computer vision systems
and other professional devices hence we
now have display and ethernet
capabilities
but more importantly we now have a dual
core processor
specifically, it's the STM32H7
in part one we installed a few hardware
drivers
and used the standard arduino ide to
blink the onboard light emitting diode
we did this using the Arm Cortex M7 core
but not the M4
to get the most out of this platform we
want to find out how to use the M7 core
and the M4 simultaneously with other
Arduino platforms
we could achieve some concurrency
through techniques such as
interrupts or multi-threading this was
the case for resources such as the
official scheduler library
but today we'll take this idea even
further by running two different
sketches
in parallel we'll build on part one by
getting the M7 core to blink red
getting the M4 chord to blink green and
finally
we'll alter this sketch so that we can
do things besides just blink the leds
if you missed out on part one consider
watching that video first
back then we had to upgrade to the
latest Arduino IDE
and also make sure that our drivers were
working properly
you'll know you're ready for part two
when you can plug in the Portenta H7
and see the resulting com port if you
double click the reset button on the
board
while it's still plugged in you should
also see the board transition from
normal mode to boot loader mode
this will be indicated by the green led
going into a breathing pattern
followed by your computer recognizing
the Portenta
as a new USB hardware device go ahead
and plug in your board if you haven't
already
and if it is in boot loader mode get it
back to normal
by unplugging it and then plugging it
back again as with anything else
electronic
this is best done with anti-static
equipment such as a grounded wristband
or a grounded mat and as a side note
you'll notice my board now has pin
headers attached
this was an optional move on my part if
you don't have an immediate need for
pins
just ignore them because they aren't
required for this procedure
if you do plan on adding them think
first about how you plan to use the high
density connectors on the bottom
these are meant for the carrier board
and potentially other equipment
and with that out of the way we can get
started the official example which came
from the website
will generate a sequence of three colors
red
using only the M7 core green using only
the M4
and yellow when red and green are both
active
that last one is a color mixing effect
because that part number
doesn't include a yellow emitter we can
also expect the yellow state to be
brighter than the others
for the red pattern we'll have the M7
blink the light with a period of 400
milliseconds
specifically 200 milliseconds on and 200
milliseconds off
for the green we'll have the M4 blink
with a period of 1000 milliseconds
in the same fashion half of the time on
and half of the time off
it's also worth noting that for these
leds sending a binary low
means we're turning the device on in the
arduino ide
create a new sketch and you might as
well name it something like
blink red led M7 as usual we'll start
with the setup function
that runs only once when the device
first boots up
here we'll add a pin mode command to set
the
led pin as an output and then we can
move down to the loop function
now we want to add a digital write
command this will command the red led to
turn on by setting it low
and we'll also add a comment labeling
that
next we want to add that 200 millisecond
delay
that will use the delay() function
third we want to do another digital
write to turn the led off
and finally we add a second delay of
equal duration
add a few more comments double check
everything and with that we've completed
the code for the M7
now we can hit the upload button bearing
in mind that uploading will also cause
the code to compile
if you run into any trouble double check
your com port number because it can
change from time to time
also it's worth double checking that you
have the correct core installed
and that your system is no longer in
bootloader mode in this case i ran into
an upload error
but it was probably caused by a
completely different device connected to
my computer that generates some trouble
from time to time
when everything is complete you should
see a long report ending in
a message saying file downloaded
successfully
now it's time to start working on the m4
core leave this sketch open
and highlight everything and copy
everything so that we can just paste it
straight into the m4 sketch
create a new sketch with a new name and
then paste in the old code luckily
we only have to make a few alterations
we want to change the durations from 200
to 500 for both lines
we want to change LEDR to LEDG
and like before we just want to double
check everything adjust any comments if
necessary
and then we're ready to upload again
double check your com ports in case
anything changed
and the code should be well on its way
give it a few seconds
and like before we should see a fairly
lengthy report
followed by the text saying file
downloaded successfully
now things are a little interesting
because if we check the device
we're only going to see it blinking red
but not green
this indicates that only the m7 core is
running even though we
uploaded code to both to fix that we
need to issue a command
from the m7 that allows the m4 core to
run
open the m7 code again and in the setup
function
enter the command ll underscore
LL_RCC_ForceCM4Boot(); you can also get a copy of this
code from the official arduino website
or from our own blog add a comment if
you want to
and like before we just double check
everything before uploading the code
upload and within a few seconds you
should see the three color sequence
blinking like we expect notice that we
didn't have to re-upload the M4's code
we just need it to enable it now what we
have is certainly functional but is it
practical
the delay function is a blocking
function that won't allow anything in
our own program to run
until it completes its time interval but
the library contains other time keeping
resources
one of these is the milliseconds
function spelled m-i-l-l-i-s
and any time it's called it will return
the up time of the arduino in
milliseconds
it does this by counting up from zero
and even though it will roll over back
to zero
after many days we can use it to do all
the time keeping for us
all we need to do is check it frequently
and it will signal us when it's time to
change the led
go back to the red led code for the M7
core
and luckily our setup function is just
fine as it is
above the function we want to create an
integer called
ledState this will store the desired
state of the led
and then we want to create a unsigned
long variable
this will serve as part of our
timekeeping mechanism
and finally we'll create a constant
value that will store our time interval
in milliseconds
we'll add a few comments here and there
so that we know what's going on
then we can move down to the loop
function clear everything within it
and create a new unsigned long variable
called currentMillis
let's also have that line assign the
value from the milliseconds function
to that variable bear in mind that the
second part
(the assignment) will happen every time
that the loop iterates
next we want to add a conditional
statement that will test whether or not
it's time to actually
invert the led state
for this we'll create an if statement
and we'll check for the difference
between
the current millisecond state the
previous state and
whether that different exceeds or meets
our switching interval
if it is time to do an update first we
want to store the previous values so
that we can do another comparison when
it's time again
next we want to use the conditional
operator also known as the "?" mark
to test the current state of the led if
the state is low
then we assign the state to a high value
basically inverting it
if the state is high then we set it low
finally we'll take that led state
variable and
use the digital write command to copy
that state
to the physical pin attached to the led
we'll double check everything
add a semicolon to that one line that's
missing it double check our commentary
and we can upload as a side note i did
once
try uploading code to both the M7 core
and the M4 at once
and it did generate an error so it looks
like we do have to be patient
and with the M7 code updated we can
highlight everything to copy it
and then go back to the M4 code like
before
this m4 code is very similar to the M7
code with just a few exceptions
for starters we can get rid of the force
boot command
secondly we can change the time interval
from 200 milliseconds to
500 milliseconds we change all
occurrences
of LEDR to LEDG and optionally
we can update our commentary
double check everything and then hit
upload give it a few seconds
and again if your com port has changed
you might need to adjust it
check the board and you should see the
expected color sequence again
if you want you can check whether or not
this is just the old code using the
delay() method
by altering the intervals but if that
were the case
you likely would have seen errors by now
the leds are now blinking like before
but the timing is now controlled by a
non-blocking function
that lets us do other things just like
in part one
using the dual core features was really
no harder than having two Arduinos
plugged in at once
now that we only had one cable to worry
about this three color
sequence could be useful in the future
since it gives us an easy way to check
whether both cores are running as
expected it's simple
but effective and even if the user might
have trouble with color distinction
you can use the timing and the cadence
of the lights to tell if things are
working or not
and speaking of both cores working you
might wonder if they can communicate
directly to each other
as one option you can use remote
procedure calls (RPCs)
and if that's not your style we could
see more methods in the future
finally it's worth planning for what
might happen when both
cores need to use a shared resource at
the same time
or when one overwrites one resource
after the other
this is a best practice for concurrent
systems and this topic is so rich
that it's practically its own sub-branch
of reliability engineering at this point
if you found this useful consider
joining us on Patreon
where we have a monthly poll and lessons
learned on technical builds like this one
(=•ω•=) The poll for august 2020 basically wants
to know if videos like this one should
be translated to
languages beyond english stay posted for
more projects in electronics robotics
IoT and communication systems and as
always have a great day
