# Setting up STM32

### Prepare

{% stepper %}
{% step %}
**Install the** [**STM32CubeProgrammer**](https://www.st.com/en/development-tools/stm32cubeprog.html) to upload to the STM32 microcontroller (available for Windows and Mac OS 64 bit).
{% endstep %}

{% step %}
**Install the** [**Dependencies**](https://docs.feelix.xyz/uploading-files/simplefoc)
{% endstep %}
{% endstepper %}

{% hint style="warning" %}
Make sure the device is in **BOOT mode** when uploading with Arduino IDE / Platform IO
{% endhint %}

{% hint style="info" %}
The **RESET** button on the STM32 micrcontroller needs to be pressed after switching between **BOOT** and **RUN mode** in order to read the change
{% endhint %}

<figure><img src="https://409642222-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M3MZhIYg7iSyaPwgNKY%2Fuploads%2FO9k1eygfqsDQnRmqfwE8%2Fuploading.png?alt=media&#x26;token=274e5c9e-8c5f-42ef-8040-9c2963eacda8" alt=""><figcaption></figcaption></figure>

### Option 1: Arduino IDE

{% stepper %}
{% step %}
Open Preferences > Additional Boards Manager URLs
{% endstep %}

{% step %}
Add a new line with the following URL: *<https://github.com/stm32duino/BoardManagerFiles/raw/main/package\\_stmicroelectronics\\_index.json>*

{% hint style="danger" %}
Installed Libaries and Boards Managers should match the version specified in the documentation below.
{% endhint %}

<figure><img src="https://409642222-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M3MZhIYg7iSyaPwgNKY%2Fuploads%2FFAYPbZhE2BQ9rDkpQyCd%2Fpreference_boardsmanager.png?alt=media&#x26;token=009eeae8-7dee-4c97-a68f-094d33d56620" alt=""><figcaption><p>Additional Boards Manager URLs window</p></figcaption></figure>
{% endstep %}

{% step %}
Open the **Boards Manager** from the Tools menu.
{% endstep %}

{% step %}
Search for '**STM32 MCU based boards**' and click **INSTALL v2.12.0**

<figure><img src="https://409642222-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M3MZhIYg7iSyaPwgNKY%2Fuploads%2FAyHTdYRSwDS3JZiqzKIu%2Fboardsmanager.png?alt=media&#x26;token=ab4077b5-cd16-4977-9811-f544a2dd7615" alt="" width="551"><figcaption></figcaption></figure>
{% endstep %}

{% step %}
Update the **Board Settings** in the Tools menu

* Set board to '**Generic STM32F4 series**'&#x20;
* Board part number to '**Generic F401RETx**'
* Upload method to '**STM32CubeProgrammer (DFU)**'
* USB support to '**CDC (generic 'Serial' supersede U(S)ART)**'
* U(S)ART support to '**Disabled (no Serial support)'**
* USB speed to '**Low/Full Speed**'

<figure><img src="https://409642222-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M3MZhIYg7iSyaPwgNKY%2Fuploads%2Fh68bQ4GwQAXYgRs0ceum%2Farduino_stm32_settings.png?alt=media&#x26;token=642ea3d1-67ba-4526-8760-c3b7839999aa" alt=""><figcaption><p>Arduino IDE Board Settings</p></figcaption></figure>

{% hint style="warning" %}
The USB Serial port is only used for **Serial Monitor**, when the device is in **RUN mode** the USB Serial port will appear in the Arduino IDE (Tools > Port)
{% endhint %}

{% hint style="info" %}
**The USB Serial port** **does&#x20;*****not*****&#x20;have to be specified** (having one specified should not cause any problems either).
{% endhint %}

{% endstep %}

{% step %}
Now you are ready to **upload** [**your program**](https://docs.feelix.xyz/downloads/c-library#library-for-exported-feelix-effects) using the upload button in the Arduino IDE&#x20;

After uploading the Arduino IDE or Platform IO will enable **RUN mode** automatically. \
If the board has been **RESET** or disconnected from the **POWER**, the switch should be manually changed to **RUN mode** in order to run the program.

After manually switching to **RUN mode** press the **RESET** button on the STM32 microcontroller.

The program should be running now, if not check out the [**troubleshoot page**](https://docs.feelix.xyz/uploading-files/troubleshoot).

{% hint style="info" %}
Press the **RESET** button when the device is not recognized by the computer. Make sure the switch is set to **RUN mode**.
{% endhint %}
{% endstep %}
{% endstepper %}

{% hint style="warning" %}
Method with Platform IO has not been tested recently.
{% endhint %}

### Option 2: Visual Studio & Platform IO

{% stepper %}
{% step %}
Add STM32 boards (search for ST STM)
{% endstep %}

{% step %}
When starting a new project, select STM32F401RE with framework Arduino

<figure><img src="https://409642222-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M3MZhIYg7iSyaPwgNKY%2Fuploads%2FpvMY17ppfHNo37e2t0Xf%2Fvisual_studio_board_setup.png?alt=media&#x26;token=f534ac22-a6c2-46cd-8609-ae8a2d60e29e" alt=""><figcaption><p>Visual Studio Project Board Settings</p></figcaption></figure>
{% endstep %}

{% step %}
Make sure the settings in the platform.ini file are as follows

```scheme
[env:genericSTM32F401RE]
platform = ststm32
board = genericSTM32F401RE
framework = arduino
upload_protocol = dfu
monitor_speed = 115200
;only for monitoring serial data 
;the monitor port name can be read when the device is in RUN mode
;monitor_port = COM3
board_build.mcu = stm32f401ret6
board_build.f_cpu = 84000000L
board_build.core = ststm32
build_flags = 
	-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
	-D USBCON
	-D USBD_VID=0x0483
	-D USBD_PID=0x0003
	-D USB_MANUFACTURER="Unknown"
	-D USB_PRODUCT="\"GENERIC_F401RETX\""
	-D HAL_PCD_MODULE_ENABLED
lib_deps = askuric/Simple FOC@^2.4.0
```

{% endstep %}

{% step %}
**Upload** your program using the upload button in Visual Studio

After uploading the Arduino IDE or Platform IO will enable **RUN mode** automatically. \
If the board has been **RESET** or disconnected from the **POWER**, the switch should be manually changed to **RUN mode** in order to run the program.

After manually switching to **RUN mode** press the **RESET** button on the STM32 microcontroller.

The program should be running now.
{% endstep %}
{% endstepper %}

{% hint style="info" %}
Press the **RESET** button when the device is not recognized by the computer. \
Check out the [troubleshoot page](https://docs.feelix.xyz/uploading-files/troubleshoot) when issues persist.
{% endhint %}
