Arduino Yun cheat sheet: Difference between revisions

 
Line 575: Line 575:
{
{
   uint8_t sreg = SREG;
   uint8_t sreg = SREG;
   noInterrupts(); // Protect from a scheduler and prevent transactionBegin                                                  
   noInterrupts(); // Protect from a scheduler and prevent transactionBegin
   if (!initialized) {
   if (!initialized) {
     // Set SS to high so a connected chip will be "deselected" by default                                                    
     // Set SS to high so a connected chip will be "deselected" by default
     uint8_t port = digitalPinToPort(SS);
     uint8_t port = digitalPinToPort(SS);
     uint8_t bit = digitalPinToBitMask(SS);
     uint8_t bit = digitalPinToBitMask(SS);
     volatile uint8_t *reg = portModeRegister(port);
     volatile uint8_t *reg = portModeRegister(port);


     // if the SS pin is not already configured as an output                                                                  
     // if the SS pin is not already configured as an output
     // then set it high (to enable the internal pull-up resistor)                                                            
     // then set it high (to enable the internal pull-up resistor)
     if(!(*reg & bit)){
     if(!(*reg & bit)){
       digitalWrite(SS, HIGH);
       digitalWrite(SS, HIGH);
     }
     }


     // When the SS pin is set as OUTPUT, it can be used as                                                                  
     // When the SS pin is set as OUTPUT, it can be used as
     // a general purpose output port (it doesn't influence                                                                  
     // a general purpose output port (it doesn't influence
     // SPI operations).                                                                                                      
     // SPI operations).
     pinMode(SS, OUTPUT);
     pinMode(SS, OUTPUT);


     // Warning: if the SS pin ever becomes a LOW INPUT then SPI                                                              
     // Warning: if the SS pin ever becomes a LOW INPUT then SPI
     // automatically switches to Slave, so the data direction of                                                            
     // automatically switches to Slave, so the data direction of
     // the SS pin MUST be kept as OUTPUT.                                                                                    
     // the SS pin MUST be kept as OUTPUT.
     SPCR |= _BV(MSTR);
     SPCR |= _BV(MSTR);
     SPCR |= _BV(SPE);
     SPCR |= _BV(SPE);