FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
spi.cpp
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "boards/board_traits.h"
16#include "spi.h"
17
18#ifdef SPDR
19// Handle SPI for ATmega
21{
22 synchronized
23 {
24 using board_traits::SPI_trait;
25 // Set MOSI and SCK as Output
26 // Set MISO as Input (high impedance)
27 // Also set SS as Output (mandatory for Master SPI as per Atmel datasheet)
28 SPI_trait::DDR = (SPI_trait::DDR & bits::CBV8(SPI_trait::MISO))
29 | bits::BV8(SPI_trait::MOSI, SPI_trait::SCK, SPI_trait::SS);
30 // Set MISO as pullup and force MOSI and SCK low
31 SPI_trait::PORT = (SPI_trait::PORT | bits::BV8(SPI_trait::MISO))
32 & ~(bits::BV8(SPI_trait::MOSI, SPI_trait::SCK));
33 }
34}
35#else
36// Handle USI for ATtiny
37void spi::init()
38{
39 synchronized
40 {
41 using board_traits::SPI_trait;
42 // Set MOSI and SCK as Output
43 // Set MISO as Input (high impedance)
44 SPI_trait::DDR = (SPI_trait::DDR & bits::CBV8(SPI_trait::MISO))
45 | bits::BV8(SPI_trait::MOSI, SPI_trait::SCK);
46 // Set MISO as pullup and force MOSI and SCK low
47 //TODO not sure this is really needed
48 // SPI_trait::PORT = (SPI_trait::PORT | bits::BV8(SPI_trait::MISO)) &
49 // ~(bits::BV8(SPI_trait::MOSI, SPI_trait::SCK));
50 }
51}
52#endif
static constexpr uint8_t CBV8(uint8_t bit)
Create a uint8_t inverted bitmask for the given bit number.
Definition: bits.h:137
static constexpr uint8_t BV8(uint8_t bit)
Create a uint8_t bitmask for the given bit number.
Definition: bits.h:41
void init()
This function must be called once in your program, before any use of an SPI device.
Definition: spi.cpp:20
SPI support for AVR MCU.