65#ifndef _SPI_H_INCLUDED
66#define _SPI_H_INCLUDED
71#include "utility/spi_com.h"
79#define SPI_HAS_TRANSACTION 1
89#define SPI_CLOCK_DIV2 2
90#define SPI_CLOCK_DIV4 4
91#define SPI_CLOCK_DIV8 8
92#define SPI_CLOCK_DIV16 16
93#define SPI_CLOCK_DIV32 32
94#define SPI_CLOCK_DIV64 64
95#define SPI_CLOCK_DIV128 128
102#define SPI_MODE0 0x00
103#define SPI_MODE1 0x01
104#define SPI_MODE2 0x02
105#define SPI_MODE3 0x03
112#define SPI_TRANSMITRECEIVE 0x0
113#define SPI_TRANSMITONLY 0x1
126#define CS_PIN_CONTROLLED_BY_USER NUM_DIGITAL_PINS
129#define NO_CONFIG ((int16_t)(-1))
136#ifndef SPI_TRANSFER_TIMEOUT
137 #define SPI_TRANSFER_TIMEOUT 1000
144#ifndef NB_SPI_SETTINGS
145 #define NB_SPI_SETTINGS 4
189 clk(SPI_SPEED_CLOCK_DEFAULT),
235 SPIClass(uint32_t mosi, uint32_t miso, uint32_t sclk, uint32_t ssel = PNUM_NOT_DEFINED);
248 _spi.pin_miso = digitalPinToPinName(miso);
253 _spi.pin_mosi = digitalPinToPinName(mosi);
258 _spi.pin_sclk = digitalPinToPinName(sclk);
267 _spi.pin_ssel = digitalPinToPinName(ssel);
273 _spi.pin_miso = (miso);
278 _spi.pin_mosi = (mosi);
283 _spi.pin_sclk = (sclk);
288 _spi.pin_ssel = (ssel);
538 return &(
_spi.handle);
551 int16_t _CSPinConfig;
558 uint8_t pinIdx(uint8_t _pin, pin_option_t option)
562 if ((_pin > NUM_DIGITAL_PINS) && (!digitalPinIsValid(_pin))) {
567 if (_pin == spiSettings[i].pinCS) {
572 if (option == ADD_NEW_PIN) {
574 if (spiSettings[i].pinCS == -1) {
575 spiSettings[i].pinCS = _pin;
583 void RemovePin(uint8_t _pin)
585 if ((_pin > NUM_DIGITAL_PINS) && (!digitalPinIsValid(_pin))) {
590 if (spiSettings[i].pinCS == _pin) {
591 spiSettings[i].pinCS = -1;
592 spiSettings[i].clk = SPI_SPEED_CLOCK_DEFAULT;
593 spiSettings[i].bOrder = MSBFIRST;
594 spiSettings[i].dMode = SPI_MODE_0;
599 void RemoveAllPin(
void)
602 spiSettings[i].pinCS = -1;
603 spiSettings[i].clk = SPI_SPEED_CLOCK_DEFAULT;
604 spiSettings[i].bOrder = MSBFIRST;
605 spiSettings[i].dMode = SPI_MODE_0;
616#if defined(SUBGHZSPI_BASE)
617class SUBGHZSPIClass :
public SPIClass {
619 SUBGHZSPIClass():
SPIClass{NC, NC, NC, NC}
621 _spi.spi = SUBGHZSPI;
630 void enableDebugPins(uint32_t mosi = DEBUG_SUBGHZSPI_MOSI, uint32_t miso = DEBUG_SUBGHZSPI_MISO, uint32_t sclk = DEBUG_SUBGHZSPI_SCLK, uint32_t ssel = DEBUG_SUBGHZSPI_SS);
An SPI master: the Arduino SPI API on one of the chip's SPI peripherals (the global SPI on this board...
Definition SPI.h:217
void setSCLK(uint32_t sclk)
Sets the clock pin.
Definition SPI.h:256
void transfer(void *_bufout, void *_bufin, size_t _count, SPITransferMode _mode=SPI_LAST)
Sends one buffer and receives into another (CS driven by the sketch).
Definition SPI.h:454
void setDataMode(uint8_t _pin, uint8_t)
Sets the mode for a CS pin.
void attachInterrupt(void)
Does nothing (kept for compatibility).
spi_t _spi
the instance's pins, peripheral and WCH init structure
Definition SPI.h:543
void usingInterrupt(uint8_t interruptNumber)
Does nothing (kept for compatibility): transfers are never guarded against interrupts.
byte transfer(uint8_t _data, SPITransferMode _mode=SPI_LAST)
Sends one byte and returns the byte received meanwhile (CS driven by the sketch).
Definition SPI.h:416
virtual byte transfer(uint8_t pin, uint8_t _data, SPITransferMode _mode=SPI_LAST)
Sends one byte and returns the byte received meanwhile.
uint16_t transfer16(uint16_t _data, SPITransferMode _mode=SPI_LAST)
Sends 16 bits and returns the 16 bits received (CS driven by the sketch).
Definition SPI.h:429
void transfer(void *_buf, size_t _count, SPITransferMode _mode=SPI_LAST)
Sends a buffer, replacing its bytes with the bytes received (CS driven by the sketch).
Definition SPI.h:441
void detachInterrupt(void)
Does nothing (kept for compatibility).
void end(void)
Stops the SPI: resets SPI1, switches its clock off and forgets every pin's settings.
SPIClass()
An SPI on the variant's default pins: MOSI, MISO and SCK (PA7, PA6 and PA5 on this board),...
virtual void transfer(uint8_t pin, void *_buf, size_t _count, SPITransferMode _mode=SPI_LAST)
Sends a buffer, replacing its bytes with the bytes received.
void setMOSI(uint32_t mosi)
Sets the MOSI pin.
Definition SPI.h:251
void endTransaction(uint8_t pin)
Forgets the pin's settings: transfers for it do nothing until the next begin() or beginTransaction() ...
virtual void begin(uint8_t _pin=CS_PIN_CONTROLLED_BY_USER)
Starts the SPI with the pin's settings (the defaults the first time: 3 MHz, MSB first,...
void setClockDivider(uint8_t _pin, uint8_t)
Sets the clock for a CS pin as a divider of 48 MHz.
void setMISO(PinName miso)
Sets the MISO pin.
Definition SPI.h:271
void setMISO(uint32_t miso)
Sets the MISO pin.
Definition SPI.h:246
virtual void beginTransaction(uint8_t pin, SPISettings settings)
Stores a device's settings for a CS pin and programs SPI1 with them.
void endTransaction(void)
Forgets the settings of the sketch-driven CS: transfer() does nothing until the next begin() or begin...
Definition SPI.h:347
void setSCLK(PinName sclk)
Sets the clock pin.
Definition SPI.h:281
SPI_HandleTypeDef * getHandle(void)
The WCH peripheral library's view of this SPI: its instance and init structure, for mixing in WCH's o...
Definition SPI.h:536
void setSSEL(uint32_t ssel)
Sets a hardware chip select, driven by the peripheral itself.
Definition SPI.h:265
void setBitOrder(BitOrder _order)
Sets the bit order.
Definition SPI.h:479
void setMOSI(PinName mosi)
Sets the MOSI pin.
Definition SPI.h:276
void setDataMode(uint8_t _mode)
Sets the mode.
Definition SPI.h:492
void beginTransaction(SPISettings settings)
Programs SPI1 with a device's settings, for a device whose CS the sketch drives itself.
Definition SPI.h:332
virtual uint16_t transfer16(uint8_t pin, uint16_t _data, SPITransferMode _mode=SPI_LAST)
Sends 16 bits as two bytes and returns the 16 bits received.
SPIClass(uint32_t mosi, uint32_t miso, uint32_t sclk, uint32_t ssel=PNUM_NOT_DEFINED)
An SPI on the given pins, which must all belong to the same SPI peripheral (on the CH32X035 only SPI1...
virtual void transfer(byte _pin, void *_bufout, void *_bufin, size_t _count, SPITransferMode _mode=SPI_LAST)
Sends one buffer and receives into another.
void setSSEL(PinName ssel)
Sets a hardware chip select.
Definition SPI.h:286
void setClockDivider(uint8_t _div)
Sets the clock as a divider of 48 MHz.
Definition SPI.h:511
void setBitOrder(uint8_t _pin, BitOrder)
Sets the bit order for a CS pin.
A device's SPI settings: its maximum clock, bit order and mode, for SPIClass::beginTransaction().
Definition SPI.h:156
constexpr SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode, bool noRecv=SPI_TRANSMITRECEIVE)
Settings for a device.
Definition SPI.h:170
constexpr SPISettings()
The default settings: 4 MHz requested (3 MHz on the wire), MSB first, SPI_MODE0, send and receive.
Definition SPI.h:187
SPITransferMode
What a transfer does with a CS pin the class drives, once it is done.
Definition SPI.h:117
SPIClass SPI
The SPI: SPI1 on PA5 (SCK), PA6 (MISO) and PA7 (MOSI), the bus the board's panel and SD card are on.
#define NB_SPI_SETTINGS
The number of CS pins whose settings an SPIClass remembers, 1 to 254 (default 4).
Definition SPI.h:145
#define SPI_MODE3
clock idles high, data sampled on the rising edge (CPOL 1, CPHA 1)
Definition SPI.h:105
#define CS_PIN_CONTROLLED_BY_USER
The "CS pin" that means the sketch drives its chip select itself, outside the SPI class (the default ...
Definition SPI.h:126
#define SPI_MODE1
clock idles low, data sampled on the falling edge (CPOL 0, CPHA 1)
Definition SPI.h:103
#define SPI_MODE2
clock idles high, data sampled on the falling edge (CPOL 1, CPHA 0)
Definition SPI.h:104
#define SPI_MODE0
clock idles low, data sampled on the rising edge (CPOL 0, CPHA 0)
Definition SPI.h:102
#define SPI_TRANSMITRECEIVE
send and receive (the default)
Definition SPI.h:112
@ SPI_CONTINUE
Transfer not finished: CS pin kept active (low)
Definition SPI.h:118
@ SPI_LAST
Transfer ended: CS pin released (high)
Definition SPI.h:119