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

📄 vs1033.c

📁 Wiznet iRadio Source
💻 C
📖 第 1 页 / 共 2 页
字号:
				
//				MyPrintf("\r\nZero padding = %d", Padding_Size);
				i += 12;
				
				if (flag != 0) {
					if (Padding_Size > 0) {
						for(j = 0; j < Padding_Size; j++) {
							stream_buffer[stream_buffer_write++] = 0;
							

							if (stream_buffer_write >= STREAM_BUF_SIZE) {
								stream_buffer_write = 0;
								wr_update_flag = 1;
							}
										
						}
					}
				}
				flag = 1;

			}
			
		}
		
		
		// copy to streambuffer
		stream_buffer[stream_buffer_write++] = http_pak.data[i];
		if (stream_buffer_write >= STREAM_BUF_SIZE) {
			stream_buffer_write = 0;
			wr_update_flag = 1;
		}
		Http_packet_recv_cnt++;
		

	}

		
//	MyPrintf("\r\nTotal = %d, loop = %d, Http_packet_recv_cnt = %d", http_pak.tot_len, i, Http_packet_recv_cnt);
	
}


void Copy_to_StreamBuffer(u16 len)
{
	u16 i;
	
	for ( i = 0; i < len; i++) {
		stream_buffer[stream_buffer_write++] = tmp_buffer[i];

			
		if (stream_buffer_write >= STREAM_BUF_SIZE) {
			stream_buffer_write = 0;
			wr_update_flag = 1;
			#ifdef ENABLE_DEBUG
				MyPrintf("\r\nwr_ptr is update");
			#endif
			if (stream_buffer_write >= stream_buffer_read) {
				#ifdef ENABLE_DEBUG
					MyPrintf("\r\n\r\n ESC ERROR");
				#endif
				break;
			}
		}
	}

	
}


u16 Recv_Stream(u8 s)
{
	u16 len = 0;
	u32 fb_size = 0;
	
		if ((len = getSn_RX_RSR(s)) > 0) 			// check Rx data
		{
			// Calculate the free buffer size
			if (wr_update_flag == 0) {
				if (stream_buffer_write >= stream_buffer_read) {
					fb_size = STREAM_BUF_SIZE - stream_buffer_write + stream_buffer_read;
				}else {
					fb_size = 0;
				}
			}else {
				if (stream_buffer_write < stream_buffer_read) {
					fb_size = stream_buffer_read - stream_buffer_write;
				}else {
					fb_size = 0;
				}
			}
			

			if (len > MAX_BUF_SIZE)  len = MAX_BUF_SIZE;
			if (len > fb_size) {
				len = fb_size;
				#ifdef ENABLE_DEBUG
						MyPrintf("\r\n\r\n len <= fb_size");
				#endif
			}
			
			
			#ifdef ENABLE_DEBUG
				MyPrintf("\r\n\r\nlen = %d, fb = %d, wr[%04x], rd[%04x]", len,fb_size,stream_buffer_write, stream_buffer_read);
			#endif
			
			len = recv(s, tmp_buffer, len);

			
			if (stream_protocol ==  STREAM_HTTP) {
				
				// Strip the HTTP header and copy only the raw data to stream buffer
				if (len > 0) {
					Parse_HTTP(len);
				}

 			}else if (stream_protocol ==  STREAM_MMS) {
 				if (len > 0) Parse_MMS(len);
			
			
			}else {
				//winamp SHOUTCAST
				// just copy
				//memcpy(&stream_buffer[stream_buffer_write], tmp_buffer, len);
				//stream_buffer_write += len;
				Copy_to_StreamBuffer(len);
			}
			


			/*
			if (stream_buffer_write >= STREAM_BUF_SIZE) {
					stream_buffer_write = 0;
					wr_update_flag = 1;
					#ifdef ENABLE_DEBUG
						MyPrintf("\r\nwr_ptr is update");
					#endif
			}
			*/
		}
	
	
	return(len);

}

void Play_MP3(u32 size)
{
	u32 i;
	
	for(i = 0; i < size; i++) {
		
		if (GPIO_BitRead(GPIO2, 11) == 0) {
			break;
		}
		
		
		vs1033_send_SDI_data(&stream_buffer[stream_buffer_read], 1);
		stream_buffer_read += 1;
		
		if (stream_buffer_read >= STREAM_BUF_SIZE) {
			#ifdef ENABLE_DEBUG
				MyPrintf("\r\n	1. rd_ptr is update");
			#endif
			stream_buffer_read = 0;
			wr_update_flag = 0;
		}
	}

}

void Push_to_VS1033(void)
{

	u32 remain_bytes;
	volatile u8 tmp;

#ifdef BUFFERING_ENABLE
	if (Enable_Play == 0) {
		if (stream_buffer_write > (STREAM_BUF_SIZE*0.2)) { // buffering
			Enable_Play = 1;
		MyPrintf("\r\n [%d] Enable play", stream_buffer_write);
			
		}
	}
#endif

#ifdef BUFFERING_ENABLE
	if (Enable_Play == 1) {
#endif		
		// DREQ is high
		tmp = GPIO_BitRead(GPIO2, 11);
		if (tmp > 0 ) {
			if (stream_buffer_write == stream_buffer_read) {
				if (wr_update_flag == 1) Play_MP3(STREAM_BUF_SIZE);
			}
			else if (stream_buffer_write > stream_buffer_read) {
				remain_bytes = stream_buffer_write - stream_buffer_read;
				Play_MP3(remain_bytes);
			}else {
				remain_bytes = STREAM_BUF_SIZE - stream_buffer_read;
				remain_bytes += stream_buffer_write; // <== 鞘夸茄啊 ?
				Play_MP3(remain_bytes);
			}
		}else {
			//MyPrintf("*");

		}
		
#ifdef BUFFERING_ENABLE		
	}
#endif	
}


void vs1033_clean_up_FIFO(void)
{

	u16 i;
	
	vs1033_mute();
	Delay_ms(20);
	
	vs1033_XDCS_LOW();
	for (i = 0; i < 2048 ; i++)
	{
		vs1033_WAIT_DREQ_HIGH();
		BPSI_DataSendReceive(0);
	}
	vs1033_XDCS_HIGH();
	
	vs1033_unmute();
}
	
void vs1033_init()
{
	vs1033_reset();
	vs1033_clean_up_FIFO();
	vs1033_set_volume(vs1033_VOL_MAX - vs1033_current_volume, vs1033_VOL_MAX- vs1033_current_volume);
}

		

void vs1033_reset()
{
	u16 tmp;

RESTART_VS1033:
	
	vs1033_RST_LOW();
	Delay_ms(2);
	vs1033_XCS_HIGH();
	vs1033_XDCS_HIGH();
	vs1033_RST_HIGH();
	Delay_ms(20);
	
	MyPrintf("\r\nVS1033 reset");

	
	// analog power down
	vs1033_WAIT_DREQ_HIGH();
	Delay_ms(20);

	// 0xa66c
	vs1033_send_SCI_data(vs1033_CLOCKF, 0x9800); 	// 12.288Mhz, Multi=3x, Add = 1.5x <== 犬牢 
	Delay_ms(20);
	vs1033_WAIT_DREQ_HIGH();
	Delay_ms(200);

	// Slow sample rate for slow analog part startup
	// 0x0010 10Hz
	vs1033_send_SCI_data(vs1033_AUDATA, 0x0010);
	Delay_ms(200);

	// switch on the analog part
	vs1033_set_volume(0xFE, 0xFE);
	// 0x3164  8Khz
	vs1033_send_SCI_data(vs1033_AUDATA, 0x1f40);
	Delay_ms(200);
	

	tmp = vs1033_get_SCI_data(vs1033_MODE);
	
	MyPrintf("\r\nVs1033 mode = %04x", tmp); 
	if (tmp != 0x0800) {
		MyPrintf("\r\n\r\nVS1033 Init Fail[%x] Re-Init !!", tmp); 
		goto RESTART_VS1033;
	}else {
		MyPrintf("\r\nVs1033 Init OK"); 
	}
	
	vs1033_soft_reset();	

}

void vs1033_soft_reset()
{
	u8 temp_data[] = {0, 0, 0, 0};
	
	vs1033_send_SCI_data(vs1033_MODE, 0x0804);
	Delay_ms(20);

	vs1033_WAIT_DREQ_HIGH();	
	Delay_ms(100);

	// Set clock reg. doubler  0xa696
	vs1033_send_SCI_data(vs1033_CLOCKF, 0x9800); 	// 12.288Mhz, Multi=3x, Add = 1.5x
	Delay_ms(100);	
	
	vs1033_send_SDI_data(temp_data, 4);
	Delay_ms(100);
}


void vs1033_set_volume(u8 left, u8 right)
{
	vs1033_send_SCI_data(vs1033_VOL, ((u16)left << 8) + right);
}

void vs1033_mute()
{
	vs1033_set_volume(0xfe, 0xfe);
}

void vs1033_unmute()
{
	vs1033_set_volume(vs1033_VOL_MAX - vs1033_current_volume, vs1033_VOL_MAX - vs1033_current_volume);	
}

void vs1033_volume_up(void)
{
	if (vs1033_current_volume < 200)
		vs1033_current_volume += 8;
	else if (vs1033_current_volume >= 200 && vs1033_current_volume <= 220)
		vs1033_current_volume += 4;
	else if (vs1033_current_volume > 220 && vs1033_current_volume <= 230)
		vs1033_current_volume += 2;
	else if (vs1033_current_volume > 220 && vs1033_current_volume <= 256)
		vs1033_current_volume += 1;

	if (vs1033_current_volume >= vs1033_VOL_MAX)
	{
		vs1033_current_volume = vs1033_VOL_MAX;
		return;
	}
	vs1033_set_volume(0xff - vs1033_current_volume, 0xff - vs1033_current_volume);	
}


void vs1033_volume_down(void)
{
	if (vs1033_current_volume < 200)
		vs1033_current_volume -= 8;
	else if (vs1033_current_volume >= 200 && vs1033_current_volume <= 220)
		vs1033_current_volume -= 4;
	else if (vs1033_current_volume > 220 && vs1033_current_volume <= 230)
		vs1033_current_volume -= 2;
	else if (vs1033_current_volume > 220 && vs1033_current_volume <= 256)
		vs1033_current_volume -= 1;
		
	if (vs1033_current_volume <= vs1033_VOL_MIN)
	{
		vs1033_current_volume = vs1033_VOL_MIN;
		return;
	}
	vs1033_set_volume(0xff - vs1033_current_volume, 0xff - vs1033_current_volume);	
}


void vs1033_WAIT_DREQ_HIGH(void)
{ 
	u16 timeout = 0;
	
	while(1)
	{
		
		timeout++;

		if (GPIO2->PD && VS1033_DREQ) break;
		
		if (timeout > 500)
		{
			MyPrintf("\r\n *** VS1033 lock down detected! resetting vs1033...");
			vs1033_reset();
		}		
	}
}


⌨️ 快捷键说明

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