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

📄 main.c

📁 Wiznet iRadio Source
💻 C
字号:
#include "71x_lib.h"
#include "eeprom.h"
#include "w5100.h"
#include "socket.h"
#include "netconfig.h"
#include "util.h"
#include "dhcp.h"
#include "vs1033.h"
#include "ir_rx.h"
#include "dns.h"
#include "lcd.h"
#include "mms.h"
#include <stdio.h> 
#include <stdarg.h> 
#include <stdlib.h> 

#define PORT2_1			1
#define PORT2_2			2
#define PORT2_3			3
#define PORT2_8			8
#define PORT2_9			9
#define CS_1				(1<<PORT2_1) // Port 2.1
#define CS_2				(1<<PORT2_2) // Port 2.2
#define CS_3				(1<<PORT2_3) // Port 2.2
#define INT_2				(1<<PORT2_8) //Port 2.8
#define INT_3				(1<<PORT2_9) //Port 2.9

#define PORT1_9			9
#define EEP_WP			(0x0001 << PORT1_9)	//P1.9

#define UART2_Rx_Pin 		(0x0001<<13)  	// Port 0. 13
#define UART2_Tx_Pin 		(0x0001<<14)  

#define BSPI1_MISO   		(0x0001 << 4)          /*  SPI1: Master in/Slave out data */
#define BSPI1_MOSI   		(0x0001 << 5)          /*  SPI1: Master out/Slave In data */
#define BSPI1_SCLK   		(0x0001 << 6)          /*  SPI1: Serial Clock */
#define BSPI1_SSN     		(0x0001 << 7)          /*  SPI1: SSN */

#define KEEP_ALIVE_TIME		30


u8 DHCPisSuccess = 0;
u16 local_port = 2000;

extern u16 LastChannel;
extern u16 Cur_Ch;
extern CONFIG_MSG Config_Msg;

#ifdef DHCP_ENABLE
	extern u32 my_time;
#endif

extern un_l2cval lease_time;
extern u8 MAC[6];
extern u8 stream_protocol;

//Channel_Info Channel;
extern u8 Ch_buf[MAX_CHINFO_SIZE];	 
extern u8 Server_URL[MAX_URL_SIZE/2]; // 64

extern u8 Need_2nd_Req;
extern u8 tmp_URL[MAX_URL_SIZE/2]; // 64
extern volatile u32 Keep_alive_timer;

// mms
extern u32 packet_length;

void Init_str710(void)
{
	// Port
	// GPIO0 Push-Pull Output
	GPIO0->PC0 = 0xFFFF;
	GPIO0->PC1 = 0x0000;
	GPIO0->PC2 = 0xFFFF;
	// GPIO0 Push-Pull Output
	GPIO1->PC0 = 0xFFFF;
	GPIO1->PC1 = 0x0000;
	GPIO1->PC2 = 0xFFFF;
	// GPIO0 Push-Pull Output
	GPIO2->PC0 = 0xFFFF;
	GPIO2->PC1 = 0x0000;
	GPIO2->PC2 = 0xFFFF;
	
	// Clock
	  //Set Main Freq to 32MHz
	 RCCU_Div2Config(ENABLE);
	 RCCU_PLL1Config(RCCU_PLL1_Mul_12,RCCU_Div_3);
	 RCCU_RCLKSourceConfig(RCCU_PLL1_Output);
	 RCCU_MCLKConfig(RCCU_DEFAULT);	//32M
	 RCCU_FCLKConfig(RCCU_DEFAULT);	//32M for PCLK1
	 RCCU_PCLKConfig(RCCU_RCLK_2);	//16M for PCLK2

	// EMI
	// /CS1 is for Wiznet
	EMI_Config(1, EMI_ENABLE+EMI_WAITSTATE(3)+EMI_SIZE_8);
	// CS2 for SRAM
	EMI_Config(2, EMI_ENABLE+EMI_WAITSTATE(2)+EMI_SIZE_8);
	// CS3 for LCD
	EMI_Config(3, EMI_ENABLE+EMI_WAITSTATE(5)+EMI_SIZE_8);
	
	
	// Alternate function
	GPIO_Config(GPIO2, CS_1, GPIO_AF_PP);
	GPIO_Config(GPIO2, CS_2, GPIO_AF_PP);
	GPIO_Config(GPIO2, CS_3, GPIO_AF_PP);
	
	// External Interrupt W5100
	GPIO_Config(GPIO2, INT_2, GPIO_IN_TRI_CMOS);
	// External Interrupt IR
	GPIO_Config(GPIO2, INT_3, GPIO_IN_TRI_CMOS);

	
	XTI_Init();
	XTI_LineModeConfig( XTI_Line3, XTI_FallingEdge);
	XTI_LineConfig(XTI_Line3, ENABLE);			// Enable the External interrupts on line 2
	XTI_ModeConfig(XTI_Interrupt, ENABLE);						// Set the XTI mode 
	EIC_IRQChannelPriorityConfig(XTI_IRQChannel, 1);	//  Set the XTI IRQ Channel priority to 1
	EIC_IRQChannelConfig(XTI_IRQChannel, ENABLE);		// Enable XTI IRQ Interrupts	
	
	// Timer
	//TIMER 0 configuration
 	TIM_Init(TIM0);										//TIMER 0 init -->RCCU_CR1,2,SR register  
 	TIM_PrescalerConfig(TIM0,0x00);						// Configure the Timer 0 Prescaler
  	TIM_OCMPModeConfig(TIM0, TIM_CHANNEL_A, 0xFA00, TIM_TIMING, TIM_HIGH);	// Ouput_Compare setting
	TIM_ITConfig(TIM0, TIM_OCA_IT, ENABLE);				// Enable the output_compare Interrupt
  	TIM_CounterConfig(TIM0,TIM_START);					// Start Timer 0
  	EIC_IRQChannelPriorityConfig(T0TIMI_IRQChannel,2);	// Set the Timer 0 interrupt chanel priority level to 1
 	EIC_IRQChannelConfig(T0TIMI_IRQChannel,ENABLE);		// Enable the Timer 0 IRQ channel interrupts

	//TIMER 1 configuration
 	TIM_Init(TIM1);										//TIMER 0 init -->RCCU_CR1,2,SR register  
 	TIM_PrescalerConfig(TIM1,16);							
  	TIM_OCMPModeConfig(TIM1, TIM_CHANNEL_A, 100, TIM_TIMING, TIM_HIGH);	// Ouput_Compare setting
	TIM_ITConfig(TIM1, TIM_OCA_IT, ENABLE);				// Enable the output_compare Interrupt
  	TIM_CounterConfig(TIM1,TIM_START);					// Start Timer 0
  	EIC_IRQChannelPriorityConfig(T1TIMI_IRQChannel, 3);	// Set the Timer 0 interrupt chanel priority level to 1
 	EIC_IRQChannelConfig(T1TIMI_IRQChannel, ENABLE);		// Enable the Timer 0 IRQ channel interrupts
	
//	EIC_IRQConfig(ENABLE);							// Enable IRQ interrupts on EIC
	
	//  LCD reset
      GPIO_Config(GPIO0, LCD_RESET, GPIO_OUT_PP);
	
	// EEPROM WP
      GPIO_Config(GPIO1, EEP_WP, GPIO_OUT_PP);
	
	// VS1033_DREQ
      GPIO_Config(GPIO2, VS1033_DREQ, GPIO_IN_TRI_TTL);

  	// Configure GPI00 on mode Alternate function Push Pull
	GPIO_Config ( GPIO0, BSPI1_MISO | BSPI1_MOSI | BSPI1_SCLK, GPIO_AF_PP );
  	// Configure SCLK & SSN  clock and data lines control
  	GPIO_Config ( GPIO2, VS1033_XCS | VS1033_XDCS |BSPI1_SSN | VS1033_RESET, GPIO_OUT_PP );
	
	// Uart
     GPIO_Config(GPIO0, UART2_Tx_Pin, GPIO_AF_PP);
     GPIO_Config(GPIO0, UART2_Rx_Pin, GPIO_IN_TRI_CMOS);

     UART_OnOffConfig(UART2, ENABLE);    	// Turn UARTX on
     UART_FifoConfig (UART2, DISABLE);    		// Disable FIFOs
     UART_FifoReset(UART2, UART_RxFIFO);	// Reset the UART_RxFIFO
     UART_FifoReset(UART2, UART_TxFIFO);	// Reset the UART_TxFIFO
     UART_LoopBackConfig(UART2, DISABLE);    //Disable Loop Back
  
     UART_Config(UART2, 115200, UART_NO_PARITY, UART_1_StopBits, UARTM_8D);
     UART_RxConfig(UART2, ENABLE);   // Enable Rx
	
}



void init_socket(u8 s, u8 * ip, u8 *port)
{
	u16 tmp;

	while(socket(s, Sn_MR_TCP, local_port++, 0x20) == 0) ;

	tmp = (u16)(port[0] << 8) + port[1];
	MyPrintf("\r\nConnect to %d.%d.%d.%d", *ip, *(ip+1), *(ip+2), *(ip+3));
	connect(s, ip, tmp);

}



u8 Need_DNS(u8 * ptr, u8 len)
{
	char tmp[5], i, j, k = 0;
	
	if ((*ptr >= '0') && (*ptr <= '9')) {
		if ((*(ptr+1) >= '0') && (*(ptr+1) <= '9')) {
			
			for (j = 0; j < 4; j++) {
				for ( i = 0; i < 4; i++) {

					tmp[i] = *(ptr + k++);
					
					if (tmp[i] == '.') {
						tmp[i] = '\0';
						Config_Msg.Sip[j] = atoi(tmp);
						memset(tmp, '\0', 5);
						//MyPrintf("[%d] = %d ",j,Config_Msg.Sip[j]);
						break;
					}

					if ((j==3) && (k == len)) {
						Config_Msg.Sip[j] = atoi(tmp);
						//MyPrintf("[%d] = %d ",j,Config_Msg.Sip[j]);
						break;
					}
				}
			}
			
 		return 0;
		}
	}
	return 1;
}


	
int main(void)
{
	s8 state;
	u16 i;
	u8 * ptr = 0;
	u8 gui_buf[20];
	u8 flag_probe;

	Init_str710();
	Init_BSPI();
	Init_I2C0();
	
	GPIO_BitWrite(GPIO1, PORT1_9, 0); //eeprom wp
  
	MyPrintf("\r\nWIZ-iRadio Ver. %d.%d",(u8)(FW_VERSION & 0xFF), (u8)((FW_VERSION & 0xFF00)>>8)); 
	
	
RESTART:

	flag_probe = 0;
	
	// H/W Reset W5100
	iinchip_init();

	// LCD
	lcd_init();
	lcd_area_clear(0, 0, 127, 63);
       lcd_update(0, 63);
	screen_version();

	// Read the All channel Info from Flash
	MyPrintf("\r\nRead Ch. Info from Flash");
	memset(Ch_buf, 0, MAX_CHINFO_SIZE);
	memcpy(Ch_buf, (u8 *)(CHANNEL_ADDR), MAX_CHINFO_SIZE);
	
	// Read network information from EEPROM
	Get_Config();

	Config_Msg.Status = SOCK_CLOSED; 

#ifdef DHCP_ENABLE	
	DHCPisSuccess = 0;
	if (Config_Msg.Dhcp == 1) {
		EIC_IRQConfig(ENABLE);							// Enable IRQ interrupts on EIC
		screen_status(" DHCP","...");
		// DHCP
		DHCPisSuccess = getIP_DHCPS();
		MyPrintf("\r\nleased time : %d sec\r\n", lease_time.lVal);
		my_time = 0;

		if (DHCPisSuccess == 0) {
			screen_status(" DHCP","...Fail");
			//TIM_CounterConfig(TIM0,TIM_STOP);		// Disable Timer 0 (DHCP)
			for (i = 0; i <4; i++) {
				Config_Msg.Lip[i] = 0;
				Config_Msg.Sn[i] = 0;
				Config_Msg.Gw[i] = 0;
			}
		goto RESTART;
			
		}
	}else 
#endif
	{
		//TIM_CounterConfig(TIM0,TIM_STOP);		// Disable Timer 0 (DHCP)
		Set_network();
	}
	EIC_IRQConfig(ENABLE);							// Enable IRQ interrupts on EIC

	memset(gui_buf, '\0', 20);	
	sprintf(gui_buf, "%d.%d.%d.%d",Config_Msg.Lip[0],Config_Msg.Lip[1],Config_Msg.Lip[2],Config_Msg.Lip[3]);
	screen_status("IP:",gui_buf);

	setIMR(0xff);

	// Create UDP socket for network configuration
	socket(SOCK_CONFIG, Sn_MR_UDP, REMOTE_CLIENT_PORT, 0x00);
	socket(SOCK_DHCP, Sn_MR_UDP, DHCP_CLIENT_PORT, 0x00);
	
	 
 	state = -1;
	Keep_alive_timer = 0;
  while(1) 
  {

#ifdef DHCP_ENABLE
  	if ((Config_Msg.Dhcp == 1) && (DHCPisSuccess == 1)) check_DHCP_state();
#endif	


	IR_MainProc();

	if (Recv_ConfigMsg() == REMOTE_SETT) {
		if (Config_Msg.Status == SOCK_ESTABLISHED) {
			disconnect(SOCK_CLIENT);	
			MyPrintf("\r\n Disconnect");
			delay(10);
		}
		
		goto RESTART;
	}

	
	Config_Msg.Status = getSn_SR(SOCK_CLIENT);
	switch(Config_Msg.Status) {
		case SOCK_ESTABLISHED :
			if (state == -1) {
				if (flag_probe == 0) {
					Send_Probe();
					flag_probe = 1;
					state = 0;
				}else state = 0;
			}else if (state == 0) {

				//if (stream_protocol == STREAM_MMS) {
					//mms_handshake();
				//}else {
				
					Send_Request();
					MyPrintf("\r\nSend HTTP request\r\n");
				//}
				state = 1;

				memset(gui_buf, '\0', 20);
				sprintf(gui_buf, "%d.%d.%d.%d",Config_Msg.Sip[0],Config_Msg.Sip[1],Config_Msg.Sip[2],Config_Msg.Sip[3]);
				screen_connection(gui_buf);

			}else if (state == 1) {
				//if (stream_protocol == STREAM_MMS) {
					// receive "mms packet_length"
					//mms_pre_receive();
					//Push_to_VS1033();
					//state = 2;
					
				//}else {
					if (Parse_Reply(SOCK_CLIENT) > 0) state = 2;				
				//}
			}else  if (state == 2) {
				if (Keep_alive_timer > KEEP_ALIVE_TIME) {
					Keep_alive_timer = 0;
					Send_KeepalivePacket(SOCK_CLIENT);
				}
				
				Recv_Stream(SOCK_CLIENT);
				Push_to_VS1033();
			}
			break;
		case SOCK_CLOSE_WAIT :
			disconnect(SOCK_CLIENT);	
			MyPrintf("\r\n Disconnect");
			break;
		case SOCK_CLOSED : 

			if (getSn_RX_RSR(SOCK_CLIENT) > 0) {
				Parse_Reply(SOCK_CLIENT);
			}

			
			close(SOCK_CLIENT);

			 if (Cur_Ch != 0) {
				 if (Need_2nd_Req != 1) { // bypass setting the Server_URL
				 	ptr = Navi_Channel(Cur_Ch);
				 }else {
				 	ptr = tmp_URL;
				 }
				 
				Parse_String(ptr);

				if (Need_DNS(Server_URL, strlen(Server_URL)) == 1) {
					if (dns_query(Server_URL) != -1) {
						//MyPrintf("\r\nDNS ok");
						// create socket & connect to the server
						init_socket(SOCK_CLIENT, Config_Msg.Sip, Config_Msg.Sport);
					}else {
						//MyPrintf("\r\nDNS Fail");
					
					}
						
	 			}else {
					// create socket & connect to the server
					init_socket(SOCK_CLIENT, Config_Msg.Sip, Config_Msg.Sport);
	 			}
			 }
			
  			state = -1;
			break;
		default:
			break;
	}

  }



}





⌨️ 快捷键说明

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