B4R Code Snippet BluePill on B4R

I have a bunch of bluepills and this weekend started to look into using them with B4R. I don't tend to use these units with arduino projects as I prefer them with MBED.

I have discovered a couple of things.

1: Don't waste your time on a boot block. It barely works. You have to do some electrical mods that are questionable. And the gain is not much. Use SWLINK instead. A module costs under $5 on ebay and is completely integrated into the Arduino IDE and thus works in B4R. No pins to move.

2: Pin mapping doesn't happen with B4R. I don't know why as it is a thing in Arduino. Here is a sub that translates.

B4X:
' bluepill mapper for PAx,PBx,PCx to arduino pins
'
Sub BluePill(PinArray As String,PinNumber As Byte) As Byte
    If PinArray = "PA" Or PinArray = "pa" Then
        Return PinNumber
    Else if PinArray = "PB" Or PinArray = "pb" Then
        Return PinNumber - 16
    else if PinArray = "PC" Or PinArray = "pc" Then
        If PinNumber = 13 Then Return 32
        If PinNumber = 14 Then Return 33
        If PinNumber = 15 Then Return 34
        Return 0 ' pick any default (error) pin you wamt
    End If
    Return 0 ' pick any default (error) pin you wamt
End Sub

If you want a serial console, use a usb console widget on PA2/PA3 (untested). I don't, so I just tell B4R to use some unused serial port.

Here is a blinky thing mode to work on the bluepill.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Dim led As Pin
    Dim t As Timer
    Dim phase As Int
End Sub

' bluepill mapper for PAx,PBx,PCx to arduino pins
'
Sub BluePill(PinArray As String,PinNumber As Byte) As Byte
    If PinArray = "PA" Or PinArray = "pa" Then
        Return PinNumber
    Else if PinArray = "PB" Or PinArray = "pb" Then
        Return PinNumber - 16
    else if PinArray = "PC" Or PinArray = "pc" Then
        If PinNumber = 13 Then Return 32
        If PinNumber = 14 Then Return 33
        If PinNumber = 15 Then Return 34
        Return 0 ' pick any default (error) pin you want
    End If
    Return 0 ' pick any default (error) pin you want
End Sub

'
' do the deed
'
Sub blink
    If phase = 0 Then
        led.DigitalWrite(True)
        phase = 1
    Else
        led.digitalWrite(False)
        phase = 0
    End If
End Sub

'
' setup and go
'
Private Sub AppStart
    phase = 0
    led.PinNumber = BluePill("pc",13)
    led.Mode = led.MODE_OUTPUT
    t.Initialize("blink",100)
    t.Enabled = True 
End Sub

Note: the regulators in these widgets are really weak and the clipon ones for breadboards usually get over driven.....don't let the smoke out. Power with the usb or swlink. Use a real power supply otherwise. But they are cheap.
 
Last edited:
Top