How to build with MBF framework and STM32 [solved] lazarus / 79-Embedded - ARM ---------------------------------------------------------------------- petex · 2021-01-15 09:57 hello, I am experiencing frustration with getting the MBF framework to work with STM32. I see lots of units related to the STM32 so I am guessing it does work. I have down loaded the package. From what I can see is that some (including cpuinfo.pas) of the existing units need to be patched with STM32 definitions before starting. Why does this need to be done ? Is it not possible to have a clean build ? Where are these units that need to be patched ? Are they part of the compiler ? I am guessing that some initial $DEFINES are required to define the particular controller, or maybe these are done behind the scenes by the compiler user options. (-CpARMV7M -WpSTM32F103X8 ) Is there a WIKI to explain how to use this framework with STM32 controller types ? I didn't see any sample or test projects using STM32. ---------------------------------------------------------------------- ccrause · 2021-01-15 10:33 Quote from: petex on January 15, 2021, 10:57:17 amI have down loaded the package. From what I can see is that some (including cpuinfo.pas) of the existing units need to be patched with STM32 definitions before starting. Why does this need to be done ? Is it not possible to have a clean build ? Where are these units that need to be patched ? Are they part of the compiler ? I am guessing that some initial $DEFINES are required to define the particular controller, or maybe these are done behind the scenes by the compiler user options. (-CpARMV7M -WpSTM32F103X8 ) Michael or maybe Don is best suited to help you get going with MBF. From quick inspection the patches add several STM controllers to the compiler.  Several files need to be updated for this to work.  If your particular controller is already supported by trunk you probably don't need the patches (I haven't studied them in detail). Quote from: petex on January 15, 2021, 10:57:17 amI am guessing that some initial $DEFINES are required to define the particular controller, or maybe these are done behind the scenes by the compiler user options. (-CpARMV7M -WpSTM32F103X8 )The compiler generates defines for the CPU type (subarch) and controller automatically (if the controller is supported by the compiler obviously). Have you tried compiling one of the examples without applying the patches?  If there isn't a project file for your particular controller, just open one of the existing ones and change the -Cp, -Wp and -d settings according to your desired controller. Edit: Apologies for the misinformation in the above sentence. It seems that MBF needs to provide controller specific settings which are provided in controller specific include files. Luckily Michael is now helping you! ---------------------------------------------------------------------- DonAlfredo · 2021-01-15 11:39 QuoteMichael or maybe Don is best suited to help you get going with MBF. Indeed. Michael will be best suited to help. Please note. Work is done to integrate FreeRTOS and FPC for ARM mCPU's. This looks very (VERY) promising. We are in the process of integrating our efforts and Michael will introduce the necessary changes into FPC trunk (if acceptable by the devs) shortly (I hope). Stay tuned. ---------------------------------------------------------------------- petex · 2021-01-15 12:02 The first error encountered is no definition for __CONTROLLERTYPE__ I assume this is substituted with "STM32f013" in order to pull in the specific STM32 modules. I tried setting it specifically e.g. {$DEFINE __CONTROLLERTYPE__ = STM32F103} {$DEFINE __CONTROLLERTYPE__  STM32F103} but that syntax doesn't work. I suppose the compiler sets this up like FPC_VERSION. Must be missing something here ???? ---------------------------------------------------------------------- MiR · 2021-01-15 12:14 Sorry for coming late to the party.... Too bad that you are experiencing troubles with mbf. I have been mostly working on mbf-freertos in the last time as this is (for me personally) the way to go in the future. I started using the git-wiki for fpc-freertos and based on your comment it looks like it makes sense to do the same thing for mbf. Please let me go through mbf with bluepill in mind, I will provide a new version later today based on what i find, i already found one or two places that could be done better for that special chip. Please also note that mbf for bluepill is curently only good for gpio, uart and spi, i2c does not work for stm32f1, but it is something that can be implemented, I just never needed it and it was never asked for. Michael ---------------------------------------------------------------------- MiR · 2021-01-15 12:23 @Petex: Can I ask you that you download latest fpcupdeluxe and as a preparation use fpcupdeluxe to install a version of fpc stable and lazarus stable or trunk to C:\fpcupdeluxe or $HOME/fpcupdeluxe on linux/mac Please use exactly those directory names, then you can use prebuild compiler binaries without the need to tweak paths (Which always makes things more complicated) When all works in the end then you can use whatever fpc/lazarus you have on your computer, but for the first steps it makes life a lot easier and cost only a little disk space. Please keep me posted on your progress.... ---------------------------------------------------------------------- petex · 2021-01-15 12:28 hello, I set this up in c:\fpcupdeluxe about 3 days ago. I shall re-update it, but it should be pretty much up-to-date and ready to go. ---------------------------------------------------------------------- MiR · 2021-01-15 12:31 Great! What is really important is that you do not try to install embedded support for arm from there and do not try to install mbf from within fpcupdeluxe. This will currently only install compilers for armv6m which will be of no use for you. Just select the fpc/lazarus versions and hit the button to build both, then you are all set. ---------------------------------------------------------------------- MiR · 2021-01-15 12:57 I started to fill the WiKi for mbf, please check if what I write maskes sense.... ---------------------------------------------------------------------- MiR · 2021-01-15 20:40 Sorry that it took longer than expected, I had to first remove the Dust from my Windows computer to be able to build the dependencies needed. I added a small Blinky example to the SamplesBoardSpecific directory so that you have a starting point to play arround. I own a Bluepill board and have compiled the sample on my Mac, it runs fine for me. Please have a look at the Wiki: https://github.com/michael-ring/mbf/wiki and check if the installation steps work for you. Michael ---------------------------------------------------------------------- trev · 2021-01-15 22:17 Quote from: MiR on January 15, 2021, 09:40:29 pmPlease have a look at the Wiki: https://github.com/michael-ring/mbf/wiki Why didn't you add this to the FPC + Lazarus Wiki? ---------------------------------------------------------------------- MiR · 2021-01-15 22:21 I can do that in a later step, the documentation may still be incorrect and misleading/wrong, when dust has setteled then I am happy to add the content to the Lazarus Wiki. ---------------------------------------------------------------------- trev · 2021-01-15 23:50 Great, thanks! ---------------------------------------------------------------------- petex · 2021-01-16 11:53 hello, i have finally got around to trying to build based on your WIKI instructions. I installed a fresh copy of laz/fpc for windows to the folder "fpcupdeluxe-mbf". There's a wee bit of confusion as you added the "-mbf" extension and some references were without. I unzipped the two packages to fpc folder as per wiki. I updated fpc.cfg. I opened up the blue pill blinky project and compiled...... Still getting errors Quote Compile Project, OS: embedded, CPU: arm, Target: Blinky: Exit code 1, Errors: 1, Hints: 2 Hint: Start of reading config file C:\fpcupdeluxe- mbf\fpc\bin\x86_64-win64\fpc.cfg Hint: End of reading config file C:\fpcupdeluxe- mbf\fpc\bin\x86_64-win64\fpc.cfg Verbose: Free Pascal Compiler version 3.2.1 [2021/01/15] for arm Verbose: Copyright (c) 1993-2020 by Florian Klaempfl and others Verbose: Target OS: Embedded Verbose: Compiling Blinky.lpr Fatal: Can't find unit system used by Blinky Verbose: Compilation aborted Verbose: C:\fpcupdeluxe-mbf\fpc\bin\x86_64-win64\ppcrossarm.exe returned an error exitcode ---------------------------------------------------------------------- MiR · 2021-01-16 12:51 The error message means that system.ppu could not be found. This can have two reasons: an issue with extracting the zip file or a typo in fpc.cfg. I have added those lines to the Wiki, please check, if this looks OK for you then please attach your fpc.cfg file located here: C:\fpcupdeluxe-mbf\fpc\bin\x86_64-win64\fpc.cfg ---snip Take the Path C:\fpcupdeluxe-mbf\fpc\units\$fpctarget\$fpcsubarch\rtl and replace $fpctarget with 'arm-embedded' and $fpcsubarch with 'armv7m' which will give you: C:\fpcupdeluxe-mbf\fpc\units\arm-embedded\armv7m\rtl Check that this directory exists and that it contains system.ppu Then change to the Directory C:\fpcupdeluxe-mbf\fpc\bin\x86_64-win64\ and check that the file arm-embedded-as.exe exists. When all checks are done you should be ready to compile your first program..... ---------------------------------------------------------------------- petex · 2021-01-16 13:42 hello, in fpc.cfg, the paths involve the folder "units" which doesn't tie in to the location of system.ppu. If i remove the /units, I get past the first hurdle. -FuC:\fpcupdeluxe-mbf\fpc/units/$fpctarget/$fpcsubarch/rtl -FuC:\fpcupdeluxe-mbf\fpc\arm-embedded\armv7m\rtl It now cannot find arm-embedded-as.exe not found which is probably related error. Another point is that when I copied the extra packages i was prompted to ovewrite fpc.cfg - I did not do this and notice the paths in your fpc.cfg do not have the -mbf extensions. It is confusing to the user here. ---------------------------------------------------------------------- petex · 2021-01-16 13:57 i can't find  "arm-embedded-as.exe" is this meant to be in the package d/l from git ? It looks like there are assembler,linker etc  files with "none-eabi" if i substitute with "embedded" something happens ---------------------------------------------------------------------- petex · 2021-01-16 14:09 if i change the names of the exes substituing "none-eabi" with "embedded" i get this...... QuoteCompile Project, OS: embedded, CPU: arm, Target: Blinky: Success, Warnings: 1, Hints: 7 Hint: Start of reading config file C:\fpcupdeluxe- mbf\fpc\bin\x86_64-win64\fpc.cfg Hint: End of reading config file C:\fpcupdeluxe- mbf\fpc\bin\x86_64-win64\fpc.cfg Verbose: Free Pascal Compiler version 3.2.1 [2021/01/15] for arm Verbose: Copyright (c) 1993-2020 by Florian Klaempfl and others Verbose: Target OS: Embedded Verbose: Compiling Blinky.lpr Verbose: Compiling C:\temp-svn\blue-pill\mbf- master\Source\MBF.STM32F1.SystemCore.pas Verbose: Compiling C:\temp-svn\blue-pill\mbf- master\Source\MBF.BitHelpers.pas Verbose: Assembling mbf.bithelpers MBF.ARM.SystemCore.inc(110,30) Warning: Calling convention directive ignored: "OldFPCCall" Verbose: Assembling mbf.stm32f1.systemcore Verbose: Compiling C:\temp-svn\blue-pill\mbf- master\Source\MBF.STM32F1.GPIO.pas MBF.STM32F1.GPIO.pas(513,8) Hint: Conversion between ordinals and pointers is not portable MBF.STM32F1.GPIO.pas(514,5) Hint: Conversion between ordinals and pointers is not portable MBF.STM32F1.GPIO.pas(515,5) Hint: Conversion between ordinals and pointers is not portable MBF.STM32F1.GPIO.pas(516,5) Hint: Conversion between ordinals and pointers is not portable MBF.STM32F1.GPIO.pas(517,23) Hint: Conversion between ordinals and pointers is not portable Verbose: Assembling mbf.stm32f1.gpio Verbose: Assembling blinky Verbose: Linking C:\temp-svn\blue-pill\mbf- master\SamplesBoardSpecific\Blinky-Bluepill\Blinky Debug: STM32F103XB Verbose: 72375 lines compiled, 6.8 sec, 5764 bytes code, 537 bytes data Verbose: 1 warning(s) issued Verbose: 7 hint(s) issued ---------------------------------------------------------------------- MiR · 2021-01-16 14:14 The errors before were 100% my fault, I did not doublecheck enough when I created the zip files. I have now uploaded fixed versions of the zip files to my server, to fix your install please do the following: create ftp\units directory move arm-embedded directory in this directory change the fpc.cfg file and change back to the proper directory name: -FuC:\fpcupdeluxe-mbf\fpc\units\$fpctarget\$fpcsubarch\rtl You can then either re-download and extract the binutils package or (if you have not already done) duplicate the arm-none-eabi- files and rename the second set to arm-embedded- Sorry for that, Michael and congrats, you successsfully compiled the Blinky project in your last step.... Grmpf.. had to edit this post several times, pleasse re-read in case there are still issues... ---------------------------------------------------------------------- petex · 2021-01-16 14:27 hello, I shall follow your last instructions. Getting closer, thanks for that. Quoteand congrats, you successsfully compiled the Blinky project in your last step.... Not so fast... The last (successful) build does not run on my setup. I have compared it to previous blinky project on Lazarus using the fpcupdeluxe embedded compiler which does run. ---------------------------------------------------------------------- MiR · 2021-01-16 14:44 I am just doing a clean install on windows to doublecheck my instructions.... Is there a bootloader installed on your Bluepill? I do not mean the ROM-Bootloader that you enable by pushing BOOT0 and NRST, that one if fine..... By the way, do you own a debug probe? if yes, which one? Michael ---------------------------------------------------------------------- MiR · 2021-01-16 15:16 FYI, just found out the hard way that it is necessary to install lazarus trunk when you want proper debugging, the last lazarus release lacks important changes that are needed for debugging. ---------------------------------------------------------------------- MiR · 2021-01-16 16:05 Following my own instructions, things look good, I can build and debug the Blinky example on my Windows computer and on my Mac without issue. I will now create & verify examples for uart and spi, but will likely take until tomorrow..... ---------------------------------------------------------------------- petex · 2021-01-16 16:20 hello Quote Is there a bootloader installed on your Bluepill? I do not mean the ROM-Bootloader that you enable by pushing BOOT0 and NRST, that one if fine..... By the way, do you own a debug probe? if yes, which one? I am a complete novice in using this Blue Pill card. I recently bought one to try out as it seemed so cheap and powerful. I am evaluating its capabilities and the development environments available at the moment. I really want to stick to Lazarus and not go back to C. The program is loaded through the serial port (A9 and A10). I had a go at using the USB loader but there's a lot of faffing about to get that to work. I don't own a probe for this. I've got a BDM probe for the centronics port somewhere, how likely is that going to work ? Maybe you could upload your bin or hex file so I can try running it. QuoteI will now create & verify examples for uart and spi, but will likely take until tomorrow..... hey man, no rush you have been v. helpful so far. This is a long term project!! ---------------------------------------------------------------------- MiR · 2021-01-16 16:44 It is quite important to know how exactly you do the upload, it looks to me that you are using the UART via BOOT0 and RESET Button. This then means that the uploaded program will run from flash start 0x08000000. For this start address my binary will work well. When you have however some Arduino bootloader on the board (but I think you would not use Serial in that case) then the start address of the program must first be changed. So which tool do you use for uploading your working example and which parameters do you use? By the way, I found this quite good page: https://www.electronicshub.org/how-to-upload-stm32f103c8t6-usb- bootloader/ which has a lot of different examples on how to upload for further reading... ---------------------------------------------------------------------- petex · 2021-01-16 17:15 Yes i am using the boot0 and reset button method I am using ST electronics loader - STMFlashLoader Demo.exe through a USB to TTL/CMOS Serial FTDI232 device at virtual port COM3. I use all the defaults, start address 8000000, verify , jump to user program. I will try your upload in due course, thanks. ---------------------------------------------------------------------- petex · 2021-01-16 17:18 your Blinky image works OK on my setup. There are considerable differences in file size. ---------------------------------------------------------------------- MiR · 2021-01-16 18:13 Is you local git repo up to date with mbf? Also, any changes when you do a clean build (or simple delete the lib directory and rebuild) Please also check that in your Blinky version the setting of the CPU speed is commented out, there is an issue in this routine... ---------------------------------------------------------------------- MiR · 2021-01-17 10:55 I have created bluepill samples for UART and SPI in the SamplesBoardSpecific directory, please pull the latest changes. The changeset is quite big because I saw that inlining of code does not work well in some cases for 3.2 and trunk compiler. Michael ---------------------------------------------------------------------- petex · 2021-01-17 11:59 I have downloaded the three zip files from the WIKI. I have copied evertything verbatim into the fpc folder. Have rebuilt the blinky program with set cpu clock commented out. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$INCLUDE MBF.Config.inc} uses  MBF.__CONTROLLERTYPE__.SystemCore,  MBF.__CONTROLLERTYPE__.GPIO; begin  SystemCore.Initialize;//  SystemCore.SetCPUFrequency(SystemCore.getMaxCPUFrequency);   GPIO.Initialize;  GPIO.PinMode[TNativePin.PC13] := TPinMode.Output;   repeat    GPIO.PinValue[TNativePin.PC13] := 1;    SystemCore.Delay(500);    GPIO.PinValue[TNativePin.PC13] := 0;    SystemCore.Delay(500);  until 1=0;end.     Successful build :- QuoteCompile Project, OS: embedded, CPU: arm, Target: Blinky: Success, Warnings: 1, Hints: 2 Hint: Start of reading config file C:\fpcupdeluxe- mbf\fpc\bin\x86_64-win64\fpc.cfg Hint: End of reading config file C:\fpcupdeluxe- mbf\fpc\bin\x86_64-win64\fpc.cfg Verbose: Free Pascal Compiler version 3.2.1 [2021/01/15] for arm Verbose: Copyright (c) 1993-2020 by Florian Klaempfl and others Verbose: Target OS: Embedded Verbose: Compiling Blinky.lpr Verbose: Compiling C:\temp-svn\blue-pill\mbf- master\Source\MBF.STM32F1.SystemCore.pas Verbose: Compiling C:\temp-svn\blue-pill\mbf- master\Source\MBF.BitHelpers.pas Verbose: Assembling mbf.bithelpers MBF.ARM.SystemCore.inc(110,30) Warning: Calling convention directive ignored: "OldFPCCall" Verbose: Assembling mbf.stm32f1.systemcore Verbose: Compiling C:\temp-svn\blue-pill\mbf- master\Source\MBF.STM32F1.GPIO.pas Verbose: Assembling mbf.stm32f1.gpio Verbose: Assembling blinky Verbose: Linking C:\temp-svn\blue-pill\mbf- master\SamplesBoardSpecific\Blinky-Bluepill\Blinky Debug: STM32F103XB Verbose: 72377 lines compiled, 0.9 sec, 5772 bytes code, 537 bytes data Verbose: 1 warning(s) issued Verbose: 7 hint(s) issued Downloaded the bin file and no success unfortunately. here are the build files.... ---------------------------------------------------------------------- MiR · 2021-01-17 12:35 The code you sent works on my Bluepill !!??!! I just uploaded it in the debugger and the LED blinks. Very strange... Grasping at straws: Can you please check, there is usually the pin number of the LED printed on the Silkscreen, does it say PC13? I have a newer Bluepill with USB-C connector, there the LED is on PB2. You can clone latest, I just added usage of the 8MHz XTAL on the board, perhaps that changes something (Grasping at straws here, too....) By the way, do you have a spare Raspberry Pi or another BluePill? Then we could build you a Debug Probe, programming embedded devices without a debug probe is for me worse than flying blind.... Michael ---------------------------------------------------------------------- petex · 2021-01-17 13:00 QuoteCan you please check, there is usually the pin number of the LED printed on the Silkscreen, does it say PC13? I have a newer Bluepill with USB-C connector, there the LED is on PB2. It says PC13 on the silkscreen. As i said before I compiled a program using the fpcupdeluxe default embedded compiler and that works fine. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; {$O-} var   sensorValue : integer; procedure delay;var  i : uint32;begin  for i := 0 to 500000 div 5 do    begin      asm        nop   // empty instruction      end;    end;end; begin  // turn on Port C  RCC.APB2ENR := RCC.APB2ENR or (%1 shl 4);   // Pin 13 from Port C to Output  PortC.CRH := $00300000;   // Note: The LED lights up at LOW.  while true do    begin      // Pin 13 - High      PortC.BSRR := 1 shl 13;      Delay;       // Pin 13 - Low      PortC.BRR := 1 shl 13;      Delay;    end; end.  QuoteBy the way, do you have a spare Raspberry Pi or another BluePill? Then we could build you a Debug Probe, programming embedded devices without a debug probe is for me worse than flying blind... Agreed. I do have another stm32 and pi zero so it would be cool to make up a probe. I would be happier just getting the v. simple example to work before making things more complex and error prone. QuoteI just uploaded it in the debugger and the LED blinks. Very strange... Is the file the same as generated in your environment ? I shall fiddle around and check for obvious signs..... ---------------------------------------------------------------------- MiR · 2021-01-17 13:05 What kind of STM? Another Bluepill? If not which chip does the 2nd STM have? If it is bluepill then converting it should be pretty easy. verifying size of the code will be difficult now that I have removed all inline code. When you build latest from git then I can check again. ---------------------------------------------------------------------- petex · 2021-01-17 14:03 hello, success i think..... Normally the programs start up immediately after download with the boot0 jumper in the progam mode. I put the jumper back to operational mode and pressed reset and the blink started. I changed the blink frequency and re-built etc. just to check i wasn't doing anything stoopid and it changed. So finally I have got the environment to work. Thanks for your help. Debugging, probes and all that malarkey can wait until a rainy day. ps. I got the serial program to run. It is reporting a clock of 80Mhz not 78 as is the spec. ??? Quote52 -> No Parity Eight Bits, One StopBit 14:12:48.852 -> 14:12:49.840 -> CPU Frequency: 8000000 14:12:49.840 -> Exact Baudrate: 115942 Baud 14:12:49.840 -> No Parity Eight Bits, One StopBit 14:12:49.840 -> 14:12:50.829 -> CPU Frequency: 8000000 14:12:50.829 -> Exact Baudrate: 115942 Baud 14:12:50.829 -> No Parity Eight Bits, One StopBit 14:12:50.829 -> 14:12:51.868 -> CPU Frequency: 8000000 14:12:51.868 -> Exact Baudrate: 115942 Baud 14:12:51.868 -> No Parity Eight Bits, One StopBit 14:12:51.868 -> 14:12:52.860 -> CPU Frequency: 8000000 14:12:52.860 -> Exact Baudrate: 115942 Baud 14:12:52.860 -> No Parity Eight Bits, One StopBit 14:12:52.860 -> 14:12:53.853 -> CPU Frequency: 8000000 14:12:53.853 -> Exact Baudrate: 115942 Baud 14:12:53.853 -> No Parity Eight Bits, One StopBi ---------------------------------------------------------------------- MiR · 2021-01-18 14:45 Not sure if this helps much, at least you will not need to use serial upload anymore: Please look here: https://github.com/Serasidis/STM32_HID_Bootloader and download ltest release here: https://github.com/Serasidis/STM32_HID_Bootloader/releases Once you have flashed this mini-bootloader you can use my Blinky-BluePill-HIDboot.lpi file to build the Blinky example, the lpi contains this addition line: -k-Ttext=0x08000800 which tells the linker that the program should start behind the bootloader. Works for me on my Mac, First I have to start ./hid-flash Blinky.bin cu.usbmodem to prepare the upload and then I attach the device with boot1 switch enabled. The process should be more simple than that, but I did not read all the details as I have a proper debug probe and just tried this tool for fun. Michael ---------------------------------------------------------------------- MiR · 2021-01-18 21:15 Found an UF2 Bootloader for Bluepill: https://github.com/mmoskal/uf2-stm32f103 Prebuilt binary: https://github.com/mmoskal/uf2-stm32f103/releases This also works well for development, the bluepill shows up as a usb- stick and you can simply drop your binary (converted to uf2 format) to the drive. A press of reset switches between bootloader mode and app mode for me. I have added a sample lpi file and Blinky.uf2 to the Blinky-BluePill project directory. You can convert properly built bin files to uf2 with the tool uf2conv.py: https://github.com/microsoft/uf2/blob/master/utils/uf2conv.py I am thinking about adding uf2 support to Freepascal, a lot of SAMD21/SAMD51 Boards come with an preinstalled UF2 bootloader.