void send_byte(unsigned char date) { unsigned char i, temp; temp = date; for(i = 0; i < 8; i ++) { if(temp&0x80) { sda = 1; } else { sda = 0; } _nop_(); scl = 1; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); scl = 0; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); temp <<= 1; } checkACK(); }
/** * Send command to the slave and delay extra 10 instruction cycles at the end * * Examples of command * Write for sending data to the slave and the slave will perform flash erase and write * Read for reading the desired segment from the slave * ID for reading the device ID of the slave * CONFIG for sending configuration data to the slave * * Input : command is the instruction for the slave * * Output : 1 for OK * 0 for failure as the master did not receive ACK from the slave * */ uint8 spiSendCommand(uint8 command) { uint8 acknack,dummy; spiConfigureMaster(); spiSendByte(command); acknack = ReadSPI(); if(!checkACK(&acknack)) return 0 ; //return 0 for error CloseSPI(); Delay10TCYx(1); return 1 ; }
//------------------------------------------------------ int wait_ack(int ackNo){ int status; do{ status = inportb(LSR) & 0x01; if(getTimer()==0){ // TIME OUT -> Send new frame printf("TIME-OUT\n"); return 1; // Not receive ACK } }while (status!=0x01); //Repeat until get Something if(!checkACK(inportb(TXDATA),ackNo)){ return 1; // Fail to receive ACK } return 0; // Receive ACK & Send Complete }
/** * Send any amount of data through spi and always check for the slave ACK * Delay extra 10 instruction cycles at the end of sending. * * Input : *data is the pointer to the data array * count is the amount of data in the array * startPoint is the starting location of the data array to access * */ uint8 spiSendData(uint8 *data,uint8 count,uint8 startPoint) { uint8 acknack , i , j = startPoint; spiConfigureMaster(); for ( i = 0 ; i < count ; i ++ ) { spiSendByte(data[j]); acknack = ReadSPI(); j++; if(!checkACK(&acknack)) return 0 ; //return 0 for error } CloseSPI(); Delay10TCYx(1); return 1 ; //return 1 for OK }