/*------------------------------------------- send.c contains code that will send data to a board that contains the receive.c code data flow is as follows: send port receive port data (0..3) <--> data (0..3) ack_img(4) --> ack_img(4) ack_ram(5) --> ack_ram(5) req_img(6) <-- req_img(6) req_ram(7) <-- req_ram(7) --------------------------------------------*/ #include <90s8515.h> #include #define SEND_PORT PORTB #define SEND_PIN PINB #define SEND_DIR DDRB #define ACK_IMG SEND_PORT.4 #define ACK_RAM SEND_PORT.5 #define REQ_IMG SEND_PIN.6 #define REQ_RAM SEND_PIN.7 #define AND_MASK 0x0f #define TRUE 1 #define FALSE 0 #define STORE 0 #define RETRIEVE 1 unsigned char sector[512]; unsigned char mode=STORE; unsigned int loc=0; // send_init() MUST be called before send is ever called void send_init(){ SEND_PORT = 0x00; // clear the send port, no pull-ups SEND_DIR = 0x30; // control pins are output delay_ms(100); // delay for other board to turn on } void send_img_nibble(unsigned char data){ // output the data SEND_PORT = data & AND_MASK; ACK_IMG = TRUE; // acknowledge the request while(REQ_IMG == TRUE){} // wait for the other board to grab data ACK_IMG = FALSE; // drop the ack } void send_ram_nibble(unsigned char data){ // output the data SEND_PORT = data & AND_MASK; ACK_RAM = TRUE; // acknowledge the request while(REQ_RAM == TRUE){} // wait for the other board to grab data ACK_RAM = FALSE; // drop the ack } unsigned char get_ram_nibble(){ // get the data unsigned char ret; ret = SEND_PIN & AND_MASK; ACK_RAM = TRUE; // acknowledge the receipt while(REQ_RAM == TRUE){} // wait for other board to drop req ACK_RAM = FALSE; // drop the ack return ret; } void ram_check(){ if(REQ_RAM == TRUE){ if(mode == STORE){ PORTD.4=0; sector[loc] = (get_ram_nibble() << 4) & 0xf0; while(REQ_RAM == FALSE){} sector[loc] = sector[loc] | (get_ram_nibble() & 0x0f); loc++; if(loc == 512){ mode = RETRIEVE; loc = 0; } } else { PORTD.4=1; SEND_DIR = 0x3f; send_ram_nibble(sector[loc] >> 4); while(REQ_RAM == FALSE){} send_ram_nibble(sector[loc]); SEND_DIR = 0x30; loc++; if(loc == 512){ mode = STORE; loc = 0; } } } } void send(unsigned char data){ while(REQ_IMG == FALSE){ ram_check(); } SEND_DIR = 0x3f; send_img_nibble(data >> 4); while(REQ_IMG == FALSE){} send_img_nibble(data); SEND_DIR = 0x30; }