Skip to content

Commit a075fef

Browse files
committed
SPI: fix library compliance
1 parent f2f6bbc commit a075fef

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

libraries/SPI/SPI.cpp

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
#include "zephyrInternal.h"
99
#include <zephyr/kernel.h>
1010

11-
/* Serial Peripheral Control Register */
12-
uint8_t SPCR;
13-
1411
arduino::ZephyrSPI::ZephyrSPI(const struct device *spi) : spi_dev(spi) {}
1512

1613
uint8_t arduino::ZephyrSPI::transfer(uint8_t data) {
@@ -65,57 +62,48 @@ void arduino::ZephyrSPI::transfer(void *buf, size_t count) {
6562
.count = 1,
6663
};
6764

68-
ret = spi_write(spi_dev, &config, &tx_buf_set);
69-
if (ret < 0) {
70-
return;
71-
}
65+
uint8_t rx[count];
66+
const struct spi_buf rx_buf = {.buf = &rx, .len = count};
67+
const struct spi_buf_set rx_buf_set = {
68+
.buffers = &rx_buf,
69+
.count = 1,
70+
};
7271

73-
ret = spi_read(spi_dev, &config, &tx_buf_set);
74-
if (ret < 0) {
75-
return;
76-
}
72+
spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set);
73+
memcpy(buf, rx, count);
7774
}
7875

7976
void arduino::ZephyrSPI::usingInterrupt(int interruptNumber) {
80-
interrupt[interrupt_pos++] = interruptNumber;
8177
}
8278

8379
void arduino::ZephyrSPI::notUsingInterrupt(int interruptNumber) {
84-
for (size_t i = 0; i < interrupt_pos; ++i) {
85-
if (interrupt[i] == interruptNumber) {
86-
memmove(&interrupt[i], &interrupt[i + 1], interrupt_pos - i - 1);
87-
interrupt_pos--;
88-
break;
89-
}
90-
}
9180
}
9281

9382
void arduino::ZephyrSPI::beginTransaction(SPISettings settings) {
9483
memset(&config, 0, sizeof(config));
9584
config.frequency = settings.getClockFreq();
96-
config.operation = ((settings.getBitOrder() ^ 1) << 4) |
97-
(settings.getDataMode() << 1) | ((SPCR >> MSTR) & 1) |
98-
SPI_WORD_SET(8);
99-
100-
detachInterrupt();
85+
auto mode = SPI_MODE_CPOL | SPI_MODE_CPHA;
86+
switch (settings.getDataMode()) {
87+
case SPI_MODE0:
88+
mode = 0; break;
89+
case SPI_MODE1:
90+
mode = SPI_MODE_CPHA; break;
91+
case SPI_MODE2:
92+
mode = SPI_MODE_CPOL; break;
93+
case SPI_MODE3:
94+
mode = SPI_MODE_CPOL | SPI_MODE_CPHA; break;
95+
}
96+
config.operation = SPI_WORD_SET(8) | (settings.getBitOrder() == MSBFIRST ? SPI_TRANSFER_MSB : SPI_TRANSFER_LSB) | mode;
10197
}
10298

10399
void arduino::ZephyrSPI::endTransaction(void) {
104100
spi_release(spi_dev, &config);
105-
attachInterrupt();
106101
}
107102

108-
void arduino::ZephyrSPI::attachInterrupt() {
109-
for (size_t i = 0; i < interrupt_pos; ++i) {
110-
enableInterrupt(interrupt[i]);
111-
}
112-
}
103+
void arduino::ZephyrSPI::attachInterrupt() {}
104+
105+
void arduino::ZephyrSPI::detachInterrupt() {}
113106

114-
void arduino::ZephyrSPI::detachInterrupt() {
115-
for (size_t i = 0; i < interrupt_pos; ++i) {
116-
disableInterrupt(interrupt[i]);
117-
}
118-
}
119107

120108
void arduino::ZephyrSPI::begin() {}
121109

libraries/SPI/SPI.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include <api/HardwareSPI.h>
1111
#include <zephyr/drivers/spi.h>
1212

13+
#undef SPI
14+
#undef SPI1
15+
1316
#define SPR0 0
1417
#define SPR1 1
1518
#define CPHA 2
@@ -75,7 +78,6 @@ extern arduino::ZephyrSPI SPI;
7578
#endif
7679

7780
/* Serial Peripheral Control Register */
78-
extern uint8_t SPCR;
7981

8082
using arduino::SPI_MODE0;
8183
using arduino::SPI_MODE1;

0 commit comments

Comments
 (0)