avr/spi/spi.c

43 lines
959 B
C
Raw Normal View History

#include <spi.h>
#include <util/delay.h>
void initialize_spi(spi_t *spi) {
set_pin_direction(&spi->clock, PIN_OUT);
set_pin_direction(&spi->data_out, PIN_OUT);
set_pin_direction(&spi->data_in, PIN_IN);
set_pin_direction(&spi->chip_select, PIN_OUT);
}
uint8_t transfer_byte(spi_t *spi, uint8_t output) {
uint8_t input = 0;
int input_bit;
set_pin(&spi->chip_select);
for (int i = 7; i >= 0; i--) {
clear_pin(&spi->clock);
if (output & _BV(i)) {
set_pin(&spi->data_out);
} else {
clear_pin(&spi->data_out);
}
_delay_us(10);
set_pin(&spi->clock);
_delay_us(10);
//input_bit = read_pin(&spi->data_in);
//input <<= input_bit;
}
clear_pin(&spi->clock);
_delay_us(10);
clear_pin(&spi->chip_select);
return input;
}
/*
void send_bytes(spi_t *line, u8 *byte, size_t length, u8 *input, size_t input_length) {
}
*/