cancel
Showing results for 
Search instead for 
Did you mean: 

FATFS_LinkDriver(&USER_Driver, USER_Path)==0) question ?

antonius
Senior
Posted on January 26, 2016 at 06:40

Guys,

Can I use SPI2 for SDcard with STM32CubeMX ? I can see from : #define HAL_SD_MODULE_ENABLED but there's no SPI implementation for it, it's for SDIO, and STM32F107 doesn't have SDIO... I tried to run if(FATFS_LinkDriver(&USER_Driver, USER_Path)==0) { HAL_UART_Transmit(&huart1, ''FATFS_LinkDriver OK! \r\n'', 55, 1000); // Debug if (f_mount(&FatFs, (TCHAR const*)USER_Path, 0)==FR_OK) { HAL_UART_Transmit(&huart1, ''F_MOUNT \r\n'', 55, 1000); // Debug if (f_open(&Fil, ''newfile.txt'', FA_WRITE | FA_CREATE_ALWAYS) == FR_OK) { HAL_UART_Transmit(&huart1, ''F_OPEN test_write..\r\n'', 15, 1000); // As a pointer, with a length if (f_write(&Fil, ''Test write line1 !\r\n'', 17, &bw) == FR_OK) /* Write data to the file */ { HAL_UART_Transmit(&huart1, ''F_WRITE 1 \r\n'', 55, 1000); // Debug //f_write(&Fil, ''Next line test\r\n'', 16, &bw); /* Write data to the file */ //HAL_UART_Transmit(&huart1, ''F_WRITE 2 \n\r'', 55, 1000); // Debug //HAL_UART_Transmit(&huart1, ''FWRITE \n\r'', 15, 1000); // As a pointer, with a length HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6 , 0 ); HAL_Delay(5000); HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6 , 1 ); } f_close(&Fil); /* Close the file */ HAL_UART_Transmit(&huart1, ''FCLOSE \r\n'', 15, 1000); // As a pointer, with a length } } } I can pass FATFS_LinkDriver(&USER_Driver, USER_Path)==0) but I can't continue to f_mount .... What's missing here ? I can not see my debug message in disk_initialize but it can give FR_OK ?? user_diskio.h extern Diskio_drvTypeDef USER_Driver; <== I got error ''unknown type Diskio_drvTypeDef'' on this line, but I can compile it, what should I fix ? user_diskio.c

Diskio_drvTypeDef USER_Driver =
 {
 USER_initialize,
 USER_status,
 USER_read, 
 #if _USE_WRITE
 USER_write,
 #endif /* _USE_WRITE == 1 */ 
 #if _USE_IOCTL == 1
 USER_ioctl,
 #endif /* _USE_IOCTL == 1 */
 };

 /**
* @brief Initializes a Drive
* @param None
* @retval DSTATUS: Operation status
*/
DSTATUS USER_initialize(void)
{
HAL_UART_Transmit(&huart1, ''USER INITIALIZE START! \r\n'', 100, 1000); //debug message 
Stat = STA_NOINIT;
DSTATUS stat = RES_OK;
BYTE n, cmd, ty, ocr[4];
BYTE pdrv; 
if (pdrv) return STA_NOINIT; /* Supports only single drive */
power_off(); /* Turn off the socket power to reset the card */
if (stat & STA_NODISK) return stat; /* No card in the socket */;
power_on(); /* Turn on the socket power */
FCLK_SLOW();
for (n = 10; n; n--) xchg_spi(0xFF); /* 80 dummy clocks */
ty = 0;
if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
Timer1 = 100; /* Initialization timeout of 1000 msec */
if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2? */
for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); /* Get trailing return value of R7 resp */
if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
while (Timer1 && send_cmd(ACMD41, 1UL << 30)); /* Wait for leaving idle state (ACMD41 with HCS bit) */
if (Timer1 && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);
ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 */
}
}
} else { /* SDv1 or MMCv3 */
if (send_cmd(ACMD41, 0) <= 1) {
ty = CT_SD1; cmd = ACMD41; /* SDv1 */
} else {
ty = CT_MMC; cmd = CMD1; /* MMCv3 */
}
while (Timer1 && send_cmd(cmd, 0)); /* Wait for leaving idle state */
if (!Timer1 || send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */
ty = 0;
}
}
CardType = ty;
deselect();
if (ty) { /* Initialization succeded */
stat &= ~STA_NOINIT; /* Clear STA_NOINIT */
FCLK_FAST();
HAL_UART_Transmit(&huart1, ''USER INITIALIZE SUCCEDED! \r\n'', 100, 1000);
} else { /* Initialization failed */
power_off();
}
HAL_UART_Transmit(&huart1, ''USER INITIALIZE FINISHED! \n \n'', 100, 1000);
return stat;
/* USER CODE HERE */
HAL_UART_Transmit(&huart1, ''DISK INITIALIZE user_diskio.c BEGIN! \r\n'', 100, 1000);
Stat &= ~STA_NOINIT;
return Stat;
}

fatfs.c

void MX_FATFS_Init(void) 
{
/*## FatFS: Link the USER driver ###########################*/
retUSER = FATFS_LinkDriver(&USER_Driver, USER_Path);
if (retUSER == 0)
{
HAL_UART_Transmit(&huart1, ''DISK connected fatfs.c Line 48!'', 55, 1000); // Debug
} 
ff_gen_drv.c
 /**
* @brief Links a compatible diskio driver and increments the number of active
* linked drivers. 
* @note The number of linked drivers (volumes) is up to 10 due to FatFs limits
* @param drv: pointer to the disk IO Driver structure
* @param path: pointer to the logical drive path 
* @retval Returns 0 in case of success, otherwise 1.
*/
uint8_t FATFS_LinkDriver(Diskio_drvTypeDef *drv, char *path)
{
uint8_t ret = 1;
uint8_t DiskNum = 0;
if(disk.nbr <= _VOLUMES)
{
disk.is_initialized[disk.nbr] = 0;
disk.drv[disk.nbr] = drv; 
DiskNum = disk.nbr++;
path[0] = DiskNum + '0';
path[1] = ':';
path[2] = '/';
path[3] = 0;
ret = 0;
}
return ret;
}

Thanks
8 REPLIES 8
Amel NASRI
ST Employee
Posted on January 27, 2016 at 10:26

Hi h.rick,

Unfortunately, it is difficult to follow the status of issues you are facing with CubeMX to configure SPI for SDcard.

Each time a new discussion on one forum for almost the same topic: 5 on STM32 forum and more than 10 on the STM32 Software Tools and Firmware forum.

This is confusing...don't know where to start exactly...

To be able to efficiently help you, I suggest you provide a clear status on old posts (may be you can add new posts to say that you got answer to a question for example.

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Posted on January 28, 2016 at 05:00

This just goes in circles, with selective cut-n-paste of code fragments, and issues with programming tasks.

Basically if the low level SPI/SD code is non-functional, none of the stuff built on top can be expected to work. Start at the bottom and work up, validating functionality as you go.

One might expect Cube to allow you to pick some pins, an interface (SPI+SD), FatFs, and it generates a working framework to start with. It seems odd to me that with all the effort expended, that there isn't a clean working example available from ST, or one that pops out of the tools. The F1 still seems to be a current platform, yet for years we see a total lack of supporting code for the EVAL boards using SPI and SDHC class cards. Can people even buy the old 128MB MicroSD cards these days?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
antonius
Senior
Posted on February 02, 2016 at 11:58

How can I test low level function ?

I tried calling directly disk initialize and it's showing a good signal on MOSI.....

but the problem is, I can't connect it with

https://www.google.com.au/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&ved=0ahUKEwjzrdTr89jKAhVHymMKHZ7vAE0QFgg3MAQ&url=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder%3D%252Fpublic%252FSTe2ecommunities%252Fmcu%252FLists%252Fcortex_mx_stm32%252FCan%2520I%2520use%...

I don't what's missing or something wrong.....I never create that function, it's from STM32CubeMX ...

Anyone from ST can help ?

I can upload all the codes for analyzing, it should be simple since I have made the disk initialization and it works very well on AVR processor...

Thanks

antonius
Senior
Posted on February 02, 2016 at 11:59

What do you want to know ? where do you want to start ?

antonius
Senior
Posted on February 02, 2016 at 21:56

I can get DISK connected fatfs.c Line 48!    On My serial but nothing in my Logic analyzer....where do I miss here ?   I don't understand until this point....??   void MX_FATFS_Init(void)

{

/*## FatFS: Link the USER driver ###########################*/

//FATFS_LinkDriver(&USER_Driver, USER_Path);

retUSER = FATFS_LinkDriver(&USER_Driver, USER_Path);

//retUSER = FATFS_LinkDriver(&USER_Driver, 0);

if (retUSER == 0)

{

HAL_UART_Transmit(&huart1, ''DISK connected fatfs.c Line 48!'', 31, 1); // Debug

HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6 , 0 );

HAL_Delay(1500);

HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6 , 1 );

}

else

{

HAL_UART_Transmit(&huart1, ''DISK is not connected fatfs.c Line 48!'', 38, 1); // Debug

HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6 , 0 );

HAL_Delay(50);

HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6 , 1 );

}

/* USER CODE BEGIN Init */

/* additional user code for init */

//testSD();

//USER_initialize(0);

/* USER CODE END Init */

}

antonius
Senior
Posted on February 02, 2016 at 22:02

Each time a new discussion on one forum for almost the same topic

Yes it is and never got a good answer from ST,

I follow UM1712, it is not working as written there...

How can I connect my SPI2 initialization to FATFs ?

Where do you want to start ? what do you want to see ? you never ask or reply...

antonius
Senior
Posted on February 02, 2016 at 22:21

SPI2 test is working OK !

as I can see on logic analyzer Code :

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10 , 1 );
HAL_Delay(100); 
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10 , 0 );
uint8_t data1[8] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07 };
HAL_SPI_Transmit(&hspi2,data1,8,1000);

0690X0000060374QAA.jpg
antonius
Senior
Posted on February 02, 2016 at 22:24

There's no SDIO on my STM32F107, so I like it or not I must use SPI2,

so far I used 1Gbyte with SPI and ATMega128, it works WELL...