SimpleFOC + Arduino + STM32F405RG [SOLVED]

Hi:

I’m trying to run SimpleFOC on a board based on STM32F405RGT6 for which the first thing is to get Arduino to work on said board.

I use PlatformIO as a development tool. I tried all possible configurations for the platformio.ino file provided in this list:

Captura de pantalla 2021-02-07 a las 0.10.31

After compiling the code and updating the board, I can’t even get a simple blik of the led to work: It compiles and uploads without any problem but nothing happens on the board.

I would like to know if anyone has tried to run SimpleFOC + Arduino on STM32F405RGT6 and if it occurs to them that I may be doing it wrong.

Best regards.

I got something very similar working a few weeks back. I had similar problems until i set up the timer/pll scalers.

To get the timers sorted i used CubeIDE device device configuration utility which has a
Graphical tool for configuring the clock scalers.

Once you’ve done that it code generated a function which you can copy and paste into the top of your main.cpp in platformio.

If you want i can paste my function, but it’ll only work if you have a HSE running at 16MHZ.

I think i had to set a #define HSE_VALUE 16000000

I’ve since burnt out my LDO on this custom chip :frowning:

1 Like

Hi @Owen_Williams,

I really appreciate that if you could copy and paste such timer/pll scalers.

Is SystemClock_Config ? I have CubeIDE installed and I use it to do another project with this board but I don’t know what function I have to copy and where I have to put it to make it work.

Regards

PD: I just copied the SystemClock_Config () function that I have in CubeIDE and call it from setup () but it seems to have no effect whatsoever.

Here is a gist of my code that was working before I burnt out my LDO (by applying 24V accidentally).

And here is my platformio.ini

[env:genericSTM32F405RG]
platform = ststm32
board = genericSTM32F405RG
board_build.f_cpu = 168000000L
; board = adafruit_feather_f405
; board = netduino2plus
framework = arduino

debug_tool = stlink
upload_protocol = stlink
; upload_flags = -c set CPUTAPID 0x2ba01477
build_flags =
    ; -D SERIAL_UART_INSTANCE=1
    -D HSE_VALUE=16000000U

; monitor_speed = 115200
monitor_speed = 1000000

lib_deps =
    git@github.com:simplefoc/Arduino-FOC.git#dev

Probably want to slow the serial down to 115200 (instead of 1MHz)

Like I said before - this is tuned to my external ocillator which runs at 16Mhz
image

If you have a 8Mhz or 25Mhz HSE (or no HSE at all) then my SystemClock_Config() won’t work for you and you’ll need to do your own work in that Clock config tool:
image

Don’t forget the -D HSE_VALUE=16000000U - it was working without it but the millis() etc was not correct without it e.g. 2 seconds took 1 second

1 Like

Hi @Owen_Williams

Your solution works like a charm !!! PC13 toggling like a devil . You make it happens.

Thank you so much :slight_smile:

Captura de pantalla 2021-02-07 a las 15.55.33

I publish here your smart and effective recipe applied to my case with 8 Mhz HSE and configuration and MCU running at 100 MHz in case someone else could have this issue.


platformio.ini :

[env:genericSTM32F405RG]
platform = ststm32
board = genericSTM32F405RG
board_build.f_cpu = 100000000L
framework = arduino

debug_tool = stlink
upload_protocol = stlink
build_flags =
    -D HSE_VALUE=8000000

monitor_speed = 115200
lib_deps = 
	askuric/Simple FOC@^2.0.2
	Wire
	SPI

And a simple sketch to verify that everything its is working:

#include <Arduino.h>
#include <SimpleFOC.h>

// This SystemClock_Config  is copy&paste from CubeIDE
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 100;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
}


void setup() {
    pinMode(PA8,OUTPUT);

}
void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(PA8, LOW);
  delay(100);
  digitalWrite(PA8, HIGH);
  delay(100);
}


Thank you so much again !!! :slight_smile: :wink:

2 Likes

This seems as good a place as any to link to my dual board based around stm32f405rg and btn8982s

driva-x2-3d

Beware - this is largely untested. I’ll know how much of it works when I receive it from JLCPCB mid march.

5 Likes

I am starting the design of a board with the same MCU. I’m still not sure which controller to use. I am at a fairly early stage. I’ll take a look, I’m sure I can learn something :slight_smile:

What is your target motor/power? I’m also playing with seeing if I can get SimpleFOC running on a vesc 4.12 which uses the same chip. Perhaps that is suitable?
https://github.com/vedderb/bldc-hardware/blob/master/design/BLDC_4.pdf?raw=true

I would like to build a BLDC motor controller to develop an robotic actuator that can be integrated into ROS and with it carry out different projects

I think that having 2 Nm of holding torque will be enough for my DIY projects. I’m still not clear about what BLDC motors are necessary to get that torque… a lot to investigate and learn yet.

2Nm Torque. Do you care if it uses gears or not?

Check out this youtube video I found for a torque test using a hoverboard motor:

In the video, Hans managed ~2.5kg at 50cm which I believe works out about 12Nm. Those hoverboard can handle 250W. The vesc 4.12 can power 1-2KW I think. I assume that no gears are involved.

So hoverboards are overkill. Take a look at this page which links to torques of various motors:

1 Like

Hey @Owen_Williams glad to see your board. My pcbs are on the way, offcourse with btn8982. I reviewed your schematic but Tja1051t/3 CAN transceiver works with 3.3v uCs but it should supply to 5v. I looked at many of them for my board but there are a few CAN ics working with 3.3v power supply like SN65HVD23x.

2 Likes

Thanks @alp - I managed to stop/reorder my jlcpcb with SN65HVD230. It is easy to miss these things!

1 Like

That’s awesome, man! You should have made a big announcement in its own thread! :smiley:

Hi,Owen_ Williams!
This project is amazing! Ask you how to calculate the real-time current feedback?
be deeply grateful!

Feng - the realtime current feedback is coming. The problem is that the solution is very board dependent. Antun (the main developer) has it working on his newer SimpleFOC boards but it doesn’t work on other boards yet. I’d like to get current sensing working with a drv8302.

My microcontroller is ESP32. By reading the IS terminal voltage of BTS7960, the current obtained according to the official gain ratio is shown in the figure. Why is this so? How do you verify his correctness?

// The BTS7960 Half Bridge Drivers have a current sense ratio of kILIS = IL / Iis = 8500.
// The external resistor Ris determines the voltage per output current. On the IBT2 there
// should be a 1kohm resistor for Ris. On my boards though, there are 10kohm resistors!
// Iis = Vis / Ris.
// Vis has to be measured on an analog port.
// Ie. Vis=0,5V with Ris=10Kohm: Iis = 0,5V/10Kohm = 50uA; IL = 8500Iis = 850050uA = 0,425A.
// Ie. Vis=0,05V with Ris=1Kohm: Iis = 0,05V/1Kohm = 50uA; IL = 8500Iis = 850050uA = 0,425A.
//Serial.println(avgAnalogRead(IA));
// Serial.println((avgAnalogRead(IA)-142)/0.913);//mv
Serial.println((avgAnalogRead(IA) - 142) / 0.913 / 1000 / 1000 * 8500);


HI,can you port the simplefoc library to stm32 using hal? using cubemx or cubeide? thanks

I imagine the code could be ported to cube fairly easily but that is not an aim of this project at the moment. We want to provide a library that works on a wide variety of Arduino board that support the Arduino wiring-based framework.

The SimpleFOC licence is permissive, so you are free to port, and I’d be interested to hear how you get on!

We don’t actually use much of the wiring api e.g the stm32 support already uses HAL for pwm.