Question about source code

SpokePOV kit for bikes

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
schenckbrandon
 
Posts: 4
Joined: Thu Jun 26, 2014 4:33 pm

Question about source code

Post by schenckbrandon »

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

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!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Question about source code

Post by adafruit_support_rick »

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

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

Re: Question about source code

Post by schenckbrandon »

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!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Question about source code

Post by adafruit_support_rick »

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

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

Re: Question about source code

Post by schenckbrandon »

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!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Question about source code

Post by adafruit_support_rick »

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.

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

Re: Question about source code

Post by schenckbrandon »

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!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Question about source code

Post by adafruit_support_rick »

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?

Locked
Please be positive and constructive with your questions and comments.

Return to “SpokePOV (discontinued)”