summaryrefslogtreecommitdiff
path: root/software/hhd70dongle/spi.c
diff options
context:
space:
mode:
Diffstat (limited to 'software/hhd70dongle/spi.c')
-rw-r--r--software/hhd70dongle/spi.c54
1 files changed, 23 insertions, 31 deletions
diff --git a/software/hhd70dongle/spi.c b/software/hhd70dongle/spi.c
index 85831d1..a2d9c55 100644
--- a/software/hhd70dongle/spi.c
+++ b/software/hhd70dongle/spi.c
@@ -36,20 +36,19 @@
#define SPI_DDR DDRB
#define SPI_PORT PORTB
#define SPI_PINB_REG PINB
-#define CS 0
-#define SCK 1
-#define MOSI 2
-#define MISO 3
-#define GDO2 4
-#define GDO0 5
-#define RE 6
-#define TE 7
+#define CS DDB0
+#define SCK DDB1
+#define MOSI DDB2
+#define MISO DDB3
+#define GDO2 DDB4
+#define GDO0 DDB5
+#define RE DDB6
+#define TE DDB7
void spi_init(void)
{
//configure Direction of SS / PB0 , MOSI and SCLK as Output to drive CS of CC1101
- DDRB = (1<<DDB0) | (1<<DDB1) | (1<<DDB2);
- SPI_PORT = 0;
+ SPI_PORT = (1<<CS);
SPI_DDR = (1<<MOSI)|(1<<SCK)|(1<<CS);
SPCR = (1<<SPE)|(1<<MSTR); // | (0<<DORD) //select MSB first: DORD == 0
// SPSR = (0<<SPI2X) // f_osc/4
@@ -60,42 +59,35 @@ void spi_init(void)
void spi_cs_enable(void)
{
//pull low
- PORTB &= ~(1<<CS);
+ SPI_PORT &= ~(1<<CS);
}
void spi_cs_disable(void)
{
//pull high
- PORTB |= (1<<CS);
+ SPI_PORT |= (1<<CS);
}
-void spi_write_byte(char byte)
+void spi_c1101_wait_chip_rdy(void)
{
- SPDR = byte; //Load byte to Data register
- while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
+ //c1101 will set MISO to low if ready
+ for (unsigned int c=0; c < 0xFFFFFF && (SPI_PINB_REG & (1<<MISO)); c++);
}
-char spi_read_byte(void)
+void spi_write_byte(char byte)
{
- while(SPI_PINB_REG & (1<<MISO)); /* wait for CC1101 to get ready... */
- return SPSR;
+ SPDR = byte; //Load byte to Data register
+ while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
}
char spi_exchange_byte(char byte)
{
spi_write_byte(byte);
- return spi_read_byte();
+ return SPDR;
}
-//~ void spi_read(unsigned int maxlen, char *data, unsigned int *len)
-//~ {
- //~ PORTB |= (1<<CS);
-
- //~ while(SPI_PINB_REG & (1<<MISO)); /* wait for CC1101 to get ready... */
-
- //~ SPDR = 0x80;
- //~ len = 0;
- //~ while(*len < maxlen && !(SPSR & (1<<SPIF)))
- //~ data[*len++]=SPSR;
- //~ PORTB &= ~(1<<CS);
-//~ }
+char spi_read_byte(void)
+{
+ //transmit something so SCLK runs for 8 bits, so that slave can transfer 1 byte
+ return spi_exchange_byte(0xFF);
+} \ No newline at end of file