tabby/avr/link.c

71 lines
1.2 KiB
C

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
#include <tabby/printer.h>
#include <tabby/avr/uart.h>
#include <tabby/avr.h>
void tabby_avr_link_init_master() {
SC_OUTPUT();
SO_OUTPUT();
SI_INPUT();
SI_PULLUP();
SC_HIGH();
SO_LOW();
}
void tabby_avr_link_init_slave() {
SS_INPUT();
SC_INPUT();
SI_INPUT();
SO_OUTPUT();
/*
* Set SPI slave mode, and shift in/out most significant bit first
*/
SPCR &= ~((1 << MSTR) | (1 << DORD));
/*
* Enable SPI in Mode 3 with interrupts, clock nominally high, output on
* falling edge, input on rising edge
*/
SPCR |= (1 << CPOL) | (1 << CPHA) | (1 << SPIE) | (1 << SPE);
/*
* Initialize the SPI Data Register and serial buffer
*/
SPDR = 0;
}
uint8_t tabby_avr_link_send_byte(uint8_t value) {
uint8_t i, ret = 0;
for (i=0; i<8; i++) {
SC_LOW();
if (value & 0x80) {
SO_HIGH();
} else {
SO_LOW();
}
_delay_us(SPI_PULSE_USEC);
value <<= 1;
ret <<= 1;
SC_HIGH();
if (SI_IS_HIGH()) {
ret |= 1;
}
_delay_us(SPI_PULSE_USEC);
}
return ret;
}