configuring petit fatfs diskio.c

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all....

I am doing a project on wave player.I am using atmega128 as the MCU. I don't know how to edit petit fatfs diskio.c to configure to run in the SPI mode.

Can anybody help regarding this matter or give some sample code...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I don't know how to edit petit fatfs diskio.c to configure to run in the SPI mode.

One of the points of Petit is that it does not use SPI. It either uses USI or just bit-bangs the SPI interface on GPIO pins. This is because it's really designed for the small micros like Tiny AVRs that may not have SPI. However it does a great job of bit-bang so unless you have a strong reason to change it I'd just let it do bit-bang SPI anyway.

See asmfunc.S which has:

#define	DDR_CS	_SFR_IO_ADDR(DDRB), 0	// MMC CS pin (DDR, PORT)
#define	PORT_CS	_SFR_IO_ADDR(PORTB), 0

#define	DDR_CK	_SFR_IO_ADDR(DDRB), 1	// MMC SCLK pin (DDR, PORT)
#define	PORT_CK	_SFR_IO_ADDR(PORTB), 1

#define	DDR_DI	_SFR_IO_ADDR(DDRB), 2	// MMC DI pin (DDR, PORT)
#define	PORT_DI	_SFR_IO_ADDR(PORTB), 2

#define	PIN_DO	_SFR_IO_ADDR(PINB), 3	// MMC DO pin (PIN, PORT)
#define	PORT_DO	_SFR_IO_ADDR(PORTB), 3


.global init_spi
.func init_spi
init_spi:
	sbi	DDR_CS		; CS: output
	sbi	DDR_DI		; DI: output
	sbi	DDR_CK		; SCLK: output
	sbi	PORT_DO		; DO: pull-up
	ret
.endfunc

;---------------------------------------------------------------------------;
; Receive a byte
;
; BYTE rcv_spi (void);

.global rcv_spi
.func rcv_spi
rcv_spi:
	ldi	r24, 0xFF	; Send 0xFF to receive data
	; Goto next function
.endfunc



;---------------------------------------------------------------------------;
; Transmit a byte
;
; void xmit_spi (BYTE);

.global xmit_spi
.func xmit_spi
xmit_spi:
	ldi	r25, 8
1:	sbrc	r24, 7		; DI = Bit to sent
	sbi	PORT_DI		; 
	sbrs	r24, 7		; 
	cbi	PORT_DI		; /
	lsl	r24		; Get DO from MMC
	sbic	PIN_DO		; 
	inc	r24		; /
	sbi	PORT_CK		; A positive pulse to SCLK
	cbi	PORT_CK		; /
	dec	r25		; Repeat 8 times
	brne	1b		; /
	ret
.endfunc

Use the #defines at the top to change the PORT/bits you want the signals to be on. The actual implementation itself is very clever.

I suppose you could replace those routines with C functions to configure then use SPCR/SPDR/SPCR but I don't really see much point.

BTW why use "Petit" when you are using a micro with 128K flash or are you trying to fit this into the bootloader space?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you clawson for your valuable reply.I am trying to use petit because I had run several trials using "FatFS" file system.I get to know that "Petit" has small in code size.So I thought that petit could be easy to configure.That's why I try to do my project using "Petit" .If is is not a good choice can you please tell me how to configure this in "FatFS".I am using "Atmel studio 6"(I don't know much about avr asm).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I get to know that "Petit" has small in code size

But why is that important in a 128K micro. Petit takes about 2-4K while FatFs takes about 10-20K. Out of 128K that still isn't a lot. What's more FatFs is much more efficient in operation as it buffers whole sectors to 512 byte arrays in SRAM.

To configure either you just edit the options in the .h file. In the case of FatFs that is ffconf.h and for Petit it is just pff.h. Both headers have either _USE* or _FS_* symbols. Define those things you want turned on as '1' and those you don't want as '0'.

As you are using such a large micro you might as well use FatFs with virtually everything turned on. One thing to be sure of whichever you use is to:

#define _CODE_PAGE	1

otherwise you end up dragging in huge amounts of code-page conversion tables which you probably don't want/need unless you really want to use LFN with foreign language symbols in the filenames.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

wow.... I have got zero compile errors.But it wont run in the actual hardware.What do you suggest to do?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Petit FatFS project with zero compile errors.But it won't run in the actual hardware.What do you suggest to do?

Here I'm attaching the Atmel Studio6 project.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

it wont run

That's a very vague description. WHAT will "not run" and how are you determining that?

(in fact on an AVR, apart from the SLEEP opcode, it's almost impossible to stop the thing running something!).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I simulate this in proteus and the actual hardware.And I use proteus spi debugger.It only shows that cs pin change the state to enable..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you please Send me a "FatFs" or "Petit FatFs" working code for atmega128. this would be really useful for me.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Chan's ffsample.zip already contains code for mega128. Just use that. But don't waste a lot of time using Proteus with this. You won't know if bugs are yours or a failure of the simulation. Far better to use a real SD/MMC attached to a real AVR and if you want to know what's going on inside use JTAG.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried to compile Chan's ffsample.zip for a long time.Ok it was compiled successfully. But it is not running in the actual hardware(I create a file using FatFS but it was not in the SD card).

Can u please give me some working sample code for atmega64(Hello world FatFS code)....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.. I finally run Fatfs code in actual hardware successfully. But I got another problem.I am going to build a simple wav player.When I try to read wav data and write to ocr1a register I note that the fread function not working in the ISR....

ISR(TIMER0_OVF_vect)
{
//f_write(&logFile, "New log opened a!\n", 16, &bytesWritten);
f_read(&logFile,a,1,&bytesWritten);//f_read is not working here
OCR1A=a[0];
}

How can I overcome this problem ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't cross post, this has been answered in your other thread. Bottom line: do not perform FatFs functions from within an ISR.