View previous topic :: View next topic |
Author |
Message |
salih260
Joined: 19 Feb 2018 Posts: 23
|
SD kart programming using elm chan PetitFat library |
Posted: Mon Feb 19, 2018 3:56 am |
|
|
Hi everybody,
I am using the elm-chan FAT library for an SD card project.
1) I ran the diskio.c and diskio.h library. So I can read and write data to the relevant sectors of the SD card. I think this is a sign that there is no hardware error. I pulled up the 4.7k resistors SDI, SDO, SCK terminals as hardware. Pf_mount () also works. The pf_read () function gives a FR_NO_FILE error even though there is a txt file for your SD card.
2) I can read and write data to the SD card in almost all libraries I found on the internet, mainly CCS C's own library, but FAT parts did not work.
3) I did not test the SD card in FAT16 format first, then tried formatting it as FAT32 again. He does not react at all.
4) Write and read data to a text file I want to do. We can call it a kind of datalogger.
> Program version: CCS C version 5.015
> Microcontroller: dsPIC33
> SD card size: 1 GB
Good work... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Mon Feb 19, 2018 4:51 am |
|
|
The filename needs to be in a RAM buffer, not as a constant string. So:
Code: |
byte File[15]={"test.txt"};
FResult = pf_open(File);
|
not:
Code: |
FResult = pf_open("test.txt");
|
#device PASS_STRINGS=IN_RAM
may make it work.
It's a known issue with this library. You'll find it discussed in the MicroChip forum, where people have had the same problem. |
|
|
salih260
Joined: 19 Feb 2018 Posts: 23
|
|
Posted: Mon Feb 19, 2018 5:27 am |
|
|
Thanks for reply.
I know. So this my code:
Code: |
char fileTxt[] = "test.txt";
rc = pf_open(&fileTxt);
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Mon Feb 19, 2018 8:31 am |
|
|
First of all, why use FAT32?. With a 1GB card, FAT16 is the preferred format for Windows. Now you do realise you have to set the bits to switch the driver to FAT32 mode?.
Code: |
#define _FS_FAT12 0 /* Enable FAT12 */
#define _FS_FAT16 0 /* Enable FAT16 */
#define _FS_FAT32 1 /* Enable FAT32 */
|
You don't actually want the '&' in your call. An array name _is_ the pointer to the array. So:
&fileTxt[0] == fileTxt
Now I think the compiler does automatically correct that, but it's worth using the correct syntax. Also switch to byte, rather than char, since you are on a PIC33, where a char is signed. It shouldn't matter for the values you are using, but can lead to problems later.... |
|
|
salih260
Joined: 19 Feb 2018 Posts: 23
|
|
Posted: Tue Feb 20, 2018 12:04 am |
|
|
I made the code like you said. But my problem is still going on.
I formatted the SD card as FAT16.
I corrected them:
1)
Code: |
#define _FS_FAT12 0 /* Disable FAT12 */
#define _FS_FAT16 1 /* Disable FAT16 */
#define _FS_FAT32 0 /* Disable FAT32 */
|
2)
Code: |
const char dosyaAdi[15] = {"test.txt"};
rc = pf_open(dosyaAdi);
|
I followed the codes of the PetitFat library. The problem is:
Code: |
static
FRESULT dir_find (
DIR *dj, /* Pointer to the directory object linked to the file name */
BYTEE *dir_r /* 32-BYTEE working buffer */
)
{
FRESULT res;
BYTEE c;
res = dir_rewind(dj); /* Rewind directory object */
if (res != FR_OK) return res;
do {
res = disk_readp(dir_r, dj->sect, (dj->index % 16) * 32, 32) /* Read an entry */
? FR_DISK_ERR : FR_OK;
if (res != FR_OK) break;
c = dir_r[DIR_Name]; /* First character */
if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
if (!(dir_r[DIR_Attr] & AM_VOL) && !mem_cmp(dir_r, dj->fn, 11)) /* Is it a valid entry? */
break;
res = dir_next(dj); /* Next entry */
} while (res == FR_OK);
return res;
}
|
BYTEE --> typedef unsigned int BYTEE
dir_rewind --> work.
disk_readp --> work.
I could not find the wrong place.
Thank you for your help. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Tue Feb 20, 2018 8:50 am |
|
|
const char dosyaAdi[15] = {"test.txt"};
Get rid of the const.
PetitFat does not accept constant strings. It only accepts ones that are in RAM. |
|
|
salih260
Joined: 19 Feb 2018 Posts: 23
|
|
Posted: Tue Feb 20, 2018 11:13 pm |
|
|
I tried like you say. But not working. This pf_open function definition.
Code: |
FRESULT pf_open (
const char* path /* [IN] Pointer to the file neme */
);
|
Code: |
char dosyaAdi[15] = {"test.txt"};
|
After editing:
Code: |
FRESULT pf_open (
const char* path /* [IN] Pointer to the file neme */
){
printf("path = %s", path); or printf("path = %s", *path);
}
printf Ouput is not "test.txt"
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Wed Feb 21, 2018 1:42 am |
|
|
Add the following:
At the top of your code, immediately after the include for the processor, add:
Code: |
#device ANSI
#device CONST=READ_ONLY
|
The library you are using is using ANSI meanings for the keywords, and you are trying to compile it with CCS meanings. |
|
|
salih260
Joined: 19 Feb 2018 Posts: 23
|
|
Posted: Wed Feb 21, 2018 1:52 am |
|
|
firstly thanks for reply.
I did what you said. But compiler give me error.
Code: |
*** Error 12 "" Line 232(16,17): Undefined identifier -- SPI_WRITE
|
I use this device:
Code: |
#include <33EP128MC506.h>
#device ICD=TRUE
#device CONST=ROM
#device PASS_STRINGS=IN_RAM
|
Last edited by salih260 on Wed Feb 21, 2018 2:40 am; edited 2 times in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Wed Feb 21, 2018 1:55 am |
|
|
Your problem now is that you have the functions using the wrong case.
Switching to ANSI mode, means that the compiler is now case significant.
So SPI_WRITE is different from spi_write
You are going to need to go through all your code and make the case used in the definitions match what you use in the calls..... |
|
|
salih260
Joined: 19 Feb 2018 Posts: 23
|
|
Posted: Wed Feb 21, 2018 2:02 am |
|
|
Ok Ttelmah. Thank you.
I'll write the results here after you test it. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Wed Feb 21, 2018 2:08 am |
|
|
The alternative would be to remove the 'const' keyword.
Leave the compiler in CCS mode, and just do a search and replace on the whole library replacing 'const' with '' (nothing). |
|
|
salih260
Joined: 19 Feb 2018 Posts: 23
|
|
Posted: Wed Feb 21, 2018 9:01 am |
|
|
I applied what you said. But my problem is still going on.
Code: |
#device ICD=TRUE
//#device CONST=ROM
#device PASS_STRINGS=IN_RAM
#device ANSI
#device CONST=READ_ONLY
|
Code: |
printf("\r\nMount a volume...\n");
rc = pf_mount(&fs);
if(rc) printf("Mount ERROR! %d\n", rc);
else printf("Mount OK! %d\n", rc);
printf("\n\rOpen test.txt ...\n");
char dosyaAdi[] = {"test.txt"};
rc = pf_open(dosyaAdi);
if(rc) printf("Open ERROR! %X \n", rc);
else printf("Open OK! %X \n", rc);
|
Code: |
#define _USE_READ 0 /* Enable pf_read() function */
#define _USE_DIR 1 /* Enable pf_opendir() and pf_readdir() function */
#define _USE_LSEEK 0 /* Enable pf_lseek() function */
#define _USE_WRITE 0 /* Enable pf_write() function */
#define _FS_FAT12 0 /* Enable FAT12 */
#define _FS_FAT16 1 /* Enable FAT16 */
#define _FS_FAT32 0 /* Enable FAT32 */
|
* I can read and write sectors of the SD card.
* pf_mount ok
But pf_open gives a FR_NO_FILE error. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Feb 21, 2018 9:52 am |
|
|
Have you ever tried the driver with a PIC24 series device ?
I ask cause I downloaded the zip file and they used a 24 series PIC NOT a 32 series. There's probably a lot of 'minor' differences between them.
While I don't use either, I'd start by confirming with a 24 series BEFORE attempt a 32 PIC.
I never take at 'face value' that a driver works,even migrating from 16 to 18 series there are differences.....
just food for thought
Jay |
|
|
salih260
Joined: 19 Feb 2018 Posts: 23
|
|
Posted: Wed Feb 21, 2018 11:50 pm |
|
|
Thanks for reply Ttelmah and temtronic..
I did not try pic24 because there was not much time.
does Petit Fat not work?
the problem still continues... :( |
|
|
|