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

Commit560c4a5

Browse files
authored
Merge pull request#200 from iabdalkader/spi_controller_mode
libraries/SPI: Add support for peripheral mode.
2 parents335f835 +cdb608b commit560c4a5

File tree

2 files changed

+55
-56
lines changed

2 files changed

+55
-56
lines changed

‎libraries/SPI/SPI.cpp‎

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,42 @@ arduino::ZephyrSPI::ZephyrSPI(const struct device *spi) : spi_dev(spi) {
1212
}
1313

1414
uint8_tarduino::ZephyrSPI::transfer(uint8_t data) {
15-
int ret;
16-
uint8_t rx;
17-
conststructspi_buf tx_buf = {.buf = &data, .len =sizeof(data)};
18-
conststructspi_buf_set tx_buf_set = {
19-
.buffers = &tx_buf,
20-
.count =1,
21-
};
22-
conststructspi_buf rx_buf = {.buf = &rx, .len =sizeof(rx)};
23-
conststructspi_buf_set rx_buf_set = {
24-
.buffers = &rx_buf,
25-
.count =1,
26-
};
27-
28-
ret =spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set);
29-
if (ret <0) {
15+
uint8_t rx = data;
16+
if (transfer(&rx,sizeof(rx), &config) <0) {
3017
return0;
3118
}
32-
3319
return rx;
3420
}
3521

3622
uint16_tarduino::ZephyrSPI::transfer16(uint16_t data) {
37-
int ret;
38-
uint16_t rx;
39-
conststructspi_buf tx_buf = {.buf = &data, .len =sizeof(data)};
40-
conststructspi_buf_set tx_buf_set = {
41-
.buffers = &tx_buf,
42-
.count =1,
43-
};
44-
conststructspi_buf rx_buf = {.buf = &rx, .len =sizeof(rx)};
45-
conststructspi_buf_set rx_buf_set = {
46-
.buffers = &rx_buf,
47-
.count =1,
48-
};
49-
50-
ret =spi_transceive(spi_dev, &config16, &tx_buf_set, &rx_buf_set);
51-
if (ret <0) {
23+
uint16_t rx = data;
24+
if (transfer(&rx,sizeof(rx), &config16) <0) {
5225
return0;
5326
}
54-
5527
return rx;
5628
}
5729

5830
voidarduino::ZephyrSPI::transfer(void *buf,size_t count) {
31+
int ret =transfer(buf, count, &config);
32+
(void)ret;
33+
}
34+
35+
intarduino::ZephyrSPI::transfer(void *buf,size_t len,conststructspi_config *config) {
5936
int ret;
60-
conststructspi_buf tx_buf = {.buf = buf, .len = count};
37+
38+
conststructspi_buf tx_buf = {.buf = buf, .len = len};
6139
conststructspi_buf_set tx_buf_set = {
6240
.buffers = &tx_buf,
6341
.count =1,
6442
};
6543

66-
uint8_t rx[count];
67-
conststructspi_buf rx_buf = {.buf = &rx, .len = count};
44+
conststructspi_buf rx_buf = {.buf = buf, .len = len};
6845
conststructspi_buf_set rx_buf_set = {
6946
.buffers = &rx_buf,
7047
.count =1,
7148
};
7249

73-
spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set);
74-
memcpy(buf, rx, count);
50+
returnspi_transceive(spi_dev, config, &tx_buf_set, &rx_buf_set);
7551
}
7652

7753
voidarduino::ZephyrSPI::usingInterrupt(int interruptNumber) {
@@ -80,21 +56,31 @@ void arduino::ZephyrSPI::usingInterrupt(int interruptNumber) {
8056
voidarduino::ZephyrSPI::notUsingInterrupt(int interruptNumber) {
8157
}
8258

83-
#ifndef SPI_MIN_CLOCK_FEQUENCY
84-
#defineSPI_MIN_CLOCK_FEQUENCY1000000
85-
#endif
86-
8759
voidarduino::ZephyrSPI::beginTransaction(SPISettings settings) {
88-
memset(&config,0,sizeof(config));
89-
memset(&config16,0,sizeof(config16));
90-
config.frequency = settings.getClockFreq() > SPI_MIN_CLOCK_FEQUENCY ? settings.getClockFreq() :
91-
SPI_MIN_CLOCK_FEQUENCY;
92-
config16.frequency = config.frequency;
60+
uint32_t mode =0;
61+
62+
// Set bus mode
63+
switch (settings.getBusMode()) {
64+
case SPI_CONTROLLER:
65+
break;
66+
case SPI_PERIPHERAL:
67+
mode |= SPI_OP_MODE_SLAVE;
68+
break;
69+
}
9370

94-
auto mode = SPI_MODE_CPOL | SPI_MODE_CPHA;
71+
// Set data format
72+
switch (settings.getBitOrder()) {
73+
case LSBFIRST:
74+
mode |= SPI_TRANSFER_LSB;
75+
break;
76+
case MSBFIRST:
77+
mode |= SPI_TRANSFER_MSB;
78+
break;
79+
}
80+
81+
// Set data mode
9582
switch (settings.getDataMode()) {
9683
case SPI_MODE0:
97-
mode =0;
9884
break;
9985
case SPI_MODE1:
10086
mode = SPI_MODE_CPHA;
@@ -106,12 +92,16 @@ void arduino::ZephyrSPI::beginTransaction(SPISettings settings) {
10692
mode = SPI_MODE_CPOL | SPI_MODE_CPHA;
10793
break;
10894
}
109-
config.operation =SPI_WORD_SET(8) |
110-
(settings.getBitOrder() == MSBFIRST ? SPI_TRANSFER_MSB : SPI_TRANSFER_LSB) |
111-
mode;
112-
config16.operation =
113-
SPI_WORD_SET(16) |
114-
(settings.getBitOrder() == MSBFIRST ? SPI_TRANSFER_MSB : SPI_TRANSFER_LSB) | mode;
95+
96+
// Set SPI configuration structure for 8-bit transfers
97+
memset(&config,0,sizeof(structspi_config));
98+
config.operation = mode |SPI_WORD_SET(8);
99+
config.frequency =max(SPI_MIN_CLOCK_FEQUENCY, settings.getClockFreq());
100+
101+
// Set SPI configuration structure for 16-bit transfers
102+
memset(&config16,0,sizeof(structspi_config));
103+
config16.operation = mode |SPI_WORD_SET(16);
104+
config16.frequency =max(SPI_MIN_CLOCK_FEQUENCY, settings.getClockFreq());
115105
}
116106

117107
voidarduino::ZephyrSPI::endTransaction(void) {
@@ -125,8 +115,6 @@ void arduino::ZephyrSPI::detachInterrupt() {
125115
}
126116

127117
voidarduino::ZephyrSPI::begin() {
128-
beginTransaction(SPISettings());
129-
endTransaction();
130118
}
131119

132120
voidarduino::ZephyrSPI::end() {

‎libraries/SPI/SPI.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
#defineSPE6
2323
#defineSPIE7
2424

25+
#defineSPI_HAS_PERIPHERAL_MODE (1)
26+
27+
// TODO:
28+
// This depends on the clock settings, can't be used for all boards.
29+
#ifndef SPI_MIN_CLOCK_FEQUENCY
30+
#defineSPI_MIN_CLOCK_FEQUENCY1000000
31+
#endif
32+
2533
/* Count the number of GPIOs for limit of number of interrupts*/
2634
#defineINTERRUPT_HELPER(n, p, i)1
2735
#defineINTERRUPT_COUNT \
@@ -50,6 +58,9 @@ class ZephyrSPI : public HardwareSPI {
5058
virtualvoidbegin();
5159
virtualvoidend();
5260

61+
private:
62+
inttransfer(void *buf,size_t len,conststructspi_config *config);
63+
5364
protected:
5465
conststructdevice *spi_dev;
5566
structspi_config config;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp