|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Ayush Singh <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "SPI.h" |
| 8 | +#include <zephyr/kernel.h> |
| 9 | + |
| 10 | +/* Serial Peripheral Control Register */ |
| 11 | +uint8_t SPCR; |
| 12 | + |
| 13 | +arduino::ZephyrSPI::ZephyrSPI(const struct device *spi) : spi_dev(spi) {} |
| 14 | + |
| 15 | +uint8_t arduino::ZephyrSPI::transfer(uint8_t data) { |
| 16 | + int ret; |
| 17 | + uint8_t rx; |
| 18 | + const struct spi_buf tx_buf = {.buf = &data, .len = sizeof(data)}; |
| 19 | + const struct spi_buf_set tx_buf_set = { |
| 20 | + .buffers = &tx_buf, |
| 21 | + .count = 1, |
| 22 | + }; |
| 23 | + const struct spi_buf rx_buf = {.buf = &rx, .len = sizeof(rx)}; |
| 24 | + const struct spi_buf_set rx_buf_set = { |
| 25 | + .buffers = &rx_buf, |
| 26 | + .count = 1, |
| 27 | + }; |
| 28 | + |
| 29 | + ret = spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set); |
| 30 | + if (ret < 0) { |
| 31 | + return 0; |
| 32 | + } |
| 33 | + |
| 34 | + return rx; |
| 35 | +} |
| 36 | + |
| 37 | +uint16_t arduino::ZephyrSPI::transfer16(uint16_t data) { |
| 38 | + int ret; |
| 39 | + uint16_t rx; |
| 40 | + const struct spi_buf tx_buf = {.buf = &data, .len = sizeof(data)}; |
| 41 | + const struct spi_buf_set tx_buf_set = { |
| 42 | + .buffers = &tx_buf, |
| 43 | + .count = 1, |
| 44 | + }; |
| 45 | + const struct spi_buf rx_buf = {.buf = &rx, .len = sizeof(rx)}; |
| 46 | + const struct spi_buf_set rx_buf_set = { |
| 47 | + .buffers = &rx_buf, |
| 48 | + .count = 1, |
| 49 | + }; |
| 50 | + |
| 51 | + ret = spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set); |
| 52 | + if (ret < 0) { |
| 53 | + return 0; |
| 54 | + } |
| 55 | + |
| 56 | + return rx; |
| 57 | +} |
| 58 | + |
| 59 | +void arduino::ZephyrSPI::transfer(void *buf, size_t count) { |
| 60 | + int ret; |
| 61 | + const struct spi_buf tx_buf = {.buf = &buf, .len = count}; |
| 62 | + const struct spi_buf_set tx_buf_set = { |
| 63 | + .buffers = &tx_buf, |
| 64 | + .count = 1, |
| 65 | + }; |
| 66 | + |
| 67 | + ret = spi_write(spi_dev, &config, &tx_buf_set); |
| 68 | + if (ret < 0) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + ret = spi_read(spi_dev, &config, &tx_buf_set); |
| 73 | + if (ret < 0) { |
| 74 | + return; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void arduino::ZephyrSPI::usingInterrupt(int interruptNumber) {} |
| 79 | + |
| 80 | +void arduino::ZephyrSPI::notUsingInterrupt(int interruptNumber) {} |
| 81 | + |
| 82 | +void arduino::ZephyrSPI::beginTransaction(SPISettings settings) { |
| 83 | + memset(&config, 0, sizeof(config)); |
| 84 | + config.frequency = settings.getClockFreq(); |
| 85 | + config.operation = ((settings.getBitOrder() ^ 1) << 4) | |
| 86 | + (settings.getDataMode() << 1) | ((SPCR >> MSTR) & 1) | |
| 87 | + SPI_WORD_SET(8); |
| 88 | +} |
| 89 | + |
| 90 | +void arduino::ZephyrSPI::endTransaction(void) { spi_release(spi_dev, &config); } |
| 91 | + |
| 92 | +void arduino::ZephyrSPI::attachInterrupt() {} |
| 93 | + |
| 94 | +void arduino::ZephyrSPI::detachInterrupt() {} |
| 95 | + |
| 96 | +void arduino::ZephyrSPI::begin() {} |
| 97 | + |
| 98 | +void arduino::ZephyrSPI::end() {} |
| 99 | + |
| 100 | +arduino::ZephyrSPI |
| 101 | + SPI(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), spis, 0))); |
0 commit comments