MCPcopy Create free account
hub / github.com/MCUdude/MiniCore / begin

Method begin

avr/libraries/SPI/src/SPI.cpp:26–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24#endif
25
26void SPIClass::begin()
27{
28 uint8_t sreg = SREG;
29 noInterrupts(); // Protect from a scheduler and prevent transactionBegin
30 if (!initialized) {
31 // Set SS to high so a connected chip will be "deselected" by default
32 uint8_t port = digitalPinToPort(SS);
33 uint8_t bit = digitalPinToBitMask(SS);
34 volatile uint8_t *reg = portModeRegister(port);
35
36 // if the SS pin is not already configured as an output
37 // then set it high (to enable the internal pull-up resistor)
38 if(!(*reg & bit)){
39 digitalWrite(SS, HIGH);
40 }
41
42 // When the SS pin is set as OUTPUT, it can be used as
43 // a general purpose output port (it doesn't influence
44 // SPI operations).
45 pinMode(SS, OUTPUT);
46
47 // Warning: if the SS pin ever becomes a LOW INPUT then SPI
48 // automatically switches to Slave, so the data direction of
49 // the SS pin MUST be kept as OUTPUT.
50 SPCR |= _BV(MSTR);
51 SPCR |= _BV(SPE);
52
53 // Set direction register for SCK and MOSI pin.
54 // MISO pin automatically overrides to INPUT.
55 // By doing this AFTER enabling SPI, we avoid accidentally
56 // clocking in a single bit since the lines go directly
57 // from "input" to SPI control.
58 // http://code.google.com/p/arduino/issues/detail?id=888
59 pinMode(SCK, OUTPUT);
60 pinMode(MOSI, OUTPUT);
61 }
62 initialized++; // reference count
63 SREG = sreg;
64}
65
66void SPIClass::end() {
67 uint8_t sreg = SREG;

Callers

nothing calls this directly

Calls 2

digitalWriteFunction · 0.85
pinModeFunction · 0.85

Tested by

no test coverage detected