B4R Question ESP8266 SPI bus?

techknight

Well-Known Member
Licensed User
Longtime User
Is there a way to interface and use the SPI or other hardware onboard the ESP?

Reason I ask, since the ESP8266 has a microcontroller internal, I would rather receive my UDP packets, parse them, and then drive an array of 74HC595 ICs all from the ESP.

any thoughts? thanks.
 

techknight

Well-Known Member
Licensed User
Longtime User
Great. But before I do, I need to know how to implement the usage of the SPI, and I2C, etc in B4R for the ESP board? As I have never used B4R, and I cant find anything specific on the ESP for SPI and I2C.

As far as the AVR is concerned, I have always used BASCOM-AVR which is a great program but since there is no underlying OS, the compiler's memory management is awful so that particular compiler complains alot on variable usage.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
74HC595 ICs which will run on the SPI bus, and then an I2C based ALS.

I was hoping B4R was low-level enough to be able to manipulate the SPI and I2C busses directly. like Driver libraries.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I was hoping B4R was low-level enough to be able to manipulate the SPI and I2C busses directly.
You can do everything you like in B4R. As it happens there is currently no low level library for SPI and I2C. However these APIs are simple to call with inline C.

Arduino C code for this chip: https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds/arduino-code

The two shift methods are not exposed in the core library, however they are also simple to call with inline C.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
I have translated the sketch in Erel's link to B4R , with the shiftOut function in inline C (the HW is like in the link, using NodeMcu):
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private d1 As D1Pins
    Private lPin, cPin, dPin As Pin
    Private clockPin As Int = 12
    Private dataPin As Int = 2
    Private bitOrder As Boolean =  False
    Private tmr As Timer
    Private Value As Byte = 0
    Private count As Int = 0
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    lPin.Initialize(d1.D5, lPin.MODE_OUTPUT)
    cPin.Initialize(d1.D6, cPin.MODE_OUTPUT)
    dPin.Initialize(d1.D4, dPin.MODE_OUTPUT)

    tmr.Initialize("tmr_Tick",1000)
    tmr.Enabled = True
End Sub

Sub tmr_tick
    If count = 8 Then
        Value = 0x00
    Else
        Value = Bit.Set(Value,count)
    End If
    lPin.DigitalWrite(False)
    RunNative("ShiftOut", Null)
    lPin.DigitalWrite(True)
    count = (count+1) Mod 9
End Sub

#If C
void ShiftOut(B4R::Object* o) {
shiftOut(b4r_main::_datapin, b4r_main::_clockpin,b4r_main::_bitorder, b4r_main::_value);
}
#End If
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
When I run it with Wemos D1 all the leds light and do not go by the sequence as in Nodemcu :eek: , checked with two boards - leds lights are not changing...
With Arduino UNO it is OK.
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
Can someone help me and show how to write the inline function of
B4X:
byte incoming = shiftIn(dataPin, clockPin, bitOrder)
Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private mIncoming, mDataPin, mClockPin As Byte 'ignore
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   Log(ShiftIn(5, 4, True))
End Sub

Sub ShiftIn (DataPin As Byte, ClockPin As Byte, MSBFirst As Boolean) As Byte
   mDataPin = DataPin
   mClockPin = ClockPin
   Dim msb As Byte
   If MSBFirst Then msb = 1 Else msb = 1
   RunNative("ShiftIn", msb)
   Return mIncoming
End Sub

#if C
void ShiftIn (B4R::Object* o) {
  b4r_main::_mincoming = shiftIn(b4r_main::_mdatapin, b4r_main::_mclockpin, o->toULong());
}
#end if
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Tried but it does not work for this shift register. It should work with this MC74HC589A 8-Bit Serial or Parallel-Input/Serial-Output Shift Register or SN74HC165N
 
Last edited:
Upvote 0
Top