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
 

KiloBravo

Active Member
Licensed User
You need to use rSD not rSPI32. Erel noted in a post "rSD is a thin wrapper for the Arduino SD."
As you mention, the issue is ... rSD Initialize method only defines the CSpin.

bool Initialize(Byte CSPin);

I will see if I can change the Initialize method for you.

I will post any progress in the other thread, because it is more appropriate for your issue.
Initialize rSD with other pins (MISO,MOSI,SCK,CS)
 
Upvote 0
Top