1+ /*
2+ * Copyright (c) 2010 by Cristian Maglie <[email protected] > 3+ * Copyright (c) 2014 by Paul Stoffregen <[email protected] > (Transaction API) 4+ * Copyright (c) 2014 by Matthijs Kooijman <[email protected] > (SPISettings AVR) 5+ * Copyright (c) 2014 by Andrew J. Kroll <[email protected] > (atomicity fixes) 6+ * SPI Master library for arduino.
7+ *
8+ * This file is free software; you can redistribute it and/or modify
9+ * it under the terms of either the GNU General Public License version 2
10+ * or the GNU Lesser General Public License version 2.1, both as
11+ * published by the Free Software Foundation.
12+ */
13+
14+ #pragma once
15+ #include < stdint.h>
16+ #include < stdlib.h>
17+ #include < sys/types.h>
18+
19+ // SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(),
20+ // usingInterrupt(), and SPISetting(clock, bitOrder, dataMode)
21+ #define SPI_HAS_TRANSACTION 1
22+
23+ // SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method
24+ #define SPI_HAS_NOTUSINGINTERRUPT 1
25+
26+ // SPI_ATOMIC_VERSION means that SPI has atomicity fixes and what version.
27+ // This way when there is a bug fix you can check this define to alert users
28+ // of your code if it uses better version of this library.
29+ // This also implies everything that SPI_HAS_TRANSACTION as documented above is
30+ // available too.
31+ #define SPI_ATOMIC_VERSION 1
32+
33+ // Uncomment this line to add detection of mismatched begin/end transactions.
34+ // A mismatch occurs if other libraries fail to use SPI.endTransaction() for
35+ // each SPI.beginTransaction(). Connect an LED to this pin. The LED will turn
36+ // on if any mismatch is ever detected.
37+ // #define SPI_TRANSACTION_MISMATCH_LED 5
38+
39+ #ifndef LSBFIRST
40+ #define LSBFIRST 0
41+ #endif
42+ #ifndef MSBFIRST
43+ #define MSBFIRST 1
44+ #endif
45+
46+ #define SPI_CLOCK_DIV4 0x00
47+ #define SPI_CLOCK_DIV16 0x01
48+ #define SPI_CLOCK_DIV64 0x02
49+ #define SPI_CLOCK_DIV128 0x03
50+ #define SPI_CLOCK_DIV2 0x04
51+ #define SPI_CLOCK_DIV8 0x05
52+ #define SPI_CLOCK_DIV32 0x06
53+
54+ #define SPI_MODE0 0x00
55+ #define SPI_MODE1 0x04
56+ #define SPI_MODE2 0x08
57+ #define SPI_MODE3 0x0C
58+
59+ #define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
60+ #define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
61+ #define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
62+
63+ // define SPI_AVR_EIMSK for AVR boards with external interrupt pins
64+ #if defined(EIMSK)
65+ #define SPI_AVR_EIMSK EIMSK
66+ #elif defined(GICR)
67+ #define SPI_AVR_EIMSK GICR
68+ #elif defined(GIMSK)
69+ #define SPI_AVR_EIMSK GIMSK
70+ #endif
71+
72+ class SPISettings {
73+ private:
74+ uint32_t clock;
75+ uint8_t bitOrder;
76+ uint8_t dataMode;
77+
78+ public:
79+ SPISettings (uint32_t clock, uint8_t bitOrder, uint8_t dataMode): clock(clock), bitOrder(bitOrder), dataMode(dataMode) {}
80+ SPISettings () { SPISettings (4000000 , MSBFIRST, SPI_MODE0); }
81+ friend class SPIClass ;
82+
83+ bool operator ==(const SPISettings &other) const {
84+ return (clock == other.clock ) && (bitOrder == other.bitOrder ) &&
85+ (dataMode == other.dataMode );
86+ }
87+ };
88+
89+ class SPIClass {
90+ public:
91+ // Initialize the SPI library
92+ virtual void begin ();
93+ virtual void end ();
94+
95+ // Before using SPI.transfer() or asserting chip select pins,
96+ // this function is used to gain exclusive access to the SPI bus
97+ // and configure the correct settings.
98+ virtual void beginTransaction (SPISettings settings);
99+
100+ // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
101+ virtual uint8_t transfer (uint8_t data);
102+ virtual void transfer (void *buf, size_t count);
103+
104+ // After performing a group of transfers and releasing the chip select
105+ // signal, this function allows others to access the SPI bus
106+ virtual void endTransaction (void );
107+
108+ // virtual ~SPIClass();
109+
110+ private:
111+ };
112+
113+ extern SPIClass SPI;
0 commit comments