Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

libraries/SPI: Add support for peripheral mode.#200

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
facchinm merged 1 commit intoarduino:mainfromiabdalkader:spi_controller_mode
Oct 2, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 44 additions & 56 deletionslibraries/SPI/SPI.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,66 +12,42 @@ arduino::ZephyrSPI::ZephyrSPI(const struct device *spi) : spi_dev(spi) {
}

uint8_tarduino::ZephyrSPI::transfer(uint8_t data) {
int ret;
uint8_t rx;
conststructspi_buf tx_buf = {.buf = &data, .len =sizeof(data)};
conststructspi_buf_set tx_buf_set = {
.buffers = &tx_buf,
.count =1,
};
conststructspi_buf rx_buf = {.buf = &rx, .len =sizeof(rx)};
conststructspi_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) {
uint8_t rx = data;
if (transfer(&rx,sizeof(rx), &config) <0) {
return0;
}

return rx;
}

uint16_tarduino::ZephyrSPI::transfer16(uint16_t data) {
int ret;
uint16_t rx;
conststructspi_buf tx_buf = {.buf = &data, .len =sizeof(data)};
conststructspi_buf_set tx_buf_set = {
.buffers = &tx_buf,
.count =1,
};
conststructspi_buf rx_buf = {.buf = &rx, .len =sizeof(rx)};
conststructspi_buf_set rx_buf_set = {
.buffers = &rx_buf,
.count =1,
};

ret =spi_transceive(spi_dev, &config16, &tx_buf_set, &rx_buf_set);
if (ret <0) {
uint16_t rx = data;
if (transfer(&rx,sizeof(rx), &config16) <0) {
return0;
}

return rx;
}

voidarduino::ZephyrSPI::transfer(void *buf,size_t count) {
int ret =transfer(buf, count, &config);
(void)ret;
}

intarduino::ZephyrSPI::transfer(void *buf,size_t len,conststructspi_config *config) {
int ret;
conststructspi_buf tx_buf = {.buf = buf, .len = count};

conststructspi_buf tx_buf = {.buf = buf, .len = len};
conststructspi_buf_set tx_buf_set = {
.buffers = &tx_buf,
.count =1,
};

uint8_t rx[count];
conststructspi_buf rx_buf = {.buf = &rx, .len = count};
conststructspi_buf rx_buf = {.buf = buf, .len = len};
conststructspi_buf_set rx_buf_set = {
.buffers = &rx_buf,
.count =1,
};

spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set);
memcpy(buf, rx, count);
returnspi_transceive(spi_dev, config, &tx_buf_set, &rx_buf_set);
}

voidarduino::ZephyrSPI::usingInterrupt(int interruptNumber) {
Expand All@@ -80,21 +56,31 @@ void arduino::ZephyrSPI::usingInterrupt(int interruptNumber) {
voidarduino::ZephyrSPI::notUsingInterrupt(int interruptNumber) {
}

#ifndef SPI_MIN_CLOCK_FEQUENCY
#defineSPI_MIN_CLOCK_FEQUENCY1000000
#endif

voidarduino::ZephyrSPI::beginTransaction(SPISettings settings) {
memset(&config,0,sizeof(config));
memset(&config16,0,sizeof(config16));
config.frequency = settings.getClockFreq() > SPI_MIN_CLOCK_FEQUENCY ? settings.getClockFreq() :
SPI_MIN_CLOCK_FEQUENCY;
config16.frequency = config.frequency;
uint32_t mode =0;

// Set bus mode
switch (settings.getBusMode()) {
case SPI_CONTROLLER:
break;
case SPI_PERIPHERAL:
mode |= SPI_OP_MODE_SLAVE;
break;
}

auto mode = SPI_MODE_CPOL | SPI_MODE_CPHA;
// 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:
mode =0;
break;
case SPI_MODE1:
mode = SPI_MODE_CPHA;
Expand All@@ -106,12 +92,16 @@ void arduino::ZephyrSPI::beginTransaction(SPISettings settings) {
mode = SPI_MODE_CPOL | SPI_MODE_CPHA;
break;
}
config.operation =SPI_WORD_SET(8) |
(settings.getBitOrder() == MSBFIRST ? SPI_TRANSFER_MSB : SPI_TRANSFER_LSB) |
mode;
config16.operation =
SPI_WORD_SET(16) |
(settings.getBitOrder() == MSBFIRST ? SPI_TRANSFER_MSB : SPI_TRANSFER_LSB) | mode;

// Set SPI configuration structure for 8-bit transfers
memset(&config,0,sizeof(structspi_config));
config.operation = mode |SPI_WORD_SET(8);
config.frequency =max(SPI_MIN_CLOCK_FEQUENCY, settings.getClockFreq());

// Set SPI configuration structure for 16-bit transfers
memset(&config16,0,sizeof(structspi_config));
config16.operation = mode |SPI_WORD_SET(16);
config16.frequency =max(SPI_MIN_CLOCK_FEQUENCY, settings.getClockFreq());
}

voidarduino::ZephyrSPI::endTransaction(void) {
Expand All@@ -125,8 +115,6 @@ void arduino::ZephyrSPI::detachInterrupt() {
}

voidarduino::ZephyrSPI::begin() {
beginTransaction(SPISettings());
endTransaction();
}

voidarduino::ZephyrSPI::end() {
Expand Down
11 changes: 11 additions & 0 deletionslibraries/SPI/SPI.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,14 @@
#define SPE 6
#define SPIE 7

#define SPI_HAS_PERIPHERAL_MODE (1)

// TODO:
// This depends on the clock settings, can't be used for all boards.
#ifndef SPI_MIN_CLOCK_FEQUENCY
#define SPI_MIN_CLOCK_FEQUENCY 1000000
#endif

/* Count the number of GPIOs for limit of number of interrupts */
#define INTERRUPT_HELPER(n, p, i) 1
#define INTERRUPT_COUNT \
Expand DownExpand Up@@ -50,6 +58,9 @@ class ZephyrSPI : public HardwareSPI {
virtual void begin();
virtual void end();

private:
int transfer(void *buf, size_t len, const struct spi_config *config);

protected:
const struct device *spi_dev;
struct spi_config config;
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp