-
Notifications
You must be signed in to change notification settings - Fork 59
libraries: SPI & Wire Bump to 0.53.1 #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from all commits
dc79a2a
5366a59
ff00459
525eb53
32099ac
127be40
0d9f4cf
2ec7fdb
df0afa8
3a1aef7
3f29a41
2ce9314
20f7b9d
da11f26
ba76a67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,118 +8,117 @@ | |
| #include "zephyrInternal.h" | ||
| #include <zephyr/kernel.h> | ||
|
|
||
| /* Serial Peripheral Control Register */ | ||
| uint8_t SPCR; | ||
|
|
||
| arduino::ZephyrSPI::ZephyrSPI(const struct device *spi) : spi_dev(spi) {} | ||
| arduino::ZephyrSPI::ZephyrSPI(const struct device *spi) : spi_dev(spi) { | ||
| } | ||
|
|
||
| uint8_t arduino::ZephyrSPI::transfer(uint8_t data) { | ||
| int ret; | ||
| uint8_t rx; | ||
| const struct spi_buf tx_buf = {.buf = &data, .len = sizeof(data)}; | ||
| const struct spi_buf_set tx_buf_set = { | ||
| .buffers = &tx_buf, | ||
| .count = 1, | ||
| }; | ||
| const struct spi_buf rx_buf = {.buf = &rx, .len = sizeof(rx)}; | ||
| const struct spi_buf_set rx_buf_set = { | ||
| .buffers = &rx_buf, | ||
| .count = 1, | ||
| }; | ||
|
|
||
| ret = spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set); | ||
| if (ret < 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| return rx; | ||
| uint8_t rx = data; | ||
| if (transfer(&rx, sizeof(rx), &config) < 0) { | ||
| return 0; | ||
| } | ||
| return rx; | ||
| } | ||
|
|
||
| uint16_t arduino::ZephyrSPI::transfer16(uint16_t data) { | ||
| int ret; | ||
| uint16_t rx; | ||
| const struct spi_buf tx_buf = {.buf = &data, .len = sizeof(data)}; | ||
| const struct spi_buf_set tx_buf_set = { | ||
| .buffers = &tx_buf, | ||
| .count = 1, | ||
| }; | ||
| const struct spi_buf rx_buf = {.buf = &rx, .len = sizeof(rx)}; | ||
| const struct spi_buf_set rx_buf_set = { | ||
| .buffers = &rx_buf, | ||
| .count = 1, | ||
| }; | ||
|
|
||
| ret = spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set); | ||
| if (ret < 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| return rx; | ||
| uint16_t rx = data; | ||
| if (transfer(&rx, sizeof(rx), &config16) < 0) { | ||
| return 0; | ||
| } | ||
| return rx; | ||
| } | ||
|
|
||
| void arduino::ZephyrSPI::transfer(void *buf, size_t count) { | ||
| int ret; | ||
| const struct spi_buf tx_buf = {.buf = buf, .len = count}; | ||
| const struct spi_buf_set tx_buf_set = { | ||
| .buffers = &tx_buf, | ||
| .count = 1, | ||
| }; | ||
|
|
||
| ret = spi_write(spi_dev, &config, &tx_buf_set); | ||
| if (ret < 0) { | ||
| return; | ||
| } | ||
|
|
||
| ret = spi_read(spi_dev, &config, &tx_buf_set); | ||
| if (ret < 0) { | ||
| return; | ||
| } | ||
| int ret = transfer(buf, count, &config); | ||
| (void)ret; | ||
| } | ||
|
|
||
| int arduino::ZephyrSPI::transfer(void *buf, size_t len, const struct spi_config *config) { | ||
| int ret; | ||
|
|
||
| const struct spi_buf tx_buf = {.buf = buf, .len = len}; | ||
| const struct spi_buf_set tx_buf_set = { | ||
| .buffers = &tx_buf, | ||
| .count = 1, | ||
| }; | ||
|
|
||
| const struct spi_buf rx_buf = {.buf = buf, .len = len}; | ||
| const struct spi_buf_set rx_buf_set = { | ||
| .buffers = &rx_buf, | ||
| .count = 1, | ||
| }; | ||
|
|
||
| return spi_transceive(spi_dev, config, &tx_buf_set, &rx_buf_set); | ||
| } | ||
|
|
||
| void arduino::ZephyrSPI::usingInterrupt(int interruptNumber) { | ||
| interrupt[interrupt_pos++] = interruptNumber; | ||
| } | ||
|
|
||
| void arduino::ZephyrSPI::notUsingInterrupt(int interruptNumber) { | ||
| for (size_t i = 0; i < interrupt_pos; ++i) { | ||
| if (interrupt[i] == interruptNumber) { | ||
| memmove(&interrupt[i], &interrupt[i + 1], interrupt_pos - i - 1); | ||
| interrupt_pos--; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
soburi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| void arduino::ZephyrSPI::beginTransaction(SPISettings settings) { | ||
| memset(&config, 0, sizeof(config)); | ||
| config.frequency = settings.getClockFreq(); | ||
| config.operation = ((settings.getBitOrder() ^ 1) << 4) | | ||
| (settings.getDataMode() << 1) | ((SPCR >> MSTR) & 1) | | ||
| SPI_WORD_SET(8); | ||
|
|
||
| detachInterrupt(); | ||
| uint32_t mode = SPI_HOLD_ON_CS; | ||
|
|
||
| // Set bus mode | ||
| switch (settings.getBusMode()) { | ||
| case SPI_CONTROLLER: | ||
| break; | ||
| case SPI_PERIPHERAL: | ||
| mode |= SPI_OP_MODE_SLAVE; | ||
| break; | ||
| } | ||
|
|
||
| // Set data format | ||
| switch (settings.getBitOrder()) { | ||
| case LSBFIRST: | ||
| mode |= SPI_TRANSFER_LSB; | ||
| break; | ||
| case MSBFIRST: | ||
| mode |= SPI_TRANSFER_MSB; | ||
| break; | ||
| } | ||
|
|
||
| // Set data mode | ||
| switch (settings.getDataMode()) { | ||
| case SPI_MODE0: | ||
| break; | ||
| case SPI_MODE1: | ||
| mode |= SPI_MODE_CPHA; | ||
| break; | ||
| case SPI_MODE2: | ||
| mode |= SPI_MODE_CPOL; | ||
| break; | ||
| case SPI_MODE3: | ||
| mode |= SPI_MODE_CPOL | SPI_MODE_CPHA; | ||
| break; | ||
| } | ||
|
|
||
| // Set SPI configuration structure for 8-bit transfers | ||
| memset(&config, 0, sizeof(struct spi_config)); | ||
| config.operation = mode | SPI_WORD_SET(8); | ||
| config.frequency = max(SPI_MIN_CLOCK_FREQUENCY, settings.getClockFreq()); | ||
|
|
||
| // Set SPI configuration structure for 16-bit transfers | ||
| memset(&config16, 0, sizeof(struct spi_config)); | ||
| config16.operation = mode | SPI_WORD_SET(16); | ||
| config16.frequency = max(SPI_MIN_CLOCK_FREQUENCY, settings.getClockFreq()); | ||
soburi marked this conversation as resolved.
Show resolved
Hide resolved
soburi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| void arduino::ZephyrSPI::endTransaction(void) { | ||
| spi_release(spi_dev, &config); | ||
| attachInterrupt(); | ||
| spi_release(spi_dev, &config); | ||
| } | ||
|
|
||
| void arduino::ZephyrSPI::attachInterrupt() { | ||
| for (size_t i = 0; i < interrupt_pos; ++i) { | ||
| enableInterrupt(interrupt[i]); | ||
| } | ||
| } | ||
|
|
||
| void arduino::ZephyrSPI::detachInterrupt() { | ||
| for (size_t i = 0; i < interrupt_pos; ++i) { | ||
| disableInterrupt(interrupt[i]); | ||
| } | ||
| } | ||
|
Comment on lines
111
to
115
|
||
|
|
||
| void arduino::ZephyrSPI::begin() {} | ||
| void arduino::ZephyrSPI::begin() { | ||
| } | ||
|
|
||
| void arduino::ZephyrSPI::end() {} | ||
| void arduino::ZephyrSPI::end() { | ||
| } | ||
|
|
||
| #if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), spis) | ||
| #if (DT_PROP_LEN(DT_PATH(zephyr_user), spis) > 1) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.