B4R Question Use SPI.H Library in B4R

Gerardo Tenreiro

Active Member
Licensed User
Hi, I'm trying to use the arduino SPI.H library in B4R without much success, it's been a couple of weeks now and I'm stuck
In Arduino the code is:
// ESP32 Arduino CODE
#include <SPI.h>
static const int spiClk = 1000000; // 1 MHz

SPIClass * vspi = NULL;


static int data = 0; // Contador de Pasadas

uint8_t D0 = 0;
uint8_t D1 = 0;
uint8_t D2 = 0;

double Valor =0;
double FET = 1000.0 / 8388607;


void setup() {

#define CSSPI 27
#define CLKSPI 14
#define OUTSPI 12
#define INSPI 13

Serial.begin(115200);

Serial.println("Arranque PRG");

//Pines Normalñizados del SPI
//SCLK = 18, MISO = 19, MOSI = 23, SS = 5

pinMode(CSSPI , OUTPUT);
pinMode(CLKSPI, OUTPUT);
pinMode(OUTSPI, OUTPUT);
pinMode(INSPI, INPUT_PULLUP);

vspi = new SPIClass(HSPI); // VSPI=Funciona, HSPI = Funciona, SPI=Funciona
vspi->begin(CLKSPI,INSPI,OUTSPI,CSSPI);

Serial.println("Inicia Rutina");
}

void loop()
{
//use the SPI buses

Envia_por_SPI();
delay(10);
//Serial.println(data);

}

void Envia_por_SPI() {

data = data + 1;
if (data > 200) data = 0;

vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE1));
digitalWrite(CSSPI, LOW); //Activa CS, lo pone a nivel bajo

vspi->transfer(81);
vspi->transfer(3);
vspi->transfer(1);
vspi->transfer(0);
vspi->transfer(240);
vspi->transfer(1);

vspi->transfer(1); // Solicita Lectura del Canal

vspi->endTransaction();
vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE2));

D2 = vspi->transfer(0);
D1 = vspi->transfer(0);
D0 = vspi->transfer(0);

digitalWrite(CSSPI, HIGH); //Desactiva CS, lo pone a nivel alto
vspi->endTransaction();

// Calcula el Valor en Bruto
Valor =(D2 * 65536) + (D1 * 256) + D0;

//' Si es un numero Negativo Realiza el Complemento a 2
if (D2 > 128) {
Valor = 16777215 - Valor;
Valor = Valor * -1;
}

Valor = Valor * FET;

Serial.printf("Lee Valor=");
Serial.println(Valor);

How I could use it in b4R
Thank you
 

tigrot

Well-Known Member
Licensed User
Longtime User
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
That library is not working with ESP32, I already wrote to Hatzisn about it and the query is to try to solve the problem.
For Arduino and ESP8266 the library is working fine but with the ESP32D the dual core does not work since the SPI is stepped on and it gives a write error on the internal memory.
The Code sent if it works correctly from arduino, so I want to pass it to B4R but my C does not give that much.
Thank you
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
Ok, It works but two important instructions are missing which are
vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE1));
with its three parameters
Thank you
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
Seen the implementation
I'm going to try it but the intention would be to pass the parameters directly as if it were the SPI library itself, so I could use it with all the functions.
I comment on the test results
Thank you
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
Seen the implementation
I'm going to try it but the intention would be to pass the parameters directly as if it were the SPI library itself, so I could use it with all the functions.
I comment on the test results
Thank you
Passing parameters is Limited, you can write a B4R library that will help. Write a wrapper following my steps. It's simple...
 
Upvote 0

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)
 
Upvote 0

KiloBravo

Active Member
Licensed User
RST is Reset which is not defined as part of the SDI (SCLK, MISO, MOSI, CS) interface definition as far as I know.
From the post above ....
***CHANGE HERE THE PIN NUMBERS (SCK, MISO, MOSI,CS)***
SPI.ESP32_SPI_Set_Pins(18,19,23,CS)
SPI.CSPinDeActivate(SPI.CS__SPI)

It looks like you should use v 1.3. CS above is Chip Select. You should be able to use any GPIO pin for CS.
Find an open GPIO pin and use it. If one GPIO does not work for CS try a different pin. It should work.
Let us know if it works.
 
Upvote 0

sasetcolombia

Member
Licensed User
Longtime User
The objective is to create a file on the SD CARD and store sensor data. (The rSD library works, but if I don't use the SIM800L integrated in TTGO TCALL )
With rSPI32, how could I assign the name of the file that is created in the SD CARD?
Thanks!
 
Upvote 0
Top