Add SPI driver
This commit is contained in:
parent
cec47de6ea
commit
4c9558852a
23
drivers/spi.c
Normal file
23
drivers/spi.c
Normal file
@ -0,0 +1,23 @@
|
||||
#include "spi.h"
|
||||
#include <avr/io.h>
|
||||
|
||||
uint8_t spi_init(uint8_t spcr) {
|
||||
PORTB |= (1 << 5); // set SCK hi
|
||||
DDRB |= (1 << 5); // set SCK as output
|
||||
DDRB &= ~(1 << 4); // set MISO as input
|
||||
DDRB |= (1 << 3); // set MOSI as output
|
||||
DDRB |= (1 << 2); // SS must be output for Master mode to work
|
||||
|
||||
SPCR = spcr | SPE;
|
||||
return(SPSR);
|
||||
}
|
||||
|
||||
uint8_t spi_transfer(uint8_t in) {
|
||||
uint8_t v;
|
||||
SPDR = in;
|
||||
// wait for transfer to complete
|
||||
while(! (SPSR & (1<<SPIF)) ) {};
|
||||
v = SPDR;
|
||||
return(v);
|
||||
}
|
||||
|
4
drivers/spi.h
Normal file
4
drivers/spi.h
Normal file
@ -0,0 +1,4 @@
|
||||
#include <stdint.h>
|
||||
|
||||
uint8_t spi_init(uint8_t spcr);
|
||||
uint8_t spi_transfer(uint8_t in);
|
Loading…
Reference in New Issue
Block a user