💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • Tutorial: PlatformIO in VSCode
  • Step 1: Install Python
  • Step 2: Install VS Code
  • Step 3: Install PlatformIO Core
  • Step 4: Install PlatformIO IDE extension in VS Code
  • Step 5: Setting Up the Project in PlatformIO
  • Step 6: Manage Environment, Build and Upload
  • Creating a new file
  • Adding Library
  • Creating Environment
  • Selecting Environment, Build and Run
  • Appendix
  • PlatformIO Toolbar

Was this helpful?

  1. EC Course
  2. Tutorial

Tutorial: PlatformIO in VSCode

PreviousTutorial: PlatformIO in CLionNextTutorial: Repository Management

Last updated 6 months ago

Was this helpful?

Tutorial: PlatformIO in VSCode

Step 1: Install Python

You can install Python in Window by following the instruction here:

How to check if you already have Python

  • Open Window Command Prompt > Type python --version

  • If you see the version, then you already have python installed.

    image

Step 2: Install VS Code

Step 3: Install PlatformIO Core

You may not need this step if you are going to use VS Code. But, install anyway.

  • Download and save it as in py file

WIN OS: In command prompt

# change directory to the folder where is located downloaded "get-platformio.py"
cd C:/path-to-dir/where/get-platformio.py/is-located

# run it
python.exe get-platformio.py

MAC OS

# change directory to the folder where is located downloaded "get-platformio.py"
cd /path-to-dir/where/get-platformio.py/is-located

# run it
python get-platformio.py

Step 4: Install PlatformIO IDE extension in VS Code

  1. Open VS Code

  1. Open VSCode Package Manager

  2. Search for the official PlatformIO IDE in extension.

  3. Install PlatformIO IDE

Step 5: Setting Up the Project in PlatformIO

  1. Go to PlatformIO Home by clicking on the PlatformIO Icon

  2. Create New Project

  1. Name the project as EC or EC2024

  2. Select Setting as follows

  • Board: ST Nucleo F411RE

  • Framework: CMSIS

  • Location: Your EC workspace

Step 6: Manage Environment, Build and Upload

Open VSCode and select the EC workspace folder

  • eg.: \repos\EC\

Go to PlatformIO Home by clicking on the PlatformIO Icon

Creating a new file

You can start your Tutorial or LAB by following the instructions given in the report.

For example, for LAB_GPIO_DIO_LED, we created the project folder and the main program file, under the workspace of ...\repos\EC\ as

  • Folder: \LAB\LAB_GPIO_DIO_LED\

  • Main src: LAB_GPIO_DIO_LED.c

For this tutorial, create the project folder and program file as

  • Folder: \tutorial\TU_CreateProject_VSC\

  • Main src 1: TU_CreateProject_Example1_main.c

  • Main src 2: TU_CreateProject_Example2_main.c

TU_CreateProject_Example1_main.c


#include "stm32f4xx.h"

#define LED_PIN    5
#define BUTTON_PIN 13

// LED ON or OFF 

int main(void) {
/* Part 1. RCC Register Setting */
	// RCC Control Register (HSI)
	RCC->CR |= ((uint32_t)RCC_CR_HSION);
	// wait until HSI is ready
	while ( (RCC->CR & (uint32_t) RCC_CR_HSIRDY) == 0 ) {;}
	// Select HSI as system clock source
	// RCC Configuration Register
	RCC->CFGR &= (uint32_t)((uint32_t) ~(RCC_CFGR_SW));
	RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSI;
	// Wait till HSI is used as system clock source
	while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != 0 ) {;}
	// HSI is used as system clock
	// RCC Peripheral Clock Enable Register
	RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;

/* Part 2. GPIO Register Setting */
	// GPIO Mode Register
	GPIOA->MODER &= ~(3UL<<(2*LED_PIN));
	GPIOA->MODER |=   1UL<<(2*LED_PIN);


	// Dead loop & program hangs here
	while(1){
		// Turn ON LED2
		GPIOA->ODR |= (1UL << LED_PIN);

		// Turn OFF LED2
		//GPIOA->ODR &= ~(1UL << LED_PIN);
	}
}

TU_CreateProject_Example2_main.c


#include "ecSTM32F4v2.h"
#define LED_PIN PA_5
#define BUTTON_PIN PC_13

void setup(void);

int main(void) {
	setup();

	while (1) {
		delay_ms(1000);
		GPIO_write(LED_PIN, LOW);
		delay_ms(1000);
		GPIO_write(LED_PIN, HIGH);
	}
}


// Initialiization
void setup(void) {
	RCC_PLL_init();
	SysTick_init();

	// initialize the pushbutton pin as an input:
	GPIO_init(BUTTON_PIN, INPUT);
	// initialize the LED pin as an output:
	GPIO_init(LED_PIN, OUTPUT);
}

Adding Library

You can add your EC library header files under the directory of \include

You can add it under the directory of \lib. But you need to modify the include_dir folder in platformio.ini

Creating Environment

For every new TU or LAB, you can create a new environment that shares the MCU configuration. You do not need to re-configure the MCU setup everytime you create a new project. Simply, add a new environment that links your new program main file.

For this tutorial, we will learn how to add new environments for each TU_CreateProject_Example1_main.c, and TU_CreateProject_Example2_main.c.

Modify platformio.ini in the workspace folder as

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html


[platformio]
src_dir = .
include_dir = include

# Default environment setting
[env]
platform = ststm32
board = nucleo_f411re
framework = cmsis
debug_tool = stlink
build_flags = -Wl,-u,_print_float,-u,_scanf_float, -std=c11, -O3

##################################################################3
# User-Defined Environment

# For example: LAB GPIO
#[env:LAB_GPIO]
#build_src_filter = +<LAB/LAB_GPIO_DIO_LED/LAB_GPIO_DIO_LED.c> +<include/*.c>

# You can add new environments
[env:TU_CreateProject_Example1]
build_src_filter = +<tutorial/TU_CreateProject_VSC/TU_CreateProject_Example1_main.c> +<include/*.c>

[env:TU_CreateProject_Example2]
build_src_filter = +<tutorial/TU_CreateProject_VSC/TU_CreateProject_Example2_main.c> +<include/*.c>

Selecting Environment, Build and Run

  1. Click on Switch the environment on VSCode

  2. Select the Environment you want to build.

    • For this tutorial, first select [env:TU_CreateProject_Example1]

  3. BUILD. If you have MCU connected, you can also UPLOAD

  4. Now, Select the other Environment that uses your EC library [env:TU_CreateProject_Example2]

Appendix

PlatformIO Toolbar

PlatformIO IDE Toolbar is located in VSCode Status Bar (left corner) and contains quick access buttons for the popular commands. Each button contains hint (delay mouse on it).

  1. PlatformIO: Build

  2. PlatformIO: Upload

  3. PlatformIO: Clean

Refer to

Download python file of PlatformIO Core: 다른이름으로 링크저장:

image
image

image

Project environment switcher (if more than one environment is available). See [Section of .

Installation-guide: VS CDOE
get-platformio.py
Read more about it here
PlatformIO Home
Serial Port Monitor
PlatformIO Core (CLI)
env]
“platformio.ini” (Project Configuration File)
Install Python 3.x in Win OS