Make bufadvance rely on bufseek instead of duplicating code
[Rockbox.git] / firmware / mp3_playback.c
blobc5c747ac2c2149d505dd2c14041f5a090f1a69f9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Code that has been in mpeg.c before, now creating an encapsulated play
11 * data module, to be used by other sources than file playback as well.
13 * Copyright (C) 2004 by Linus Nielsen Feltzing
15 * All files in this archive are subject to the GNU General Public License.
16 * See the file COPYING in the source tree root for full license agreement.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include <stdbool.h>
23 #include "config.h"
24 #include "debug.h"
25 #include "panic.h"
26 #include <kernel.h>
27 #include "mpeg.h" /* ToDo: remove crosslinks */
28 #include "mp3_playback.h"
29 #include "sound.h"
30 #ifndef SIMULATOR
31 #include "i2c.h"
32 #include "mas.h"
33 #include "dac.h"
34 #include "system.h"
35 #endif
36 #include "audiohw.h"
38 /* hacking into mpeg.c, recording is still there */
39 #if CONFIG_CODEC == MAS3587F
40 enum
42 MPEG_DECODER,
43 MPEG_ENCODER
44 } mpeg_mode;
45 #endif /* #ifdef MAS3587F */
47 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)) && !defined(SIMULATOR)
48 extern unsigned long shadow_io_control_main;
49 extern unsigned shadow_codec_reg0;
50 #endif
52 /**** globals ****/
54 /* own version, independent of mpeg.c */
55 static bool paused; /* playback is paused */
56 static bool playing; /* We are playing an MP3 stream */
58 #ifndef SIMULATOR
59 /* for measuring the play time */
60 static long playstart_tick;
61 static long cumulative_ticks;
63 /* the registered callback function to ask for more mp3 data */
64 static void (*callback_for_more)(unsigned char**, size_t*);
65 #endif /* #ifndef SIMULATOR */
67 /* list of tracks in memory */
68 #define MAX_ID3_TAGS (1<<4) /* Must be power of 2 */
69 #define MAX_ID3_TAGS_MASK (MAX_ID3_TAGS - 1)
71 #ifndef SIMULATOR
72 bool audio_is_initialized = false;
73 #endif
75 /* FIX: this code pretty much assumes a MAS */
77 #ifndef SIMULATOR
79 unsigned long mas_version_code;
81 #if CONFIG_CODEC == MAS3507D
82 static void mas_poll_start(void)
84 unsigned int count;
86 count = 9 * FREQ / 10000 / 8; /* 0.9 ms */
88 /* We are using timer 1 */
90 TSTR &= ~0x02; /* Stop the timer */
91 TSNC &= ~0x02; /* No synchronization */
92 TMDR &= ~0x02; /* Operate normally */
94 TCNT1 = 0; /* Start counting at 0 */
95 GRA1 = count;
96 TCR1 = 0x23; /* Clear at GRA match, sysclock/8 */
98 /* Enable interrupt on level 5 */
99 IPRC = (IPRC & ~0x000f) | 0x0005;
101 TSR1 &= ~0x02;
102 TIER1 = 0xf9; /* Enable GRA match interrupt */
104 TSTR |= 0x02; /* Start timer 1 */
106 #elif CONFIG_CODEC != SWCODEC
107 static void postpone_dma_tick(void)
109 unsigned int count;
111 count = 8 * FREQ / 10000 / 8; /* 0.8 ms */
113 /* We are using timer 1 */
115 TSTR &= ~0x02; /* Stop the timer */
116 TSNC &= ~0x02; /* No synchronization */
117 TMDR &= ~0x02; /* Operate normally */
119 TCNT1 = 0; /* Start counting at 0 */
120 GRA1 = count;
121 TCR1 = 0x23; /* Clear at GRA match, sysclock/8 */
123 /* Enable interrupt on level 5 */
124 IPRC = (IPRC & ~0x000f) | 0x0005;
126 TSR1 &= ~0x02;
127 TIER1 = 0xf9; /* Enable GRA match interrupt */
129 TSTR |= 0x02; /* Start timer 1 */
131 #endif
134 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
135 void demand_irq_enable(bool on)
137 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
139 if(on)
141 IPRA = (IPRA & 0xfff0) | 0x000b;
142 ICR &= ~0x0010; /* IRQ3 level sensitive */
144 else
145 IPRA &= 0xfff0;
147 set_irq_level(oldlevel);
149 #endif /* #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) */
152 void play_tick(void)
154 if(playing && !paused)
156 /* Start DMA if it is disabled and the DEMAND pin is high */
157 if(!(SCR0 & 0x80) && (PBDR & 0x4000))
159 SCR0 |= 0x80;
162 playback_tick(); /* dirty call to mpeg.c */
166 void DEI3(void) __attribute__((interrupt_handler));
167 void DEI3(void)
169 unsigned char* start;
170 size_t size = 0;
172 if (callback_for_more != NULL)
174 callback_for_more(&start, &size);
177 if (size > 0)
179 DTCR3 = size & 0xffff;
180 SAR3 = (unsigned int) start;
182 else
184 CHCR3 &= ~0x0001; /* Disable the DMA interrupt */
187 CHCR3 &= ~0x0002; /* Clear DMA interrupt */
190 void IMIA1(void) __attribute__((interrupt_handler));
191 void IMIA1(void) /* Timer 1 interrupt */
193 if(playing)
194 play_tick();
195 TSR1 &= ~0x01;
196 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
197 /* Disable interrupt */
198 IPRC &= ~0x000f;
199 #endif
202 void IRQ6(void) __attribute__((interrupt_handler));
203 void IRQ6(void) /* PB14: MAS stop demand IRQ */
205 SCR0 &= ~0x80;
208 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
209 void IRQ3(void) __attribute__((interrupt_handler));
210 void IRQ3(void) /* PA15: MAS demand IRQ */
212 /* Begin with setting the IRQ to edge sensitive */
213 ICR |= 0x0010;
215 #if CONFIG_CODEC == MAS3587F
216 if(mpeg_mode == MPEG_ENCODER)
217 rec_tick();
218 else
219 #endif
220 postpone_dma_tick();
222 /* Workaround for sh-elf-gcc 3.3.x bug with -O2 or -Os and ISRs
223 * (invalid cross-jump optimisation) */
224 asm volatile ("");
226 #endif /* #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) */
228 static void setup_sci0(void)
230 /* PB15 is I/O, PB14 is IRQ6, PB12 is SCK0, PB9 is TxD0 */
231 PBCR1 = (PBCR1 & 0x0cff) | 0x1208;
233 /* Set PB12 to output */
234 or_b(0x10, &PBIORH);
236 /* Disable serial port */
237 SCR0 = 0x00;
239 /* Synchronous, no prescale */
240 SMR0 = 0x80;
242 /* Set baudrate 1Mbit/s */
243 BRR0 = 0x02;
245 /* use SCK as serial clock output */
246 SCR0 = 0x01;
248 /* Clear FER and PER */
249 SSR0 &= 0xe7;
251 /* Set interrupt ITU2 and SCI0 priority to 0 */
252 IPRD &= 0x0ff0;
254 /* set PB15 and PB14 to inputs */
255 and_b(~0x80, &PBIORH);
256 and_b(~0x40, &PBIORH);
258 /* Enable End of DMA interrupt at prio 8 */
259 IPRC = (IPRC & 0xf0ff) | 0x0800;
261 /* Enable Tx (only!) */
262 SCR0 |= 0x20;
265 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
266 static void init_playback(void)
268 unsigned long val;
269 int rc;
271 mp3_play_pause(false);
273 mas_reset();
275 /* Enable the audio CODEC and the DSP core, max analog voltage range */
276 rc = mas_direct_config_write(MAS_CONTROL, 0x8c00);
277 if(rc < 0)
278 panicf("mas_ctrl_w: %d", rc);
280 /* Stop the current application */
281 val = 0;
282 mas_writemem(MAS_BANK_D0, MAS_D0_APP_SELECT, &val, 1);
285 mas_readmem(MAS_BANK_D0, MAS_D0_APP_RUNNING, &val, 1);
286 } while(val);
288 /* Enable the D/A Converter */
289 shadow_codec_reg0 = 0x0001;
290 mas_codec_writereg(0x0, shadow_codec_reg0);
292 /* ADC scale 0%, DSP scale 100% */
293 mas_codec_writereg(6, 0x0000);
294 mas_codec_writereg(7, 0x4000);
296 #ifdef HAVE_SPDIF_OUT
297 val = 0x09; /* Disable SDO and SDI, low impedance S/PDIF outputs */
298 #else
299 val = 0x2d; /* Disable SDO and SDI, disable S/PDIF output */
300 #endif
301 mas_writemem(MAS_BANK_D0, MAS_D0_INTERFACE_CONTROL, &val, 1);
303 /* Set Demand mode and validate all settings */
304 shadow_io_control_main = 0x25;
305 mas_writemem(MAS_BANK_D0, MAS_D0_IO_CONTROL_MAIN, &shadow_io_control_main, 1);
307 /* Start the Layer2/3 decoder applications */
308 val = 0x0c;
309 mas_writemem(MAS_BANK_D0, MAS_D0_APP_SELECT, &val, 1);
312 mas_readmem(MAS_BANK_D0, MAS_D0_APP_RUNNING, &val, 1);
313 } while((val & 0x0c) != 0x0c);
315 #if CONFIG_CODEC == MAS3587F
316 mpeg_mode = MPEG_DECODER;
317 #endif
319 /* set IRQ6 to edge detect */
320 ICR |= 0x02;
322 /* set IRQ6 prio 8 */
323 IPRB = ( IPRB & 0xff0f ) | 0x0080;
325 DEBUGF("MAS Decoding application started\n");
327 #endif /* #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) */
328 #endif /* SIMULATOR */
330 void mp3_init(int volume, int bass, int treble, int balance, int loudness,
331 int avc, int channel_config, int stereo_width,
332 int mdb_strength, int mdb_harmonics,
333 int mdb_center, int mdb_shape, bool mdb_enable,
334 bool superbass)
336 #ifdef SIMULATOR
337 (void)volume;
338 (void)bass;
339 (void)treble;
340 (void)balance;
341 (void)loudness;
342 (void)avc;
343 (void)channel_config;
344 (void)stereo_width;
345 (void)mdb_strength;
346 (void)mdb_harmonics;
347 (void)mdb_center;
348 (void)mdb_shape;
349 (void)mdb_enable;
350 (void)superbass;
351 #else
352 #if CONFIG_CODEC == MAS3507D
353 unsigned long val;
354 (void)loudness;
355 (void)avc;
356 (void)mdb_strength;
357 (void)mdb_harmonics;
358 (void)mdb_center;
359 (void)mdb_shape;
360 (void)mdb_enable;
361 (void)superbass;
362 #endif
364 setup_sci0();
366 #ifdef HAVE_MAS_SIBI_CONTROL
367 and_b(~0x01, &PBDRH); /* drive SIBI low */
368 or_b(0x01, &PBIORH); /* output for PB8 */
369 #endif
371 #if CONFIG_CODEC == MAS3507D
372 mas_reset();
373 #elif CONFIG_CODEC == MAS3587F
374 or_b(0x08, &PAIORH); /* output for /PR */
375 init_playback();
377 mas_version_code = mas_readver();
378 DEBUGF("MAS3587 derivate %d, version %c%d\n",
379 (mas_version_code & 0xf000) >> 12,
380 'A' + ((mas_version_code & 0x0f00) >> 8), mas_version_code & 0xff);
381 #elif CONFIG_CODEC == MAS3539F
382 or_b(0x08, &PAIORH); /* output for /PR */
383 init_playback();
385 mas_version_code = mas_readver();
386 DEBUGF("MAS3539 derivate %d, version %c%d\n",
387 (mas_version_code & 0xf000) >> 12,
388 'A' + ((mas_version_code & 0x0f00) >> 8), mas_version_code & 0xff);
389 #endif
391 #ifdef HAVE_DAC3550A
392 dac_init();
393 #endif
395 #if CONFIG_CODEC == MAS3507D
396 /* set IRQ6 to edge detect */
397 ICR |= 0x02;
399 /* set IRQ6 prio 8 */
400 IPRB = ( IPRB & 0xff0f ) | 0x0080;
402 mas_readmem(MAS_BANK_D1, 0xff7, &mas_version_code, 1);
404 mas_writereg(0x3b, 0x20); /* Don't ask why. The data sheet doesn't say */
405 mas_run(1);
406 sleep(HZ);
408 /* Clear the upper 12 bits of the 32-bit samples */
409 mas_writereg(0xc5, 0);
410 mas_writereg(0xc6, 0);
412 /* We need to set the PLL for a 14.31818MHz crystal */
413 if(mas_version_code == 0x0601) /* Version F10? */
415 val = 0x5d9d0;
416 mas_writemem(MAS_BANK_D0, 0x32d, &val, 1);
417 val = 0xfffceceb;
418 mas_writemem(MAS_BANK_D0, 0x32e, &val, 1);
419 val = 0x0;
420 mas_writemem(MAS_BANK_D0, 0x32f, &val, 1);
421 mas_run(0x475);
423 else
425 val = 0x5d9d0;
426 mas_writemem(MAS_BANK_D0, 0x36d, &val, 1);
427 val = 0xfffceceb;
428 mas_writemem(MAS_BANK_D0, 0x36e, &val, 1);
429 val = 0x0;
430 mas_writemem(MAS_BANK_D0, 0x36f, &val, 1);
431 mas_run(0xfcb);
434 #endif
436 #if CONFIG_CODEC == MAS3507D
437 mas_poll_start();
439 mas_writereg(MAS_REG_KPRESCALE, 0xe9400);
440 dac_enable(true);
441 #endif
443 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
444 ICR &= ~0x0010; /* IRQ3 level sensitive */
445 PACR1 = (PACR1 & 0x3fff) | 0x4000; /* PA15 is IRQ3 */
446 #endif
448 /* Must be done before calling sound_set() */
449 audio_is_initialized = true;
451 sound_set(SOUND_BASS, bass);
452 sound_set(SOUND_TREBLE, treble);
453 sound_set(SOUND_BALANCE, balance);
454 sound_set(SOUND_VOLUME, volume);
455 sound_set(SOUND_CHANNELS, channel_config);
456 sound_set(SOUND_STEREO_WIDTH, stereo_width);
458 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
459 sound_set(SOUND_LOUDNESS, loudness);
460 sound_set(SOUND_AVC, avc);
461 sound_set(SOUND_MDB_STRENGTH, mdb_strength);
462 sound_set(SOUND_MDB_HARMONICS, mdb_harmonics);
463 sound_set(SOUND_MDB_CENTER, mdb_center);
464 sound_set(SOUND_MDB_SHAPE, mdb_shape);
465 sound_set(SOUND_MDB_ENABLE, mdb_enable);
466 sound_set(SOUND_SUPERBASS, superbass);
467 #endif
468 #endif /* !SIMULATOR */
470 playing = false;
471 paused = true;
474 void mp3_shutdown(void)
476 #ifndef SIMULATOR
477 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
478 unsigned long val = 1;
479 mas_writemem(MAS_BANK_D0, MAS_D0_SOFT_MUTE, &val, 1); /* Mute */
480 #endif
482 #if CONFIG_CODEC == MAS3507D
483 dac_volume(0, 0, false);
484 #endif
486 #endif
489 /* new functions, to be exported to plugin API */
491 #ifndef SIMULATOR
493 void mp3_play_init(void)
495 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
496 init_playback();
497 #endif
498 playing = false;
499 paused = true;
500 callback_for_more = NULL;
501 mp3_reset_playtime();
504 void mp3_play_data(const unsigned char* start, int size,
505 void (*get_more)(unsigned char** start, size_t* size) /* callback fn */
508 /* init DMA */
509 DAR3 = 0x5FFFEC3;
510 CHCR3 &= ~0x0002; /* Clear interrupt */
511 CHCR3 = 0x1504; /* Single address destination, TXI0, IE=1 */
512 DMAOR = 0x0001; /* Enable DMA */
514 callback_for_more = get_more;
516 SAR3 = (unsigned int)start;
517 DTCR3 = size & 0xffff;
519 playing = true;
520 paused = true;
522 CHCR3 |= 0x0001; /* Enable DMA IRQ */
524 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
525 demand_irq_enable(true);
526 #endif
529 void mp3_play_pause(bool play)
531 if (paused && play)
532 { /* resume playback */
533 SCR0 |= 0x80;
534 paused = false;
535 playstart_tick = current_tick;
537 else if (!paused && !play)
538 { /* stop playback */
539 SCR0 &= 0x7f;
540 paused = true;
541 cumulative_ticks += current_tick - playstart_tick;
545 bool mp3_pause_done(void)
547 unsigned long frame_count;
549 if (!paused)
550 return false;
552 mas_readmem(MAS_BANK_D0, MAS_D0_MPEG_FRAME_COUNT, &frame_count, 1);
553 /* This works because the frame counter never wraps,
554 * i.e. zero always means lost sync. */
555 return frame_count == 0;
558 void mp3_play_stop(void)
560 playing = false;
561 mp3_play_pause(false);
562 CHCR3 &= ~0x0001; /* Disable the DMA interrupt */
563 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
564 demand_irq_enable(false);
565 #endif
568 long mp3_get_playtime(void)
570 if (paused)
571 return cumulative_ticks;
572 else
573 return cumulative_ticks + current_tick - playstart_tick;
576 void mp3_reset_playtime(void)
578 cumulative_ticks = 0;
579 playstart_tick = current_tick;
582 bool mp3_is_playing(void)
584 return playing;
588 /* returns the next byte position which would be transferred */
589 unsigned char* mp3_get_pos(void)
591 return (unsigned char*)SAR3;
595 #endif /* #ifndef SIMULATOR */