atMETEO
An ATmega based weather station
spi.h
Go to the documentation of this file.
1 /*
2  * atMETEO - An ATmega based weather station
3  * Copyright (C) 2014-2015 Christian Fetzer
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #pragma once
21 
38 #include <inttypes.h> // AVR toolchain doesn't offer cinttypes header
39 
40 #include "lib/utils.h"
41 
42 #include "pin.h"
43 
44 namespace Avr
45 {
46 
70 template <class TCsDigitalIo, uint8_t csPinNumber>
71 class Spi
72 {
73 public:
79  static Spi& instance()
80  {
81  return s_instance;
82  }
83 
87  void select()
88  {
89  s_cs.on();
90  }
91 
95  void deselect()
96  {
97  s_cs.off();
98  }
99 
105  uint8_t transceive(uint8_t value)
106  {
107  SPDR = value;
108  while (!(Sensors::bitRead(SPSR, SPIF)));
109  return SPDR;
110  }
111 
116  void transmit(uint8_t value)
117  {
118  transceive(value);
119  }
120 
125  uint8_t receive()
126  {
127  return transceive(0x00);
128  }
129 
130 private:
131  static Spi s_instance;
133 
134  Spi()
135  {
139 
140  deselect();
141  Sensors::bitSet(SPCR, SPE); // Enable SPI
142  Sensors::bitSet(SPCR, MSTR); // SPI Master
143  Sensors::bitSet(SPSR, SPI2X); // Clock rate
144  }
145 };
146 
147 template <class TCsDigitalIo, uint8_t csPinNumber>
150  // \addtogroup libtarget_spi
152 
153 } // namespace Avr
void transmit(uint8_t value)
Sends the byte value over SPI.
Definition: spi.h:116
uint8_t transceive(uint8_t value)
Sends the byte value and receives a byte over SPI.
Definition: spi.h:105
static Spi & instance()
Returns the Avr::Spi instance.
Definition: spi.h:79
void select()
Signals chip select to slave.
Definition: spi.h:87
void on()
Turns output pin on.
Definition: pin.h:160
A C++ wrapper for accessing the built-in SPI communication interface.
Definition: spi.h:71
uint8_t receive()
Receives a byte over SPI.
Definition: spi.h:125
Bit manipulation utilities.
Namespace containing all symbols of the AVR C++ utilities library.
Definition: adc.h:48
Output pin (normal mode).
Definition: pin.h:217
bool bitRead(T &value, uint8_t bit)
Returns the bit in the given value.
Definition: utils.h:92
void bitSet(T &value, uint8_t bit)
Sets the bit in the given value to 1.
Definition: utils.h:54
void deselect()
Signals chip deselect to slave.
Definition: spi.h:95
void off()
Turns output pin off.
Definition: pin.h:165
Input pin.
Definition: pin.h:239
Wrapper for accessing digital I/O ports.