# Going Through A Starter Project

0\. Create New Project

**File** -> **New** -> **STM32 Project**

Identify the MCU version that you are using. For this entire tutorial, we will be using STM32F446RET6 unless otherwise noticed.

<details>

<summary>Identifying the MCU version</summary>

</details>

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-44435b2c2eba5196d4df19deb0101ccc4f29c606%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Search for "STM32F446RET6" and select the first result. Then click **Next**.

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-3cf2ff7af87fb190f0be95ca205c9982cf1655bf%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Name the project, select a suitable location, and then keep the following settings. Then click **Finish**.

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-4102e4efd4f6b0e8c7cedf4d377db7e06b8a2f94%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

## 1. Configure STM32

STM32CubeIDE will open the .ioc graphical configuration utility first.

In the left sidebar, select **System Core** -> **SYS**.

Select Debug to use Serial Wire

![](https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-42a8d3ed52ed571fbb0170f37a29bd8e8c4319d9%2Fimage.png?alt=media)

Select **Connectivity** -> **USART2**.

Set **Mode** to be Asynchronous.

![](https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-434beffe4edeac9071d3483d5d2c4053ff1a0da7%2Fimage.png?alt=media)

In the Clock Configuration tab, set **HCLK** to **160 MHz** and click Enter, the IDE will automatically derive the required PLL parameters.

![](https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-19ad28b31909fa635e6c31968c262c28d6e875e8%2Fimage.png?alt=media)

Press **Ctrl+S**. STM32CubeIDE will start to generate the codes.

## 2. Code with Minimal Setup

In `main.h` and `main.c`, add the following code

{% tabs %}
{% tab title="Code" %}
**main.h**

<pre class="language-c"><code class="lang-c"><strong>/* Private includes ----------------------------------------------------------*/
</strong>/* USER CODE BEGIN Includes */
#include &#x3C;stdio.h>
#include &#x3C;string.h>
/* USER CODE END Includes */
</code></pre>

**main.c**

```c
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  uint32_t counter = 0;
  
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    char str[64];

    sprintf(str, "hello world %lu\r\n", counter);

    HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen(str), 1000);

    counter += 1;
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

```

{% endtab %}

{% tab title="Screenshot" %}
**main.h**

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-f0fdd50f92c73880100e4e19de764eb5f5685cdc%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

**main.c**

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-3de7716541bbb3367c008020135235fe6fe51e77%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}

## 3. Upload

Click the green "run ..." button

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-da12e2035862b14935b5eb9857d8201a587bd333%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

If there's a popup menu, click "OK".

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-d428463699dd9ca58df25d9b1b4495288e4af26d%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

## 4. Result

We should be able to see hello world with an upward counter printing from any serial monitor.

Recommend using the [VSCode Serial Monitor extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-serial-monitor) from Microsoft.

## 5. Code with Cleaner Hierarchy

Although our code is running now, putting everything into `main.c` and `main.h` isn't really a good idea ---- these files are autogenerated by STM32CubeIDE, and we would risk breaking it when putting and editing our code between those autogenerated lines.

Thus, a better way of managing the code would be to separate the system-managed code from our user code. Now we would create two new source files, `app.h` and `app.c`, and move our existing code there.

In the Project Explorer, right click the "Src" folder, select "New -> Source File".

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-a93b9e28faf6d735e96c7aaad32eb85b3ada2386%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Name the file "app.c" and click "Finish".

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-db5447cdca4885e89bce031c03c22353ff6a8ac2%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Similarly, in the Project Explorer, right click the "Inc" folder, select "New -> Header File".

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-c1cfde30294daf70f76aa095e007a54a6b2d54e0%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Name the file "app.h" and click "Finish".

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-dd36dad1ddd299776003dddcd0b348f215ef9237%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Then, we can move all the previous user codes into these newly created files.

{% tabs %}
{% tab title="Code" %}
**app.h**

```c
/*
 * app.h
 *
 *  Created on: Dec 30, 2022
 *      Author: TK
 */

#ifndef INC_APP_H_
#define INC_APP_H_

#include <stdio.h>
#include <string.h>

#include "stm32g4xx_hal.h"


void APP_init();

void APP_main();

#endif /* INC_APP_H_ */

```

**app.c**

```c
/*
 * app.c
 *
 *  Created on: Dec 30, 2022
 *      Author: TK
 */

#include "app.h"

uint32_t counter = 0;

void APP_init() {

}

void APP_main() {
  char str[64];

  sprintf(str, "hello world %lu\r\n", counter);

  HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen(str), 1000);

  counter += 1;
}

```

{% endtab %}

{% tab title="Screenshot" %}
**app.h**

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-701fe53f4371624a1c1db2df9b9936fd4fdde116%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

**app.c**

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-011f105613b2d8346ff18259ff0386eb6c1b2383%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}

Finally, we need to invoke the user application functions in `main.c`

{% tabs %}
{% tab title="Code" %}
**main.h**

```c
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "app.h"
/* USER CODE END Includes */

```

**main.c**

```c

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  APP_init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    APP_main();
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

```

{% endtab %}

{% tab title="Screenshot" %}
**main.h**

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-5291d0cea32482a7d9ab6138080761a056e2ffc8%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

**main.c**

<figure><img src="https://1287130752-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvVJ0h2a4qMIhB1I8GdV8%2Fuploads%2Fgit-blob-872d605f51f1eb79fb8e22ca35cad9fe24732620%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}
