/* rf.c*/ #include "../shared.h" #include "door.h" #include "rf.h" /* buffers */ static unsigned char in_buffer [IN_BUFFER_SIZE]; static unsigned char out_buffer [OUT_BUFFER_SIZE]; /* total # of bytes received into in_buffer */ static unsigned char bytes_received = 0; /* # of bytes sent from out_buffer */ static unsigned char bytes_sent = 0; /* total # of bytes to send from out_buffer */ static unsigned char bytes_to_send = 0; /* # of 10ms before timeout */ static unsigned char overflow_count = 0; /* time out flag */ static bit timed_out = 0; char value = 0x01; char xdata *ledaddress = 0x0f000; // the FPGA is configured to memory map led to this address static void switch_to(unsigned char nextState) { TR0 = 0; TR1 = 0; bytes_to_send = 0; state = nextState; } void RF_Task() { P1 = 0x00; *ledaddress = 0x5B; //3 RF_init(); //generate request packet format_request(out_buffer); //send address byte of packet to test if RF board is busy bytes_received = 0; bytes_sent = 0; bytes_to_send = 1; set_timeout(5); TI = 1; //wait till RF board is NOT busy while(1) { // *ledaddress = 0x52;//7 if (bytes_received > 0) { if(in_buffer[0] == out_buffer[0]) { unset_timeout(); //address byte echoed; send the rest of packet bytes_to_send = HEADER_LENGTH + REQUEST_LENGTH; //assert(bytes_sent == 1); TI = 1; break; } bytes_received = 0; } else if (timed_out) { //test again bytes_sent = 0; set_timeout(5); TI = 1; } } //wait till whole packet has been sent while(bytes_sent < bytes_to_send) ; *ledaddress = 0x3A; //4 bytes_received = 0; //wait for reply packet (includes BadgeID) set_timeout(50);//0.5 sec time-out while(1) { if(bytes_received >= HEADER_LENGTH + RESPONSE_LENGTH) { unset_timeout(); //pass badgeID to FPGA for checking badgeID = in_buffer[HEADER_LENGTH + RESPONSE_LENGTH -1 -1]; P1 = badgeID; switch_to(CHECKING_ID); *ledaddress = 0x6B; //5 // *ledaddress = badgeID; // to be deleted later return; } else if(timed_out) { unset_timeout(); // switch_to(DETECTING_USER); *ledaddress = 0x6F; //6 return; } } }/* end RF_Task */ static void set_timeout(unsigned char n) { overflow_count = n; timed_out = 0; TH0 = RF_RELOAD_HI; TL0 = RF_RELOAD_LO; TR0 = 1; } static void unset_timeout() { TR0 = 0; TF0 = 0; timed_out = 0; } void RF_Timer0_handler() { if(--overflow_count==0) { timed_out = 1; TR0 = 0; } TH0 = RF_RELOAD_HI; TL0 = RF_RELOAD_LO; } //end of RF_Timer0_handler() void RF_Serial_handler() { if(RI==1) { RI = 0; if(bytes_received < IN_BUFFER_SIZE) in_buffer[bytes_received++] = SBUF; } else { TI = 0; if(bytes_sent < bytes_to_send) SBUF = out_buffer[bytes_sent++]; } } /* end of RF_Serial_handler() */ /* create the request packet in out_buffer */ static void format_request(char* out_buffer) { out_buffer[0] = BADGE_ADDRESS; out_buffer[0] <<= 4; out_buffer[0] |= 4; out_buffer[1] = 8; //telemetry mode out_buffer[2] = REQUEST_LENGTH; //out_buffer[3] = REQUEST_CODE; out_buffer[3] = 2; //out_buffer[4] = REQUEST_CODE; out_buffer[4] = badgeID; out_buffer[5] = 3; } //end of format_reply static void RF_init() { /* interrupt settings */ EA = 1; ET0 = 1; ET1 = 0; ES = 1; /* set serial */ SM0 = 0; SM1 = 1; REN = 1; /* setup timer0 as a 10 ms counter, timer1 as 19.2 kbps baud rate generator, assuming a 25MHz clock */ TMOD = 0x21; PCON = PCON | 0x80; TH0 = RF_RELOAD_HI; TL0 = RF_RELOAD_LO; TH1 = 249; TR1 = 1; }/* end of RF_init() */