STM32H562 board and STM32SD - problems

Post here first, or if you can't find a relevant section!
STM32ardui
Posts: 142
Joined: Mon May 06, 2024 1:46 pm
Answers: 1
Location: Germany

STM32H562 board and STM32SD - problems

Post by STM32ardui »

I got a board with STM32H562 from WeActStudio
( https://github.com/WeActStudio/WeActStu ... ree/master )
On lower side is a microSD slot connected with 4 data lanes:
STM32H652-SD.jpg
STM32H652-SD.jpg (66.32 KiB) Viewed 5056 times

1. SD_DETECT_PIN not working or wrong level?

First I used this small sketch to check the contact for SD-card presence.

Code: Select all

#include <STM32SD.h>        // STM32SD@1.3.2  incl. FatFs@2.1.4
#define LED             PB2     // Bultin-LED is connected to GND
#define SD_DETECT_PIN   PA8

Sd2Card card;
SdFatFs fatFs;
File    root;

void setup() {
  Serial.setRx(PA10);
  Serial.setTx(PA9);
  Serial.begin(115200);
  delay(1000);
  Serial.print("\n\n ---  SDIO-Kartentest auf STM32H562RGT6 -- \n ");

  pinMode(SD_DETECT_PIN, INPUT_PULLUP); 

  card.setDx(PC8, PC9, PC10, PC11);
  card.setCMD(PD2); 
  card.setCK(PC12);

  Serial.printf("SD_DETECT_PIN: %d  CARD_INIT: %d \n", digitalRead(SD_DETECT_PIN), card.init(SD_DETECT_PIN) );
}
Without a microSD-card:

Code: Select all

 SD_DETECT_PIN: 0  CARD_INIT: 0 
With microSD-card inserted:

Code: Select all

 SD_DETECT_PIN: 1  CARD_INIT: 0 
So card dection is not working, it is always 0 or false ?
Same problem with card.init(SD_DETECT_NONE) or card.init()

As next I used a while-loop like in examples. With card.init() it works, looks like it is really slow???

Again with parameter:

Code: Select all

#define SD_DETECT_PIN   PA8

Sd2Card   card;
SdFatFs   fatFs;
File      root;
uint32_t   i = 0; 

void setup() {
  Serial.setRx(PA10);
  Serial.setTx(PA9);
  Serial.begin(115200);
  delay(1000);
  Serial.print("\n\n ---  SDIO-Kartentest auf STM32H562RGT6 -- \n ");

  pinMode(SD_DETECT_PIN, INPUT_PULLUP); 

  card.setDx(PC8, PC9, PC10, PC11);
  card.setCMD(PD2); 
  card.setCK(PC12);
  while (!card.init(SD_DETECT_PIN))        // funktioniert nicht mit SD_DETECT
  {
    delay(1);
    i++;
  }
  Serial.printf("%d ms -  initialization done\n",i);

  Serial.printf("SD_DETECT_PIN: %d  \n", digitalRead(SD_DETECT_PIN) );
}
Result is:

Code: Select all

 8046 ms -  initialization done
SD_DETECT_PIN: 0
OK, I have to explain:
a) When sketch started, microSD-card was inserted. No "initialization done" came up on Serial Monitor.
b) Then I took out card and saw thes two lines

Does librray detect inserted card by LOW-level?
So wiring on board is wrong and should be opposite way for STM32SD-library?


2. SD card not recognized!

I used a simple 4GB card class10 speed and I formatted it with the SD CARD FORMATTER.

After adding

Code: Select all

if (!fatFs.init()) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    //return;
  }
I got - after 6 seconds! - an error "Could not find FAT16/FAT32 partition".


Inside SdFatFs.cpp I see:

Code: Select all

bool SdFatFs::init(void)
{
  /*##-1- Link the SD disk I/O driver ########################################*/
    if (FATFS_LinkDriver(&SD_Driver, _SDPath) == 0) {
    /*##-2- Register the file system object to the FatFs module ##############*/
	if (f_mount(&_SDFatFs, (TCHAR const *)_SDPath, 1) == FR_OK) {
      /* FatFs Initialization done */
      return true;
    }
  }
  return false;
}
FATFS_LinkDriver(&SD_Driver, _SDPath)   returns 0
f_mount(&_SDFatFs, (TCHAR const *)_SDPath, 1)   return 3

But I don't what is the real reason for the error.

Does soemone ever tested STM32SD succesful with SDIO?
ag123
Posts: 1898
Joined: Thu Dec 19, 2019 5:30 am
Answers: 30

Re: STM32H562 board and STM32SD - problems

Post by ag123 »

STM32ardui
Posts: 142
Joined: Mon May 06, 2024 1:46 pm
Answers: 1
Location: Germany

Re: STM32H562 board and STM32SD - problems

Post by STM32ardui »

ag123 wrote: Thu Sep 05, 2024 4:49 am nice new board :)
https://www.aliexpress.com/item/1005007502799023.html
I don't agree - at least for the card interface. :cry:

The 10 kΩ resistor at PA8 is only just working.
Without a microSD-card PA8 is connected to GND, with a card inside slot it is floating.
With an internal pullup you may have 10/(10 + 30) = 0,25 x VDD. So it works, but an additional external 100 kΩ pullup would be better.

I used SD-cards on ESP32 with SPI-interface. It works only with card modules having pullup resistors.
I found information, that also for a SDIO-interface must be pullup resistors on all lines except CLK.
In ArduinoIDE I can define a GPIO as input with pullup. But data lines are bidirectional - you want to write something from MCU to SD-card and also read something from card to MCU.
I guess, the reason why my sketch is not working are these missing pullup resistors ...


And like on other boards WeActStudio connects VBAT-pin via two diodes to external VBAT and VDD. So you can't measure, if a battery for VBAT still has enough voltage level.
fpiSTM
Posts: 1944
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 108
Location: Le Mans
Contact:

Re: STM32H562 board and STM32SD - problems

Post by fpiSTM »

STM32ardui wrote: Wed Sep 04, 2024 1:20 pm Does soemone ever tested STM32SD succesful with SDIO?
Seriously? You really think the library was never tested ?
fpiSTM
Posts: 1944
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 108
Location: Le Mans
Contact:

Re: STM32H562 board and STM32SD - problems

Post by fpiSTM »

STM32ardui wrote: Wed Sep 04, 2024 1:20 pm First I used this small sketch to check the contact for SD-card presence.
First use default example.
STM32ardui wrote: Wed Sep 04, 2024 1:20 pm

Code: Select all

  pinMode(SD_DETECT_PIN, INPUT_PULLUP); 

 
This is not required as the library will properly configure the pin. As Pull up.
STM32ardui wrote: Wed Sep 04, 2024 1:20 pm Does librray detect inserted card by LOW-level?
https://github.com/stm32duino/STM32SD/b ... _sd.c#L452
STM32ardui wrote: Wed Sep 04, 2024 1:20 pm 2. SD card not recognized!

I used a simple 4GB card class10 speed and I formatted it with the SD CARD FORMATTER.

After adding

Code: Select all

if (!fatFs.init()) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    //return;
  }
I got - after 6 seconds! - an error "Could not find FAT16/FAT32 partition".
What is your "SD CARD FORMATTER". Is it really a FAT 32 partition?

My guess is the clock config is probably not correct. Ensure to have a 48 MHz clock for SD IP.
STM32ardui
Posts: 142
Joined: Mon May 06, 2024 1:46 pm
Answers: 1
Location: Germany

Re: STM32H562 board and STM32SD - problems

Post by STM32ardui »

fpiSTM wrote: Thu Sep 05, 2024 7:25 am
STM32ardui wrote: Wed Sep 04, 2024 1:20 pm Does soemone ever tested STM32SD succesful with SDIO?
Seriously? You really think the library was never tested ?
Sounds like you are upset?

I only see some code snipets, but no complete example.
And may I remind you, that example SPI_loop.ino stills uses a SPI.begin(CS_PIN), that is not valid anymore?
How I can be sure, that other (older) examples will still work today?


fpiSTM wrote: Thu Sep 05, 2024 7:47 am What is your "SD CARD FORMATTER". Is it really a FAT 32 partition?
It is from SD Association.
I use it to get definitely a correct formatted card (instead of using Windows for this task).
fpiSTM wrote: Thu Sep 05, 2024 7:47 am First use default example.
???
None example sets pins for {MOSI, MISO, CLK, CS} or {DAT0 - DAT3, CK, CMD} !
So how can I know, if it is made for SPI, SDIO or SDMMC or it will work for all?


STM32ardui wrote: Wed Sep 04, 2024 1:20 pm
STM32ardui wrote: Wed Sep 04, 2024 1:20 pm Does librray detect inserted card by LOW-level?
https://github.com/stm32duino/STM32SD/b ... _sd.c#L452
From this code part

Code: Select all

if (!LL_GPIO_IsInputPinSet(SD_detect_gpio_port, SD_detect_ll_gpio_pin)) {
    status = SD_PRESENT;
  }
I guess, that _LL_ stand for "low level library" instead of HAL?
A port is detected and a pin is detected - but which logic level? HIGH or LOW?
You are an expert for STM32 but you simply forget, that other users are not!

The readme for STM32SD:
SD detect and timeout
SD_DETECT_PIN pin number
SD_DATATIMEOUT constant for Read/Write block
Please add information to readme
- SD presence is recognized by LOW-level (if it is correct)
- library automatically adds pullup to SD_DETECT_PIN
so everyone is able to know.

BTW: are there also pullups for DAT0 - DAT3 and CMD?
STM32ardui
Posts: 142
Joined: Mon May 06, 2024 1:46 pm
Answers: 1
Location: Germany

Re: STM32H562 board and STM32SD - problems

Post by STM32ardui »

STM32ardui wrote: Wed Sep 04, 2024 1:20 pm My guess is the clock config is probably not correct. Ensure to have a 48 MHz clock for SD IP.
Does it has to be exactly 48 MHz?

I open again STMCubeIDE to update generic_clock.c - some days before ago I only configured SystemClock.
"SDMMC1 Clock Mux" is grey, so not active?
I have to go to "Pinout&Configuration" tab and set SDMMC1 from 'disable' to 'SD 4bits wide bus' or another one.

If I choose in "PLL1 Source Mux" output diverd \Q to 10, I get 50 MHz, that's too fast. With \Q = 11 it is 45,45 MHz.
HSI48 seems to go only to "MCO1 Source Mux" and "CRS clock" - I'm unable to find CRS clock ...

STM32H562-48MHz.jpg
STM32H562-48MHz.jpg (86.11 KiB) Viewed 4901 times
With "PLL1 Source Mux" I can use input divider 2, mulitplier 12 and output divider 4 to get 48 MHz.
But I don't see it inside SystemClock_Config()

Code: Select all

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);

  while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV2;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLL1_SOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 125;
  RCC_OscInitStruct.PLL.PLLP = 2;
  RCC_OscInitStruct.PLL.PLLQ = 11;
  RCC_OscInitStruct.PLL.PLLR = 2;
  RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1_VCIRANGE_2;
  RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1_VCORANGE_WIDE;
  RCC_OscInitStruct.PLL.PLLFRACN = 0;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_PCLK3;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure the programming delay
  */
  __HAL_FLASH_SET_PROGRAM_DELAY(FLASH_PROGRAMMING_DELAY_2);
}
And it still not works.
fpiSTM
Posts: 1944
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 108
Location: Le Mans
Contact:

Re: STM32H562 board and STM32SD - problems

Post by fpiSTM »

STM32ardui wrote: Thu Sep 05, 2024 10:55 am
fpiSTM wrote: Thu Sep 05, 2024 7:25 am
STM32ardui wrote: Wed Sep 04, 2024 1:20 pm Does soemone ever tested STM32SD succesful with SDIO?
Seriously? You really think the library was never tested ?
Sounds like you are upset?
Right. This comment is useless and do not help you to get support. Mainly in red.
And this is not the first time that your comments have offended users.
STM32ardui wrote: Thu Sep 05, 2024 10:55 am I only see some code snipets, but no complete example.
Examples are the same than Arduino SD library (over SPI). They are basics to help users to start and test their setup.
So use it to validate your setup is correct if it didn't work then no need to test a more complex code.
STM32ardui wrote: Thu Sep 05, 2024 10:55 am And may I remind you, that example SPI_loop.ino stills uses a SPI.begin(CS_PIN), that is not valid anymore?
How I can be sure, that other (older) examples will still work today?
When you found an issue, please, submit an issue on the dedicated repo to help to track it. This will avoid to forget it.
I try to always follows "all" discussions but sometimes I can forget...
This is a community project all help are welcome.
STM32ardui wrote: Thu Sep 05, 2024 10:55 am
fpiSTM wrote: Thu Sep 05, 2024 7:47 am What is your "SD CARD FORMATTER". Is it really a FAT 32 partition?
It is from SD Association.
I use it to get definitely a correct formatted card (instead of using Windows for this task).
Fine.
STM32ardui wrote: Thu Sep 05, 2024 10:55 am
fpiSTM wrote: Thu Sep 05, 2024 7:47 am First use default example.
???
None example sets pins for {MOSI, MISO, CLK, CS} or {DAT0 - DAT3, CK, CMD} !
So how can I know, if it is made for SPI, SDIO or SDMMC or it will work for all?
This is explicitly written in the README:
https://github.com/stm32duino/STM32SD?t ... or-arduino
With an STM32 board with SD card slot availability, this library enables reading and writing on SD card using SD card slot of a STM32 board (NUCLEO, DISCOVERY, ...). This library is for SD card slots connected to the SDIO-/SDMMC-hardware of the processor. For slots connected to SPI-hardware use the standard Arduino SD library.
STM32ardui wrote: Thu Sep 05, 2024 10:55 am
STM32ardui wrote: Wed Sep 04, 2024 1:20 pm
STM32ardui wrote: Wed Sep 04, 2024 1:20 pm Does librray detect inserted card by LOW-level?
https://github.com/stm32duino/STM32SD/b ... _sd.c#L452
From this code part

Code: Select all

if (!LL_GPIO_IsInputPinSet(SD_detect_gpio_port, SD_detect_ll_gpio_pin)) {
    status = SD_PRESENT;
  }
I guess, that _LL_ stand for "low level library" instead of HAL?
A port is detected and a pin is detected - but which logic level? HIGH or LOW?
You are an expert for STM32 but you simply forget, that other users are not!
LOW as pin configured PULLUP.
Sorry, but when I read you dig until the FatFS code I though you were able to see/understand this line of code. Sorry, if I offend you,
STM32ardui wrote: Thu Sep 05, 2024 10:55 am The readme for STM32SD:
SD detect and timeout
SD_DETECT_PIN pin number
SD_DATATIMEOUT constant for Read/Write block
Please add information to readme
- SD presence is recognized by LOW-level (if it is correct)
- library automatically adds pullup to SD_DETECT_PIN
so everyone is able to know.
Feel free to contribute and provide a PR. Any contribution are welcome.
STM32ardui wrote: Thu Sep 05, 2024 10:55 am BTW: are there also pullups for DAT0 - DAT3 and CMD?
It depends of the pin:
https://github.com/stm32duino/Arduino_C ... #L476-L518
fpiSTM
Posts: 1944
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 108
Location: Le Mans
Contact:

Re: STM32H562 board and STM32SD - problems

Post by fpiSTM »

STM32ardui wrote: Thu Sep 05, 2024 10:56 am But I don't see it inside SystemClock_Config()

And it still not works.
The code generated by CubeMX is splitted per IP. Check the file with "msp" in its name you will find the code to set the SDIO clock.
ag123
Posts: 1898
Joined: Thu Dec 19, 2019 5:30 am
Answers: 30

Re: STM32H562 board and STM32SD - problems

Post by ag123 »

out of curiosity, I dug around for some docs
the SDIO simplified specs seem to cover some of it
https://www.sdcard.org/downloads/pls/

then another doc found here about it
https://www.infineon.com/dgdl/Infineon- ... a3a9973619

it seemed SDIO may not necessarily need 48 Mhz, rather
2.1 SDIO Card Types
This specification defines two types of SDIO cards. The Full-Speed card supports SPI, 1-bit SD and the
4-bit SD transfer modes at the full clock range of 0-25MHz. Full-Speed SDIO cards have a data transfer
rate of over 100 Mb/second (10 MB/Sec). The second version of the SDIO card is the Low-Speed SDIO
card. This card requires only the SPI and 1-bit SD transfer modes. 4-bit support is optional. In addition,
Low-Speed SDIO cards shall support a full clock range of 0-400 KHz. The intended use of Low-Speed
cards is to support low-speed I/O capabilities with a minimum of hardware. The Low-Speed cards support
such functions as modems, bar-code scanners, GPS receivers etc. If a card is a 'Combo card' (memory
plus SDIO) then Full-Speed and 4-bit operation is mandatory for both the memory and SDIO portions of
the card.
I'm not too sure if stm32 may have specific requirements to do true SDIO.

And that these info are kind of 'dated' considering that today's 'high speed' cards may possibly operate at much higher speeds.

A trouble with SPI, SDIO etc is that it may take monitoring signals on the line to literally even do a 'dummy proof' check if things work if at all.
don't think there is a simple way as 'blink a led' for that test.

it may take a logic analyzer or scope and I've a lousy (low speed) one here
viewtopic.php?t=116

I think I remembered once succeeded working with SD cards using the SPI interface, using some SDFat libraries e.g.
https://github.com/greiman/SdFat
but that is with 'old' stm32f103 and (roger's/steve's) libmaple core, I'm not sure how well if they'd still work at all today.

I have some issues working the tft lcd on weact stm32h723xx board
https://github.com/WeActStudio/MiniSTM32H7xx
again I think it is some SPI issues 'stm' core, but I've not yet got down to really study the issue e.g. with a logic analyzer etc.
so today I've not got the lcd to work yet.

a strange and curious thing would be about the pullup resistors
https://www.mouser.com/datasheet/2/302/ ... 187808.pdf
Table 3. SD-memory card operating conditions
RCMD external pull-up resistor value to prevent bus
floating
10 100 k
RDAT external pull-up resistor value to prevent bus
floating; DAT0, DAT1 and DAT2
10 100 k
RDAT3 SD-memory card internal pull-up resistor value
DAT3/CD pin only
it seemed based on this that the pull ups may not be absolutely necessary, and that the pull ups should be a fairly high resistance to prevent the line from being 'stuck' high.
my guess is without pull ups, there could be some troubles with the line floating if both stm32 and the sd card switch those pins DAT0-DAT3 to floating e.g. INPUT mode.
So I'd guess internal INPUT_PULLUP is more than adequate to fix that.
Post Reply

Return to “General discussion”