void spi_reset(); char spi_read(char); char spi_read_rx_buffer (char, char); void spi_write(char, char); void spi_load_tx_buffer(char, char, char, char); void spi_RTS(char, char, char); char spi_read_status(); char spi_rx_status(); void spi_bit_modify(char, char, char); // resete the CAN controller void spi_reset() { PORTB.4 = 0; SPDR = 0b11000000; while(SPSR.7 == 0) { } // clear SPIF flag SPSR.7 = 0; PORTB.4 = 1; } // read a register on the controller char spi_read(char addr) { unsigned char data; // set chip select low SPCR = 0b01010000; PORTB.4 = 0; SPDR = 0b00000011; while (SPSR.7 == 0) { } SPDR = addr; while (SPSR.7 == 0) { } SPDR = 0x00; while (SPSR.7 == 0) { } data = SPDR; // set chip select high PORTB.4 = 1; return data; } // read from an rx buffer register char spi_read_rx_buffer (char n, char m) { // n and m each are a single bit, 1 or 0 unsigned char cmd, data; cmd = 0b10010000 | (n<<2) | (m<<1); SPCR = 0b01010000; PORTB.4 = 0; SPDR = cmd; while (SPSR.7 == 0) { } SPDR = 0x00; while (SPSR.7 == 0) { } data = SPDR; PORTB.4 = 1; return data; } // write to a register void spi_write(char addr, char data) { SPCR = 0b01010000; PORTB.4 = 0; SPDR = 0b00000010; while (SPSR.7 == 0) { } SPDR = addr; while (SPSR.7 == 0) { } SPDR = data; while (SPSR.7 == 0) { } // clear SPIF flag SPSR.7 = 0; PORTB.4 = 1; } // write to a tx buffer register void spi_load_tx_buffer(char a, char b, char c, char data) { // a,b,c, are each 1 bit unsigned char cmd; SPCR = 0b01010000; cmd = 0b01000000 | (a<<2) | (b<<1) | c; PORTB.4 = 0; SPDR = cmd; while (SPSR.7 == 0) { } SPDR = data; while (SPSR.7 == 0) { } SPSR.7 = 0; PORTB.4 = 1; } // send request-to-send command (initiate transmission) void spi_RTS(char two, char one, char zero) { // two, one, zero are each 1 bit unsigned char cmd; SPCR = 0b01010000; cmd = 0b10000000 | (two<<2) | (one<<1) | zero; PORTB.4 = 0; SPDR = cmd; while (SPSR.7 == 0) { } SPSR.7 = 0; PORTB.4 = 1; } // read status bits from the controller char spi_read_status() { unsigned char data; SPCR = 0b01010000; PORTB.4 = 0; SPDR = 0b10100000; while (SPSR.7 == 0) { } SPDR = 0x00; while (SPSR.7 == 0) { } SPDR = 0x00; while (SPSR.7 == 0) { } data = SPDR; PORTB.4 = 1; return data; } // read the rx status bits char spi_rx_status() { unsigned char data; SPCR = 0b01010000; PORTB.4 = 0; SPDR = 0b10110000; while (SPSR.7 == 0) { } SPDR = 0x00; while (SPSR.7 == 0) { } SPDR = 0x00; while (SPSR.7 == 0) { } data = SPDR; PORTB.4 = 1; return data; } // modify one or several bits in a register void spi_bit_modify(char addr, char mask, char data) { SPCR = 0b01010000; PORTB.4 = 0; SPDR = 0b00000101; while (SPSR.7 == 0) { } SPDR = addr; while (SPSR.7 == 0) { } SPDR = mask; while (SPSR.7 == 0) { } SPDR = data; while (SPSR.7 == 0) { } SPSR.7 = 0; PORTB.4 = 1; }