Файл: Embedded system development and labs for ARM (R. Muresan, 2005).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 13.06.2025

Просмотров: 2342

Скачиваний: 4

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Embedded Systems Development and Labs; The English Edition

Stream Socket (SOCK_STREAM). The stream socket provides a reliable stream oriented communication connection. It uses the TCP protocol and guarantees the correct data transmission and series number checking.

Datagram Socket (SOCK_DGRAM). The Datagram Socket defines a non-connection service. Data is transferred independently and the order is not relevant. It doesn’t guarantee that the data transmission is reliable and correct. It uses the UDP protocol.

Original Socket. The original socket allows the user to directly use the lower level protocol (IP or ICMP for example). The original socket is powerful but not convenient to use. It is mostly used in developing other protocols.

The Socket programming flow diagram is shown at Figure 6-17.

Server End

Create a server-end socket

Server end socket binds to a port

Listen to the connection requests from clients

Program blocks until the client send a connection request

Receive data from clients

Process the data

Client End

Create a client-end socket

Send a connection request via the server IP address and the server port number

Send data to server

Send the data to clients

Receive data

Figure 6-17 Socket Programming Flow Diagram

The most commonly used Socket interface functions are the following:

socket() -- Create a socket

bind() -- specify a local address

253


Embedded Systems Development and Labs; The English Edition

connect() -- connect to a socket accept() -- wait for a socket connection listen() -- listening connection

send() -- send data recv() -- receive data

select() -- input/output multi channels multiplexing closesocket() -- close socket

A standard server-end data receiving sample program is as following:

#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h>

#define MYPORT 4950 /* the port users will be sending to */ #define MAXBUFLEN 100

void main()

{

int sockfd;

struct sockaddr_in my_addr; /* my address information */

struct sockaddr_in their_addr; /* connector's address information */ int addr_len, numbytes;

char buf[MAXBUFLEN];

254

Embedded Systems Development and Labs; The English Edition

if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 )

{

perror("socket");

exit(1);

}

my_addr.sin_family = AF_INET; /* host byte order */ my_addr.sin_port = htons(MYPORT); /* short, network byte order */ my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */ bzero(&(my_addr.sin_zero),; /* zero the rest of the struct */

if( bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1 )

{

perror("bind");

exit(1);

}

addr_len = sizeof(struct sockaddr);

if ( (numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, \ (struct sockaddr *)&their_addr, &addr_len)) == -1 )

{

perror("recvfrom");

exit(1);

}

printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr)); printf("packet is %d bytes long\n",numbytes);

buf[numbytes] = '\0';

printf("packet contains \"%s\"\n",buf); close(sockfd);

}

255


Embedded Systems Development and Labs; The English Edition

(2) Transmission Layer Specific Interface Programming Methods

Network protocols can provide specific function call interfaces for higher layer/interlayer protocols/applications. Users can call the specific interfaces provided by the protocol source code to implement a fast data transfer. The Embest development board provides TFTP protocol specific interface functions. The user can use this interface to receive data from the host computer. The main interface functions are the following:

TftpRecv(int *len) receives data. The network library automatically finishes the connection process. The maximum length of each reception is determined by “len” parameter. Before the function returns, the “len” will be changed by actual length of received data. The function returns a pointer value to the first address of data. If it returns Null, it means that a communication error has happed.

MakeAnswer() Every time after the data has been processed, this function should be called in order to send a acknowledge signal to the host. The receiving of the ACK will allow the transmission to continue.

6.2.5 Operational Steps

(1)Prepare the Lab environment. Connect the Embest Emulator to the target board. Connect the target board UART0 to the PC serial port using the serial cable that comes with the Embest development system. Connect the network port to the hub through a network cable. Connect the network port of PC to the hub through a network cable.

(2)Set the IP address of PC as 192.192.192.x (x is within the 30-200 range). Reboot the PC to make the IP address valid.

(3)Run PC DOS window or select StartÆRun at the desktop, input the command:

arp –s 192.192.192.7 00-06-98-01-7e-8f

(4)Connect the Embest Emulator to the target board. Open the TFTP_Test.ews project file in the TFTP_Test sub directory in the sample directory. After compiling and linking, connect to the target board and download the program.

(5)Run the TFTPDown.exe on PC, input the target board address 192.192.192.7. Input the address 0x30000 at the Flash Start Address. Select the file that needs to be downloaded (bin, elf, etc. maximum 1M). Click the Download button. The program will download the file into the flash of the target board using the TFTP protocol. Success or error message will be prompted at the dialog box.

(6)Stop the target board run the Embest IDE. Open the Memory window and display the content from address 0x30000. Check if the data in flash is consistent with the downloaded file.

(7)After understanding the functionality of the lab, finish the Lab exercises.

Sample Programs void Tftp_Test()

{

char* pData;

unsigned long write_addr;

256

Embedded Systems Development and Labs; The English Edition

char input_string[64];

char tmp_ip[4] = {0,0,0,0};

int

tmp,len,i,j,num=0;

int

b10 =0; int b100 =0; int flag=0;

NicInit();

//Initialize the Ethernet driver

NetInit();

//Initialize the Network protocol

Uart_Printf("\n Do you want to configure local IP ?\n");

Uart_Printf(" Y/y to configure local IP addr; D/d to use Default IP addr(192.168.0.200).\n"); Uart_Printf(" Press any key to continue ...\n");

Uart_Printf(" ( %c )",i = Uart_Getch()); if( i == 'Y' || i == 'y') {

Uart_Printf(" Please input IP address(xxx.xxx.xxx.xxx) then press ENTER:\n");

for( i = 16; i != 0; i--) input_string[i] = 0xaa;

Uart_GetString(&input_string);

for( i = 0;((i <16)&(input_string[i] != 0xAA)); i++) if(input_string[i] == '.') num +=1;

if(num != 3) flag = 1; else

{

num = i - 2; j =0;

for( i = num; i >= 0; i--)

{

if(input_string[i] != '.' )

{

if((input_string[i] < '0' | input_string[i] > '9')) flag = 1; else

{

tmp = (input_string[i] - 0x30); if (b100) { tmp *=100; b10 =0; }

if (b10) { tmp *= 10; b100 =1;}

b10 = 1;

if(tmp < 256) tmp_ip[j] += tmp; else local_ip = 0x4dc0c0c0;

}

257


Embedded Systems Development and Labs; The English Edition

}else { j++; b10 =0; b100 =0;}

}

}

if(!flag)

{

Uart_Printf("\nManual Set local ip %d.%d.%d.%d\n", tmp_ip[3],tmp_ip[2],tmp_ip[1],tmp_ip[0] );

local_ip = ((tmp_ip[0]<<24))+((tmp_ip[1]<<16))\ +((tmp_ip[2]<<8))+tmp_ip[3];

}else

Uart_Printf("\nIP address error (xxx.xxx.xxx.xxx)!\n");

}// yes

else if(i == 'D' || i == 'd') {

local_ip = 0xc800a8c0;

// config local ip 192.168.0.200

Uart_Printf("\nDefault Set local ip %d.%d.%d.%d\n", local_ip&0x000000FF,(local_ip&0x0000FF00)>>8, (local_ip&0x00FF0000)>>16,(local_ip&0xFF000000)>>24 );

}

Uart_Printf("\nPress any key to exit ...\n");

for( ; ; )

{

if( Uart_GetKey() ) return;

pData = (char *)TftpRecv(&len); //receive data if( (pData == 0) || (len <= 0) )

continue;

write_addr = (pData[0])+(pData[1]<<8)+(pData[2]<<16)+(pData[3]<<24); pData = pData + sizeof(long);

if( Program(write_addr,pData,len-4) == FALSE ) // write data to the flash

{

258

Embedded Systems Development and Labs; The English Edition

continue;

}

MakeAnswer(); //answer to the TFTP protocol

}

}

Exercises

Rewrite the TFTP_test sample program; change the IP address of the development board and change the download address of flash; redo the Lab and check if the downloaded data is correct.

6.3 IIS Voice Interface Lab

6.3.1 Purpose

Get familiar with the principles of IIS (Inter-IC Sound) interface.

Learn the programming techniques of the S3C44B0 IIS interface.

6.3.2 Lab Equipment

● Hardware: Embest S3CEV40 hardware platform, Embest Standard/Power Emulator, PC. ● Software: Embest IDE 2003, Windows 98/2000/NT/XP operation system.

6.3.3 Content of the Lab

Write a program that plays a wav file that is stored in the memory.

6.3.4 Principles of the Lab 1) Digital Sound Basics

In digital voice systems, the analog voice signal is converted into a series of binary digital data and then after transmission the digital data will be converted back into an analog signal. One of the devices used in this process is the A/D converter (ADC). The ADC samples the sound signal at a rate of thousands of samples per second every time it records a status of the sound wave. This record is called a sample.

The number of samples per second is called the sample frequency. The unit of measure for the sample frequency is Hz. The higher the sample frequency, the higher the frequency of sound wave that can be described. The number of bits per sample is call sample precision. The sample frequency and sample precision determines the quality of the recovered sound. The frequency range of human’s hearing is 20-20Khz. According to Nequest Law, if the sampling frequency of a sine wave is two times greater than the frequency of the wave the sine wave can be accurately reproduced. As a result, a sampling frequency higher than 40KHz is sufficient to maintain a good digital-to-analog conversion quality of the sound.

2) Voice Coding

PCM (Pulse Code Modulation) is used for sampling the voice signal and coding each sample. ITU-T 64kb/s standard G.711 is based on the PCM method. The sample frequency is 8khz. Each sample is coded with nonlinear u law or A law. The speed is 64 kb/s.

259


Embedded Systems Development and Labs; The English Edition

The CD voice using PCM coding, the sample frequency is 44khz, every sample uses 16-bit coding.

The PCM voice file used in Windows is a wav format file t.wav that uses 44.100 kHz sample frequency, 16 bit code dimensional sound stereo.

Other coding methods include ADPCM (Adaptive Differential Pulse Code Modulation), LPC (Linear Predictive Coding) and LD-CELP (Low Delay – Code Excited Linear Prediction) etc.

The current trend of coding format includes MP3 (MPEG Audio Layer 3), WMA (Windows Media Audio) and RA (Real Audio). Some features of these coding formats is that they are used on the network, support playing while reading, etc.

2. IIS Voice Interface

IIS is a serial bus design technology developed by SONY, Philips, etc. It is an interface standard for voice processing technology and devices such as CD, digital voice processors, etc. IIS separates the clock signal from the voice signal in order to avoid the clock jitter.

IIS processes only voice data. Other data (such as control signals) are transferred separately. IIS bus has only 3 serial lines that are: time multiplexing Serial Data (SD) line, Word Selection (WS) line, and Continuous Serial Clock (CSK) line.

The IIS system interconnection diagram is shown in Figure 6-18.

Figure 6-18 IIS System Interconnection Diagram

The basic IIS signal diagram is resented in Figure 6-19.

Figure 6-19. IIS time signal diagram.

WSD signal line indicates what channel (left or right) will be used for data transfer. SD signal line enables the

260