CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

rs232 problems

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Iulian Demetrescu
Guest







rs232 problems
PostPosted: Wed Apr 17, 2002 4:02 pm     Reply with quote

hi to all.
i have a very big problem. i try to use the hardware uart of an 16f877 at 115200 baud. I created the receive and send functions for the uart but after two packets succesfuly received the entire system locks up and i do not know what to do. The ASM counterpart of tis application works fine.
i post below the code.

#include <16F877.h>
#device PIC16F877 *=16 ADC=10
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP,NOCPD,WRT
#zero_ram
#use delay(clock=20000000)

//general var and const definitions


#define BUF_SIZE 96
#define LCP 0xC021 //lcp packet
#define IP 0x0021 //ip packet
#define IPCP 0x8021 //ipcp config packet
#define CCP 0x80FD //compression
#define CHAP 0xC223 //chap authentication
short lcp_up; //lcp negotiated successfuly
short ip_up; //ipcp negotiated succesfuly
short frm_avail;//frame available for decoding
short adcmp; //address and ctl field compression
short err_out; //error out sys
long curfcs; //frame check sequence
int buf_pos; //receive buffer position





//sw uart
#use RS232(BAUD=9600,XMIT=PIN_B1,RCV=PIN_B0)


void put_to_a( char c){
putc(c);
}

char get_from_a ( ) {
return(getc());
}
//hw uart
#use RS232(BAUD=115200,XMIT=PIN_C6,RCV=PIN_C7)

void put_to_b( char b) {
putc(b);
}

char get_from_b ( ) {
return(getc());
}


//clear bank 2 ram buffer
void clr_pos_buf(){
int ilp = 0;
while (ilp <= BUF_SIZE) {
write_bank(1,ilp++,0x00);
}
}

//init and setup modem
void init_zapp_mdm(){
char m;
put_to_b("AT&FV0E0\r"); //say reset to modem
put_to_b("ATV0E0+CRM=1\r"); //init string to modem
//m = get_from_b(); //wait for something from modem
//switch(m){
//case '0' : put_to_a("ok\n\r");break;
//case '4' : put_to_a("error\n\r"); break;
//}
}

void dial_zapp_mdm(){
put_to_b("ATDT#777\r");
}


//signalling states


//routine tested and working
//checksum calculator part 1
long calc(long cs) {
int i; //loop variable
cs&=0xFF;
for (i=0;i<8;i++){
if (cs&1) {
cs /= 2;
cs ^=0x8408;
} else {
cs /=2;
}
}
return cs;
}
//routine tested and working
//checksum calculator part 2
void valid_fcs(){
int l;
char z;
//we should init the fcs engine
for (l=0;l z=read_bank(1,l); //read data from meory
curfcs=calc(z^curfcs)^(curfcs/256);
}
if (curfcs==0xF0B8) {
put_to_a("o");
} else {
put_to_a("w");
}
}




void init_ppp_engine(){
lcp_up = 0;
ip_up = 0;
adcmp=0;
err_out=0; //no exit
//lcp_off();
//ip_off();
curfcs = 0xFFFF;//init fcs value to default
}



//routine tested and running at 17.04.2002
void rcv_packet(){ //main packet receiver routine
char c; //data holder
short in_packet=0;//in frame indicator
short escc=0; //escape char sequence
buf_pos=0;
do { //we stay in this until something arrives and check for conformity
c = get_from_b();//read from port
if (c==0x7E){
in_packet = 1;//we succesfuly received start frame sequence
}
}while(!in_packet);
do {//we stay in here until we reach end of packet
c=get_from_b(); //read from port
if (!lcp_up){
switch(c){
case 0x7E:in_packet=0;break;
case 0x7D:escc=1;break;
default :if (!escc) {write_bank(1,buf_pos++,c);} else {
c^=0x20;
escc=0;
write_bank(1,buf_pos++,c);}
}

}
else {
if(c==0x7E){in_packet=0;} else {write_bank(1,buf_pos++,c);}

}
}while(in_packet);
//when we reach this stage we have a ppp packet decoded in memory

}

void snd_packet(int osz){ //send packet routine
int slp; //send loop char
char z; //mini buffer
curfcs=0xFFFF;//reset fcs
put_to_b(0x7E);//frame start
for (slp=0;slp z=read_bank(1,slp); //read char in buffer
curfcs=calc(z^curfcs)^(curfcs/256); //calculate fcs value
if (z<0x20) {
z ^= 0x20;
put_to_b(0x7D); //send escape sequence
put_to_b(z); //send char
} else {
put_to_b(z);
}
}
curfcs =~curfcs; //ones complement
put_to_b(curfcs&255);
put_to_b(curfcs/256);
put_to_b(0x7E);
}

void decode_packet(){ //main ppp packet decoder
long p_type; //packet type as read from buffer
int offset=0; //offset in packet
char r; //request type

if (!adcmp) {offset=2;}//we don't have address and field compression
p_type=read_bank(1,offset) * 256 + read_bank(1,offset+1);//protocol type
if (p_type == LCP) {

r=read_bank(1,offset+2);//read request type
if (r==0x01) { //we have a request
write_bank(1,offset+2,0x02); //we make it ack
snd_packet(buf_pos - 2); //send the packet minus old fcs
//now we send the request
put_to_b(0x7e);
put_to_b(0xff);
put_to_b(0x7d);
put_to_b(0x23);
put_to_b(0xc0);
put_to_b(0x21);
put_to_b(0x7d);
put_to_b(0x21);
put_to_b(0x7d);
put_to_b(0x22);
put_to_b(0x7d);
put_to_b(0x20);
put_to_b(0x7d);
put_to_b(0x34);
put_to_b(0x7d);
put_to_b(0x22);
put_to_b(0x7d);
put_to_b(0x26);
put_to_b(0x7d);
put_to_b(0x20);
put_to_b(0x7d);
put_to_b(0x20);
put_to_b(0x7d);
put_to_b(0x20);
put_to_b(0x7d);
put_to_b(0x20);
put_to_b(0x7d);
put_to_b(0x25);
put_to_b(0x7d);
put_to_b(0x26);
put_to_b(0xfa);
put_to_b(0x33);
put_to_b(0xa0);
put_to_b(0x7d);
put_to_b(0x32);
put_to_b(0x7d);
put_to_b(0x27);
put_to_b(0x7d);
put_to_b(0x22);
put_to_b(0x7d);
put_to_b(0x28);
put_to_b(0x7d);
put_to_b(0x22);
put_to_b(0x83);
put_to_b(0x95);
put_to_b(0x7e);
}
if (r==0x02){
adcmp=1; //addres and field compression
lcp_up=1; //lcp negotiated

}

if (r==0x09){ //we have an echo request
//put_to_a("E");
write_bank(1,offset+2,0x0A); //we make it echo reply
snd_packet(buf_pos - 2); //send the packet minus old fcs
}


}//end lcp processing
if (p_type == CHAP) {
put_to_a("C");//say chap

}//end chap processing
}




main() {
char s;
put_to_a("Online\n\r");
init_zapp_mdm(); //set modem
dial_zapp_mdm();//dial to zapp host
init_ppp_engine();
while(!err_out) {
clr_pos_buf();
rcv_packet();
decode_packet();
}
put_to_a("end");

}
___________________________
This message was ported from CCS's old forum
Original Post ID: 3862
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: rs232 problems
PostPosted: Wed Apr 17, 2002 4:48 pm     Reply with quote

:= but after two packets succesfuly received the entire system locks up and i do not know what to do.
:=
:=#use RS232(BAUD=115200,XMIT=PIN_C6,RCV=PIN_C7)
----------------------------------------------------

I didn't review your code in detail, but I have this advice:

You can check to see if the hardware USART is locking up by
adding the ERRORS directive. Example:

#use RS232(BAUD=115200,XMIT=PIN_C6,RCV=PIN_C7, ERRORS)

This will clear any over-run or framing errors that occur.
You'll still lose the character, but at least it won't lock up.
___________________________
This message was ported from CCS's old forum
Original Post ID: 3863
Iulian Demetrescu
Guest







Re: rs232 problems
PostPosted: Thu Apr 18, 2002 12:04 am     Reply with quote

Thank you very much. my code is now running ok. i spent an entire night debugging and changing things.CCS is really great.What i did in assembly in many hours and days i did in c in about 5 hours. Thank you CCS.


:=:= but after two packets succesfuly received the entire system locks up and i do not know what to do.
:=:=
:=:=#use RS232(BAUD=115200,XMIT=PIN_C6,RCV=PIN_C7)
:=----------------------------------------------------
:=
:=I didn't review your code in detail, but I have this advice:
:=
:=You can check to see if the hardware USART is locking up by
:=adding the ERRORS directive. Example:
:=
:=#use RS232(BAUD=115200,XMIT=PIN_C6,RCV=PIN_C7, ERRORS)
:=
:=This will clear any over-run or framing errors that occur.
:=You'll still lose the character, but at least it won't lock up.
___________________________
This message was ported from CCS's old forum
Original Post ID: 3869
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group