Skip to content
This repository was archived by the owner on Mar 31, 2023. It is now read-only.

Commit 4b422c6

Browse files
ridhaosWi6labsVVESTM
authored andcommitted
Add SPI library
Add from https://github.com/stm32duino/Arduino_Core_STM32 version 1.2.0.
1 parent 41414db commit 4b422c6

File tree

7 files changed

+1053
-0
lines changed

7 files changed

+1053
-0
lines changed

libraries/SPI/README.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## SPI
2+
3+
STM32 SPI library has been modified with the possibility to manage several CS pins without to stop the SPI interface.
4+
_We do not describe here the [SPI Arduino API](https://www.arduino.cc/en/Reference/SPI) but the functionalities added._
5+
6+
We give to the user 3 possiblities about the management of the CS pin:
7+
1. the CS pin is managed directly by the user code before to transfer the data (like the Arduino SPI library)
8+
2. the user gives the CS pin number to the library API and the library manages itself the CS pin (see example below)
9+
3. the user uses a hardware CS pin linked to the SPI peripheral
10+
11+
### New API functions
12+
13+
* **`SPIClass::SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel)`**: alternative class constructor
14+
_Params_ SPI mosi pin
15+
_Params_ SPI miso pin
16+
_Params_ SPI sclk pin
17+
_Params_ (optional) SPI ssel pin. This pin must be an hardware CS pin. If you configure this pin, the chip select will be managed by the SPI peripheral. Do not use API functions with CS pin in parameter.
18+
19+
* **`void SPIClass::begin(uint8_t _pin)`**: initialize the SPI interface and add a CS pin
20+
_Params_ spi CS pin to be managed by the SPI library
21+
22+
* **`void beginTransaction(uint8_t pin, SPISettings settings)`**: allows to configure the SPI with other parameter. These new parameter are saved this an associated CS pin.
23+
_Params_ SPI CS pin to be managed by the SPI library
24+
_Params_ SPI settings
25+
26+
* **`void endTransaction(uint8_t pin)`**: removes a CS pin and the SPI settings associated
27+
_Params_ SPI CS pin managed by the SPI library
28+
29+
**_Note 1_** The following functions must be called after initialization of the SPI instance with `begin()` or `beginTransaction()`.
30+
If you have several device to manage, you can call `beginTransaction()` several time with different CS pin in parameter.
31+
Then you can call the following functions with different CS pin without call again `beginTransaction()` (until you call `end()` or `endTransaction()`).
32+
33+
**_Note 2_** If the mode is set to `SPI_CONTINUE`, the CS pin is kept enabled. Be careful in case you use several CS pin.
34+
35+
* **`byte transfer(uint8_t pin, uint8_t _data, SPITransferMode _mode = SPI_LAST)`**: write/read one byte
36+
_Params_ SPI CS pin managed by the SPI library
37+
_Params_ data to write
38+
_Params_ (optional) if `SPI_LAST` CS pin is reset, `SPI_CONTINUE` the CS pin is kept enabled.
39+
_Return_ byte received
40+
41+
* **`uint16_t transfer16(uint8_t pin, uint16_t _data, SPITransferMode _mode = SPI_LAST)`**: write/read half-word
42+
_Params_ SPI CS pin managed by the SPI library
43+
_Params_ 16bits data to write
44+
_Params_ (optional) if `SPI_LAST` CS pin is reset, `SPI_CONTINUE` the CS pin is kept enabled.
45+
_Return_ 16bits data received
46+
47+
* **`void transfer(uint8_t pin, void *_buf, size_t _count, SPITransferMode _mode = SPI_LAST)`**: write/read several bytes. Only one buffer used to write and read the data
48+
_Params_ SPI CS pin managed by the SPI library
49+
_Params_ pointer to data to write. The data will be replaced by the data read.
50+
_Params_ number of data to write/read.
51+
_Params_ (optional) if `SPI_LAST` CS pin is reset, `SPI_CONTINUE` the CS pin is kept enabled.
52+
53+
* **`void transfer(byte _pin, void *_bufout, void *_bufin, size_t _count, SPITransferMode _mode = SPI_LAST)`**: write/read several bytes. One buffer for the output data and one for the input data
54+
_Params_ SPI CS pin managed by the SPI library
55+
_Params_ pointer to data to write.
56+
_Params_ pointer where to store the data read.
57+
_Params_ number of data to write/read.
58+
_Params_ (optional) if `SPI_LAST` CS pin is reset, `SPI_CONTINUE` the CS pin is kept enabled.
59+
60+
### Example
61+
62+
This is an example of the use of the CS pin management:
63+
64+
```C++
65+
SPI.begin(2); //Enables the SPI instance with default settings and attachs the CS pin
66+
SPI.beginTransaction(1, settings); //Attachs another CS pin and configure the SPI instance with other settings
67+
SPI.transfer(1, 0x52); //Transfers data to the first device
68+
SPI.transfer(2, 0xA4); //Transfers data to the second device. The SPI instance is configured with the right settings
69+
SPI.end() //SPI instance is disabled
70+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
SCP1000 Barometric Pressure Sensor Display
3+
4+
Shows the output of a Barometric Pressure Sensor on a
5+
Uses the SPI library. For details on the sensor, see:
6+
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
7+
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
8+
9+
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
10+
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
11+
12+
Circuit:
13+
SCP1000 sensor attached to pins 6, 7, 10 - 13:
14+
DRDY: pin 6
15+
CSB: pin 7
16+
MOSI: pin 11
17+
MISO: pin 12
18+
SCK: pin 13
19+
20+
created 31 July 2010
21+
modified 14 August 2010
22+
by Tom Igoe
23+
*/
24+
25+
// the sensor communicates using SPI, so include the library:
26+
#include <SPI.h>
27+
28+
//Sensor's memory register addresses:
29+
const int PRESSURE = 0x1F; //3 most significant bits of pressure
30+
const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
31+
const int TEMPERATURE = 0x21; //16 bit temperature reading
32+
const byte READ = 0b11111100; // SCP1000's read command
33+
const byte WRITE = 0b00000010; // SCP1000's write command
34+
35+
// pins used for the connection with the sensor
36+
// the other you need are controlled by the SPI library):
37+
const int dataReadyPin = 6;
38+
const int chipSelectPin = 7;
39+
40+
void setup() {
41+
Serial.begin(9600);
42+
43+
// start the SPI library:
44+
SPI.begin();
45+
46+
// initalize the data ready and chip select pins:
47+
pinMode(dataReadyPin, INPUT);
48+
pinMode(chipSelectPin, OUTPUT);
49+
50+
//Configure SCP1000 for low noise configuration:
51+
writeRegister(0x02, 0x2D);
52+
writeRegister(0x01, 0x03);
53+
writeRegister(0x03, 0x02);
54+
// give the sensor time to set up:
55+
delay(100);
56+
}
57+
58+
void loop() {
59+
//Select High Resolution Mode
60+
writeRegister(0x03, 0x0A);
61+
62+
// don't do anything until the data ready pin is high:
63+
if (digitalRead(dataReadyPin) == HIGH) {
64+
//Read the temperature data
65+
int tempData = readRegister(0x21, 2);
66+
67+
// convert the temperature to celsius and display it:
68+
float realTemp = (float)tempData / 20.0;
69+
Serial.print("Temp[C]=");
70+
Serial.print(realTemp);
71+
72+
73+
//Read the pressure data highest 3 bits:
74+
byte pressure_data_high = readRegister(0x1F, 1);
75+
pressure_data_high &= 0b00000111; //you only needs bits 2 to 0
76+
77+
//Read the pressure data lower 16 bits:
78+
unsigned int pressure_data_low = readRegister(0x20, 2);
79+
//combine the two parts into one 19-bit number:
80+
long pressure = ((pressure_data_high << 16) | pressure_data_low) / 4;
81+
82+
// display the temperature:
83+
Serial.println("\tPressure [Pa]=" + String(pressure));
84+
}
85+
}
86+
87+
//Read from or write to register from the SCP1000:
88+
unsigned int readRegister(byte thisRegister, int bytesToRead) {
89+
byte inByte = 0; // incoming byte from the SPI
90+
unsigned int result = 0; // result to return
91+
Serial.print(thisRegister, BIN);
92+
Serial.print("\t");
93+
// SCP1000 expects the register name in the upper 6 bits
94+
// of the byte. So shift the bits left by two bits:
95+
thisRegister = thisRegister << 2;
96+
// now combine the address and the command into one byte
97+
byte dataToSend = thisRegister & READ;
98+
Serial.println(thisRegister, BIN);
99+
// take the chip select low to select the device:
100+
digitalWrite(chipSelectPin, LOW);
101+
// send the device the register you want to read:
102+
SPI.transfer(dataToSend);
103+
// send a value of 0 to read the first byte returned:
104+
result = SPI.transfer(0x00);
105+
// decrement the number of bytes left to read:
106+
bytesToRead--;
107+
// if you still have another byte to read:
108+
if (bytesToRead > 0) {
109+
// shift the first byte left, then get the second byte:
110+
result = result << 8;
111+
inByte = SPI.transfer(0x00);
112+
// combine the byte you just got with the previous one:
113+
result = result | inByte;
114+
// decrement the number of bytes left to read:
115+
bytesToRead--;
116+
}
117+
// take the chip select high to de-select:
118+
digitalWrite(chipSelectPin, HIGH);
119+
// return the result:
120+
return (result);
121+
}
122+
123+
124+
//Sends a write command to SCP1000
125+
126+
void writeRegister(byte thisRegister, byte thisValue) {
127+
128+
// SCP1000 expects the register address in the upper 6 bits
129+
// of the byte. So shift the bits left by two bits:
130+
thisRegister = thisRegister << 2;
131+
// now combine the register address and the command into one byte:
132+
byte dataToSend = thisRegister | WRITE;
133+
134+
// take the chip select low to select the device:
135+
digitalWrite(chipSelectPin, LOW);
136+
137+
SPI.transfer(dataToSend); //Send register location
138+
SPI.transfer(thisValue); //Send value to record into register
139+
140+
// take the chip select high to de-select:
141+
digitalWrite(chipSelectPin, HIGH);
142+
}
143+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Digital Pot Control
3+
4+
This example controls an Analog Devices AD5206 digital potentiometer.
5+
The AD5206 has 6 potentiometer channels. Each channel's pins are labeled
6+
A - connect this to voltage
7+
W - this is the pot's wiper, which changes when you set it
8+
B - connect this to ground.
9+
10+
The AD5206 is SPI-compatible,and to command it, you send two bytes,
11+
one with the channel number (0 - 5) and one with the resistance value for the
12+
channel (0 - 255).
13+
14+
The circuit:
15+
* All A pins of AD5206 connected to +5V
16+
* All B pins of AD5206 connected to ground
17+
* An LED and a 220-ohm resisor in series connected from each W pin to ground
18+
* CS - to digital pin 10 (SS pin)
19+
* SDI - to digital pin 11 (MOSI pin)
20+
* CLK - to digital pin 13 (SCK pin)
21+
22+
created 10 Aug 2010
23+
by Tom Igoe
24+
25+
Thanks to Heather Dewey-Hagborg for the original tutorial, 2005
26+
27+
*/
28+
29+
30+
// inslude the SPI library:
31+
#include <SPI.h>
32+
33+
34+
// set pin 10 as the slave select for the digital pot:
35+
const int slaveSelectPin = 10;
36+
37+
void setup() {
38+
// set the slaveSelectPin as an output:
39+
pinMode(slaveSelectPin, OUTPUT);
40+
// initialize SPI:
41+
SPI.begin();
42+
}
43+
44+
void loop() {
45+
// go through the six channels of the digital pot:
46+
for (int channel = 0; channel < 6; channel++) {
47+
// change the resistance on this channel from min to max:
48+
for (int level = 0; level < 255; level++) {
49+
digitalPotWrite(channel, level);
50+
delay(10);
51+
}
52+
// wait a second at the top:
53+
delay(100);
54+
// change the resistance on this channel from max to min:
55+
for (int level = 0; level < 255; level++) {
56+
digitalPotWrite(channel, 255 - level);
57+
delay(10);
58+
}
59+
}
60+
61+
}
62+
63+
void digitalPotWrite(int address, int value) {
64+
// take the SS pin low to select the chip:
65+
digitalWrite(slaveSelectPin, LOW);
66+
// send in the address and value via SPI:
67+
SPI.transfer(address);
68+
SPI.transfer(value);
69+
// take the SS pin high to de-select the chip:
70+
digitalWrite(slaveSelectPin, HIGH);
71+
}

libraries/SPI/keywords.txt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#######################################
2+
# Syntax Coloring Map SPI
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
SPI KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
begin KEYWORD2
15+
end KEYWORD2
16+
transfer KEYWORD2
17+
#setBitOrder KEYWORD2
18+
setDataMode KEYWORD2
19+
setClockDivider KEYWORD2
20+
setMISO KEYWORD2
21+
setMOSI KEYWORD2
22+
setSCLK KEYWORD2
23+
setSSEL KEYWORD2
24+
25+
#######################################
26+
# Constants (LITERAL1)
27+
#######################################
28+
SPI_MODE0 LITERAL1
29+
SPI_MODE1 LITERAL1
30+
SPI_MODE2 LITERAL1
31+
SPI_MODE3 LITERAL1
32+
33+
SPI_CONTINUE LITERAL1
34+
SPI_LAST LITERAL1

libraries/SPI/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SPI
2+
version=1.0
3+
author=Arduino, Wi6Labs
4+
maintainer=stm32duino
5+
sentence=Enables the communication with devices that use the Serial Peripheral Interface (SPI) Bus.
6+
paragraph=This library is based on the official Arduino SPI library and adapted to STM32 boards.
7+
category=Communication
8+
url=http://www.arduino.cc/en/Reference/SPI
9+
architectures=stm32

0 commit comments

Comments
 (0)