So I tweeked a bit with an example regarding SD card (2GB). Mainly I wish to load a message from a text file which is stored inside an SD card to be displayed on the touch screen in which later on hoepfully I would be able to use the same logic of this program to allow me to laod a bitmap image from SD CARD. However when I am running the program, the file is not read and it is not creating a new file either! is there something wrong with the logic of this program? As you can see I made a function that whenever there is a read or write error it goes to the fault_err() function in which the program terminates.
Any help regarding this issue would be greatly appreciated.
Thanks in advance below is my source code
/*Private Defines ------------------------------------------------------------*/
#define LCD_BASE_Data ((u32)(0x60000000|0x00100000))
#define LCD_Data (*(vu16 *)LCD_BASE_Data)
/* Private variables ---------------------------------------------------------*/
SD_Error Status = SD_OK;
FATFS filesystem; /* volume lable */
FRESULT ret; /* Result code */
FIL file; /* File object */
DIR dir; /* Directory object */
FILINFO fno; /* File information object */
UINT bw, br;
uint8_t buff[128];
/* Private function prototypes -----------------------------------------------*/
static void Delay(__IO uint32_t nCount);
static void fault_err (FRESULT rc);
void delay(__IO uint32_t nCount);
/*Main Function --------------------------------------------------------------*/
int main(void)
{
uint8_t text [100];
volatile uint32_t dlycnt;
/* wait the power stable */
for (dlycnt = 0; dlycnt < 10000000; dlycnt++);
/* Initializes LCD */
STM32f4_Discovery_LCD_Init();
/* Clear the LCD */
LCD_Clear(LCD_COLOR_WHITE);
/* Set the LCD Text size */
LCD_SetFont(&Font16x24);
/* Clear the LCD */
LCD_Clear(LCD_COLOR_BLACK);
/* Set the LCD Back Color */
LCD_SetBackColor(LCD_COLOR_BLACK);
/* Set The LCD Text colour */
LCD_SetTextColor(LCD_COLOR_WHITE);
STM32f4_Discovery_Debug_Init();
LCD_DisplayStringLine(LINE(2), " Welcome");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
LCD_SetFont(&Font8x12);
/* Initialize Debug uart available on Devkit407 board *******************/
STM32f4_Discovery_Debug_Init();
/* mount the filesystem */
if (f_mount(0, &filesystem) != FR_OK) {
LCD_DisplayStringLine (Line2, " Could not open filesystem");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
}
Delay(10);
LCD_DisplayStringLine (Line2, " Open a test file");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
ret = f_open(&file, "message", FA_READ);
if (ret) {
LCD_DisplayStringLine (Line2, " Not exist the test file");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
} else {
LCD_DisplayStringLine (Line2, " Type the file content");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
for (;;) {
ret = f_read(&file, buff, sizeof(buff), &br); /* Read a chunk of file */
if (ret || !br) {
break; /* Error or end of file */
}
buff[br] = 0;
sprintf((char*)text," %s",buff);//Display the Message
LCD_DisplayStringLine(LINE(3), text);
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
}
if (ret) {
LCD_DisplayStringLine (Line2, " Read the file error");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
fault_err(ret);
}
LCD_DisplayStringLine (Line2, " Close the File");
ret = f_close(&file);
if (ret) {
LCD_DisplayStringLine (Line2, " Close the file error");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
}
}
/* hello.txt write test*/
Delay(50);
LCD_DisplayStringLine (Line2, " Create a new file");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
ret = f_open(&file, "HELLO.TXT", FA_WRITE | FA_CREATE_ALWAYS);
if (ret)
{
LCD_DisplayStringLine (Line2, " Create a new file error");
fault_err(ret);
}
else
{
LCD_DisplayStringLine (Line2, " Write a text data. (hello.txt)");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
ret = f_write(&file, "Hello world!", 14, &bw);
if (ret)
{
LCD_DisplayStringLine (Line2, " Write a text data to file error");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
}
else
{
sprintf((char*)text," %u bytes written", bw);
LCD_DisplayStringLine (Line2, text);
}
Delay(50);
LCD_DisplayStringLine (Line2, " Close the file");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
ret = f_close(&file);
if (ret)
{
LCD_DisplayStringLine (Line2, " Create a new file error");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
}
}
/* hello.txt read test*/
Delay(50);
LCD_DisplayStringLine (Line2, " Read the file");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
ret = f_open(&file, "HELLO.TXT", FA_READ);
if (ret)
{
LCD_DisplayStringLine (Line2, " Open file error");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
}
else
{
LCD_DisplayStringLine (Line2, " Type the file content");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
for (;;)
{
ret = f_read(&file, buff, sizeof(buff), &br); /* Read a chunk of file */
if (ret || !br)
{
break; /* Error or end of file */
}
buff[br] = 0;
sprintf((char*)text,"%s",buff);
LCD_DisplayStringLine(LINE(3), text);
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
}
if (ret)
{
LCD_DisplayStringLine (Line2, " Read File Error");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
fault_err(ret);
}
LCD_DisplayStringLine (Line2, " Close the File");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
ret = f_close(&file);
if (ret)
{
LCD_DisplayStringLine (Line2, " Close file error");
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
}
}
/* directory display test*/
Delay(50);
LCD_DisplayStringLine (Line2, "Open Root Directory");
ret = f_opendir(&dir, "");
if (ret)
{
LCD_DisplayStringLine (Line2, "Open root directory error");
} else
{
LCD_DisplayStringLine (Line2, "Directory listing...");
for (;;)
{
ret = f_readdir(&dir, &fno); /* Read a directory item */
if (ret || !fno.fname[0])
{
break; /* Error or end of dir */
}
if (fno.fattrib & AM_DIR)
{
sprintf((char*)text," <dir> %s\n\r", fno.fname);
LCD_DisplayStringLine(LINE(3), text);
}
else
{
sprintf((char*)text,"%8lu %s\n\r", fno.fsize, fno.fname);
LCD_DisplayStringLine(LINE(3), text);
}
}
if (ret)
{
LCD_DisplayStringLine (Line2, "Read a directory error");
fault_err(ret);
}
}
Delay(50);
LCD_DisplayStringLine (Line2, "Test completed\n\r");
/* Infinite loop */
while (1)
{
// STM_EVAL_LEDToggle(LED3);
Delay(100);
}
}
/**
* @brief FatFs err dispose
* @param None
* @retval None
*/
static void fault_err (FRESULT rc)
{
uint8_t text [100];
const char *str =
"OK\0" "DISK_ERR\0" "INT_ERR\0" "NOT_READY\0" "NO_FILE\0" "NO_PATH\0"
"INVALID_NAME\0" "DENIED\0" "EXIST\0" "INVALID_OBJECT\0" "WRITE_PROTECTED\0"
"INVALID_DRIVE\0" "NOT_ENABLED\0" "NO_FILE_SYSTEM\0" "MKFS_ABORTED\0" "TIMEOUT\0"
"LOCKED\0" "NOT_ENOUGH_CORE\0" "TOO_MANY_OPEN_FILES\0";
FRESULT i;
for (i = (FRESULT)0; i != rc && *str; i++) {
while (*str++) ;
}
sprintf((char*)text," rc=%u FR_%s", (UINT)rc, str);
LCD_DisplayStringLine (Line2, text);
Delay(200);
LCD_Clear(LCD_COLOR_BLACK);
//STM_EVAL_LEDOn(LED6);
while(1);
}
/**
* @brief Delay
* @param None
* @retval None
*/
static void Delay(__IO uint32_t nCount)
{
__IO uint32_t index = 0;
for (index = (100000 * nCount); index != 0; index--);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_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