Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9df4fe0

Browse files
committed
feat(spi): add transfer api with tx/rx buffer
Fixes#2205Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent0b9c63d commit9df4fe0

File tree

5 files changed

+62
-24
lines changed

5 files changed

+62
-24
lines changed

‎libraries/SPI/README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ User have 2 possibilities about the management of the CS pin:
77
* the CS pin is managed directly by the user code before to transfer the data (like the Arduino SPI library)
88
* the user uses a hardware CS pin linked to the SPI peripheral
99

10-
###New API functions
10+
##New API functions
1111

12-
*`SPIClass::SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel)`: alternative class constructor
13-
_Params_ SPI`mosi` pin
14-
_Params_ SPI`miso` pin
15-
_Params_ SPI`sclk` pin
16-
_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.
12+
####Alternative class constructor
13+
*`SPIClass::SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel)`
14+
15+
_Param_ SPI`mosi` pin
1716

18-
*`SPI_HandleTypeDef *getHandle(void)`: Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA).**Use at your own risk.**
17+
_Param_ SPI`miso` pin
1918

19+
_Param_ SPI`sclk` pin
20+
21+
_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.
2022

2123
#####Example
2224

@@ -35,9 +37,15 @@ void setup() {
3537
}
3638
```
3739
38-
### Extended API
40+
#### Transfer with Tx/Rx buffer
41+
42+
* `void transfer(const void *tx_buf, void *rx_buf, size_t count)` :Transfer several bytes. One constant buffer used to send and one to receive data.
3943
40-
* All `transfer()` API's have a new bool argument `skipReceive`. It allows to skip receive data after transmitting. Value can be `SPI_TRANSMITRECEIVE` or `SPI_TRANSMITONLY`. Default `SPI_TRANSMITRECEIVE`.
44+
_Param_ `tx_buf`: constant array of Tx bytes that is filled by the user before starting the SPI transfer. If NULL, default dummy 0xFF bytes will be clocked out.
45+
46+
_Param_ `rx_buf`: array of Rx bytes that will be filled by the slave during the SPI transfer. If NULL, the received data will be discarded.
47+
48+
_Param_ `count`: number of bytes to send/receive.
4149
4250
#### Change default `SPI` instance pins
4351
It is also possible to change the default pins used by the `SPI` instance using above API:
@@ -63,3 +71,9 @@ It is also possible to change the default pins used by the `SPI` instance using
6371
SPI.setMOSI(PC2); // using pin number PYn
6472
SPI.begin(2);
6573
```
74+
75+
*`SPI_HandleTypeDef *getHandle(void)`: Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA).**Use at your own risk.**
76+
77+
##Extended API
78+
79+
* All defaustatndard`transfer()` API's have a new bool argument`skipReceive`. It allows to skip receive data after transmitting. Value can be`SPI_TRANSMITRECEIVE` or`SPI_TRANSMITONLY`. Default`SPI_TRANSMITRECEIVE`.

‎libraries/SPI/src/SPI.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void SPIClass::setClockDivider(uint8_t divider)
163163
*/
164164
uint8_tSPIClass::transfer(uint8_t data,bool skipReceive)
165165
{
166-
spi_transfer(&_spi, &data,sizeof(uint8_t), skipReceive);
166+
spi_transfer(&_spi, &data,(!skipReceive) ? &data :NULL,sizeof(uint8_t));
167167
return data;
168168
}
169169

@@ -184,7 +184,7 @@ uint16_t SPIClass::transfer16(uint16_t data, bool skipReceive)
184184
tmp = ((data &0xff00) >>8) | ((data &0xff) <<8);
185185
data = tmp;
186186
}
187-
spi_transfer(&_spi, (uint8_t *)&data,sizeof(uint16_t), skipReceive);
187+
spi_transfer(&_spi, (uint8_t *)&data,(!skipReceive) ? (uint8_t *)&data :NULL,sizeof(uint16_t));
188188

189189
if (_spiSettings.bitOrder) {
190190
tmp = ((data &0xff00) >>8) | ((data &0xff) <<8);
@@ -206,11 +206,27 @@ uint16_t SPIClass::transfer16(uint16_t data, bool skipReceive)
206206
*/
207207
voidSPIClass::transfer(void *buf,size_t count,bool skipReceive)
208208
{
209-
if ((count !=0) && (buf !=NULL)) {
210-
spi_transfer(&_spi, ((uint8_t *)buf), count, skipReceive);
211-
}
209+
spi_transfer(&_spi, (uint8_t *)buf, (!skipReceive) ? (uint8_t *)buf :NULL, count);
210+
211+
}
212+
213+
/**
214+
* @brief Transfer several bytes. One constant buffer used to send and
215+
* one to receive data.
216+
* begin() or beginTransaction() must be called at least once before.
217+
* @param tx_buf: array of Tx bytes that is filled by the user before starting
218+
* the SPI transfer. If NULL, default dummy 0xFF bytes will be
219+
* clocked out.
220+
* @param rx_buf: array of Rx bytes that will be filled by the slave during
221+
* the SPI transfer. If NULL, the received data will be discarded.
222+
* @param count: number of bytes to send/receive.
223+
*/
224+
voidSPIClass::transfer(constvoid *tx_buf,void *rx_buf,size_t count)
225+
{
226+
spi_transfer(&_spi, ((constuint8_t *)tx_buf), ((uint8_t *)rx_buf), count);
212227
}
213228

229+
214230
/**
215231
* @brief Not implemented.
216232
*/

‎libraries/SPI/src/SPI.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ class SPIClass {
138138
uint16_ttransfer16(uint16_t data,bool skipReceive = SPI_TRANSMITRECEIVE);
139139
voidtransfer(void *buf,size_t count,bool skipReceive = SPI_TRANSMITRECEIVE);
140140

141+
/* Expand SPI API
142+
* https://github.com/arduino/ArduinoCore-API/discussions/189
143+
*/
144+
voidtransfer(constvoid *tx_buf,void *rx_buf,size_t count);
145+
141146
/* These methods are deprecated and kept for compatibility.
142147
* Use SPISettings with SPI.beginTransaction() to configure SPI parameters.
143148
*/

‎libraries/SPI/src/utility/spi_com.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -500,17 +500,18 @@ void spi_deinit(spi_t *obj)
500500
* @brief This function is implemented by user to send/receive data over
501501
* SPI interface
502502
* @param obj : pointer to spi_t structure
503-
* @param buffer : tx data to send before reception
503+
* @param tx_buffer : tx data to send before reception
504+
* @param rx_buffer : rx data to receive if not numm
504505
* @param len : length in byte of the data to send and receive
505-
* @param skipReceive: skip receiving data after transmit or not
506506
* @retval status of the send operation (0) in case of error
507507
*/
508-
spi_status_espi_transfer(spi_t*obj,uint8_t*buffer,uint16_tlen,boolskipReceive)
508+
spi_status_espi_transfer(spi_t*obj,constuint8_t*tx_buffer,uint8_t*rx_buffer,
509+
uint16_tlen)
509510
{
510511
spi_status_eret=SPI_OK;
511512
uint32_ttickstart,size=len;
512513
SPI_TypeDef*_SPI=obj->handle.Instance;
513-
uint8_t*tx_buffer=buffer;
514+
uint8_t*tx_buf=(uint8_t*)tx_buffer;
514515

515516
if (len==0) {
516517
ret=SPI_ERROR;
@@ -530,15 +531,17 @@ spi_status_e spi_transfer(spi_t *obj, uint8_t *buffer, uint16_t len, bool skipRe
530531
#else
531532
while (!LL_SPI_IsActiveFlag_TXE(_SPI));
532533
#endif
533-
LL_SPI_TransmitData8(_SPI,*tx_buffer++);
534+
LL_SPI_TransmitData8(_SPI,tx_buf ?*tx_buf++ :0XFF);
534535

535-
if (!skipReceive) {
536536
#if defined(SPI_SR_RXP)
537-
while (!LL_SPI_IsActiveFlag_RXP(_SPI));
537+
while (!LL_SPI_IsActiveFlag_RXP(_SPI));
538538
#else
539-
while (!LL_SPI_IsActiveFlag_RXNE(_SPI));
539+
while (!LL_SPI_IsActiveFlag_RXNE(_SPI));
540540
#endif
541-
*buffer++=LL_SPI_ReceiveData8(_SPI);
541+
if (rx_buffer) {
542+
*rx_buffer++=LL_SPI_ReceiveData8(_SPI);
543+
}else {
544+
LL_SPI_ReceiveData8(_SPI);
542545
}
543546
if ((SPI_TRANSFER_TIMEOUT!=HAL_MAX_DELAY)&&
544547
(HAL_GetTick()-tickstart >=SPI_TRANSFER_TIMEOUT)) {

‎libraries/SPI/src/utility/spi_com.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ typedef enum {
110110
/* Exported functions ------------------------------------------------------- */
111111
voidspi_init(spi_t*obj,uint32_tspeed,SPIModemode,uint8_tmsb);
112112
voidspi_deinit(spi_t*obj);
113-
spi_status_espi_transfer(spi_t*obj,uint8_t*buffer,uint16_tlen,boolskipReceive);
113+
spi_status_espi_transfer(spi_t*obj,constuint8_t*tx_buffer,uint8_t*rx_buffer,uint16_tlen);
114114
uint32_tspi_getClkFreq(spi_t*obj);
115115

116116
#ifdef__cplusplus

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp