Adding Custom Board Questions/Feedback

The official STMicroelectronics Arduino core
tomvdb
Posts: 9
Joined: Mon Jul 17, 2017 2:10 pm

Adding Custom Board Questions/Feedback

Post by tomvdb » Tue Jul 18, 2017 9:35 am

Hi,

Thanks for all the work on the STM Core for Arduino. It's awesome.

At our local makerspace we have been putting together a stm32 based mini robot controller. It uses the STM32F042P6. The idea of using arduino to program it appeals to a lot of our members.

Image

I've spent some time following https://github.com/stm32duino/wiki/wiki ... nt-(board) to add our board, modified files are here:https://github.com/tomvdb/EDUBOT

I managed to add it and compile the blink which is working :D I still need to test other pheriperals.

One of the issues I had along the way was with Step 5, adding the System Clock Configuration. The SystemClock_Config generated calls the Error_Handler function if there is an issue. This function is not part of stm32core so it fails compilation. I removed reference to it from the SystemClock_Config and it worked fine. Just something to take note off.

Code: Select all

void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.HSI14CalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

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

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }

    /**Configure the Systick interrupt time 
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick 
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
My current issue is programming the board. The current options are mass storage or stlink. Our board uses usb dfu. I dug around in platform.txt file and it seems simple enough to add it using the command line dfu-util. Unfortunately I can't get the dfu-util to work (or rather detect our board).

Code: Select all

dfu-util -l
returns nothing, but the dfu tools from st works fine for the board. Does the dfu-util in the tools/win work with stm32 usb dfu?

It seems that if I can get it working from the commandline then I can probably add the necessary paths to platform.txt to add a dfu option.

Other things on the todo list would be to get the usb serial working for debug messages to the arduino monitor. A problem is see is that in stm32cubemx I need to select "Pins PA11/12 instead of pins PA9/10" in the SYS option to enable the usb pins before I can enable the usb. I'm not sure how that would need to translate to the arduino side since it is not set in the systemclock config, but rather in the stm32f0xx_hal_msp.c file:

Code: Select all

__HAL_REMAP_PIN_ENABLE(HAL_REMAP_PA11_PA12);
Maybe I can just add that line to the systemclock_config function ?

tomvdb
Posts: 9
Joined: Mon Jul 17, 2017 2:10 pm

Re: Adding Custom Board Questions/Feedback

Post by tomvdb » Tue Jul 18, 2017 11:05 am

ok, I managed to hack a dfu loader together. It's not pretty but it works:

I used this https://hackaday.io/project/4139-stm32- ... -converter to convert the hex file to a dfu file, and then used the dfuse command line tool from st.

I had to change platforms.txt to generate a hex file instead of a bin file and also add the following dfu upload section:

Code: Select all

# dfu upload
tools.dfu_upload.cmd=dfu_upload
tools.dfu_upload.cmd.windows=dfu_upload.bat
tools.dfu_upload.path.windows={runtime.tools.STM32Tools.path}/tools/win
tools.dfu_upload.path.macosx={runtime.tools.STM32Tools.path}/tools/macosx
tools.dfu_upload.path.linux={runtime.tools.STM32Tools.path}/tools/linux
tools.dfu_upload.path.linux64={runtime.tools.STM32Tools.path}/tools/linux64
tools.dfu_upload.upload.params.verbose=-d
tools.dfu_upload.upload.params.quiet=
tools.dfu_upload.upload.pattern="{path}/{cmd}" "{build.path}/{build.project_name}.hex" "{build.path}/{build.project_name}.dfu"
I created the following dfu_upload.bat (based on the stlink bat file) file to do the conversion and the upload:

Code: Select all

@echo off

rem: Note %~dp0 get path of this batch file
rem: Need to change drive if My Documents is on a drive other than C:
set driverLetter=%~dp0
set driverLetter=%driverLetter:~0,2%
%driverLetter%
cd %~dp0
rem: the two line below are needed to fix path issues with incorrect slashes before the bin file name
set str1=%1
set str1=%str1:/=\%

set str2=%2
set str2=%str2:/=\%

dfu_commandline %str1% %str2%
dfusecommand -c -d --fn %str2%

In the boards.txt file I added the following to add the dfu option in the arduino menu

Code: Select all

Nucleo_64.menu.upload_method.DFUMethod=DFU USB
Nucleo_64.menu.upload_method.DFUMethod.upload.protocol=DFU
Nucleo_64.menu.upload_method.DFUMethod.upload.tool=dfu_upload
It's not pretty, and probably not the right way of doing this, but it works for now. I tested my variant and inputs and outputs work and the onboard serial port also works

User avatar
Wi6Labs
Posts: 25
Joined: Fri Sep 16, 2016 11:39 am
Location: Rennes, France
Contact:

Re: Adding Custom Board Questions/Feedback

Post by Wi6Labs » Tue Jul 18, 2017 11:55 am

Hi tomvdb,

Thank you to use the STM32 Core for Arduino.
I'm enjoyed to see all is working for you.
One of the issues I had along the way was with Step 5, adding the System Clock Configuration. The SystemClock_Config generated calls the Error_Handler function if there is an issue. This function is not part of stm32core so it fails compilation. I removed reference to it from the SystemClock_Config and it worked fine. Just something to take note off.
It is sure something is missing inside the step 5. I will see with ST if we should add Error_Handler or a sentence inside the wiki.
ok, I managed to hack a dfu loader together. It's not pretty but it works
Great job. It is true the native STM32 Core do not support (yet?) the DFU mode. Did you see the PR #61?
You could open a pull request to propose your work. The community could help you to make it more pretty ;)
Wi6Labs team

tomvdb
Posts: 9
Joined: Mon Jul 17, 2017 2:10 pm

Re: Adding Custom Board Questions/Feedback

Post by tomvdb » Tue Jul 18, 2017 12:29 pm

Its looking very good. Very happy from my side so far. I tested the pwm and the analog inputs. All seems well. I did find a bug in pins_arduino.h regarding the digitalPinToInterrupt macro. It was missing a parentheses. Fixed that and now my interrupt works.

Pull request for the fix

https://github.com/stm32duino/Arduino_C ... 32/pull/65
Great job. It is true the native STM32 Core do not support (yet?) the DFU mode. Did you see the PR #61?
You could open a pull request to propose your work. The community could help you to make it more pretty
I'll checkout #61. I copied various files from other projects to make it work, so not sure I can just add it in. Also, it doesn't have linux/mac binaries. Will still play with it.

Obviously I would like to have our other makerspace members play with it as well. But having a custom board it would mean that they need to do modifications, etc to stm32 arduino core. In short, can we do a pull request for adding custom boards ? or what would be the best way to handle it?

User avatar
Wi6Labs
Posts: 25
Joined: Fri Sep 16, 2016 11:39 am
Location: Rennes, France
Contact:

Re: Adding Custom Board Questions/Feedback

Post by Wi6Labs » Wed Jul 19, 2017 9:20 am

Firstly, thank you for the fix.

Secondly, you're right. Maybe I spoke too quickly. To add a custom board which impacts the core will be difficult.
But propose some features to the core because you find a way to add it and make it generic enough to work with all variants, we'll take it.

I don't have the final word. You open a real discussion about this subject.
Wi6Labs team

tomvdb
Posts: 9
Joined: Mon Jul 17, 2017 2:10 pm

Re: Adding Custom Board Questions/Feedback

Post by tomvdb » Wed Jul 19, 2017 11:28 am

Ideally one would like to use the board manager to create a variant from my custom board that is dependent on the stm32-arduino core. So the user would need to install the stm32-core first and then my custom board (which essentially adds the variant).

Normally a custom board would not need to change any files to the core except for the following:

Code: Select all

Edit cores/arduino/stm32/stm32_def_build.h and add the CMSIS_STARTUP_FILE definition.

Code: Select all

#elif defined(STM32F207xx)
#define CMSIS_STARTUP_FILE "startup_stm32f207xx.s"
These can still be handled as addons to the core since you would only need to do it once for every chip, can probably build a list of all of them without affecting anything else?

The other file is this one:

Code: Select all

boards.txt
It wouldn't surprise me if the boards manager can already handle this in some way. Lots of custom arduino boards have their own entry in the boards manager but they use the arduino core.

tomvdb
Posts: 9
Joined: Mon Jul 17, 2017 2:10 pm

Re: Adding Custom Board Questions/Feedback

Post by tomvdb » Wed Jul 19, 2017 1:42 pm

ok, so I managed to figure out how to add a variant without messing with the core (with one exception). I basically created my own package for my board. This involves creating a json file with the download package information.

Mine is located here:

Code: Select all

https://www.binaryspace.co.za/edubot/package_edubot_index.json
and the main package is located here:

Code: Select all

http://www.binaryspace.co.za/edubot/edubot-1.0.0.zip
The main package basically consists of the variant/EDUBOT folder as created by following the 'add a variant page'

It also has the platform.txt (essentially a copy of the main core platform.txt with the name changed
my own boards.txt that has just my own board definition in there

Originally when I did this it tried to access the core files in my own directories which obviously doesn't exist, but I figured out that you can setup the board core in the boards.txt file

Code: Select all

YourBoard.build.core=STM32:arduino
Then all the paths update correctly to the installed stm32 arduino core

The only change I still had to do to the original core was to add the correct definition of the cmsis startup file to cores/arduino/stm32/stm32_def_build.h, but since that is a generic partnumber and not linked to a specific board I'm thinking thats ok since it only needs to be added once for each part (and can be added to the core via a pull request or just by adding all the possibilities in one go).

User avatar
RogerClark
Posts: 7680
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Adding Custom Board Questions/Feedback

Post by RogerClark » Wed Jul 19, 2017 9:55 pm

Thanks.

Someone on another forum recently told me about being able to add new boards to an existing core, but I have not had time to try it.

I will take a look at your files as if we can separate out the boards into different json files it will simply the number of variants in the core repo

We could also do things like bake json files for the blue pill and black pill etc etc

User avatar
Wi6Labs
Posts: 25
Joined: Fri Sep 16, 2016 11:39 am
Location: Rennes, France
Contact:

Re: Adding Custom Board Questions/Feedback

Post by Wi6Labs » Thu Jul 20, 2017 1:06 pm

tomvdb,
The only change I still had to do to the original core was to add the correct definition of the cmsis startup file to cores/arduino/stm32/stm32_def_build.h, but since that is a generic partnumber and not linked to a specific board I'm thinking thats ok since it only needs to be added once for each part (and can be added to the core via a pull request or just by adding all the possibilities in one go).
It is planned to fully completed the stm32_def_build.h file. But PR works too ;)

Roger,

It will be great if it is possible. I will open an issue about this feature.
Wi6Labs team

tomvdb
Posts: 9
Joined: Mon Jul 17, 2017 2:10 pm

Re: Adding Custom Board Questions/Feedback

Post by tomvdb » Fri Jul 21, 2017 11:21 am

We have been playing with it last night at our makerspace meetup and it worked great. Everyone could just install the stm32 arduino core using the boardsmanager and then my edubot variant (also using the boardsmanager). It went smoothly without issues and we had everyone up and running in minutes :)

Post Reply