kiwih / cubeide-sd-card

CubeIDE/CubeMX compatible MMC/SD memory card FatFs driver
Other
100 stars 58 forks source link

f_open error (128) #6

Closed thibeaux closed 3 years ago

thibeaux commented 3 years ago

I'm getting an error 128. I am following this tutorial https://01001000.xyz/2020-08-09-Tutorial-STM32CubeIDE-SD-card/ and was able to solve the issue with the f_open error 3 by fixing the user_diskio.c code by adding in the SPI returns using the user_diskio_spi.h. Now, that I am getting the size available on my SD card to print to putty using my UART. I'm getting a new error. f_open error (128).

  1. I'm using a Nucleo-F446RE with a HW-125 SD Card SPI converter.
  2. Prescaler setting to 256
  3. Data Size 8
  4. MOSI, MISO, SCK, and CS are all on pull ups

I manually changed these values to match my naming scheme `#define CS_HIGH() {HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_SET);}

define CS_LOW() {HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_RESET);}`

thibeaux commented 3 years ago

Don't think I did this step correct in user_diskio_spi.c:

//Make sure you set #define SD_SPI_HANDLE as some hspix in main.h
//Make sure you set #define SD_CS_GPIO_Port as some GPIO port in main.h
//Make sure you set #define SD_CS_Pin as some GPIO pin in main.h
extern SPI_HandleTypeDef SD_SPI_HANDLE;

In user_diskio_spi.c my code begins with this:

#include "stm32f4xx_hal.h"" /* Provide the low-level HAL functions */
#include "user_diskio_spi.h"

//If you want to use a different SPI port change hspi1 here and elsewhere in this file
extern SPI_HandleTypeDef hspi1;

//(Note that the _256 is used as a mask to clear the prescalar bits as it provides binary 111 in the correct position)
#define FCLK_SLOW() { MODIFY_REG(hspi1.Instance->CR1, SPI_BAUDRATEPRESCALER_256, SPI_BAUDRATEPRESCALER_128); }  /* Set SCLK = slow, approx 280 KBits/s*/
#define FCLK_FAST() { MODIFY_REG(hspi1.Instance->CR1, SPI_BAUDRATEPRESCALER_256, SPI_BAUDRATEPRESCALER_8); }    /* Set SCLK = fast, approx 4.5 MBits/s */

#define CS_HIGH()   {HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_SET);}
#define CS_LOW()    {HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_RESET);}

and my main.h looks like this:

/* Private defines -----------------------------------------------------------*/
#define B1_Pin GPIO_PIN_13
#define B1_GPIO_Port GPIOC
#define USART_TX_Pin GPIO_PIN_2
#define USART_TX_GPIO_Port GPIOA
#define USART_RX_Pin GPIO_PIN_3
#define USART_RX_GPIO_Port GPIOA
#define TMS_Pin GPIO_PIN_13
#define TMS_GPIO_Port GPIOA
#define TCK_Pin GPIO_PIN_14
#define TCK_GPIO_Port GPIOA
#define SWO_Pin GPIO_PIN_3
#define SWO_GPIO_Port GPIOB
#define SPI1_CS_Pin GPIO_PIN_6
#define SPI1_CS_GPIO_Port GPIOB
/* USER CODE BEGIN Private defines */
#define SD_SPI_HANDLE hspi1 // added to main.h
/* USER CODE END Private defines */
thibeaux commented 3 years ago

FIXED I forgot to ensure that I formatted this file to a FAT32 format and I did not preload the test.txt file on to the SD card.

Here is proof of success: image