0

Question about source code
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Question about source code

by schenckbrandon on Thu Jun 26, 2014 4:39 pm

This may not be the right forum for a question like this, so I apologize in advance:

Could someone help me understand the purpose of the tx_computer_byte subroutine?

Code: Select all | TOGGLE FULL SIZE
uint8_t tx_computer_byte(uint8_t b) {
  uint8_t v;

  DDRA = 0x0;           // outputs
  DDRB = 0x5F;          // input on MOSI/DI (for SPI), all others output
  USICR = _BV(USIWM0) | _BV(USICS1);
  USIDR = b;                 // transfer 0x0
  USISR = _BV(USIOIF);       // ready
  while (! (USISR & _BV(USIOIF)) ) {
    asm("wdr");

    if (sensor_timer == 0xFFFF) {
      stopcomputertx = 1;
    }
    if (stopcomputertx) {
      break;
    }
  }

  v = USIDR;

  DDRB = 0xDF;
  USICR = 0;

  return v;
}


It seems like this wants to read 1 byte on SPI, but I don't see where the clock source is given that PB7 is defined as an input. Perhaps I just need some help understanding SPI implementation on AVR a bit better.

Thanks!
schenckbrandon
 
Posts: 4
Joined: Thu Jun 26, 2014 4:33 pm

Re: Question about source code

by adafruit_support_rick on Fri Jun 27, 2014 9:31 am

It writes a byte to SPI, then waits for the transfer to complete. It returns the same byte value.

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Question about source code

by schenckbrandon on Fri Jun 27, 2014 10:44 am

adafruit_support_rick wrote:It writes a byte to SPI, then waits for the transfer to complete. It returns the same byte value.


Hi Rick,

Where is the clock source? It looks like USCK (PB7) is declared as an INPUT which would imply we are trying to act as a slave, but I don't see where the clock would come from?

Thanks!
schenckbrandon
 
Posts: 4
Joined: Thu Jun 26, 2014 4:33 pm

Re: Question about source code

by adafruit_support_rick on Fri Jun 27, 2014 11:10 am

The USI hardware takes care of that. You probably ought to have a look at the ATtiny2313 data sheet. It goes into detail on USI operations.
http://www.atmel.com/images/doc2543.pdf

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Question about source code

by schenckbrandon on Fri Jun 27, 2014 12:09 pm

adafruit_support_rick wrote:The USI hardware takes care of that. You probably ought to have a look at the ATtiny2313 data sheet. It goes into detail on USI operations.
http://www.atmel.com/images/doc2543.pdf


Thanks, Rick. Yes, I've read through that entirely and I am somewhat new to this so I appreciate your help explaining. I am also in three-wire mode (USIWM0). With USICS1 set, the shift register/counter reg clock is externally sourced according to Table 61. I am still confused what this external clock source is, and why the PB7 pin is defined as an input? Is there someplace more specific you can point me to in the USI description?

Thanks again!
schenckbrandon
 
Posts: 4
Joined: Thu Jun 26, 2014 4:33 pm

Re: Question about source code

by adafruit_support_rick on Fri Jun 27, 2014 1:33 pm

It's not an input. The alternate port function as SCLK overrides the DDRB setting. It's the clock signal. The "external clock" refers to the source of the clock being used by the SCI hardware. It does not refer to the SCLK signal begin external to the SCI.

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Question about source code

by schenckbrandon on Fri Jun 27, 2014 3:24 pm

adafruit_support_rick wrote:It's not an input. The alternate port function as SCLK overrides the DDRB setting. It's the clock signal. The "external clock" refers to the source of the clock being used by the SCI hardware. It does not refer to the SCLK signal begin external to the SCI.


Ah! I understand now.

So what is the reason for changing the DDRB bit 7 setting '1' and back to '0' at the end of the loop? Why not just leave it alone from the initial ioinit() loop?

Thanks again for your help!
schenckbrandon
 
Posts: 4
Joined: Thu Jun 26, 2014 4:33 pm

Re: Question about source code

by adafruit_support_rick on Fri Jun 27, 2014 8:43 pm

Beats me. They set it to 0 on the way in and back to 1 on the way out. I don't know why. Maybe some other part of the code expects it to be set to output?

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Please be positive and constructive with your questions and comments.