B4R Tutorial How to Implement STM32 into B4R

If you want more FLASH memory space (say 128K flash), need more RAM (how about 20K), would like your program to run a little faster, or simply want to play around with a different micro-controller family, then have a go at this.

The included PDF document "How to Implement STM32 into B4R.PDF" contains the many steps required to get B4R to compile, upload and run code for a STM32F103RB Nucleo evaluation board.

en.nucleo-F1.jpg


Download the PDF document and get started.

Note:
Now revised 15th December 2016
Improved B4R integration using the existing STM32 library define
_VARIANT_ARDUINO_STM32_
instead of my own define
USING_STM32.
Now revised 11th August 2020
No need to alter the B4R file, Now included in B4R distribution.
See revised "How to Implement STM32 into B4R" document now
up to date with current distribution of STM32 Library.
 

Attachments

  • How to Implement STM32 to B4R.pdf
    207.7 KB · Views: 638
Last edited:

Starchild

Active Member
Licensed User
Longtime User
Here is a pin out map of the Arduino Headers on the STM32F103 Nucleo board to see the compatibility with Arduino programming (reference for B4R programming).

st-nucleo-f103rb-arduino_2.png

This diagram show the GPIO connectors pin outs for a more complete look at the I/O of the STM32F103RB Nucleo board.
cyfnl9cezh8k0buaz.gif
 
Last edited:

whcir

Member
Licensed User
Longtime User
If you want more FLASH memory space (say 128K flash), need more RAM (how about 20K), would like your program to run a little faster, or simply want to play around with a different micro-controller family, then have a go at this.

The included PDF document "How to Implement STM32 into B4R.PDF" contains the many steps required to get B4R to compile, upload and run code for a STM32F103RB Nucleo evaluation board.

en.nucleo-F1.jpg


Download the PDF document and get started.

Starchild,

I read your "How to Implement STM32 into B4R.PDF" and made the changes you outlined. loaded the simple blink program and nothing. The program compiles and loads ok. I used the Arduino IDE and blink program and everything works as it should, so everything is connected right. I'm not using the Nucleo board, I'm using a nano type STM32f103c8t6 clone so I modified the files in that folder under Arduino_STM32/STM32F1/variants/generic_stm32f103c not the nucleo folder(is that correct?). My code is posted below, what am I doing wrong

B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private Timer1 As Timer
   Private pin13 As Pin
End Sub
Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   pin13.Initialize(2, pin13.MODE_OUTPUT)
   Timer1.Initialize("Timer1_Tick", 1000) '1000ms = 1 second
   Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick
   Dim currentState As Boolean = pin13.DigitalRead
   Log("CurrentState: ", currentState)
   Dim NewState As Boolean = Not(currentState)
   Log("NewState: ", NewState)
   pin13.DigitalWrite(NewState)
End Sub
 

Starchild

Active Member
Licensed User
Longtime User
Starchild,

I read your "How to Implement STM32 into B4R.PDF" and made the changes you outlined. loaded the simple blink program and nothing. The program compiles and loads ok. I used the Arduino IDE and blink program and everything works as it should, so everything is connected right. I'm not using the Nucleo board, I'm using a nano type STM32f103c8t6 clone so I modified the files in that folder under Arduino_STM32/STM32F1/variants/generic_stm32f103c not the nucleo folder(is that correct?). My code is posted below, what am I doing wrong

B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private Timer1 As Timer
   Private pin13 As Pin
End Sub
Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   pin13.Initialize(2, pin13.MODE_OUTPUT)
   Timer1.Initialize("Timer1_Tick", 1000) '1000ms = 1 second
   Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick
   Dim currentState As Boolean = pin13.DigitalRead
   Log("CurrentState: ", currentState)
   Dim NewState As Boolean = Not(currentState)
   Log("NewState: ", NewState)
   pin13.DigitalWrite(NewState)
End Sub

I have only done testing with the STM32F103RB Nucleo board.
I did briefly connected up a generic STM32F103C8T6 ( one with a JTAG programming interface on board) which connected to an external ST-LINK dongle and back to the PC USB.
I had problems B4R compiling/programming this using any of the available generic C8 board types. But, if I selected the "STM32F103RB Mucleo" board type it would compile and program with your BLINK example.
My difference is that I connected my LED to PA5 (being arduino pin 13, not 2)

How are you programming your C8 board?
Which Board Type did you try?

The B4RBoardsExtractor.jar has trouble parsing the board type sub menus available in Arduino. It creates a lot of similar looking Board Types in the drop down list in B4R making choosing the right one difficult.
I'll do some more testing this weekend and see if I can nail down the problem.
You could also try browsing www.stm32duino.com for any relevant info.

You can try to open the Arduino project created by B4R.
It is located in ..//You-Project/Object/src folder.
You can click on the INO file to open the project in Arduino IDE.
Try compiling it in Arduino IDE.
You can then test different Board Type settings in the Arduino IDE.
 

whcir

Member
Licensed User
Longtime User
I have only done testing with the STM32F103RB Nucleo board.
I did briefly connected up a generic STM32F103C8T6 ( one with a JTAG programming interface on board) which connected to an external ST-LINK dongle and back to the PC USB.
I had problems B4R compiling/programming this using any of the available generic C8 board types. But, if I selected the "STM32F103RB Mucleo" board type it would compile and program with your BLINK example.
My difference is that I connected my LED to PA5 (being arduino pin 13, not 2)

How are you programming your C8 board?
Which Board Type did you try?

The B4RBoardsExtractor.jar has trouble parsing the board type sub menus available in Arduino. It creates a lot of similar looking Board Types in the drop down list in B4R making choosing the right one difficult.
I'll do some more testing this weekend and see if I can nail down the problem.
You could also try browsing www.stm32duino.com for any relevant info.

You can try to open the Arduino project created by B4R.
It is located in ..//You-Project/Object/src folder.
You can click on the INO file to open the project in Arduino IDE.
Try compiling it in Arduino IDE.
You can then test different Board Type settings in the Arduino IDE.


Starchild,

I'm using the USB to serial interface to program.
I'm using Generic STM32f103c series (upload method=serialMethod).
compiling it in Arduino IDE did not change anything.
I now am receiving log data but the led still does not work. My led is attached to port PC13. when looking at the STM32 data sheet PC13 is physical pin 2, that is why I used pin 2.
How are the ports of the STM32 matched with the pins (PC13.Initialize(???, PC13.MODE_OUTPUT)) of the arduino in B4R

Thanks

Update: after trying every pin number, I finally found the one, pin #32 = port PC13. there has to be a better way to map the physical pins to the ports.
 
Last edited:

Starchild

Active Member
Licensed User
Longtime User
Starchild,

I'm using the USB to serial interface to program.
I'm using Generic STM32f103c series (upload method=serialMethod).
compiling it in Arduino IDE did not change anything.
I now am receiving log data but the led still does not work. My led is attached to port PC13. when looking at the STM32 data sheet PC13 is physical pin 2, that is why I used pin 2.
How are the ports of the STM32 matched with the pins (PC13.Initialize(???, PC13.MODE_OUTPUT)) of the arduino in B4R

Thanks

Update: after trying every pin number, I finally found the one, pin #32 = port PC13. there has to be a better way to map the physical pins to the ports.

The generic STM32 Board Types have a much simpler pin to port mapping.
I have attached a XML document (now a PDF) detailing the Arduino pin mapping for F103R and F103C STM32 microcontrollers.

I did some testing with my own F103C8 board and it works fine now with the Generic F103C Board Types available through B4R. The compiler error I received is that I was using Arduino pin 13. I needed to use Arduino pin 5 to target the PA5 output under Generic F103C.
 

Attachments

  • Arduino to STM32 Pin Allocation Table (rev 1).pdf
    231.8 KB · Views: 2,140
Last edited:

whcir

Member
Licensed User
Longtime User
The generic STM32 Board Types have a much simpler pin to port mapping.
I have attached a XML document detailing the Arduino pin mapping for F103R and F103C STM32 microcontrollers.

I did some testing with my own F103C8 board and it works fine now with the Generic F103C Board Types available through B4R. The compiler error I received is that I was using Arduino pin 13. I needed to use Arduino pin 5 to target the PA5 output under Generic F103C.

Thanks for the file, but how do I use it. When I open it all I see is a bunch of programming language.
 

whcir

Member
Licensed User
Longtime User
The XML document was created and opened in Microsoft Word.
But, I have reposted the file (above) as a PDF format document.

Starchild,

Have you tried to use rwire yet? Just initializing causes errors when compiling. could you take a look, c/c++ is out of my league

Thanks
 

Starchild

Active Member
Licensed User
Longtime User
I can see this stm32 implementation is going to be an on going challenge.

I have had a flick through the Arduino_STM32 libraries and I believe that the I2C implementation that has been used is a software emulation of I2C. It does not use the imbedded hardware I2C support of the STM32 chip. As such the rwire library of B4R is not supported. In fact I believe the software emulation of I2C is only for MASTER and not SLAVE mode as there is no provision for a callback.

I have investigated this further and I have had a go of wrapping the Wire class within the Arduino_STM32 library to create a STM32 compatible rwire library for B4R. It only supports MASTER mode. SCL=PB6 and SDA=PB7.

The big unknown is that I don't have any I2C devices to test. Hope it works.

You will need to unpack the ZIP file "stm32_rwire.zip" and copy the contents to your "Additional Libraries" folder. You should have already set this up under B4R menu Tools/Configuration-Paths. Then re-run B4R for this new library to be presented in the Library Manager.

Library has been updated with Changes made by "derez" 16/Oct/17
https://www.b4x.com/android/forum/threads/how-to-implement-stm32-into-b4r.68034/page-2#post-538711
Now v1.11
 

Attachments

  • stm32_rWire v1-11.zip
    2 KB · Views: 543
Last edited:

whcir

Member
Licensed User
Longtime User
I can see this stm32 implementation is going to be an on going challenge.

I have had a flick through the Arduino_STM32 libraries and I believe that the I2C implementation that has been used is a software emulation of I2C. It does not use the imbedded hardware I2C support of the STM32 chip. As such the rwire library of B4R is not supported. In fact I believe the software emulation of I2C is only for MASTER and not SLAVE mode as there is no provision for a callback.

I have investigated this further and I have had a go of wrapping the Wire class within the Arduino_STM32 library to create a STM32 compatible rwire library for B4R. It only supports MASTER mode. SCL=PB6 and SDA=PB7.

The big unknown is that I don't have any I2C devices to test. Hope it works.

You will need to unpack the ZIP file "stm32_rwire.zip" and copy the contents to your "Additional Libraries" folder. You should have already set this up under B4R menu Tools/Configuration-Paths. Then re-run B4R for this new library to be presented in the Library Manager.

Starchild,

Thanks for wrapping the library. Its a shame that B4R is not compatible without writing different libraries. I was hopping to use this MCU because the cost is cheap and has the power of 32 bit using B4R would be the perfect combination for a non programmer like me. I wanted to connect several micros using rwire but without slave functions I Guess I will have to stick with the Due if I want the full functionally of all the peripherals in B4R. Thanks again for your time wrapping this library.
 

whcir

Member
Licensed User
Longtime User
I can see this stm32 implementation is going to be an on going challenge.

I have had a flick through the Arduino_STM32 libraries and I believe that the I2C implementation that has been used is a software emulation of I2C. It does not use the imbedded hardware I2C support of the STM32 chip. As such the rwire library of B4R is not supported. In fact I believe the software emulation of I2C is only for MASTER and not SLAVE mode as there is no provision for a callback.

I have investigated this further and I have had a go of wrapping the Wire class within the Arduino_STM32 library to create a STM32 compatible rwire library for B4R. It only supports MASTER mode. SCL=PB6 and SDA=PB7.

The big unknown is that I don't have any I2C devices to test. Hope it works.

You will need to unpack the ZIP file "stm32_rwire.zip" and copy the contents to your "Additional Libraries" folder. You should have already set this up under B4R menu Tools/Configuration-Paths. Then re-run B4R for this new library to be presented in the Library Manager.

Starchild

After searching around on the internet, I found an Arduino Wire library for the SMT32F103xx. after looking at the code it appears to have Master & Slave modes. I tried to wrap it but ran into trouble that is above my knowledge. I have attached the file, can you take a look at the file and see if I am correct and see if you can wrap it.

Thanks
 

Attachments

  • libraries.zip
    17.8 KB · Views: 582

Starchild

Active Member
Licensed User
Longtime User
Starchild

After searching around on the internet, I found an Arduino Wire library for the SMT32F103xx. after looking at the code it appears to have Master & Slave modes. I tried to wrap it but ran into trouble that is above my knowledge. I have attached the file, can you take a look at the file and see if I am correct and see if you can wrap it.

Thanks

I have looked at these libraries extensively. I am unable to get the slave/i2c implementation to compile.
I have had a further look into the libraries from stm32rduino.com (implemented in this tut). They do support interrupt handling, but no I2c slave support has been included. It does however, support functions such as "attach_interrupt" it would be possible for a C competent person to write a B4R library to provide slave I2C functionality. However, my C is weak. I am unable to achieve this myself.

Roger Clark has provided more details on the Arduino_stm32 libraries at
https://github.com/rogerclarkmelbourne/Arduino_STM32/wiki
The implementation and management of interrupts can be read here.
Click on "LibMaple Interrupt Handler" right side menu.
 

whcir

Member
Licensed User
Longtime User
Has anybody tried the serial function to send and receive data on the STM32? Tried sending a byte using a b4j program and echo the byte back, but the micro locks up. The same code used on an arduino nano works fine. Any ideas?

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial As Serial
    Private astream As AsyncStreams
    Dim led As Pin
End Sub

Private Sub AppStart
    Serial.Initialize(115200)
    astream.Initialize(Serial.Stream, "astream_NewData", "astream_Error")
    led.Initialize(32,led.MODE_OUTPUT)
    Log("AppStart")
End Sub

Sub astream_newdata (Buffer() As Byte)
    led.DigitalWrite(Not(led.DigitalRead))       
     astream.Write(buffer)   
End Sub

Sub astream_Error
   
End Sub
 

whcir

Member
Licensed User
Longtime User
I can try to add built-in support for STM32. Is this a good board for development: http://www.ebay.com/itm/Mini-USB-ST...863658?hash=item5d43d30dea:g:GQkAAOSwMmBV0vq4 ?

Does it include everything needed to start testing?

Hi Erel,

The board you selected is leaflabs maple mini. I believe this board http://www.ebay.com/itm/STM32F103C8...768817?hash=item2eec1c3b71:g:74YAAOSwZ8ZW5jfe is a generic stm32f103c8t6 that would be better to add support for. I have been working with this board for a couple months, its a good alternative to the arduino DUE but would need some communication libraries. If you don't install the bootloader, you cannot use the usb port, you have to use a usb to serial and load programs to the serial port
 
Last edited:

whcir

Member
Licensed User
Longtime User
I think that I will wait with it for now (Arduino and ESP8266 are much simpler to start working with).

Erel,

The board you were originally looking at has the bootloader pre loaded and uses the same MCU as the board I have so its less complicated. The reason I chose the generic board was more I/O pins were exposed and am going to use the MCU in a DIY project without the bootloader. I really wish you would consider adding support.

Thanks
 
Top