Developer World
Spresense Arduino Library v3.3.0-b2e6327
SPI.h
Go to the documentation of this file.
1/*
2 * SPI.h - Spresense Arduino SPI library
3 * Copyright 2018 Sony Semiconductor Solutions Corporation
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#ifndef Spi_h
21#define Spi_h
22
38/*
39 This header file maybe inclued in plain C file.
40 To avoid compiling error all C++ stuff should be ignored
41 */
42// #ifdef __cplusplus
43
44#include <sdk/config.h>
45#include <nuttx/config.h>
46#include <nuttx/spi/spi.h>
47#include <Arduino.h>
48
49// SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(),
50// usingInterrupt(), and SPISetting(clock, bitOrder, dataMode)
51#define SPI_HAS_TRANSACTION 1
52
53// SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method
54#define SPI_HAS_NOTUSINGINTERRUPT 1
55
56// SPI_ATOMIC_VERSION means that SPI has atomicity fixes and what version.
57// This way when there is a bug fix you can check this define to alert users
58// of your code if it uses better version of this library.
59// This also implies everything that SPI_HAS_TRANSACTION as documented above is
60// available too.
61#define SPI_ATOMIC_VERSION 1
62
63#define SPI_MODE0 SPIDEV_MODE0
64#define SPI_MODE1 SPIDEV_MODE1
65#define SPI_MODE2 SPIDEV_MODE2
66#define SPI_MODE3 SPIDEV_MODE3
68#define SPI_CLOCK_DIV2 2
69#define SPI_CLOCK_DIV4 4
70#define SPI_CLOCK_DIV8 8
71#define SPI_CLOCK_DIV16 16
72#define SPI_CLOCK_DIV32 32
73#define SPI_CLOCK_DIV64 64
74#define SPI_CLOCK_DIV128 128
84};
85
100public:
108 SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
109 : clock_(clock), bit_order_(bitOrder), data_mode_(dataMode) { }
110
115 : clock_(4000000), bit_order_(MSBFIRST), data_mode_(SPI_MODE0) { }
116
117private:
118 friend class SPIClass;
119 uint32_t clock_;
120 uint8_t bit_order_;
121 uint8_t data_mode_;
122};
123
131class SPIClass {
132public:
140 SPIClass(int port);
141
145 void begin(void);
146
150 void end(void);
151
160
165 void endTransaction(void);
166
173 void setBitOrder(uint8_t bitOrder);
174
181 void setDataMode(uint8_t dataMode);
182
189 void setClockDivider(uint8_t clockDiv);
190
207 void usingInterrupt(uint8_t interruptNumber);
208
220 void notUsingInterrupt(uint8_t interruptNumber);
221
228 uint8_t transfer(uint8_t data);
229
236 uint16_t transfer16(uint16_t data);
237
244 void transfer(void *buf, size_t count);
245
252 void transfer16(void *buf, size_t count);
253
263 void send(void *buf, size_t count);
264
274 void send16(void *buf, size_t count);
275
285 void selectCS(int cs);
286
292 void enableCS();
293
299 void disableCS();
300
301private:
302 int spi_port;
303 uint8_t ref_count;
304 FAR struct spi_dev_s* spi_dev;
305 uint32_t spi_base_clock;
306 uint8_t spi_bit_order;
308 uint8_t spi_transmitting;
310 SpiInterruptMode interrupt_mode;
323 uint16_t interrupt_mask;
324
330 uint16_t interrupt_save;
331
332 int spi3_cs1_enable;
333};
334
335#if defined(CONFIG_CXD56_SPI4) && defined(CONFIG_SPI_EXCHANGE)
336extern SPIClass SPI4;
337
338#ifndef IS_INCLUDED_BY_SPI_CPP
339extern SPIClass SPI;
340#endif
341
342#else
343#error Please enable SPI4 and SPI_EXCHANGE in NuttX
344#endif
345
346#if defined(CONFIG_CXD56_SPI5) && defined(CONFIG_SPI_EXCHANGE)
347extern SPIClass SPI5;
348#endif
349
350#if defined(CONFIG_CXD56_SPI3) && defined(CONFIG_SPI_EXCHANGE)
351extern SPIClass SPI3;
352#endif
353
354// #endif // __cplusplus
355
358#endif // Spi_h
SPI controller.
Definition: SPI.h:131
void setBitOrder(uint8_t bitOrder)
This function is deprecated. New applications should use beginTransaction() to configure SPI settings...
void usingInterrupt(uint8_t interruptNumber)
Register interrupt with the SPI library.
void send16(void *buf, size_t count)
Write 16-bit buffer the SPI bus (only write transfer)
SPIClass(int port)
Create SPIClass object.
void transfer16(void *buf, size_t count)
Write 16-bit data to the SPI bus and also receive data.
void end(void)
Disable the SPI bus.
void setClockDivider(uint8_t clockDiv)
This function is deprecated. New applications should use beginTransaction() to configure SPI settings...
uint16_t transfer16(uint16_t data)
Write 16-bit data to the SPI bus and also receive 16-bit data.
void notUsingInterrupt(uint8_t interruptNumber)
Disable interrupt with the SPI library.
void selectCS(int cs)
Select chip select number (only for SPI3)
void setDataMode(uint8_t dataMode)
This function is deprecated. New applications should use beginTransaction() to configure SPI settings...
void disableCS()
Disable chip select by software (only for SPI3)
void begin(void)
Initialize the SPI library.
void send(void *buf, size_t count)
Write buffer to the SPI bus (only write transfer)
void endTransaction(void)
After performing a group of transfers and releasing the chip select signal, this function allows othe...
void enableCS()
Enable chip select by software (only for SPI3)
void beginTransaction(SPISettings settings)
Before using SPI.transfer() or asserting chip select pins, this function is used to gain exclusive ac...
void transfer(void *buf, size_t count)
Write data to the SPI bus and also receive data.
uint8_t transfer(uint8_t data)
Write 8-bit data to the SPI bus and also receive 8-bit data.
SPI settings.
Definition: SPI.h:99
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
Construct a new SPISettings object.
Definition: SPI.h:108
SPISettings()
Construct a new SPISettings object.
Definition: SPI.h:114
SpiInterruptMode
Spi interrupt mode.
Definition: SPI.h:80
#define SPI_MODE0
Definition: SPI.h:63
@ SPI_INT_MODE_MASK
Definition: SPI.h:82
@ SPI_INT_MODE_GLOBAL
Definition: SPI.h:83
@ SPI_INT_MODE_NONE
Definition: SPI.h:81