Skip to content

Commit 029bd83

Browse files
committed
libraries: SPI: Initial SPI implementation
- Tested controller mode on beagleconnect_freedom Signed-off-by: Ayush Singh <ayushdevel1325@gmail.com>
1 parent 95e608f commit 029bd83

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

libraries/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# SPDX-License-Identifier: Apache-2.0
22

33
add_subdirectory(Wire)
4+
add_subdirectory(SPI)

libraries/SPI/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
zephyr_include_directories(.)
3+
4+
if(NOT DEFINED ARDUINO_BUILD_PATH)
5+
zephyr_sources_ifdef(CONFIG_SPI SPI.cpp)
6+
endif()

libraries/SPI/SPI.cpp

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

libraries/SPI/SPI.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2024 Ayush Singh <ayush@beagleboard.org>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <Arduino.h>
10+
#include <api/HardwareSPI.h>
11+
#include <zephyr/drivers/spi.h>
12+
13+
#define SPR0 0
14+
#define SPR1 1
15+
#define CPHA 2
16+
#define CPOL 3
17+
#define MSTR 4
18+
#define DORD 5
19+
#define SPE 6
20+
#define SPIE 7
21+
22+
namespace arduino {
23+
class ZephyrSPI : public HardwareSPI {
24+
public:
25+
ZephyrSPI(const struct device *spi);
26+
27+
virtual uint8_t transfer(uint8_t data);
28+
virtual uint16_t transfer16(uint16_t data);
29+
virtual void transfer(void *buf, size_t count);
30+
31+
// Transaction Functions
32+
virtual void usingInterrupt(int interruptNumber);
33+
virtual void notUsingInterrupt(int interruptNumber);
34+
virtual void beginTransaction(SPISettings settings);
35+
virtual void endTransaction(void);
36+
37+
// SPI Configuration methods
38+
virtual void attachInterrupt();
39+
virtual void detachInterrupt();
40+
41+
virtual void begin();
42+
virtual void end();
43+
44+
private:
45+
const struct device *spi_dev;
46+
struct spi_config config;
47+
};
48+
49+
} // namespace arduino
50+
51+
extern arduino::ZephyrSPI SPI;
52+
/* Serial Peripheral Control Register */
53+
extern uint8_t SPCR;
54+
55+
using arduino::SPI_MODE0;
56+
using arduino::SPI_MODE1;
57+
using arduino::SPI_MODE2;
58+
using arduino::SPI_MODE3;
59+
using arduino::SPISettings;

0 commit comments

Comments
 (0)