⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sta013.c

📁 Build ourself a MP3 Player
💻 C
字号:
#include "sta013.h"
#include "config.c"
#include "i2c+spi.c"

// The ByteEnable line on the STA
void sta_enable() {
  output_high(ENABLE_MP3);
}

void sta_disable() {
  output_low(ENABLE_MP3);
}

// the /reset line for hardware reset
void sta_reset() {
	output_low(MP3_RESET);
	delay_ms(100);
	output_high(MP3_RESET);
	delay_ms(100);
	printf("STA Reset\n\r");
}

// attempts to start comm with the STA,
// reading reg 0x1 should return 0XAC
boolean sta_identify() {
	byte data;
	signed int i;

	sta_enable();
	sta_reset();
	data = sta_readreg(STA_REG_IDENT);
	if (data != STA_IDENT) {
		printf("STA013 not found :(\n\r");
		return 0;
	}
	printf("Found STA013\n\r");
	return 1;
}

/******************************************************************/
// general STA reading and writing via i2c

byte sta_readreg(byte reg)
{
	mp3_i2cStart();
	if (! mp3_i2cWrite(STA_I2C_DEV)) {
			printf("didnt ack dev! \n\r");
		mp3_i2cStop();
		return 0;
	}
	if (! mp3_i2cWrite(reg) ) {
		printf("didnt ack reg 0x%x\n\r", reg);
		mp3_i2cStop();
		return 0;
	}
	mp3_i2cStop();
	mp3_i2cStart();
	if (! mp3_i2cWrite(STA_I2C_DEV | 0x1)) {
		printf("didnt ack dev! \n\r");
		mp3_i2cStop();
		return 0;
	}
	return mp3_i2cRead();
}

byte sta_writereg(byte reg, byte data) {
	mp3_i2cStart();
	if (! mp3_i2cWrite(STA_I2C_DEV)) {
		printf("didnt ack dev! \n\r");
		mp3_i2cStop();
		return 0;
	}
	if (! mp3_i2cWrite(reg) ) {
		printf("didnt ack reg 0x%x\n\r", reg);
		mp3_i2cStop();
		return 0;
	}
	if (! mp3_i2cWrite(data) ) {
		//printf("didnt ack data ");
	}

	mp3_i2cStop();
   // printf("wrote %x to %x\n\r", data, reg);
	return 1;
}

/******************************************************************/
// writes the high quality mp3 decoding and 10MHZ configuration data
// to the sta013.
// for some reason, the compiler doesn't like the data to be in one
// large array so its broken up. eit.

byte sta_loadconfig() {
	byte addr, data;
	int16 i;

	i = 0;
	sta_enable();
	addr = data = 0;
	for (i=0; i<1000; i+=2) {
		addr = sta013_firmware1[i];
		data = sta013_firmware1[i+1];
	  	if (! sta_writereg(addr, data)) {
	  		printf("Finished\n\r");
			break;
		}
	}
	addr = data = 0;
	for (i=0; i<1000; i+=2) {
		addr = sta013_firmware2[i];
		data = sta013_firmware2[i+1];
	  	if (! sta_writereg(addr, data)) {
	  		printf("Finished\n\r");
			break;
		}
	}

	addr = data = 0;
	for (i=0; i<1000; i+=2) {
		addr = sta013_firmware3[i];
		data = sta013_firmware3[i+1];
	  	if (! sta_writereg(addr, data)) {
	  		printf("Finished\n\r");
			break;
		}
	}

	addr = data = 0;
	for (i=0; i<1016; i+=2) {
		addr = sta013_firmware4[i];
		data = sta013_firmware4[i+1];
	  	if (! sta_writereg(addr, data)) {
	  		printf("Finished\n\r");
			break;
		}
	}


   /* for 10mhz clock */
	sta_writereg(6, 18);
	sta_writereg(11, 3);
	sta_writereg(97, 15);
	sta_writereg(80, 16);
	sta_writereg(101, 169);
	sta_writereg(82, 49);
	sta_writereg(100, 42);
	sta_writereg(81, 60);
	sta_writereg(5, 161);
}

/****************************************************************/
// Sets and retreives the software volume control

void sta_setvolume(byte v) {
   sta_writereg(STA_REG_RVOL, v);
   sta_writereg(STA_REG_LVOL, v);
   printf("setting volume to -%d dB\n\r", v);
}

byte sta_getvolume() {
   return sta_readreg(STA_REG_RVOL);
}
/****************************************************************/

byte sta_playfilenum(num) {
	byte addr, data, ret;
	int16 i;

	sta_enable();

	// start playing
	sta_writereg(STA_REG_RUN, 1);
	sta_writereg(STA_REG_PLAY, 1);

   // get file
	FAT_StartReadFileNum(num);

	while (1) {
		if (! FAT_ReadNextSectorIntoBuff()) {
			printf("done!\n\r");      // file finished
         break;
		}
      while(paused) {
         // wait for them to hit the pause button again
         if (buttonquery() == BUTTON_PAUSE) {
            paused = false;
            t=0;              // reset the button delay timer
	         sta_writereg(STA_REG_RUN, 1);
            sta_writereg(STA_REG_PLAY, 1);
         }
      }
      //putc('.');
      for (i=0; i<512;) {
			   if (input(MP3_DATAREQ)) {
				//mp3_spiwrite(sectorbuff[i]);
            // inline version of above, bang out a byte of data over SPI
				data = sectorbuff[i++];
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 7));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 6));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 5));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 4));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 3));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 2));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 1));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 0));
				output_high(SPI_CLK);
			   }
       }

      // in between, check if theyve hit any buttons
       ret = buttonquery();
       switch (ret) {
            case BUTTON_NEXT: return NEXT_TRACK;        // go back to the driver loop
            case BUTTON_PREV: return PREV_TRACK;        // and with instruction for what to do next
            case BUTTON_PAUSE: {
                   paused = true;
                   sta_writereg(STA_REG_RUN, 0);
                   sta_writereg(STA_REG_PLAY, 0);
                   break; }
            default: break;
      }
   }
   return NEXT_TRACK;                                  // tell the driver loop to play the next track
}


// same as above, but for playing from the serial port control
byte sta_playfilename(char *name) {
	byte addr, data, ret;
	int16 i;

	sta_enable();
	// start playing
	sta_writereg(STA_REG_RUN, 1);
	sta_writereg(STA_REG_PLAY, 1);

	if (! FAT_StartReadFile(name)) {
      printf("couldnt find %s!\n\r", name);
      return 0;
   }
   
	while (1) {
		if (! FAT_ReadNextSectorIntoBuff()) {
			printf("done!\n\r"); break;
		}
      while(paused) {

         // wait for them to hit the pause button again
         if (buttonquery() == BUTTON_PAUSE) {
            paused = false;
            t=0;              // reset the button delay timer
	         sta_writereg(STA_REG_RUN, 1);
            sta_writereg(STA_REG_PLAY, 1);
         }
      }

		for (i=0; i<512;) {
			if (input(MP3_DATAREQ)) {
				//ada_spiwrite(sectorbuff[i]);
            // inline!
				data = sectorbuff[i++];
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 7));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 6));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 5));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 4));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 3));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 2));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 1));
				output_high(SPI_CLK);
				output_low(SPI_CLK);
				output_bit(SPI_SDO, bit_test(data, 0));
				output_high(SPI_CLK);
			}
		}
       ret = buttonquery();
       switch (ret) {
            case BUTTON_NEXT: return NEXT_TRACK;        // go back to the driver loop
            case BUTTON_PREV: return PREV_TRACK;        // and with instruction for what to do next
            case BUTTON_PAUSE: {
                   paused = true;
                   sta_writereg(STA_REG_RUN, 0);
                   sta_writereg(STA_REG_PLAY, 0);
                   break; }
            default: break;
      }
   }
   return NEXT_TRACK;                                  // tell the driver loop to play the next track
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -