B4R Tutorial How to Implement STM32 into B4R

Discussion in 'B4R Tutorials' started by Starchild, Jun 17, 2016.

Similar threads

B4R Code Snippet STM32 - Bluetooth
B4R Code Snippet STM32 - With int/ext RTC and LCD
B4R Question STM32 and SoftwareSerial
B4R Question STM32
Wish STM32 SPI interface - SDcard
Wish STM32 Support?
  1. Erel

    Erel Administrator Staff Member Licensed User

    I've ordered it. I will test it when it arrives.
  2. whcir

    whcir Member Licensed User

    Just wondering if there has been any progress or updates on using B4R with STM32 MCU
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. Starchild

    Starchild Active Member Licensed User

    I have changed the "How to Implement STM32" document.
    Available from top post.

    Improved B4R integration using the existing STM32 library define
    instead of my own define

    No need to add or remove a define from the B4R defines template when changing board types.

    It would now be possible to include the altered "Pin::setMode" code into B4R distribution so no additional patch would be required.

        void Pin::setMode(Byte arduino_Mode) {
    #ifdef _VARIANT_ARDUINO_STM32_
        WiringPinMode stm32_Mode;
    switch (arduino_Mode) {
    case INPUT:
            stm32_Mode = INPUT;
    case OUTPUT:
            stm32_Mode = OUTPUT;
    case INPUT_PULLUP:
            stm32_Mode = INPUT_PULLUP;
        pinMode(PinNumber, stm32_Mode);
        pinMode(PinNumber, arduino_Mode);
        if (arduino_Mode == INPUT_PULLUP)
            CurrentValue = true;
  5. Erel

    Erel Administrator Staff Member Licensed User

    I've added it to the core library.
  6. whcir

    whcir Member Licensed User

    Could someone wrap the files for the internal RTC for the STM32? Its looks pretty simple but I don't have the experience.


    Attached Files:

    • src.zip
      File size:
      7.4 KB
  7. whcir

    whcir Member Licensed User


    Would it be possible to do the same thing with the Serial class and LOG display in board.cpp so when switching between AVR and STM32 I don't have to edit this file every time.

    //DEFINE_HWSERIAL(Serial, 3);// Use HW Serial 2 as "Serial"
    DEFINE_HWSERIAL(Serial, 2); // changed for B4R
    //DEFINE_HWSERIAL(Serial1, 2);
    DEFINE_HWSERIAL(Serial1, 3); // changed for B4R
    DEFINE_HWSERIAL(Serial2, 1);
    BaGRoS likes this.
  8. Starchild

    Starchild Active Member Licensed User

    This file "board.cpp" is part of the stm32 library. As such it does not need editting back and forth to use AVR then STM32 devices. The STM32 library is only used when compiling for STM32 devices.
  9. Starchild

    Starchild Active Member Licensed User

    I have had a look at the RTC class available for STM32. Unfortunately, it relies on the support of hardware interrupts to wake from Sleep. At this time B4R can not support the SLEEP modes of the STM32 making it impossible to support the RTC class.

    However, I have created a Generic version of a Software Real Time Clock library for B4R. This is compatible with STM32.
    This library is designed to support the features of B4R.
    You can download the library (rSoftRtc) from here..

    derez likes this.
  10. rbghongade

    rbghongade Active Member Licensed User

    Dear friends,
    What are the corresponding pin numbers for ADC?
  11. derez

    derez Expert Licensed User

    Using SCL=PB6 and SDA=PB7 I can see results on a LCD 1602, it does not use wire library, only LiquidCristal_I2C :)
  12. derez

    derez Expert Licensed User

    Using the stm32_rwire library I got error with STM32F103C8T6. I have modified Starchild's library (stm32_rWire.cpp) like this:
    Byte STM32_WireMaster::WriteTo2(Byte Address, bool SendStop, ArrayByte* Data) {
            byte b = Data->length;
            Wire.write((Byte*)Data->data, Data->length);
    if (SendStop) {
        //        Wire.i2c_stop();
        //    Wire.endTransmission();
            return b;
    Now it works without errors and I have connected HMC5883L magnetometer successfuly (connecting SCL to PB6 and SDA to PB7), and MPU6050 IMU as well.
    The programs are the same as this https://www.b4x.com/android/forum/t...nts-mpu-6050-and-magnetometer-hmc5883l.65917/
    Last edited: Oct 16, 2017
  13. Starchild

    Starchild Active Member Licensed User

    Thanks for discovering the fix.
    I have updated the download of the "stm32_rwire.zip" library file to include this change now v1.11 at its original posting.