clean up some debugging output.
[Rockbox.git] / firmware / mp3_playback.c
blob7c561e475f158affdedb05363693d95edb8d43bd
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 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include <stdbool.h>
25 #include "config.h"
26 #include "debug.h"
27 #include "panic.h"
28 #include <kernel.h>
29 #include "mpeg.h" /* ToDo: remove crosslinks */
30 #include "mp3_playback.h"
31 #include "sound.h"
32 #ifndef SIMULATOR
33 #include "i2c.h"
34 #include "mas.h"
35 #include "dac.h"
36 #include "system.h"
37 #endif
38 #include "audiohw.h"
40 /* hacking into mpeg.c, recording is still there */
41 #if CONFIG_CODEC == MAS3587F
42 enum
44 MPEG_DECODER,
45 MPEG_ENCODER
46 } mpeg_mode;
47 #endif /* #ifdef MAS3587F */
49 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)) && !defined(SIMULATOR)
50 extern unsigned long shadow_io_control_main;
51 extern unsigned shadow_codec_reg0;
52 #endif
54 /**** globals ****/
56 /* own version, independent of mpeg.c */
57 static bool paused; /* playback is paused */
58 static bool playing; /* We are playing an MP3 stream */
60 #ifndef SIMULATOR
61 /* for measuring the play time */
62 static long playstart_tick;
63 static long cumulative_ticks;
65 /* the registered callback function to ask for more mp3 data */
66 static void (*callback_for_more)(unsigned char**, size_t*);
67 #endif /* #ifndef SIMULATOR */
69 /* list of tracks in memory */
70 #define MAX_ID3_TAGS (1<<4) /* Must be power of 2 */
71 #define MAX_ID3_TAGS_MASK (MAX_ID3_TAGS - 1)
73 #ifndef SIMULATOR
74 bool audio_is_initialized = false;
75 #endif
77 /* FIX: this code pretty much assumes a MAS */
79 #ifndef SIMULATOR
81 unsigned long mas_version_code;
83 #if CONFIG_CODEC == MAS3507D
84 static void mas_poll_start(void)
86 unsigned int count;
88 count = 9 * FREQ / 10000 / 8; /* 0.9 ms */
90 /* We are using timer 1 */
92 TSTR &= ~0x02; /* Stop the timer */
93 TSNC &= ~0x02; /* No synchronization */
94 TMDR &= ~0x02; /* Operate normally */
96 TCNT1 = 0; /* Start counting at 0 */
97 GRA1 = count;
98 TCR1 = 0x23; /* Clear at GRA match, sysclock/8 */
100 /* Enable interrupt on level 5 */
101 IPRC = (IPRC & ~0x000f) | 0x0005;
103 TSR1 &= ~0x02;
104 TIER1 = 0xf9; /* Enable GRA match interrupt */
106 TSTR |= 0x02; /* Start timer 1 */
108 #elif CONFIG_CODEC != SWCODEC
109 static void postpone_dma_tick(void)
111 unsigned int count;
113 count = 8 * FREQ / 10000 / 8; /* 0.8 ms */
115 /* We are using timer 1 */
117 TSTR &= ~0x02; /* Stop the timer */
118 TSNC &= ~0x02; /* No synchronization */
119 TMDR &= ~0x02; /* Operate normally */
121 TCNT1 = 0; /* Start counting at 0 */
122 GRA1 = count;
123 TCR1 = 0x23; /* Clear at GRA match, sysclock/8 */
125 /* Enable interrupt on level 5 */
126 IPRC = (IPRC & ~0x000f) | 0x0005;
128 TSR1 &= ~0x02;
129 TIER1 = 0xf9; /* Enable GRA match interrupt */
131 TSTR |= 0x02; /* Start timer 1 */
133 #endif
136 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
137 void demand_irq_enable(bool on)
139 int oldlevel = disable_irq_save();
141 if(on)
143 IPRA = (IPRA & 0xfff0) | 0x000b;
144 ICR &= ~0x0010; /* IRQ3 level sensitive */
146 else
147 IPRA &= 0xfff0;
149 restore_irq(oldlevel);
151 #endif /* #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) */
154 void play_tick(void)
156 if(playing && !paused)
158 /* Start DMA if it is disabled and the DEMAND pin is high */
159 if(!(SCR0 & 0x80) && (PBDR & 0x4000))
161 SCR0 |= 0x80;
164 playback_tick(); /* dirty call to mpeg.c */
168 void DEI3(void) __attribute__((interrupt_handler));
169 void DEI3(void)
171 unsigned char* start;
172 size_t size = 0;
174 if (callback_for_more != NULL)
176 callback_for_more(&start, &size);
179 if (size > 0)
181 DTCR3 = size & 0xffff;
182 SAR3 = (unsigned int) start;
184 else
186 CHCR3 &= ~0x0001; /* Disable the DMA interrupt */
189 CHCR3 &= ~0x0002; /* Clear DMA interrupt */
192 void IMIA1(void) __attribute__((interrupt_handler));
193 void IMIA1(void) /* Timer 1 interrupt */
195 if(playing)
196 play_tick();
197 TSR1 &= ~0x01;
198 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
199 /* Disable interrupt */
200 IPRC &= ~0x000f;
201 #endif
204 void IRQ6(void) __attribute__((interrupt_handler));
205 void IRQ6(void) /* PB14: MAS stop demand IRQ */
207 SCR0 &= ~0x80;
210 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
211 void IRQ3(void) __attribute__((interrupt_handler));
212 void IRQ3(void) /* PA15: MAS demand IRQ */
214 /* Begin with setting the IRQ to edge sensitive */
215 ICR |= 0x0010;
217 #if CONFIG_CODEC == MAS3587F
218 if(mpeg_mode == MPEG_ENCODER)
219 rec_tick();
220 else
221 #endif
222 postpone_dma_tick();
224 /* Workaround for sh-elf-gcc 3.3.x bug with -O2 or -Os and ISRs
225 * (invalid cross-jump optimisation) */
226 asm volatile ("");
228 #endif /* #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) */
230 static void setup_sci0(void)
232 /* PB15 is I/O, PB14 is IRQ6, PB12 is SCK0, PB9 is TxD0 */
233 PBCR1 = (PBCR1 & 0x0cff) | 0x1208;
235 /* Set PB12 to output */
236 or_b(0x10, &PBIORH);
238 /* Disable serial port */
239 SCR0 = 0x00;
241 /* Synchronous, no prescale */
242 SMR0 = 0x80;
244 /* Set baudrate 1Mbit/s */
245 BRR0 = 0x02;
247 /* use SCK as serial clock output */
248 SCR0 = 0x01;
250 /* Clear FER and PER */
251 SSR0 &= 0xe7;
253 /* Set interrupt ITU2 and SCI0 priority to 0 */
254 IPRD &= 0x0ff0;
256 /* set PB15 and PB14 to inputs */
257 and_b(~0x80, &PBIORH);
258 and_b(~0x40, &PBIORH);
260 /* Enable End of DMA interrupt at prio 8 */
261 IPRC = (IPRC & 0xf0ff) | 0x0800;
263 /* Enable Tx (only!) */
264 SCR0 |= 0x20;
267 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
268 static void init_playback(void)
270 unsigned long val;
271 int rc;
273 mp3_play_pause(false);
275 mas_reset();
277 /* Enable the audio CODEC and the DSP core, max analog voltage range */
278 rc = mas_direct_config_write(MAS_CONTROL, 0x8c00);
279 if(rc < 0)
280 panicf("mas_ctrl_w: %d", rc);
282 /* Stop the current application */
283 val = 0;
284 mas_writemem(MAS_BANK_D0, MAS_D0_APP_SELECT, &val, 1);
287 mas_readmem(MAS_BANK_D0, MAS_D0_APP_RUNNING, &val, 1);
288 } while(val);
290 /* Enable the D/A Converter */
291 shadow_codec_reg0 = 0x0001;
292 mas_codec_writereg(0x0, shadow_codec_reg0);
294 /* ADC scale 0%, DSP scale 100% */
295 mas_codec_writereg(6, 0x0000);
296 mas_codec_writereg(7, 0x4000);
298 #ifdef HAVE_SPDIF_OUT
299 val = 0x09; /* Disable SDO and SDI, low impedance S/PDIF outputs */
300 #else
301 val = 0x2d; /* Disable SDO and SDI, disable S/PDIF output */
302 #endif
303 mas_writemem(MAS_BANK_D0, MAS_D0_INTERFACE_CONTROL, &val, 1);
305 /* Set Demand mode and validate all settings */
306 shadow_io_control_main = 0x25;
307 mas_writemem(MAS_BANK_D0, MAS_D0_IO_CONTROL_MAIN, &shadow_io_control_main, 1);
309 /* Start the Layer2/3 decoder applications */
310 val = 0x0c;
311 mas_writemem(MAS_BANK_D0, MAS_D0_APP_SELECT, &val, 1);
314 mas_readmem(MAS_BANK_D0, MAS_D0_APP_RUNNING, &val, 1);
315 } while((val & 0x0c) != 0x0c);
317 #if CONFIG_CODEC == MAS3587F
318 mpeg_mode = MPEG_DECODER;
319 #endif
321 /* set IRQ6 to edge detect */
322 ICR |= 0x02;
324 /* set IRQ6 prio 8 */
325 IPRB = ( IPRB & 0xff0f ) | 0x0080;
327 DEBUGF("MAS Decoding application started\n");
329 #endif /* #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) */
330 #endif /* SIMULATOR */
332 void mp3_init(int volume, int bass, int treble, int balance, int loudness,
333 int avc, int channel_config, int stereo_width,
334 int mdb_strength, int mdb_harmonics,
335 int mdb_center, int mdb_shape, bool mdb_enable,
336 bool superbass)
338 #ifdef SIMULATOR
339 (void)volume;
340 (void)bass;
341 (void)treble;
342 (void)balance;
343 (void)loudness;
344 (void)avc;
345 (void)channel_config;
346 (void)stereo_width;
347 (void)mdb_strength;
348 (void)mdb_harmonics;
349 (void)mdb_center;
350 (void)mdb_shape;
351 (void)mdb_enable;
352 (void)superbass;
353 #else
354 #if CONFIG_CODEC == MAS3507D
355 unsigned long val;
356 (void)loudness;
357 (void)avc;
358 (void)mdb_strength;
359 (void)mdb_harmonics;
360 (void)mdb_center;
361 (void)mdb_shape;
362 (void)mdb_enable;
363 (void)superbass;
364 #endif
366 setup_sci0();
368 #ifdef HAVE_MAS_SIBI_CONTROL
369 and_b(~0x01, &PBDRH); /* drive SIBI low */
370 or_b(0x01, &PBIORH); /* output for PB8 */
371 #endif
373 #if CONFIG_CODEC == MAS3507D
374 mas_reset();
375 #elif CONFIG_CODEC == MAS3587F
376 or_b(0x08, &PAIORH); /* output for /PR */
377 init_playback();
379 mas_version_code = mas_readver();
380 DEBUGF("MAS3587 derivate %d, version %c%d\n",
381 (mas_version_code & 0xf000) >> 12,
382 'A' + ((mas_version_code & 0x0f00) >> 8), mas_version_code & 0xff);
383 #elif CONFIG_CODEC == MAS3539F
384 or_b(0x08, &PAIORH); /* output for /PR */
385 init_playback();
387 mas_version_code = mas_readver();
388 DEBUGF("MAS3539 derivate %d, version %c%d\n",
389 (mas_version_code & 0xf000) >> 12,
390 'A' + ((mas_version_code & 0x0f00) >> 8), mas_version_code & 0xff);
391 #endif
393 #ifdef HAVE_DAC3550A
394 dac_init();
395 #endif
397 #if CONFIG_CODEC == MAS3507D
398 /* set IRQ6 to edge detect */
399 ICR |= 0x02;
401 /* set IRQ6 prio 8 */
402 IPRB = ( IPRB & 0xff0f ) | 0x0080;
404 mas_readmem(MAS_BANK_D1, 0xff7, &mas_version_code, 1);
406 mas_writereg(0x3b, 0x20); /* Don't ask why. The data sheet doesn't say */
407 mas_run(1);
408 sleep(HZ);
410 /* Clear the upper 12 bits of the 32-bit samples */
411 mas_writereg(0xc5, 0);
412 mas_writereg(0xc6, 0);
414 /* We need to set the PLL for a 14.31818MHz crystal */
415 if(mas_version_code == 0x0601) /* Version F10? */
417 val = 0x5d9d0;
418 mas_writemem(MAS_BANK_D0, 0x32d, &val, 1);
419 val = 0xfffceceb;
420 mas_writemem(MAS_BANK_D0, 0x32e, &val, 1);
421 val = 0x0;
422 mas_writemem(MAS_BANK_D0, 0x32f, &val, 1);
423 mas_run(0x475);
425 else
427 val = 0x5d9d0;
428 mas_writemem(MAS_BANK_D0, 0x36d, &val, 1);
429 val = 0xfffceceb;
430 mas_writemem(MAS_BANK_D0, 0x36e, &val, 1);
431 val = 0x0;
432 mas_writemem(MAS_BANK_D0, 0x36f, &val, 1);
433 mas_run(0xfcb);
436 #endif
438 #if CONFIG_CODEC == MAS3507D
439 mas_poll_start();
441 mas_writereg(MAS_REG_KPRESCALE, 0xe9400);
442 dac_enable(true);
443 #endif
445 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
446 ICR &= ~0x0010; /* IRQ3 level sensitive */
447 PACR1 = (PACR1 & 0x3fff) | 0x4000; /* PA15 is IRQ3 */
448 #endif
450 /* Must be done before calling sound_set() */
451 audio_is_initialized = true;
453 sound_set(SOUND_BASS, bass);
454 sound_set(SOUND_TREBLE, treble);
455 sound_set(SOUND_BALANCE, balance);
456 sound_set(SOUND_VOLUME, volume);
457 sound_set(SOUND_CHANNELS, channel_config);
458 sound_set(SOUND_STEREO_WIDTH, stereo_width);
460 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
461 sound_set(SOUND_LOUDNESS, loudness);
462 sound_set(SOUND_AVC, avc);
463 sound_set(SOUND_MDB_STRENGTH, mdb_strength);
464 sound_set(SOUND_MDB_HARMONICS, mdb_harmonics);
465 sound_set(SOUND_MDB_CENTER, mdb_center);
466 sound_set(SOUND_MDB_SHAPE, mdb_shape);
467 sound_set(SOUND_MDB_ENABLE, mdb_enable);
468 sound_set(SOUND_SUPERBASS, superbass);
469 #endif
470 #endif /* !SIMULATOR */
472 playing = false;
473 paused = true;
476 void mp3_shutdown(void)
478 #ifndef SIMULATOR
479 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
480 unsigned long val = 1;
481 mas_writemem(MAS_BANK_D0, MAS_D0_SOFT_MUTE, &val, 1); /* Mute */
482 #endif
484 #if CONFIG_CODEC == MAS3507D
485 dac_volume(0, 0, false);
486 #endif
488 #endif
491 /* new functions, to be exported to plugin API */
493 #ifndef SIMULATOR
495 void mp3_play_init(void)
497 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
498 init_playback();
499 #endif
500 playing = false;
501 paused = true;
502 callback_for_more = NULL;
503 mp3_reset_playtime();
506 void mp3_play_data(const unsigned char* start, int size,
507 void (*get_more)(unsigned char** start, size_t* size) /* callback fn */
510 /* init DMA */
511 DAR3 = 0x5FFFEC3;
512 CHCR3 &= ~0x0002; /* Clear interrupt */
513 CHCR3 = 0x1504; /* Single address destination, TXI0, IE=1 */
514 DMAOR = 0x0001; /* Enable DMA */
516 callback_for_more = get_more;
518 SAR3 = (unsigned int)start;
519 DTCR3 = size & 0xffff;
521 playing = true;
522 paused = true;
524 CHCR3 |= 0x0001; /* Enable DMA IRQ */
526 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
527 demand_irq_enable(true);
528 #endif
531 void mp3_play_pause(bool play)
533 if (paused && play)
534 { /* resume playback */
535 SCR0 |= 0x80;
536 paused = false;
537 playstart_tick = current_tick;
539 else if (!paused && !play)
540 { /* stop playback */
541 SCR0 &= 0x7f;
542 paused = true;
543 cumulative_ticks += current_tick - playstart_tick;
547 bool mp3_pause_done(void)
549 unsigned long frame_count;
551 if (!paused)
552 return false;
554 mas_readmem(MAS_BANK_D0, MAS_D0_MPEG_FRAME_COUNT, &frame_count, 1);
555 /* This works because the frame counter never wraps,
556 * i.e. zero always means lost sync. */
557 return frame_count == 0;
560 void mp3_play_stop(void)
562 playing = false;
563 mp3_play_pause(false);
564 CHCR3 &= ~0x0001; /* Disable the DMA interrupt */
565 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
566 demand_irq_enable(false);
567 #endif
570 long mp3_get_playtime(void)
572 if (paused)
573 return cumulative_ticks;
574 else
575 return cumulative_ticks + current_tick - playstart_tick;
578 void mp3_reset_playtime(void)
580 cumulative_ticks = 0;
581 playstart_tick = current_tick;
584 bool mp3_is_playing(void)
586 return playing;
590 /* returns the next byte position which would be transferred */
591 unsigned char* mp3_get_pos(void)
593 return (unsigned char*)SAR3;
595 #else /* #ifndef SIMULATOR */
597 void mp3_play_pause(bool play)
599 (void)play;
601 void mp3_play_stop(void)
605 unsigned char* mp3_get_pos(void)
607 return NULL;
610 void mp3_play_data(const unsigned char* start, int size,
611 void (*get_more)(unsigned char** start, size_t* size) /* callback fn */
614 (void)start; (void)size; (void)get_more;
616 #endif