Skip to content

Commit 43ec3cb

Browse files
committed
chore(SPI): clean up internal CS pin managements
To be aligned with Arduino API. Only Hardware CS pin support kept. Allows to save memory space and increase execution speed. Fixes #257. Signed-off-by: Frederic Pillon <[email protected]>
1 parent 97bebdc commit 43ec3cb

File tree

3 files changed

+126
-457
lines changed

3 files changed

+126
-457
lines changed

Diff for: libraries/SPI/README.md

+43-50
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,63 @@
11
## SPI
22

3-
STM32 SPI library has been modified with the possibility to manage several CS pins without to stop the SPI interface.
3+
STM32 SPI library has been modified with the possibility to manage hardware CS pin linked to the SPI peripheral.
44
_We do not describe here the [SPI Arduino API](https://www.arduino.cc/en/Reference/SPI) but the functionalities added._
55

6-
We give to the user 3 possibilities 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
6+
User have 2 possibilities about the management of the CS pin:
7+
* the CS pin is managed directly by the user code before to transfer the data (like the Arduino SPI library)
8+
* the user uses a hardware CS pin linked to the SPI peripheral
109

11-
### New API functions
10+
### New SPISetting parameter
1211

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.
12+
* `noReceive`: value can be `SPI_TRANSMITRECEIVE` or `SPI_TRANSMITONLY`. It allows to skip receive data after transmitting. Default `SPI_TRANSMITRECEIVE`.
1813

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
14+
### New API functions
2115

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
16+
* `SPIClass::SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel)`: alternative class constructor
17+
_Params_ SPI `mosi` pin
18+
_Params_ SPI `miso` pin
19+
_Params_ SPI `sclk` pin
20+
_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.
2521

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
22+
* `SPI_HandleTypeDef *getHandle(void)`: Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA). **Use at your own risk.**
2823

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()`).
3224

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.
25+
##### Example
3426

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
27+
This is an example of the use of the hardware CS pin linked to the SPI peripheral:
4028

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
29+
```C++
30+
#include <SPI.h>
31+
// MOSI MISO SCLK SSEL
32+
SPIClass SPI_3(PC12, PC11, PC10, PC9);
33+
34+
void setup() {
35+
SPI_3.begin(); // Enable the SPI_3 instance with default SPISsettings
36+
SPI_3.beginTransaction(settings); // Configure the SPI_3 instance with other settings
37+
SPI_3.transfer(0x52); // Transfers data to the first device
38+
SPI_3.end() //SPI_3 instance is disabled
39+
}
40+
```
4641
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.
42+
#### Change default `SPI` instance pins
43+
It is also possible to change the default pins used by the `SPI` instance using above API:
5244
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.
45+
[[/img/Warning-icon.png|alt="Warning"]] **Have to be called before `begin()`.**
5946
60-
### Example
47+
* `void setMISO(uint32_t miso)`
48+
* `void setMOSI(uint32_t mosi)`
49+
* `void setSCLK(uint32_t sclk)`
50+
* `void setSSEL(uint32_t ssel)`
51+
* `void setMISO(PinName miso)`
52+
* `void setMOSI(PinName mosi)`
53+
* `void setSCLK(PinName sclk)`
54+
* `void setSSEL(PinName ssel)`
6155
62-
This is an example of the use of the CS pin management:
56+
**_Note 1_** Using `setSSEL()` allows to enable hardware CS pin management linked to the SPI peripheral.
6357
58+
##### Example:
6459
```C++
65-
SPI.begin(2); //Enables the SPI instance with default settings and attaches the CS pin
66-
SPI.beginTransaction(1, settings); //Attaches 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
60+
SPI.setMISO(PC_4); // using pin name PY_n
61+
SPI.setMOSI(PC2); // using pin number PYn
62+
SPI.begin(2);
7063
```

0 commit comments

Comments
 (0)