|
8 | 8 | #include "zephyrInternal.h"
|
9 | 9 | #include <zephyr/kernel.h>
|
10 | 10 |
|
11 |
| -/* Serial Peripheral Control Register */ |
12 |
| -uint8_t SPCR; |
13 |
| - |
14 | 11 | arduino::ZephyrSPI::ZephyrSPI(const struct device *spi) : spi_dev(spi) {}
|
15 | 12 |
|
16 | 13 | uint8_t arduino::ZephyrSPI::transfer(uint8_t data) {
|
@@ -65,57 +62,48 @@ void arduino::ZephyrSPI::transfer(void *buf, size_t count) {
|
65 | 62 | .count = 1,
|
66 | 63 | };
|
67 | 64 |
|
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 | + }; |
72 | 71 |
|
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); |
77 | 74 | }
|
78 | 75 |
|
79 | 76 | void arduino::ZephyrSPI::usingInterrupt(int interruptNumber) {
|
80 |
| - interrupt[interrupt_pos++] = interruptNumber; |
81 | 77 | }
|
82 | 78 |
|
83 | 79 | 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 |
| - } |
91 | 80 | }
|
92 | 81 |
|
93 | 82 | void arduino::ZephyrSPI::beginTransaction(SPISettings settings) {
|
94 | 83 | memset(&config, 0, sizeof(config));
|
95 | 84 | 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; |
101 | 97 | }
|
102 | 98 |
|
103 | 99 | void arduino::ZephyrSPI::endTransaction(void) {
|
104 | 100 | spi_release(spi_dev, &config);
|
105 |
| - attachInterrupt(); |
106 | 101 | }
|
107 | 102 |
|
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() {} |
113 | 106 |
|
114 |
| -void arduino::ZephyrSPI::detachInterrupt() { |
115 |
| - for (size_t i = 0; i < interrupt_pos; ++i) { |
116 |
| - disableInterrupt(interrupt[i]); |
117 |
| - } |
118 |
| -} |
119 | 107 |
|
120 | 108 | void arduino::ZephyrSPI::begin() {}
|
121 | 109 |
|
|
0 commit comments