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

📄 vs1033.c

📁 Wiznet iRadio Source
💻 C
📖 第 1 页 / 共 2 页
字号:

#include "vs1033.h"
#include "gpio.h"
#include "bspi.h"
#include "util.h"
#include "w5100.h"
#include "socket.h"
#include "eeprom.h"
#include "netconfig.h"
#include "mms.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h> 


#define HTTP_DEBUG
//#define ENABLE_DEBUG

u8 Enable_Play;

// Global Variables
u16	vs1033_current_volume = vs1033_VOL_DEFAULT;
u8 ASF_File_Object[11] = {0xA1, 0xDC, 0xAB, 0x8C, 0x47, 0xA9, 0xCF, 0x11, 0x8E, 0xE4, '\0'};

u8 stream_buffer[STREAM_BUF_SIZE];
u32	stream_buffer_write;
u32	stream_buffer_read;
u8 wr_update_flag;

u8 tmp_buffer[MAX_BUF_SIZE];
u8 stream_protocol;


u16 H_Size, D_Size;
u8 Padding_Size;

u8 Need_2nd_Req;
u8 tmp_URL[MAX_URL_SIZE/2]; // 64

u16 Http_packet_recv_cnt;
u8 flag;
u8 H_flag;
HTTP_PACKET http_pak;

void (*jump_function3)(void);

extern u8 http_request[MAX_URL_SIZE + 256];
extern u8 Server_URL[MAX_URL_SIZE/2]; // 64
extern u8 Server_Page[MAX_URL_SIZE/4]; // 32



void Init_BSPI (void)
{
  // Initialize BSPI1 device
  BSPI_Init ( BSPI1 );

  // Configure Baud rate Frequency: ---> APB1/6 
  BSPI_ClockDividerConfig (BSPI1, 6);

  // Enable BSPI1
  BSPI_Enable ( BSPI1, ENABLE );
  // Configure BSPI1 as a Master
  BSPI_MasterEnable  ( BSPI1, ENABLE );

  // Configure the clock to be active low 
  BSPI_ClkActiveHigh ( BSPI1, DISABLE);

 // Enable capturing the first Data sample on the first edge of SCK
  BSPI_ClkFEdge ( BSPI1, DISABLE);

 // Set the word length to 8 bit
  BSPI_8bLEn ( BSPI1, ENABLE);
}


u8 BPSI_DataSendReceive(u8 DATA)    
{
  u8 temp;
  
  while(BSPI1->CSR2&BSPI_RFNE)
  temp = BSPI1->RXR;

  // Wait until the Transmit FIFO is empty
  while(!(BSPI1->CSR2&BSPI_TFE));    

  // Send data to Transmit buffer
  BSPI1->TXR = DATA<<8;              

  // Wait until the end of transmission
  while(!(BSPI1->CSR2&BSPI_RFF));    

  //  Read the received data
  temp = (BSPI1->RXR)>>8;            
  
  return temp;                     
}


void vs1033_send_SCI_data(u8 addr, u16 data)
{
	
	vs1033_XDCS_HIGH();
	vs1033_XCS_LOW();
	
	BPSI_DataSendReceive(vs1033_COMMAND_WRITE); //
	
	BPSI_DataSendReceive(addr); // 

	BPSI_DataSendReceive(HIGH(data)); // data 
	
	BPSI_DataSendReceive(LOW(data)); // data 
	
	vs1033_XCS_HIGH();

}

u16 vs1033_get_SCI_data(u8 addr)
{
	u8 temp1, temp2;
	u16 data;

	vs1033_XDCS_HIGH();
	vs1033_XCS_LOW();

	BPSI_DataSendReceive(vs1033_COMMAND_READ); // command 
	
	BPSI_DataSendReceive(addr); //  addr

	temp1 = 0xff;
	temp2 = BPSI_DataSendReceive(temp1); //dummy
	data =  (u16)temp2 << 8;


	temp1 = 0xff;
	temp2 = BPSI_DataSendReceive(temp1); //dummy
	data |= (u16)temp2;
	
	vs1033_XCS_HIGH();
	
	return data;
}


void vs1033_send_SDI_data(u8* data, u16 length)
{
	u16 i;

	//vs1033_MCU_DISABLE_INTERRUPT();

	vs1033_XDCS_LOW();	
	for (i=0; i<=length - 1; i++)
	{
		BPSI_DataSendReceive(data[i]);
	}
	vs1033_XDCS_HIGH();	

	//vs1033_MCU_ENABLE_INTERRUPT();
}


void Send_Probe(void)
{
	u16 len;
	u8 * ptr;
	u16 time_out = 0;
	
	
       memset(http_request, '\0', sizeof(http_request));
	sprintf(http_request, "GET %s HTTP/1.1\r\nUser-Agent: http_parser\r\nHost: %s\r\n\r\n", &Server_Page[0], Server_URL);
	send(SOCK_CLIENT, http_request, strlen(http_request));
	
	memset(tmp_buffer, '\0', sizeof(tmp_buffer));
	//receive 
	while(1){
		len = getSn_RX_RSR(SOCK_CLIENT);
		if (len > 0) {
			recv(SOCK_CLIENT, tmp_buffer , len);
			
			ptr = memstr(&tmp_buffer[0], "ICY 200 OK", (int)len);
			if (ptr > 0) {
				stream_protocol = STREAM_WINAMP;
				
			}else {
				stream_protocol = STREAM_HTTP;
			}

			break;
		}else {
			if (time_out++ > 50) {
				stream_protocol = STREAM_HTTP;
				MyPrintf("\r\ntimeout");
				break;
			}
		}
		Delay_ms(10);
	}

	Delay_ms(10);
	disconnect(SOCK_CLIENT);
	
}

void Send_Request(void)
{
	
	vs1033_init();
	
	memset(stream_buffer, 0, STREAM_BUF_SIZE);
		
	stream_buffer_read = 0;
	stream_buffer_write = 0;
	wr_update_flag = 0;

	// Init structure
	Need_2nd_Req = 0;
	H_flag = 0;

#ifdef BUFFERING_ENABLE
	Enable_Play = 0;
#endif	

	  memset(http_request, '\0', sizeof(http_request));

	  if (stream_protocol == STREAM_HTTP) {
		 sprintf(http_request, "GET %s HTTP/1.0\r\nAccept: */*\r\nUser-Agent: NSPlayer/4.1.0.3936\r\nHost: %s\r\nPragma: no-cache,rate=1.000000,stream-time=0,stream-offset=4294967295:4294967295,request-context=2,max-duration=0\r\nPragma: xPlayStrm=1\r\nPragma: xClientGUID={3300AD50-2C39-46c0-AE0A-1DF5EAAEF99C}\r\nPragma: stream-switch-count=1\r\nPragma: stream-switch-entry=ffff:1:0\r\n\r\n", &Server_Page[0], Server_URL);
		 
	  }else if (stream_protocol == STREAM_WINAMP) {
		sprintf(http_request, "GET %s HTTP/1.0\r\nAccept: */*\r\nUser-Agent: WinampMPEG/5.35\r\nHost: %s\r\n\r\n",&Server_Page[0], Server_URL);		  		
	  }



	send(SOCK_CLIENT, http_request, strlen(http_request));
}

char *memstr(char *haystack, char *needle, int size)
{
	char *p;
	char needlesize = strlen(needle);

	for (p = haystack; p <= (haystack-needlesize+size); p++)
	{
		if (memcmp(p, needle, needlesize) == 0)
			return p; // found
	}
	return NULL;
}

// For Speed
char *memstr2(char *haystack, char *needle)
{
	char *p;

	for (p = haystack; p <= (haystack); p++)
	{
		if (memcmp(p, needle, 2) == 0)
			return p; // found
	}
	return NULL;
}

u16 Recv_HTTP(u8 s)
{
	u16 len = 0;

	// check Rx data
	if ((len = getSn_RX_RSR(s)) > 0) {
		if (len > MAX_BUF_SIZE)  len = MAX_BUF_SIZE;
		len = recv(s, tmp_buffer, len);
		/*
		for(i = 0; i < len;i++) {
			MyPrintf("%c", tmp_buffer[i]);
		}

		MyPrintf("\r\n 1. Recv %d bytes", len);
		*/
	}
	
	return(len);
}
u16 Parse_Reply(u8 s)
{
	u16 i, len = 0;
	u8 * ptr_data = 0;
	u16 len2;
	u8 tmp;


	// check Rx data
	if ((len = getSn_RX_RSR(s)) > 0) {
		MyPrintf("\r\nParse_Reply");
		
		if (len > MAX_BUF_SIZE)  len = MAX_BUF_SIZE;
		len = recv(s, tmp_buffer, len);

		//for(i = 0; i < len;i++) MyPrintf("%c", tmp_buffer[i]);
		//MyPrintf("\r\nRecv %d bytes", len);

		
		// Extract Location
		if (stream_protocol == STREAM_WINAMP) {
			ptr_data = memstr(tmp_buffer, "Location: ", len);
			if (ptr_data > 0) {
				// need to another request
				Need_2nd_Req = 1;
				ptr_data +=  17; // skip Location: http://
				memset(tmp_URL, '\0', MAX_URL_SIZE/2);	  
				memcpy(tmp_URL, "wim://",6);
				
				for(i = 0; i < len; i++) {
					
					tmp = *(ptr_data+i);
					if (tmp == '\r') break;
					tmp_URL[6+i] = tmp;
					
				}
				

			}else Need_2nd_Req = 0;
		}
		
		// search CRLF, CRLF
		ptr_data = memstr(tmp_buffer, "\r\n\r\n", len); 

			//MyPrintf("\r\nptr_data = %x, tmp_buffer = %x", ptr_data, &tmp_buffer);
			
		if (ptr_data > 0) {
			ptr_data += 4; // + 0x0d 0x0a 0x0d 0x0a 
			len2 = (u16)&tmp_buffer + len - (u16)ptr_data;

			MyPrintf("\r\n len to copy : %d", len2);
				
			for(i = 0; i < len2; i++) {
				  stream_buffer[stream_buffer_write++] = *(ptr_data + i);
				  //MyPrintf("\r\n[%d] = %02x", stream_buffer_write-1,  stream_buffer[stream_buffer_write-1]);
			}
		}
		
		
	}
	
	return(len);
}


void Parse_HTTP(u16 len)
{
	u8 * ptr, j;
	u16 i, recv_cnt;

//	MyPrintf("\r\n\r\nlen = %d",len);

	// $H
	if (H_flag == 0) {
		H_flag = 1;
		recv_cnt = len -12;		

			memcpy(&http_pak, tmp_buffer, len);
			
			if ((http_pak.command[0] == '$') && (http_pak.command[1] == 'H')) {
				
					ptr = memstr(&http_pak.data[0], &ASF_File_Object[0], recv_cnt);
					if (ptr > 0) {
						ptr += 92;
						memcpy(&H_Size, ptr, 2);
						#ifdef HTTP_DEBUG
							MyPrintf("\r\nH_Size = %d", H_Size);
						#endif
					}
			}else MyPrintf("\r\n$H header missing");
	}else {
			memcpy(&http_pak.data[0], tmp_buffer, len);
			recv_cnt = len;
	}


	for (i =0; i < recv_cnt; i++) {

		if (Http_packet_recv_cnt >= http_pak.tot_len-8) {
			Http_packet_recv_cnt = 0;
			
//			MyPrintf("\r\n [%d]Http_packet_recv_cnt = 0",i);
			
			if ((http_pak.data[i] == '$') && (http_pak.data[i+1] == 'D')) {
				http_pak.tot_len = ((http_pak.data[i+3]<<8) + http_pak.data[i+2]);

				D_Size = http_pak.data[i+18] * 256 + http_pak.data[i+17];
				Padding_Size = H_Size - D_Size;

⌨️ 快捷键说明

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