Use VSCode to compile and run STM32 projects - visual-studio-code

I been using Visual Studio and VSCode for a long time; therefore, it will be great if I can use it instead of Eclipse or Keil. Should I move to those IDEs? I like having no dependencies where I have a make file that compiles and flashes my code to an stm32. Moreover, I can customize VSCode a lot. For example I have a plugin called highlight and I have a regex that will highlight everything that is between USER CODE BEGIN and USER CODE END. As a result my code looks like this:
Now I know that my code can only exist on the highlighted regions. That way if I make any changes using CubeMx my code will not be lost.
Thanks to this videos I am able to debug, compile and flash stm32 projects:
Part 1: https://youtu.be/PxQw5_7yI8Q
Part 2: https://youtu.be/PxQw5_7yI8Q
For some reason I had to create a build directory at the root of my project for my make file to run.
Anyways my question is how can I remove the squiggles on VSCode? It will be great if VSCode gave me no errors/warnings
For example I get a lot of squiggles on the SystemCLock_Config function:
But that is strange because if I press F12 on the __HAL_PWR_VOLTAGESCALING_CONFIG function it takes me to ...MyProject\Drivers\STM32L1xx_HAL_Driver\Inc\stm32l1xx_hal_pwr.h and the definition is #define __HAL_PWR_VOLTAGESCALING_CONFIG(__REGULATOR__) (MODIFY_REG(PWR->CR, PWR_CR_VOS, (__REGULATOR__)))
So for some reason F12 is able to find the definition but not the compiler. A solution would be to place this at the top of my main.c file:
#ifdefine _DEBUG
#define PWR 0
#endif
But it is tedious to do that for every squiggly error.
This question from stack-overflow has help me remove some errors but not all:
uint32_t does not name a type - VSCode with STM32 in Windows.
Anyways from doing research from the internet this is how my c_cpp_properties.json file looks like:
{
"configurations": [
{
"name": "STM32 L1",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE",
"DUSE_HAL_DRIVER",
"DSTM32L152xE",
"STM32L1xx",
"__CC_ARM"
],
"intelliSenseMode": "gcc-arm",
"compilerPath": "${env:ARMGCC_DIR}/arm-none-eabi-gcc.exe",
"compilerArgs": [
"-mcpu=cortex-m3",
"-mthumb"
],
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
I am using this board: https://www.st.com/en/evaluation-tools/nucleo-l152re.html . I have also tried it with the bluepill and I get the same results.

Finally found the solution:
This file ...MyProject\Drivers\CMSIS\Device\ST\STM32L1xx\Include\stm32l1xx.h
Has this code
//.....
#if defined(STM32L100xB)
#include "stm32l100xb.h"
#elif defined(STM32L100xBA)
#include "stm32l100xba.h"
#elif defined(STM32L100xC)
#include "stm32l100xc.h"
#elif defined(STM32L151xB)
#include "stm32l151xb.h"
#elif defined(STM32L151xBA)
#include "stm32l151xba.h"
#elif defined(STM32L151xC)
#include "stm32l151xc.h"
#elif defined(STM32L151xCA)
#include "stm32l151xca.h"
#elif defined(STM32L151xD)
#include "stm32l151xd.h"
#elif defined(STM32L151xDX)
#include "stm32l151xdx.h"
#elif defined(STM32L151xE)
#include "stm32l151xe.h"
#elif defined(STM32L152xB)
#include "stm32l152xb.h"
#elif defined(STM32L152xBA)
#include "stm32l152xba.h"
#elif defined(STM32L152xC)
#include "stm32l152xc.h"
#elif defined(STM32L152xCA)
#include "stm32l152xca.h"
#elif defined(STM32L152xD)
#include "stm32l152xd.h"
#elif defined(STM32L152xDX)
#include "stm32l152xdx.h"
#elif defined(STM32L152xE)
#include "stm32l152xe.h"
#elif defined(STM32L162xC)
#include "stm32l162xc.h"
#elif defined(STM32L162xCA)
#include "stm32l162xca.h"
#elif defined(STM32L162xD)
#include "stm32l162xd.h"
#elif defined(STM32L162xDX)
#include "stm32l162xdx.h"
#elif defined(STM32L162xE)
#include "stm32l162xe.h"
#else
#error "Please select first the target STM32L1xx device used in your application (in stm32l1xx.h file)"
#endif
So I had to add the definition STM32L152xE on my c_cpp_properties.json file. My c_cpp_properties.json file contains this:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE",
"DUSE_HAL_DRIVER",
"DSTM32L152xE",
"STM32L152xE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "${env:ARMGCC_DIR}/arm-none-eabi-gcc.exe",
"cStandard": "c17",
"cppStandard": "c++20",
"intelliSenseMode": "gcc-arm",
"compilerArgs": [
"-mcpu=cortex-m3",
"-mthumb"
]
}
],
"version": 4
}

Related

Difficulty adding Paillier algorithm to perceptual hash implementation

Received error:
libNCPH.h:39:15: fatal error: paillier.h: No such file or directory
39 | #include <paillier.h>
In libNCPH.h, it is also in this way:
extern "C" {
#include <gmp.h>
#include <paillier.h>
#include "tcp.h"}

E0020 identifier "sleep" is undefined Visual Studio 2019

The Visual Studio 2019 shows E0020: "Sleep() is undefined" and C3861 'sleep':identifier not found.
I use "windows.h" but it doesn't work.
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <windows.h>
#include <dos.h>
#pragma comment(lib, "pthreadVC2.lib")
using namespace std;
..........
int main() {
pthread_t tid0;
pthread_t tid1;
pthread_create(&tid0, NULL, workerThreadFunc, (void*)&tid0);
pthread_create(&tid1, NULL, workerThreadFunc2, (void*)&tid1);
sleep(1); // E0020 identifier "sleep" is undefined**
THREADS_CREATED = 1;
pthread_exit(NULL);
return 0;
}
the "sleep(1)" should like this "Sleep(1)". The letter "s" should be capitalized.

Visual Studio Code's intellisense doesn't recognize Arduino port managing

I'm trying to use VScode for Arduino Uno early development. I'd like to use intellisense but in the "Problems tab" it shows me errors:
identifier "DDRD" is undefined
identifier "PIND" is undefined
identifier "PORTD" is undefined
identifier "PORTD" is undefined
I can compile and upload the code flawlessly and it works.
I read everywhere that the solution to this is adding at the very top of the code the macro definition of the microcontroller, but it seems that this doesn't work for me only.
This is my code
#define __AVR_ATmega328P__
#include <avr/io.h>
#include <util/delay.h>
int main(void) {
int switchState = 0;
DDRD &= ~(1 << 2);
DDRD |= 1 << 3;
DDRD |= 1 << 4;
DDRD |= 1 << 5;
for (;;) {
switchState = 0x01 & (PIND >> 2);
if (!switchState) {
PORTD |= 1 << 3;
PORTD &= ~(1 << 4);
PORTD &= ~(1 << 5);
} else {
PORTD &= ~(1 << 3);
PORTD &= ~(1 << 4);
PORTD |= 1 << 5;
_delay_ms(250);
PORTD |= 1 << 4;
PORTD &= ~(1 << 5);
_delay_ms(250);
}
}
return 0;
}
If I right-click a DDRD underlined macro and select "Go to definition" it brings me to the DDRD macro definition in io76c711.h, while I expect been redirected in iom328p.h, because io.h contains
#elif defined (__AVR_ATmega328P__)
# include <avr/iom328p.h>
It seems that AVR_AT76C711 is defined somewhere else but I can't wonder where.
Do you have any suggestion? I also post my c_cpp_properties.json if there is something wrong in the one generated by the VScode Arduino extension
{
"configurations": [
{
"name": "Win32",
"includePath": [
"C:\\Program Files (x86)\\Arduino\\tools\\**",
"D:\\Arduino\\libraries\\**",
"C:\\Program Files (x86)\\Arduino\\libraries\\**",
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\**",
"C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\**"
],
"forcedInclude": [
"C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino\\Arduino.h"
],
"defines": [
"USBCON"
],
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14"
}
],
"version": 4
}
Add "__AVR_ATmega328P__" to the define section of your c_cpp_properties.json file as follows:
"defines": [
"__AVR_ATmega328P__",
Then it will be able to find the correct AVR IO include file.
In your case, AVR_AT76C711 was just one possibility for the definition, but it likely wasn't actually defined in your project.
Note: the define will automatically be removed the next time that the IntelliSense Configuration is rebuilt (e.g.: Verify or Upload). This issue is currently tracked here.
The issue is that the header files with register definitions/initializations such as <avr/iom328p.h> are only included at compile-time by the compiler. As a result, intellisense has no clue about those definitions while error-checking your code. By manually defining which board you use or by setting a compiler argument (which would normally be set at compile-time), you can get intellisense to recognize the necessary header files.
Adding the flag "-mmcu=atmega328" to my Arduino configuration's compilerArgs (within c_cpp_properties.json) resolved this intellisense issue. Obviously change the atmega chip number as needed.
"configurations": [
{
"name": "Arduino",
"compilerPath":
"path\\to\\your\\compiler",
"compilerArgs": [
"-mmcu=atmega328"
],
"intelliSenseMode": "gcc-x64",
"includePath": [
"paths\\to\\include"
]
}
]
If this change keeps getting cleared, it will be necessary to disable the auto-generation of the c_cpp_properties.json file. This setting ID is : arduino.disableIntelliSenseAutoGen

How to define a undefinded reference to a function

I am working on two Beaglebone Black with Xenomai and RTnet. I have two c-files for a roundtrip ethernet frame between the BBB's. When I try to compile the first c-file there occur some errors:
undefined reference to 'rt_task_self'
rt_task_self is a function in my c-file and is declared in my headerfile "task.h". So in my opinion "undefined" means that it is just not defined in any cpp-file "task.cpp" for the headerfile "task.h".
But I am a little bit confused: How do I tell my program that my headerfile "task.h" is defined in my other file "task.cpp" or "task.o" or...
I have many header files in my C-file but only error with my "task.h" file and I do not see any differences in the #include rows between my "task.h" and all the other header files.
Part of Roundtrip C-file:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
/*XENOMAI*/
#include "task.h"
#include <rtdm/rtdm.h>
#include <asm/ioctl.h>
#define SERVER "192.168.127.10"
#define BUFLEN 512
#define PORT 8888
void die(char *s)
{
perror(s);
exit(1);
}
Part of task.h:
#ifndef _XENO_TASK_H
#define _XENO_TASK_H
#include <nucleus/sched.h>
#include <native/types.h>
/* Creation flags. */
#define T_FPU XNFPU
#define T_SUSP XNSUSP
/* <!> High bits must not conflict with XNFPU|XNSHADOW|XNSUSP. */
#define T_CPU(cpu) (1 << (24 + (cpu & 7))) /* Up to 8 cpus [0-7] */
#define T_CPUMASK 0xff000000
Part of another headerfile in the roundtrip c-file:
#ifndef _RTDM_H
#define _RTDM_H
#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/ioctl.h>
#include <linux/sched.h>
#include <linux/socket.h>
typedef u32 socklen_t;
typedef struct task_struct rtdm_user_info_t;
#else /* !__KERNEL__ */
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
My makefile:
# Allow overriding xeno-config on make command line
XENO_CONFIG=xeno-config
prefix := $(shell $(XENO_CONFIG) --prefix)
ifeq ($(prefix),)
$(error Please add <xenomai-install-path>/bin to your PATH variable)
endif
CC := $(shell $(XENO_CONFIG) --skin=posix --cc)
STD_CFLAGS := $(shell $(XENO_CONFIG) --skin=posix --cflags) -g
STD_LDFLAGS := $(shell $(XENO_CONFIG) --skin=posix --ldflags) -g -lrtdm
STD_TARGETS := rtt_rt
all: $(STD_TARGETS)
$(STD_TARGETS): $(STD_TARGETS:%=%.c)
$(CC) -o $# $< $(STD_CFLAGS) $(STD_LDFLAGS)
clean:
$(RM) -f *.o *~ $(STD_TARGETS)

Load library error in Matlab

I am trying to use loadlibrary in MATLAB by importing the qliblibrary header file but when I use the load library command:
loadlibrary('qlib','qlib.h')
I get following error:
Error using loadlibrary
Failed to preprocess the input file.
Output from preprocessor is:C:\Program Files\MATLAB\R2017a\extern\examples\Legacy_code\qlib.h:168:7: error: #error
unsupported compiler/os combination
#error unsupported compiler/os combination
This is the qlib header file where I am getting the compiler error:
#pragma message ("used compiler: CVI LabWindows C")
#define QLIB_WIN_32
#define QAPICALLER _stdcall
#ifndef FAR
#define FAR
#endif
#ifndef WORD
#define WORD unsigned short
#endif
#pragma message ("destination OS: Windows 9x / NT / 2000 / XP")
#pragma message ("executable type: CVI")
#elif defined (__GNUC__) /* gcc (on Linux) */
#define FAR
#define far
#ifdef __unix__
#define QLIB_LINUX
#define QAPICALLER
#else
#error unsupported compiler/os combination
#endif
#else
#error unsupported compiler detected /* unknown compiler */
#endif