diff options
Diffstat (limited to 'software/hhd70dongle/c1101lib.c')
-rw-r--r-- | software/hhd70dongle/c1101lib.c | 60 |
1 files changed, 40 insertions, 20 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; |