arduino-emulator
Loading...
Searching...
No Matches
HardwareSPI.h
1/*
2 HardwareSPI.h - Hardware SPI interface for Arduino
3 Copyright (c) 2018 Arduino LLC. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library 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 GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#pragma once
21
22#include "Common.h"
23#include <inttypes.h>
24#include "Stream.h"
25
26#define SPI_HAS_TRANSACTION
27
28namespace arduino {
29
30typedef enum {
31 SPI_MODE0 = 0,
32 SPI_MODE1 = 1,
33 SPI_MODE2 = 2,
34 SPI_MODE3 = 3,
35} SPIMode;
36
37// Platforms should define SPI_HAS_PERIPHERAL_MODE if SPI peripheral
38// mode is supported, to allow applications to check whether peripheral
39// mode is available or not.
40typedef enum {
41 SPI_CONTROLLER = 0,
42 SPI_PERIPHERAL = 1,
43} SPIBusMode;
44
45
47 public:
48 SPISettings(uint32_t clock, BitOrder bitOrder, SPIMode dataMode, SPIBusMode busMode = SPI_CONTROLLER) {
50 init_AlwaysInline(clock, bitOrder, dataMode, busMode);
51 } else {
52 init_MightInline(clock, bitOrder, dataMode, busMode);
53 }
54 }
55
56 SPISettings(uint32_t clock, BitOrder bitOrder, int dataMode, SPIBusMode busMode = SPI_CONTROLLER) {
58 init_AlwaysInline(clock, bitOrder, (SPIMode)dataMode, busMode);
59 } else {
60 init_MightInline(clock, bitOrder, (SPIMode)dataMode, busMode);
61 }
62 }
63
64 // Default speed set to 4MHz, SPI mode set to MODE 0 and Bit order set to MSB first.
65 SPISettings() { init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0, SPI_CONTROLLER); }
66
67 bool operator==(const SPISettings& rhs) const
68 {
69 if ((this->clockFreq == rhs.clockFreq) &&
70 (this->bitOrder == rhs.bitOrder) &&
71 (this->dataMode == rhs.dataMode) &&
72 (this->busMode == rhs.busMode)) {
73 return true;
74 }
75 return false;
76 }
77
78 bool operator!=(const SPISettings& rhs) const
79 {
80 return !(*this == rhs);
81 }
82
83 uint32_t getClockFreq() const {
84 return clockFreq;
85 }
86 SPIMode getDataMode() const {
87 return dataMode;
88 }
89 BitOrder getBitOrder() const {
90 return (bitOrder);
91 }
92 SPIBusMode getBusMode() const {
93 return busMode;
94 }
95
96 private:
97 void init_MightInline(uint32_t clock, BitOrder bitOrder, SPIMode dataMode, SPIBusMode busMode) {
98 init_AlwaysInline(clock, bitOrder, dataMode, busMode);
99 }
100
101 // Core developer MUST use an helper function in beginTransaction() to use this data
102 void init_AlwaysInline(uint32_t clock, BitOrder bitOrder, SPIMode dataMode, SPIBusMode busMode) __attribute__((__always_inline__)) {
103 this->clockFreq = clock;
104 this->dataMode = dataMode;
105 this->bitOrder = bitOrder;
106 this->busMode = busMode;
107 }
108
109 uint32_t clockFreq;
110 SPIMode dataMode;
111 BitOrder bitOrder;
112 SPIBusMode busMode;
113
114 friend class HardwareSPI;
115};
116
117const SPISettings DEFAULT_SPI_SETTINGS = SPISettings();
118
120{
121 public:
122 virtual ~HardwareSPI() { }
123
124 virtual uint8_t transfer(uint8_t data) = 0;
125 virtual uint16_t transfer16(uint16_t data) = 0;
126 virtual void transfer(void *buf, size_t count) = 0;
127
128 // Transaction Functions
129 virtual void usingInterrupt(int interruptNumber) = 0;
130 virtual void notUsingInterrupt(int interruptNumber) = 0;
131 virtual void beginTransaction(SPISettings settings) = 0;
132 virtual void endTransaction(void) = 0;
133
134 // SPI Configuration methods
135 virtual void attachInterrupt() = 0;
136 virtual void detachInterrupt() = 0;
137
138 virtual void begin() = 0;
139 virtual void end() = 0;
140};
141
142// Alias SPIClass to HardwareSPI since it's already the defacto standard for SPI class name
143typedef HardwareSPI SPIClass;
144
145}
Definition DMAPool.h:103
Definition HardwareSPI.h:120
Definition HardwareSPI.h:46
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31