Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help stm32f411 two devices on SPI #167

Closed
brightproject opened this issue Dec 27, 2024 · 3 comments
Closed

Need help stm32f411 two devices on SPI #167

brightproject opened this issue Dec 27, 2024 · 3 comments
Assignees
Labels
type: support OT: Request for help using the project

Comments

@brightproject
Copy link

I have stm32f411ceu6

STM32F411CEU6_WeAct_Black_Pill_V2 0-2

I connect two different devices to it via SPI:
CAN bus transceiver

4651 970

module with microSD

7396e22b64db0e3b92a150d329473239

Two device boards are connected to one SPI, only different SS contacts.

The connection is very simple, I use standard contacts for the SPI:

MOSI - PA7
MISO - PA6
SCK - PA5
SD_CS/SS - PB0
CAN_CS/SS - PA4

The CAN module works fine, checked in another code.
Now I decided to launch in addition to the CAN module also a module for saving data to the micro SDHC card.
I have three microSD cards - 512 MB, 8 GB and 32 GB.

5436064841996561252

The cards are not of the best quality, the 512 MB card stopped being detected after the first attempt to consider it a SD module, and it can't even be formatted via the program.

https://www.sdcard.org/downloads/formatter/sd-memory-card-formatter-for-windows-download/

Apparently the card has become unusable☹

I'll say in advance - all cards except 512 MB were formatted using this program.

However, I don't understand what is missing in the code or hardware for this to work, the code is:

// include the SD library:
#include <SPI.h>
#include <SD.h>

//#define  SERIAL_PORT_SPEED  9600
// #define  SERIAL_PORT_SPEED  115200
#define  SERIAL_PORT_SPEED  230400

// Pin assignment for SPI
const uint8_t CAN_CS_PIN = PA4;
const uint8_t SD_CS_PIN  = PB0;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(SERIAL_PORT_SPEED);

pinMode(CAN_CS_PIN, OUTPUT);
digitalWrite(CAN_CS_PIN, HIGH);

  Serial.print("\nInitializing SD card...");

if (!SD.begin(SD_CS_PIN)) {
  Serial.println("initialization failed. Things to check:");
  Serial.println("1. is a card inserted?");
  Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
  while (true);
}

Serial.println("initialization done.");

}

void loop(void) {
}

The output of the serial port is as follows:

Initializing SD card...initialization failed. Things to check:

  1. is a card inserted?
    Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!

I tried to pull the CS pins of two devices to logical 1, and swap the pins. But the cards are not detected.

For comparison, I tried running the examples of this library:

https://github.com/greiman/SdFat/blob/master/examples/examplesV1/dataLogger/dataLogger.ino

I removed some of the code to make it easier to understand the reasons, but the same conclusion was with the full text of the example code.

#include <SPI.h>
#include "SdFat.h"

//#define  SERIAL_PORT_SPEED  9600
// #define  SERIAL_PORT_SPEED  115200
#define  SERIAL_PORT_SPEED  230400

// File system object.
SdFat sd;

// Pin assignment for SPI
const uint8_t CAN_CS_PIN = PA4;
const uint8_t SD_CS_PIN  = PB0;

// Error messages stored in flash.
#define error(msg) sd.errorHalt(F(msg))

void setup() {

  Serial.begin(SERIAL_PORT_SPEED);

  delay(1000);

  Serial.println(F("Type any character to start"));
  // while (!Serial.available()) {
  //   yield();
  // }

  // Initialize at the highest speed supported by the board that is
  // not over 50 MHz. Try a lower speed if SPI errors occur.
  // if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(16))) {
    sd.initErrorHalt();
  }

}

void loop() {

}

Running code on a faulty/incorrectly formatted microSD:

  • 512 MB inserted

Type any character to start
begin() failed
Do not reformat the SD.
SdError: 0X17,0XFF

  • 512 MB removed

Type any character to start
begin() failed
Do not reformat the SD.
No card, wrong chip select pin, or wiring error?
SdError: 0X1,0X0

Known good microSD:

  • 8 GB inserted

Type any character to start
begin() failed
Do not reformat the SD.
SdError: 0X17,0X1

  • 8 GB removed

Type any character to start
begin() failed
Do not reformat the SD.
No card, wrong chip select pin, or wiring error?
SdError: 0X1,0X0

  • 32 GB inserted

Type any character to start
begin() failed
Do not reformat the SD.
SdError: 0X17,0XFF

  • 32 GB removed

Type any character to start
begin() failed
Do not reformat the SD.
No card, wrong chip select pin, or wiring error?
SdError: 0X1,0X0

I would really like to understand the problem, but I lack some knowledge in the library codes, and in the operation of the SPI bus - since the main problem is in its configuration for two devices, as it seems to me.

@brightproject
Copy link
Author

Some addition - I power the stm32 from a stlink programmer, which produces 3.3 volts, which is of course not enough for the SD card module, and I know about it. The thing is that when I power the stm32 board and the SD card module from 5 volts, the code also behaves inadequately:

// include the SD library:
#include <SPI.h>
#include <SD.h>

//#define  SERIAL_PORT_SPEED  9600
// #define  SERIAL_PORT_SPEED  115200
#define  SERIAL_PORT_SPEED  230400

// Pin assignment for SPI
// const int chipSelect = PA4;
const uint8_t CAN_CS_PIN = PA4;
// const uint8_t SD_CS_PIN  = PB0;
const uint8_t SD_CS_PIN  = -1;

#define  SS  SD_CS_PIN

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(SERIAL_PORT_SPEED);

pinMode(CAN_CS_PIN, OUTPUT);
digitalWrite(CAN_CS_PIN, HIGH);

pinMode(SD_CS_PIN, OUTPUT);
digitalWrite(SD_CS_PIN, HIGH);

  Serial.print("\nInitializing SD card...");

if (!SD.begin(SD_CS_PIN)) {
  Serial.println("initialization failed. Things to check:");
  Serial.println("1. is a card inserted?");
  Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
  while (true);
}

Serial.println("initialization done.");

}

void loop(void) {
}

In case of 5 volt power supply and code as above, the output to the serial port is always OK.

Initializing SD card...initialization done.

Either the STM32 platform is incorrectly defined in the library and the SS pin is always substituted by default, or the library has difficulty substituting a different pin and working with two devices on the SPI bus.

@brightproject
Copy link
Author

Strange things with a 512 MB no-name card.
I formatted the first such flash drive using Windows 10, and apparently killed it, although there is hope to somehow reformat it in the future, but now it is not visible by any means.
I took the second same card, and formatted it with the program

https://www.sdcard.org/downloads/formatter/sd-memory-card-formatter-for-windows-download/

I was surprised that this is an SD card and FAT16, apparently I did it wrong that I formatted the first card using Windows.

512_mb_erase

The second 512 MB SD card works fine - checked with the SdFat library.

@per1234 per1234 self-assigned this Dec 27, 2024
@per1234
Copy link
Contributor

per1234 commented Dec 27, 2024

Hi @brightproject. Thanks for your interest in this open source project. This issue tracker is only to be used to report bugs or feature requests specific to the project. This topic is more appropriate for the Arduino Forum. I'm sure we will be able to help you out over there:

https://forum.arduino.cc/

@per1234 per1234 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2024
@per1234 per1234 added the type: support OT: Request for help using the project label Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: support OT: Request for help using the project
Projects
None yet
Development

No branches or pull requests

2 participants