STM32F042F6 programming

Hi,

is it possible to program a STM32F042F6 with platformio?

tks

Danilo

It currently does not have a built-in board definition for that you but you can create one yourself based on similiar boards + changing up the attributes, like disco_f030r8.json.

tks for the prompt replay bau not so easy for me… is there any topic you can link as exampl?

tks

Sure I can create an example. What framework do you want to program the board with? STM32CubeHAL, CMSIS, Arduino, …?

1 Like

arduino, many thanks

danilo

Download + open GitHub - maxgerhardt/pio-stm32f042f6 in VScode. It should compile fine. Default upload action is to flash it via an ST-Link :slight_smile:

great!

it works…many thanks

danilo

Can you suggest me a valid library to implement Can Bus on STM32F042F6?

tks

Danilo

Raw CAN functionality is included in the STM32 HAL code (see header and example) but

is an Arduino library you could use.

Note that the Arduino framework and these libraries have a lot of overhead. The code might be too big to fit in your chip, in which case you’d have to use other frameworks like STM32HAL directly.

tks again,

i tried to compile the Read.ino including:
#include <Arduino.h>
#include “STM32_CAN.h”
#include “hal_conf_extra.h”

but i get this error:

main.cpp:(.text.startup._GLOBAL__sub_I_Can+0xc): undefined reference to `STM32_CAN::STM32_CAN(CAN_TypeDef*, CAN_PINS, RXQUEUE_TABLE, TXQUEUE_TABLE)’

I can see that the library has two options for pin mapping (Tx and Rx):
STM32_CAN Can( CAN1, DEF ); //Use PA11/12 pins for CAN1.
//STM32_CAN Can( CAN1, ALT ); //Use PB8/9 pins for CAN1.

but STM32F042F6P6TR has CanBus on PA9 and PA10…

The comments may be wrong, but the CAN peripheral should know about its pin inherently.

Are you sure you included the library by using the lib_deps system in the platformio.ini?

lib_deps =
   https://github.com/pazi88/STM32_CAN/archive/refs/heads/main.zip

No…i did not…i simply copied the library .h file in the same folder where main.cpp is…

I will try (how? :slight_smile: ) and get back to you.

Thanks again

You should delete that .h again and use lib_deps.

A header file (.h) only contains declarations of functions, global variables and macros, but not the implementation, that’s in the .cpp file. They come in pairs.

Hi, I added

   https://github.com/pazi88/STM32_CAN/archive/refs/heads/main.zip

in platformio.ini and deleted .h as suggested.

I added this in platformio.ini : build_flags = -DHAL_CAN_MODULE_ENABLED as suggested in GitHub - pazi88/STM32_CAN: CAN bus Library for Arduino STM32.

Now my platformio.ini is:

[env:genericSTM32F042F6]
platform = ststm32
board = genericSTM32F042F6
framework = arduino
monitor_speed = 115200
lib_deps = https://github.com/pazi88/STM32_CAN/archive/refs/heads/main.zip
build_flags = -DHAL_CAN_MODULE_ENABLED

and my main.cpp is:

#include <Arduino.h>
#include "STM32_CAN.h"
STM32_CAN Can( CAN1, DEF );  //Use PA11/12 pins for CAN1.
static CAN_message_t CAN_RX_msg;

void setup() {
  Can.begin();
  Can.setBaudRate(500000);  //500KBPS
}

void loop() {
}

I get this error:

.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp: In member function 'void STM32_CAN::begin(bool)':
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:82:35: error: 'GPIO_AF9_CAN1' was not declared in this scope; did you mean 'GPIO_AF4_CAN'?
   82 |       GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
      |                                   ^~~~~~~~~~~~~
      |                                   GPIO_AF4_CAN
Compiling .pio/build/genericSTM32F042F6/FrameworkArduino/stm32/usb/cdc/cdc_queue.c.o
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:85:31: error: 'GPIO_SPEED_FREQ_VERY_HIGH' was not declared in this scope; did you mean 'GPIO_SPEED_FREQ_HIGH'?
   85 |       GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
      |                               ^~~~~~~~~~~~~~~~~~~~~~~~~
      |                               GPIO_SPEED_FREQ_HIGH
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:103:35: error: 'GPIO_AF9_CAN1' was not declared in this scope; did you mean 'GPIO_AF4_CAN'?
  103 |       GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
      |                                   ^~~~~~~~~~~~~
      |                                   GPIO_AF4_CAN
Compiling .pio/build/genericSTM32F042F6/FrameworkArduino/stm32/usb/cdc/usbd_cdc.c.o
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:105:31: error: 'GPIO_SPEED_FREQ_VERY_HIGH' was not declared in this scope; did you mean 'GPIO_SPEED_FREQ_HIGH'?
  105 |       GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
      |                               ^~~~~~~~~~~~~~~~~~~~~~~~~
      |                               GPIO_SPEED_FREQ_HIGH
Compiling .pio/build/genericSTM32F042F6/FrameworkArduino/stm32/usb/cdc/usbd_cdc_if.c.o
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:113:7: error: '__HAL_RCC_GPIOD_CLK_ENABLE' was not declared in this scope; did you mean 'HAL_RCC_MODULE_ENABLED'?
  113 |       __HAL_RCC_GPIOD_CLK_ENABLE();
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~
      |       HAL_RCC_MODULE_ENABLED
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:124:35: error: 'GPIO_AF9_CAN1' was not declared in this scope; did you mean 'GPIO_AF4_CAN'?
  124 |       GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
      |                                   ^~~~~~~~~~~~~
      |                                   GPIO_AF4_CAN
Compiling .pio/build/genericSTM32F042F6/FrameworkArduino/stm32/usb/hid/usbd_hid_composite.c.o
Compiling .pio/build/genericSTM32F042F6/FrameworkArduino/stm32/usb/hid/usbd_hid_composite_if.c.o
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:126:31: error: 'GPIO_SPEED_FREQ_VERY_HIGH' was not declared in this scope; did you mean 'GPIO_SPEED_FREQ_HIGH'?
  126 |       GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
      |                               ^~~~~~~~~~~~~~~~~~~~~~~~~
      |                               GPIO_SPEED_FREQ_HIGH
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:129:21: error: 'GPIOD' was not declared in this scope; did you mean 'GPIOA'?
  129 |       HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
      |                     ^~~~~
      |                     GPIOA
Compiling .pio/build/genericSTM32F042F6/FrameworkArduino/stm32/usb/usb_device_core.c.o
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:133:26: error: 'CAN1_RX0_IRQn' was not declared in this scope
  133 |     HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 15, 0); // 15 is lowest possible priority
      |                          ^~~~~~~~~~~~~
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:136:26: error: 'CAN1_TX_IRQn' was not declared in this scope
  136 |     HAL_NVIC_SetPriority(CAN1_TX_IRQn, 15, 0); // 15 is lowest possible priority
      |                          ^~~~~~~~~~~~
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp: In member function 'void STM32_CAN::enableMBInterrupts()':
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:739:24: error: 'CAN1_TX_IRQn' was not declared in this scope
  739 |     HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);
      |                        ^~~~~~~~~~~~
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp: In member function 'void STM32_CAN::disableMBInterrupts()':
.pio/libdeps/genericSTM32F042F6/STM32_CAN/STM32_CAN.cpp:759:25: error: 'CAN1_TX_IRQn' was not declared in this scope
  759 |     HAL_NVIC_DisableIRQ(CAN1_TX_IRQn);
      |                         ^~~~~~~~~~~~
Compiling .pio/build/genericSTM32F042F6/FrameworkArduino/stm32/usb/usb_device_ctlreq.c.o
Compiling .pio/build/genericSTM32F042F6/FrameworkArduino/stm32/usb/usb_device_ioreq.c.o
Compiling .pio/build/genericSTM32F042F6/FrameworkArduino/stm32/usb/usbd_conf.c.o
Compiling .pio/build/genericSTM32F042F6/FrameworkArduino/stm32/usb/usbd_desc.c.o
Compiling .pio/build/genericSTM32F042F6/FrameworkArduino/stm32/usb/usbd_ep_conf.c.o
*** [.pio/build/genericSTM32F042F6/lib54f/STM32_CAN/STM32_CAN.cpp.o] Error 1

help please…

…Seems like the library doesn’t explicitly take core of STM32F0x or F042 series chips and needs source code modifications. I’ll look into it.

1 Like

Hi, still stucked…could you give a look?

tks