1+ /*
2+ * Copyright (c) 2010 by Cristian Maglie <c.maglie@arduino.cc>
3+ * Copyright (c) 2014 by Paul Stoffregen <paul@pjrc.com> (Transaction API)
4+ * Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
5+ * Copyright (c) 2014 by Andrew J. Kroll <xxxajk@gmail.com> (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+ public:
74+ SPISettings (uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {}
75+ SPISettings () { SPISettings (4000000 , MSBFIRST, SPI_MODE0); }
76+ friend class SPIClass ;
77+ };
78+
79+ class SPIClass {
80+ public:
81+ // Initialize the SPI library
82+ virtual void begin ();
83+ virtual void end ();
84+
85+ // Before using SPI.transfer() or asserting chip select pins,
86+ // this function is used to gain exclusive access to the SPI bus
87+ // and configure the correct settings.
88+ virtual void beginTransaction (SPISettings settings);
89+
90+ // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
91+ virtual uint8_t transfer (uint8_t data);
92+ virtual void transfer (void *buf, size_t count);
93+
94+ // After performing a group of transfers and releasing the chip select
95+ // signal, this function allows others to access the SPI bus
96+ virtual void endTransaction (void );
97+
98+ // virtual ~SPIClass();
99+
100+ private:
101+ };
102+
103+ extern SPIClass SPI;
0 commit comments