summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--software/hhd70dongle/c1101lib.c60
-rw-r--r--software/hhd70dongle/spi.c14
-rw-r--r--software/hhd70dongle/spi.h1
-rw-r--r--software/hhd70dongle/util.c2
4 files changed, 48 insertions, 29 deletions
diff --git a/software/hhd70dongle/c1101lib.c b/software/hhd70dongle/c1101lib.c
index 8f9f5a7..cda199b 100644
--- a/software/hhd70dongle/c1101lib.c
+++ b/software/hhd70dongle/c1101lib.c
@@ -40,6 +40,8 @@
/**** Helper Functions ****/
#define SPIC1101_MAX_WAIT 1024
+#define SPIC1101_SEND_MOSTLY_HARMLESS 0
+
unsigned int attempts = 0;
char spi_c1101_exchange(char *data, int len)
{
@@ -51,20 +53,23 @@ char spi_c1101_exchange(char *data, int len)
while (len-- > 0)
{
//~ while ( ! (SPIC1101_SB_CHIPRDY(spi_read_byte())));
- spi_write_byte(*(data++));
+ sb = spi_exchange_byte(*data);
usb_rawhid_send((uint8_t*)"spi byte written",255);
- do
+ debug_sprint_int16hex(debug_sb, sb);
+ usb_rawhid_send(debug_sb,255);
+ while ( ! (SPIC1101_SB_CHIPRDY(sb)));
{
- usb_rawhid_send((uint8_t*)"wait chipready",255);
- sb = spi_read_byte();
+ usb_rawhid_send((uint8_t*)"not yet chipready",255);
+ sb = spi_exchange_byte(*data);
debug_sprint_int16hex(debug_sb, sb);
usb_rawhid_send(debug_sb,255);
if (attempts++ > SPIC1101_MAX_WAIT)
return -1;
- } while ( ! (SPIC1101_SB_CHIPRDY(sb)));
+ }
usb_rawhid_send((uint8_t*)"spi chipready",255);
+ data++;
}
- rbyte = spi_read_byte();
+ rbyte = spi_exchange_byte(SPIC1101_SEND_MOSTLY_HARMLESS);
usb_rawhid_send((uint8_t*)"spi byte read",255);
spi_cs_disable();
return rbyte;
@@ -98,21 +103,29 @@ char spi_c1101_strobe_command(char address)
int spi_c1101_read_rxfifo(int leave_num_bytes, char *buffer, int maxlen)
{
char sb;
+ uint8_t debug_sb[6];
+ attempts = 0;
int num_read = 0;
int num_fifo_available = 0;
spi_cs_enable();
- while ( ! (SPIC1101_SB_CHIPRDY(spi_read_byte())));
- spi_write_byte(SPIC1101_ADDR_FIFO_READ_BURST);
- do
+ sb = spi_exchange_byte(SPIC1101_ADDR_FIFO_READ_BURST);
+ while ( ! (SPIC1101_SB_CHIPRDY(sb)));
{
- sb = spi_read_byte();
- } while ( ! (SPIC1101_SB_CHIPRDY(sb)));
+ usb_rawhid_send((uint8_t*)"not yet chipready",255);
+ sb = spi_exchange_byte(SPIC1101_ADDR_FIFO_READ_BURST);
+ debug_sprint_int16hex(debug_sb, sb);
+ usb_rawhid_send(debug_sb,255);
+ if (attempts++ > SPIC1101_MAX_WAIT)
+ return -1;
+ }
+ usb_rawhid_send((uint8_t*)"spi chipready",255);
num_fifo_available = SPIC1101_SB_FIFO_BYTES_AVAILABLE(sb);
-
+ //note if num_fifo_available == 15 then 15 or more bytes are available
+ //FIXTHIS
while (maxlen-- && num_fifo_available - num_read <= leave_num_bytes)
{
//hope this works !!
- buffer[num_read++] = spi_read_byte();
+ buffer[num_read++] = spi_exchange_byte(SPIC1101_SEND_MOSTLY_HARMLESS);
}
spi_cs_disable();
return num_read;
@@ -122,18 +135,25 @@ int spi_c1101_read_rxfifo(int leave_num_bytes, char *buffer, int maxlen)
int spi_c1101_write_txfifo(char *buffer, int len)
{
char sb;
+ uint8_t debug_sb[6];
+ attempts = 0;
int num_written = 0;
int num_fifo_available = 0;
spi_cs_enable();
- while ( ! (SPIC1101_SB_CHIPRDY(spi_read_byte())));
- spi_write_byte(SPIC1101_ADDR_FIFO_WRITE_BURST);
- do
+ sb = spi_exchange_byte(SPIC1101_ADDR_FIFO_WRITE_BURST);
+ while ( ! (SPIC1101_SB_CHIPRDY(sb)));
{
- sb = spi_read_byte();
- } while ( ! (SPIC1101_SB_CHIPRDY(sb)));
+ usb_rawhid_send((uint8_t*)"not yet chipready",255);
+ sb = spi_exchange_byte(SPIC1101_ADDR_FIFO_WRITE_BURST);
+ debug_sprint_int16hex(debug_sb, sb);
+ usb_rawhid_send(debug_sb,255);
+ if (attempts++ > SPIC1101_MAX_WAIT)
+ return -1;
+ }
+ usb_rawhid_send((uint8_t*)"spi chipready",255);
num_fifo_available = SPIC1101_SB_FIFO_BYTES_AVAILABLE(sb);
-
- //if there is less space in tx-fifo than num-byte we want to send, only fill fifo until full and leave rest of buffer unsent (for now)
+ //note if num_fifo_available == 15 then 15 or more bytes are available
+ //FIXTHIS
if (num_fifo_available < len)
len = num_fifo_available;
diff --git a/software/hhd70dongle/spi.c b/software/hhd70dongle/spi.c
index e676fbf..85831d1 100644
--- a/software/hhd70dongle/spi.c
+++ b/software/hhd70dongle/spi.c
@@ -69,26 +69,24 @@ void spi_cs_disable(void)
PORTB |= (1<<CS);
}
-//synchronous
void spi_write_byte(char byte)
{
SPDR = byte; //Load byte to Data register
while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
}
-//~ void spi_write(char* data, unsigned int len)
-//~ {
- //~ for (unsigned int c=0; c<len; c++)
- //~ spi_write_byte(data[c]);
-//~ }
-
-//MSB first
char spi_read_byte(void)
{
while(SPI_PINB_REG & (1<<MISO)); /* wait for CC1101 to get ready... */
return SPSR;
}
+char spi_exchange_byte(char byte)
+{
+ spi_write_byte(byte);
+ return spi_read_byte();
+}
+
//~ void spi_read(unsigned int maxlen, char *data, unsigned int *len)
//~ {
//~ PORTB |= (1<<CS);
diff --git a/software/hhd70dongle/spi.h b/software/hhd70dongle/spi.h
index 8b45493..cc1102b 100644
--- a/software/hhd70dongle/spi.h
+++ b/software/hhd70dongle/spi.h
@@ -38,6 +38,7 @@ void spi_cs_enable(void);
void spi_cs_disable(void);
void spi_write_byte(char byte);
char spi_read_byte(void);
+char spi_exchange_byte(char byte);
//~ void spi_write(char* data,unsigned int len);
//~ void spi_read(unsigned int maxlen, char *data, unsigned int *len);
diff --git a/software/hhd70dongle/util.c b/software/hhd70dongle/util.c
index b1f7e85..b0e6e89 100644
--- a/software/hhd70dongle/util.c
+++ b/software/hhd70dongle/util.c
@@ -73,7 +73,7 @@ void reset(void)
int16_t adc_read(uint8_t mux)
{
uint8_t low;
- char aref = 0b11000000;
+ char aref = 0b01000000;
ADCSRA = (1<<ADEN) | ADC_PRESCALER; // enable ADC
ADCSRB = (1<<ADHSM) | (mux & 0x20); // high speed mode
ADMUX = aref | (mux & 0x1F); // configure mux input