| Author |
Message |
|
|
Posted: Apr 18, 2012 - 02:51 PM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
Hello freaks
I want to import to my project read/write operations from an sd card with the uc3a3256 processor.
The only options that i found are FatFs library and the ASF FAT file system.
I have already worked with Fatfs with a mega32 but not with the avr32 processors.
With ASF i have only seen the examples but not actually work with it.
The i/o operations from the sd card that i need is all the standard file i/o , opening and reading directories.
I was thinking that maybe the ASF driver is too much with the navigators and all this functionality(extra code that i wont even use)
Also ,because i want to work with libtiff that gives handlers for the file i/o functions i was thinking fatfs is more suitable with the functions of the library.
I would like your opinion on which option i should choose.Since i dont have actually worked with the ASF library.
Has anyone got fatfs library working with avr32?
Is there a source file(diskio.h) with the low level i/o functions?
I would like your opinion on which option i should choose.
Thanks |
|
|
| |
|
|
|
|
|
Posted: Apr 19, 2012 - 12:35 PM |
|


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
| At the end of the day the only hardware interaction of FatFs is all isolated in mmc.c and just involves the setup and send/receive use of the SPI and a few IO lines for reading the WP and CP signals from the card socket. Id' have thought it would be fairly trivial to convert that to any CPU architecture as SPI is generally the simplest built-in peripheral in any CPU. |
_________________
|
| |
|
|
|
|
|
Posted: Apr 20, 2012 - 11:33 AM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
Hello
I found all the Platform Dependent functions and macros that need to be changed in mmc.c.
I thought i could just take the spi master driver from the asf and give fatfs those functions.
Code:
/*-----------------------------------------------------------------------*/
/* Transmit a byte to MMC via SPI (Platform dependent) */
/*-----------------------------------------------------------------------*/
#define xmit_spi(dat) spi_write_single(&AVR32_SPI1 , dat )
/*-----------------------------------------------------------------------*/
/* Receive a byte from MMC via SPI (Platform dependent) */
/*-----------------------------------------------------------------------*/
static
BYTE rcvr_spi (void)
{
uint8_t data;
spi_read_single(&AVR32_SPI1 , &data);
return data;
}
/* Alternative macro to receive data fast */
#define rcvr_spi_m(dst) spi_read_single(&AVR32_SPI1 , dst)
/*-----------------------------------------------------------------------*/
/* Power Control (Platform dependent) */
/*-----------------------------------------------------------------------*/
/* When the target system does not support socket power control, there */
/* is nothing to do in these functions and chk_power always returns 1. */
static
void power_on (void)
{
spi_master_init(&AVR32_SPI1);
}
but i dont know what those macros are and what i should assing to them
Code:
/* Port Controls (Platform dependent) */
#define SELECT() /*MMC CS = L */
#define DESELECT() /*MMC CS = H */
#define SOCKPORT /* Socket contact port */
#define SOCKWP 0x2 /* Write protect switch */
#define SOCKINS 0x1 /* Card detect switch */
#define FCLK_SLOW() /* Set slow clock (100k-400k) */
#define FCLK_FAST() /* Set fast clock (depends on the CSD) */
is this all i have to do?
Also with the spi master driver a call to spi_master_init(&AVR32_SPI1) is enough to set up the spi unit ? or i need anything else?
thanks |
|
|
| |
|
|
|
|
|
Posted: Apr 22, 2012 - 12:53 PM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
Hello
Can someone help with the fatfs platform dependent macros?
Code:
/* Port Controls (Platform dependent) */
#define SELECT() spi_selectChip((&AVR32_SPI1), 0)/*MMC CS = L */
#define DESELECT() spi_unselectChip((&AVR32_SPI1), 0)/*MMC CS = H */
#define SOCKPORT /* Socket contact port */
#define SOCKWP /* Write protect switch */
#define SOCKINS /* Card detect switch */
#define FCLK_SLOW() /* Set slow clock (100k-400k) */
#define FCLK_FAST() /* Set fast clock (depends on the CSD) */
What should i put in SOCKPORT , SOCKWP , SOCKINS?
I can see that those macros are used only in disk_timeproc() functions but i dont know what are they.
Here is me read/write operations
Code:
#define xmit_spi(dat) spi_write(&AVR32_SPI1 , (uint16_t)dat )
static
BYTE rcvr_spi (void)
{
uint16_t data;
spi_read(&AVR32_SPI1 , &data );
return (BYTE)data;
}
#define rcvr_spi_m(dst) *(dst) = rcvr_spi()//spi_read(&AVR32_SPI1 , (uint16_t*)dst)
Also the sd card is working with 8-bits and SPI_MODE1(cpha = 0 , cpol = 0).Is this right?
Thanks |
|
|
| |
|
|
|
|
|
Posted: Apr 22, 2012 - 04:58 PM |
|


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
What should i put in SOCKPORT , SOCKWP , SOCKINS?
SOCKPORT is the port (such as PINC) on which the WP and INS input signals occur. The other two are pin numbers. It's going to do things such as:
Code:
if (PINC & (1 << 3)) {
to read the state of the write protect or card insertion signals. |
_________________
|
| |
|
|
|
|
|
Posted: Apr 22, 2012 - 05:45 PM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
But what if dont want to use those signals?I dont want to have write protect or incertion signals.
Im just using the spi interface.If i let those macros undefined or just some random value will i have any problems?
Does anyone have the mmc.c file set up for a uc3 processor?I have tried everything but still not working.
Thanks |
|
|
| |
|
|
|
|
|
Posted: Apr 22, 2012 - 07:47 PM |
|


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
FatFs has a timer interrupt that does a periodic check for Insertion and Write Protection and sets bits in a global status byte that is then used elsewhere. Fake it by saying that the card is always inserted and never write protected. This does mean that (a) you may write to cards that are write protected and (b) you will have to reset after changing cards.
EDIT: In mmc.c this is the code reading those status lines:
Code:
void disk_timerproc (void)
{
BYTE n, s;
n = Timer1; /* 100Hz decrement timer */
if (n) Timer1 = --n;
n = Timer2;
if (n) Timer2 = --n;
s = Stat;
if (SOCKWP) /* Write protected */
s |= STA_PROTECT;
else /* Write enabled */
s &= ~STA_PROTECT;
if (SOCKINS) /* Card inserted */
s &= ~STA_NODISK;
else /* Socket empty */
s |= (STA_NODISK | STA_NOINIT);
Stat = s; /* Update MMC status */
}
Just hard code the result in "Stat". |
_________________
|
| |
|
|
|
|
|
Posted: Apr 22, 2012 - 08:40 PM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
Thanks for the code.
It looks like i have done everything i should do but still nothing.
I set up the spi correct and confirmed it with another spi device.
I have my 10ms interrupt for the disk_timeproc() and i changed every function and macros.
When i debug something strange happen with the spi_read() function.It never actually sends the data,it stayes in the while loop until timeout and gives error.
Code:
spi_status_t spi_read(volatile avr32_spi_t *spi, unsigned short *data)
{
unsigned int timeout = SPI_TIMEOUT;
while ((spi->sr & (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) !=
(AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) {
if (!timeout--) {
return SPI_ERROR_TIMEOUT;
}
}
*data = spi->rdr >> AVR32_SPI_RDR_RD_OFFSET;
return SPI_OK;
}
Any ides why is that? |
|
|
| |
|
|
|
|
|
Posted: Apr 23, 2012 - 09:44 AM |
|


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
Any ides why is that?
'Fraid you've crossed the boundary from FatFs Land to UC3 Ville. That is a territory for which I don't own a map. Hope someone else can help but I wonder if they'll spot the thread here in ASF forum? You may be better off opening a more general thread in one of the UC3 hardware forums. |
_________________
|
| |
|
|
|
|
|
Posted: Apr 23, 2012 - 07:58 PM |
|

Joined: May 11, 2011
Posts: 337
Location: Boston, MA
|
|
I'm not sure why you're having errors, but I just wanted to chime in and let you know that I did get FatFS working on a UC3A3 chip, but I used the native SD 4 bit interface (listed under MCI I think in the datasheet). I wound up with a pretty hacked up FatFS, but that's because I had a somewhat special use case. It works great though, just use some of the ASF examples to figure out how ASF initializes and reads/writes the SD card, and copy those into mmc.c. It's not quite as easy as that sounds, but it's actually not as hard or scary as you would think it might wind up being.
Unfortunately, since I ignored the SPI route, I'm not sure if I can help much there. |
|
|
| |
|
|
|
|
|
Posted: Apr 23, 2012 - 08:04 PM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
I know its not so difficult and i have copied what the fatfs library needs.The fatfs is very clear of what it needs and its not so much.
The thing is that somewhere i have a mistake or a line of code missing but i dont know what else to try.
Anyway thanks for your time |
|
|
| |
|
|
|
|
|
Posted: Apr 29, 2012 - 10:56 AM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
Hello again
I finally got working with some help from some freaks.
I have two general questions about fatfs.
I want to use long file names and i have to provide these two functions
Code:
WCHAR ff_convert (WCHAR, UINT);
WCHAR ff_wtoupper (WCHAR);
I have no idea what OEM is and how i should implement those functions.I believe that almost everyone is using long file names.So how should i implement those two functions?
Also in disk_timeproc() which checks if the card is inserted.Im confused with which pin i should use to chech if the card is in.The pins im using are only what the card needs(spi , vdd , gnd).Can i check with one of those?
Thanks |
|
|
| |
|
|
|
|
|
Posted: Apr 29, 2012 - 11:00 AM |
|


Joined: Mar 27, 2002
Posts: 18757
Location: Lund, Sweden
|
|
|
|
|
|
|
Posted: Apr 29, 2012 - 11:12 AM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
I can see that in fatfs depending on what CODE_PAGE i define it will define a different set of characters or a different OEM (US , greek , arabic...) but i dont know how to write these functions.
I i want to use the US OEM it will define the corresponding table , but what should i do with it? |
|
|
| |
|
|
|
|
|
Posted: Apr 29, 2012 - 05:52 PM |
|


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
ffsample.zip includes cc932_avr.c which shows a sample implementation of the conversion functions (for Japanese). Just adapt that for your own language.
Be warned that if you enabled LFN support you become subject to Microsoft's patents. |
_________________
|
| |
|
|
|
|
|
Posted: Apr 29, 2012 - 11:42 PM |
|

Joined: Aug 19, 2003
Posts: 404
Location: Australia
|
|
|
Quote:
I was wondering how the disk_timeproc() is working in order to detect if the card is there or not.Do i have to use an extra pin ?All im using is the spi interfase vdd and gnd pins for the card.How can i change it to detect the card?
The SDCard holder usually has 2 mechanical switches to indicate 'card is inserted' and 'card is write-protected'.
Usually, those switches are connected so that they pull a input pin to ground., so, check your schematic or find 2 unused GPIO inputs (you will need to provide or enable the pullup on those inputs) and in disk_timerproc() check the state of those pins to set the STA_PROTECT and STA_NODISK flags. |
|
|
| |
|
|
|
|
|
Posted: Apr 30, 2012 - 03:29 PM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
Hello again
Im having a strange problem with fatfs.
I got it working but im having trouble with f_read.
If i want to read a small amount of bytes less than 512 it works well it reads it inside the buffer and then i write it into another file.
But if i want more than 512 bytes it skips some parts of the file and reads from somewhere else.
If i set the _FS_TINY_ to 0 then it cant read anything.
Should i provide anything more if i want to set the _FS_TINY to 0?
Thanks |
|
|
| |
|
|
|
|
|
Posted: May 01, 2012 - 03:03 AM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
Hello
Quote:
The SDCard holder usually has 2 mechanical switches to indicate 'card is inserted' and 'card is write-protected'.
It doesnt have these switches so its not an option to include it.
I found that the problem with fatfs is caused by the external sdram.Im using the sdram to place the heap.
The exact same program runs perfect with the heap in the internal ram but not with the external.
The external is working good until i pass the pointer to f_read.Before that i can use malloc, read/write to that memory and then free it.But when i pass the pointer(from malloc in the external ram) to f_read, it doesnt read consecutively the data from the card into ram.And when i try to free the space the program crashes.
The strange thing is that the sdram works perfect and only crashes when i use the pointer with fatfs.
Any ideas why this would happen?
Thanks |
|
|
| |
|
|
|
|
|
Posted: May 01, 2012 - 09:23 AM |
|


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
| Could it be an interaction between SPI and the external bus signals somehow? |
_________________
|
| |
|
|
|
|
|
Posted: May 01, 2012 - 01:29 PM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
I dont think SPI0 is connected with the external bus.But im not sure either.
Im looking in the datasheet http://www.atmel.com/Images/32072s.pdf page 9
are the gpio functions.
Im using SPI0 with NPCS3 as chip select.I can see that SPI0 and EBI share some pins but for the EBI is gpio function A and for the SPI0 are gpio function B.
Im not sure if i understand the table right but the pins .
Also the SPI0 connected with EBI is using PX port but the one im using SPI0 is in PA port.
Can someone explain this table in the datasheet?
Thanks |
|
|
| |
|
|
|
|
|
Posted: May 02, 2012 - 01:26 AM |
|

Joined: Apr 03, 2010
Posts: 82
|
|
Another strange thing is that even if i dont have data in sdram and only the heap the first adress it gives with malloc is at 0xD0000188 .
I dont know if the memory from 0x00 to 0x188 is kept for internal puproses of the alocator .
But even though the pointer is at 0xD0000188 when i read from the sdcard to that memory some of the data are stored before 0xD0000188.
But this only happens when i use it with fatfs.
Is this normal with the adresses? |
|
|
| |
|
|
|
|
|
Posted: May 02, 2012 - 09:49 AM |
|


Joined: Jul 18, 2005
Posts: 62944
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
If this is GCC then surely the source of the malloc() being used is open so you can study exactly how it's allocation strategy works.
But given that malloc() always allocates continuous blocks from address N onwards then if you see some code writing to N - m (where m!=0) then that code is in error and writing outside the allocated block (or the hardware addressing scheme is "faulty"). |
_________________
|
| |
|
|
|
|
|