2020年10月1日 星期四

[ESP32] Using the MFRC522 RFID reader and SD card reader

Today some guy in the Facebook proposed an interesting problem about how to use the RFID and the SD in ESP32s .  Both peripheral devices used the SPI connections. The point is they need it's own CS (chip select) pin to enable the SPI (MOSI/MISO/CLK) bus. Only one of them can be enabled in the same time because the enabled device will occupy the SPI bus.

Uusing the original SD ReadWrite code of the arduino example, we could find it's failed even if the CSs are defined well. To see what happened, I checked the compiler messages, as the following:

Use library SPI、verison 1.0,in folder:C:\Users\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\SPI
Use library SD、version 1.0.5,in follder:C:\Users\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\SD
Use library FS、version 1.0,in folder:C:\Users\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\FS

From the view point of the software architecture, the SD layer is stacked above the FS which is stacked on the SPI hearware driver. The above messages showed that ESP32 SD library was applied, which caused the original SD ReadWrite code to be failed. 

To solve the problem, some library definition are to be modified as the follows.

To stop using the ESP32 SD Library

Modify the file:  C:\Users\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\SD\library.properties , change the mcu architecture name to something, e.g.: change architectures=esp32 to architectures=esp88. This will disable the use of ths ESP32 SD library to be applied.

Modify the Arduino SD Library 

After a deep look at the compiler message:

 "C:\\Users\\user\\AppData\\Local\\Arduino15\\packages\\esp32\\tools\\xtensa-esp32-elf-gcc\\1.22.0-80-g6c4433a-5.2.0/bin/xtensa-esp32-elf-g++"
-DESP_PLATFORM "-DMBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\""
-DHAVE_CONFIG_H -DGCC_NOT_5_2_0=0
-DWITH_POSIX
"-IC:\\Users\\user\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\1.0.4/tools/sdk/include/config"
"-IC:\\Users\\user\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\1.0.4/tools/sdk/include/app_trace"
"-IC:\\Users\\user\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\1.0.4/tools/sdk/include/app_update"
......

-DF_CPU=240000000L
-DARDUINO=10813
-DARDUINO_ESP32_DEV
-DARDUINO_ARCH_ESP32
"-DARDUINO_BOARD=\"ESP32_DEV\""
"-DARDUINO_VARIANT=\"esp32\""
-DESP32
-DCORE_DEBUG_LEVEL=0
......

I found some symbols named ARDUINO_ESP32_DEV, ARDUINO_ARCH_ESP32...  are defined for the ESP32 in the compiling time by the  -D command option. We can apply the symbol definitions to change the compiler behavior in the preprocessing steps.

Modify the file: C:\Program Files (x86)\Arduino\libraries\SD\src\utility\Sd2PinMap.h,  use the ARDUINO_ARCH_ESP32 as the compiler condition:

...
#endif	// Arduino ARC
#elif defined (ARDUINO_ARCH_ESP32)
  #include <Arduino.h>
  // Two Wire (aka I2C) ports
  //uint8_t const SDA_PIN = 21;
  //uint8_t const SCL_PIN = 22;

  // SPI port
  //uint8_t const SS_PIN = 5;
  uint8_t const MOSI_PIN = 23;
  uint8_t const MISO_PIN = 19;
  uint8_t const SCK_PIN = 18;
#else
...

That is, we want to apply the original Arduino SD library stacked over the ESP32 SPI hardware dirver library. 
Source code: https://github.com/ghostyguo/ESP32_RC522_SD 
YouTube demo: https://www.youtube.com/watch?v=JWypqkTJkaU 

 


沒有留言:

張貼留言