SD card

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SD card

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by shabnam on Mon Jul 13 00:21:03 MST 2015
hello dear all,
i am working on a project for create a text file on SD card. i work on LPC1768. but it can't create a text file i receive this error: A hard error occurred in the low level disk I/O layer   i write here my code, i don't know what is the problem. i attached my code. thanks
/****************************************Copyright (c)****************************************************
**                                      
**                              
**
**--------------File Info---------------------------------------------------------------------------------
** File name:               main.c
** Descriptions:            The File System application function
**
**--------------------------------------------------------------------------------------------------------
*********************************************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "ff.h"
#include "lpc17xx_uart.h"
#include "SPI_MSD_Driver.h"
#include <stdio.h>
#include <string.h>

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

/* Private variables ---------------------------------------------------------*/
FATFS fs;         /* Work area (file system object) for logical drive */
FIL fsrc;         /* file objects */   
FRESULT res;
UINT br;

char path[512]="0:";
uint8_t textFileBuffer[] = "Thank \r\n";   

/* Private function prototypes -----------------------------------------------*/
int SD_TotalSize(void);
void USART_Configuration(void);
FRESULT scan_files (char* path);


/*******************************************************************************
* Function Name  : Delay
* Description    : Delay Time
* Input          : - nCount: Delay Time
* Output         : None
* Return         : None
* Attention : None
*******************************************************************************/
void  Delay (uint32_t nCount)
{
  for(; nCount != 0; nCount--);
}

/*******************************************************************************
* Function Name  : main
* Description    : Main program
* Input          : None
* Output         : None
* Return         : None
* Attention : None
*******************************************************************************/
int main(void)
{
char A;

USART_Configuration();

if( _card_insert() == 0 )
    {
  printf("-- SD card detected OK \r\n");
    }
    else
    {
      printf("-- Please connect a SD card \r\n");
      while( _card_insert() != 0 );
      printf("-- SD card connection detected \r\n");
  Delay(0xffffff);
    }
A=f_mount(0,&fs);
  printf("%d",A);
res = f_open( &fsrc , "0:/Demo.TXT" , FA_CREATE_ALWAYS | FA_OPEN_ALWAYS);
printf("%d",res);
    if ( res == FR_OK )
    { 
      /* Write buffer to file */
      res = f_write(&fsrc, textFileBuffer, sizeof(textFileBuffer), &br);     
printf("%d/r/n",res);
printf("Demo.TXT successfully created        \r\n");
    
      /*close file */
      f_close(&fsrc);      
    }
    else if ( res == FR_EXIST )
    {
  printf("Demo.TXT created in the disk      \r\n");
    }

scan_files(path);
SD_TotalSize();
f_close(&fsrc);
    /* Infinite loop */
    while (1)
{
    }
}
void USART_Configuration(void)
{ 
    uint32_t  Fdiv;
PINSEL_CFG_Type PinCfg;


/*
 * Initialize UART2 pin connect
 */
PinCfg.Funcnum = 1;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Portnum = 0;
PinCfg.Pinnum = 10;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 11;
PINSEL_ConfigPin(&PinCfg);

/* Initialize UART Configuration parameter structure to default state:
 * Baudrate = 115200bps
 * 8 data bit
 * 1 Stop bit
 * None parity
 */
LPC_SC->PCONP = LPC_SC->PCONP|(1<<24);  /* UART2 Power bit */

    LPC_UART2->LCR = 0x83;              /* 8 bits, no Parity, 1 Stop bit */

    #define FOSC    12000000                  /* Õñµ´Æ÷ƵÂÊ */

    #define FCCLK   (FOSC  * 8)               /* Ö÷ʱÖÓƵÂÊ<=100Mhz FOSCµÄÕûÊý±¶ */

    #define FPCLK   (FCCLK / 4)               /* ÍâÉèʱÖÓƵÂÊ FCCLKµÄ1/2 1/4 */

Fdiv = ( FPCLK / 16 ) / 115200 ;      /*baud rate */

    LPC_UART2->DLM = Fdiv / 256;
    LPC_UART2->DLL = Fdiv % 256;
    LPC_UART2->LCR = 0x03;              /* DLAB = 0 */
    LPC_UART2->FCR = 0x07;              /* Enable and reset TX and RX FIFO. */
}


/*******************************************************************************
* Function Name  : USART_Configuration
* Description    : Configure USART1 
* Input          : None
* Output         : None
* Return         : None
* Attention : None
*******************************************************************************/

/*******************************************************************************
* Function Name  : scan_files
* Description    : ËÑË÷ÎļþĿ¼ÏÂËùÓÐÎļþ
* Input          : - path: ¸ùĿ¼
* Output         : None
* Return         : FRESULT
* Attention : ²»Ö§³Ö³¤ÎļþÃû
*******************************************************************************/
FRESULT scan_files (char* path)
{
    FILINFO fno;
    DIR dir;
    int i;
    char *fn;
#if _USE_LFN
    static char lfn[_MAX_LFN * (_DF1S ? 2 : 1) + 1];
    fno.lfname = lfn;
    fno.lfsize = sizeof(lfn);
#endif

    res = f_opendir(&dir, path);
    if (res == FR_OK) {
        i = strlen(path);
        for (;;) {
            res = f_readdir(&dir, &fno);
            if (res != FR_OK || fno.fname[0] == 0) break;
            if (fno.fname[0] == '.') continue;
#if _USE_LFN
            fn = *fno.lfname ? fno.lfname : fno.fname;
#else
            fn = fno.fname;
#endif
            if (fno.fattrib & AM_DIR) {
                sprintf(&path, "/%s", fn);
                res = scan_files(path);
                if (res != FR_OK) break;
                path = 0;
            } else {
                printf("%s/%s \r\n", path, fn);
            }
        }
    }
    return res;
}

/*******************************************************************************
* Function Name  : SD_TotalSize
* Description    : Îļþ¿Õ¼äÕ¼ÓÃÇé¿ö
* Input          : None
* Output         : None
* Return         : ·µ»Ø1³É¹¦ ·µ»Ø0ʧ°Ü
* Attention : None
*******************************************************************************/
int SD_TotalSize(void)
{
    FATFS *fs;
    DWORD fre_clust;        

    res = f_getfree("0:", &fre_clust, &fs);  /* ±ØÐëÊǸùĿ¼£¬Ñ¡Ôñ´ÅÅÌ0 */
    if ( res==FR_OK ) 
    {
  /* Print free space in unit of MB (assuming 512 bytes/sector) */
      printf("\r\n%d MB total drive space.\r\n"
           "%d MB available.\r\n",
           ( (fs->n_fatent - 2) * fs->csize ) / 2 /1024 , (fre_clust * fs->csize) / 2 /1024 );

  return ENABLE;
}
else 
  return DISABLE;   
} 

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
/* wait for current transmission complete - THR must be empty */
while (UART_CheckBusy(LPC_UART2) == SET);

UART_SendByte(LPC_UART2, ch);

return ch;
}

#ifdef  DEBUG
/*******************************************************************************
* @briefReports the name of the source file and the source line number
* where the CHECK_PARAM error has occurred.
* @param[in]file Pointer to the source file name
* @param[in]    line assert_param error line source number
* @returnNone
*******************************************************************************/
void check_failed(uint8_t *file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* Infinite loop */
while(1);
}
#endif

///*********************************************************************************************************
//      END FILE
//*********************************************************************************************************/


//FATFS FatFs;   /* Work area (file system object) for logical drive */

//int main (void)
//{
//    FIL fil;       /* File object */
//    char line[82]; /* Line buffer */
//    FRESULT fr;    /* FatFs return code */


//    /* Register work area to the default drive */
//    f_mount(0,&FatFs);

//    /* Open a text file */
//    fr = f_open(&fil, "message.txt", FA_READ);
//    if (fr) return (int)fr;

//    /* Read all lines and display it */
//    while (f_gets(line, sizeof line, &fil))
//        printf(line);

//    /* Close the file */
//    f_close(&fil);

//    return 0;
//}

Original Attachment has been moved to: fat.rar

Labels (1)
0 Kudos
1 Reply

492 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by embd02161991 on Thu Jul 16 22:20:06 MST 2015
Hi,

Have you tried the sdmmc example in lpcopen for LPC17xx ? The example can help as a starting point.
https://www.lpcware.com/content/nxpfile/lpcopen-software-development-platform-lpc17xx-packages

Thanks
NXP Technical Support
0 Kudos