char spiX_readreg(char RegCommand) { char val; CSN_H(); delay_ms(2); CSN_L(); spiX_put(RegCommand); spiX_wait(); spiX_get(); delay_ms(3); spiX_put(0); spiX_wait(); val = spiX_get(); CSN_H(); return val; }
uint8_t spi_xfer(uint8_t b) { spiX_put(b); spiX_wait(); return spiX_get(); }
int main(void) { usi_enable(); spiX_initslave(SPIMODE); sei(); //Initialize slave slave_init(); //Let settings catch up _delay_ms(100); //An initial packet, not sure what it's for, but the other code had it prepare_packet("", 0); spiX_put(0); unsigned char input; do{ //Wait for SS while(!slave_selected()); //Wait for pending transfers spiX_wait(); //Read first character from master input = spiX_get(); //Is the master telling us to receive? //If so, interpret the input and prepare a response packet if(input == RECEIVE_CHAR) { //Retrieve the rest of the packet from master receive_packet(); //Check integrity if(!check_integrity()) { prep_err(); continue; } //Lowercase are read operations if(incoming_packet[1] > 96 && incoming_packet[1] < 127) { prep_response(incoming_packet[1]); } else { do_action(incoming_packet[1]); } } //Is the master telling us that it's ready to receive? else if(input == SEND_CHAR) { //Send the prepared packet send_packet(); } if(waiting_measure){ slave_run_measure(); waiting_measure = 0; } if(waiting_write){ slave_apply(); waiting_write = 0; } } while(1); // Loop forever... }