FatFS is not working in ISR

Go To Last Post
21 posts / 0 new
Author
Message
#1
  • 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

In what way is it "not working" - is the application freezing, are you getting an error code, or does it return invalid data?

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

Some of the things FatFs does use timeouts policed by the timer interrupt you supply. That mechanism isn't going to work inside an ISR.

Anyway it is extremely poor design to be doing anything like an f_read inside an ISR. Set a flag then service it in main().

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

abcminiuser wrote:
In what way is it "not working" - is the application freezing, are you getting an error code, or does it return invalid data?

- Dean :twisted:


The application freeze.It wont read any data....

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

Can you suggest a method to read a file in mmc and genarate a pwm signal at 44.1kHz..

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

Google "double-buffering". The way this works is that you will keep two 512 byte sector buffers in memory. The code can be using the samples in one to play PCM through the DAC while the other is being read from the card to memory. When the first buffer is exhausted you then switch over and that buffer becomes the play buffer while you start to read into the other one. This is sometimes called "ping-pong" buffering. Do the file reading outside the interrupt context. The main requirement is that you can get one buffer read from the file and into RAM before the contents of the previous have been exhausted. If you cannot do that you cannot play 44.1kHz.

BTW presumably you are using an R-2R or a real DAC? You won't achieve 44.1kHz using a timer PWM to do DAC.

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

Thank you clawson for your reply.can you please tell me what is the low fuse configuration to achieve 62.5kHz pwm frequency?

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

What AVR, what clock speed? But to be honest you'd be far better off putting a real DAC in the design or as a minimum resistors in an R-2R ladder. One option would be to use Xmega - they have DAC built in. There's also so AT90PWM models of AVR that have DAC too.

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

clawson wrote:
What AVR, what clock speed?

I am using atmega128 and crystal is 16M..
can you please tell me what is the low fuse configuration to achieve 62.5kHz pwm frequency?

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

16MHz / 256 = 62.5kHz so I guess it is "do-able" after all.

Low fuse looks like being 0xDE with high at 0x89 (it includes the CKOPT you need to enable). See: http://www.engbedded.com/fusecalc

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

Wow ... now I got 62.5kHz

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

How to get the accurate 44.1kHz interrupt?

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

Set a timer in CTC mode to give you an interrupt every 362 cycles. This will give you an ISR at about 44.2 Khz. If it has to be more accurate, then get a special crystal or possibly you can pull the crystal a little bit with a trim cap.

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

I'm confused. First, OP asked for 62.5 and got that. Now, asking for 44.1... Wikipedia article doesn't mention 62.5 for common audio rate: http://en.wikipedia.org/wiki/Sam...

I'm certainly not very experienced at this sound stuff. If ElmChan did it with a Tiny85 running off the internal oscillator... http://elm-chan.org/works/sd8p/r...

But, then again, the AVR models used have the PLL/high-speed PWM.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

If I read it correctly, the 62.5 Khz. is a PWM output & the 44.1 is an ISR. I am probably confused to!

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

Can't I use 62.5Khz as a PWM output and 44.1kHz ISR to feed the wave data to pwm(OCR0 register) I never made a wav player before...

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

Can't I use 62.5Khz as a PWM output and 44.1kHz ISR to feed the wave data to pwm(OCR0 register) I never made a wav player before...

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

Milprasad wrote:
Can't I use 62.5Khz as a PWM output and 44.1kHz ISR to feed the wave data to pwm(OCR0 register) I never made a wav player before...

Why on earth would you want to do that?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

Brian Fairchild wrote:

Why on earth would you want to do that?

I asked my university senior as he explained me I make my mind to do in this way..So then how do you suggest to generate pwm audio?

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

What I'm saying is that to run your output sample rate at a different rate to your input sample rate is asking for trouble.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

To give you an idea. On an 8MHz mega16 I ran 16bit timer 1 at 31.25kHz and loaded a new audio sample on every 4th overflow (on overflows 1 and 2 I was doing two parts of an ADPCM decode). The audio was recorded on a PC at 22kHz but I then downsampled it to 7.8kHz which is 1/4 of 31.25kHz.

Trying to do 44.1kHz may be a bit ambitious for a first try at this stuff! Stick around the 8kHz mark and the consider stretching things later when you have that working.

If you want to play 44.1kHz add a DAC. (I think I've now typed that same advice in 3 threads you started - hopefully you will soon get the message!).