📄 s7600.h
字号:
int1 to;
char c;
} tcp_getc_data;
tcp_getc_data tcp_getc_timeout(int8 socket, int16 to) {
tcp_getc_data ret;
ret.to=1;
while (to) {
if (tcp_kbhit(socket)) {
ret.to=0;
ret.c=tcp_getc(socket);
return(ret);
}
delay_ms(1);
to--;
}
return(ret);
}
/********************************************************************
//
// function: tcp_connected
//
// description: if a socket has been opened into server mode, this will return true
// if a host is trying to connect to our PIC.
//
// input: socket (socket which to check if there is a server connection)
// output: boolean; true if connected, false if not
// calls: s7600_DMA_Write(), s7600_DMA_Read()
//
//*******************************************************************/
int1 tcp_connected(int8 socket)
{
int8 status;
int1 rc;
s7600_DMA_Write(SOCKET_SELECT_REG, socket);
status=s7600_DMA_Read(SOCKET_STATUS_REG+1);
if ((status & 0x10) && (!(status & 0x20)) && (!(status & 0x40)))
rc=1;
else
rc=0;
return(rc);
}
/********************************************************************
//
// function: tcp_getd
//
// description: same as tcp_getc(), but an extra parameter has been added to
// read a variable amount of data from the tcpip receive buffer.
// NOTE: this performs no checks as to whether the socket is valid
// or the socket is connected. NOTE: also, this will wait indefinately until
// data arrives, just like getc(). using tcp_kbhit() will check for
// valid socket, if it is connected, and will not wait indefinately. just like kbhit().
//
// NOTE: if less data than len is in buffer it will only pull that data, will not wait until len is filled
//
// input: socket (socket which to get data from), data (pointer to place to store received characters),
// len (length of data to pull from the buffer).
// output: contents pointed to data modified.
// calls: s7600_DMA_Write(), s7600_DMA_Read()
//
//*******************************************************************/
void tcp_getd(int8 socket, int8 * data, int16 len)
{
int8 status;
int16 size=0;
s7600_DMA_Write(SOCKET_SELECT_REG, socket);
do {
status=s7600_DMA_Read(SOCKET_STATUS_REG);
} while (!(status & 0x10));
size = s7600_DMA_Read(SOCKET_BUFFER_IN_SIZE + 1);
size <<= 8;
size += s7600_DMA_Read(SOCKET_BUFFER_IN_SIZE + 0);
if( size > len )
size = len;
while (len--) {
*data=s7600_DMA_Read(SOCKET_DATA_REG);
data++;
}
}
/********************************************************************
//
// function: tcp_flush_socket
//
// description: clears all the data from the tcpip receive buffer to make
// room for new data
//
// input: socket (socket which buffer to clear)
// output: none
// calls: tcp_kbhit(), tcp_getc()
//
//*******************************************************************/
void tcp_flush_socket(int8 socket) {
while (tcp_kbhit(socket)) {
tcp_getc(socket);
}
}
/********************************************************************
//
// function: tcp_kbhit
//
// description: checks to see if there is data in the receive buffer of the tcpip stack
//
// input: socket (socket which to check)
// output: boolean; true if valid data available for this socket, false if not
// calls: s7600_DMA_Read(), s7600_DMA_Write()
//
//*******************************************************************/
int1 tcp_kbhit(int8 socket)
{
int8 status,tstatus;
int1 rc=0;
if (socket < MAX_SOCKET) {
s7600_DMA_Write(SOCKET_SELECT_REG, socket);
status=s7600_DMA_Read(SOCKET_STATUS_REG);
tstatus=s7600_DMA_Read(SOCKET_STATUS_REG + 1) & 0x0F;
//readable states are ESTABLISHED, CLOSE_WAIT, FIN_WAIT1 and FIN_WAIT2
if ( ( (tstatus == 0x02) || (tstatus == 0x03) || (tstatus == 0x05) || (tstatus == 0x06) ) && ( status & 0x10 ) )
rc=1; //data available
}
return(rc);
}
/********************************************************************
//
// function: tcp_putc
//
// description: puts out a character on the tcpip stack, PIC -> host. just like putc()
//
// input: socket (socket which to put data out), c (character to send)
// output: s7600_ec (error code type which gives a reason for error)
// calls: s7600_DMA_Write(), s7600_DMA_Read()
//
//*******************************************************************/
s7600_ec tcp_putc(int8 socket, int8 c)
{
int8 status;
int16 space;
if (socket > MAX_SOCKET) {return(INVALID_SOCKET);} //fail if an invalid socket
s7600_DMA_Write(SOCKET_SELECT_REG,socket); //address to proper socket
status=s7600_DMA_Read(SOCKET_STATUS_REG+1) & 0x0F; //0x23
if ( (status != 0x02) && (status != 0x03) ) {return(SOCKET_NOT_CONNECTED);} //connection not established or socket closed
//find space left on the sucket buffer
space = s7600_DMA_Read(SOCKET_BUFFER_OUT_SIZE+1);
space <<= 8;
space += s7600_DMA_Read(SOCKET_BUFFER_OUT_SIZE);
if (!space) {return(NO_BUFFER_SPACE);} //not enough space to send a char
else {
s7600_DMA_Write(SOCKET_DATA_REG, c);
s7600_DMA_Write(SOCKET_BUFFER_OUT_SIZE, 0);
return(OK);
}
}
/********************************************************************
//
// function: tcp_putd
//
// description: puts out data onto the tcpip stack, pic -> host, like tcp_putc but has
// this sends an array of data instead of just one byte.
//
// input: socket (socket which to put data out), c (pointer to array of char),
// len (number of bytes to send)
// output: s7600_ec (error code type which gives a reason for error)
// calls: s7600_DMA_Write(), s7600_DMA_Read()
//
//*******************************************************************/
s7600_ec tcp_putd(int8 socket, int8 * c, int16 len)
{
int8 status;
int16 space;
if (socket > MAX_SOCKET) {return(INVALID_SOCKET);} //fail if an invalid socket
//address to proper socket
s7600_DMA_Write(SOCKET_SELECT_REG,socket);
status=s7600_DMA_Read(SOCKET_STATUS_REG+1) & 0x0F; //0x23
if ( (status != 0x02) && (status != 0x03) ) {return(SOCKET_NOT_CONNECTED);} //connection not established or socket closed
//find space left on the sucket buffer
space = s7600_DMA_Read(SOCKET_BUFFER_OUT_SIZE+1);
space <<= 8;
space += s7600_DMA_Read(SOCKET_BUFFER_OUT_SIZE);
if (space < len) {return(NO_BUFFER_SPACE);} //not enough space to send a char
else {
// debug("\r\nTCP_PUTD: ");
while (len) {
s7600_DMA_Write(SOCKET_DATA_REG, *c);
// debug("%c",*c);
c++;
len--;
}
s7600_DMA_Write(SOCKET_BUFFER_OUT_SIZE, 0);
return(OK);
}
}
/********************************************************************
//
// function: tcp_close
//
// description: closes a connection between PIC and Host.
//
// input: socket (socket to close)
// output: s7600_ec (error code type which gives a reason for error)
// calls: s7600_DMA_Write(), s7600_DMA_Read()
//
//*******************************************************************/
s7600_ec tcp_close(int8 socket)
{
short int closed=0;
int16 to=0;
int8 state;
int1 a_close = 0;
if (socket > MAX_SOCKET) {return(INVALID_SOCKET);} //invalid socket
//address to proper socket
s7600_DMA_Write(SOCKET_SELECT_REG,socket);
while ( (to < TCP_TIMEOUT) && (!closed) ) {
state = s7600_DMA_Read(SOCKET_STATUS_REG + 1) & 0x0F; //0x22 + 1
if (a_close) {
if ( (state == 5) || (state == 6) || (state == 7) || (state == 0) || (state == 8) || (state == 4) ) {return(OK);}
}
else if ( (state == 0) || (state == 9) || (state == 8) ) { //not connected, but disable socket anyways
s7600_DMA_Write(SOCKET_ACTIVATE_REG, s7600_DMA_Read(SOCKET_ACTIVATE_REG) & ~(1 << socket) );
return(OK);
}
else if ( (state == 0x0A) || (state == 1) || (state == 2) || (state == 3) ) {
s7600_DMA_Write(SOCKET_ACTIVATE_REG, s7600_DMA_Read(SOCKET_ACTIVATE_REG) & ~(1 << socket) );
a_close=1;
}
else if ( (state == 5) || (state == 6) || (state == 7) || (state == 4) ) {}
delay_ms(1);
}
if ( (to) && (!closed) ) {return(SOCKET_CLOSE_TIMEOUT);} //we timed out
}
/********************************************************************
//
// function: tcp_find_free_socket
//
// description: finds the next available free socket that can be opened, or 0xFF if none is available
//
// input: none
// output: the next free socket, or 0xFF if none is available
// calls: s7600_DMA_Read()
//
//*******************************************************************/
int8 tcp_find_free_socket(void) {
int8 avail,i;
int8 socket=1;
avail=s7600_DMA_Read(SOCKET_ACTIVATE_REG); //bits are set if they are closed
for (i=0;i<MAX_SOCKET;i++) {
if (socket & ~avail) {return(i);} //open socket
else {socket = socket << 1;} //move mask
}
return(0xFF); //no open socket
}
/********************************************************************
//
// function: ppp_check
//
// description: determines if there currently is a PPP connection to the internet or not
//
// input: none
// output: boolean; true if PPP connection, false if not
// calls: s7600_DMA_Read()
//
//*******************************************************************/
int1 ppp_check(void)
{
if (s7600_DMA_Read(IR_PPP_STATUS_REG) & 0x01)
return(1);
else
return(0);
}
/********************************************************************
//
// function: ppp_connect
//
// description: makes a PPP connection to the internet. dials the modem and logs into the ISP
//
// input: phone_number (an array of characters that has the phone number to dial, null terminated),
// username (an array of characters that has the username to login with, null terminated),
// password (an array of characters that has the password to login with, null terminated)
// *NOTE* - Some ISPs need a capitol P in front of the username to start a PPP connection. example Pusername
// output: s7600_ec (error code type which gives a reason for error)
// MyIPAddr, a global variable, is also modified
// calls: modem_connect(), s7600_DMA_Write(), s7600_DMA_Read()
//
//*******************************************************************/
s7600_ec ppp_connect(char * phone_number, char * username, char * password)
{
int16 timeout;
int8 tmp,ip1,ip2,ip3,ip4,retry;
s7600_ec connect_status;
char *c;
#if USE_HWFC
s7600_DMA_Write(IR_PORT_STAT_REG,0x20); //regain control and config communication port
#else
s7600_DMA_Write(IR_PORT_STAT_REG,0x00); //regain control and config communication port
#endif
s7600_DMA_Write(IR_PPP_STATUS_REG, 0x01); //IR_PPP_CONFIG_SW_RESET
timeout=PPP_RESET_TIMEOUT;
while (((s7600_DMA_Read(IR_PPP_STATUS_REG) & 0x01) == 0x01) && timeout) {
timeout--;
delay_ms(1);
}
if (!timeout) {return(PPP_FROZEN);}
s7600_DMA_Write(IR_PPP_MAX_RETRY,0x0F);
retry=MODEM_CONNECT_RETRY;
while(retry != 0) {
connect_status = modem_connect(phone_number);
if (connect_status == MODEM_CONNECTED) {break;}
else {delay_ms(REDIAL_DELAY);retry--;}
}
if (connect_status != MODEM_CONNECTED) {return(connect_status);}
#IF USE_PAP
#IF USE_HWFC
s7600_DMA_Write(IR_PORT_STAT_REG,0x21);//release control and config communication port.
#ELSE
s7600_DMA_Write(IR_PORT_STAT_REG,0x01);//release control and config communication port.
#ENDIF
s7600_DMA_Write(IR_PPP_STATUS_REG,1);
s7600_DMA_Write(IR_PPP_STATUS_REG,0);
s7600_DMA_Write(0x6f,0); //not documented
s7600_DMA_Write(IR_PPP_STATUS_REG,0x20); //IR_PPP_CONFIG_PAP_ENBL
tmp = strlen(username);
if (0 == tmp) {return 0;}
s7600_DMA_WRITE(IR_PAP_STRING, tmp);
c=username;
while (tmp--) {
s7600_DMA_WRITE(IR_PAP_STRING,*c);
c++;
}
tmp = strlen(password);
s7600_DMA_WRITE(IR_PAP_STRING, tmp);
c=password;
while (tmp--) {
s7600_DMA_WRITE(IR_PAP_STRING,*c);
c++;
}
s7600_DMA_WRITE(IR_PAP_STRING,0);
s7600_DMA_WRITE(IR_PPP_STATUS_REG,0x62); //start PPP negotiations (0x6A turns on PPP interrupts, which seiko does in their code.)
#ELSE
delay_ms(10000);
printf(s7600_Serial_Write,"%s\n",username);
delay_ms(3000);
printf(s7600_Serial_Write,"%s\n",password);
delay_ms(1000);
#IF USE_HWFC
s7600_DMA_Write(IR_PORT_STAT_REG,0x21);//release control and config communication port.
#ELSE
s7600_DMA_Write(IR_PORT_STAT_REG,0x01);//release control and config communication port.
#ENDIF
s7600_DMA_Write(IR_PPP_STATUS_REG,0x42); //start PPP negotiations (0x4A turns on PPP interrupts, which seiko does in their code.)
#ENDIF
timeout=PPP_CONNECT_TIMEOUT;
while ( (timeout != 0) && ((s7600_DMA_Read(IR_PPP_STATUS_REG) & 0x01) != 0x01) ) {
timeout--;
delay_ms(1);
}
if (timeout == 0) {modem_disconnect(); return(TIMEOUT);} //we didn't connect
else { //we connected
ip1=s7600_DMA_Read(IR_OUR_IP+0);
ip2=s7600_DMA_Read(IR_OUR_IP+1);
ip3=s7600_DMA_Read(IR_OUR_IP+2);
ip4=s7600_DMA_Read(IR_OUR_IP+3);
MyIPAddr=make32(ip4,ip3,ip2,ip1);
s7600_free_sockets();
return(OK);
}
}
/********************************************************************
//
// function: modem_connect
//
// description: dials a phone number and makes a modem connection. (does not establish a PPP connection)
//
// input: phone_number (an array of char that contains the phone number to dial, null terminated)
// output: s7600_ec (error code type which gives a reason for error)
// calls: s7600_Serial_Write(), modem_response(), s7600_DMA_Write(), s7600_DMA_Read()
//
//*******************************************************************/
s7600_ec modem_connect(char * phone_number)
{
int8 status;
status=s7600_DMA_Read(IR_PORT_STAT_REG);
if ((status & 0x40) != 0x40) {return(NO_CARRIER);}
s7600_Serial_Flush();
printf(s7600_Serial_Write,"%s\r\n",MODEM_INIT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -