MP3 Player, How To Use The STA013 MP3 Decoder Chip
MP3 Player, How To Use The STA013 MP3 Decoder Chip
MP3 Player, How To Use The STA013 MP3 Decoder Chip
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
Shopping Cart
Checkout
Shipping Cost
Download Website
Home
MP3 Player
Technical Docs
8051 Tools
Using The STA013
All Projects
PJRC Store
Site Map
Search PJRC
PJRC Store
Main Board, $150 LCD & Pushbuttons, $42 LCD/Backlight/PB, $77 IDE Cable, $9 Complete Parts List
MP3 Player
Main Page Detailed Info User Photo Gallery Connecting The Board Firmware Download Side Projects Technical Docs Document Index Kit Assembly, Rev C Kit Assembly, Rev A Win32 Compiler Firmware Library Calls Memory Map Data Sheets IR Remote Control Schematic, Rev C Schematic, Rev A Schematic, FPGA PCB Layout, Rev C PCB Layout, Rev A LCD Fonts LCD Protocol LCD Pushbuttons Using The STA013 72 Pin SIMM Old MP3 Player Design Freq. Asked Questions FAQ #2 News And Updates
You can purchase the STA013 and CS4334 directly from PJRC: Purchase STA013 MP3 Decoder, $12 Purchase CS4334 Digital to Analog Converter, $5
1 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
crystals, but the examples here will focus only on a 14.7456 MHz crystal, which is inexpensive and a standard value (that also happens to be an exact multiple of standard baud rates). The examples also use the CS4334, which provides excellent quality line-level audio output, and also includes 4X interpolation and a continuous time analog output filter that eliminates the need for external opamps and complex output filtering. The CS4334 makes it simple and easy to obtain excellent quality line-level audio output without adding noise. Essential Commands/Queries Only: there are many options and parameters available in the STA013, but only the most important ones are covered here, in the interest of keeping this page simple and easy to understand. A long and detailed list appears in the datasheet, but most MP3 player designs will only need to use the ones mentioned here, at least for their basic functioning. The hope is that this limited focus will allow this page to explain how to use the STA013 much more clearly, for the common application in a media-based MP3 player. More advanced applications (real-time low-latency radio reception, audio data fed into another processor, etc) may need to use other features, but this page still may be a good place to start, particularly if the STA013 datasheet is a bit confusing.
Aries makes adaptors that are ideal for connecting the STA013 in a prototype, which DIP pins are easier to use. The adaptor shown in Figure 2 is available at DigiKey (P/N: A323-ND). This is actually a 32 pin adaptor, so 4 pins are unused with the 28 pin STA013. While an adaptor circuit board is easier, it is also possible to solder wires to all the pins. Here's a student project by Peter D'Antonio and Danial Riiff, with 28 wires soldered. TODO: contact Peter and Daniel (anyone know their email address??) to ask for permission to display their soldered wires photo. Update: Scott McNab wrote this nice page with instructions to make your own adaptors.
Figure 3: Interfacing 3 Volt Output to 5 Volt TTL Input: DATA_REQ, SDO, SCKT, LRCKT, OCLK
The outputs from a 5 volt powered chip must not be directly connected to the STA013 input pins. The STA013 inputs are not 5 volt tolerant. Each pin has a pair of input protection diodes. These diodes may conduct a small current. The simplest and easiest way to interface a 5 volt output to the STA013 input pins is with a series resistor, that will limit the current when the 5 volt output is high. There is some input capacitance (3.5 pF) on the input pins, so adding a small capacitor in parallel with the resistor will allow the rapid edge to properly drive the input pin. The value of the resistor and capacitor are not critical, Figure 4
2 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
shows 4.7K and 47pF, which limits the steady-state current to less than 1/2 mA, and has been tested. The capacitor is not really necessary if the pin doesn't need to be driven rapidly, such as the RESET pin.
Figure 4: Interfacing 5 Volt Output to 3 Volt Input: SCL, SDI, SCKR, RESET
Connecting the SDA signal from a 5 volt microcontroller to the STA013 is a bit more complex, though a simple circuit can often be used. The SDA line is bidirectional, where either device can pull down, and a resistor provides the pull-up. Most microcontrollers have TTL thresholds, so a pullup to the 3 volt supply will easily satisfy the 2.0 volt input high requirement. If the microcontroller will never drive the line high (only pull low or tri-state), then no other parts are probably required. This may be the case if the microcontroller has a dedicated IC pin. In most cases, the microcontroller can drive the line high, and an additional resistor should be added to prevent damage to the STA013. This current limiting resistor should be small, as it will form a resistor divider with the pullup when the microcontroller drives low. If the pin can be put in a tri-state mode, the firmware should be written to use the tri-state for a logic high, or at least tri-state the SDA signal when not transmitting data, to save power.
Philips suggest a somewhat more complex 5V to 3V connection involving a pair of mosfet transistors. Their solution is a much better approach for a general purpose bus. The pair of resistors shown here should only be used with a single microcontroller and the STA013. If you have a N-channel mosfet transistor, the Philips circuit would be better. This is only necessary on the SDA line, since the microcontroller will always generate the clock, so the circuit in Figure 4 will work well for the SCL signal.
3 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
The resistors connected to PVDD and PVSS are 4.7 Ohm (not 4.7k). Any value near a few ohms is ok, if 4.7 ohms is not available. The capacitors connected to the crystal may need to be changed to match the resonance of the crystal. Values are usually in the range of 15 to 47 pF, and many crystals are not very sensitive to the exact capacitor values used. The inductor shown on the CS4334 power supply is intended to block noise from digital circuitry that's also connected to that line. The value of the inductor is not critical, so 100 H would probably work well. In no inductor is available, and alternative is to create a separate ("clean") supply for the CS4334 with a LM7805 or similar linear-mode voltage regulator. Here's a table listing how each pin should be connected, which can be used as a "check off" list while wiring the chip on a breadboard. Pin Pin Name 1 2 3 4 5 6 7 8 9 VDD_1 VSS_1 SDA SCL SDI SCKR BIT_EN SCR_INT SDO Group In/Out Power +3 Volts Power Ground To microcontroller, IC Clock Connect to Data Source (Data) Connect to Data Source (Clock) +3 Volts +3 Volts To CS4334 SDATA, Pin 1 To CS4334 DEM/SCLK, Pin 2 To CS4334 LRCK, Pin 3 To CS4334 MCLK, Pin 4 How To Connect
DAC Output Out DAC Output Out DAC Output Out DAC Output Out Misc Misc Misc Misc Misc Reset -
10 SCKT 11 LRCKT 12 OCLK 13 VSS_2 14 VDD_2 15 VSS_3 16 VDD_3 17 PVDD 18 PVSS 19 FILT 20 XTO 21 XTI 22 VSS_4 23 VDD_4 24 TESTEN 25 SCANEN 26 RESET 27 VSS_5
Power Ground Power +3 Volts Power Ground Power +3 Volts Power +3 Volts through RC filter Power Ground through RC filter Out Out In Connect Resistor and two Capacitors Connect Crystal Circuit Connect Crystal Circuit
Power Ground Power +3 Volts In In In Out +3 Volts Ground Low = Reset, High = Able To Run Connect to Data Source (Ready)
Power Ground
4 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
The STA013 requires initialization by IC communication. This step can not be avoided. Part of the required initialization is to send a group of 2007 writes provided by ST in the file p02_0609.bin. A frequently asked question is "do I really need to use IC and the file from ST?". The answer is Yes!, the STA013 will not work without being properly initialized. The config file from ST is part of this process. There does not appear to be a way to make the chip work properly without it. Fortunately, the initialization step isn't difficult, and you will find working example code at the end of this page. First, a little background about IC. IC is a 2-wire protocol, created by Philips. One line acts as a clock (SCL) and the other data (SDA). The protocol defines the ability to have multiple masters initiate communication, but here we'll only worry about the simple and common case where the microcontroller is the only device that controls the bus, and all the other chips (like the STA013) respond to queries initiated by the microcontroller. There are many sites with detailed info about this protocol. Philip's Main IC Page.... remember, for the simple case of chips like the STA013 connected to a single microcontroller, you don't need to worry about all that complicated bus arbitration stuff. IC FAQ 2.0 by Vincent Himpe Atmel 8051 App Notes, see the "Interfacing 24Cxxx Serial EEPROMs". Atmel AVR App Notes, see AVR300: "Software IC Master Interface". Microchip IC EEPROM Examples: PIC12CE51x, PIC12CE673, PIC16CE62x, PIC18Cxx2. Keil's IC Example Code TODO: add more useful IC links If you have a link or two, please email them to me. I particularly want to add links to app notes and free code that implements IC.
5 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
address to our sta013_write function. 4. Send A Byte The value is the data passed from the main program to write into the STA013. 5. Stop Condition The code that implements these steps should check the ACK bit returned by each byte transmit, and return an error to the main program if there is no ACK bit received. There are other faster and somewhat more complex ways to communicate with the STA013, such as writing to multiple consecutive locations. These optimizations are usually of little benefit with the STA013.
The example code near the end of this page contains a perl script to convert this data format into intel-hex, which can be programmed into a EEPROM or downloaded to a monitor program. The perl script also adds a length count and simple checksum, which can be used to check the data before sending to the the chip. Once the config file is sent, the board specific settings must be sent. Here are the settings used in this example, for 14.7456 MHz crystal and the CS4334 DAC: Register Addr Data Comment
6 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
PCMDIVIDER PCMCONF PLLCTL PLLCTL ??? ("reserved") MFSDF_441 PLLFRAC_441_L PLLFRAC_441_H MFSDF PLLFRAC_L PLLFRAC_H PLLCTL
84 85 7 6 11 80 81 82 97 100 101 5
1 33 0 12 3 16 0 4 15 85 85 4
256X oversample, 32 bit words (allows 24 bit output) I2S format for CS4334 Default is zero, not in Table 10, but is zero in ConfigPLL v1.0 14.7456 MHz Clock, 256 Oversample, Table 10 in Datasheet 14.7456 MHz Clock, 256 Oversample, Table 10 in Datasheet 14.7456 MHz Clock, 256 Oversample, Table 10 in Datasheet 14.7456 MHz Clock, 256 Oversample, Table 10 in Datasheet 14.7456 MHz Clock, 256 Oversample, Table 10 in Datasheet 14.7456 MHz Clock, 256 Oversample, Table 10 in Datasheet 14.7456 MHz Clock, 256 Oversample, Table 10 in Datasheet 14.7456 MHz Clock, 256 Oversample, Table 10 in Datasheet Enable data request pin
DATA_REQ_ENABLE 24
It is possible to use the STA013 with other crystals. For example, to use a 10 MHz crystal, use the values in Table 5 from datasheet, and to use a 14.31818 MHz crystal, use Table 7. ST also provides a ConfigPLL v1.0 utility to compute these parameters for other crystals. It's interesting that register 7 is shown in ConfigPLL, but not in the tables of the datasheet. ConfigPLL seems to always set it to zero. Likewise, the tables list register 5, but ConfigPLL does not. All the tables show register 5 at 161, so perhaps that's the right setting for any crystal. One thing is certain, the settings shown here (same as in the example code) have been tested and are known to work with the 14.7456 MHz crystal and CS4334 DAC, so using them with 14.7456 MHz and a CS4334 is a very safe bet! The easiest way to send these setting it to just add them on to the end of the p02_0609.bin file, and let the loop that sends that file do the work. The last several lines of that file are actually writes to some of these registers, so they can be removed and replaced with the configuration you need. The copy of p02_0609.bin in the download section has these settings added, so you can use it with a CS4334 and 14.7456 MHz crystal without any changes. After you've checked the 0xAC, and sent the config file (with config data appended), then all that's left it telling the chip to start running. This could perhaps be added to the config file as well, though the example shown below does it separately. Just write a 1 to address 114 to tell the STA013 to start running, and then a 1 to address 19 to tell it to start playing. The DATA_REQ pin should be asserted, and when you start feeding valid MP3 data in, the chip will detect the MP3 sample rate, create the correct clocks for the DAC, and start pushing data into it.
There are three signal lines used for sending the MP3 data to the STA013 decoder: SDI (DATA), SCKR (CLOCK), and DATA_REQ. The STA013 asserts the DATA_REQ line when more MP3 data is needed from the host controller. The host
7 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
controller then feeds MP3 data, most significant bit first, on the SDI line. Each bit placed on SDI is clocked, by the controller, into the STA013 with the SCKR signal line. Some example code for sending MP3 data follows:
char mp3data[]; /* imagine that this buffer is already full of data */ unsigned long mp3ptr = 0; for(;;) { while(DATA_REQ) { /* STA013 is requesting more data, send a byte */ for(short j = 7; j >= 0; j--) { SCKR = 0; SDI = (mp3data[mp3ptr] >> j) & 1; SCKR = 1; } mp3ptr++; } /* do anything you want here... but don't take too long at it :-) */ }
The example code near the end of this page has a very similar function named "xmit_mp3_clip" implemented in 8051 assembly. It contains 8 copies instead of a loop, which nearly doubles the speed. Also keep in mind that if something goes wrong with the STA013 and it doesn't assert DATA_REQ, this will turn into an infinite loop. For a "real" player design, some sort of timeout should be added.
The above code assumes that whenever the STA013 asserts DATA_REQ, it can accept at least eight bits of data. The STA013 datasheet never explicitly says this is the case (AFAIK), however, in the practice it does seem to hold true. Which is very good, because this lets us use the 8051 serial mode 0 to send data more efficiently... In 8051 serial mode 0, the RxD pin shifts data out of the serial buffer (special function register SBUF) as the TxD pin provides clocking for each bit transmitted. If you hooked the STA013 SDI pin to the 8051 RxD pin and STA013 SCKR to 8051 TxD, you could in theory now use the 8051's UART hardware to do the bit shifting that we had to do manually in the code example before. However, there's a catch: the 8051 UART shifts out data least significant bit first, which is the opposite of what the STA013 is expecting. Therefore, before writing a byte of MP3 data to the SBUF register, we must reverse the bits so that the STA013 receives the bits in the order it expects:
/* swaps unsigned return | | | | | | | } bits char i >> (i & (i & (i & (i & (i & (i & i << MSB<->LSB to put bits in correct order for STA013 SDI */ swapbits(unsigned char i) { 7 0x40) >> 5 0x20) >> 3 0x10) >> 1 0x08) << 1 0x04) << 3 0x02) << 5 7;
char mp3data[]; /* imagine that this buffer is already full of data */ unsigned long mp3ptr = 0; SCON = 0; /* go into serial mode 0 */ for(;;) { while(DATA_REQ && TI) { /* STA013 is requesting more data and our serial port isn't busy */ TI = 0; /* clear transmit flag -- serial port busy */ SBUF = swapbits(mp3data[mp3ptr++]); } /* do anything you want here, but don't take too long... ;-) */ } Martin Frost also suggested this code to swap the bits unsigned char swapbits (unsigned char i) { i = ((i & 0xAA) >> 1) | ((i & 0x55) << 1); i = ((i & 0xCC) >> 2) | ((i & 0x33) << 2); return (i >> 4) | (i << 4); }
If you have enough memory, it would be fastest to build a 256 byte lookup table ahead of time that will let you swap bits without running the swapbits() function on each byte transmitted.
The lookup table should be implemented in code-space (MOVC). The 8051 serial mode 0 is so fast that the checks on the TI bit are unnecessary... you can't get the data and do a fast table lookup bit swap before all of the previous bits are sent. My old player design used this approach, and you can look at it's code, including the "flip_bits" lookup table, to save you the effort of regenerating this table. Look at the code in "idle_loop" for a timing analysis, best case is 13 cycles, and 8051 mode 0 takes 9 cycles to transmit.
8 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
A microcontroller with a dedicated SPI port would be even better because the SPI interface will send data in the proper bit order by default. So, use SPI instead of you have the option.
End of Ed's section. Thanks Ed, that really helped.
The fastest and most efficient method, but also by far the most difficult to design, is to build dedicated hardware to send large blocks of data to the STA013 automatically, without any CPU overhead. The new MP3 player does this using a Xilinx FPGA chip. The basic idea is that the CPU writes the address and length of a block of MP3 data that resides in memory, and then writes to a "go" bit. The hardware automatically detects when the CPU doesn't need to access the memory, and reads the MP3 data into a shift register, which sends the bits to the STA013 as it requests them. The bits are shifted at about 7 Mbit/sec (very fast, but the STA013 can handle 20 Mbit/sec). Each time the shift register is empty, the hardware makes another read from the next memory location, as soon as there is an opportunity to access the memory when the CPU doesn't need it. When all of that block has been sent, the hardware gives the CPU an interrupt, so that the CPU can give it the next block's addr/len to transmit. For some more details about how this is done, take a look at the memory map page for the new player design. This design allows even a slow (low power) CPU to easily play the fastest MP3 streams (320 kbps), but it is a very difficult approach. While it is possible to design a very high performance data transfer, the example code below uses the simplest possible method, directly manipulating the port pins. The intention of the example code is to be simple and easy to understand.
The left photo shows a burst of transfer every 26 ms, which suggest that an average of 1664 bits are transfered into the buffer each time, to maintain 64 kbps throughput. Our sony mavica camera uses approx 15 ms shutter speed in this lighting, which made the left photo very difficult... it's either a way-too-bright spot a few divisions long, or a weak image if the shutter happens to open between traces (as in the one I finally ended up using). That's why you see a weak trace, when a slow sweep is usually a bright and annoyingly flashing image. The center photo shows that the bursts are about 345 s long. The right photo shows the effective bit rate. Bursts of 16 bits are sent at 7.37 MHz, with a delay between each 16 bits while the control state machine arbitrates for access and then reads the DRAM. 32 bits are sent in approx 6.4 s, for a bit rate of 5 Mbit/sec, which suggests about 1725 bits are loaded into the STA013's input buffer during the 345 s burst. This second method probably over-estimates the buffer size slightly, since the control state state machine must sometimes wait during the bus arbitration (the slight "ghost" image in the right photo), causing the effective bit rate to be somewhat less than 5 Mbit/sec. Still, 1664 and 1725 bits is a 3.7% error (if you assume 1664 is closer to the truth)... not bad for counting divisions on the 'scope screen! These waveforms only measure the input buffer between the point where the STA013 asserts and releases DATA_REQ, which is says that the buffer is at least 206 bytes.
9 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
Here's a typical session using the example code. The code is written to run with the PM2 monitor program. It can be easily modified to run under a different development system, or stand-alone. See the comments in the source code for details. The example uses the 8051 development board. Here, the board is booted and the memory and flash are cleared. It's not really necessary to clear the memory, but if anything is residing in the flash memory (other than MP3 data), it must be erased.
Welcome to PAULMON2 v2.1, by Paul Stoffregen See PAULMON2.DOC, PAULMON2.EQU and PAULMON2.HDR for more information.
10 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
Program Name List Single-Step Memory Editor (VT100) STA013 Example PAULMON2 Loc:2000 > Clear memory First Location: 2000 Last Location: 3FFF Are you sure?
PAULMON2 Loc:2000 > Erase flash rom Erase flash rom, Are you sure? Flash rom erased
Three files must be downloaded. The "sta013.hex" was sent first in this example (794 bytes of data). Then the "p02_0609.hex" file was transmitted next, and finally one of the MP3 test clips was sent. In this example, it was "faith_hill_clip.mp3", which is several seconds from a well known song. The example code uses the following memory: 0x2000 to 0x232D: STA013.HEX, the simple code example 0x3000 to 0x3FBA: P02_0609.BIN, sta013 config and settings 0x8000 to 0xFFFF: MP3 Data to play This memory arrangement fits the memory map of the 8051 development board. For other 8051 boards, the intel-hex files may need to be recreated at different memory ranges, depending on the board's memory map. With the PJRC 8051 development board, the example code will run without and modifications. Any MP3 data could be downloaded, but the four test clips in the download section are encoded at very low bit rates, so that a 32 kbyte clip can play for several seconds. At the usual 128 kbit/sec MP3 bitrate, 32 kbytes will play for only 2 seconds.
PAULMON2 Loc:2000 > ^................................................... Download completed Summary: 51 lines received 794 bytes received 794 bytes written No errors detected
PAULMON2 Loc:2000 > ^........................................................... .................................................................... Download completed Summary: 127 lines received 4026 bytes received 4026 bytes written No errors detected
PAULMON2 Loc:2000 > ^........................................................... ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ................................................................................ ..... Download completed Summary: 1024 lines received 32735 bytes received 32735 bytes written No errors detected
Once all three files are loaded into memory, just run the program. The example code does some checks to make sure that a valid STA013 config file is present. It does not check the validity of the MP3 data... whatever is in the 0x8000 to 0xFFFF memory range is sent to the STA013 chip.
11 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
PAULMON2 Loc:2000 > Run program A - STA013 Example run which program(A-A), or ESC to quit: A STA013 MP3 Decoder Test Program Reseting STA013 Reading IDENT register: AC Ok Checking p02_0609 (config data) at 0x3000 Ok Downloading Config Data to STA013 chip: Ok Sending RUN Command: Ok Sending PLAY Command: Ok Playing MP3 Clip From Memory: Ok Press any key to restart:
12 de 13
23/11/2013 12:10
http://www.csee.umbc.edu/courses/undergraduate/CMSC391/summer0...
TODO: add a wrap-up section, with some suggestions about what to do next, links to new and old player. MP3 Player, Using The STA013 Chip, Paul Stoffregen. http://www.pjrc.com/tech/mp3/sta013.html Last updated: December 25, 2003 Questions, Comments?? <[email protected]>
13 de 13
23/11/2013 12:10