|
11 | 11 | /* Serial Peripheral Control Register */ |
12 | 12 | uint8_t SPCR; |
13 | 13 |
|
14 | | -arduino::ZephyrSPI::ZephyrSPI(const struct device *spi) : spi_dev(spi) {} |
| 14 | +arduino::ZephyrSPI::ZephyrSPI(const struct device *spi) : spi_dev(spi) { |
| 15 | +} |
15 | 16 |
|
16 | 17 | uint8_t arduino::ZephyrSPI::transfer(uint8_t data) { |
17 | | - int ret; |
18 | | - uint8_t rx; |
19 | | - const struct spi_buf tx_buf = {.buf = &data, .len = sizeof(data)}; |
20 | | - const struct spi_buf_set tx_buf_set = { |
21 | | - .buffers = &tx_buf, |
22 | | - .count = 1, |
23 | | - }; |
24 | | - const struct spi_buf rx_buf = {.buf = &rx, .len = sizeof(rx)}; |
25 | | - const struct spi_buf_set rx_buf_set = { |
26 | | - .buffers = &rx_buf, |
27 | | - .count = 1, |
28 | | - }; |
29 | | - |
30 | | - ret = spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set); |
31 | | - if (ret < 0) { |
32 | | - return 0; |
33 | | - } |
34 | | - |
35 | | - return rx; |
| 18 | + int ret; |
| 19 | + uint8_t rx; |
| 20 | + const struct spi_buf tx_buf = {.buf = &data, .len = sizeof(data)}; |
| 21 | + const struct spi_buf_set tx_buf_set = { |
| 22 | + .buffers = &tx_buf, |
| 23 | + .count = 1, |
| 24 | + }; |
| 25 | + const struct spi_buf rx_buf = {.buf = &rx, .len = sizeof(rx)}; |
| 26 | + const struct spi_buf_set rx_buf_set = { |
| 27 | + .buffers = &rx_buf, |
| 28 | + .count = 1, |
| 29 | + }; |
| 30 | + |
| 31 | + ret = spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set); |
| 32 | + if (ret < 0) { |
| 33 | + return 0; |
| 34 | + } |
| 35 | + |
| 36 | + return rx; |
36 | 37 | } |
37 | 38 |
|
38 | 39 | uint16_t arduino::ZephyrSPI::transfer16(uint16_t data) { |
39 | | - int ret; |
40 | | - uint16_t rx; |
41 | | - const struct spi_buf tx_buf = {.buf = &data, .len = sizeof(data)}; |
42 | | - const struct spi_buf_set tx_buf_set = { |
43 | | - .buffers = &tx_buf, |
44 | | - .count = 1, |
45 | | - }; |
46 | | - const struct spi_buf rx_buf = {.buf = &rx, .len = sizeof(rx)}; |
47 | | - const struct spi_buf_set rx_buf_set = { |
48 | | - .buffers = &rx_buf, |
49 | | - .count = 1, |
50 | | - }; |
51 | | - |
52 | | - ret = spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set); |
53 | | - if (ret < 0) { |
54 | | - return 0; |
55 | | - } |
56 | | - |
57 | | - return rx; |
| 40 | + int ret; |
| 41 | + uint16_t rx; |
| 42 | + const struct spi_buf tx_buf = {.buf = &data, .len = sizeof(data)}; |
| 43 | + const struct spi_buf_set tx_buf_set = { |
| 44 | + .buffers = &tx_buf, |
| 45 | + .count = 1, |
| 46 | + }; |
| 47 | + const struct spi_buf rx_buf = {.buf = &rx, .len = sizeof(rx)}; |
| 48 | + const struct spi_buf_set rx_buf_set = { |
| 49 | + .buffers = &rx_buf, |
| 50 | + .count = 1, |
| 51 | + }; |
| 52 | + |
| 53 | + ret = spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set); |
| 54 | + if (ret < 0) { |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | + return rx; |
58 | 59 | } |
59 | 60 |
|
60 | 61 | void arduino::ZephyrSPI::transfer(void *buf, size_t count) { |
61 | | - int ret; |
62 | | - const struct spi_buf tx_buf = {.buf = buf, .len = count}; |
63 | | - const struct spi_buf_set tx_buf_set = { |
64 | | - .buffers = &tx_buf, |
65 | | - .count = 1, |
66 | | - }; |
67 | | - |
68 | | - ret = spi_write(spi_dev, &config, &tx_buf_set); |
69 | | - if (ret < 0) { |
70 | | - return; |
71 | | - } |
72 | | - |
73 | | - ret = spi_read(spi_dev, &config, &tx_buf_set); |
74 | | - if (ret < 0) { |
75 | | - return; |
76 | | - } |
| 62 | + int ret; |
| 63 | + const struct spi_buf tx_buf = {.buf = buf, .len = count}; |
| 64 | + const struct spi_buf_set tx_buf_set = { |
| 65 | + .buffers = &tx_buf, |
| 66 | + .count = 1, |
| 67 | + }; |
| 68 | + |
| 69 | + ret = spi_write(spi_dev, &config, &tx_buf_set); |
| 70 | + if (ret < 0) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + ret = spi_read(spi_dev, &config, &tx_buf_set); |
| 75 | + if (ret < 0) { |
| 76 | + return; |
| 77 | + } |
77 | 78 | } |
78 | 79 |
|
79 | 80 | void arduino::ZephyrSPI::usingInterrupt(int interruptNumber) { |
80 | | - interrupt[interrupt_pos++] = interruptNumber; |
| 81 | + interrupt[interrupt_pos++] = interruptNumber; |
81 | 82 | } |
82 | 83 |
|
83 | 84 | 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 | | - } |
| 85 | + for (size_t i = 0; i < interrupt_pos; ++i) { |
| 86 | + if (interrupt[i] == interruptNumber) { |
| 87 | + memmove(&interrupt[i], &interrupt[i + 1], interrupt_pos - i - 1); |
| 88 | + interrupt_pos--; |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
91 | 92 | } |
92 | 93 |
|
93 | 94 | void arduino::ZephyrSPI::beginTransaction(SPISettings settings) { |
94 | | - memset(&config, 0, sizeof(config)); |
95 | | - config.frequency = settings.getClockFreq(); |
96 | | - config.operation = ((settings.getBitOrder() ^ 1) << 4) | |
97 | | - (settings.getDataMode() << 1) | ((SPCR >> MSTR) & 1) | |
98 | | - SPI_WORD_SET(8); |
| 95 | + memset(&config, 0, sizeof(config)); |
| 96 | + config.frequency = settings.getClockFreq(); |
| 97 | + config.operation = ((settings.getBitOrder() ^ 1) << 4) | (settings.getDataMode() << 1) | |
| 98 | + ((SPCR >> MSTR) & 1) | SPI_WORD_SET(8); |
99 | 99 |
|
100 | | - detachInterrupt(); |
| 100 | + detachInterrupt(); |
101 | 101 | } |
102 | 102 |
|
103 | 103 | void arduino::ZephyrSPI::endTransaction(void) { |
104 | | - spi_release(spi_dev, &config); |
105 | | - attachInterrupt(); |
| 104 | + spi_release(spi_dev, &config); |
| 105 | + attachInterrupt(); |
106 | 106 | } |
107 | 107 |
|
108 | 108 | void arduino::ZephyrSPI::attachInterrupt() { |
109 | | - for (size_t i = 0; i < interrupt_pos; ++i) { |
110 | | - enableInterrupt(interrupt[i]); |
111 | | - } |
| 109 | + for (size_t i = 0; i < interrupt_pos; ++i) { |
| 110 | + enableInterrupt(interrupt[i]); |
| 111 | + } |
112 | 112 | } |
113 | 113 |
|
114 | 114 | void arduino::ZephyrSPI::detachInterrupt() { |
115 | | - for (size_t i = 0; i < interrupt_pos; ++i) { |
116 | | - disableInterrupt(interrupt[i]); |
117 | | - } |
| 115 | + for (size_t i = 0; i < interrupt_pos; ++i) { |
| 116 | + disableInterrupt(interrupt[i]); |
| 117 | + } |
118 | 118 | } |
119 | 119 |
|
120 | | -void arduino::ZephyrSPI::begin() {} |
| 120 | +void arduino::ZephyrSPI::begin() { |
| 121 | +} |
121 | 122 |
|
122 | | -void arduino::ZephyrSPI::end() {} |
| 123 | +void arduino::ZephyrSPI::end() { |
| 124 | +} |
123 | 125 |
|
124 | 126 | #if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), spis) |
125 | 127 | #if (DT_PROP_LEN(DT_PATH(zephyr_user), spis) > 1) |
|
0 commit comments