Let’s say Hello to the Hardware. A Magical Conversation!

Reading Time: 4 minutes

In recent blogs we discussed the consequences of writing directly to a Register’s Address, if you have not read that blog then I suggest you please go through that one, you’ll find it interesting.

What we are gonna do today?? Okay, let’s do something more interesting today we are not gonna work on registers nor in-depth of Hardware. Today we will talk with our hardware(stm32f3-discovery-board). Let’s Say Hello to hardware.

We will put a message inside our hardware and the hardware will print that message on our screen.

As we are working with the Embedded Systems they make our task fast and reliable. Also, they are much smaller in size compared to traditional computers, which makes them compact and portable, and useful for mass production. Management of Embedded Systems is pretty easy, as elements used in their creation are cheap & long-lasting. Embedded Systems are also cost-effective.


What to do about that?

Yes, we need to do something for carrying out this process. If you have an older version of the board then you don’t have to do anything because your board is already soldered but if you have a new one then you need to solder your board.

Don’t worry you can check this using the board User Manual(Page-No.21).

I found that my board is new and I have to manually solder it.


Why and what we are doing??

I know this question is coming to your mind and it should be.

  1. We are going to use ITM and iprint! macro to print the data from the micro-controller to the Console.
  2. Now to print the data to the console we need to solder our Board so that our ITM and iprint! work fine.

Steps to Solder our Board

  • Take a female-to-female jumper wire.
  • Connect them to the SWO and PB3.
connection

After this, we are good to go. Now we will be able to transfer the data from the microcontroller to the ITM Console. Let’s do this now…


The Code

We are going to use this code and we are going to say Hello to hardware(Discovery) on the ITM Console. You can go to this Repository for the following code and the dependencies used here.


#![no_main]
#![no_std]

/// This program is going to print to the itmdump terminal.
use cortex_m_rt::entry;
use cortex_m::{iprintln,peripheral::ITM};
use panic_itm as _;
use stm32f3_discovery::stm32f3xx_hal::prelude::*;

#[entry()]
fn main() -> !{

    let peripherals = cortex_m::Peripherals::take().unwrap();
    let mut itm = peripherals.ITM;


    iprintln!(&mut itm.stim[0],"Hello Discovery!!");
    

    loop {

    }
}

What are ITM and iprintln!

ITM

  • ITM stands for Instrumentation Trace Macrocell and it’s a communication protocol on top of SWD (Serial Wire Debug).
  • It can be used to send messages from the microcontroller to the debugging host.
  • This communication is only one way: the debugging host can’t send data to the microcontroller.

iprintln!

  • The iprintln macro will format messages and output them to the microcontroller’s ITM.
  • iprintln will help you to say Hello to hardware

So what happens is OpenOCD, which is managing the debug session, receives data sent through this ITM channel and redirects it to a file.

ITM protocol works with Frames. OpenOCD will receive these frames and write them directly to a file without parsing them.

Now, these frames which are present inside the file contain the data received from the microcontroller but the problem is that the data is not the original one. Now for that, we have to do something else.


What happens with the OpenOCD file?

If the microcontroller sends the string “Hello, world!” using the iprintln macro, OpenOCD’s output file won’t exactly contain that string.

To get the original string, OpenOCD’s output file will have to be parsed. We’ll use the itmdump program to perform the parsing as new data arrives.

Note: Before that, you need to install the itm and you can use this link for that.


Open itmdump terminal

For this follow these steps:-

Just open the terminal and run this command inside the /tmp directory for *nix OS OR from the %TEMP% directory for Windows.

$ # itmdump terminal

$ # *nix
$ cd /tmp && touch itm.txt

$ # Windows
$ cd %TEMP% && type nul >> itm.txt

$ # both
$ itmdump -F -f itm.txt

code

This command will block as itmdump is now watching the itm.txt file. Leave this terminal open.

The next thing you need to do is to run the openOCD but remember to run it from the same directory as itmdump.

We are all set now. The next thing we need to do is to connect our stm32f3 Board with the system and run the command to run openOCD.

/tmp$ openocd -f interface/stlink-v2-1.cfg -f target/stm32f3x.cfg

code

Note: If you are not familiar with the openOCD then you can visit this to set this up.


Run the program

Everything is set up now we just need to build/run the code and load it to the hardware.

Follow these steps now:

  1. cargo run
  2. step
  3. next or n
  4. next

code


Hello Discovery!

code

The magic is done already you can go and check your itmdump terminal you would surely see the output that came directly from the microcontroller.

That’s all in today’s blog I hope you liked it. Thanks for giving your time.


If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: https://bit.ly/2Vdlld7.

Magical Address

Written by 

Nitin is a Software Consultant, with experience of more than 1.4 years. He works on Rust Programming Language and Embedded Development using Rust. He is also fond of Java Programming & Artificial Intelligence. Apart from that, his hobbies are Watching Netflix, Reading, Singing & Writing.

1 thought on “Let’s say Hello to the Hardware. A Magical Conversation!5 min read

Comments are closed.