The STM32 line is a whole family 32 bit ARM
microcontrollers from ST micro electronics. They've been out since 2007 and seem to be
gaining a lot of popularity in the last few years From what I can tell this is because 32 bit
microcontrollers are just about as cheap as 8-bit microcontrollers, but offer a lot more
power. Understanding the naming convention can be
quite confusing. The best place to start is probably on ST's
site at microcontrollers and microprocessors. We want to look at MCUs, the microcontroller
units. MPUs are microprocessors. Those are good if you want something that
will run Linux. ST has broken down the chips into four categories,
high performance, mainstream, low power and wireless. If you are unsure of what to use, the mainstream
list is usually a good start. They'll likely have all the pins and peripherals
you're used to from a microcontroller and they're usually the cheapest of the bunch. If you're just starting out, the F0 series
is usually a good bet. They're based on the ARM Cortex M0 architecture
so they can act as a replacement for many, 8 and 16 bit microcontrollers. This is where things get confusing. F1 is based on cortex M3 and F3 is based on
cortex M4, make sure you carefully read the series description to know what architecture
you're getting to make learning easier, ST has created a series of development boards
known as Nucleo boards. These things are great, lots of features,
and they cost between $10 and $20. Once again, the naming can be odd on ST's
Nucleo board page, we get a nice chart showing us which STM32 parts have accompanying Nucleo
boards. Take a look at the family names for the boards. Notice that the Nucleo 32 family are small
breadboard compatible boards with 32 pins. 32 has nothing to do with the number of bits
here. The Nucleo 64 are your Arduino sized boards
with more pins available. Finally, the Nucleo 144 are reserved for the
really powerful STM32 parts with even more pins. Personally, I'm a big fan of the Nucleo F02K6,
as it's super low power, cheap fits on a breadboard, and has built in USB. For the first few episodes in this series,
you can use any of these boards. However, I'm going to use the Nucleo L476RG
that's because in later episodes, I'm going to show you how to use Digi-Key's, IOT studio
with STM32, which only supports the L476RG. When I made this video, one of the reasons
I really liked the Nucleo boards is because you can program them with straight C or C++. You can also use ARM's Mbed platform. Some of the boards even have Arduino support. So you have lots of options. The next part is probably my favorite feature
of the STM32 line.
0:02:58.110,1193:02:47.295
Let's say you're working with F042 part. I mentioned earlier, and you've already created
a board for this 32 pin LQFP package shown here. It's a cortex M0 part, and we think it's good
enough. However, we quickly run into limits with our
code. We've got a maximum of 48 megahertz, 32 kilobytes
of flash and 6 kilobytes of Ram, but take a look at the LQFP 32 pin out. Let's go back to Digi-Key site and search
for STM32. We narrow our search to 32 LQFP parts. Then we narrow it again to just cortex M4,
since we know we need more power and something with USB, aha, let's try this L422 part. This microcontroller can go up to 80 megahertz
with 4x the flash and over 6x the Ram. Scroll down to find the pin out for the LQFP32
part, compare this to the F042 pin out and we see that they're just about pin for pin
compatible. There's a few extra features on some of the
L422's pins, but you should be able to just drop this beefier, part right into your board
design and have it work with the same part shape and pin number. Most of the STM32 chips are pin for pin compatible. That can save you lots of effort if you're
running into limits with speed or memory, I hope this gives you an idea of how to navigate
the STM32 lineup, but enough talk. Let's see how to actually program a Nucleo
board. To begin, we'll need to install the development
environment. The easiest way to find it, I found, is to
search for STM32 cube IDE. The first hit should be ST's page, click on
it and scroll down to download the package for your operating system. Accept the user agreement. Do note that you'll need to sign up with an
email and password. Don't worry, it's free. Unzip and run the installer or whatever installation
steps your particular operating system has you do. Accept all the defaults. This is a good time to plug in the Nucleo
board to your computer. When you start the IDE, you should get a,
pop-up asking you where you want your workspace. STM32 cube IDE is based on the eclipse development
environment, which uses the concept of a workspace, which is just a directory on your computer. Somewhere that houses all your project files
for this particular IDE, feel free to change it, but I'll leave mine as default click launch
and the IDE should open giving you a welcome screen. Since you might not always see the screen
on future visits, click file, new, STM32 project to start a new project you'll then be presented
with a target selection window. Here you can pick the STM32 part that you
want to develop for. You can also use the parametric search to
narrow down your part. This can be sometimes more helpful than the
website. We're using an official ST board, so click
on the board selector tab. Type Nucleo in the search bar to get a list
of the available Nucleo boards. Find and click on our Nucleo L476RG. You should see our board in the list below,
click on it and click next. Give your project a name. I like to keep the board or chip name in my
project. So I'll call this one Nucleo-L476RG-Blinky. We can use either C or C++. I find C a little easier when starting out. So let's stick with that. Click finish and you'll be asked to initialize
all peripherals in default mode. If you're working with a bare chip, I recommend
saying "no" here and only turning on the peripherals as needed. However, since this is a demo board click,
"yes" it'll initialize all the functions of the Nucleo board by default, such UART, which
can help with debugging. You'll then be asked about switching perspectives. A perspective is a collection of windows,
panes and views in eclipse. We want to open this initial perspective. So click "yes". Note that STM32 cube IDE does not come with
board definitions and individual part libraries by default, as a result, whenever you create
your first project for a particular chip or board, the IDE will download all the associated
files for that chip or board. This can take some time. So be patient. It does mean you need an internet connection
for the first time you initialize a project for a chip or board. However, once you've downloaded these files,
once you shouldn't need to do it again, if you start a project for the same chipper board,
once it's done, you should see a graphical representation of your microcontroller. STS STM32 cube Amex tool was a graphical tool
that helped you initialize peripherals for your STM, 32 part and generated code for you. That tool has now been integrated into this
new IDE, which makes life a lot easier. You can find all the available peripherals
for your part on the left side, pane under categories. Alternatively, you can click on a pin to see
its available peripherals. For example, we can make PC eight into a digital
output pin by selecting GPIO underscore output. Simply select that option again, to revert
the pin to its default, unused state. As another example, we can set PA 10 as the
receive pin for you. Start one. If we wanted note that many of the pins are
already set for us, and that's because we're using a Nucleo board, which has most of its
peripherals already defined for us at the top, you can see several tabs, which help
us initialize our part. The clock configuration tab gives us a schematic
like view showing how all the clocks, dividers and multipliers are connected. Once again, this is all set for us because
we're using a Nucleo board, but if you're making your own board, you might have to set
these. The project manager tab lets us change some
of the project settings, such as the name and default allocated memory sizes. The tools tab has some advanced features such
as a calculator for estimating the current draw of your microcontroller. Click on file save. And you'll be asked to generate code click. Yes, and the IDE will update the code in your
project files. While there are tons of files to look at that
makeup, a project, we just care about source main dot C for now. You'll notice these begin and end comment
guards. They may make the code look cluttered, but
they do serve a purpose that we'll show you in a minute, scroll down to find main, which
is the beginning of our program. Scroll some more to find the part we care
most about the wild one. Superloop most often you'll want to put your
setup code just before the wild loop and your execute forever stuff inside the loop. Feel free to look through the rest of the
code generated by the graphical tool. You can see library functions being called
to initialize system clocks and various peripherals. You will rarely have to change any of this,
but you might need to say change the URL baud rate. We can also see how our GPIO pins are being
initialized. Scroll back up to the wild loop. I say we write some code between one of the
comment guards, and let's say we write some other code that exists outside a set of begin
and end the comment guards I'll then save my work. Next we go back into the graphical tool and change
a hardware configuration I'll switch pin to output, and then switch it back just to illustrate
this point. The little asterisk next to the file tab shows
us that we've made changes. So we click file save and it asks us to generate
code again, click yes, and go back to our main function. You can see that the code we wrote between
the comment guards persisted after cogeneration, however, test two, which was outside the guards
after an end. But before I begin was deleted, the first
time you start development with an ARM microcontroller, it can be a little daunting. You can use assembly C, C++ ARM's cm, SIS
library, or the manufacturers specific libraries. If you take a look in project properties,
C, C++ build settings, you can see that we're using the good new tool chain for STM32. This is good news for us as this tool chain,
along with GCC are free, but that doesn't answer which libraries we should use. This diagram on ARM's site has a good overview
of how the libraries work together. Arm created the cortex microcontroller software
interface standard or CM says to be a set of libraries that help you control registers
and set up peripherals on any arm cortex controller. Well, this is great for learning arm development. In general, it won't necessarily create portable
code. As each microcontroller can have different
registers and peripherals as defined by the Silicon manufacturer, ST's hardware abstraction
layer, or how makes controlling some of the peripherals in the STM. 32 parts, a little
easier as it handles much of the setup for us. In most cases, you'll also find that if you
write HAL code for one STM32 microcontroller, it should be able to run on another STM32
microcontroller. This helps with code portability, at least
within the STM32 family. Unfortunately, most of the HAL documentation
exists within a series of PDFs published by St which makes things a little hard to navigate. The best way I've found of finding these docs
is to search for the family. I'm working with say STM32 L4 and how in the
of contents, the peripherals are mostly in alphabetical order. We want to blink an led. So look for GPIO in that section, we want
to just toggle. So look for how underscore GPIO underscore
Tuggle pin. It looks like we need to give this function
a port letter and a pin number. The house system driver also contains a useful
delay function that we'll use to wait between toggles back in the IDE click on the dot IOC
file in your project. This corresponds to the graphical setup tool
we were using earlier, zoom in, and you can see that port a pin five is connected to the
green led on the Nucleo board. So back in Maine, under the wild loop, right? Hal GPIO toggle pin GPIO comma GPIO pin 5
under that, right? HAL delay 1000, which will delay the microprocessor
for 1000 milliseconds click project build project. When that's complete click run debug as STM32
application, you'll be asked to create a new debug configuration, leave everything as default,
since it's automatically set up for the Nucleo board and click OK When asked click "switch" to open a new eclipse
perspective. Here, you'll see that you have a new toolbar, click the play button to start
running your application on the Nucleo board. At this point, you should see the green led
labeled LD two on your Nucleo board. Blink on for one second and off for one second. Here comes the fun part. Step through debugging with our code, still
running on the microcontroller. We can click the pause button to stop it temporarily
and see what line it's on. Open up the main dot see file again, and find
our toggle pin line double click the line number to place a break point, run the code
again, and it should stop at the break point. Now you can click the step into button to
execute line by line while delving into individual functions, press the step return button to
exit that function while executing the rest of the code in it. And if you want to execute a function without
stepping into it, you can press the step over button, double click on the line number again,
to remove the break point. You can then press the play button again to
resume running. Normally when you're done press the stop button,
which will return you to the code editing perspective. You can do this line by line debugging while
the code runs on actual hardware, the STM32 family and Nucleo boards act as a great introduction
to 32 bit ARM development. Next time we'll talk to a sensor using a communication
protocol. Please subscribe. If you want to keep up with these videos and
see you next time.