Bonjour à tous,
je veux ajouter un lecteur SD à mon système monté sur ESP32-Wroom qui utilise déjà le port SPI pour un panneau 4x8x8 MAX7219.
N'ayant pas cherché - ni trouvé, évidemment - comment changer les broches du lecteur SD, j'ai opté pour le déménagement du panneau MAX vers le second set de broches SPI du micro-contrôleur.
Les broches « par défaut » du port SPI de ESP32 sont:
#define broche_CLK 18 //GPIO-18 ... SPI microSD / broche no 30 -- vert // noir => GND blanc => 3.3V
#define broche_OUT 19 //GPIO-19 ... SPI microSD / broche no 31 -- gris
#define broche_DIN 23 //GPIO-23 ... SPI microSD / broche no 37 -- violet
#define broche_CS 5 //GPIO-05 ... SPI microSD / broche no 29 -- bleu
Les broches du second port SPI sont:
#define broch2_CLK 14 //GPIO-14 ... SPI écran / broche no 12 -- vert
#define broch2_OUT 12 //GPIO-12 ... SPI écran / broche no 13 -- MISO -> non-connecté
#define broch2_DIN 13 //GPIO-13 ... SPI écran / broche no 15 -- MOSI -> blanc
#define broch2_CS 15 //GPIO-15 ... SPI écran / broche no 23 -- rouge
Telles que présentées par Ruis Santos
Tout semble bien expliqué dans la page sus-mentionnée, mais je me mêle les pinceaux quand même. L'exemple donné à la page ci-haut est répété sur gitHub
Voici le code de l'exemple pour vous exempter de chercher :
/* The ESP32 has four SPi buses, however as of right now only two of
* them are available to use, HSPI and VSPI. Simply using the SPI API
* as illustrated in Arduino examples will use VSPI, leaving HSPI unused.
*
* However if we simply initialize two instance of the SPI class for both
* of these buses both can be used. However when just using these the Arduino
* way only will actually be outputting at a time.
*
* Logic analyzer capture is in the same folder as this example as
* "multiple_bus_output.png"
*
* created 30/04/2018 by Alistair Symonds
*/
#include <SPI.h>
// Define ALTERNATE_PINS to use non-standard GPIO pins for SPI bus
#ifdef ALTERNATE_PINS
#define VSPI_MISO 2
#define VSPI_MOSI 4
#define VSPI_SCLK 0
#define VSPI_SS 33
#define HSPI_MISO 26
#define HSPI_MOSI 27
#define HSPI_SCLK 25
#define HSPI_SS 32
#else
#define VSPI_MISO MISO
#define VSPI_MOSI MOSI
#define VSPI_SCLK SCK
#define VSPI_SS SS
#define HSPI_MISO 12
#define HSPI_MOSI 13
#define HSPI_SCLK 14
#define HSPI_SS 15
#endif
#if !defined(CONFIG_IDF_TARGET_ESP32)
#define VSPI FSPI
#endif
static const int spiClk = 1000000; // 1 MHz
//uninitialized pointers to SPI objects
SPIClass *vspi = NULL;
SPIClass *hspi = NULL;
void setup() {
//initialize two instances of the SPIClass attached to VSPI and HSPI respectively
vspi = new SPIClass(VSPI);
hspi = new SPIClass(HSPI);
//clock miso mosi ss
#ifndef ALTERNATE_PINS
//initialize vspi with default pins
//SCLK = 18, MISO = 19, MOSI = 23, SS = 5
vspi->begin();
#else
//alternatively route through GPIO pins of your choice
vspi->begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS); //SCLK, MISO, MOSI, SS
#endif
#ifndef ALTERNATE_PINS
//initialize hspi with default pins
//SCLK = 14, MISO = 12, MOSI = 13, SS = 15
hspi->begin();
#else
//alternatively route through GPIO pins
hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS); //SCLK, MISO, MOSI, SS
#endif
//set up slave select pins as outputs as the Arduino API
//doesn't handle automatically pulling SS low
pinMode(vspi->pinSS(), OUTPUT); //VSPI SS
pinMode(hspi->pinSS(), OUTPUT); //HSPI SS
}
// the loop function runs over and over again until power down or reset
void loop() {
//use the SPI buses
spiCommand(vspi, 0b01010101); // junk data to illustrate usage
spiCommand(hspi, 0b11001100);
delay(100);
}
void spiCommand(SPIClass *spi, byte data) {
//use it as you would the regular arduino SPI API
spi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(spi->pinSS(), LOW); //pull SS slow to prep other end for transfer
spi->transfer(data);
digitalWrite(spi->pinSS(), HIGH); //pull ss high to signify end of data transfer
spi->endTransaction();
}
Là où je me mêle les pinceaux, c'est dans le démarrage de mes SPI.
Voici mon code:
#include "FS.h" // //FS comme dans « File System » : système d'enregistrement de fichiers Pour le lecter microSD
#include "SD.h" // Pour le lecter microSD ----- https://randomnerdtutorials.com/esp32-microsd-card-arduino/
#include "SPI.h" // Pour le lecter microSD
#include <LedControl.h>
#include "chiffres_et_symboles.h"
// Définition des broches de raccordement Arduino Nano → Matrice LED pilotée par MAX7219 et carte mémoire microSD
#define broche_CLK 18 //GPIO-18 ... SPI microSD / broche no 30 -- vert // noir => GND blanc => 3.3V
#define broche_OUT 19 //GPIO-19 ... SPI microSD / broche no 31 -- gris
#define broche_DIN 23 //GPIO-23 ... SPI microSD / broche no 37 -- violet
#define broche_CS 5 //GPIO-05 ... SPI microSD / broche no 29 -- bleu
#define broch2_CLK 14 //GPIO-14 ... SPI écran / broche no 12 -- vert
#define broch2_OUT 12 //GPIO-12 ... SPI écran / broche no 13 -- non-connecté
#define broch2_DIN 13 //GPIO-13 ... SPI écran / broche no 15 -- blanc
#define broch2_CS 15 //GPIO-15 ... SPI écran / broche no 23 -- rouge
#define nbMatrices 5 //Combien y a-t-il d'écrans MAX72xx ?
LedControl matriceLed = LedControl(broch2_DIN, broch2_CLK, broch2_CS , nbMatrices);
void setup(void) {
Serial.begin(115200);
delay(400);
//Gestionnaire de fichiers sur carte microSD externe
SPI.begin(broche_CLK, broche_OUT, broche_DIN, broche_CS);
SPI.begin(broch2_CLK, broch2_OUT, broch2_DIN, broch2_CS);
if(!SD.begin(broche_CS)){
Serial.println("Lecteur de carte microSD inaccessible ");
carteSD = false;
} else {
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("Aucune carte microSD trouvée dans le lecteur idoine.");
carteSD = false;
}
Serial.print("Type de carte trouvée: ");
if(cardType == CARD_MMC){ Serial.println("MMC");
} else if(cardType == CARD_SD){ Serial.println("SDSC");
} else if(cardType == CARD_SDHC){ Serial.println("SDHC");
} else { Serial.println("Inconnu");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("Taille de la carte microSD: %lluMB\n", cardSize);
carteSD = true;
}
//Définitions utiles à l'affichage DEL 8x8
for (int x=0; x<nbMatrices; x++) {
matriceLed.setIntensity(x,8);
matriceLed.clearDisplay(x);
matriceLed.shutdown(x,true);
}
}
J'ai ajouté la ligne
SPI.begin(broch2_CLK, broch2_OUT, broch2_DIN, broch2_CS);
tout juste avant de vous écrire sans avoir testé.
Sans cette ligne, la matrice DEL (MAX7912) affiche n'importe quelle mélange DEL allumées. Des fois, ça ressemble vaguement à la valeur souhaitée ... un peu comme voir un visage dans un nuage, on peut reconnaître un « 2 » édenté, par exemple.
Oui, j'ai donc une réaction, mais je n'ai pas le résultat attendu.
Quelle pourrait être une bonne piste à explorer ?
Merci