B4R Library rSPI

Hi all,

yesterday I was watching a tutorial about using spi library in Arduino. I tried to port this in B4R. I think I succeeded but someone more experienced should check it and let me know if everything works ok. There is the necessary needed functionality in the Code Module but there are also some additions. Please check and let me know if everything is ok.

Please take under note that (as described in this tutorial) not all boards support the begin transaction command (that this library uses):


and you must check it.

Further more you must be aware that this impementation transfers a byte and receives a byte and if your device communication implementation (protocol) requires multiple bytes transfer (as it is mentioned in several online videos) it will not work. Be aware also that you must know the frequency of communications (MAX = 20000000) and also if it is MSBFIRST or LSBFIRST and at last the SPI_MODEx where x=0,1,2,3 in order to make it work.

See the attached example. Feel free to post corrections. Please note that until today (25/10/2021) the library works only for Arduino and ESP8266.

(21-9-2021)
Four new functions have been added:

B4X:
'This command will write to the logs "--" if the board is not supported
'and "-You can use this library with your board-" if your board is supported
SPI.CheckIfThisBoardIsSupported

'Transfer Byte Array
Dim iDimension As UInt = 3
Dim bSend(iDimension) As Byte
bSend(0) = SPI.GetByteFromString("11001100")
bSend(1) = SPI.GetByteFromString("01100110")
bSend(2) = SPI.GetByteFromString("00110011")
Dim bRet(iDimension) As Byte
SPI.Transfer_Byte_Array(bSend, bRet)
 
'Disable Inerrupts
SPI.DisableInterrupts

'Enable Inerrupts
SPI.EnableInterrupts


(3-2-2021) Two new functions have been added:

B4X:
    Dim b as Byte
    b = SPI.Transfer_Byte(SPI.GetByteFromString("01100111"))

    'We are interested only in the last 4 bits
    b = SPI.ApplyMask(b, SPI.GetByteFromString("00001111"))
    Log(b)

    'We want to know the value of the 2nd bit from right in the byte
    Dim iBitInSecondPositionFromRight As UInt = SPI.GetBitAt(b, 2)
    Log(iBitInSecondPositionFromRight)
 

Attachments

  • rSPI.b4xlib
    1.6 KB · Views: 375
  • SPI.zip
    2.6 KB · Views: 372
Last edited:

Gerardo Tenreiro

Active Member
Licensed User
Hello good
Check the plate documentation and it says:
CS -> GPIO15 -> D8
CLK -> GPIO14 -> D5
Out -> GPIO13 -> D7
In -> GPI12 -> D6
I put a logic analyzer on all the pins (D1, D2, D3, D4, D5, D6, D7, D8) of the board and the only thing that works is the CS (D8) and the blue Led that I have flashing
I used the SPI with other equipment but it is ESP8266, I can't get it to send anything at all
Surely this library is compatible with the ESP9266
Thank you
 

hatzisn

Expert
Licensed User
Longtime User
Hello good
Check the plate documentation and it says:
CS -> GPIO15 -> D8
CLK -> GPIO14 -> D5
Out -> GPIO13 -> D7
In -> GPI12 -> D6
I put a logic analyzer on all the pins (D1, D2, D3, D4, D5, D6, D7, D8) of the board and the only thing that works is the CS (D8) and the blue Led that I have flashing
I used the SPI with other equipment but it is ESP8266, I can't get it to send anything at all
Surely this library is compatible with the ESP9266
Thank you

Can you please write the name of your device (the one you are trying to connect) to find its datasheet and check the communication protocol? Even better can you post a link to the datasheet?
 

Gerardo Tenreiro

Active Member
Licensed User
I want to read an ADS1256, currently I am reading it with a PIC and it works correctly, from the PIC I send the data serially to the ESP8266 and from this to the Web but now what I want is for the ESP8266 to read the ADS1256 directly and send the data.
The real intention is not to use the PIC and to pass the whole project to a single ESP8266.

The link to ADS1256 is
https://www.ti.com/lit/ds/symlink/a...=https%3A%2F%2Fwww.ti.com%2Fproduct%2FADS1256

Thank you very much for the help
 

candide

Active Member
Licensed User
do you want to test this wrapper for spi library ?

compilation is OK, loading in esp8266 is OK but i cannot test more
 

Attachments

  • rSPI.zip
    10 KB · Views: 318

Gerardo Tenreiro

Active Member
Licensed User
The library works but I cannot assign the pins of the SPI. There is no SPI.begin instruction (CLKSPI, INSPI, OUTSPI, CSSPI) so I don't know which pins you are using
I also notice that if I run two "SPI.Begin_Transaction (F_SPI, SPI.MSBFIRST, SPI.SPI_MODE1)" instructions without a "SPI.End_Transaction" embedded the ESP32 crashes and stops. It is normal?
Attached is the small ESP32 test program

Finally, could you change the version to rSPI which is always in 1.12?

Thank you
 

Attachments

  • Test_SPI.zip
    1.4 KB · Views: 300
Last edited:

candide

Active Member
Licensed User
wrapper i propose is based on standard SPI library for esp8266 v1.0 : https://github.com/esp8266/Arduino/tree/master/libraries/SPI
all commands in arduino are available in B4R except "pin"
in my wrapper, we don't have "pin" because it is included in "initialize" with pins parameters : "bool Initialize(byte sck, byte miso, byte mosi, byte ss);"

and i don't understand how your code B4R can work with my wrapper because your code B4R is not based on commands available in this wrapper
 

Gerardo Tenreiro

Active Member
Licensed User
Hello
Now locate the SPI output pins, use CLK = 18, Out = 23 and In = 19
Now reviewing the output I observe that the instruction "SPI.Begin_Transaction (F_SPI, SPI.MSBFIRST, SPI.SPI_MODE1)" does not work properly
Sending capture of the analyzer where it is observed that it is not able to decode because SPI_MODE1 does not act in the library.
The same instruction in ARDUINO with SPI.H works correctly
1635098609417.png


Regarding how to use the library in B4R, I am sending you a copy of the program that I use for the tests, which I am attaching again with the latest modifications.
I am also sending you a copy of the program that I use in ARDUINO to use the SPI.H library with the ESP32. This way of doing it works correctly and you can see as if I used the SPI: BEGIN instruction to assign the pins to use in the SPI.
Thank you very much and I hope to be of help to make this library work with the ESP32, remember that it has 4 SPI of which two are for free use.
Feel free to send or order what you need.
Thank you
 

Attachments

  • Test_SPI.zip
    1.4 KB · Views: 307
  • Test_SPI.zip
    1,004 bytes · Views: 294

candide

Active Member
Licensed User
i checked SPI.SPI_MODE1 and coding is "1". you can use directly "1" but i don't thing it is your issue.

i checked SPI library for esp32 and it is different with this one for esp8266
i have to check what are differences
 

Gerardo Tenreiro

Active Member
Licensed User
I am using the library with ESP32 and I checked again that it does not work correctly. To do this, load the program from B4R and analyze it, observing that the clock sequence and the start of the data is not correct, I also have an ADS1256 connected to the SPI bus which does not respond to the telegrams that I send with the program loaded from B4R.
Then I send the same codes from ARDUINO and the analyzer recognizes them perfectly and at the same time the ADS1256 responds to the sent telegrams.
That is, it is verified that the sequence is not the correct one from the B4R library.
This is the image of the analyzer with the program loaded from B4R

1635104339306.png


and this is the image of the analyzer with the program loaded from ARDUINO.
1635104787465.png



If you are going to check the library for the ESP32 it would be very convenient to have the Begin instruction to be able to define the pins of the SPI since with the ESP32 we have two possible SPIs.

Thank you very much and I hope to be of help
 

Gerardo Tenreiro

Active Member
Licensed User
Hello
Reviewing this library I observe several things that happen or comment:
The library runs and uses pins CLK = 18, Out = 19. I cannot determine the IN because the data it sends is not in phase with the clock and neither the decoder nor the device understand it.
The time between sending data is very long, normally it is on the order of "us" and is now 49ms. Attached image

1635151433357.png



I still don't know how to define the pins of the SPI, does it take them by default or is there some way?
Thanks and I'll keep testing and commenting
 

candide

Active Member
Licensed User
in arduino example with SPI_esp32, we have several pins configuration, and if "begin" is without parameter, a standard configuration is used. (in attached file)

if you provide pins in "begin", inputs are provided to arduino library without change, wrapper will not impact at this level.
if you provide a "begin" without parameter, configuration by default in arduino library is used.
 

Attachments

  • SPI_Multiple_Buses.zip
    1.4 KB · Views: 269

Gerardo Tenreiro

Active Member
Licensed User
Correct, that is what I was referring to with the definition of the PINs to be used in the SPI.
Is that pin assignment implemented in the submitted library?


hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS);

Thanks
 

candide

Active Member
Licensed User
this code is your code working well with arduino converted in B4R : can you test it with rSPI32 provided ?
 

Attachments

  • test_spi32.zip
    1.3 KB · Views: 259

Gerardo Tenreiro

Active Member
Licensed User
Yes, I am in contact with Hatzisn, who was the one who originally wrote the library. We are trying to make it work, in fact we are at a dead end and we cannot move forward.
The rSPI library works correctly with the ESP8266 but with the ESP32 it does not work or at least for the moment.
So any contribution is welcome
Thank you very much for your help and I look forward to new news to keep moving forward.
My "C" is not that good at solving the problem but if Your "C" is good you can give it a try. Send what you need to try and I will test it without any problem.
Thanks
 

candide

Active Member
Licensed User
a library SPI dedicated to esp32 is available in esp32 package and with wrapper provided it should be the best way to have SPI working on esp32 for B4R.

Good luck for your project !
 

sasetcolombia

Member
Licensed User
Longtime User
Is it possible to adapt it to record data on SDCARD?
SPI1.begin(MY_SCLK, MY_MISO, MY_MOSI, MY_CS);

I'm trying to use an sd card module with the TTGO T-call SIM800L.
I can't use the SPI pins because I need to use also the SIM800L module.
the TTGO TCAL SIM800L V1.4 uses pin23 as PWR and this same pin is MOSI for SDCARD
In version 1.3 it would conflict with pin 5. RST to SIM800L and VPPI_SS (CP)

https://user-images.githubusercontent.com/53688337/85398433-d9036900-b572-11ea-99ec-04e912b5fd72.JPG
The rSD library uses by default the MOSI(23),MISO(19),SCK(18) and CS(5) pins.
I need to configure other pins for SDCARD (or 23)
 

hatzisn

Expert
Licensed User
Longtime User
Is it possible to adapt it to record data on SDCARD?
SPI1.begin(MY_SCLK, MY_MISO, MY_MOSI, MY_CS);

I'm trying to use an sd card module with the TTGO T-call SIM800L.
I can't use the SPI pins because I need to use also the SIM800L module.
the TTGO TCAL SIM800L V1.4 uses pin23 as PWR and this same pin is MOSI for SDCARD
In version 1.3 it would conflict with pin 5. RST to SIM800L and VPPI_SS (CP)

https://user-images.githubusercontent.com/53688337/85398433-d9036900-b572-11ea-99ec-04e912b5fd72.JPG
The rSD library uses by default the MOSI(23),MISO(19),SCK(18) and CS(5) pins.
I need to configure other pins for SDCARD (or 23)

No, rSPI library uses the default SPI communications. You can only change frequency, bit order and SPI Mode.
 
Top